diff --git a/demo.c b/demo.c index 262efff..dc6b731 100644 --- a/demo.c +++ b/demo.c @@ -8,7 +8,7 @@ #define W 800 #define H 600 #define FOCAL_LEN 1.0 -#define VIEWPORT_H 2.0 +#define FOV 120 #define SAMPLES_PER_PIXEL 100 #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) @@ -31,8 +31,8 @@ static pix_t pixbuf[W * H]; int main() { img_t img = { .pix = pixbuf }; - camera_t camera = camera_init( - camera_pos, FOCAL_LEN, VIEWPORT_H, W, H, SAMPLES_PER_PIXEL); + camera_t camera + = camera_init(camera_pos, FOCAL_LEN, FOV, W, H, SAMPLES_PER_PIXEL); camera_render(&camera, scene, NELEMS(scene), &img); ff_write(STDOUT_FILENO, img); diff --git a/include/camera.h b/include/camera.h index 354f14c..a9fdbf5 100644 --- a/include/camera.h +++ b/include/camera.h @@ -15,7 +15,7 @@ typedef struct { } camera_t; camera_t camera_init( - vec3_t pos, double focal_len, double viewport_height, uint32_t img_width, + vec3_t pos, double focal_len, double fov, uint32_t img_width, uint32_t img_height, unsigned samples_per_pixel); void camera_render( diff --git a/src/camera.c b/src/camera.c index 2a41393..efa4f2b 100644 --- a/src/camera.c +++ b/src/camera.c @@ -9,6 +9,10 @@ #include #include +#ifndef M_PI +#define M_PI 3.14159265258979323846264 +#endif + #define MAX_ITER 10 #define MIN_T 1e-6 #define SAMPLE_STDDEV 0.333 @@ -113,12 +117,16 @@ static int render_thread(void *arg) } camera_t camera_init( - vec3_t pos, double focal_len, double viewport_height, uint32_t img_width, + vec3_t pos, double focal_len, double fov, uint32_t img_width, uint32_t img_height, unsigned samples_per_pixel) { const double aspect = (double)img_width / (double)img_height; + + const double fov_rad = M_PI * fov / 180.0; + const double viewport_height = focal_len * tan(fov_rad / 2); const double viewport_width = viewport_height * aspect; const vec3_t viewport_disp = { 0, 0, focal_len }; + const vec3_t u = { viewport_width, 0, 0 }; const vec3_t v = { 0, -viewport_height, 0 }; const vec3_t topleft = vec3_sub(