# How to properly modify a ray in a raytracer to achieve camera rotation

I am trying to make a particular kind of raytracer, but there is some strange bug occurring when I attempt to rotate the camera.

As you can see, there is some important distortion ocurring when the voxels are closed to the camera and I am not sure what may be cuasing it.

This is how I am calculating my ray:

vec3 r = (vec3(f_coord.xy,1.f/tan(radians(40)))) //initialize the ray for a field of view of 40, f_coord is the pizel coordinate in OpenGL (-1 to 1);
r.y /= aspect_ratio // crunch the y direction to fit the aspect ratio;
vec3 dir = vec3(view_m*vec4(normalize(r),1)); //rotate the ray by the camera's rotation
r+= camera_pos; //Set the ray at the camera's position

• From you video it looks like the ray direction, not ray-origin is being skewed by changes to the position. Does view_m have a translation component ? You should be multiplying normalised r with W=0 also when multiplying direction vectors by a matrix. – PaulHK Jun 22 '18 at 5:43
• I construct the rotation matrix using glm:rotate(mat4, float, vec3). I don't think that creates a translation component. But I agree with your diagnosis – Makogan Jun 22 '18 at 5:47
• I've just editted my comment, try view_m*vec4(normalize(r), 0)); – PaulHK Jun 22 '18 at 5:48
• @PaulHK I did the suggested change however the outcome is the same – Makogan Jun 22 '18 at 6:55
• Presumably you start from 'r' in the direction of 'dir' ? What is the purpose of r+= camera_pos; ? Should you be starting from 'camera_pos' ? – PaulHK Jun 22 '18 at 8:23