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

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

## Recommended Posts

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 on other sites
If both ranges are the same, as in the thread title, then why do you have to do anything?

Dave

##### Share on other sites
He wants a linear range. Z buffer is non linear.

##### Share on other sites
Is it??

How the hell can i not know that lol!

##### Share on other sites
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 on other sites
Hmm... this snippet
Quote:
 The 1/Z bufferThe mapping equation ain't linear anymore: Z buffer = ( 1/Z - 1/nearZ ) / ( 1/farZ - 1/nearZ )

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 on other sites
You could use:
f(z) = z/(a - (a-1)z),

where:
a = farZ/nearZ

##### Share on other sites
Why can't you use w itself by passing it to the pixel shader?

##### Share on other sites
http://www.mvps.org/directx/articles/linear_z/linearz.htm

Hope that helps.

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633310
• Total Posts
3011304
• ### Who's Online (See full list)

There are no registered users currently online

×