Split scene definition to use it in both raytracer and rasteriser
This commit is contained in:
		
							
								
								
									
										1
									
								
								compile
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								compile
									
									
									
									
									
								
							| @@ -6,6 +6,7 @@ LIBS="$(pkg-config --libs sdl2) -lm" | ||||
|  | ||||
| RAYTRACER_SRC="src/window/*.c \ | ||||
| 	src/vector/*.c \ | ||||
| 	src/scene/*.c \ | ||||
| 	src/raytracer/*.c \ | ||||
| 	src/math/*.c \ | ||||
| " | ||||
|   | ||||
| @@ -2,41 +2,10 @@ | ||||
| #define RAYTRACER_H | ||||
|  | ||||
| #include "c_cpp_aliases/aliases.h" | ||||
| #include "scene/scene.h" | ||||
| #include "vector/vec.h" | ||||
| #include "window/window.h" | ||||
|  | ||||
| typedef struct { | ||||
|   f32 radius; | ||||
|   vec3f_t centre; | ||||
|   colour_t colour; | ||||
|   f32 specular; | ||||
|   f32 reflective; | ||||
| } sphere_t; | ||||
|  | ||||
| typedef enum { | ||||
|   LIGHT_TYPE_POINT, | ||||
|   LIGHT_TYPE_DIRECTIONAL, | ||||
|   LIGHT_TYPE_AMBIENT, | ||||
|  | ||||
|   COUNT_LIGHT_TYPE, | ||||
| } light_type_t; | ||||
|  | ||||
| typedef struct { | ||||
|   light_type_t type; | ||||
|   f32 intensity; | ||||
|   union { | ||||
|     vec3f_t position; | ||||
|     vec3f_t direction; | ||||
|   }; | ||||
| } light_t; | ||||
|  | ||||
| typedef struct { | ||||
|   sphere_t *spheres; | ||||
|   light_t *lights; | ||||
|   u32 spheres_count; | ||||
|   u32 lights_count; | ||||
| } scene_t; | ||||
|  | ||||
| typedef struct { | ||||
|   f32 t1; | ||||
|   f32 t2; | ||||
| @@ -44,7 +13,7 @@ typedef struct { | ||||
|  | ||||
| typedef struct { | ||||
|   f32 closest_t; | ||||
|   sphere_t *closest_sphere; | ||||
|   const sphere_t *closest_sphere; | ||||
| } intersection_t; | ||||
|  | ||||
| colour_t trace_ray(vec3f_t origin, vec3f_t direction, f32 t_min, f32 t_max, | ||||
|   | ||||
							
								
								
									
										41
									
								
								include/scene/scene.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								include/scene/scene.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| #ifndef SCENE_H | ||||
| #define SCENE_H | ||||
|  | ||||
| #include "c_cpp_aliases/aliases.h" | ||||
| #include "window/window.h" | ||||
|  | ||||
| typedef struct { | ||||
|   f32 radius; | ||||
|   vec3f_t centre; | ||||
|   colour_t colour; | ||||
|   f32 specular; | ||||
|   f32 reflective; | ||||
| } sphere_t; | ||||
|  | ||||
| typedef enum { | ||||
|   LIGHT_TYPE_POINT, | ||||
|   LIGHT_TYPE_DIRECTIONAL, | ||||
|   LIGHT_TYPE_AMBIENT, | ||||
|  | ||||
|   COUNT_LIGHT_TYPE, | ||||
| } light_type_t; | ||||
|  | ||||
| typedef struct { | ||||
|   light_type_t type; | ||||
|   f32 intensity; | ||||
|   union { | ||||
|     vec3f_t position; | ||||
|     vec3f_t direction; | ||||
|   }; | ||||
| } light_t; | ||||
|  | ||||
| typedef struct { | ||||
|   const sphere_t *spheres; | ||||
|   const light_t *lights; | ||||
|   u32 spheres_count; | ||||
|   u32 lights_count; | ||||
| } scene_t; | ||||
|  | ||||
| const scene_t *build_test_scene(void); | ||||
|  | ||||
| #endif // !SCENE_H | ||||
| @@ -1,5 +1,6 @@ | ||||
| #include "c_cpp_aliases/aliases.h" | ||||
| #include "raytracer/raytracer.h" | ||||
| #include "scene/scene.h" | ||||
| #include "vector/vec.h" | ||||
| #include "window/window.h" | ||||
| #include <SDL2/SDL_events.h> | ||||
| @@ -8,7 +9,6 @@ | ||||
| #include <stdbool.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #define ARR_LEN(ARR) sizeof(ARR) / sizeof(ARR[0]) | ||||
| #define RECURSION_DEPTH 3 | ||||
|  | ||||
| typedef struct { | ||||
| @@ -31,72 +31,11 @@ i32 main(i32 argc, char *argv[]) { | ||||
|     return EXIT_FAILURE; | ||||
|   } | ||||
|  | ||||
|   const scene_t *scene = build_test_scene(); | ||||
|  | ||||
|   bool running = true; | ||||
|   SDL_Event event = {0}; | ||||
|  | ||||
|   sphere_t spheres[] = { | ||||
|       (sphere_t){ | ||||
|           .radius = 1.0f, | ||||
|           .centre = (vec3f_t){.x = 0.0f, .y = -1.0f, .z = 3.0f}, | ||||
|           .colour = | ||||
|               (colour_t){ | ||||
|                   .rgba.r = 245, .rgba.g = 238, .rgba.b = 158, .rgba.a = 255}, | ||||
|           .specular = 500.0f, | ||||
|           .reflective = 0.3f, | ||||
|       }, | ||||
|       (sphere_t){ | ||||
|           .radius = 1.0f, | ||||
|           .centre = (vec3f_t){.x = -2.0f, .y = 0.0f, .z = 4.0f}, | ||||
|           .colour = | ||||
|               (colour_t){ | ||||
|                   .rgba.r = 59, .rgba.g = 142, .rgba.b = 165, .rgba.a = 255}, | ||||
|           .specular = 10.0f, | ||||
|           .reflective = 0.1f, | ||||
|       }, | ||||
|       (sphere_t){ | ||||
|           .radius = 1.0f, | ||||
|           .centre = (vec3f_t){.x = 2.0f, .y = 0.0f, .z = 4.0f}, | ||||
|           .colour = | ||||
|               (colour_t){ | ||||
|                   .rgba.r = 171, .rgba.g = 52, .rgba.b = 40, .rgba.a = 255}, | ||||
|           .specular = 500.0f, | ||||
|           .reflective = 0.4f, | ||||
|       }, | ||||
|       (sphere_t){ | ||||
|           .radius = 5000.0f, | ||||
|           .centre = (vec3f_t){.x = 0.0f, .y = -5001.0f, .z = 0.0f}, | ||||
|           .colour = | ||||
|               (colour_t){ | ||||
|                   .rgba.r = 255, .rgba.g = 255, .rgba.b = 0, .rgba.a = 255}, | ||||
|           .specular = 1000.0f, | ||||
|           .reflective = 0.5f, | ||||
|       }, | ||||
|   }; | ||||
|  | ||||
|   light_t lights[] = { | ||||
|       (light_t){ | ||||
|           .type = LIGHT_TYPE_AMBIENT, | ||||
|           .intensity = 0.2f, | ||||
|       }, | ||||
|       (light_t){ | ||||
|           .type = LIGHT_TYPE_POINT, | ||||
|           .intensity = 0.6f, | ||||
|           .position = (vec3f_t){.x = 2.0f, .y = 1.0f, .z = 0.0f}, | ||||
|       }, | ||||
|       (light_t){ | ||||
|           .type = LIGHT_TYPE_DIRECTIONAL, | ||||
|           .intensity = 0.2f, | ||||
|           .direction = (vec3f_t){.x = 1.0f, .y = 4.0f, .z = 4.0f}, | ||||
|       }, | ||||
|   }; | ||||
|  | ||||
|   scene_t scene = { | ||||
|       .spheres = spheres, | ||||
|       .lights = lights, | ||||
|       .spheres_count = ARR_LEN(spheres), | ||||
|       .lights_count = ARR_LEN(lights), | ||||
|   }; | ||||
|  | ||||
|   i32 w_min = ((i32)window.half_width) * -1; | ||||
|   i32 w_max = (i32)window.half_width; | ||||
|   i32 h_min = ((i32)window.half_height) * -1; | ||||
| @@ -118,7 +57,7 @@ i32 main(i32 argc, char *argv[]) { | ||||
|         vec3f_t direction = window_to_viewport(&window, x, y, viewport); | ||||
|         vec3f_t rotated = vec_rotate_vec3f_t(direction, camera.rotation); | ||||
|         colour_t colour = trace_ray(camera.position, rotated, 1, INFINITY, | ||||
|                                     &scene, bg, RECURSION_DEPTH); | ||||
|                                     scene, bg, RECURSION_DEPTH); | ||||
|         set_pixel(&window, x, y, colour); | ||||
|       } | ||||
|     } | ||||
|   | ||||
							
								
								
									
										69
									
								
								src/scene/scene.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/scene/scene.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | ||||
| #include "scene/scene.h" | ||||
| #include "c_cpp_aliases/aliases.h" | ||||
|  | ||||
| #define ARR_LEN(ARR) sizeof(ARR) / sizeof(ARR[0]) | ||||
|  | ||||
| internal const sphere_t spheres[] = { | ||||
|     (sphere_t){ | ||||
|         .radius = 1.0f, | ||||
|         .centre = (vec3f_t){.x = 0.0f, .y = -1.0f, .z = 3.0f}, | ||||
|         .colour = | ||||
|             (colour_t){ | ||||
|                 .rgba.r = 245, .rgba.g = 238, .rgba.b = 158, .rgba.a = 255}, | ||||
|         .specular = 500.0f, | ||||
|         .reflective = 0.3f, | ||||
|     }, | ||||
|     (sphere_t){ | ||||
|         .radius = 1.0f, | ||||
|         .centre = (vec3f_t){.x = -2.0f, .y = 0.0f, .z = 4.0f}, | ||||
|         .colour = | ||||
|             (colour_t){ | ||||
|                 .rgba.r = 59, .rgba.g = 142, .rgba.b = 165, .rgba.a = 255}, | ||||
|         .specular = 10.0f, | ||||
|         .reflective = 0.1f, | ||||
|     }, | ||||
|     (sphere_t){ | ||||
|         .radius = 1.0f, | ||||
|         .centre = (vec3f_t){.x = 2.0f, .y = 0.0f, .z = 4.0f}, | ||||
|         .colour = | ||||
|             (colour_t){ | ||||
|                 .rgba.r = 171, .rgba.g = 52, .rgba.b = 40, .rgba.a = 255}, | ||||
|         .specular = 500.0f, | ||||
|         .reflective = 0.4f, | ||||
|     }, | ||||
|     (sphere_t){ | ||||
|         .radius = 5000.0f, | ||||
|         .centre = (vec3f_t){.x = 0.0f, .y = -5001.0f, .z = 0.0f}, | ||||
|         .colour = | ||||
|             (colour_t){ | ||||
|                 .rgba.r = 255, .rgba.g = 255, .rgba.b = 0, .rgba.a = 255}, | ||||
|         .specular = 1000.0f, | ||||
|         .reflective = 0.5f, | ||||
|     }, | ||||
| }; | ||||
|  | ||||
| internal const light_t lights[] = { | ||||
|     (light_t){ | ||||
|         .type = LIGHT_TYPE_AMBIENT, | ||||
|         .intensity = 0.2f, | ||||
|     }, | ||||
|     (light_t){ | ||||
|         .type = LIGHT_TYPE_POINT, | ||||
|         .intensity = 0.6f, | ||||
|         .position = (vec3f_t){.x = 2.0f, .y = 1.0f, .z = 0.0f}, | ||||
|     }, | ||||
|     (light_t){ | ||||
|         .type = LIGHT_TYPE_DIRECTIONAL, | ||||
|         .intensity = 0.2f, | ||||
|         .direction = (vec3f_t){.x = 1.0f, .y = 4.0f, .z = 4.0f}, | ||||
|     }, | ||||
| }; | ||||
|  | ||||
| internal scene_t scene = (scene_t){ | ||||
|     .spheres = spheres, | ||||
|     .lights = lights, | ||||
|     .spheres_count = ARR_LEN(spheres), | ||||
|     .lights_count = ARR_LEN(lights), | ||||
| }; | ||||
|  | ||||
| const scene_t *build_test_scene(void) { return &scene; } | ||||
		Reference in New Issue
	
	Block a user