PDA

View Full Version : Loss of depth buffer performance



ben
04-04-2003, 10:33 AM
I am trying to visualize a model consisting of a single 1x1x1 cube located at the origin. This works fine. However, this model also includes a stray vertex that is at something like (0.0, 0.0, 1e14). When ever I adjust my front and back clipping ranges to accomodate the vertex, the precision of the depth buffer is hosed and the cube looks really bad.

The pixel format that I'm using has 32 bits of precision for the depth buffer, so I don't think I'll be able to get a more precise depth buffer.

Any ideas on how to make the cube appear as it should AND have the stray vertex obey depth rules so that it is occluded properly when the model is rotated?

HalcyonBlaze
04-04-2003, 03:46 PM
You should probably pull the near and far plane closer together and scale the model down. The further your far plane is from your near plane, the less precision you get.

- Halcyon

oliii
04-04-2003, 04:28 PM
would a semi-infinite projection matrix fix that?




void SetupProjection(void)
{
float fov = 90.0f;
float nearp = 1.0f;
float aspect = 4.0f / 3.0f;

float pinf[4][4] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

pinf[0][0] = atan(fov) / aspect;
pinf[1][1] = atan(fov);
pinf[3][2] = -2.0f * nearp;
pinf[2][2] = -1.0f;
pinf[2][3] = -1.0f;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf(&pinf[0][0]);
}


[This message has been edited by oliii (edited 04-04-2003).]

dorbie
04-06-2003, 10:50 AM
Push the near clip plane out as much as you can towards the cube. Hopefully your crazy stray vertex will be on the other side. If not you'll have to tolerate the clipping anyway and the far plane can be brought in.

Depth buffer precision is spent non linearly between the near plane and far plane. The non linearity of precision is determined by the ratio of near to far. The closer the near is to the eye the more precision tends to get spent on the area immediately in front of the near clip.

In your case the far plane is radically distant and it's squandering your precision. About the only option left is moving the near out to give enough precision in the area of the cube.

This may interest you:
http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html

ben
04-09-2003, 01:43 AM
Actually, I figured out a work-around. Prior to drawing the stray vertex, I project the vertex coordinate into window coordinates. I then test the resulting z-value to see if it falls either way in front of the near or way behind the far clipping planes. If so, I adjust the z-value to be just inside the appropriate clipping plane and unproject back to model coordinates. This seemed to work OK, but I think the key thing is to get rid of the need to draw the stray vertex.