Add the structures used for defining the scene
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 21 Nov 2024 12:18:22 +0000 (13:18 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 23 Nov 2024 11:05:50 +0000 (12:05 +0100)
Add the structures that are needed for the scene representation.
Also change the functions in main a little to accommodate some of these
changes.

.gitmodules
inc/miniRT.h
src/main.c

index 7594a50cd70189b4f5b2cccccc1ef3ebd203f9cc..1dc1793fc8a27e59fd36b8e2b64aee98c6eb7f34 100644 (file)
@@ -2,6 +2,7 @@
        path = minilibx-linux
        url = https://github.com/Kycilak/minilibx-linux
        branch = 58-repair-invalid-read
+
 [submodule "Libft"]
        path = Libft
        url = git://ljiriste.work/Libft
index 7866a560cc98d3a97018c65054a3cec6966aac3d..40e650f2e36c7aac23ad14e4461981471e14d812 100644 (file)
@@ -1,4 +1,117 @@
 #ifndef MINIRT_H
 # define MINIRT_H
 
+# include "libft.h"
+
+typedef struct s_vector_3d
+{
+       double  x;
+       double  y;
+       double  z;
+}                      t_vec3;
+
+typedef t_vec3 t_color;
+
+typedef struct s_color_sRGB
+{
+       unsigned char   a;
+       unsigned char   r;
+       unsigned char   g;
+       unsigned char   b;
+}                                      t_color_sRGB;
+
+typedef struct s_sphere
+{
+       t_vec3  center;
+       double  radius;
+       t_color color;
+}                      t_sphere;
+
+typedef struct s_plane
+{
+       t_vec3  point;
+       t_vec3  normal;
+       t_color color;
+}                      t_plane;
+
+typedef struct s_cylinder
+{
+       t_vec3  center;
+       t_vec3  rot_axis;
+       double  radius;
+       double  height;
+}                      t_cylinder;
+
+typedef struct s_light
+{
+       t_vec3  position;
+       double  brightness;
+       t_color color;
+}                      t_light;
+
+typedef struct s_camera
+{
+       t_vec3  position;
+       t_vec3  orientation;
+       t_vec3  up_direction;
+       double  field_of_view;
+}                      t_camera;
+
+union u_object_union
+{
+       t_sphere        sphere;
+       t_plane         plane;
+       t_cylinder      cylinder;
+       t_light         light;
+       t_camera        camera;
+};
+
+enum e_object_type
+{
+       SPHERE,
+       PLANE,
+       CYLINDER,
+       LIGHT,
+       CAMERA,
+};
+
+typedef struct s_object
+{
+       enum e_object_type              type;
+       union u_object_union    object;
+}                                                      t_object;
+
+typedef struct s_ambient_light
+{
+       double  brightness;
+       t_color color;
+}                      t_ambient_light;
+
+typedef struct s_scene
+{
+       t_ambient_light ambient_light;
+       t_vec                   objects;
+       t_camera                *current_camera;
+}                                      t_scene;
+
+typedef struct s_img
+{
+       void    *img;
+       char    *addr;
+       int             bpp;
+       int             bpl;
+       int             endian;
+       int             width;
+       int             height;
+}                      t_img;
+
+typedef struct s_session
+{
+       void    *mlx;
+       void    *win;
+       t_img   img;
+       t_scene scene;
+}                      t_session;
+
+
 #endif // MINIRT_H
index ac13219cbd664e80d2482592278ae087094af65d..9144af538d1b289909cc0473722c1595841194ad 100644 (file)
@@ -1,32 +1,11 @@
 #include "miniRT.h"
-
-typedef struct s_img
-{
-       void    *img;
-       char    *addr;
-       int             bpp;
-       int             bpl;
-       int             endian;
-       int             width;
-       int             height;
-}                      t_img;
-
-typedef struct s_camera
-{
-       t_vec3                  pos;
-       t_quaternion    orientation;
-       float                   field_of_view;
-       t_img                   img;
-}
-
-typedef struct s_session
-{
-       void    *mlx;
-       void    *win;
-       t_cam   camera;
-}                      t_session;
-
+#include <mlx.h>
+#include <X11/X.h>
 #include <X11/keysym.h>
+#include <math.h>
+#include <limits.h>
+#include <stdlib.h>
+
 int    handle_key_press(int keycode, t_session *s)
 {
        if (keycode == XK_Escape)
@@ -61,27 +40,74 @@ void        *get_pixel(t_img *img, int x, int y)
        return (img->addr + y * img->bpl + x * img->bpp / CHAR_BIT);
 }
 
+double channel_sRGB_to_lin(unsigned char sRGB_channel)
+{
+       double  normalized;
+
+       normalized = sRGB_channel / 255.;
+       if (normalized < 0.04045)
+               return (normalized / 12.92);
+       else
+               return (pow((normalized + 0.055) / 1.055, 2.4));
+}
+
+t_color        color_sRGB_to_lin(t_color_sRGB sRGB)
+{
+       t_color res;
+
+       res.x = channel_sRGB_to_lin(sRGB.r);
+       res.y = channel_sRGB_to_lin(sRGB.g);
+       res.z = channel_sRGB_to_lin(sRGB.b);
+       return (res);
+}
+
+unsigned char  channel_lin_to_sRGB(double lin_channel)
+{
+       if (lin_channel < 0)
+               lin_channel = 0;
+       else if (lin_channel > 1)
+               lin_channel = 1;
+       if (lin_channel < 0.0031308)
+               return (255 * lin_channel * 12.92);
+       else
+               return (255 * (1.055 * pow(lin_channel, 1/2.4) - 0.055));
+}
+
+t_color_sRGB   color_lin_to_sRGB(t_color c)
+{
+       t_color_sRGB    res;
+
+       res.a = 255;
+       res.r = channel_lin_to_sRGB(c.x);
+       res.g = channel_lin_to_sRGB(c.y);
+       res.b = channel_lin_to_sRGB(c.z);
+       return (res);
+}
+
 void   ft_putpx_img(t_img *img, int x, int y, t_color c)
 {
        char    *px_addr;
+       t_color_sRGB    sRGB_c;
 
+       sRGB_c = color_lin_to_sRGB(c);
        px_addr = get_pixel(img, x, y);
        if (img->endian)
        {
-               px_addr[0] = c.a;
-               px_addr[1] = c.r;
-               px_addr[2] = c.g;
-               px_addr[3] = c.b;
+               px_addr[0] = sRGB_c.a;
+               px_addr[1] = sRGB_c.r;
+               px_addr[2] = sRGB_c.g;
+               px_addr[3] = sRGB_c.b;
        }
        else
        {
-               px_addr[3] = c.a;
-               px_addr[2] = c.r;
-               px_addr[1] = c.g;
-               px_addr[0] = c.b;
+               px_addr[3] = sRGB_c.a;
+               px_addr[2] = sRGB_c.r;
+               px_addr[1] = sRGB_c.g;
+               px_addr[0] = sRGB_c.b;
        }
        return ;
 }
+
 void   draw(t_session *s)
 {
        int             x;
@@ -94,12 +120,12 @@ void       draw(t_session *s)
                y = 0;
                while (y < s->img.height)
                {
-                       ft_putpx_img(&s->img, x, y, trace_ray(x, y, &s->cam));
+                       ft_putpx_img(&s->img, x, y, trace_ray(x, y, &s->scene));
                        ++y;
                }
                ++x;
        }
-       mlx_put_image_to_window(s->mlx, s->win, s->cam.img.img, 0, 0);
+       mlx_put_image_to_window(s->mlx, s->win, s->img.img, 0, 0);
        return ;
 }
 
@@ -107,11 +133,8 @@ void       init_session(t_session *s)
 {
        s->mlx = mlx_init();
        s->win = mlx_new_window(s->mlx, s->img.width, s->img.height, "miniRT");
-       s->cam.pos = ;
-       s->cam.orientation = ;
-       s->cam.field_of_view = ;
-       s->cam.img.img = mlx_new_image(s->mlx, s->img.width, s->img.height);
-       s->cam.img.addr = mlx_get_data_addr(s->img.img,
+       s->img.img = mlx_new_image(s->mlx, s->img.width, s->img.height);
+       s->img.addr = mlx_get_data_addr(s->img.img,
                        &s->img.bpp, &s->img.bpl, &s->img.endian);
 }
 
@@ -131,15 +154,16 @@ static void       free_session(t_session *s)
 
 void   cleanup(t_session *s)
 {
-       mlx_destroy_image(s->mlx, s->cam.img.img);
+       mlx_destroy_image(s->mlx, s->img.img);
        free_session(s);
        return ;
 }
 
 void   set_defaults(t_session *s)
 {
-       s->cam.img.width = 1920;
-       s->cam.img.height = 1011;
+       s->img.width = 1920;
+       s->img.height = 1011;
+}
 
 int    main(int argc, char **argv)
 {