Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to draw 2d text in a QGLWidget
Forum Updated to NodeBB v4.3 + New Features

How to draw 2d text in a QGLWidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    julio jerez
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      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]

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        julio jerez
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          baysmith
          wrote on last edited by
          #4

          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

          Nokia Certified Qt Specialist.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Franzk
            wrote on last edited by
            #5

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

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • J Offline
              J Offline
              julio jerez
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved