PDA

View Full Version : zNear/zFar problem



ahuarte
03-05-2002, 10:03 PM
I am developing a single large 3d-terrain viewer.

I use the "gluPerspective" function for setup the modelview matrix of the scene.

I want calculate the zNear/zFar values in order to view the whole world but I donīt want corrupt the z-depth buffer.

I have the current bounding-3dExtent of the all graph objects (saved in 8 3dPoints), and I want calculate the zNear value equal the lowest distance of the current position of the camera to this 3dExtent.
Then, I get the zFar value biggest possible.

How I do it?
Do you think that this setting is correct?


Sorry my english http://www.opengl.org/discussion_boards/ubb/smile.gif
Thanks in avance

LordKronos
03-06-2002, 05:10 AM
To calculate the Z distance from the camera to a point, you need to know:
The coordinate of the point (call this 'point')
The coordinate of the camera (call this 'camera')
The normalized direction vector of the camera (call this 'viewdir')

First, calculate the direction vector from the camera to the point: dir = point - camera

Now, the z distance is really just the magnitude of 'dir' in the direction the camera is facing ('viewdir'). This is a dot product operation.
zdistance = dotProduct(dir, viewdir)

Now, of course this assumes that your transformation and projection matricies arent doing any type of scaling (or that you already took such scaling into account when you determinged 'point', 'camera', and 'viewdir'. If this isnt the case, and it is difficult to take this into account, you can always take the worldview and projection matrix that the driver is using and run your 8 points through those, but its probably not as efficient.