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\$
  • 3
    \$\begingroup\$ Tip: since we're a Q&A site, you don't need to title your question "I have a question about..." - the fact that you're posting a question already tells us that. Saying the question is about projection on OpenGL also doesn't add any information that's not already in your question's tags: opengl & projection. So, try to use the title to describe what your question is. The more clear your topic is from the title alone, the more likely it will attract users with relevant expertise to click on it and read the body text. \$\endgroup\$ – DMGregory Feb 22 at 17:29
0
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.