Sign in to follow this  
oreste

ZBUFFER question 2

Recommended Posts

oreste    162
This a more fundamental aspect and i have been trying to get to a question clearly submitted. The fact is this: where does the ZBUFFER get the depth coordinates from? The code i am studying is an initiation to the ZBUUFER. Here the triangle is rendered twice, in the geometry pipeline Triangle A is translated 0.0f, 0.0f, 2.0f and Triangle B is translated 0.0f, 0.0f, -2.0f. I suppose the ZBUFFER uses the Z coordinates 2.0f and -2.0f........................................Now suppose i have 2 different objects like a triangle and a square, one's vertices have the same z-coordinate (for ex. 2.0f) and the other's vertices have a different z-coordinate (for ex. -2.0f)then what? where does the ZBUFFER gets the depth?

Share this post


Link to post
Share on other sites
megadan    789

After the projection matrix is applied, the coordinates are converted into normalized device coordinates by dividing by the w component. This puts the x,y coordinates into the range [-1, 1] and the z coordinate into the range [0, 1] (for DirectX). For z, 0 is closest and 1 is farthest.

 

When each pixel is rendered, it's z value is compared to the same location in the zbuffer using a configurable comparison function.  By default, the comparison function is less than. This means that if the z value of the new pixel being rendered is less than the z value already in that location of the zbuffer, the new pixel is rendered and the zbuffer is updated with the new value.  If the z value greater than or equal to the existing value, the new pixel is skipped and not rendered because it is farther away than the pixel already there.  Clearing the zbuffer typically resets everything to 1 since that is the farthest value, and you usually want the closest objects to be visible.

 

If you are using a left handed coordinate system where +z goes into the screen, and you are using the default < compare, then in your example the zbuffer will contain the lower z value since it is closer (providing it isn't behind the camera).  However, that value will have been normalized to the range [0, 1], so it won't actually be -2.

 

Edit: I only mentioned DirectX because your other post referred to DirectX, but for completeness OpenGL normalizes z values to [-1, 1].

Edited by megadan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this