Jump to content
  • Advertisement
Sign in to follow this  
wildboar

View matrix to reflected view matrix

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

I am trying to make a simple water shader in OpenGL and I need to get the mirrored view matrix for the reflection map of the water.
I made my camera with lookat and I am a bit stuck.

I am following a tutorial on habib's site and he has code like this:


private Matrix reflectionViewMatrix; float reflectionCamZCoord = -cameraPosition.Z + 2*waterHeight; Vector3 reflectionCamPosition = new Vector3(cameraPosition.X, cameraPosition.Y, reflectionCamZCoord); float reflectionTargetZCoord = -targetPos.Z + 2 * waterHeight; Vector3 reflectionCamTarget = new Vector3(targetPos.X, targetPos.Y, reflectionTargetZCoord); Vector3 forwardVector = reflectionCamTarget - reflectionCamPosition; Vector3 sideVector = Vector3.Transform(new Vector3(1, 0, 0), cameraRotation); Vector3 reflectionCamUp = Vector3.Cross(sideVector, forwardVector); reflectionViewMatrix = Matrix.CreateLookAt(reflectionCamPosition, reflectionCamTarget, reflectionCamUp);


but I need something like that for OpenGL, the only things I know is the water Y value, the view matrix and the parameters that go into lookat function.
Can someone please help me

Share this post


Link to post
Share on other sites
Advertisement
Mirroring an object with respect to a plane is independent of the rendering API that is used. If you know the position and the lookat direction of the original camera, you can construct the position and lookat direction of the virtual camera by mirroring the position point and lookat direction vector independently with respect to the plane, and then use the same LookAt function to generate the world (and hence the view) matrix of the virtual camera.

Share this post


Link to post
Share on other sites
To get mirrored view matrix (for reflection) you should multiply your view matrix (generated with lookAt) with mirror matrix of given plane.

M'camera = Mreflection * Mcamera

You can take a look at Parts 1.1 and 1.2 of my reflection tutorial, the construction of reflection matrix is shown there (or check any other 3D/matrix/vector geometry reference).

If your water level in scene is Y, the water plane is (0, 1, 0, -Y) - assuming Y is upwards direction.
You may also want to add extra mirror transformation (around X or Z axis in your case), to keep the winding (culling) order of polygons intact.
You can also use the same mirror matrix to transform the view and target points (and possibly up vector) for lookAt function.

Share this post


Link to post
Share on other sites

To get mirrored view matrix (for reflection) you should multiply your view matrix (generated with lookAt) with mirror matrix of given plane.

M'camera = Mreflection * Mcamera

You can take a look at Parts 1.1 and 1.2 of my reflection tutorial, the construction of reflection matrix is shown there (or check any other 3D/matrix/vector geometry reference).

If your water level in scene is Y, the water plane is (0, 1, 0, -Y) - assuming Y is upwards direction.
You may also want to add extra mirror transformation (around X or Z axis in your case), to keep the winding (culling) order of polygons intact.
You can also use the same mirror matrix to transform the view and target points (and possibly up vector) for lookAt function.


Sweet tutorial dude :)
TIme to study it.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!