# Is there any way to calculate depth without using z-buffer?

Hello.
I’m very beginner, so kind explanation and tolerance will be really appreciated.
I coded simple game with C++ and openGL.
And then, I want to calculate the depth of objects.
(to color them differently according to their depth)

I found the concept of z-buffer, and thought.
“Oh. Depth is already calculated! Just use it.”
But problem occurs.
Almost every objects has z-buffer in 0.9 ~ 1.0.
I noticed that the reason is non-linear distribution of z-buffer.
(May the designer of z-buffer wanted to calculate closer object more accurately, right?)

So, I linearized them with code.
(Depth Buffer - How do I get the pixel's Z coord - OpenGL: Advanced Coding - Khronos Forums)

After linearizing them, distribution be linear.
But other problem occurs.

[ATTACH=CONFIG]1930[/ATTACH]

Can you see?
Strange discontinuity ruined my graphic.
I suppose the reason is limited memory of z-buffer, but not sure.

So I decided just ignore z-buffer, and calculate it own way.
But is there any nice way to do it?
Or the problem above can be solved while still using z-buffer?

Thank you.

edit---------------------------------

And I feel really sorry about do not post the part of my code here.
I do not have permission yet from my partners about it…
If I got it, then I would immediately post it.

A perspective projection divides all 3 coordinates by eye-space Z, so the transformation from eye-space Z to window-space Z (i.e. depth) will always look like a/z+b for some constants a, b. A perspective projection preserves planarity, so depth can be calculated without requiring a division operation for each pixel.

I assume that you’re referring to the limited precision?

If most of your depth values are close to 1.0, this may indicate that the near distance used in the perspective transformation is too small. For any factor N>1, approximately 1/N of the depth values correspond to eye-space -Z values greater than N times the near distance.

If you’re using shaders, you can just use the eye-space Z coordinate.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.