From: Lukas Jiriste Date: Thu, 21 Nov 2024 12:18:22 +0000 (+0100) Subject: Add the structures used for defining the scene X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=3983e00240009981a432a9d62ced1b8d694cc41a;p=42%2FminiRT.git Add the structures used for defining the scene Add the structures that are needed for the scene representation. Also change the functions in main a little to accommodate some of these changes. --- diff --git a/.gitmodules b/.gitmodules index 7594a50..1dc1793 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/inc/miniRT.h b/inc/miniRT.h index 7866a56..40e650f 100644 --- a/inc/miniRT.h +++ b/inc/miniRT.h @@ -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 diff --git a/src/main.c b/src/main.c index ac13219..9144af5 100644 --- a/src/main.c +++ b/src/main.c @@ -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 +#include #include +#include +#include +#include + 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) {