From 950919ae585e897570cfedd55f9cc24c08db9ee1 Mon Sep 17 00:00:00 2001 From: Emily Martins Date: Wed, 13 Nov 2019 01:50:41 +0100 Subject: [PATCH] feat: some more init and multi threading start. --- Makefile | 7 ++- scenes/cornell.json | 46 +++++++++++++++++++ scenes/room.json | 58 ++++++++++++++++++++++++ scenes/shapes_test.json | 53 ++++++++++++++++++++++ src/algebra/shapes/cube_init.c | 35 +++++++++++++++ src/algebra/shapes/disk_init.c | 27 +++++++++++ src/algebra/shapes/init.c | 5 ++- src/algebra/shapes/shape.h | 14 +++++- src/algebra/shapes/shapes.h | 6 ++- src/algebra/shapes/square_init.c | 29 ++++++++++++ src/algebra/shapes/triangle_init.c | 25 +++++++++++ src/compdef.h | 5 +-- src/core/camera/camera.c | 8 ++-- src/core/renderer/renderer.c | 72 +++++++++++++++++++++--------- src/core/renderer/renderer.h | 11 ++++- 15 files changed, 365 insertions(+), 36 deletions(-) create mode 100644 scenes/cornell.json create mode 100644 scenes/room.json create mode 100644 scenes/shapes_test.json create mode 100644 src/algebra/shapes/cube_init.c create mode 100644 src/algebra/shapes/disk_init.c create mode 100644 src/algebra/shapes/square_init.c create mode 100644 src/algebra/shapes/triangle_init.c diff --git a/Makefile b/Makefile index e272a07..1bc0be9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: pacovali +#+ # # +#+ # # Created: 2019/11/05 19:50:44 by pacovali #+# #+# # -# Updated: 2019/11/12 18:19:28 by nmartins ######## odam.nl # +# Updated: 2019/11/13 01:03:26 by nmartins ######## odam.nl # # # # **************************************************************************** # @@ -88,6 +88,9 @@ STNAMES= \ algebra/shapes/init \ algebra/shapes/sphere_init \ algebra/shapes/plane_init \ + algebra/shapes/square_init \ + algebra/shapes/cube_init \ + algebra/shapes/disk_init \ algebra/shapes/move \ algebra/shapes/cone_move \ algebra/shapes/cube_move \ @@ -193,4 +196,4 @@ re: dev: @$(MAKE) -j10 -s @echo "$(TIME) $(CNORM) $(CRED)Running $(CCYAN)$(NAME)$(CDEF)" - @./$(NAME) + @./$(NAME) $(SCENE) diff --git a/scenes/cornell.json b/scenes/cornell.json new file mode 100644 index 0000000..f122bee --- /dev/null +++ b/scenes/cornell.json @@ -0,0 +1,46 @@ +{ + "camera": { + "fov": 50, + "origin": [0, 18, -75] + }, + "objects": [ + { + "type": "square", + "origin": [-15, 40, -20], + "normal": [0, -1, 0], + "side_a": [0, 0, 30], + "side_b": [30, 0, 0], + "emission": [255, 255, 255] + }, + { + "type": "plane", + "origin": [0, 0, 20], + "normal": [0, 0, -1], + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [20, 0, 0], + "normal": [-1, 0, 0], + "color": [100, 255, 100] + }, + { + "type": "plane", + "origin": [-20, 0, 0], + "normal": [1, 0, 0], + "color": [255, 100, 100] + }, + { + "type": "plane", + "origin": [0, 0, 0], + "normal": [0, 1, 0], + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [0, 40, 0], + "normal": [0, -1, 0], + "color": [255, 255, 255] + } + ] +} diff --git a/scenes/room.json b/scenes/room.json new file mode 100644 index 0000000..c7b0743 --- /dev/null +++ b/scenes/room.json @@ -0,0 +1,58 @@ +{ + "camera": { + "fov": 50, + "origin": [0, 18, -50] + }, + "objects": [ + { + "type": "square", + "origin": [-15, 40, -20], + "normal": [0, -1, 0], + "side_a": [0, 0, 30], + "side_b": [30, 0, 0], + "emission": [255, 255, 255] + }, + { + "type": "sphere", + "origin": [-10, 10, 10], + "radius": 10, + "color": [255, 255, 255] + }, + { + "type": "sphere", + "origin": [5, 8, 10], + "radius": 8, + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [0, 0, 20], + "normal": [0, 0, -1], + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [20, 0, 0], + "normal": [-1, 0, 0], + "color": [100, 255, 100] + }, + { + "type": "plane", + "origin": [-20, 0, 0], + "normal": [1, 0, 0], + "color": [255, 100, 100] + }, + { + "type": "plane", + "origin": [0, 0, 0], + "normal": [0, 1, 0], + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [0, 40, 0], + "normal": [0, -1, 0], + "color": [255, 255, 255] + } + ] +} diff --git a/scenes/shapes_test.json b/scenes/shapes_test.json new file mode 100644 index 0000000..68d3ffb --- /dev/null +++ b/scenes/shapes_test.json @@ -0,0 +1,53 @@ +{ + "camera": { + "fov": 50, + "origin": [0, 18, -75] + }, + "objects": [ + { + "type": "disk", + "origin": [0, 40, -20], + "normal": [0, -1, 0], + "outer_radius": 10, + "emission": [5000, 5000, 5000] + }, + { + "type": "square", + "origin": [-5, 10, -20], + "normal": [0, -1, 0], + "side_a": [0, 0, 10], + "side_b": [10, 0, 0], + "color": [50, 50, 50] + }, + { + "type": "plane", + "origin": [0, 0, 20], + "normal": [0, 0, -1], + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [20, 0, 0], + "normal": [-1, 0, 0], + "color": [100, 255, 100] + }, + { + "type": "plane", + "origin": [-20, 0, 0], + "normal": [1, 0, 0], + "color": [255, 100, 100] + }, + { + "type": "plane", + "origin": [0, 0, 0], + "normal": [0, 1, 0], + "color": [255, 255, 255] + }, + { + "type": "plane", + "origin": [0, 40, 0], + "normal": [0, -1, 0], + "color": [255, 255, 255] + } + ] +} diff --git a/src/algebra/shapes/cube_init.c b/src/algebra/shapes/cube_init.c new file mode 100644 index 0000000..6598457 --- /dev/null +++ b/src/algebra/shapes/cube_init.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* cube_init.c :+: :+: */ +/* +:+ */ +/* By: nmartins +#+ */ +/* +#+ */ +/* Created: 2019/11/13 00:05:41 by nmartins #+# #+# */ +/* Updated: 2019/11/13 01:00:56 by nmartins ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "shape.h" + +// typedef struct s_cube +// { +// t_vec origin; +// t_vec size; +// t_vec up_axis; +// t_vec front_axis; +// t_vec right_axis; +// t_square side[6]; +// } t_cube; + +bool cube_init( + t_shape *shape_out, const t_json_value *value) +{ + t_cube *cube; + + (void)value; + shape_out->type = SHAPE_CUBE; + cube = &shape_out->val.as_cube; + /* TODO */ + return (true); +} diff --git a/src/algebra/shapes/disk_init.c b/src/algebra/shapes/disk_init.c new file mode 100644 index 0000000..5db6cfc --- /dev/null +++ b/src/algebra/shapes/disk_init.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* disk_init.c :+: :+: */ +/* +:+ */ +/* By: nmartins +#+ */ +/* +#+ */ +/* Created: 2019/11/13 01:00:23 by nmartins #+# #+# */ +/* Updated: 2019/11/13 01:04:02 by nmartins ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "shape.h" + +bool disk_init( + t_shape *shape_out, const t_json_value *value) +{ + t_disk *disk; + + shape_out->type = SHAPE_DISK; + disk = &shape_out->val.as_disk; + dict_def_vec(value, "origin", (t_vec){0, 0, 0}, &disk->plane.origin); + dict_def_vec(value, "normal", (t_vec){0, 0, 0}, &disk->plane.normal); + disk->inner_radius = dict_def_double(value, "inner_radius", 0.0); + disk->outer_radius = dict_def_double(value, "outer_radius", 0.0); + return (true); +} \ No newline at end of file diff --git a/src/algebra/shapes/init.c b/src/algebra/shapes/init.c index 3ff6d7f..bb28875 100644 --- a/src/algebra/shapes/init.c +++ b/src/algebra/shapes/init.c @@ -6,7 +6,7 @@ /* By: nmartins +#+ */ /* +#+ */ /* Created: 2019/11/12 18:03:38 by nmartins #+# #+# */ -/* Updated: 2019/11/12 20:09:52 by nmartins ######## odam.nl */ +/* Updated: 2019/11/13 01:02:45 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,6 +20,9 @@ const static t_shape_init_kvp g_shape_init_map[] = { { "sphere", sphere_init }, { "plane", plane_init }, + { "square", square_init }, + { "cube", cube_init }, + { "disk", disk_init }, }; static char *get_type(const t_json_value *value) diff --git a/src/algebra/shapes/shape.h b/src/algebra/shapes/shape.h index b58d240..b94e863 100644 --- a/src/algebra/shapes/shape.h +++ b/src/algebra/shapes/shape.h @@ -6,7 +6,7 @@ /* By: pacovali +#+ */ /* +#+ */ /* Created: 2019/01/12 18:56:23 by pacovali #+# #+# */ -/* Updated: 2019/11/12 18:05:52 by nmartins ######## odam.nl */ +/* Updated: 2019/11/13 01:10:44 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -133,6 +133,18 @@ bool sphere_init( bool plane_init( t_shape *shape_out, const t_json_value *value); +bool square_init( + t_shape *shape_out, const t_json_value *value); + +bool cube_init( + t_shape *shape_out, const t_json_value *value); + +bool disk_init( + t_shape *shape_out, const t_json_value *value); + +bool triangle_init( + t_shape *shape_out, const t_json_value *value); + /* ** Moving objects */ diff --git a/src/algebra/shapes/shapes.h b/src/algebra/shapes/shapes.h index f7fb4cb..24ec84d 100644 --- a/src/algebra/shapes/shapes.h +++ b/src/algebra/shapes/shapes.h @@ -5,8 +5,8 @@ /* +:+ */ /* By: pacovali +#+ */ /* +#+ */ -/* Created: 2019/01/12 18:56:23 by pacovali #+# #+# */ -/* Updated: 2019/01/23 17:53:36 by pacovali ######## odam.nl */ +/* Created: 2019/01/12 18:56:23 by pacovali #+# #+# */ +/* Updated: 2019/11/13 01:12:30 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -44,6 +44,8 @@ typedef struct s_square REAL dot_b; } t_square; +// TODO: defo reimplement this using just 3 `t_vec`s + typedef struct s_triangle { t_plane plane; diff --git a/src/algebra/shapes/square_init.c b/src/algebra/shapes/square_init.c new file mode 100644 index 0000000..dc3c930 --- /dev/null +++ b/src/algebra/shapes/square_init.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* square_init.c :+: :+: */ +/* +:+ */ +/* By: nmartins +#+ */ +/* +#+ */ +/* Created: 2019/11/12 23:16:16 by nmartins #+# #+# */ +/* Updated: 2019/11/12 23:21:13 by nmartins ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "shape.h" + +bool square_init( + t_shape *shape_out, const t_json_value *value) +{ + t_square *square; + + shape_out->type = SHAPE_SQUARE; + square = &shape_out->val.as_square; + dict_def_vec(value, "origin", (t_vec){0, 0, 0}, &square->plane.origin); + dict_def_vec(value, "normal", (t_vec){0, 0, 0}, &square->plane.normal); + dict_def_vec(value, "side_a", (t_vec){0, 0, 0}, &square->side_a); + dict_def_vec(value, "side_b", (t_vec){0, 0, 0}, &square->side_b); + square->dot_a = vec_dot(&square->side_a, &square->side_a); + square->dot_b = vec_dot(&square->side_b, &square->side_b); + return (true); +} \ No newline at end of file diff --git a/src/algebra/shapes/triangle_init.c b/src/algebra/shapes/triangle_init.c new file mode 100644 index 0000000..624db49 --- /dev/null +++ b/src/algebra/shapes/triangle_init.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* triangle_init.c :+: :+: */ +/* +:+ */ +/* By: nmartins +#+ */ +/* +#+ */ +/* Created: 2019/11/13 01:10:30 by nmartins #+# #+# */ +/* Updated: 2019/11/13 01:13:12 by nmartins ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "shape.h" + +bool triangle_init( + t_shape *shape_out, const t_json_value *value) +{ + t_triangle *tri; + + shape_out->type = SHAPE_TRIANGLE; + tri = &shape_out->val.as_triangle; + (void)shape_out; + (void)value; + return (true); +} \ No newline at end of file diff --git a/src/compdef.h b/src/compdef.h index 5500029..4e922f4 100644 --- a/src/compdef.h +++ b/src/compdef.h @@ -6,7 +6,7 @@ /* By: nmartins +#+ */ /* +#+ */ /* Created: 2019/10/26 13:20:57 by nmartins #+# #+# */ -/* Updated: 2019/11/12 20:36:35 by nmartins ######## odam.nl */ +/* Updated: 2019/11/13 01:38:24 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,8 +23,7 @@ ** By doing it this way, we are able to ** switch from floats to doubles. */ -# define SUPERSAMPLE 256 -# define SQRT_SUPERS 16 +# define SUPERSAMPLE 1 # define SUCCESS 0 # define FAILURE 1 diff --git a/src/core/camera/camera.c b/src/core/camera/camera.c index 6357917..c6c425a 100644 --- a/src/core/camera/camera.c +++ b/src/core/camera/camera.c @@ -6,7 +6,7 @@ /* By: nmartins +#+ */ /* +#+ */ /* Created: 2019/10/26 16:32:43 by nmartins #+# #+# */ -/* Updated: 2019/11/07 17:20:19 by nmartins ######## odam.nl */ +/* Updated: 2019/11/13 01:37:32 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,9 +36,9 @@ t_point2 supersample(REAL delta, int cur_pos) static REAL super_delta = 0; if (!super_delta) - super_delta = delta / SQRT_SUPERS; - super_pixel.x = super_delta * (cur_pos % SQRT_SUPERS); - super_pixel.y = super_delta * (cur_pos / SQRT_SUPERS); + super_delta = delta / sqrt(SUPERSAMPLE); + super_pixel.x = super_delta * (cur_pos % (int)sqrt(SUPERSAMPLE)); + super_pixel.y = super_delta * (cur_pos / sqrt(SUPERSAMPLE)); return (super_pixel); } diff --git a/src/core/renderer/renderer.c b/src/core/renderer/renderer.c index 077a704..c0306b6 100644 --- a/src/core/renderer/renderer.c +++ b/src/core/renderer/renderer.c @@ -6,12 +6,13 @@ /* By: nmartins +#+ */ /* +#+ */ /* Created: 2019/11/07 16:35:34 by nmartins #+# #+# */ -/* Updated: 2019/11/07 19:46:43 by nmartins ######## odam.nl */ +/* Updated: 2019/11/13 01:50:12 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ #include #include +#include #include "algebra/mmath/mmath.h" #include "renderer.h" @@ -69,60 +70,87 @@ t_vec trace(const t_scene *scene, const t_ray *ray, t_intersection *isect) hemi.x * sys.nb.y + hemi.y * isect->normal.y + hemi.z * sys.nt.y, hemi.x * sys.nb.z + hemi.y * isect->normal.z + hemi.z * sys.nt.z); new_ray.o = isect->p; - // new_ray.o = vec_adds(ray->o, vec_mults_scalar(isect->normal, EPS)); new_ray.depth = ray->depth - 1; new_isect.t = INFINITY; - // ft_printf("ro: %lf %lf %lf, rd: %lf %lf %lf, depth: %hhu\n", - // new_ray.o.x, new_ray.o.y, new_ray.o.z, - // new_ray.d.x, new_ray.d.y, new_ray.d.z, - // new_ray.depth - // ); if (container_is_intersect(&scene->obj_container, &new_ray, &new_isect)) { hemi = trace(scene, &new_ray, &new_isect); + t_vec temp = vec_mults_scalar(isect->obj_ptr->material.color, 1.0 / 255.0); + vec_mult_mut(&hemi, &temp); vec_add_mut(&aggregate_color, &hemi); } } - vec_color_clamp_mut(&aggregate_color); return (aggregate_color); } -void render_image(const t_scene *scene, SDL_Surface *surf) + +void *render_segm(void *data) { + const t_render_segm *segm = data; + t_intersection isect; t_ray ray; t_point2 pixel; pixel = (t_point2){0, 0}; - while (pixel.y < surf->h) + while (pixel.y < segm->end_position.y) { pixel.x = 0; - while (pixel.x < surf->w) + while (pixel.x < segm->end_position.x) { t_vec aggregate = vec_make0(); for (size_t i = 0; i < SUPERSAMPLE; i++) { isect.t = INFINITY; - camera_cast_ray(&scene->camera, &pixel, &ray, i); - if (container_is_intersect(&scene->obj_container, &ray, &isect)) + camera_cast_ray(&segm->scene->camera, &pixel, &ray, i); + if (container_is_intersect(&segm->scene->obj_container, &ray, &isect)) { - t_vec idk = trace(scene, &ray, &isect); + t_vec idk = trace(segm->scene, &ray, &isect); vec_add_mut(&aggregate, &idk); } } vec_mult_mut_scalar(&aggregate, 1.0 / SUPERSAMPLE); vec_color_clamp_mut(&aggregate); - // isect.t = INFINITY; - // camera_cast_ray(&scene->camera, &pixel, &ray); - // if (container_is_intersect(&scene->obj_container, &ray, &isect)) - // ui_put_pixel(surf, - // (size_t)pixel.x, - // (size_t)pixel.y, vec_to_ints(trace(scene, &ray, &isect))); - // else - ui_put_pixel(surf, (size_t)pixel.x, (size_t)pixel.y, vec_to_int(&aggregate)); + ui_put_pixel(segm->surface, (size_t)pixel.x, (size_t)pixel.y, vec_to_int(&aggregate)); pixel.x++; } pixel.y++; } + return (NULL); +} + +#define THREAD_COUNT 8 + +#define MULTITHREAD + +void render_image(const t_scene *scene, SDL_Surface *surf) +{ + t_render_segm segments[THREAD_COUNT]; + pthread_t threads[THREAD_COUNT]; + size_t i; + + i = 0; + while (i < THREAD_COUNT) + { + segments[i].surface = surf; + segments[i].scene = scene; + segments[i].start_position = (t_point2){0, i * surf->h / THREAD_COUNT}; + segments[i].end_position = (t_point2){surf->w, (i + 1) * surf->h / THREAD_COUNT}; +#ifdef MULTITHREAD + pthread_create(&threads[i], NULL, render_segm, &segments[i]); +#else + (void)threads; + render_segm(&segments[i]); +#endif + i++; + } +#ifdef MULTITHREAD + i = 0; + while (i < THREAD_COUNT) + { + pthread_join(threads[i], NULL); + i++; + } +#endif ui_get_fps(1); } diff --git a/src/core/renderer/renderer.h b/src/core/renderer/renderer.h index db1ea3d..2ecb9b3 100644 --- a/src/core/renderer/renderer.h +++ b/src/core/renderer/renderer.h @@ -6,7 +6,7 @@ /* By: nmartins +#+ */ /* +#+ */ /* Created: 2019/11/07 16:32:18 by nmartins #+# #+# */ -/* Updated: 2019/11/07 16:37:39 by nmartins ######## odam.nl */ +/* Updated: 2019/11/13 01:27:28 by nmartins ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,6 +17,15 @@ # include "core/scene/scene.h" +typedef struct s_render_segm +{ + SDL_Surface *surface; + const t_scene *scene; + + t_point2 start_position; + t_point2 end_position; +} t_render_segm; + REAL ui_get_fps(int do_tick); void render_image(const t_scene *scene, SDL_Surface *surf);