Jump to content
  • Advertisement
Sign in to follow this  
openglJunkie

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.

If you intended to correct an error in the post then please contact us.

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

	glShadeModel(GL_SMOOTH);


	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

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

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


		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);
	glLoadIdentity();
	{
		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);
	glLoadIdentity();
}
[Edited by - openglJunkie on November 29, 2008 1:56:34 PM]

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!