Hello,
I'm programming a C raytracer. I'm encountering some difficulties to realize the camera rotation.
I'm using this function :
void move_cam(t_specs *s, int key)
{
t_camera *cam;
t_vec3 orientation[3];
cam = s->current_cam;
orientation[0] = cam->right;
orientation[1] = cam->up;
orientation[2] = cam->forward;
if (key == KEY_W)
cam->coord = vec_add(cam->coord, cam->up);
else if (key == KEY_S)
cam->coord = vec_sub(cam->coord, cam->up);
else if (key == KEY_A)
cam->coord = vec_sub(cam->coord, cam->right);
else if (key == KEY_D)
cam->coord = vec_add(cam->coord, cam->right);
else if (key == KEY_Q)
cam->coord = vec_sub(cam->coord, cam->vec);
else if (key == KEY_E)
cam->coord = vec_add(cam->coord, cam->vec);
else if (key == KEY_H)
cam->yaw += 0.1;
else if (key == KEY_F)
cam->yaw -= 0.1;
else if (key == KEY_T)
cam->pitch -= 0.1;
else if (key == KEY_G)
cam->pitch += 0.1;
else if (key == KEY_R)
cam->roll += 0.1;
else if (key == KEY_Y)
cam->roll -= 0.1;
make_rotation_z(orientation, cam->roll);
make_rotation_x(rotation, cam->pitch);
mult_matrix(orientation, rotation, orientation);
make_rotation_y(rotation, cam->yaw);
mult_matrix(orientation, rotation, orientation);
s->current_cam->right = orientation[0];
s->current_cam->up = orientation[1];
s->current_cam->forward = orientation[2];
normalize(&s->current_cam->right);
normalize(&s->current_cam->up);
normalize(&s->current_cam->forward);
}
I increment/decrement the angles by pressing keys on the keyboard and then I reconstruct the matrix.
The problem is that this method assumes that the camera has direction (0, 0, -1), right (1, 0, 0, 0) and up (0, 1, 0) and the 3 angles initialized at zero.
When another camera is not initialized like this one but with a random orientation, as soon as I press a key it rotates from the camera directed to (0, 0, -1). (Because all three angles are initialized to 0). I can't figure out how to initialize my angles with my camera.
I would be very grateful for your help in order to be able to finish this camera rotation function.