Results 1 to 8 of 8

Thread: 2D OpenGL text rendering with existing app

  1. #1
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default 2D OpenGL text rendering with existing app

    I have an existing C++ MFC app which renders to a 3D OpenGL view.

    I'd like to use Qt to render 2D text over the 3D scene. I found the below example and have been looking through it.

    http://qt.nokia.com/doc/4.6/opengl-2dpainting.html

    My question is, how do I setup Qt to render to my existing OpenGL context? I don't want Qt to create a new context for me I already have a context and I want Qt to render to my existing context.

    The example seems to come down to the QGLContext class. But I didn't see any way to initialize that class to point to an existing GL context.

    Basically, although I don't want to convert everything over to Qt yet, I'd like to use Qt to render 2D text within my current application framework (C++ MFC).

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: 2D OpenGL text rendering with existing app

    My question is, how do I setup Qt to render to my existing OpenGL context?
    I don't know, but if your main goal is just to write 2D text to your 3D scene than you can use the code below, is from an example of the "Opengl Superbible" book. It works fine with me. Also you can check this site, lots of information about opengl and Qt.

    Basically, although I don't want to convert everything over to Qt yet, I'd like to use Qt to render 2D text within my current application framework (C++ MFC).
    MFC ... I've been there and since I meet Qt never use it again


    Qt Code:
    1. #define MAX_STRING 1024
    2.  
    3. GLuint SmallFont, /* Small raster font */
    4. SymbolFont, /*symbol font*/
    5. MediumFont, /* Medium raster font */
    6. LargeFont; /* Large raster font */
    7.  
    8.  
    9. GLuint FontCreateBitmaps(HDC hdc, char *typeface, int height,
    10. int weight, DWORD italic);
    11. GLuint FontCreateOutlines(HDC hdc, char *typeface, int height,
    12. int weight, DWORD italic);
    13. void FontDelete(GLuint font);
    14.  
    15. void FontPrintf(GLuint font, char *format, ...);
    16. void FontPuts(GLuint font, char *s);
    17.  
    18.  
    19.  
    20. GLuint
    21. FontCreateBitmaps(HDC hdc, /* I - Device Context */
    22. char *typeface, /* I - Font specification */
    23. int height, /* I - Font height/size in pixels */
    24. int weight, /* I - Weight of font (bold, etc) */
    25. DWORD italic) /* I - Text is italic */
    26. {
    27. GLuint base; /* Base display list for font */
    28. HFONT font; /* Windows font ID */
    29.  
    30.  
    31. //if ((base = glGenLists(96)) == 0)
    32. if ((base = glGenLists(224)) == 0)
    33. return (0);
    34.  
    35. if (stricmp(typeface, "symbol") == 0)
    36. font = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,
    37. SYMBOL_CHARSET, OUT_TT_PRECIS,
    38. CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
    39. DEFAULT_PITCH, typeface);
    40. else
    41. font = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,
    42. ANSI_CHARSET, OUT_TT_PRECIS,
    43. CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
    44. DEFAULT_PITCH, typeface);
    45.  
    46. /*font = CreateFont(height, 0, 0, 0, weight, italic, FALSE, FALSE,
    47.   UNICODE_CHARSET, OUT_TT_PRECIS,
    48.   CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
    49.   DEFAULT_PITCH, typeface);
    50. */
    51. SelectObject(hdc, font);
    52.  
    53. //wglUseFontBitmaps(hdc, 32, 96, base);
    54. wglUseFontBitmaps(hdc, 32, 224, base);
    55.  
    56. return (base);
    57. }
    58.  
    59.  
    60. /*
    61.  * 'FontDelete()' - Delete the OpenGL display lists used for a font.
    62.  */
    63.  
    64. void
    65. FontDelete(GLuint font) /* I - Font to delete */
    66. {
    67. if (font == 0)
    68. return;
    69.  
    70. // glDeleteLists(font, 96);
    71. glDeleteLists(font, 224);
    72. }
    73.  
    74.  
    75. /*
    76.  * 'FontPuts()' - Display a string using the specified font.
    77.  */
    78.  
    79. void
    80. FontPuts(GLuint font, /* I - Font to use */
    81. char *s) /* I - String to display */
    82. {
    83. if (font == 0)
    84. return;
    85.  
    86. if (s == NULL)
    87. return;
    88.  
    89. glPushAttrib(GL_LIST_BIT);
    90. glListBase(font - 32);
    91. glCallLists(strlen(s), GL_UNSIGNED_BYTE, s);
    92. glPopAttrib();
    93. }
    94.  
    95.  
    96.  
    97. /*
    98.  * 'FontPrintf()' - Display a formatted string using the specified font.
    99.  */
    100.  
    101. void
    102. FontPrintf(GLuint font, /* I - Font to use */
    103. char *format, /* I - printf() style format string */
    104. ...) /* I - Other arguments as necessary */
    105. {
    106. va_list ap; /* Argument pointer */
    107. char s[MAX_STRING + 1]; /* Output string */
    108.  
    109.  
    110. if (format == NULL)
    111. return;
    112.  
    113. va_start(ap, format);
    114. vsprintf(s, format, ap);
    115. va_end(ap);
    116.  
    117. FontPuts(font, s);
    118. }
    119.  
    120.  
    121. //finnally to draw the text inside your function
    122.  
    123. {
    124. SmallFont = FontCreateBitmaps(m_hDC, "Courier", 12, FW_BOLD, 0);
    125. double x=10;
    126. double y=20;
    127. double z=15;
    128.  
    129. glRasterPos3d(x , y, z);
    130. FontPuts(SmallFont, "-x");
    131.  
    132. }
    To copy to clipboard, switch view to plain text mode 
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  3. #3
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2D OpenGL text rendering with existing app

    if your main goal is just to write 2D text to your 3D scene than you can use the code below
    My experience with the GL text output methods you referred to is that you don't get anti-aliasing and so the text doesn't look very good.
    MFC ... I've been there and since I meet Qt never use it again
    That's great, and perhaps thats eventually the way to go. But from a standpoint of cost, I was hoping I could have Qt perform OpenGL rendering using my existing GL rendering context. I mentioned MFC for completeness, but really, the question is that I have a GL rendering context setup and ready to render, so how do I get Qt to just use that and do its GL rendering.

    I've seen examples of Qt doing 2D rendering in OpenGL and it looks FAR nicer than what I've achieved using the font techniques you included in your reply. So I'm wondering how I can leverage that Qt OpenGL rendering in my existing app.

  4. #4
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: 2D OpenGL text rendering with existing app

    I dont think this will work for you, but in a QGLWidget I actually just render 2D text with

    Qt Code:
    1. QFont font;
    2. font.setPointSize(10);
    3. renderText(x,y,z,"some text",font);
    To copy to clipboard, switch view to plain text mode 

    this is somewath powerfull because QFont gives a lot of text format options. I have no experience in using Qt with MFC, I am assuming you're using visual studio integration, so I dont know if this renderText will work for you. Good look.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  5. #5
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2D OpenGL text rendering with existing app

    I noticed that sort of text rendering code in the 2D GL example. But if you look at the other classes involved you end up needing a QGLContext class which houses the OpenGL rendering context.

    The problem is, I can't see a way to tell QGLContext to just use my existing context.

    MFC is not the issue and I don't think the issue has anything to do with MFC.

    Its just that I have already setup an OpenGL rendering context and I'd like Qt to render using that context. Getting Qt to render GL seems to hinge on the QGLContext class, but I'm not sure how to set that up to use an existing GL context.

  6. #6
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: 2D OpenGL text rendering with existing app

    Perhaps a workaround would be to use Qt opengl context to render text to a image, than import that image to your current gl context. Of course this could have performance issues.
    Or depending the dimension of your project, with more or less work, you could instead of triyng to draw to an existent context, adapt your existent gl methods to draw to the Qt opengl context.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  7. #7
    Join Date
    Mar 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2D OpenGL text rendering with existing app

    Maybe that's what I need to try -- See if I can just use Qt to create the GL rendering context instead of doing it directly with Windows APIs. I suspect that to create a Qt context it expects to create its own windowing surface as well. So the question just migrates to, ok how do I create a Qt GL context attached to my own window I'll take a look at it and see what I can find. If that doesn't work then maybe I'd have to have Qt create the window for me which might get messy because currently MFC creates the window. Not impossible I guess. Time to see how far down the rabbit hole this will go!

    Certainly for a new application (or if there was unlimited time and resources) just doing it all with Qt would be cleaner. I'm hoping I can leverage Qt for some snazzy GL rendering without some major conversion or overhaul of an existing app though.

  8. #8
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: 2D OpenGL text rendering with existing app

    I just remember some months ago googling the web I find some librarys do draw text in opengl. Dont remember witch library or witch site but perhaps
    you can find other ways to display better quality text than the methods of the previews posts. Than you would avoid messing with qt gl context.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

Similar Threads

  1. OpenGL rendering and threads
    By brcain in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2008, 09:45
  2. QGraphicsView + OpenGL -> Pixmap rendering issue
    By JoergRe in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2008, 18:41
  3. for the openGl to work should the direct rendering be enabled
    By babu198649 in forum General Programming
    Replies: 1
    Last Post: 27th April 2008, 15:22
  4. problem in rendering opengl on QGraphicsView
    By Sandip in forum Qt Programming
    Replies: 17
    Last Post: 15th April 2008, 08:27
  5. OpenGL rendering problem
    By spud in forum Qt Programming
    Replies: 5
    Last Post: 27th February 2007, 19:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.