|
|
|
@ -14,12 +14,15 @@
@@ -14,12 +14,15 @@
|
|
|
|
|
#include <SDL2/SDL_timer.h> |
|
|
|
|
#include <SDL2/SDL_video.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <sys/types.h> |
|
|
|
|
|
|
|
|
|
int keyboardstate[322] = {}; // 322 is the number of SDLK_DOWN events
|
|
|
|
|
int exitnow = 0; |
|
|
|
|
static int exitnow = 0; |
|
|
|
|
SDL_Renderer * ren;
|
|
|
|
|
Uint32 pixels[B_INTERNAL_HEIGHT][B_INTERNAL_WIDTH]; |
|
|
|
|
|
|
|
|
|
static bool shitty_semaphore[B_NUM_RAYMARCH_THREADS] = {}; |
|
|
|
|
|
|
|
|
|
struct object *scene_object; |
|
|
|
|
struct camera *camera; |
|
|
|
|
|
|
|
|
@ -53,7 +56,7 @@ void handle_inputs(void)
@@ -53,7 +56,7 @@ void handle_inputs(void)
|
|
|
|
|
|
|
|
|
|
const double dist = 0.1; |
|
|
|
|
|
|
|
|
|
struct vec ** in = malloc(sizeof(struct vec *) * 3); |
|
|
|
|
struct vec ** in = (struct vec **)malloc(sizeof(struct vec *) * 3); |
|
|
|
|
in[0] = camera->x; |
|
|
|
|
in[1] = camera->y; |
|
|
|
|
in[2] = camera->z; |
|
|
|
@ -82,6 +85,11 @@ void handle_inputs(void)
@@ -82,6 +85,11 @@ void handle_inputs(void)
|
|
|
|
|
exitnow = 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) { |
|
|
|
|
scene_object->sol.pos.elements[i] = camera->pos->elements[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
free(in); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -117,16 +125,25 @@ static int raymarch_threadfn(void *data) {
@@ -117,16 +125,25 @@ static int raymarch_threadfn(void *data) {
|
|
|
|
|
|
|
|
|
|
/* draw stuff */ |
|
|
|
|
/* march the rays */ |
|
|
|
|
for (int i = 0; i < B_INTERNAL_WIDTH; i++) { |
|
|
|
|
for (int j = 0; j < B_INTERNAL_HEIGHT; j++) { |
|
|
|
|
/* checkerboard rendering */ |
|
|
|
|
if ((i + j * B_INTERNAL_WIDTH) % B_NUM_RAYMARCH_THREADS != id) { |
|
|
|
|
continue; |
|
|
|
|
do { |
|
|
|
|
|
|
|
|
|
while (shitty_semaphore[id]) { |
|
|
|
|
; // wait
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < B_INTERNAL_WIDTH; i++) { |
|
|
|
|
for (int j = 0; j < B_INTERNAL_HEIGHT; j++) { |
|
|
|
|
/* checkerboard rendering */ |
|
|
|
|
if ((i + j * B_INTERNAL_WIDTH) % B_NUM_RAYMARCH_THREADS != id) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
//sdlb_draw_col_pixel(p.col, j, i);
|
|
|
|
|
pixels[j][i] = process_pixel(i, j); |
|
|
|
|
} |
|
|
|
|
//sdlb_draw_col_pixel(p.col, j, i);
|
|
|
|
|
pixels[j][i] = process_pixel(i, j); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
shitty_semaphore[id] = true; |
|
|
|
|
} while (!exitnow); |
|
|
|
|
|
|
|
|
|
free(data); |
|
|
|
|
return 0; |
|
|
|
@ -137,7 +154,7 @@ void setup_camera_scene()
@@ -137,7 +154,7 @@ void setup_camera_scene()
|
|
|
|
|
{ |
|
|
|
|
//struct object white_sphere = new_object(new_vec3(0.75, 0, 8), 0, 1, sdf_box, yeet_whit);
|
|
|
|
|
|
|
|
|
|
camera = calloc(1, sizeof(struct camera)); |
|
|
|
|
camera = (struct camera *)calloc(1, sizeof(struct camera)); |
|
|
|
|
camera->pos = new_vec(4); |
|
|
|
|
camera->x = new_vec(4); |
|
|
|
|
camera->y = new_vec(4); |
|
|
|
@ -158,7 +175,7 @@ void setup_camera_scene()
@@ -158,7 +175,7 @@ void setup_camera_scene()
|
|
|
|
|
const int num_trees = 4; |
|
|
|
|
int lastelem = 1; |
|
|
|
|
|
|
|
|
|
struct object* scene_objects = malloc((lastelem + 2 * num_trees) * sizeof(struct object)); |
|
|
|
|
struct object* scene_objects = (struct object *)malloc((lastelem + 2 * num_trees) * sizeof(struct object)); |
|
|
|
|
|
|
|
|
|
struct vec *plane_pos = new_vec4(0,2,-5,0); |
|
|
|
|
scene_objects[0] = new_plane(plane_pos, 1, 1); |
|
|
|
@ -174,7 +191,13 @@ void setup_camera_scene()
@@ -174,7 +191,13 @@ void setup_camera_scene()
|
|
|
|
|
|
|
|
|
|
scene_object = new_scene(2 * num_trees + lastelem, scene_objects); |
|
|
|
|
scene_object->sol.pos.dimension = 3; |
|
|
|
|
scene_object->sol.pos.elements = camera->pos->elements; |
|
|
|
|
// oh no does this actually have to point to the same thing ;-;
|
|
|
|
|
// scene_object->sol.pos.elements = camera->pos->elements;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) { |
|
|
|
|
scene_object->sol.pos.elements[i] = camera->pos->elements[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) { |
|
|
|
@ -195,23 +218,23 @@ int main(int argc, char **argv) {
@@ -195,23 +218,23 @@ int main(int argc, char **argv) {
|
|
|
|
|
SDL_Texture *texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
|
|
|
|
|
SDL_TEXTUREACCESS_STATIC, B_INTERNAL_WIDTH, B_INTERNAL_HEIGHT); |
|
|
|
|
|
|
|
|
|
SDL_Thread ** threads = calloc(B_NUM_RAYMARCH_THREADS, sizeof(SDL_Thread *)); |
|
|
|
|
SDL_Thread ** threads = (SDL_Thread **)calloc(B_NUM_RAYMARCH_THREADS, sizeof(SDL_Thread *)); |
|
|
|
|
|
|
|
|
|
SDL_Thread *input_thread = SDL_CreateThread(input_loop, "input", (void *)NULL); |
|
|
|
|
setup_camera_scene(); |
|
|
|
|
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) { |
|
|
|
|
int *tid = (int *)malloc(sizeof(int)); |
|
|
|
|
*tid = i; |
|
|
|
|
threads[i] = SDL_CreateThread(raymarch_threadfn, "raymarch", tid); |
|
|
|
|
// tid is freed in raymarch_threadfn
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (!exitnow) { |
|
|
|
|
/* clear the view */ |
|
|
|
|
start = SDL_GetPerformanceCounter(); |
|
|
|
|
SDL_LockMutex(frame_mutex); |
|
|
|
|
SDL_RenderClear(ren); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) { |
|
|
|
|
int *tid = malloc(sizeof(int)); |
|
|
|
|
*tid = i; |
|
|
|
|
threads[i] = SDL_CreateThread(raymarch_threadfn, "raymarch", tid); |
|
|
|
|
// tid is freed in raymarch_threadfn
|
|
|
|
|
} |
|
|
|
|
/*
|
|
|
|
|
SDL_LockMutex(frame_mutex); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) { |
|
|
|
|
int status; |
|
|
|
@ -219,12 +242,33 @@ int main(int argc, char **argv) {
@@ -219,12 +242,33 @@ int main(int argc, char **argv) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SDL_UnlockMutex(frame_mutex); |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
bool done = false; |
|
|
|
|
while (!done) { |
|
|
|
|
done = true; |
|
|
|
|
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) { |
|
|
|
|
if (!shitty_semaphore[i]) { |
|
|
|
|
done = false; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SDL_UpdateTexture(texture, NULL, pixels, B_INTERNAL_WIDTH * sizeof(Uint32)); |
|
|
|
|
|
|
|
|
|
SDL_RenderCopy(ren, texture, NULL, NULL); |
|
|
|
|
|
|
|
|
|
SDL_RenderPresent(ren); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < B_NUM_RAYMARCH_THREADS; i++) { |
|
|
|
|
shitty_semaphore[i] = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end = SDL_GetPerformanceCounter(); |
|
|
|
|
double el = (1000 * (end - start) / SDL_GetPerformanceFrequency()); |
|
|
|
|
if (el > 0) { |
|
|
|
|