As I understand, the 'lookat' method is one of the simplest way to placing/rotate the camera in a scene. So I implemented the Matrix available on (https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/lookat-function) in the code of my ray-tracing but I have no idea of how using it to compute rays.
Basically what I do is place the camera at negatives Z, send a ray to positive Z and select the pixel iterating the X and Y of my view plane. It is easy because the view plane is in front of the camera and I have to simply assign X and Y of my iterations to ray destination X and Y. However I would like to be able to send ray in any part of the space.
Could you please help me to understand how to do that?
Thank you!
What I do basically:
{
double deg = 50.;
double rad = deg / (180.0 / M_PI);
double distance = (WIDTH / 2) * (cotan(rad / 2));
ray.orig.x = HEIGH / 2.0;
ray.orig.y = WIDTH / 2.0;
ray.orig.z = -distance;
y = -1;
while (++y <= HEIGH)
{
x = -1;
while (++x <= WIDTH)
{
ray.dest.x = x - ray.orig.x;
ray.dest.y = y - ray.orig.y;
ray.dest.z = 0. - ray.orig.z;
ray.dest = ve_normalize(&ray.dest);
check_objects(c, &ray, 0);
add_diffuse_light(c);
put_pixel(c, &x, &y);
}
}
}
The functions to handle the lookat matrix:
t_lookat lookati(t_vector *from, t_vector *to)
{
t_lookat lookat;
t_vector fo;
t_vector ri;
t_vector up;
t_vector tmp;
tmp.x = 0; tmp.y = 1; tmp.z = 0;
fo = ve_subtraction(from, to);
fo = ve_normalize(&fo);
ri = ve_cross(&tmp, &fo);
ri = ve_normalize(&ri);
up = ve_cross(&fo, &ri);
up = ve_normalize(&up);
lookat.ri.x = ri.x;
lookat.ri.y = ri.y;
lookat.ri.z = ri.z;
lookat.up.x = up.x;
lookat.up.y = up.y;
lookat.up.z = up.z;
lookat.fo.x = fo.x;
lookat.fo.y = fo.y;
lookat.fo.z = fo.z;
lookat.fr.x = from->x;
lookat.fr.y = from->y;
lookat.fr.z = from->z;
return(lookat);
}
t_vector orientate(t_vector *a, t_vector *from, t_vector *to)
{
t_lookat k;
k = lookati(from, to);
t_vector orientate;
orientate.x = a->x * k.ri.x + a->y * k.up.x + a->z * k.fo.x + a->x * k.fr.x;
orientate.y = a->x * k.ri.y + a->y * k.up.y + a->z * k.fo.y + a->x * k.fr.y;
orientate.z = a->x * k.ri.z + a->y * k.up.z + a->z * k.fo.z + a->x * k.fr.z;
return(orientate);
}