Skip to main content
GameDev.net gamedev.net
🔒 Locked

Z-Interpolation

Started by AdamGL Jun 9, 2008 at 8:55 PM 4 replies 7.2k views
Original Post
AdamGL
AdamGL
I don't know what's wrong with me. My mind doesn't seem to want to work. I am making a mini software renderer and I have successfully implemented a triangle rasterizer. Now I need to find the depth values of each pixel in the triangle so I can implement a Z-Buffer. Problem is, I can't seem to get the interpolation to work. It's such standard math, but here I am, asking for help. So, to my knowledge, the formula for finding the z value of a point on one of the edges of the triangle is: float z0 = 1.0 / ((1.0 / z1) + s * ((1.0 / z2) - (1.0 / z1)))); Now, one of the issues was what exactly is the s value? Is it the position on the line segment relative to the start point? And these reciprocals, are they correct? Thanks in advance for the help. And if anyone has any examples of this interpolation in either C/C++ or Java, I would greatly appreciate a glance :)
bi-FreeSoftware
silvermace
silvermace
dont really know the specifics of Z buffer interpolation, but breaking down the equation always helps me


a = 1 / z1
b = 1 / z2
c = a + s * (b - a)
z0 = 1 / c


so, 'c' looks like a linear interpolation.. which means 's' is the percentage of interpolation?

if 's' is the percentage of interpolation that should mean its normalised 0.0-1.0 which would intuitively be some fraction of the two extremes?

two articles I found via [google]
http://gamma.cs.unc.edu/QVDR/temp_homepage/project/comp236/PA2.htm
http://www.gamespp.com/graphicsprogramming/informationOnTheZBufferAlgorithm.html

Good luck, hope I helped (doubt it haha!) [smile]
Jason Z
Jason Z
If your final vertex coordinates are in clip space, then the you just need to do a basic interpolation between vertices. Start along one of the triangle edges, and interpolate between the two vertices on that edge. Then repeat for the other two edges. Finally, interpolate across each of the horizontal scanlines using the intermediate values you just calculated along the edges.

The equation you listed looks more complex than it needs to be...
Jason Zink :: DirectX MVP   Direct3D 11 engine on CodePlex: Hieroglyph 3 Direct3D Books: 
Zipster
Zipster
Typically your vertexes will be represented by 4D homogeneous coordinates. Unfortunately, you can't do the interpolation in clip-space since the homogeneous divide has already been done and Z/W is non-linear. What you want to do instead is multiply your vertexes by the projection matrix and perform the interpolation on the vertexes before the divide. Then Z/W will be your depth buffer value.

That's for perspective projections. If it's simply orthographic, then you don't need to do anything special and can interpolate the Z-value directly in the space of your choosing.
AdamGL
AdamGL
Thanks guys! I am not doing this with matrices :o because I had this other idea. I know, I know, bash me all you want, but for now I will do it my way. Those links cleared up the problem, thank you silvermace and everyone who helped out. I can't believe that didn't come up for me in Google. Because I searched... long and hard...
bi-FreeSoftware
AdamGL
AdamGL
Ok, I managed to whip something up that I thought made sense but it fails when drawing a cube. Here's a sample of how it works(I think its wrong):

//where z1 and z2 are depth coordinates//where py1 and py2 are the vertical components of the endpoints of one line//where gy1 and gy2 are the vertical components of the endpoints of another linedouble pZ = 1.0 / z1;double gZ = pZ;//calculate the z incrementsdouble pInc = Math.abs(((1.0 / z2) - pZ) / (py2 - py1));double gInc = Math.abs(((1.0 / z2) - gZ) / (gy2 - gy1));for(int y = 0; y < maximumY; y++){    //compute edge x coordinates of triangle at a given y coordinate        //for every y, increment the z accordingly    pZ += pInc;    gZ += gInc;    //interpolate linearly between the two z values to get each pixel's depth    //the scanline.    //test depth against depth buffer}


What am I doing wrong? And everywhere I go, I get instructions on how to do Texture mapping, in which they introduce U and V, which I don't want. I just want to get the z-value of an individual pixel so I can check it against the z-buffer. Thank you for your help!
bi-FreeSoftware

Topic Locked

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

Sign in to reply to this topic.