Sign in to follow this  
Trentelshark

Realistic Mirror Behaviour in DX

Recommended Posts

Hi, I searched high and low and was unable to find a decent example of this but perhaps I'm searching for the wrong terms. I am very familiar with the stencil buffer and masking off a section of geometry occupied by a "mirror"; however this of course has one drawback as the object must be in front of the mirror for it to pass the stencil test. What I am looking for is an example showing how to at least emulate a real mirror which will take the angle of the viewer into consideration so geometry which is angled with the mirror will be displayed even if it is not in front of the mirror so to speak. I suppose I could mask of a section of the stencil buffer with the same height as the mirror (but width as long as the full wall for example) and perform visibility testing (using the dot product between the plane's normal and the viewer's angle and reflect this across the plane's normal) but this seems counter-productive to me. I've thought of setting up a frustrum for mirrors to help accomplish this but again this doesn't sound overly intuitive. Does anyone know of a tutorial which may help me better understand how to accomplish this (even if it's via shaders) so I can begin looking into this further? Thanks.

Share this post


Link to post
Share on other sites
I think I may have answered my own question by viewing the contents of the mirror as a bounding box as I mentioned in my previous post. I'll still post my thoughts here in case anyone has feedback.

As a setup for the explanation, you need to visualize the following:

1) A flat mirror object defined with a bounding wall containing the object
2) A viewer who starts out positioned with an angle of 0 between the plane's normal and the current view angles
3) The stencil buffer will be used to create the necessary mask

The contents of the mirror could be viewed as a bounding box extending from the mirror to directly behind the viewer with a near infinite far plane when viewing straight on. At this point, the mask itself is equal to the size of the bounding box as defined by the mirror's min x/miny and max x/max y values.

Now, if the viewer steps off to the side outside of the bounding box they are now angled with the normal vector of the mirror with an angle being easily calculated using the dot product, etc. As the viewer is now angled, the initial bounding box is ALSO angled with the normal vector by the negative angle of the viewer. So, to determine the new mask for the stencil buffer, I could project the bounding box into the stencil buffer so only the necessary area is masked off to avoid unseen overdraw as well as to avoid the potential for geometry being straight ahead of the mirror still being shown.

The calculation would be more involved than the simplified version above (and would contain a translation of the bounding-box based on the viewer's distance from the mirror as well) but I wanted to keep things as simple as possible.

I would of course need to limit this at a certain angle to the mirror as reaching approximately 85 degrees would raise the potential for bizarre artifacts to show due to the sharp angle; however if I am thinking this through correctly I believe this should work out just fine.

Thoughts?

Share this post


Link to post
Share on other sites

Hi,

you could render the scene to texture from the reflected point of the view. Setting the view frustum accoding to the mirror size may result less objects being rendered. The rendered texture needs then to be projected on the mirror surface using the reflected camera matrix.

Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by kauna

Hi,

you could render the scene to texture from the reflected point of the view. Setting the view frustum accoding to the mirror size may result less objects being rendered. The rendered texture needs then to be projected on the mirror surface using the reflected camera matrix.

Good luck!

Good point, that would definitely help to simplify things a bit! Leave it to me to find a super difficult way to do things; though the project I am working on allows a camera to be assigned to any geometry in the world so that would definitely come in handy here.

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this