Jump to content
  • Advertisement
Sign in to follow this  
Oscon

Detecting if a model is hidden behind geometry.

This topic is 3304 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

In some games, including the recent Torchlight, models (partially) hidden behind geometry have their hidden portions rendered in a different way. How is this done using D3D? Is it simply some alphablending magic, or a stencil/zbuffer trick? I'm a beginner programmer, so bear with me if this is common knowledge. :) This is a feature i'd like to add in my own little project. Here's a picture which describes what i mean. The main character is rendered half normal/half ghostly: http://home.broadpark.no/~ohu/torchlight.jpg I'm not interested in the ghostly effect itself, just how to detect if parts of a mesh are hidden, and if possible, how much of it (in percentage). In addition, when i'm rendering the hidden part, how can i avoid rendering the visible part? This is assuming i have to render the mesh twice, to turn off the texture.

Share this post


Link to post
Share on other sites
Advertisement
The easiest way would be to render the scene normally, then render the character again, setting the depth-test to GREATER. This will cause only those parts of the model that are hidden to be drawn, on top of closer geometry. Use different colors or shaders, or alpha blending, when drawing the model the second time, to make it look different.
To determine how much of it is hidden is harder. Either you can do it geometrically, by clipping the geometry, or if available you can use occlusion queries. These are a way of detecting how many pixels pass the depth-test. Exactly how it's done depends on your API, but you basically draw the model without anything else in the scene, check how many pixels that was, draw the whole scene, and finally draw the model again with EQUAL depth test. Then check how many pixels of the model are actually visible (drawn in the second pass), and then you can calculate the percentage.

Share this post


Link to post
Share on other sites
I guess they're just rendering the models twice: Once with normal shading and Z-Test and once using the ghost-effect with Z-Test set to GREATER and Z-writing disabled.

For determining how much of the model is hidden, probably Occlusion Queries are your best option, but IMO it's quite hard to use them efficiently (especially if you're a novice to Direct3D)

Share this post


Link to post
Share on other sites
Thanks both for the z-test idea. Sounds like that's what i'm looking for. The reason i wanted to programatically find out IF a mesh was partially hidden, was to avoid rendering a mesh twice in case it was 100% visible.

But i guess rendering every unit twice (i only got 10 or so in the scene at any time) is faster than implementing a hefty occlusion thingy. :)

Thanks again!

Share this post


Link to post
Share on other sites
Stencil buffer may do what you want, too. Render the torchlight's volume as a sphere (or whatever shape you want), only setting stencil, and not outputting color or depth.

Then, you can render your your character (or anything) testing whether stencil is set or is not set (or even some particular value) and do whatever you want based on that. It will skip all fragments where the condition that you check for is not met.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oscon
Thanks both for the z-test idea. Sounds like that's what i'm looking for. The reason i wanted to programatically find out IF a mesh was partially hidden, was to avoid rendering a mesh twice in case it was 100% visible.


Use an approximation test, like a bounding sphere. It should be a fairly cheap test to see if a sphere is not covered at all by another sphere, in which case you can use a faster draw-method.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oscon
Thanks both for the z-test idea. Sounds like that's what i'm looking for. The reason i wanted to programatically find out IF a mesh was partially hidden, was to avoid rendering a mesh twice in case it was 100% visible.

But i guess rendering every unit twice (i only got 10 or so in the scene at any time) is faster than implementing a hefty occlusion thingy. :)

Thanks again!

I think that for a small number of units you're right, and just rendering them twice will work well. In D3D10 you can also output vertex processing results using stream output and reuse them, to save any heavy vertex shader work you may be doing.

Also, you can use the stencil to do everything in a single pass, although you won't get the exact result you have there (unless you read from the stencil, which complicates things). Just draw the entire scene, then draw the characters, marking in the stencil when depth tests failed. Then draw a full screen quad.

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!