 # Mirror Matrices

Hi,
I would like to do a mirror/reflection effect
in Opengl. The technique has to be general and work for arbitrary oriented planes,so the glScale approach does not work.
My problem is having isto create the mirror matrix for mirroring the objects around a
mirror plane.

Cheers
Dimi

OK, assume you have a point P that you want to reflect across a plane with normal N. Pick any vertex on the plane and call it V.
Now, calculate D = (P-V) dot N, this will give you the distance from the plane to point P in the direction of the normal N. Now, to reflect the point P, translate it by 2 times that distance D in the direction opposite of normal N. In otherwords, your new point is P - 2DN.

You want code? OK. Im just going to cut and paste this out of my CMatrix class, so some details you will need to fill in for yourself, but it should be pretty self expanatory.

CMatrix CMatrix::getReflection(CPlane& plane)
{
CMatrix mat;
float a = plane.A;
float b = plane.B;
float c = plane.C;
float d = plane.D;

``````mat.m._00 = 1-2*a*a;	mat.m._01 = -2*a*b;	mat.m._02 = -2*a*c;	mat.m._03 = -2*a*d;
mat.m._10 = -2*a*b; 	mat.m._11 = 1-2*b*b;	mat.m._12 = -2*b*c;	mat.m._13 = -2*b*d;
mat.m._20 = -2*a*c;	mat.m._21 = -2*b*c;	mat.m._22 = 1-2*c*c;	mat.m._23 = -2*c*d;
mat.m._30 = 0.0f;		mat.m._31 = 0.0f;		mat.m._32 = 0.0f;		mat.m._33 = 1.0f;

return mat;
``````

};

Thanks,
I understood the operations with the vectors
and the plane, but I just do not understand how the 4x4 matrix is derived.
But even like that I think its very usefull.

Cheers

Deriving the matrix can be tricky. Let me try to help you out with this one. Once you see how to do it, it will help you figure out a lot of stuff. Try to hang with me here, as the logic can be a bit difficult to expain in text.

Lets assume we want to build a 4x4 matrix M, which we can multiply by a 4x1 vector P. When we multiply M*P we will get the reflected vector R. To make things simpiler, lets look at how to generate the first row of matrix M. When you think about it, to get the x component of vector R, we multiply (the first row of M) * P. Now, lets see again how we calculated the x component of reflected point again:

R = P-2*((P-V) dot N)*N

lets remove V from the equation. Lets use the plane description (A,B,C,D) where the normal N is actually the vector (A,B,C). We now have:

R = P-2*((P dot N)-D)*N

thus

R.x = P.x - 2*(((P dot N)-D)*N.x
noting that P = (P.x,P.y,P.z) and that N = (N.x,N.y,N.z) = (A,B,C) we can rewrite this again (expanding the dot product too) as

R.x = P.x - 2*(P.xA + P.yB + P.z*C -D)*A

distributing the terms -2 and A, we get

R.x = P.x - 2AP.xA - 2AP.yB - 2AP.zC - 2A*D

regrouping terms and factoring out the P.* terms:

R.x = P.x*(1 - 2AA) + P.y*(-2AB) + P.z*(-2AC) + 1*(-2AD)

Noting that our coefficients of (P.x, P.y, P.z, 1) make our 4x1 vector P, it should be fairly simple to see how to put the remainder of the components into the first row of the matrix M. Repeat this process for the other 3 rows, and you should get the whole matrix.

Does this help? Kind of obscure in text, but hopefully it will be enough to point you in the right direction so you can solve these types of things on your own in the future. I had to use this technique to develop this reflection matrix function by hand, as I could not find a generic reflection matrix function anywhere I looked.

[This message has been edited by LordKronos (edited 10-12-2000).]

Thanks…
Now I have understood it. Had to be a
quite difficult to find it out yourself.
Well done.

Just one clarification for the other users regarding the transformation from
R = P-2*((P-V) dot N)N
to
R = P-2
((P dot N)-D)N
which was the only difficult point.
V (representing a point on the plane) was replace with the vector representation of a plane. The vector representation of aplane is
taken from the classical (ABCD) representation as follows
Vector representation : Xn dot X= d
Xn normal = (A,B,C)
X any point on plane
d float = -D
transforming the cector representation we get X=-d
Xn
substituting this X instead of V we get
we represent Xn as N here

P- 2*(P-(-dN) dot N) * N
thus
P- 2
(P dot N -(-dN dot N)) N
but -d
N dot N = -d because N dot N =1
so we get
P - 2
((P dot N)-D) * N

Thanks a lot
Dimi