diff --git a/frame.c b/frame.c new file mode 100644 index 0000000..f0cb924 --- /dev/null +++ b/frame.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* frame.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/17 19:30:45 by yantoine #+# #+# */ +/* Updated: 2025/02/17 19:30:59 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +int update_frame(t_app *app) +{ + update_camera(app); + render_scene(app); + mlx_put_image_to_window(app->mlx, app->win, app->img, 0, 0); + return (0); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..7ed4719 --- /dev/null +++ b/main.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/17 19:54:03 by yantoine #+# #+# */ +/* Updated: 2025/02/17 19:58:12 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +static int init_app_config(t_app *app, int argc, char **argv) +{ + if (argc > 1) + load_config(argv[1]); + app->win_width = WIDTH; + app->win_height = HEIGHT; + app->move_speed = 0.5f; + app->rot_speed = 0.03f; + app->mouse_sens = 0.002f; + app->key_w = 0; + app->key_s = 0; + app->key_a = 0; + app->key_d = 0; + app->key_left = 0; + app->key_right = 0; + app->key_up = 0; + app->key_down = 0; + return (0); +} + +static int init_mlx_and_image(t_app *app) +{ + app->mlx = mlx_init(); + if (!app->mlx) + { + ft_putstr_fd("Erreur mlx_init\n", 2); + return (1); + } + app->win = mlx_new_window(app->mlx, app->win_width * 2, + app->win_height * 2, "Raytracer interactif"); + if (!app->win) + { + ft_putstr_fd("Erreur mlx_new_window\n", 2); + return (1); + } + app->img = mlx_new_image(app->mlx, app->win_width, app->win_height); + if (!app->img) + { + ft_putstr_fd("Erreur mlx_new_image\n", 2); + return (1); + } + app->pixels = (int *)mlx_get_data_addr(app->img, &app->bpp, + &app->size_line, &app->endian); + if (!app->pixels) + { + ft_putstr_fd("Erreur mlx_get_data_addr\n", 2); + return (1); + } + return (0); +} + +static void setup_hooks(t_app *app) +{ + mlx_hook(app->win, 2, 1L << 0, key_press, app); + mlx_hook(app->win, 3, 1L << 1, key_release, app); + mlx_hook(app->win, 6, 1L << 6, mouse_move, app); + mlx_loop_hook(app->mlx, update_frame, app); +} + +int main(int argc, char **argv) +{ + t_app app; + + init_app_config(&app, argc, argv); + if (init_mlx_and_image(&app)) + return (1); + setup_hooks(&app); + mlx_loop(app.mlx); + return (0); +} diff --git a/miniRT.h b/miniRT.h index 3277896..0380308 100644 --- a/miniRT.h +++ b/miniRT.h @@ -6,15 +6,15 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 20:02:36 by yantoine #+# #+# */ -/* Updated: 2025/02/17 18:58:31 by yantoine ### ########.fr */ +/* Updated: 2025/02/17 19:53:54 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MINIRT_H # define MINIRT_H -# include # include +#include "mlx.h" # include # include # include @@ -34,9 +34,42 @@ # define MAX_AMBIENT 1 # define MAX_CAMERA 1 -// ----- Calcul d'interesction +// ----- Minilibx +typedef struct s_app +{ + void *mlx; + void *win; + void *img; + int *pixels; + int bpp; + int size_line; + int endian; + int win_width; + int win_height; + int key_w; + int key_s; + int key_a; + int key_d; + int key_left; + int key_right; + int key_up; + int key_down; + float move_speed; + float rot_speed; + float mouse_sens; + t_vec3 cam_pos; + t_vec3 cam_dir; + t_vec3 right; + t_vec3 cam_up; + float fov; + float yaw; + float pitch; +} t_app; -typdef struct s_calc + +// ----- Calcul + +typedef struct s_calc { t_vec3 oc; float a; @@ -65,6 +98,17 @@ typdef struct s_calc float t_final; float proj; t_vec3 n; + float ndc_x; + float ndc_y; + float aspect; + float scale; + float screen_x; + float screen_y; + t_vec3 ray_dir; + t_vec3 color; + unsigned char r; + unsigned char g; + unsigned char b; } t_calc; // ----- Espace 3d @@ -182,4 +226,14 @@ float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal); float intersectPlane(Ray ray, Plane p, t_vec3 *hitNormal); float intersectSphere(Ray ray, Sphere s, t_vec3 *hitNormal); bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos); + +// Peripherique +int key_press(int keycode, t_app *app); +int key_release(int keycode, t_app *app); +int mouse_move(int x, int y, t_app *app); + +// Render +void update_camera(t_app *app); +void render_scene(t_app *app); +int update_frame(t_app *app); #endif diff --git a/peripherique.c b/peripherique.c new file mode 100644 index 0000000..fe604db --- /dev/null +++ b/peripherique.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* peripherique.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/17 19:22:27 by yantoine #+# #+# */ +/* Updated: 2025/02/17 19:23:41 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +int key_press(int keycode, t_app *app) +{ + if (keycode == 53) + exit(0); + if (keycode == 13) + app->key_w = 1; + if (keycode == 1) + app->key_s = 1; + if (keycode == 0) + app->key_a = 1; + if (keycode == 2) + app->key_d = 1; + if (keycode == 123) + app->key_left = 1; + if (keycode == 124) + app->key_right = 1; + if (keycode == 126) + app->key_up = 1; + if (keycode == 125) + app->key_down = 1; + return (0); +} + +int key_release(int keycode, t_app *app) +{ + if (keycode == 13) + app->key_w = 0; + if (keycode == 1) + app->key_s = 0; + if (keycode == 0) + app->key_a = 0; + if (keycode == 2) + app->key_d = 0; + if (keycode == 123) + app->key_left = 0; + if (keycode == 124) + app->key_right = 0; + if (keycode == 126) + app->key_up = 0; + if (keycode == 125) + app->key_down = 0; + return (0); +} + +int mouse_move(int x, int y, t_app *app) +{ + static int last_x = -1; + static int last_y = -1; + int dx; + int dy; + + if (last_x == -1 || last_y == -1) + { + last_x = x; + last_y = y; + return (0); + } + dx = x - last_x; + dy = y - last_y; + app->yaw += dx * app->mouse_sens; + app->pitch -= dy * app->mouse_sens; + last_x = x; + last_y = y; + return (0); +} + diff --git a/render.c b/render.c new file mode 100644 index 0000000..293566d --- /dev/null +++ b/render.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/17 19:24:30 by yantoine #+# #+# */ +/* Updated: 2025/02/17 19:52:46 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +static void render_pixel(t_app *app, int x, int y) +{ + t_calc calc; + + calc.ndc_x = (x + 0.5f) / app->win_width; + calc.ndc_y = (y + 0.5f) / app->win_height; + calc.aspect = (float)app->win_width / app->win_height; + calc.scale = tanf((app->fov * 0.5f) * (M_PI / 180.0f)); + calc.screen_x = (2 * calc.ndc_x - 1) * calc.aspect * calc.scale; + calc.screen_y = (1 - 2 * calc.ndc_y) * calc.scale; + calc.ray_dir = vec3_add(app->cam_dir, + vec3_add(vec3_scale(app->right, calc.screen_x), + vec3_scale(app->cam_up, calc.screen_y))); + calc.ray_dir = vec3_normalize(calc.ray_dir); + calc.ray.origin = app->cam_pos; + calc.ray.dir = calc.ray_dir; + calc.color = trace(calc.ray); + calc.r = (unsigned char)(fminf(calc.color.x, 1.0f) * 255); + calc.g = (unsigned char)(fminf(calc.color.y, 1.0f) * 255); + calc.b = (unsigned char)(fminf(calc.color.z, 1.0f) * 255); + app->pixels[y * app->win_width + x] = (255 << 24) | (calc.r << 16) | (calc.g << 8) | calc.b; +} + +void render_scene(t_app *app) +{ + int y; + int x; + + y = 0; + while (y < app->win_height) + { + x = 0; + while (x < app->win_width) + { + render_pixel(app, x, y); + x++; + } + y++; + } +} diff --git a/tags b/tags index ad8c37f..5e223c7 100644 --- a/tags +++ b/tags @@ -84,9 +84,12 @@ ambient_color miniRT.h /^ t_vec3 ambient_color;$/;" m struct:s_ambient typeref: ambient_color raytracer_formatted.c /^t_vec3 ambient_color = {0.1f, 0.1f, 0.1f};$/;" v typeref:typename:t_vec3 ambient_ratio miniRT.h /^ float ambient_ratio;$/;" m struct:s_ambient typeref:typename:float ambient_ratio raytracer_formatted.c /^float ambient_ratio = 0.1f;$/;" v typeref:typename:float +aspect miniRT.h /^ float aspect;$/;" m struct:s_calc typeref:typename:float axis miniRT.h /^ t_vec3 axis; \/\/ Axe normalisé$/;" m struct:s_cylinder typeref:typename:t_vec3 b miniRT.h /^ float b;$/;" m struct:s_calc typeref:typename:float +b miniRT.h /^ unsigned char b;$/;" m struct:s_calc typeref:typename:unsigned char better_ray_tracer README.md /^# better_ray_tracer/;" c +bpp miniRT.h /^ int bpp;$/;" m struct:s_app typeref:typename:int brightness miniRT.h /^ float brightness;$/;" m struct:s_light typeref:typename:float c miniRT.h /^ float c;$/;" m struct:s_calc typeref:typename:float calcLighting trace.c /^t_vec3 calcLighting(t_vec3 hitPoint, t_vec3 hitNormal, t_vec3 objColor) {$/;" f typeref:typename:t_vec3 @@ -94,6 +97,9 @@ camDir miniRT.h /^ t_vec3 camDir;$/;" m struct:s_camera typeref:typename:t_vec3 camDir raytracer_formatted.c /^t_vec3 camDir = {0.0f, 0.0f, -1.0f};$/;" v typeref:typename:t_vec3 camPos miniRT.h /^ t_vec3 camPos;$/;" m struct:s_camera typeref:typename:t_vec3 camPos raytracer_formatted.c /^t_vec3 camPos = {0.0f, 0.0f, 0.0f};$/;" v typeref:typename:t_vec3 +cam_dir miniRT.h /^ t_vec3 cam_dir;$/;" m struct:s_app typeref:typename:t_vec3 +cam_pos miniRT.h /^ t_vec3 cam_pos;$/;" m struct:s_app typeref:typename:t_vec3 +cam_up miniRT.h /^ t_vec3 cam_up;$/;" m struct:s_app typeref:typename:t_vec3 camera miniRT.h /^ t_camera camera;$/;" m struct:s_scene typeref:typename:t_camera center miniRT.h /^ t_vec3 center;$/;" m struct:s_cylinder typeref:typename:t_vec3 center miniRT.h /^ t_vec3 center;$/;" m struct:s_sphere typeref:typename:t_vec3 @@ -106,6 +112,7 @@ color miniRT.h /^ t_vec3 color;$/;" m struct:s_cylinder typeref:typename:t_vec3 color miniRT.h /^ t_vec3 color;$/;" m struct:s_light typeref:typename:t_vec3 color miniRT.h /^ t_vec3 color;$/;" m struct:s_plane typeref:typename:t_vec3 color miniRT.h /^ t_vec3 color;$/;" m struct:s_sphere typeref:typename:t_vec3 +color miniRT.h /^ t_vec3 color;$/;" m struct:s_calc typeref:typename:t_vec3 compute_cap_intersection parsing_cylinder_utils.c /^static void compute_cap_intersection(Ray ray, Cylinder cy, t_calc *calc) {$/;" f typeref:typename:void file: compute_hit_normal parsing_cylinder_utils.c /^static void compute_hit_normal(Ray ray, Cylinder cy, t_calc *calc, t_vec3 *hitNormal) {$/;" f typeref:typename:void file: compute_side_intersection parsing_cylinder_utils.c /^static void compute_side_intersection(Ray ray, Cylinder cy, t_calc *calc) {$/;" f typeref:typename:void file: @@ -118,13 +125,19 @@ d_perp miniRT.h /^ t_vec3 d_perp;$/;" m struct:s_calc typeref:typename:t_vec3 dir miniRT.h /^ t_vec3 dir;$/;" m struct:s_ray typeref:typename:t_vec3 disc miniRT.h /^ float disc;$/;" m struct:s_calc typeref:typename:float dist miniRT.h /^ float dist;$/;" m struct:s_calc typeref:typename:float +endian miniRT.h /^ int endian;$/;" m struct:s_app typeref:typename:int fd_if_exit miniRT.h /^ const int fd_if_exit;$/;" m struct:s_scene typeref:typename:const int fov miniRT.h /^ float fov;$/;" m struct:s_camera typeref:typename:float +fov miniRT.h /^ float fov;$/;" m struct:s_app typeref:typename:float fov raytracer_formatted.c /^float fov = 90.0f;$/;" v typeref:typename:float +g miniRT.h /^ unsigned char g;$/;" m struct:s_calc typeref:typename:unsigned char get_tokens_secure parsing_utils.c /^inline char **get_tokens_secure(t_scene scene, const int numObject, const int numObjectMax, cons/;" f typeref:typename:char ** height miniRT.h /^ float height;$/;" m struct:s_cylinder typeref:typename:float hitPoint miniRT.h /^ t_vec3 hitPoint;$/;" m struct:s_calc typeref:typename:t_vec3 +img miniRT.h /^ void *img;$/;" m struct:s_app typeref:typename:void * +init_app_config main.c /^static int init_app_config(t_app *app, int argc, char **argv)$/;" f typeref:typename:int file: init_intersection parsing_cylinder_utils.c /^static int init_intersection(Ray ray, Cylinder cy, t_calc *calc) {$/;" f typeref:typename:int file: +init_mlx_and_image main.c /^static int init_mlx_and_image(t_app *app)$/;" f typeref:typename:int file: intersectCylinder parsing_cylinder_utils.c /^float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal) {$/;" f typeref:typename:float intersectCylinder raytracer_formatted.c /^float intersectCylinder(Ray ray, Cylinder cy, t_vec3 *hitNormal)$/;" f typeref:typename:float intersectObjects trace.c /^bool intersectObjects(Ray ray, float *tMin, t_vec3 *hitNormal, t_vec3 *objColor) {$/;" f typeref:typename:bool @@ -134,12 +147,29 @@ intersectSphere parsing_sphere.c /^float intersectSphere(Ray ray, Sphere s, t_ve intersectSphere raytracer_formatted.c /^float intersectSphere(Ray ray, Sphere s, t_vec3 *hitNormal)$/;" f typeref:typename:float isInShadow raytracer_formatted.c /^bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos)$/;" f typeref:typename:bool isInShadow shadows.c /^bool isInShadow(t_vec3 hitPoint, t_vec3 lightPos) {$/;" f typeref:typename:bool +key_a miniRT.h /^ int key_a;$/;" m struct:s_app typeref:typename:int +key_d miniRT.h /^ int key_d;$/;" m struct:s_app typeref:typename:int +key_down miniRT.h /^ int key_down;$/;" m struct:s_app typeref:typename:int +key_left miniRT.h /^ int key_left;$/;" m struct:s_app typeref:typename:int +key_press peripherique.c /^int key_press(int keycode, t_app *app)$/;" f typeref:typename:int +key_release peripherique.c /^int key_release(int keycode, t_app *app)$/;" f typeref:typename:int +key_right miniRT.h /^ int key_right;$/;" m struct:s_app typeref:typename:int +key_s miniRT.h /^ int key_s;$/;" m struct:s_app typeref:typename:int +key_up miniRT.h /^ int key_up;$/;" m struct:s_app typeref:typename:int +key_w miniRT.h /^ int key_w;$/;" m struct:s_app typeref:typename:int lights miniRT.h /^ t_light lights[MAX_LIGHTS];$/;" m struct:s_scene typeref:typename:t_light[] line_if_exit miniRT.h /^ const char *line_if_exit;$/;" m struct:s_scene typeref:typename:const char * load_config config.c /^t_scene load_config(const char *filename)$/;" f typeref:typename:t_scene load_config raytracer_formatted.c /^void load_config(const char *filename)$/;" f typeref:typename:void +main main.c /^int main(int argc, char **argv)$/;" f typeref:typename:int main raytracer_formatted.c /^int main(int argc, char *argv[])$/;" f typeref:typename:int +mlx miniRT.h /^ void *mlx;$/;" m struct:s_app typeref:typename:void * +mouse_move peripherique.c /^int mouse_move(int x, int y, t_app *app)$/;" f typeref:typename:int +mouse_sens miniRT.h /^ float mouse_sens;$/;" m struct:s_app typeref:typename:float +move_speed miniRT.h /^ float move_speed;$/;" m struct:s_app typeref:typename:float n miniRT.h /^ t_vec3 n;$/;" m struct:s_calc typeref:typename:t_vec3 +ndc_x miniRT.h /^ float ndc_x;$/;" m struct:s_calc typeref:typename:float +ndc_y miniRT.h /^ float ndc_y;$/;" m struct:s_calc typeref:typename:float normal miniRT.h /^ t_vec3 normal;$/;" m struct:s_plane typeref:typename:t_vec3 numAmbient miniRT.h /^ int numAmbient;$/;" m struct:s_scene typeref:typename:int numCylinders miniRT.h /^ int numCylinders;$/;" m struct:s_scene typeref:typename:int @@ -162,17 +192,26 @@ parsing_line config.c /^static inline t_scene parsing_line(const char *line, t_s parsing_plane parsing_plane.c /^t_scene parsing_plane(const char *line, t_scene scene)$/;" f typeref:typename:t_scene parsing_sphere parsing_sphere.c /^t_scene parsing_sphere(const char *line, t_scene scene)$/;" f typeref:typename:t_scene pitch miniRT.h /^ float pitch; \/\/ vue de haut en bas en radians$/;" m struct:s_camera typeref:typename:float +pitch miniRT.h /^ float pitch;$/;" m struct:s_app typeref:typename:float pitch raytracer_formatted.c /^float pitch = 0.0f; \/\/ vue de haut en bas en radians$/;" v typeref:typename:float +pixels miniRT.h /^ int *pixels;$/;" m struct:s_app typeref:typename:int * planes miniRT.h /^ t_plane planes[MAX_PLANES];$/;" m struct:s_scene typeref:typename:t_plane[] point miniRT.h /^ t_vec3 point;$/;" m struct:s_plane typeref:typename:t_vec3 pos miniRT.h /^ t_vec3 pos;$/;" m struct:s_light typeref:typename:t_vec3 proj miniRT.h /^ float proj;$/;" m struct:s_calc typeref:typename:float +r miniRT.h /^ unsigned char r;$/;" m struct:s_calc typeref:typename:unsigned char radius miniRT.h /^ float radius;$/;" m struct:s_sphere typeref:typename:float radius miniRT.h /^ float radius; \/\/ Demi-diamètre$/;" m struct:s_cylinder typeref:typename:float range_is_ok parsing_color.c /^static inline int range_is_ok(char **token_color)$/;" f typeref:typename:int file: range_is_ok parsing_vector.c /^static inline int range_is_ok(char **token_vector)$/;" f typeref:typename:int file: +ray_dir miniRT.h /^ t_vec3 ray_dir;$/;" m struct:s_calc typeref:typename:t_vec3 +render_pixel render.c /^static void render_pixel(t_app *app, int x, int y)$/;" f typeref:typename:void file: +render_scene render.c /^void render_scene(t_app *app)$/;" f typeref:typename:void +right miniRT.h /^ t_vec3 right;$/;" m struct:s_app typeref:typename:t_vec3 +rot_speed miniRT.h /^ float rot_speed;$/;" m struct:s_app typeref:typename:float s_ambient miniRT.h /^typedef struct s_ambient$/;" s -s_calc miniRT.h /^typdef struct s_calc$/;" s +s_app miniRT.h /^typedef struct s_app$/;" s +s_calc miniRT.h /^typedef struct s_calc$/;" s s_camera miniRT.h /^typedef struct s_camera$/;" s s_cylinder miniRT.h /^typedef struct s_cylinder$/;" s s_light miniRT.h /^typedef struct s_light$/;" s @@ -181,15 +220,21 @@ s_ray miniRT.h /^typedef struct s_ray$/;" s s_scene miniRT.h /^typedef struct s_scene$/;" s s_sphere miniRT.h /^typedef struct s_sphere$/;" s s_vec3 miniRT.h /^typedef struct s_vec3$/;" s +scale miniRT.h /^ float scale;$/;" m struct:s_calc typeref:typename:float +screen_x miniRT.h /^ float screen_x;$/;" m struct:s_calc typeref:typename:float +screen_y miniRT.h /^ float screen_y;$/;" m struct:s_calc typeref:typename:float select_final_intersection parsing_cylinder_utils.c /^static float select_final_intersection(t_calc *calc) {$/;" f typeref:typename:float file: +setup_hooks main.c /^static void setup_hooks(t_app *app)$/;" f typeref:typename:void file: +size_line miniRT.h /^ int size_line;$/;" m struct:s_app typeref:typename:int spheres miniRT.h /^ t_sphere spheres[MAX_SPHERES];$/;" m struct:s_scene typeref:typename:t_sphere[] sqrtDisc miniRT.h /^ float sqrtDisc;$/;" m struct:s_calc typeref:typename:float t miniRT.h /^ float t;$/;" m struct:s_calc typeref:typename:float t0 miniRT.h /^ float t0;$/;" m struct:s_calc typeref:typename:float t1 miniRT.h /^ float t1;$/;" m struct:s_calc typeref:typename:float t_ambient miniRT.h /^} t_ambient;$/;" t typeref:struct:s_ambient +t_app miniRT.h /^} t_app;$/;" t typeref:struct:s_app t_bot miniRT.h /^ float t_bot;$/;" m struct:s_calc typeref:typename:float -t_calc miniRT.h /^} t_calc;$/;" v typeref:struct:s_calc +t_calc miniRT.h /^} t_calc;$/;" t typeref:struct:s_calc t_camera miniRT.h /^} t_camera;$/;" t typeref:struct:s_camera t_cap miniRT.h /^ float t_cap;$/;" m struct:s_calc typeref:typename:float t_cylinder miniRT.h /^} t_cylinder;$/;" t typeref:struct:s_cylinder @@ -205,6 +250,10 @@ t_vec3 miniRT.h /^} t_vec3;$/;" t typeref:struct:s_vec3 token_if_exit miniRT.h /^ const char **token_if_exit;$/;" m struct:s_scene typeref:typename:const char ** trace raytracer_formatted.c /^t_vec3 trace(Ray ray)$/;" f typeref:typename:t_vec3 trace trace.c /^t_vec3 trace(Ray ray) {$/;" f typeref:typename:t_vec3 +update_camera update_camera.c /^void update_camera(t_app *app)$/;" f typeref:typename:void +update_camera_movement update_camera.c /^static void update_camera_movement(t_app *app)$/;" f typeref:typename:void file: +update_camera_rotation update_camera.c /^static void update_camera_rotation(t_app *app)$/;" f typeref:typename:void file: +update_frame frame.c /^int update_frame(t_app *app)$/;" f typeref:typename:int v miniRT.h /^ t_vec3 v;$/;" m struct:s_calc typeref:typename:t_vec3 vec3_add calcul_de_vecteur.c /^t_vec3 vec3_add(t_vec3 a, t_vec3 b)$/;" f typeref:typename:t_vec3 vec3_cross calcul_de_vecteur.c /^t_vec3 vec3_cross(t_vec3 a, t_vec3 b)$/;" f typeref:typename:t_vec3 @@ -214,9 +263,13 @@ vec3_mul calcul_de_vecteur2.c /^t_vec3 vec3_mul(t_vec3 a, t_vec3 b)$/;" f typere vec3_normalize calcul_de_vecteur2.c /^t_vec3 vec3_normalize(t_vec3 a)$/;" f typeref:typename:t_vec3 vec3_scale calcul_de_vecteur.c /^t_vec3 vec3_scale(t_vec3 a, float s)$/;" f typeref:typename:t_vec3 vec3_sub calcul_de_vecteur.c /^t_vec3 vec3_sub(t_vec3 a, t_vec3 b)$/;" f typeref:typename:t_vec3 +win miniRT.h /^ void *win;$/;" m struct:s_app typeref:typename:void * +win_height miniRT.h /^ int win_height;$/;" m struct:s_app typeref:typename:int +win_width miniRT.h /^ int win_width;$/;" m struct:s_app typeref:typename:int x miniRT.h /^ float x;$/;" m struct:s_vec3 typeref:typename:float y miniRT.h /^ float y;$/;" m struct:s_vec3 typeref:typename:float y miniRT.h /^ float y;$/;" m struct:s_calc typeref:typename:float yaw miniRT.h /^ float yaw;\/\/ vue de gauche a droite$/;" m struct:s_camera typeref:typename:float +yaw miniRT.h /^ float yaw;$/;" m struct:s_app typeref:typename:float yaw raytracer_formatted.c /^float yaw = 0.0f;\/\/ vue de gauche a droite$/;" v typeref:typename:float z miniRT.h /^ float z;$/;" m struct:s_vec3 typeref:typename:float diff --git a/update_camera.c b/update_camera.c new file mode 100644 index 0000000..77778a0 --- /dev/null +++ b/update_camera.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* update_camera.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/17 19:27:23 by yantoine #+# #+# */ +/* Updated: 2025/02/17 19:30:13 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +static void update_camera_rotation(t_app *app) +{ + if (app->key_left) + app->yaw -= app->rot_speed; + if (app->key_right) + app->yaw += app->rot_speed; + if (app->key_up) + app->pitch += app->rot_speed; + if (app->key_down) + app->pitch -= app->rot_speed; + if (app->pitch > 1.57f - 0.01f) + app->pitch = 1.57f - 0.01f; + if (app->pitch < -1.57f + 0.01f) + app->pitch = -1.57f + 0.01f; + app->cam_dir.x = cosf(app->pitch) * sinf(app->yaw); + app->cam_dir.y = sinf(app->pitch); + app->cam_dir.z = -cosf(app->pitch) * cosf(app->yaw); + app->cam_dir = vec3_normalize(app->cam_dir); + app->right = (t_vec3){cosf(app->yaw), 0, sinf(app->yaw)}; + app->right = vec3_normalize(app->right); + app->cam_up = vec3_normalize(vec3_cross(app->right, app->cam_dir)); +} + +static void update_camera_movement(t_app *app) +{ + if (app->key_w) + app->cam_pos = vec3_add(app->cam_pos, + vec3_scale(app->cam_dir, app->move_speed)); + if (app->key_s) + app->cam_pos = vec3_sub(app->cam_pos, + vec3_scale(app->cam_dir, app->move_speed)); + if (app->key_a) + app->cam_pos = vec3_sub(app->cam_pos, + vec3_scale(app->right, app->move_speed)); + if (app->key_d) + app->cam_pos = vec3_add(app->cam_pos, + vec3_scale(app->right, app->move_speed)); +} + +void update_camera(t_app *app) +{ + update_camera_rotation(app); + update_camera_movement(app); +}