Basic functional raytracer

This commit is contained in:
David 2021-08-23 00:37:57 +02:00
commit c37be6798f
5 changed files with 159 additions and 33 deletions

View file

@ -8,29 +8,45 @@ struct camera {
point3 origin;
point3 lower_left_corner;
vec3 horizontal;
vec3 vertical;
vec3 vertical;
vec3 u,v,w;
double lens_radius;
/* Constructors */
camera()
camera(point3 lookfrom,
point3 lookat,
vec3 vup,
double vfov,
double aspect_ratio,
double aperture,
double focus_dist)
{
double aspect_ratio = 16.0 / 9;
double viewport_height = 2.0;
double theta = degrees_to_radians(vfov);
double h = tan(theta/2);
double viewport_height = 2.0 * h;
double viewport_width = aspect_ratio * viewport_height;
double focal_length = 1.0;
origin = vec3(0,0,0);
horizontal = vec3(viewport_width, 0, 0);
vertical = vec3(0, viewport_height, 0);
lower_left_corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal_length);
w = normalize(lookfrom - lookat);
u = normalize(cross(vup,w));
v = cross(w, u);
origin = lookfrom;
horizontal = focus_dist * viewport_width * u;
vertical = focus_dist * viewport_height * v;
lower_left_corner = origin - horizontal/2 - vertical/2 - focus_dist*w;
lens_radius = aperture/2;
}
/* Methods */
ray get_ray(double u, double v) const
ray get_ray(double s, double t) const
{
return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin);
vec3 rd = lens_radius * random_in_unit_disk();
vec3 offset = u * rd.x + v * rd.y;
return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset);
};
};
#endif