Objects behind other objects

Hiya!

Im having a problem with openGL not recognising that an object is behind another.

To put things into context - i am drawing a terrain and at the moment im adding some water. For the water i’m simply drawing a level quad across the terrain with some blue alpha… Good in theory but when i zoom out on the terrain the quad appears on parts of the terrain where is should be beneath it and sometimes the entire quad is shown.

Is there an easy way to fix this (ie - by changing a flag?) or am I going to have to place my water down with much more accuracy so it doesn’t go beneath the landscape (if you get what i mean)…

Any help much appreciated!

Thankyou.

Have you done glEnable(GL_DEPTH_TEST) ?

Yeah, I’v enabled Depth Testing…

Perhaps an image would show my problem better.

The white square shown should be half under the green terrain and half above. The part that is below is half above and below.

It only corrects itself properley when i zoom in really close.

Any ideas?

Could be z fighting. Do you have a big range in your znear zfar when you set the projection matrix?

And you’re using 16-bit or 24-bit (32 whatever) depth buffer when you set the pixel format?

Could be z fighting. Do you have a big range in your znear zfar when you set the projection matrix?

Yeah - i have a massive range

And you’re using 16-bit or 24-bit (32 whatever) depth buffer when you set the pixel format?

32 bits

Could be the z fighting thing then I guess…

Is there a simple resolve for this?

If not perhaps the best solution would be for me to calculate the coordinates of a quad that fitted the gap i wanted to fill more precisley…

Push out the near clip plane, this will help to improve depth buffer accuracy. You could also pull the far plane in a bit, but moving out the near plane will give a better improvment

Simple answer is to keep your near/far clip planes as tight as possible. Maybe you can change the position of your near clip plane depending on your camera position? So if you are closer to the action, your clip plane is nearer the camera. I suppose I mean you could adjust the near clip plane so it just catches the “nearest” object.

If you change the near/far values you’ll soon be able to see if it improves, so it should be easy to test the z fighting theory.