I’m reading the 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.
glOrthomultiplies 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
glOrthois 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: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.