PDA

View Full Version : Issues with Linear Z buffer due to Ortho projection



myk45
06-06-2013, 05:57 AM
Hello All,

I have the following problem:
- I need 2D billboarded items, that are screen-space constant in size, inside a 3D scene.

My solution:
- Use 2 matrices, one for orthographic projection, one for perspective.

Using this solution works fine, except, i have depth issues. This is because the Z-Buffer would be linear when i use the ortho matrices, but non-linear for perspective matrices.
This is now leading to wrong depth test results.
I'm not sure how exactly i can solve this problem. Any insights would really be helpful.

Also, please let me know if this use-case is common.

Thanks!

thokra
06-06-2013, 08:42 AM
If you need a linear depth buffer, just lay out a linear depth buffer. Are you using shaders or is this legacy stuff?

myk45
06-06-2013, 08:46 AM
Thanks for the reply! Yeah, i am using Shaders(OpenGL 2.0). Is it possible to have a linear depth buffer with a perspective matrix? Can you please explain this?

thokra
06-06-2013, 08:52 AM
Well the mathematics is simple, but I think I might have misunderstood something.

You want billboards that have a constant size in screen-space. Are we talking something like a HUD or what is it you want?

myk45
06-06-2013, 08:55 AM
I am referring to something like 2D labels, that are drawn using alpha textured 2D quads. These label items in the 3D scene. And, these also need to be billboarded.
Thanks!

thokra
06-06-2013, 09:06 AM
Hmm, from the top of my head I'd go straight to simply projecting the billboard as well, but scaling it with a factor that's a function of distance to the camera. Another question is, do you want the billboards to show on top of everything else or is it ok if they are correctly depth tested?

myk45
06-06-2013, 09:09 AM
> Hmm, from the top of my head I'd go straight to simply projecting the billboard as well, but scaling it with a factor that's a function of distance to the camera.

Well the current implementation involves this scaling, that is a function of the Z distance from the near plane.
But, im trying for a better implementation.

> Another question is, do you want the billboards to show on top of everything else or is it ok if they are correctly depth tested?
Yes, depth test is the main concern.(i.e i need proper depth testing)

Thanks!

thokra
06-06-2013, 09:27 AM
Could you post a screen shot of what is actually going wrong?

myk45
06-06-2013, 09:35 AM
hmm, i currently do not have access to it.
But the problem is that the depth testing would have problems since there is a mix of linear and non-linear depth calculation. So, stuff that need to fail depth test would actually pass.

I found a very similar article here:
http://stackoverflow.com/questions/8990735/how-to-use-opengl-orthographic-projection-with-the-depth-buffer

I saw the solution there. I'm wondering if there could be a different approach.

thokra
06-06-2013, 10:08 AM
Well, I guess Alfonse's SO answer sums it up pretty nicely. I guess this is best solution.