Skip to main content
gamedev.net
🔒 Locked

Depth buffer

Started by osh 4 replies 400+ views
Original Post
osh
osh
Hello, I''m developing my own sw rasterizer but I don''t properly understand theory behind generating z values. For now, I was computing z value this way: z = ((v.z - mNearPlane) / mFarPlane) where v.z is z coord in view space, but I think it''s not correct because the values differ from that in OpenGL. More I want to store values as 32 bit integer, but how can I indicate if the val is behind the far plane/ in front of near plane? My only idea was to set buffer to negative value...
Lantz
Lantz
Aren''t you supposed to discard the pixel if it''s outside the view frustum?
SoulSpectre
SoulSpectre
(Z - NEAR) / (FAR- NEAR)
This will give a linear depth value in the the range of 0 to 1.0. where 0 = the near plane and 1.0 = the far plane.

However most depth systems use a biased system where 90% of the depth range is used by the near 10% of distance. This is because it is much easier to pick up depth problems in objects close to the eye point than they if they were far away. For example you may have noticed the z tearing of objects far away in games but as you get closer to them the tearing slowly disappears.
SoulSpectre
SoulSpectre
oh yeah, for your second problem, all pixels outside of the view frustrum are discarded. You do not write any values to the depth buffer where they are outside of the near/far planes. Also you would want to use an unsigned format for your depth buffer as there is no need for negative values.
osh
osh
Thx! You helped me. I have one more thing. It is slightly something different, but... I'm still unable to corectly simulate OpenGL in coord generation, same triangle in OpenGL seems to be closer to viewer. This is the code I use:
z = 1 / v.z;
mAspect = mHalfWidth / mHalfHeight;
mDist = (mHalfHeight - 0.5f) / tan(mFov * 0.5);
x = ftoi((v.x * z * mDist * mAspect) + mHalfWidth);
y = ftoi((v.y * z * mDist) + mHalfHeight);

What's wrong with it? Or is it anything completly differrent?

Edit: Forgot to give computation of z.

[edited by - osh on May 13, 2004 2:08:45 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.