# View matrix to reflected view matrix

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

## 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.

##### Share on other sites
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 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 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.