Ejemplo n.º 1
0
/*!
    \internal
*/
void QGLView::initializeGL()
{
    d->logEnter("QGLView::initializeGL");
    QGLPainter painter;
    painter.begin();

    // Set the default depth buffer options.
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);
#if defined(QT_OPENGL_ES)
    glDepthRangef(0.0f, 1.0f);
#else
    glDepthRange(0.0f, 1.0f);
#endif

    // Set the default blend options.
    qt_gl_BlendColor(0, 0, 0, 0);
    qt_gl_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    qt_gl_BlendEquation(GL_FUNC_ADD);

    painter.setCullFaces(QGL::CullDisabled);
    initializeGL(&painter);
    d->logLeave("QGLView::initializeGL");
}
Ejemplo n.º 2
0
/*!
    \internal
*/
void QGLView::initializeGL()
{
    d->logEnter("QGLView::initializeGL");
    QGLPainter painter;
    painter.begin();

    // Set the default depth buffer options.
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);
#if defined(QT_OPENGL_ES)
    glDepthRangef(0.0f, 1.0f);
#else
    glDepthRange(0.0f, 1.0f);
#endif

    // Set the default blend options.
    if (painter.hasOpenGLFeature(QOpenGLFunctions::BlendColor))
        painter.glBlendColor(0, 0, 0, 0);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    if (painter.hasOpenGLFeature(QOpenGLFunctions::BlendEquation))
        painter.glBlendEquation(GL_FUNC_ADD);
    else if (painter.hasOpenGLFeature(QOpenGLFunctions::BlendEquationSeparate))
        painter.glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);

    glDisable(GL_CULL_FACE);
    initializeGL(&painter);
    d->logLeave("QGLView::initializeGL");
}
Ejemplo n.º 3
0
/*!
    The main paint function for the Viewport class.  It takes a  QPainter \a p, which performs the
    painting of objects in the 3d environment.

    The paint function is responsible for setting up the viewing transform, as well as other display
    options, before calling the draw function to perform the actual drawing of the scene.

    Note, currently \a style and \a widget are unused, but are reserved for later development.
*/
void Viewport::paint(QPainter *p, const QStyleOptionGraphicsItem * style, QWidget *widget)
{
    Q_UNUSED(style);

    if (!d->viewWidget)
        d->viewWidget = widget;
    d->needsPick = true;

    QGLPainter painter;
    if (!painter.begin(p)) {
        qWarning("GL graphics system is not active; cannot use 3D items");
        return;
    }

    // Initialize the objects in the scene if this is the first paint.
    if (!d->itemsInitialized) {
        initialize(0);
        initializeGL(&painter);
    }

    // Modify the GL viewport to only cover the extent of this QDeclarativeItem.
    QTransform transform = p->transform();
    painter.setViewport(QRect(qRound(transform.dx()), qRound(transform.dy()),
                              width(), height()));

    // Perform early drawing operations.
    earlyDraw(&painter);

    // Set up the camera the way QGLView would if we were using it.
    if (d->camera) {
        painter.setCamera(d->camera);
    } else {
        QGLCamera defCamera;
        painter.setCamera(&defCamera);
    }

    // Draw the Item3d children.
    draw(&painter);
    painter.setPicking(false);
}
Ejemplo n.º 4
0
void MapNode::updateFBO()
{
    QGLPainter painter;

    if (!painter.begin()) {
        qWarning() << "GL graphics system is not active, can not update FBO for map";
        return;
    }

    painter.pushSurface(m_fboSurface);

    glClearColor(0.9, 0.9, 0.9, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    painter.disableEffect();
#ifdef GL_RESCALE_NORMAL
    // Scale normals by a scale factor derived from modelview matrix.
    // Note: normals need to be unit length.
    glEnable(GL_RESCALE_NORMAL);
#endif

#if !defined(QT_OPENGL_ES_2)
    glShadeModel(GL_SMOOTH);
    glEnable(GL_MULTISAMPLE);
#endif

    // Set the default effect for the scene.
    painter.setStandardEffect(QGL::LitMaterial);
    painter.setFaceColor(QGL::AllFaces, Qt::white);

#if defined(QT_OPENGL_ES)
    glClearDepthf(1);
#else
    glClearDepth(1);
#endif

#if defined(GL_LINE_SMOOTH) && defined(GL_LINE_SMOOTH_HINT)
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
#endif

#ifdef QSGSHADEREFFECTSOURCE_AVAILABLE
    // Update any map objects that may have dirty textures
    for (int i = 0; i < mapItems_.count(); ++i) {
        mapItems_.at(i)->updateItem();
    }
#endif

    // No stereo rendering, set the eye as neutral
    painter.setEye(QGL::NoEye);
    // TODO this needs to be figured out (or confirmed as invalid thing).
    // Currently applied transforms for this Map3D object - how to get/apply current transforms?
    // QTransform transform = painter->combinedTransform();
    // Then we get the rectangle that is gotten by applying the QTransform on the rect
    // --> this is the viewport for Map3D
    // QRect viewport = transform.mapRect(boundingRect()).toRect();

    // boundingRect is in local coordinates. We need to map it to the scene coordinates
    // in order to render to correct area.

    if (m_map->glCamera()) {
        painter.setCamera(m_map->glCamera());
    } else {
        QGLCamera defCamera;
        painter.setCamera(&defCamera);
    }
    m_map->paintGL(&painter);

    restoreDefaults(&painter);

    // Draw the children items
    painter.popSurface();
    // QSG does not expect anyone to alter gl context state; restore defaults.
    // Default heaps of things, because we cannot be sure what the Qt3D internally
    // sets.

#if defined(QT_OPENGL_ES)
    glClearDepthf(0);
#else
    glClearDepth(0);
#endif

    painter.end();

    QSGGeometry::updateTexturedRectGeometry(&m_geometry,
                                            QRectF(QPointF(0, m_fbo->size().height()), QPointF(m_fbo->size().width(), 0)),
                                            QRectF(0, 0, 1, 1));
    delete m_texture;
    m_texture = new QSGPlainTexture();
    m_texture->setOwnsTexture(false);
    m_texture->setTextureSize(m_fbo->size());
    m_texture->setTextureId(m_fbo->texture());
    m_opaqueMaterial.setTexture(m_texture);
    m_material.setTexture(m_texture);

    markDirty(DirtyMaterial);
}
Ejemplo n.º 5
0
/*!
    \internal
*/
void QGLView::paintGL()
{
    d->logEnter("QGLView::paintGL");
    // We may need to regenerate the pick buffer on the next mouse event.
    d->pickBufferMaybeInvalid = true;

    // Paint the scene contents.
    QGLPainter painter;
    QGLAbstractSurface *surface;
    painter.begin();
    if ( (d->options & QGLView::ShowPicking) &&
            d->stereoType == QGLView::RedCyanAnaglyph) {
        // If showing picking, then render normally.  This really
        // only works if we aren't using hardware or double stereo.
        painter.setPicking(true);
        painter.clearPickObjects();
        painter.setEye(QGL::NoEye);
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.setPicking(false);
    } else if (d->camera->eyeSeparation() == 0.0f &&
               (surface = d->bothEyesSurface()) != 0) {
        // No camera separation, so render the same image into both buffers.
        painter.pushSurface(surface);
        painter.setEye(QGL::NoEye);
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.popSurface();
    } else {
        // Paint the scene twice, from the perspective of each camera.
        QSize size(this->size());
        painter.setEye(QGL::LeftEye);
        if (d->stereoType != QGLView::Hardware)
            earlyPaintGL(&painter);     // Clear both eyes at the same time.
        painter.pushSurface(d->leftEyeSurface(size));
        if (d->stereoType == QGLView::Hardware)
            earlyPaintGL(&painter);     // Clear the left eye only.
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        if (d->stereoType == QGLView::RedCyanAnaglyph)
            glClear(GL_DEPTH_BUFFER_BIT);
        painter.setEye(QGL::RightEye);
        painter.setSurface(d->rightEyeSurface(size));
        if (d->stereoType == QGLView::Hardware)
            earlyPaintGL(&painter);     // Clear the right eye only.
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.popSurface();
    }
    d->logLeave("QGLView::paintGL");
}
/*!
    Paints this GL graphics viewport onto \a painter, with the specified
    \a option and \a widget parameters.

    This override creates a QGLPainter for \a painter and passes
    it to paintGL().

    \sa paintGL()
*/
void QGLGraphicsViewportItem::paint
    (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_D(QGLGraphicsViewportItem);

    Q_UNUSED(option);
    Q_UNUSED(widget);

    if (d->rect.isEmpty())
        return;

    // Initialize a QGLPainter for the surface and bail out if not active.
    QGLPainter glpainter;
    if (!glpainter.begin(painter)) {
        qWarning("GL graphics system is not active; cannot use 3D items");
        return;
    }

    // Set up the GL viewport to limit drawing to the bounds of this item.
    QRect viewport = painter->deviceTransform().mapRect(rect()).toRect();
    QGLSubsurface surface(glpainter.currentSurface(), viewport);
    glpainter.pushSurface(&surface);

    // Set up the desired drawing options.
    glDisable(GL_CULL_FACE);
    d->setDefaults(&glpainter);
    if (d->backgroundColor.isValid()) {
        // We clear the background by drawing a triangle fan so
        // that the background color will blend with the underlying
        // screen content if it has an alpha component.
        glDisable(GL_DEPTH_TEST);
        if (d->backgroundColor.alpha() != 255)
            glEnable(GL_BLEND);
        else
            glDisable(GL_BLEND);
        QVector2DArray array;
        array.append(-1, -1);
        array.append(1, -1);
        array.append(1, 1);
        array.append(-1, 1);
        glpainter.projectionMatrix().setToIdentity();
        glpainter.modelViewMatrix().setToIdentity();
        glpainter.setStandardEffect(QGL::FlatColor);
        glpainter.setColor(d->backgroundColor);
        glpainter.setVertexAttribute(QGL::Position, array);
        glpainter.draw(QGL::TriangleFan, 4);
    }
    glClear(GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);

    // Apply the camera.
    glpainter.setEye(QGL::NoEye);
    glpainter.setCamera(d->camera);

    // Paint the GL contents.
    paintGL(&glpainter);

    // Disable the current drawing effect so that QGLPainter will
    // forcibly update the GL context the next time QGLPainter is used.
    glpainter.disableEffect();

    // Try to restore the GL state to something paint-engine compatible.
    glDisable(GL_CULL_FACE);
    d->setDefaults(&glpainter);
    glDisable(GL_DEPTH_TEST);

    glpainter.popSurface();
}
Ejemplo n.º 7
0
/*!
    \internal
*/
void QGLView::paintGL()
{
    d->logEnter("QGLView::paintGL");
    // We may need to regenerate the pick buffer on the next mouse event.
    d->pickBufferMaybeInvalid = true;

    // Paint the scene contents.
    QGLPainter painter;
    painter.begin();
    painter.resetViewport();
    if (d->options & QGLView::ShowPicking &&
            d->stereoType == QGLView::RedCyanAnaglyph) {
        // If showing picking, then render normally.  This really
        // only works if we aren't using hardware or double stereo.
        painter.setPicking(true);
        painter.clearPickObjects();
        painter.setEye(QGL::NoEye);
        earlyPaintGL(&painter);
        painter.setCamera(d->camera);
        paintGL(&painter);
        painter.setPicking(false);
    } else if (d->camera->eyeSeparation() == 0.0f &&
                    (d->stereoType == QGLView::Hardware ||
                     d->stereoType == QGLView::RedCyanAnaglyph)) {
        // No camera separation, so draw without stereo.  If the hardware
        // has stereo buffers, then render the same image into both buffers.
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
        if (d->stereoType == QGLView::Hardware) {
            bool doubleBuffered = doubleBuffer();
            if (doubleBuffered)
                glDrawBuffer(GL_BACK);
            else
                glDrawBuffer(GL_FRONT);
            painter.setEye(QGL::NoEye);
            earlyPaintGL(&painter);
            painter.setCamera(d->camera);
            paintGL(&painter);
        } else
#endif
        {
            painter.setEye(QGL::NoEye);
            earlyPaintGL(&painter);
            painter.setCamera(d->camera);
            paintGL(&painter);
        }
    } else {
        // Paint the scene twice, from the perspective of each camera.
        // In RedCyanAnaglyph mode, the color mask is set each time to only
        // extract the color planes that we want to see through that eye.
        if (d->stereoType == QGLView::RedCyanAnaglyph) {
            painter.setEye(QGL::LeftEye);
            glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
            earlyPaintGL(&painter);

            glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
            painter.setCamera(d->camera);
            paintGL(&painter);

            painter.setEye(QGL::RightEye);
            glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
            glClear(GL_DEPTH_BUFFER_BIT);
            painter.setCamera(d->camera);
            paintGL(&painter);

            glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
        } else if (d->stereoType != QGLView::Hardware) {
            // Render the stereo images into the two halves of the window.
            QSize sz = size();
            painter.setEye(QGL::LeftEye);
            qt_qglview_left_viewport(&painter, sz, d->stereoType);
            earlyPaintGL(&painter);
            painter.setCamera(d->camera);
            paintGL(&painter);
            painter.setEye(QGL::RightEye);
            qt_qglview_right_viewport(&painter, sz, d->stereoType);
            painter.setCamera(d->camera);
            paintGL(&painter);
            painter.setViewportOffset(QPoint(0, 0));
        }
#if defined(GL_BACK_LEFT) && defined(GL_BACK_RIGHT)
        else {
            bool doubleBuffered = doubleBuffer();
            if (doubleBuffered)
                glDrawBuffer(GL_BACK_LEFT);
            else
                glDrawBuffer(GL_FRONT_LEFT);
            painter.setEye(QGL::LeftEye);
            earlyPaintGL(&painter);
            painter.setCamera(d->camera);
            paintGL(&painter);

            if (doubleBuffered)
                glDrawBuffer(GL_BACK_RIGHT);
            else
                glDrawBuffer(GL_FRONT_RIGHT);
            painter.setEye(QGL::RightEye);
            earlyPaintGL(&painter);
            painter.setCamera(d->camera);
            paintGL(&painter);
        }
#endif
    }
    d->logLeave("QGLView::paintGL");
}