Re: wxWidgets and OpenGL

108 views
Skip to first unread message

Leslie Newell

unread,
Mar 12, 2009, 8:12:47 AM3/12/09
to wx-u...@lists.wxwidgets.org
Don't try to draw anything on the DC of an OpenGl window. If you want
text you have to draw it in OpenGL.
GLUT<http://www.opengl.org/resources/libraries/glut/> has functions for
drawing text in OpenGl.

Les

Srinivaz111 wrote:
> Hello all,
> I am developing one FireFox plugin for Windows which creates one
> wxWidgets window with GL enabled(wxGLCanvas). I am drawing some 3d
> objects(using OpenGL functions) and some text on this window. The problem is
> that the text disappears sporadically. I mean when any message box or dialog
> box is opened and closed, the text goes. And again any message box is opened
> and closed the text appears.
> I do not know why this is happening. The OnPaint of the window consists
> of the following code.
>

Srinivaz111

unread,
Mar 12, 2009, 7:29:46 AM3/12/09
to wx-u...@lists.wxwidgets.org

Hello all,
I am developing one FireFox plugin for Windows which creates one
wxWidgets window with GL enabled(wxGLCanvas). I am drawing some 3d
objects(using OpenGL functions) and some text on this window. The problem is
that the text disappears sporadically. I mean when any message box or dialog
box is opened and closed, the text goes. And again any message box is opened
and closed the text appears.
I do not know why this is happening. The OnPaint of the window consists
of the following code.


wxPaintDC dc(this);

SetCurrent();

// Init OpenGL once, but after SetCurrent
if (!m_init)
{
InitGL();
m_init = true;
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 3.0f);
glMatrixMode(GL_MODELVIEW);


// clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if( m_gllist == 0 )
{
m_gllist = glGenLists( 1 );
glNewList( m_gllist, GL_COMPILE_AND_EXECUTE );

// draw six faces of a cube
glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glVertex3f( 0.5f, 0.5f, 0.5f); glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f,-0.5f, 0.5f); glVertex3f( 0.5f,-0.5f, 0.5f);

glNormal3f( 0.0f, 0.0f,-1.0f);
glVertex3f(-0.5f,-0.5f,-0.5f); glVertex3f(-0.5f, 0.5f,-0.5f);
glVertex3f( 0.5f, 0.5f,-0.5f); glVertex3f( 0.5f,-0.5f,-0.5f);

glNormal3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 0.5f, 0.5f, 0.5f); glVertex3f( 0.5f, 0.5f,-0.5f);
glVertex3f(-0.5f, 0.5f,-0.5f); glVertex3f(-0.5f, 0.5f, 0.5f);

glNormal3f( 0.0f,-1.0f, 0.0f);
glVertex3f(-0.5f,-0.5f,-0.5f); glVertex3f( 0.5f,-0.5f,-0.5f );
glVertex3f( 0.5f,-0.5f, 0.5f); glVertex3f(-0.5f,-0.5f, 0.5f );

glNormal3f( 1.0f, 0.0f, 0.0f);
glVertex3f( 0.5f, 0.5f, 0.5f); glVertex3f( 0.5f,-0.5f, 0.5f );
glVertex3f( 0.5f,-0.5f,-0.5f); glVertex3f( 0.5f, 0.5f,-0.5f);

glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f,-0.5f,-0.5f); glVertex3f(-0.5f ,-0.5f , 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f); glVertex3f(-0.5f, 0.5f ,-0.5f);

glEnd();

glEndList();
}
else
{
glCallList(m_gllist);
}

glFlush();
SwapBuffers();


int width, height;
GetClientSize(&width, &height);

dc.SetTextForeground( wxColour(255,255,0,128) );

wxCoord CharHeight = dc.GetCharHeight();

for (int i = 0; i < m_Index; i++)
{
dc.DrawText(m_strDisplayStrings[i].c_str(), 0, CharHeight*i);
}

dc.DrawText("Applet3", width/2-20, height/2);

I will be grateful to you if you tell me why the text is going on and off.


Thanks,
Srinu
--
View this message in context: http://www.nabble.com/wxWidgets-and-OpenGL-tp22473806p22473806.html
Sent from the wxWidgets - Users mailing list archive at Nabble.com.

Srinivaz111

unread,
Mar 12, 2009, 8:33:24 AM3/12/09
to wx-u...@lists.wxwidgets.org

Hi Les,
Thanks for the reply. I tried GLUT and found that GLUT has a stand alone
Message Loop. I mean I found that it can`t be used in other application. Is
there any way to use GLUT without using glutCreateWindow, glutMainLoop,
etc...? I understood that it requires a window(Created by glutCreateWindow)
where it draws. But what my question is can I draw in my window which is
already created using GLUT?

Thanks,
Srinu

> _______________________________________________
> wx-users mailing list
> wx-u...@lists.wxwidgets.org
> http://lists.wxwidgets.org/mailman/listinfo/wx-users
>
>

--
View this message in context: http://www.nabble.com/wxWidgets-and-OpenGL-tp22473806p22475393.html

Philippe Pignatta

unread,
Mar 12, 2009, 8:39:06 AM3/12/09
to wx-u...@lists.wxwidgets.org
GLUT is probably a really easy way to draw text in OpenGL, but it's
certainly not the only way to achieve this.
You can use (non-portable) functions like wglUseFontBitmaps () (under
Windows, or equivalent functions on other operating systems) to create
display lists of the different characters.
I don't think there is a wxWidgets function to portably do that, but I
may be wrong.

good luck

Srinivaz111 a écrit :

Carsten Fuchs

unread,
Mar 12, 2009, 8:55:53 AM3/12/09
to wx-u...@lists.wxwidgets.org
Srinivaz111 wrote:
> I do not know why this is happening. The OnPaint of the window consists
> of the following code.

In addition to what Leslie suggested, you may check out the font-related tutorials at NeHe:

http://nehe.gamedev.net/lesson.asp?index=03
http://nehe.gamedev.net/lesson.asp?index=04
http://nehe.gamedev.net/lesson.asp?index=09

Also see http://www.opengl.org/resources/faq/technical/ (FAQ 17).

Best,
Carsten

--
Carsten Fuchs Software
Multi-Player, Multi-Platform, Real-Time 3D Graphics
http://www.Ca3D-Engine.de

Jonathan Liu

unread,
Mar 12, 2009, 9:24:42 AM3/12/09
to wx-u...@lists.wxwidgets.org

Hello Srinu,

Srinivaz111 wrote:
> Hi Les,
> Thanks for the reply. I tried GLUT and found that GLUT has a stand alone
> Message Loop. I mean I found that it can`t be used in other application. Is
> there any way to use GLUT without using glutCreateWindow, glutMainLoop,
> etc...? I understood that it requires a window(Created by glutCreateWindow)
> where it draws. But what my question is can I draw in my window which is
> already created using GLUT?

