[SOLVED] Drawing text on an QGLWidget (OGLES2.0)



  • Hi,

    I'm trying to draw text over a QGLWidget displaying OpenGL ES2.0 graphics. I'd prefer to do this using in-built Qt functions (e.g. QPainter) than have to write the text to a texture and display it on some trias.

    I've tried using a QPainter that is created with the QGLWidget as it's parent. This worked under Qt 4.6.2 but not under 4.7. Right now just creating the QPainter makes the whole screen revert to a blank screen. If I remove the QPainter statements the 3D graphics work okay.

    Please let me know if you have any ideas!

    @
    void CMyGLWidget::paintEvent(QPaintEvent *event){

    QPainter painter;

    bool result = painter.begin(this);

    painter.beginNativePainting();

    // Clear color and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set modelview-projection matrix
    program->setUniformValue("mvp_matrix", projection * matrix);

    // Tell OpenGL which VBOs to use
    glBindBuffer(GL_ARRAY_BUFFER, vboIds[2]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[3]);

    // Offset for position
    int offset = 0;

    // Tell OpenGL programmable pipeline how to locate vertex position data
    int vertexLocation = program->attributeLocation("a_position");
    program->enableAttributeArray(vertexLocation);
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*NUM_GLFLOAT_PER_VERTEX, (const void *)offset);

    // Offset for colour
    offset += sizeof(GLfloat)*3;

    // Tell OpenGL programmable pipeline how to locate vertex colour data
    int colLocation = program->attributeLocation("ourcolor");
    program->enableAttributeArray(colLocation);
    glVertexAttribPointer(colLocation, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*NUM_GLFLOAT_PER_VERTEX, (const void *)offset);

    // Offset for normal
    offset += sizeof(GLfloat)*3;

    // Tell OpenGL programmable pipeline how to locate vertex normal
    int normLocation = program->attributeLocation("normal");
    program->enableAttributeArray(normLocation);
    glVertexAttribPointer(normLocation, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*NUM_GLFLOAT_PER_VERTEX, (const void *)offset);

    // Enable depth buffer
    glEnable(GL_DEPTH_TEST);

    // Disable back face culling
    glDisable(GL_CULL_FACE);

    if (vert2Dsurf){
    glDrawElements(GL_TRIANGLES, numind2Dsurf, GL_UNSIGNED_INT, 0);
    }

    // Unbind the VBOs
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    painter.endNativePainting();

    painter.end();

    } // End paintEvent()
    @



  • What I do for drawing text on OpenGL canvas is I make a texture atlas of the alphabet and draw dynamically the characters as a "2D" texture using ortho projection.



  • Thanks for the advice - but I really want to render the text using a QPainter or similar to keep the class small and easy to maintain.



  • Good point. I tried to do this and couldnt get it working - that is, not on a desktop build at least. That's why I went the texture way - it's more hassle to implement but at least it's portable :)

    However it seems this should work - see the OpenGL Qt examples like Bulletdice, they have some overlay drawing on top of the QGLWidget.



  • I wasn't aware of that example, thanks - will have a look through code and see how it works. Thanks again.



  • I've tried the example, and it seems to do what I want, but my program is still showing a blank screen - it's very frustrating.

    I've tried looking through the example for differences but I haven't seen anything that think is important yet. Currently my program still shows a blank screen (of the clear background colour) and the GL 3D model is not present.

    I'll report back if I solve it...



  • Matti - thanks - no sooner did I post for more help than I spotted it i the example - that example helped a lot!

    I was missing program->bind() and program->release():

    @
    QPainter painter;
    painter.begin(this);
    painter.beginNativePainting();
    ...
    program->bind();
    [OpenGL drawing operations]
    program->release();
    painter.endNativePainting();
    [Painter operations]
    painter->end();
    glFinish();
    swapBuffers();
    @

    Thanks again!


Log in to reply
 

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