axis spheres still aren't showing, but some obvious bugs were removed. still debugging

This commit is contained in:
2025-08-22 11:13:16 -05:00
parent 19f786ee7c
commit a79b074201
6 changed files with 39 additions and 34 deletions

View File

@@ -4,6 +4,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "util.hpp" #include "util.hpp"
static uint shader;
struct Body { struct Body {
glm::mat4 pose; glm::mat4 pose;
float scale; float scale;
@@ -18,3 +20,4 @@ struct Body {
bool load_body(Body* out_body, const char* obj_filepath); bool load_body(Body* out_body, const char* obj_filepath);
void draw_body(const Body& b); void draw_body(const Body& b);
void create_new_sphere(Body* b, float scale=1.0f);

View File

@@ -6,9 +6,6 @@
#include <glm/ext/vector_float4.hpp> #include <glm/ext/vector_float4.hpp>
#include <stdio.h> #include <stdio.h>
#define min(a, b) ((a < b) ? a : b)
#define max(a, b) ((a > b) ? a : b)
typedef unsigned int uint; typedef unsigned int uint;
template<class T> struct Array { template<class T> struct Array {

View File

@@ -122,3 +122,10 @@ void draw_body(const Body& b) {
glDrawElements(GL_TRIANGLES, b.faces.len, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, b.faces.len, GL_UNSIGNED_INT, 0);
//glDisableVertexAttribArray(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));
}

View File

@@ -10,8 +10,7 @@
#include "camera_poses.hpp" #include "camera_poses.hpp"
#define LINE_BUF_SIZE 256
#define LINE_BUF_SIZE 128
#define NUM_SPHERES_PER_AXE 25 #define NUM_SPHERES_PER_AXE 25
#define SPACE_PER_SPHERE 10.0f #define SPACE_PER_SPHERE 10.0f
@@ -21,8 +20,8 @@ glm::vec4 blue_color = glm::vec4(0, 0, 1, 1);
bool parse_poses(Array<Body>* bodies_out, const char* filepath) { bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
FILE* fp; FILE* fp;
if (!fopen_s(&fp, filepath, "rb")) { if (fopen_s(&fp, filepath, "rb")) {
printf("Error parsing poses csv: %s\n", filepath); printf("Error parsing %s\n", filepath);
return false; return false;
} }
@@ -32,13 +31,14 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
// read in header // read in header
fgets(line, LINE_BUF_SIZE, fp); 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<char*> words = split_str(line, delim); Array<char*> words = split_str(line, delim);
float x = atof(words[0]); float x = atof(words[0])/100.0f;
float y = atof(words[1]); float y = atof(words[1])/100.0f;
float z = atof(words[2]); float z = atof(words[2])/100.0f;
float w = atof(words[3]); float w = atof(words[3]);
float i = atof(words[4]); float i = atof(words[4]);
float j = atof(words[5]); float j = atof(words[5]);
@@ -54,9 +54,7 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
for (int j = 0; j < NUM_SPHERES_PER_AXE; j++) { for (int j = 0; j < NUM_SPHERES_PER_AXE; j++) {
Body b; Body b;
if (!load_body(&b, "Icosphere.obj")) { create_new_sphere(&b);
return false;
}
// How far along the axis is this ball // How far along the axis is this ball
glm::vec3 trans = glm::vec3(0, 0, 0); glm::vec3 trans = glm::vec3(0, 0, 0);
@@ -64,8 +62,12 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
// Now move the translated pose via the camera's pose // Now move the translated pose via the camera's pose
b.pose = pose * glm::translate(b.pose, trans); 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);
} }
} }

View File

@@ -31,14 +31,6 @@ void process_input() {
glfwSetWindowShouldClose(window, true); 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 bool stop = false;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
const float max_hp const float max_hp
@@ -189,9 +181,10 @@ int main() {
glfwSwapBuffers(window); // front buffer is now back glfwSwapBuffers(window); // front buffer is now back
glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf) glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf)
/*Body b2; Body b2;
create_new_sphere(&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); // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// set_uniform(shader, "color", glm::vec4 { sin(time), sin(time + glm::radians(45.0f)), sin(time + // 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); glEnable(GL_DEPTH_TEST);
glm::mat4 projection_t 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_t thread_id;
pthread_create(&thread_id, NULL, process_cin, NULL); pthread_create(&thread_id, NULL, process_cin, NULL);
Array<Body> camera_pose_axes; Array<Body> camera_pose_axes = { NULL, 0 };
if (!parse_poses(&camera_pose_axes, "poses.csv")) { if (!parse_poses(&camera_pose_axes, "poses.csv")) {
return -1; return -1;
} }
@@ -217,16 +210,18 @@ int main() {
set_uniform(shader, "camera_t", world_to_camera); set_uniform(shader, "camera_t", world_to_camera);
set_uniform(shader, "projection_t", projection_t); set_uniform(shader, "projection_t", projection_t);
draw_body(b2);
for (int i = 0; i < camera_pose_axes.len; i++) { for (int i = 0; i < camera_pose_axes.len; i++) {
draw_body(camera_pose_axes[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++) { for (int i = 0; i < camera_bodies.size(); i++) {
if (!std::get<1>(camera_bodies[i])) { if (!std::get<1>(camera_bodies[i])) {
Body* b = (Body*)malloc(sizeof(Body)); Body* b = (Body*)malloc(sizeof(Body));
create_new_sphere(b); 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; std::get<1>(camera_bodies[i]) = b;
} }
draw_body(*std::get<1>(camera_bodies[i])); draw_body(*std::get<1>(camera_bodies[i]));

View File

@@ -7,12 +7,13 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "util.hpp" #include "util.hpp"
#include "body.hpp"
Array<char*> _split_str_inner(const char* s, char delim, bool just_check_ws); Array<char*> _split_str_inner(const char* s, char delim, bool just_check_ws);
template<class T> void append(Array<T>& a, T el) { template<class T> void append(Array<T>& a, T el) {
if (a.len == a.cap) { 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[a.len] = el;
a.len++; a.len++;
@@ -27,11 +28,11 @@ template<class T> T pop(Array<T>& a) {
template<class T> void resize(Array<T>& a, size_t new_cap) { template<class T> void resize(Array<T>& a, size_t new_cap) {
T* new_data = (T*)malloc(sizeof(T) * new_cap); T* new_data = (T*)malloc(sizeof(T) * new_cap);
if (a.len > 0) { 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); free(a.data);
a.data = new_data; a.data = new_data;
a.len = min(a.len, new_cap); a.len = std::min(a.len, new_cap);
a.cap = new_cap; a.cap = new_cap;
} }