Depth issues (draw 2 things with diffrent znear zfar)

Started by
4 comments, last by Osbios 8 years, 1 month ago

Im drawing terrain and water with z_near 2.0, and z_far 1000 * 3.0

now i need to draw a ship with z_near 0.01 z far 1000.0

how do i accomplish this?

Advertisement

Draw the terrain and the water then switch to an other projection matrix (or whatever you have) and draw the ship.

Why do you want to do this?

thats the problem, after drawing water and terrain i switch projection matrix and the ship appears to be under that water and terrain, its because depth test is wrong, when depth buffer is filled lets say with 0.5 on some pixel, and my ship is at 0.7 of its own projection mat etc.

like here:

device-2016-03-06-183511.png

the reason i am doing this is that water and terrain cover large area and giving too close z_near it makes geometry to flicker etc. plus i need to move on the ship so i need the smallest z_near possible.

I think the problem is that the near-far ranges of the different projection matrices overlap and the same depth value maps to different camera space positions for each projection, so the depth buffer will not work correctly.

If you can afford to render the water twice, you can to the following:

1. Set z-near to 1000.0 and z-far to 10000.0

2. Draw water only

3. Clear z-buffer

4. Set z-near to 0.1 and z-far to 1000.0

5. Draw Ship and water

You probably have to experiment with the values for the near and far ranges. What is important, is that the z-near value of the first projection is the z-far value of the second, so you draw the background first and then the foreground, without an overlap.

yeah thats seems like a reasonable solution thanks

You can also use GL_ARB_clip_control to create a "inverted floating point depth buffer" to get very very good precision. Its very easy to set up. You just need a different projection matrix and invert depth buffer settings (clear value, invert the comparing, invert clipping) https://www.opengl.org/discussion_boards/showthread.php/185674-How-to-setup-projectionMatrix-worldMatrix-for-use-with-inverted-float-depth-buffer?p=1264627&viewfull=1#post1264627

This topic is closed to new replies.

Advertisement