Public Group

# z buffer - getting the z values of triangle

This topic is 4955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, i have an array with triangles. now i want to implement a z-buffer. my question is now, how can i get the z value for every pixel of every triangle? the triangles contain of three vertexes, every vertex has a x, y and z value. i also do a perspective projection of my triangles and get a u and v value for all three triangle vertexes on the screen. do i need the u and v values for the calculation or only the x,y,z values? would be nice if someone can give me a formula to calculate the z value of my triangles.

##### Share on other sites
Hmm, I'm not to good with the 3d math. Maybe someone should move this over to the math forum.

##### Share on other sites
Ok let's say if you are scanning every single pixel of a polygon.
One way to get the Z value of every pixel is the Plane Equation method. The point-normal form of it looks like this:

nx*(x-x0) + ny*(y-y0) + nz*(z-z0) = 0

Where nx,ny,nz is the normal to the polygon being scan-converted and x0,y0,z0 is a point on a polygon.

This can easily be converted to this form:

a*x + b*y + c*z + d = 0

Where a = nx, b = ny, c = nz, and d = -(nx*x0 + ny*y0 + nz*z0)

Then solving for z by rearranging terms, we see that

z = -(a*x + b*y + d)/c

which can be computed since you may know the (x,y) values of every pixel. If you don't, you may wanna look into scanline interpolation and flat shading polygons (manually per pixel, not using hardware). Now the problem with this is that there are 2 multiplications, 1 division, and 2 additions per pixel to find every Z. Let's trim it down, shall we. First of all, as it does it's scanline drawing, every X pixel of a polygon that get's rastered is no matter what incremented by 1 until it reaches its end point. Then Y get's incremented by 1. It does over and over this until the polygon is complete.

So now that we know that, we can break it down even easier:

Z as a function of X

Z(i) = -(a*x + b*y + d)/c

Z(i+1) = Z(i) + (-a/c)

Therefore, we see that the next z value on a horizontal run is simply the previous z value plus the constant term (-a/c). So now we just cut down Z interpolation to a single addition per pixel. Of course we still need to compute the first z value of every line, but this can be done incrementally because the change in Y in each line is 1. Therefore, carrying out the same incremental calculations on Z, we get the following equations:

Z as a function of Y

Z(i) = -(a*x + b*y + d)/c

Z(i+1) = Z(i) + (-b/c)

Which states that as y is incremented down one line, the new value of z is equal to the old value at the beginning of the last line plus the constant (-b/c).

Hope this helps. Bare in mind though, what I said above is NOT 1/Z buffering, which is more accurate.

##### Share on other sites
hmm... if you project a vertex through a view and/or a projection matrix you should get a screen x, screen y, and a screen z... ( and a w though i din't know if it's any use )...

and then you just do linear interpolation for ther surface...

• 40
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631371
• Total Posts
2999610
×