near and far planes (orthographic)

I’m trying to set the near and far planes according to what model I render and I’m not sure how because this is an orthographic projection.

I was looking at glFrustrum and gluPerspective and if I understand all this correctly, it seems to be for perspective projections which is all the red book talks about in this context.

My problem is that my models are being clipped either on the near or far planes and I’m trying to get them in my viewing volume correctly. Can I use these functions for orthographic projections?

Any help much appreciated.

You can specify the near and far clipping plane with glOrtho.

Then for some reason, my models are still being clipped and the min/max x, y, and z coordinates are all within the values I put in the near and far of glOrtho. And no matter what I change my near and far too, they don’t solve the problem, in fact, it doesn’t make any difference at all.

gl.glOrtho(0.0, 400.0, 0.0, 400.0, 1, -1);

I could make that …
gl.glOrtho(0.0, 400.0, 0.0, 400.0, 10, -10);

and my model gets clipped exactly the same.

Try

gl.glOrtho(0.0, 400.0, 0.0, 400.0, 10000, -10000);

or

gl.glOrtho(0.0, 400.0, 0.0, 400.0, -10000, 10000);

If it still don’t work, make sure you’ve not got any other glOrtho calls in your code. You could also disable culling.

I tried combinations of what you said and no change whatsoever. I can see that when I rotate it, it’s being clipped and not culled where just my backside surfaces are hidden. There’s no other glOrtho calls being made. Here is what my reshape() and display look like.

Is there anything there that would interfere w/ my viewing volume not being sized correctly?


public void reshape(GLDrawable drawable, int x, int y, int width, int height)
{
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();

//float aspectRatio = width / height;
	
gl.glViewport(0, 0, width, height);

gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glOrtho(-width / 2, width / 2, -height / 2, height / 2, 10000, -10000);

}

public void display(GLDrawable drawable) {
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();

this.gldrawable = drawable;
	
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();

// viewing tranformation			
eyeZ = centroid.getZ() < 0 ? centroid.getZ() * -2: centroid.getZ() * 2;

glu.gluLookAt(centroid.getX() + xTrans, 
	centroid.getY() + yTrans, 
	centroid.getZ() + zTrans + (centroid.getMax_Z() * 2),
	centroid.getX() + xTrans, 
	centroid.getY() + yTrans, 
	centroid.getZ(), 
	0.0, 1.0, 0.0);

// model tranformation
gl.glScalef(xScale, yScale, zScale);
			
gl.glTranslated(centroid.getX(), centroid.getY(), centroid.getZ());
			
gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);
			
gl.glTranslated( -centroid.getX(), -centroid.getY(), -centroid.getZ());

// keyboard/mouse input
gl.glTranslatef(xTrans, yTrans, zTrans);
			
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
				
model.draw(active_id);
gl.glDisable(GL.GL_BLEND);
gl.glFlush();
  1. Try and avoid changing GL state in reshape(), you end up with more fragile code thats harder to debug. You’re not really saving yourself any work anyway - just move it to display().

  2. Perspective or ortho projections go in the projection matrix, not the model view (yes, it’ll work at first, but stuff like fog will break).

  3. Your actual mistake is resetting the modelview matrix in your display method, undoing your previous ortho setup. So fix the above and it’ll work. :slight_smile:

Thank you very much. This fixed that problem. And I coded that according to examples I found elsewhere online but that’s good to know.

Now my only question is whether I can use glOrtho from my display() method because my problem is that I don’t know the size of the model till it’s loaded. I’ve been working w/ a model I already know the size of.

It works like this.

  1. Empty canvas initializes (w/ fixed code as above).
  2. User loads a model.
  3. I calculate the size of the model (i.e. min/max x, y, z, centroid, etc…)
  4. Now I need to reset glOrtho’s viewing volume according to model …

Is it ok practice to call glOrtho from within the display method of the perspective matrix before the modelview matrix?