From 0a6f14900b243838f22eab86e2419b1b37ed0405 Mon Sep 17 00:00:00 2001 From: Francesco Cozzuto Date: Thu, 10 Oct 2024 23:58:59 +0200 Subject: [PATCH] Better camera movement --- src/gpu_and_windowing.c | 44 ++++++---------------------- src/main.c | 65 +++++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 54 deletions(-) diff --git a/src/gpu_and_windowing.c b/src/gpu_and_windowing.c index 61e221f..5e7c91d 100644 --- a/src/gpu_and_windowing.c +++ b/src/gpu_and_windowing.c @@ -254,39 +254,11 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, { switch (key) { case GLFW_KEY_SPACE: - if (0) {} - else if (action == GLFW_PRESS) push_event(EVENT_PRESS_SPACE); - else if (action == GLFW_REPEAT) push_event(EVENT_AGAIN_SPACE); + if (action == GLFW_PRESS) push_event(EVENT_PRESS_SPACE); break; case GLFW_KEY_ESCAPE: - if (0) {} - else if (action == GLFW_PRESS) push_event(EVENT_PRESS_ESC); - else if (action == GLFW_REPEAT) push_event(EVENT_AGAIN_ESC); - break; - - case GLFW_KEY_W: - if (0) {} - else if (action == GLFW_PRESS) push_event(EVENT_PRESS_W); - else if (action == GLFW_REPEAT) push_event(EVENT_AGAIN_W); - break; - - case GLFW_KEY_A: - if (0) {} - else if (action == GLFW_PRESS) push_event(EVENT_PRESS_W); - else if (action == GLFW_REPEAT) push_event(EVENT_AGAIN_W); - break; - - case GLFW_KEY_S: - if (0) {} - else if (action == GLFW_PRESS) push_event(EVENT_PRESS_W); - else if (action == GLFW_REPEAT) push_event(EVENT_AGAIN_W); - break; - - case GLFW_KEY_D: - if (0) {} - else if (action == GLFW_PRESS) push_event(EVENT_PRESS_W); - else if (action == GLFW_REPEAT) push_event(EVENT_AGAIN_W); + if (action == GLFW_PRESS) push_event(EVENT_PRESS_ESC); break; } } @@ -405,11 +377,8 @@ void move_frame_to_the_gpu(int w, int h, Vector3 *data) void draw_frame(void) { - Vector3 clear_color = {1, 1, 1}; - //glViewport(0, 0, screen_w, screen_h); - glClearColor(clear_color.x, clear_color.y, clear_color.z, 1.0f); - glClearStencil(0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glClearColor(1, 1, 1, 1); + glClear(GL_COLOR_BUFFER_BIT); glUseProgram(screen_program); glActiveTexture(GL_TEXTURE0); @@ -420,4 +389,9 @@ void draw_frame(void) glfwSwapBuffers(window); glfwPollEvents(); + + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { push_event(EVENT_PRESS_W); } + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { push_event(EVENT_PRESS_A); } + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { push_event(EVENT_PRESS_S); } + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { push_event(EVENT_PRESS_D); } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 22ef0b4..2c2c1e1 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#include #include #include #include @@ -434,11 +435,19 @@ void update_frame(void) int main(int argc, char **argv) { + fprintf(stderr, "Started\n"); + char *scene_file; parse_arguments_or_exit(argc, argv, &num_columns, &init_scale, &scene_file); - if (!parse_scene_file(scene_file, &scene)) + fprintf(stderr, "Parsed arguments\n"); + + if (!parse_scene_file(scene_file, &scene)) { + fprintf(stderr, "Couldn't parse scene\n"); return -1; + } + + fprintf(stderr, "Scene parsed\n"); const char *faces[] = { [CF_RIGHT] = "assets/skybox/right.jpg", @@ -450,60 +459,75 @@ int main(int argc, char **argv) }; load_cubemap(&skybox, faces); + fprintf(stderr, "Cubemap loaded\n"); + startup_window_and_opengl_context_or_exit(2 * 640, 2 * 480, "Ray Tracing"); + fprintf(stderr, "Started windows and opengl context\n"); + start_workers(); - for (;;) { + fprintf(stderr, "Workers started\n"); + + for (bool exit = false; !exit; ) { - bool exit = false; for (;;) { double mouse_x; double mouse_y; int event = pop_event(&mouse_x, &mouse_y); + if (event == EVENT_EMPTY) break; float speed = 0.5; - if (event == EVENT_CLOSE || event == EVENT_PRESS_ESC) { - + switch (event) { + case EVENT_CLOSE: + case EVENT_PRESS_ESC: + fprintf(stderr, "Exiting\n"); exit = true; break; - } else if (event == EVENT_PRESS_W || event == EVENT_AGAIN_W) { - + case EVENT_PRESS_W: + case EVENT_AGAIN_W: move_camera(UP, speed); invalidate_accumulation(); + break; - } else if (event == EVENT_PRESS_A || event == EVENT_AGAIN_A) { - + case EVENT_PRESS_A: + case EVENT_AGAIN_A: move_camera(LEFT, speed); invalidate_accumulation(); + break; - } else if (event == EVENT_PRESS_S || event == EVENT_AGAIN_S) { - + case EVENT_PRESS_S: + case EVENT_AGAIN_S: move_camera(DOWN, speed); invalidate_accumulation(); + break; - } else if (event == EVENT_PRESS_D || event == EVENT_AGAIN_D) { - + case EVENT_PRESS_D: + case EVENT_AGAIN_D: move_camera(RIGHT, speed); invalidate_accumulation(); + break; - } else if (event == EVENT_MOVE_MOUSE) { - + case EVENT_MOVE_MOUSE: rotate_camera(mouse_x, mouse_y); invalidate_accumulation(); + break; - } else if (event == EVENT_PRESS_SPACE) { + case EVENT_PRESS_SPACE: screenshot(); + break; } } - if (exit) break; update_frame(); draw_frame(); } + // Tell workers to stop evaluating frames + invalidate_accumulation(); + stop_workers(); free_cubemap(&skybox); cleanup_window_and_opengl_context(); @@ -561,9 +585,12 @@ void parse_arguments_or_exit(int argc, char **argv, int *num_columns, int *init_ *num_columns = MAX_COLUMNS; } -// Must be executed on the main thread +// Must be executed while holding the frame lock void screenshot(void) { + // Choose a file name in the form "screenshot_X.png" where X + // is an integer between 0 and 1000 that is not being used + // already. char file[1<<12]; int i = 0; while (i < 1000) { @@ -583,11 +610,11 @@ void screenshot(void) i++; } + // Convert the frame buffer from one float per pixel to one byte. uint8_t *converted = malloc(frame_w * frame_h * 3 * sizeof(uint8_t)); if (converted == NULL) { fprintf(stderr, "Couldn't take screenshot (out of memory)\n"); } - for (int i = 0; i < frame_w * frame_h; i++) { converted[i * 3 + 0] = frame[i].x * 255; converted[i * 3 + 1] = frame[i].y * 255;