# OpenGL PushMatrix and PopMatrix and Clipping Range?!

This topic is 3670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, so I've never had to use PushMatrix and PopMatrix before when just setting up my Orthographic view, then switching to Modelview Matrix to draw some stuff on the screen, but apparently I had to this time and it also affected my zNear and zFar. Here's a snippet of the code. Lighting was added so the teapot could be seen more vividly. ** EDIT** I've discovered because I had from -1.0 to 100.0 for gluPerspective that messed it up and I changed it to +1.0 to 100.0. Although, my objects still aren't being drawn centered and I know it's because of Ortho2D, but the only way to fix it is to remove Ortho2D. I also had to change ortho2DProjection( 0, 0, w, h, 0.0, 1.0, 0.0, 1.0); to ortho2DProjection( 0, 0, w, h, -1.0, 1.0, -1.0, 1.0); so the object would be centered when it became drawn. I've discovered that I would use the original coordinates for Ortho 2D (0, 1, 0, 1) if I wanted to draw a 2D interface, but I would have to switch to it first and then switch back to the second ortho 2D(-1, 1, -1, 1) when I wanted to redraw my objects centered. ********** ** UPDATED CODE **
bool init()
{
int w = opengl->getWidth(), h = opengl->getHeight();

glClearColor(0.93f, 0.93f, 1.0f, 0.0f);

// Enable States
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0);

glMatrixMode(GL_PROJECTION);

//Setup Drawing Area
ortho2DProjection(
0, 0, w, h,
-1.0, 1.0, -1.0, 1.0);
gluPerspective(45.0, (w/h), 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);

return true;
}

void ortho2DProjection(GLint x, GLint y, GLsizei w, GLsizei h,
double left, double right, double bottom, double top)
{
// Setup Orthographic Projection
glViewport(x, y, w, h); // Set rendering area.
gluOrtho2D(left, right, bottom, top);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
{

gluLookAt(
0.0, 0.0, 2.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);

GLfloat ambientColor[] = {1.2f, 1.2f, 1.2f, 1.0f};
GLfloat lightPos[] = {0.0f, 0.0f, 0.0f};

glLightfv(GL_LIGHT0, GL_DIFFUSE, ambientColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

glTranslatef(0.0, 0.0, zPos);
glutSolidTeapot(0.2);
}
glFlush();
}

void resize(int w, int h)
{
glMatrixMode(GL_PROJECTION);
{
glViewport(0, 0, w, h);
//gluOrtho2D(0.0, 1.0, 0.0, 1.0);
gluPerspective(45.0, w/h, 1.0, 100.0);
}
glMatrixMode(GL_MODELVIEW);
}

[Edited by - openglJunkie on November 29, 2008 1:56:34 PM]

##### Share on other sites
Quote:
 Original post by openglJunkie glLoadIdentity(); glPushMatrix(); glViewport(0, 0, w, h); gluOrtho2D(0.0, 1.0, 0.0, 1.0); gluPerspective(45.0, w/h, -1.0, 100.0); glPopMatrix();
Hmm, you seem to be operating under some misapprehensions about how the OpenGL matrix stack works.

Firstly, why are you calling gluOrtho2D immediately before gluPerspective? gluOrtho2D sets up an orthographic projection, and then gluPerspective immediately multiplies that with a perspective projection.

Secondly, the code snippet I have quoted has precisely no effect, because when you call glPopMatrix at the end, it replaces all your changes with whatever was there when you last called glPushMatrix (namely the identity matrix produced by the call to glLoadIdentity)

##### Share on other sites
Quote:
 Firstly, why are you calling gluOrtho2D immediately before gluPerspective? gluOrtho2D sets up an orthographic projection, and then gluPerspective immediately multiplies that with a perspective projection.

I used gluOrtho2D to set the drawing area box (-1.0, 1.0, -1.0, 1.0) and then I called gluPerspective so my objects would get smaller as their z coordinate did. I thought they were seperate. I've also removed all PushMatrix and PopMatrix. I'll update the code in my original thread.

I think I figured it out. You don't need a call to gluOrtho2D because when you call loadIdentity() the drawing position is centerd to (0, 0, 0)

[Edited by - openglJunkie on November 29, 2008 2:53:29 PM]

##### Share on other sites
Read the documentation for glOrtho and gluPerspective. In particular, the second paragraph of the "description" section for gluPerspective, and the whole "description" section for glOrtho. Additionally, it would be a good idea to read Chapter 3, and Appendix F, of the red book.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013654
×