Jump to content
  • Advertisement
Sign in to follow this  
vx7johmi

"sharp" holes in terrain with stencil buffer

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I would like to get some comments and suggestions on my current terrain implementation that supports adding of holes. Also I would like to hear if someone is using a similar system. First of all, the reason for my implementation is that i want to keep my terrain as a regular grid of some resolution. I do not want to re-triangulate any part of it to "open up" a hole. In order to make it more clear, I have the following example: Lets say we have a large open terrain with hills and valleys. In some part of the terrain I would like to place a canal. So, in order to mask out portions of the terrain where there should be a canal I first render the "lid" of the canal into the stencil buffer. Then I draw the terrain as usual but I make sure that no terrain can be drawn where the lid is rendered into the stencil buffer. Unfortunatetly, this will mask out all of the terrain where the lid of the canal is. If we are standing on the other side of a hill and really shouldnt see the canal, the lid will still be masked out in the terrain. In order to account for this i first render the back-faces of the terrain into depth-buffer only. When i render the lid into stencil-buffer i do depth testing in order to only render the non-occluded parts of the lid into the stencil buffer. I've also extended the idea to render tunnels through the terrain with a similar idea but the solution isn't working 100% yet. So, any comments? Has someone made something similar?? /Mike

Share this post


Link to post
Share on other sites
Advertisement
Would this work?

1. Draw the terrain.
2. Draw a "lid" with color-write and z-write off, incrementing the stencil buffer on z-pass.
3. Clear the Z-buffer (to remove the terrain depth values over the canal).
4. Draw the canal using the stencil buffer.

Share this post


Link to post
Share on other sites
John, thanks for the reply!

Quote:
Original post by JohnBolton
Would this work?

1. Draw the terrain.
2. Draw a "lid" with color-write and z-write off, incrementing the stencil buffer on z-pass.
3. Clear the Z-buffer (to remove the terrain depth values over the canal).
4. Draw the canal using the stencil buffer.


Should work fine. But in this case you have to be sure that the "lid" always is a tiny amount above the terrain to make sure that it always pass the z-test (when it is supposed to pass it). The good thing with your suggestion is that you can skip the first "render back-face terrain into depth" pass.
The reason i tried with "my" solution was that it isn't so sensitive in cases where the "lid" isn't guaranteed to be above the terrain triangles at the place of the canal. I sometimes work with terrains that actually are TINs created from topographic data. This data can sometimes be a little bit rough and spiky in some areas. So in order to use "your" solution I will have to triangulate the "lid" so it fits a tiny amount above the terrain surface, instead of just creating a simple "quad-lid" for the canal.
On the other hand I can use "your" solution if i just push down the terrain vertices at the places where the "lid" is....

Share this post


Link to post
Share on other sites
Tested the "JohnBolton" method and it turned out to work fine except for when you are standing in the canal (because then we are standing under the "lid" and have no entries in the stencil buffer). However, this can be solved by also rendering the canal when we draw the terrain:

1. Draw the terrain and canal (and buildings and other stuff...).
2. Draw a "lid" with color-write and z-write off, incrementing the stencil buffer on z-pass.
3. Clear the Z-buffer (to remove the terrain depth values over the canal).
4. Draw the canal using the stencil buffer (if we don't have any stencil entries because we are "in" the canal, the canal is still there, rendered by the first pass).


Now, to take this a bit further and also enable the rendering of tunnels through the terrain... Does anyone know if it is possible to render anything behind a specific polygon (an "invisible" polygon) normally and at the same time make sure that nothing is rendered in front of that polygon, by use of some clever stencil/depth - kung fu...?

/Mike

Share this post


Link to post
Share on other sites
Quote:
Original post by vx7johmi
Now, to take this a bit further and also enable the rendering of tunnels through the terrain... Does anyone know if it is possible to render anything behind a specific polygon (an "invisible" polygon) normally and at the same time make sure that nothing is rendered in front of that polygon, by use of some clever stencil/depth - kung fu...?



It would involve GPU Ninjitsu for which I have yet to attain Grand Master status, but it would involve drawing the invisible polygon with color buffer writes turned off and depth writes turned on. This would also need to be drawn into the stencil buffer. You then set the depth mode to greater than (so any depth value that is behind the invisible pixel will get rendered), while making sure you only draw the pixels that were written into the stencil buffer.

Now I remember why I switched to programming AI :).

Share this post


Link to post
Share on other sites
MENTAL, thanks for the reply!

Quote:
Original post by MENTAL
It would involve GPU Ninjitsu for which I have yet to attain Grand Master status, but it would involve drawing the invisible polygon with color buffer writes turned off and depth writes turned on. This would also need to be drawn into the stencil buffer. You then set the depth mode to greater than (so any depth value that is behind the invisible pixel will get rendered), while making sure you only draw the pixels that were written into the stencil buffer.

Now I remember why I switched to programming AI :).



I thought of that solution before, but wouldn't that make all geometry behind the invisible pixel to be rendered "in the wrong order"? Let's say that, behind the invisible pixel, I have a couple of buildings and after that some terrain. In this case, if I render the buildings and then the terrain, the terrain will pass the depth test (because we have depth mode to greater than) and be rendered "on top/in front" of the building.


/Daniel-San ;)

Share this post


Link to post
Share on other sites
Hmmm... for a tunnel I can see it working maybe like this...

1) Render your terrain as normal
2) Render the cap of your tunnel with z-testing on, colour writes off and stencil writes on appropriately.

3) Clear the depth buffer
4) Render your tunnel with normal z-testing, z-writing and colour writes only where it passes the stencil test.

However, I think this only works if you're outside the tunnel. There is also typically a performance penalty for clearing the depth buffer without clearing the stencil buffer as well.

-Mezz

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!