Use this to initialise GLUT (only do this once):
int argc = 1;
char* argv[1] = { wxString(wxApp::argv[0]).char_str() };
glutInit(&argc, argv);

After that, you can call the GLUT functions just like OpenGL functions
(e.g. glutSolidTeapot, glutWireTeapot, etc.). You don't need to use use
glutCreateWindow, glutMainLoop, etc. for window management because
wxWidgets already handles that.

Regards,
Jonathan

Leslie Newell

unread,
Mar 12, 2009, 9:32:13 AM3/12/09
to wx-u...@lists.wxwidgets.org
You could download freeglut
<http://sourceforge.net/project/showfiles.php?group_id=1032&package_id=1028&release_id=333849>
and just use the text code. Freeglut is under a MIT license so you can
do what you want with it.

Les

Chris Spencer

unread,
Mar 12, 2009, 9:22:07 AM3/12/09
to wx-u...@lists.wxwidgets.org
2009/3/12 Carsten Fuchs <Carste...@t-online.de>:

> In addition to what Leslie suggested, you may check out the font-related
> tutorials at NeHe:
>
> http://nehe.gamedev.net/lesson.asp?index=03
> http://nehe.gamedev.net/lesson.asp?index=04
> http://nehe.gamedev.net/lesson.asp?index=09
>
> Also see http://www.opengl.org/resources/faq/technical/ (FAQ 17).
>
> Best,
> Carsten

I use OGLFT [1] for rendering text in OpenGL. It is easy to use,
renders nicely, and is fast. It is worth looking into.

Chris.

[1] http://oglft.sourceforge.net/

Srinivaz111

unread,
Mar 12, 2009, 11:36:37 AM3/12/09
to wx-u...@lists.wxwidgets.org

Hi Jonathan,
Thanks for your reply. I tried whatever you said. But without setting any
window where the glut draws the text? I created one wxWidget window. I want
to set this window to glut. Can you please tell me how to do that and help
me in drawing text to wxWidgets window using OpenGL?

Thanks,
Srinu

> _______________________________________________
> wx-users mailing list
> wx-u...@lists.wxwidgets.org
> http://lists.wxwidgets.org/mailman/listinfo/wx-users
>
>

--
View this message in context: http://www.nabble.com/wxWidgets-and-OpenGL-tp22473806p22478526.html

Jonathan Liu

unread,
Mar 12, 2009, 8:53:25 PM3/12/09
to wx-u...@lists.wxwidgets.org

Hello Srinu,

Srinivaz111 wrote:
> Hi Jonathan,
> Thanks for your reply. I tried whatever you said. But without setting any
> window where the glut draws the text? I created one wxWidget window. I want
> to set this window to glut. Can you please tell me how to do that and help
> me in drawing text to wxWidgets window using OpenGL?

You don't need to set the window where GLUT draws the text, GLUT calls
OpenGL and will use whatever OpenGL rendering context is currently set.
In your code you called SetCurrent() which will set the proper OpenGL
rendering context.

Instead of drawing using the dc after SwapBuffers, do something like
this before glFlush:
wxString text = wxT("Hello");
const char *c = text.ToAscii();
glRasterPos3f(0.0f, 0.0f, 0.0f);
for (; c; ++c) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *s);
}

Regards,
Jonathan

Jonathan Liu

unread,
Mar 12, 2009, 9:21:48 PM3/12/09
to wx-u...@lists.wxwidgets.org

> Instead of drawing using the dc after SwapBuffers, do something like
> this before glFlush:
> wxString text = wxT("Hello");
> const char *c = text.ToAscii();
> glRasterPos3f(0.0f, 0.0f, 0.0f);
> for (; c; ++c) {
> glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *s);
> }

Sorry typo.

Fixed:


wxString text = wxT("Hello");

glRasterPos3f(0.0f, 0.0f, 0.0f);
for (const char *c = text.ToAscii(); *c; ++c) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *c);
}

Regards,
Jonathan

Srinivaz111

unread,
Mar 13, 2009, 8:45:02 AM3/13/09
to wx-u...@lists.wxwidgets.org

Thanks to all the people who replied to my message.
My actual problem is that I want to draw on a wxWidgets window which is
painted using OpenGL. Please tell me how to draw text on this window using
OpenGL.

Thanks,
Srinu

--
View this message in context: http://www.nabble.com/wxWidgets-and-OpenGL-tp22473806p22495542.html

Reply all
Reply to author
Forward
0 new messages