I'm having fun writing my own raytracer. I'm doing correctly a metal surface ray reflection on world coordinates:
- I have an incident ray in world coords, defined by
origin_pointanddirection_vector; inverse_transform_matrix * origin_pointgives me the ray origin in object space, andinverse_transform_matrix ^ direction_vectorgives me the ray direction in object space (with^I mean matrix-vector multiplication with no translation).- Now I do my calculations to get the hit point and hit normal vector, in object space;
transform_matrix * hit_pointgives me the hit point in world coords, and(inverse_transform_matrix_transposed ^ hit_normal).normalized()gives me theworld_normal_vectorwith 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.