Parametrize the camera
This commit is contained in:
parent
7e6ba7d445
commit
e5e5863c1d
29
camera.c
29
camera.c
@ -1,23 +1,34 @@
|
|||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
#include "point3.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "vec3.h"
|
||||||
|
|
||||||
void camera_init(Camera *camera, double aspect_ratio) {
|
#include <math.h>
|
||||||
double viewport_height = 2;
|
|
||||||
|
void camera_init(Camera *camera, Point3 look_from, Point3 look_at, Vec3 up,
|
||||||
|
double vertical_fov, double aspect_ratio) {
|
||||||
|
double theta = degrees_to_radians(vertical_fov);
|
||||||
|
double h = tan(theta / 2.0);
|
||||||
|
double viewport_height = 2.0 * h;
|
||||||
double viewport_width = aspect_ratio * viewport_height;
|
double viewport_width = aspect_ratio * viewport_height;
|
||||||
double focal_length = 1.0;
|
|
||||||
|
|
||||||
camera->origin = (Point3){0, 0, 0};
|
Vec3 w = vec3_normalize(point3_sub(look_from, look_at));
|
||||||
camera->horizontal = (Vec3){viewport_width, 0, 0};
|
Vec3 u = vec3_normalize(vec3_cross(up, w));
|
||||||
camera->vertical = (Vec3){0, viewport_height, 0};
|
Vec3 v = vec3_cross(w, u);
|
||||||
|
|
||||||
|
camera->origin = look_from;
|
||||||
|
camera->horizontal = vec3_mul(viewport_width, u);
|
||||||
|
camera->vertical = vec3_mul(viewport_height, v);
|
||||||
Vec3 offset =
|
Vec3 offset =
|
||||||
vec3_add(vec3_div(camera->horizontal, 2), vec3_div(camera->vertical, 2));
|
vec3_add(vec3_div(camera->horizontal, 2), vec3_div(camera->vertical, 2));
|
||||||
offset = vec3_add(offset, (Vec3){0, 0, focal_length});
|
offset = vec3_add(offset, w);
|
||||||
camera->lower_left_corner = point3_add(camera->origin, vec3_neg(offset));
|
camera->lower_left_corner = point3_add(camera->origin, vec3_neg(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ray camera_get_ray(const Camera *camera, double u, double v) {
|
Ray camera_get_ray(const Camera *camera, double s, double t) {
|
||||||
Point3 screen_point = point3_add(
|
Point3 screen_point = point3_add(
|
||||||
camera->lower_left_corner,
|
camera->lower_left_corner,
|
||||||
vec3_add(vec3_mul(u, camera->horizontal), vec3_mul(v, camera->vertical)));
|
vec3_add(vec3_mul(s, camera->horizontal), vec3_mul(t, camera->vertical)));
|
||||||
Vec3 direction = point3_sub(screen_point, camera->origin);
|
Vec3 direction = point3_sub(screen_point, camera->origin);
|
||||||
return (Ray){camera->origin, direction};
|
return (Ray){camera->origin, direction};
|
||||||
}
|
}
|
||||||
|
|||||||
5
camera.h
5
camera.h
@ -12,8 +12,9 @@ typedef struct Camera {
|
|||||||
Vec3 vertical;
|
Vec3 vertical;
|
||||||
} Camera;
|
} Camera;
|
||||||
|
|
||||||
void camera_init(Camera *camera, double aspect_ratio);
|
void camera_init(Camera *camera, Point3 look_from, Point3 look_at, Vec3 up,
|
||||||
|
double vertical_fov, double aspect_ratio);
|
||||||
|
|
||||||
Ray camera_get_ray(const Camera *camera, double u, double v);
|
Ray camera_get_ray(const Camera *camera, double s, double t);
|
||||||
|
|
||||||
#endif /* INCLUDED_CAMERA_H */
|
#endif /* INCLUDED_CAMERA_H */
|
||||||
|
|||||||
5
main.c
5
main.c
@ -74,7 +74,7 @@ int main(void) {
|
|||||||
Sphere sphere_inside_left = {
|
Sphere sphere_inside_left = {
|
||||||
.type = HITTABLE_SPHERE,
|
.type = HITTABLE_SPHERE,
|
||||||
.center = (Point3){-1.0, 0.0, -1.0},
|
.center = (Point3){-1.0, 0.0, -1.0},
|
||||||
.radius = -0.4,
|
.radius = -0.45,
|
||||||
.material = (const Material *)&material_left,
|
.material = (const Material *)&material_left,
|
||||||
};
|
};
|
||||||
Sphere sphere_right = {
|
Sphere sphere_right = {
|
||||||
@ -91,7 +91,8 @@ int main(void) {
|
|||||||
|
|
||||||
/* Camera */
|
/* Camera */
|
||||||
Camera camera;
|
Camera camera;
|
||||||
camera_init(&camera, aspect_ratio);
|
camera_init(&camera, (Point3){-2.0, 2.0, 1.0}, (Point3){0.0, 0.0, -1.0},
|
||||||
|
(Vec3){0.0, 1.0, 0.0}, 20, aspect_ratio);
|
||||||
|
|
||||||
printf("P3\n%u %u\n255\n", image_width, image_height);
|
printf("P3\n%u %u\n255\n", image_width, image_height);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user