Question about Coordinates

Hi,

What specifies the volume which is mapped onto the viewport?

I thought it was glOrtho and included the line:

glOrtho(-100.0,100.0,-100.0,100.0,-100.0,100.0);

into my code setting up the opengl window. But then if I try to draw a line such as

glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0f, 50.0f, 50.0f);
glEnd();

it stretches out way beyond the window boundary. A few other experiments have revealed that the volume that is projected onto my window is -1.0->+1.0 in all three dimensions.

How can I change this?

thanks,
Aengus.

The volume is -1 to +1 after transformation, but that is not the issue here. Your ortho loads a matrix onto the projection matrix that ensures that lines are appropriately transformed to the normalized space that maps to the viewport, seeing -1 to +1 mapping to the viewport suggests an identity projection matrix.

There are some things you need to ensure.

First, make sure that you apply the ortho matrix to the projection matrix stack using the glMatrixMode(GL_PROJECTION) call glLoadIdentity on the projection matrix before calling ortho, then LEAVE THE PROJECTION MATRIX ALONE, to do this make sure you switch to the modelview matrix once you set the projection matrix by calling glMatrixMode(GL_MODELVIEW).

I’d guess your problem is you were loading the ortho onto the modelview matrix then loading identity over it when you started drawing the scene and you wound up with a -1 to +1 ortho (i.e. the default identity matrix).

Thanks fo rthe suggestion, but I’m pretty sure I wasn’t doing that. After the OpenGL window is set up (but before any drawing), I use the code:

glViewport(0,0,win_width, win_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0f,100.0f,-100.0f,100.0f,-100.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and then my RenderScene() function looks like:

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();

glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(-25.0f, 25.0f, 0.0f);
glVertex3f(25.0f, 25.0f, 0.0f);
glEnd();

glPopMatrix();

glFlush();
SwapBuffers(hDC);

So as far as I can see, I never do anything to the GL_RPOJECTION matrix once the ortho function has been called.

Is there anything obviously wrong here?

Well, a few hints are in order. Your reshape function looks great. The problem lies in your display function.

  1. You don’t really need to call glClearColor every frame if you don’t need it. If it will stay in one color, call this function in your init function.
  2. call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    remember to clear every buffer you use.
    3)You don’t need a push pop matrix. What is usually done is call glLoadIdentity() after glClear and then take it from the top :slight_smile: I mean each frame we redraw objects in their current frame coordinates.
  3. After glLoadIdentity(), you might want to put gluLookAt(). Look it up in help or google. It “sets” the camera in the virtual world. The reason your line reaches the window edges is probably because your camera is too close to the line.

In general, try to learn more about the view frustum and how you can manipulate this and your camera.

On the face of things it looks OK (I think :-)).

w.r.t. the other respondent, in the case of an ortho a lookat call may confuse things. The line and eye does have a location in 3D but remember that the ortho is not the same as a frustum projection. The ‘near’ and ‘far’ can correctly map a line to screen space without the need for a translate on the viewing matrix. Near can map behind the origin of eyespace (I could say near can be behind the viewer but it doesn’t mean the same thing in ortho).

P.S. if you’re seeing -1 to +1 then it’s too suspicious, you’re not showing us something and are getting identity on your projection. Try calling your projection ortho code in your draw function.

My new guess is that you don’t have a valid context or something silly when you set your ortho.