#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)
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;
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 ;
}
{
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);
}
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)
{