Pawel Klapuch
Pawel Klapuch

Reputation: 490

Drawing over terrain with depth test?

i'm trying to render geometrical shapes over uneven terrain (loaded from heightmap / shapes geometry is also generated based on averaged heights across the heightmap however they do not fit it exactly). I have the following problem - somethimes the terrain shows through the shape like showed on the picture.

Open Image

I need to draw both terrain and shapes with depth testing enabled so they do not obstruct other objects in the scene.. Could someone suggest a solution to make sure the shapes are always rendered on top ? Lifting them up is not really feasible... i need to replace the colors of actual pixel on the terrain and doing this in pixel shader seems too expensive..

thanks in advance

Upvotes: 3

Views: 174

Answers (1)

Egor
Egor

Reputation: 809

I had a similar problem and this is how I solved it:

  • You first render the terrain and keep the depth buffer. Do not render any objects
  • Render solid bounding box of the shape you want to put on the terrain.
  • You need to make sure that your bounding box covers all the height range the shape covers
  • An over-conservative estimation is to use the global minimum and maximum elevation of the entire terrain
  • In the pixel shader, you read depth buffer and reconstructs world space position
  • You check if this position is inside your shape
  • In your case you can check if its xy (xz) projection is within the given distance from the center of your given circle
  • Transform this position into your shape's local coordinate system and compute the desired color
  • Alpha-blend over the render target

This method results in shapes perfectly aligned with the terrain surface. It also does not produce any artifacts and works with any terrain. The possible drawback is that it requires using deferred-style shading and I do not know if you can do this. Still, I hope this might be helpful for you.

Upvotes: 1

Related Questions