Implement camera to ray function
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 22 Nov 2024 19:37:00 +0000 (20:37 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 23 Nov 2024 11:05:51 +0000 (12:05 +0100)
src/main.c

index a706b9945f583cd7d80a4b91ba42f0f77697f1cd..8e4f55eeeb1774a23e7a88eb1089aa89d983705f 100644 (file)
@@ -1,3 +1,4 @@
+#include "vec3.h"
 #include "miniRT.h"
 #include <mlx.h>
 #include <X11/X.h>
@@ -79,6 +80,29 @@ void ft_putpx_img(t_img *img, int x, int y, t_color c)
        return ;
 }
 
+t_ray  get_camera_ray(int x, int y, const t_session *s)
+{
+       t_ray                                   res;
+       const float                             x_max = s->img.width;
+       const float                             y_max = s->img.height;
+       const t_camera *const   camera = s->scene.current_camera;
+
+       res.direction = camera->orientation;
+       res.direction =
+               vec_add(
+                       res.direction,
+                       vec_real_mul(
+                               vec_add(
+                                       vec_real_mul(
+                                               camera->up_direction,
+                                               y_max / x_max * (y_max - 1 - 2 * y) / (y_max - 1)),
+                                       vec_real_mul(
+                                               vec_vec_mul(camera->orientation, camera->up_direction),
+                                               (x_max - 1 - 2 * x) / (x_max - 1))),
+                               tan(camera->field_of_view / 2)));
+       return (res);
+}
+
 void   draw(t_session *s)
 {
        int             x;