• Advertisement
Sign in to follow this  

Planar reflections

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

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

Share this post


Link to post
Share on other sites
Advertisement
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.z
0 , 0 , 0 , 1


- Jaap

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement