From a79b07420125bcf5eb66d0859276c6241da54c13 Mon Sep 17 00:00:00 2001 From: Seth Hamilton Date: Fri, 22 Aug 2025 11:13:16 -0500 Subject: [PATCH] axis spheres still aren't showing, but some obvious bugs were removed. still debugging --- inc/body.hpp | 3 +++ inc/util.hpp | 5 +---- src/body.cpp | 7 +++++++ src/camera_poses.cpp | 28 +++++++++++++++------------- src/main.cpp | 23 +++++++++-------------- src/util.cpp | 7 ++++--- 6 files changed, 39 insertions(+), 34 deletions(-) diff --git a/inc/body.hpp b/inc/body.hpp index 6b3f0aa..1c17195 100644 --- a/inc/body.hpp +++ b/inc/body.hpp @@ -4,6 +4,8 @@ #include #include "util.hpp" +static uint shader; + struct Body { glm::mat4 pose; float scale; @@ -18,3 +20,4 @@ struct Body { bool load_body(Body* out_body, const char* obj_filepath); void draw_body(const Body& b); +void create_new_sphere(Body* b, float scale=1.0f); \ No newline at end of file diff --git a/inc/util.hpp b/inc/util.hpp index 26b5383..6c0329c 100644 --- a/inc/util.hpp +++ b/inc/util.hpp @@ -6,9 +6,6 @@ #include #include -#define min(a, b) ((a < b) ? a : b) -#define max(a, b) ((a > b) ? a : b) - typedef unsigned int uint; template struct Array { @@ -26,4 +23,4 @@ bool read_file(Array* out, const char* filepath); Array split_str(const char* s, char delimiter); Array split_str(const char* s); -glm::mat4 quat_to_mat4(glm::quat q); +glm::mat4 quat_to_mat4(glm::quat q); \ No newline at end of file diff --git a/src/body.cpp b/src/body.cpp index 2578957..9c1e0ba 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -122,3 +122,10 @@ void draw_body(const Body& b) { glDrawElements(GL_TRIANGLES, b.faces.len, GL_UNSIGNED_INT, 0); //glDisableVertexAttribArray(0); } + +void create_new_sphere(Body* b, float scale) { + assert(load_body(b, "Icosphere.obj")); + b->shader = shader; + b->scale = scale; + b->pose = glm::translate(b->pose, glm::vec3(0, 0, 0)); +} \ No newline at end of file diff --git a/src/camera_poses.cpp b/src/camera_poses.cpp index c8953d4..92f092d 100644 --- a/src/camera_poses.cpp +++ b/src/camera_poses.cpp @@ -10,8 +10,7 @@ #include "camera_poses.hpp" - -#define LINE_BUF_SIZE 128 +#define LINE_BUF_SIZE 256 #define NUM_SPHERES_PER_AXE 25 #define SPACE_PER_SPHERE 10.0f @@ -21,8 +20,8 @@ glm::vec4 blue_color = glm::vec4(0, 0, 1, 1); bool parse_poses(Array* bodies_out, const char* filepath) { FILE* fp; - if (!fopen_s(&fp, filepath, "rb")) { - printf("Error parsing poses csv: %s\n", filepath); + if (fopen_s(&fp, filepath, "rb")) { + printf("Error parsing %s\n", filepath); return false; } @@ -32,13 +31,14 @@ bool parse_poses(Array* bodies_out, const char* filepath) { // read in header fgets(line, LINE_BUF_SIZE, fp); - while (!feof(fp)) { - fgets(line, LINE_BUF_SIZE, fp); + fgets(line, LINE_BUF_SIZE, fp); + + for (int camera_i = 0; !feof(fp); camera_i++) { Array words = split_str(line, delim); - float x = atof(words[0]); - float y = atof(words[1]); - float z = atof(words[2]); + float x = atof(words[0])/100.0f; + float y = atof(words[1])/100.0f; + float z = atof(words[2])/100.0f; float w = atof(words[3]); float i = atof(words[4]); float j = atof(words[5]); @@ -54,9 +54,7 @@ bool parse_poses(Array* bodies_out, const char* filepath) { for (int i = 0; i < 3; i++) { for (int j = 0; j < NUM_SPHERES_PER_AXE; j++) { Body b; - if (!load_body(&b, "Icosphere.obj")) { - return false; - } + create_new_sphere(&b); // How far along the axis is this ball glm::vec3 trans = glm::vec3(0, 0, 0); @@ -64,8 +62,12 @@ bool parse_poses(Array* bodies_out, const char* filepath) { // Now move the translated pose via the camera's pose b.pose = pose * glm::translate(b.pose, trans); - bodies_out->data[i*j + j] = b; + b.color = glm::vec4(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1); + b.scale = 0.2; + printf("%d, ", camera_i * 3 * NUM_SPHERES_PER_AXE + i * NUM_SPHERES_PER_AXE + j); + bodies_out->data[camera_i*3*NUM_SPHERES_PER_AXE + i*NUM_SPHERES_PER_AXE + j] = b; } } + fgets(line, LINE_BUF_SIZE, fp); } } diff --git a/src/main.cpp b/src/main.cpp index a33c4ea..76aabee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,14 +31,6 @@ void process_input() { glfwSetWindowShouldClose(window, true); } -static uint shader; -void create_new_sphere(Body* b, float scale = 1) { - assert(load_body(b, "Icosphere.obj")); - b->shader = shader; - b->scale = scale; - b->pose = glm::translate(b->pose, glm::vec3(0, 0, 0)); -} - static bool stop = false; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; const float max_hp @@ -189,9 +181,10 @@ int main() { glfwSwapBuffers(window); // front buffer is now back glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf) - /*Body b2; + Body b2; create_new_sphere(&b2); - b2.color = glm::vec4(0, 0.5, 0, 1);*/ + b2.pose = glm::translate(b2.pose, glm::vec3(0, 0, 20)); + b2.color = glm::vec4(1, 0, 0, 1); // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // set_uniform(shader, "color", glm::vec4 { sin(time), sin(time + glm::radians(45.0f)), sin(time + @@ -201,12 +194,12 @@ int main() { glEnable(GL_DEPTH_TEST); glm::mat4 projection_t - = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1300.0f); + = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 2000.0f); pthread_t thread_id; pthread_create(&thread_id, NULL, process_cin, NULL); - Array camera_pose_axes; + Array camera_pose_axes = { NULL, 0 }; if (!parse_poses(&camera_pose_axes, "poses.csv")) { return -1; } @@ -217,16 +210,18 @@ int main() { set_uniform(shader, "camera_t", world_to_camera); set_uniform(shader, "projection_t", projection_t); + draw_body(b2); + for (int i = 0; i < camera_pose_axes.len; i++) { draw_body(camera_pose_axes[i]); } - if (pthread_mutex_trylock(&lock)) { + if (pthread_mutex_trylock(&lock) == 0) { for (int i = 0; i < camera_bodies.size(); i++) { if (!std::get<1>(camera_bodies[i])) { Body* b = (Body*)malloc(sizeof(Body)); create_new_sphere(b); - b->color = glm::vec4(i & 0x4, i & 0x2, i & 0x1, max_hp); + b->color = glm::vec4((i+1) & 0x4, (i+1) & 0x2, (i+1) & 0x1, max_hp); std::get<1>(camera_bodies[i]) = b; } draw_body(*std::get<1>(camera_bodies[i])); diff --git a/src/util.cpp b/src/util.cpp index 8c9ab44..7882d8b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -7,12 +7,13 @@ #include #include "util.hpp" +#include "body.hpp" Array _split_str_inner(const char* s, char delim, bool just_check_ws); template void append(Array& a, T el) { if (a.len == a.cap) { - resize(a, max(8, a.cap * 2)); + resize(a, std::max((size_t)8, a.cap * 2)); } a[a.len] = el; a.len++; @@ -27,11 +28,11 @@ template T pop(Array& a) { template void resize(Array& a, size_t new_cap) { T* new_data = (T*)malloc(sizeof(T) * new_cap); if (a.len > 0) { - memcpy(new_data, a.data, min(a.len, new_cap)); + memcpy(new_data, a.data, std::min(a.len, new_cap)); } free(a.data); a.data = new_data; - a.len = min(a.len, new_cap); + a.len = std::min(a.len, new_cap); a.cap = new_cap; }