cg-from-scratch/src/raytracer/main.c

72 lines
1.7 KiB
C

#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>
#include <SDL2/SDL_keycode.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#define RECURSION_DEPTH 3
typedef struct {
vec3f_t position;
vec3f_t rotation;
} camera_t;
i32 main(i32 argc, char *argv[]) {
colour_t bg =
(colour_t){.rgba.r = 27, .rgba.g = 38, .rgba.b = 79, .rgba.a = 255};
camera_t camera = {
.position = {.x = 0.0f, .y = 0.0f, .z = 0.0f},
.rotation = {.x = 0.0f, .y = 0.0f, .z = 0.0f},
};
vec3f_t viewport = {.x = 1.0f, .y = 1.0f, .z = 1.0f};
window_t window = {0};
if (!init_window(&window, 800, 800, "CG From Scratch Raytracer")) {
return EXIT_FAILURE;
}
const scene_t *scene = build_test_scene();
bool running = true;
SDL_Event event = {0};
i32 w_min = ((i32)window.half_width) * -1;
i32 w_max = (i32)window.half_width;
i32 h_min = ((i32)window.half_height) * -1;
i32 h_max = (i32)window.half_height;
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
}
}
clear_window(&window, bg);
for (i32 y = h_min; y < h_max; ++y) {
for (i32 x = w_min; x < w_max; ++x) {
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);
set_pixel(&window, x, y, colour);
}
}
swap_buffers(&window);
}
close_window(&window);
return EXIT_SUCCESS;
}