Reflection and Clipping Plane

Started by
3 comments, last by HenriqueRocha 11 years, 10 months ago
Hi again!

I'm on an adventure with the reflection in the water.

I have 3 Render Targets:
- Down Target - where I draw everything below the water height
- Reflect Target - where I draw everything reflected
- Up Target - where I draw everything above the water height

Then I start Render with that order with:

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);


here is a screenshot:
problemaxb.png


Now my problem is to remove the down part from the reflection. By other words, render the down part only where is Water (remove the red area).

PS: I draw the water in the down target. Ignore the trees in the down target.

Any Advice?
Thanks ;-)
Advertisement
One simple way would be to use the stencil buffer. Before you draw your reflections, make a stencil of the water (so render the water plane like you normally would, only writing to the stencil buffer), then when you render your reflections, only draw in the stencil area.

One simple way would be to use the stencil buffer. Before you draw your reflections, make a stencil of the water (so render the water plane like you normally would, only writing to the stencil buffer), then when you render your reflections, only draw in the stencil area.



And how can I write to the stencil buffer?

Thanks ;-)
I'm not sure the specifics with XNA, but here's a good tutorial which explains stencil shadows in XNA. The operations are the same and the tutorial seems to explain some of the general stencil ideas and what it does.

http://msdn.microsoft.com/en-us/library/bb464050(v=xnagamestudio.30).aspx

The basic idea is that the stencil buffer is part of your depth buffer, when you create your device and specify a depth buffer format, you specify one that includes a stencil channel. You can then render into that just like you do the depth/color buffers, but you can also do tests against the values as well. When you render the stencil, you basically just render your water to create it's shape in the stencil buffer. Then when you render your reflections, you test against that shape to see if you should render a pixel or not, only pixels inside your stencil (inside the water shape) will pass the test and be drawn.
Thank you, thank you, thank you, thank you, thank you, thank you.
I am so happy :-)
Here is the result:

yeyj.png

This topic is closed to new replies.

Advertisement