OpenGL not rendering properly



  • Not entirely sure why this isn't working. I suspect there is something subtly different about OpenGL in Qt that I'm not using correctly.

    I've Created a QDeclarativeView using a QGLWidget as the Viewport and a class derived from QGraphicsScene as the scene. With the below test code the view is properly cleared to blue but I can't see the polygon that I'm drawing. I'm able to run the test OpenGL applications fine.

    void OpenGLScene::drawBackground(QPainter *painter, const QRectF &rect)
    {
    float width = float(painter->device()->width());
    float height = float(painter->device()->height());

    painter->save();

    painter->beginNativePainting();

    glClearColor( 0.0f, 0.0f, 1.0f, 1.0f );
    glOrtho( -5.0f, 5.0f, -5.0f, 5.0f, -1.0f, 1.0f );
    glMatrixMode(GL_MODELVIEW);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin( GL_POLYGON );
    glColor4f(1.0, 1.0, 1.0, 1);
    glVertex2f( -5.0f, -5.0f );
    glVertex2f( 5.0f, -5.0f );
    glColor4f(0.0, 1.0, 0.0, 1);
    glVertex2f( 5.0f, 0.0f );
    glVertex2f( -5.0f, 0.0f );
    glEnd();

    painter->endNativePainting();
    glFlush();
    painter->restore();
    update(this->sceneRect());
    }



  • sorry, not in a position to test any code, but if this is all the opengl code then I would think you'd see something flying off the screen. First of all, i think the glOrtho() call should be made with the glMatrixMode as projection, since that's the projection transformation.

    Second of all, the ortho call is being made every frame, so what ever matrix is on the top of the stack, so it is just getting completely warped.

    tl;dr:
    @
    ClearTheScreenAlready();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(ORTHO_PARAMS_CONVENIENTLY_DEFINED_SOMEWHERE_ELSE);
    glMatrixMode(MODELVIEW);
    @

    While you are at it, i'm not so sure the matrices initialize to identity, so a glLoadIdentity() call when the model view matrix is up would maybe help. Not sure. anyhow, good luck.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.