How to draw 2d text in a QGLWidget



  • Hi I am trying to make a QGLWidget for a 3d viewer,
    At the very basic level my class looks like this.

    @DemoEntityManager::DemoEntityManager(QWidget* const parent)
    :QGLWidget (parent)
    ,m_font()
    //,m_font("Arial")
    {
    // Create the main Camera
    m_camera = new DemoCamera();

    // initialized the physics world for the new scene
    Cleanup ();

    // Start rendering after the system is set properly
    ContinueExecution();
    }

    void DemoEntityManager::paintGL()
    {
    glClearColor (0.5f, 0.5f, 0.5f, 0.0f );
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    renderText (4, 4, "This is a test", m_font);
    glFlush();
    }@

    However no mater what I do I can no get the ext to show on the screen, what I am doin wrong.

    I windows the way I do this is by creation and array display List wih on for diaply lest mtaching ever font, windo come with spacil funtion to do that,
    At typical implemantaion will look like this

    @GLuint GLContext::BuildFont()
    {
    HFONT font = CreateFont(
    20, // nHeight
    0, // nWidth
    0, // nEscapement
    0, // nOrientation
    FW_BOLD, // nWeight
    FALSE, // bItalic
    FALSE, // bUnderline
    0, // cStrikeOut
    ANSI_CHARSET, // nCharSet
    OUT_DEFAULT_PRECIS, // nOutPrecision
    CLIP_DEFAULT_PRECIS, // nClipPrecision
    DEFAULT_QUALITY, // nQuality
    DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
    _T("Arial")); // lpszFacename

    // only 96 usable ascii for performance in some low end system
    GLuint displayLists = glGenLists(96);

    // remember the old font
    HFONT oldfont = (HFONT)SelectObject (m_hDC, font);
    wglUseFontBitmaps(m_hDC, 32, 96, displayLists);
    SelectObject(m_hDC, oldfont);
    DeleteObject(font);

    return displayLists;
    }@

    Then to print any text, is just a matter to call the display list with the array on text as an array of display list starting from the base,
    I assuming Qt does something similar but I cannot figure it out,
    Can someone help me?



  • The advice from "QGLWidget":http://doc.trolltech.com/latest/qglwidget.html#renderText docs:

    [quote]"Overpaint":http://doc.trolltech.com/latest/opengl-overpainting.html with QPainter::drawText() instead.[/quote]



  • I go it, thank you.

    I have anothe quetsion related to this and peroformance.
    do you know if the 2d text is drawn using Open Gl command or is by using the drawing funtionality.

    one of the big problems in many graphics apps is that bitmap fonts becomes the slower part of the application, you can see that for example when ising SDL ot GLUT.
    It is always better to make fonts by a using a true font and building a display List for each character.



  • Another way is using a QPainter to draw to a texture which is displayed with quads. This is the approach I use in MIFit. You can see the code here:

    http://gitorious.org/mifit/mifit/blobs/master/libs/opengl/Text.h
    http://gitorious.org/mifit/mifit/blobs/master/libs/opengl/Text.cpp



  • QPainter uses openGL calls to render the text. (It's a gl widget after all.)



  • Thanks I saw that, and I have what I want aleady.
    QPaint provides lots of 2d functionality, it is awesome.

    Thanks you for the help


Log in to reply
 

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