Planar reflections

Started by
3 comments, last by maxest 15 years, 8 months ago
Hello everybody, I am trying to implement planar reflections for mirrors and water but I have run into a little problem. How do I reflect the camera vectors over the mirror plane?


Vector3f ReflectOverPlane(Vector3f& point, Vector3f& plane, float w)
{
	Vector3f planepos = plane*w;

	// Calculate the distance to the plane
	float distance = VectorMath::Dot(plane, point-planepos);

	return point + (plane * distance);
}

void SceneObject::View(reflection_request *req)
{
	Vector3f campos = renderrer.camera->position;
	Vector3f camview = Vector3f(0.0f, 0.0f, -1.0f);

	Vector3f plane = Vector3f(0.0f, 0.0f, -1.0f);

	Vector3f pos = position;
	Vector3f up = Vector3f(0.0f, 1.0f, 0.0f);


	plane *= matrix;

	// Make a plane out of our position
	float w = (float)pos.Length();
	VectorMath::Normalize(plane);

	camview *= matrix;

	Vector3f right = Vector3f(1.0f, 0.0f, 0.0f);
	right *= matrix;

	// Calculate the up vector
	VectorMath::cross(right, camview, up);


	camview += campos;

	campos = ReflectOverPlane(campos, plane, w);
	camview = ReflectOverPlane(camview, plane, w);
	up = ReflectOverPlane(up, plane, 1.0f);
	
	// Set the lookat
	gluLookAt(campos.X(), campos.Y(), campos.Z(),
		camview.X(), camview.Y(), camview.Z(),
		up.X(), up.Y(), up.Z());
}
Could anyone tell me what I am doing wrong and how to correct it. If my code is hard to read/understand because it is unclear let me know too. - Jaap
Advertisement
Can anyone please help me with this?
The best way of doing this is to multiply your current view matrix with a reflection matrix. The method is described here.
Thanks.

So if I got it correct it the matrix would be something like this:


1 - 2*V.x*V.x ,  -2V.x*V.y       ,  -2*V.x*V.z      ,  2*(p.x*v.x + p.y*v.y + p.z*v.z)*v.x-2*V.x*V.y    ,  1 - 2*(V.y*V.y) ,  -2*V.y*V.z      ,  2*(p.x*v.x + p.y*v.y + p.z*v.z)*v.y-2*V.x*V.z    ,  -2V.y*V.z       ,  1 - 2*(V.z*V.z) ,  2*(p.x*v.x + p.y*v.y + p.z*v.z)*v.z0             ,  0               ,  0               ,  1


- Jaap
It's worth-noting that this matrix is a special case of scaling matrix with scale factor of -1. You will find a lot of such useful information in "3D Math Primer for Graphics & Games Development"

This topic is closed to new replies.

Advertisement