Sign in to follow this  
Code-R

Converting Z-Buffer [0.0, 1.0] value to W-buffer linear [0.0,1.0] value

Recommended Posts

Code-R    136
How can I convert a z-buffer value, read in a fragment shader, into a linear w-buffer-like value that varies LINEARLY from the near plane to the far plane?

Share this post


Link to post
Share on other sites
Xai    1838
I personally don't know 3D coding well so I don't know what the zbuffer is, but I have done many function conversion, you just apply the inverse of the range you have - sometimes with some pre and post domain adjustment to fit the piece of the curve you want / have.

For instance I have a slider class which generates a linear 0.0 to 1.0 range of values, and I convert it to an logrithmic 0.0 to 1.0 range of values for use as a volume (which is logrithmic to the ear) - the forula for that (in C#) is:

public static double Log(double input)
{
return System.Math.Log10(input * 9.0 + 1.0);
}

or you might need to go the other way:

public static double Exponential(double input)
{
return ((System.Math.Pow(input, 10) - 1.0) / 9.0);
}

obviously you will need to use C++ versions of the log and power functions if you are not in .NET (I assume you are not).

hope that gets you started.

Share this post


Link to post
Share on other sites
smitty1276    560
Hmm... this snippet
Quote:

The 1/Z buffer

The mapping equation ain't linear anymore:

Z buffer = ( 1/Z - 1/nearZ ) / ( 1/farZ - 1/nearZ )



...came from this page: http://www.gamedev.net/reference/articles/article673.asp

I, like Dave, had completely forgotten that the z-buffer was non-linear (until I read your post), let alone how it is calculated. If the above snippet is correct, then maybe you can solve for z and get what you are wanting.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Why can't you use w itself by passing it to the pixel shader?

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