How can I use glOrtho and gluPerspective in same frame?

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

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();

...

Originally posted by shelll:
[b] 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();

[/b]

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

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.

Originally posted by knackered:
[b]

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. [/b]

Thanks knackered that worked except you forgot to add

glEnable((GL_CULL_FACE);

at the end. :slight_smile: 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

You cheeky young wippersnapper. <clip-round-ear>
BTW, just clear the depth buffer.

Originally posted by knackered:
You cheeky young wippersnapper. <clip-round-ear>
BTW, just clear the depth buffer.

:slight_smile: 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.

I can’t hear a word you’re saying. Come closer, I’m over here in the beginners forum, covered in phlegm and custard.

RTFM http://www.opengl.org/resources/features/fontsurvey/