PDA

View Full Version : Clipping problems with GL setup

PollyPocket4eva
07-21-2009, 09:02 AM
I'm using the method below in a 3D viewer. This code comes from an Apple Developer example. I'm having a problem where objects seem to clip (all or part of the object becomes invisible when the camera is moved), and I can't get a handle on why exactly. I believe it is related to the clipping plane variables "near" and "far" but it doesn't seem as clear cut as just that. I'm not sure what the variable "shapeSize" is intended to do exactly, but this too seems to play a role in the problem. shapeSize = 7.0f; is Apple's default for that var.
Anyone have any ideas? Thanks!

- (void) updateProjection
{
GLdouble left, right, top, bottom, near, far;

[[self openGLContext] makeCurrentContext];

// set projection
glMatrixMode (GL_PROJECTION);
near = -camera.viewPos.z - shapeSize * 0.5;
if (near < 0.00001)
near = 0.00001;
far = -camera.viewPos.z + shapeSize * 0.5;
if (far < 1.0)
far = 1.0;
radians = 0.0174532925 * camera.aperture / 2; // half aperture degrees to radians
ratio = camera.viewWidth / (float) camera.viewHeight;
if (ratio >= 1.0) {
left = -ratio * wd2;
right = ratio * wd2;
top = wd2;
bottom = -wd2;
} else {
left = -wd2;
right = wd2;
top = wd2 / ratio;
bottom = -wd2 / ratio;
}
glFrustum (left, right, bottom, top, near, far);
}

dorbie
07-22-2009, 05:19 AM
The code is trying to sandwich the object between near and far planes presumably to make efficient use of the zbuffer an other interpolation precision.

You can simply discard this and set the near plane close but not too close and the far plane a good distance out.

If you want to fix the code then you probably have to fix the calculation of "shapeSize" which is probably wrong (underestimated) based on your description. Or the object centroid is not factored correctly.

Of course you may also be drawing a VERY small object where their minimum near clip kicks in and that too is easily fixed.

The code is clearly written to view an object centered at the origin. If you have modified the viewer to move the object then you need to account for that in the clip calculation or just discard it and go with something cnservative.

PollyPocket4eva
07-22-2009, 07:59 AM
Hi,
Thanks for your help. I think it is trying to sandwich between the planes.
I've tried discarding near and far, replacing them with 0.00001 and 10000 or something like that. I've also tried increasing shapeSize, however both of these alterations sometimes cause the entire scene to clip.
The code was definitely written to view an object centered at origin...
can you suggest a different approach or point me to some sample that will suit various object/scene sizes and camera movement?
Thanks!