# Thread: reflection matrix how to derive

1. ## reflection matrix how to derive

Check out the matrix R in this html page:

Code :
`http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node159.html`

It say, the matrix was derived like this:

The reflection transformation can be decomposed for convenience into a translation to the origin, a rotation mapping the mirror into the XY plane, a scale of -1 in Z, the inverse of the rotation previously used, and a translation back to the mirror location.
Now to the the "mapping the mirror into the XY plane" part. I understand the new z axis in the mirror coordinate system is the normal of the mirror. But how about the x and y axes?

2. ## Re: reflection matrix how to derive

I got it, no problem at all, basically just orthogonal projection with a twist. Very bad explanation, but hey it's free

3. ## Re: reflection matrix how to derive

Ugh, is it possible at all to derive the matrix in way described in the article? I did it like this:

Translate(P)(I-2*V*V^T)Translate(-P)

4. ## Re: reflection matrix how to derive

Code :
```Plane p;
p.FromTriangle(vec3(0,0.5,1),vec3(1,0.7,2),vec3(0,7,0));

Mat4 r = p.MakeReflectionMatrix();
g_Matrix_MV = g_Matrix_MV*r;

-------------------------------------------------
struct Plane{
public:
vec3 norm;
float d;

Plane() { }
Plane(float a, float b, float c, float d) : norm(a,b,c), d(d) { }

void FromTriangle(const vec3&amp; p0,const vec3&amp; p1,const vec3&amp; p2);
void Normalize();
Mat4 MakeReflectionMatrix();

};
----------------------------------------------------------
void Plane::FromTriangle(const vec3&amp; p0,const vec3&amp; p1,const vec3&amp; p2){
norm = cross(p1-p0,p2-p0);
norm.normalize();
d = -dot(norm,p0);
}

void Plane::Normalize(){
float len = norm.x*norm.x+norm.y*norm.y+norm.z*norm.z;
if(len>0.00001f){
len=1.0f/len;
norm.x*=len;	norm.y*=len;	norm.z*=len;	d*=len;
return;
}
norm.y = 1;
}
#include "ILX.h"

Mat4 Plane::MakeReflectionMatrix(){
//Normalize(); // let's expect it to be already normalized
Mat4 m;
m.a00 = -2 * norm.x * norm.x + 1;
m.a10 = -2 * norm.y * norm.x;
m.a20 = -2 * norm.z * norm.x;
m.a30 = 0;

m.a01 = -2 * norm.x * norm.y;
m.a11 = -2 * norm.y * norm.y + 1;
m.a21 = -2 * norm.z * norm.y;
m.a31 = 0;

m.a02 =	-2 * norm.x * norm.z;
m.a12 = -2 * norm.y * norm.z;
m.a22 = -2 * norm.z * norm.z + 1;
m.a32 = 0;

m.a03 =	-2 * norm.x * d;
m.a13 = -2 * norm.y * d;
m.a23 = -2 * norm.z * d;
m.a33 = 1;
return m;
}```

5. ## Re: reflection matrix how to derive

Thanks for the code, but how is it relevant? I've asked about the derivation.

6. ## Re: reflection matrix how to derive

One way to go about it...

For some scale s, plane P and plane normal N, a vertex

V' = V + s N

is a point on the opposite side of P with an equal but negative distance,
which implies that

<V + s N | P> = -<V | P>

Solving for s we have

s = -2 <V|P> / <N|P>.

Taking N to be unit length,

s = -2 <V|P>.

So now V' becomes

V' = V - 2 <V | P> N

= V - 2 N <P | V>,

which when we factor the V yields the matrix form

V' = (I - 2 N P^T) V.

So the overly general and slow form of the psuedo code might be

Vector4 plane(reflectorPlane);

Vector4 normal(reflectorPlane, 0);

Matrix4 reflector = Identity() - Tensor(normal * 2, plane);

Say, this looks a bit the the vector form we all know and love...

7. ## Re: reflection matrix how to derive

What do the operator | and Tensor() function do?

8. ## Re: reflection matrix how to derive

<*|*> is shorthand for dot product and tensor is a somponent-wise vector multiply that results in a matrix (try a google for "vector outerproduct").

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•