I'm reading th开发者_运维百科e documentation for glOrtho and not understanding it.
I'm looking at this resize code for an 2D drawing and not getting why some of the index are positive and some negative. The drawing is supposed to stay flat so I don't know if its doing what's supposed to:
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
//left, right, bottom, top, nearval, farval
glMatrixMode(GL_MODELVIEW);
}
It's constructing the projection matrix. glOrtho
multiplies the current matrix,which is the identity matrix due to the previous statement, by the orthographic projection matrix specified by the function.
Specifically, this call to glOrtho
is constructing a matrix that will put world co-ordinates of (-1, -1, -1) at the bottom-left-front of the screen, and world co-ordinates of (1, 1, 1) at the top-right-back of the screen. So if you were to draw the triangle:
glBegin(GL_TRIANGLES);
glVertex3f(-1, -1, 0);
glVertex3f(-1, 1, 0);
glVertex3f(1, -1, 0);
glEnd();
It would cover exactly half of the screen.
Note that if you're only using 2D then the last two parameters aren't that important (although they can be used to control layers). Just draw everything with z=0, and if you need to put something in front of something else then use a higher z value.
It actually does nothing except flip the z coordinate. If you plug in your values you'll see that it generates the matrix
1 0 0 0
0 1 0 0
0 0 -1 0
0 0 0 1
Note that it's not the identity matrix because glOrtho
is weird stupid (note the minus signs):
(left, bottom, -nearVal)
and(right, top, -nearVal)
specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, respectively, assuming that the eye is located at (0, 0, 0).-farVal
specifies the location of the far clipping plane.
These minus signs mean that fNear
and fFar
have the opposite meaning.
精彩评论