0
\$\begingroup\$

I am confused about the projection matrix in OpenGL.

i have a habit of writing the code as follows.

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
   glOrtho(0.0f,250.0f,0.0f,250.0f*h/w,1.0,-1.0);
else
   glOrtho(0.0f,250.0f*w/h,0.0f,250.0f,1.0,-1.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

Simple, This code sets the viewing clipping region that maintains the equal width and height. negative z value points away from the viewer. as z value extends from 1 to -1. anything that lies outside this value wont be visible to the user. But i have seen the code like this.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(left,right,bottom,top,1.0, 500.0);  //how????
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -250.0f);       ///?????
glutSolidSphere(15.0f,15,15);           /// how is this visible on the screen
}

Please help me understand this code. As from my point of view the z value is totally flipped near is 1.0 its okay but zfar is 500 and following that code there is a translation of z value to -250. and drawing that sphere will be visible on the screen how? My question is can we flip znear and zfar so that positive z value will go away from the viewer.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

In the second piece of code, I can't tell what you're unsure of, so I'll go through all the lines you have commented.

  • The glOrtho call appears to be made with predefined variables to avoid using magic numbers.
  • The glTranslatef call will move the origin 250 units into the scene, since by default, the coordinate system points outward. However, from the rest of your post it appears you already knew this, so I'm unsure of what to make of this comment.
  • The glutSolidSphere call just puts a sphere at the origin of the scene. Since the origin was just moved 250 units into the scene, the sphere appears at [0, 0, -250] relative to the camera.

(I'd like to emphasize that I have very little experience with OpenGL, so if someone sees something wrong with the above explanation, please comment or edit!)

As for the rest of your question, I suspect you want to use glScalef(1.0, 1.0, -1.0); to reflect your coordinate system across the XY plane.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .