I'm having fun writing my own raytracer. I'm doing correctly a metal surface ray reflection on world coordinates:
origin_point
and direction_vector
;inverse_transform_matrix * origin_point
gives me the ray origin in object space, and inverse_transform_matrix ^ direction_vector
gives me the ray direction in object space (with ^
I mean matrix-vector multiplication with no translation).transform_matrix * hit_point
gives me the hit point in world coords, and (inverse_transform_matrix_transposed ^ hit_normal).normalized()
gives me the world_normal_vector
with length 1 in world coordinates.Now I can have the mirror-like reflected ray direction in world coords: reflected_dir_vector = direction_vector - 2*(direction_vector • world_normal_vector) * world_normal_vector
(•
is the dot product). The hit point in world coords is the origin point of my new ray.
Now I can check the angles of the incoming ray and reflected ray respect to the normal (still in world coordinates) using the cross product and they are equal; I can also export my vectors in a wavefront file, load it in Blender and see with my eyes that everything is fine - the angles between the vectors are ok and all the vectors are on the same plane. My eyes aren't a math proof but the rendered picture with these calculation is fine.
BUT:
I'd like to calculate the reflected ray direction in objected coordinates and after that go back to world coordinates, due to the way I want my code to work.
I can calculate the reflected vector direction in object space just like before but using object coords: reflected_dir_vector = object_coords_direction_vector - 2*(object_coords_direction_vector • object_coords_normal_vector) * object_coords_normal_vector
. Now I don't get what matrix I should use to move this vector to world coordinates; I tried all my direct, inverse and trasponsed transformation matrices but all of them gives me a wrong reflected direction.
Where's my error?
I'm not showing code to not to make and excessively long post.
Thanks.
If your implementation of multiplication is correct you should use your local to global matrix to transform the reflection from local to world space. You should do: transformation_matrix ^ reflection_vector
. Also make sure that your vectors are normalised.
transforming a normalised vector does not yield a normalised result.