tags:

views:

57

answers:

1

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

}
+3  A: 

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.

Peter Alexander
what would glOrtho(0.0f, windowWidth, windowHeight, 0.0f, 0.0f, 1.0f); accomplish? The limits of the world would be windowWidth and and WindowHeight, right?
omgzor
Correct, although you'd have to be careful drawing with z=0 as it would be right on the edge of the z-domain (anything with z<0 or z>1 would be hidden). If you plan on drawing at z=0 then it's best to just make the range something like (-1, +1) for safety's sake.
Peter Alexander