PDA

View Full Version : How can I use glOrtho and gluPerspective in same frame?



Mars_999
03-12-2006, 08:03 PM
I am trying to use glOrtho to draw text and other UI stuff on my screen and then swith back to gluPerspective. As of now I am having no luck with it. Is this even possible? I have setup my initial mode as gluPerspective.


void RenderText(CFont &font)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho(0.0, gWidth, gHeight, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw text here
//if(show_fps)
//font.DrawText(timer.Fps_TO_String(), 10.0, -50.0, -10.0, .0f, .0f, .0f);

glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0);
glVertex2f(0.0, 0.0); // Bottom Left Of The Texture and Quad
glVertex2f(400.0, 0.0); // Bottom Right Of The Texture and Quad
glVertex2f(400.0, 400.0); // Top Right Of The Texture and Quad
glVertex2f(0.0, 400.0); // Top Left Of The Texture and Quad
glEnd();

//switch back to perspective mode
glMatrixMode(GL_PROJECTION);
glPopMatrix();
gluPerspective(45.0f, gAspect, 3.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}Thanks

shelll
03-12-2006, 08:57 PM
you don't understand push/pop functions.

then... first render scene and after that, render GUI. so:


// redner scene
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, gAspect, 3.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

...

// render GUI
// it is good to disable depth testing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, gWidth, gHeight, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

...

Mars_999
03-13-2006, 03:07 AM
Originally posted by shelll:
you don't understand push/pop functions.

then... first render scene and after that, render GUI. so:


// redner scene
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, gAspect, 3.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

...

// render GUI
// it is good to disable depth testing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, gWidth, gHeight, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

...Hmmm. that was suggested to me from someone else, and with your new code fragment it still isn't working... Thanks

knackered
03-13-2006, 10:02 AM
void RenderText(CFont &font)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity(); // <----
glOrtho(0.0, gWidth, gHeight, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity(); // <----
glDisable(GL_CULL_FACE); // <--- possibly need this

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw text here
//if(show_fps)
//font.DrawText(timer.Fps_TO_String(), 10.0, -50.0, -10.0, .0f, .0f, .0f);

glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0);
glVertex2f(0.0, 0.0); // Bottom Left Of The Texture and Quad
glVertex2f(gWidth, 0.0); // Bottom Right Of The Texture and Quad
glVertex2f(gWidth, gHeight); // Top Right Of The Texture and Quad
glVertex2f(0.0, gHeight); // Top Left Of The Texture and Quad
glEnd();

//switch back to perspective mode
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// DO NOT NEED THIS: gluPerspective(45.0f, gAspect, 3.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}I'm confused, why are you clearing the colour/depth buffers if you're drawing on top of the already-rendered 3D stuff?
Now sling 'yer hook. Post future questions in the 'Beginners' forum.

Mars_999
03-13-2006, 06:07 PM
Originally posted by knackered:

void RenderText(CFont &amp;font)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity(); // <----
glOrtho(0.0, gWidth, gHeight, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity(); // <----
glDisable(GL_CULL_FACE); // <--- possibly need this

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw text here
//if(show_fps)
//font.DrawText(timer.Fps_TO_String(), 10.0, -50.0, -10.0, .0f, .0f, .0f);

glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0);
glVertex2f(0.0, 0.0); // Bottom Left Of The Texture and Quad
glVertex2f(gWidth, 0.0); // Bottom Right Of The Texture and Quad
glVertex2f(gWidth, gHeight); // Top Right Of The Texture and Quad
glVertex2f(0.0, gHeight); // Top Left Of The Texture and Quad
glEnd();

//switch back to perspective mode
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// DO NOT NEED THIS: gluPerspective(45.0f, gAspect, 3.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}I'm confused, why are you clearing the colour/depth buffers if you're drawing on top of the already-rendered 3D stuff?
Now sling 'yer hook. Post future questions in the 'Beginners' forum. Thanks knackered that worked except you forgot to add


glEnable((GL_CULL_FACE);at the end. :) BTW I didn't consider this to be a beginner topic or I would have put it in that forum. Not many tutorials on this subject from the results I get with a google search. Thanks for the help

knackered
03-14-2006, 12:43 AM
You cheeky young wippersnapper. <clip-round-ear>
BTW, just clear the depth buffer.

Mars_999
03-14-2006, 05:24 AM
Originally posted by knackered:
You cheeky young wippersnapper. <clip-round-ear>
BTW, just clear the depth buffer. :) One last question which way should I go about rendering text? I dont' need 3D text for as I am just trying to display text for a fps counter, ammo amounts, health ect... for a RTS game. Outline fonts? Thanks again.

knackered
03-14-2006, 06:38 AM
I can't hear a word you're saying. Come closer, I'm over here in the beginners forum, covered in phlegm and custard.

ZbuffeR
03-14-2006, 07:06 AM
RTFM http://www.opengl.org/resources/features/fontsurvey/