Fixed the bug where different source files were referencing a static variable. Turns out static vars are internally linked, which means there was a separate static variable defined for each source. I could have fixed this issue by using extern (see https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c), but instead went with just assigning the shader manually im main. This is becoming very messy and needs a cleanup.

This commit is contained in:
2025-08-22 15:09:34 -05:00
parent a79b074201
commit c961ccf7cd
4 changed files with 29 additions and 27 deletions

View File

@@ -4,8 +4,6 @@
#include <glm/glm.hpp>
#include "util.hpp"
static uint shader;
struct Body {
glm::mat4 pose;
float scale;
@@ -20,4 +18,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);
void create_new_sphere(Body* b, float scale=1.0f);

View File

@@ -116,7 +116,7 @@ bool load_body(Body* out_body, const char* obj_filepath) {
void draw_body(const Body& b) {
use_shader(b.shader);
set_uniform(b.shader, "global_t", b.scale*b.pose);
set_uniform(b.shader, "global_t", glm::scale(b.pose, glm::vec3(b.scale)));
set_uniform(b.shader, "color", b.color);
glBindVertexArray(b.vao);
glDrawElements(GL_TRIANGLES, b.faces.len, GL_UNSIGNED_INT, 0);
@@ -125,7 +125,6 @@ void draw_body(const Body& b) {
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

@@ -27,6 +27,9 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
char delim = ',';
char line[LINE_BUF_SIZE];
// because clang refuses to cooperate with my append implementation and generate the function code...
*bodies_out
= { (Body*)malloc(sizeof(Body) * 3 * NUM_SPHERES_PER_AXE * 14), 3 * NUM_SPHERES_PER_AXE * 14 };
// read in header
fgets(line, LINE_BUF_SIZE, fp);
@@ -36,9 +39,9 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
for (int camera_i = 0; !feof(fp); camera_i++) {
Array<char*> words = split_str(line, delim);
float x = atof(words[0])/100.0f;
float y = atof(words[1])/100.0f;
float z = atof(words[2])/100.0f;
float x = atof(words[0]);
float y = atof(words[1]);
float z = atof(words[2]);
float w = atof(words[3]);
float i = atof(words[4]);
float j = atof(words[5]);
@@ -46,10 +49,6 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
glm::mat4 pose = glm::translate(quat_to_mat4(glm::quat(w, i, j, k)), glm::vec3(x, y, z));
// because clang refuses to cooperate with my append implementation and generate the function code...
*bodies_out
= { (Body*)malloc(sizeof(Body) * 3 * NUM_SPHERES_PER_AXE * 14), 3 * NUM_SPHERES_PER_AXE * 14 };
// Generate axis spheres
for (int i = 0; i < 3; i++) {
for (int j = 0; j < NUM_SPHERES_PER_AXE; j++) {
@@ -63,11 +62,12 @@ bool parse_poses(Array<Body>* bodies_out, const char* filepath) {
// Now move the translated pose via the camera's pose
b.pose = pose * glm::translate(b.pose, trans);
b.color = glm::vec4(i == 0 ? 1 : 0, i == 1 ? 1 : 0, i == 2 ? 1 : 0, 1);
b.scale = 0.2;
b.scale = 3;
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);
}
return true;
}

View File

@@ -39,6 +39,7 @@ static std::vector<std::tuple<char*, Body*, float>>
camera_bodies; // I would use my array here, but was getting a linking error
void* process_cin(void* args) {
return NULL;
std::string line;
while (true) {
std::getline(std::cin, line);
@@ -99,17 +100,18 @@ static void cursor_position_callback(GLFWwindow* window, double xpos, double ypo
prev_cursor_x = xpos;
prev_cursor_y = ypos;
double len = glm::length(camera_loc - focal_point);
if (mouse_pressed) {
phi += glm::radians(dx * (360 / width)); // * glm::radians(360.0);
theta += glm::radians(dy * (360 / height)); // * glm::radians(360.0);
double len = glm::length(camera_loc - focal_point);
camera_loc.x = focal_point.x + (len * glm::cos(theta) * glm::cos(-phi));
camera_loc.y = focal_point.y + (len * glm::sin(theta));
camera_loc.z = focal_point.z + (-len * glm::cos(theta) * glm::sin(-phi));
}
if (scroll_pressed) {
glm::vec4 move_vec = -strafe_x * (dx / 100) + strafe_y * (dy / 100);
glm::vec4 move_vec = (float)len * (- strafe_x * (dx / 500) + strafe_y * (dy / 500));
focal_point += move_vec;
camera_loc += move_vec;
}
@@ -141,7 +143,7 @@ bool glfw_setup() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
window = glfwCreateWindow(800, 800, "LivePlotter", NULL, NULL);
if (window == NULL) {
printf("Failed to create GLFW window\n");
glfwTerminate();
@@ -171,21 +173,17 @@ int main() {
const char* vertex_filepath = "src/shaders/vertex.glsl";
const char* fragment_filepath = "src/shaders/fragment.glsl";
uint shader;
if (!load_shader(&shader, vertex_filepath, fragment_filepath))
return -1;
// glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer
glfwSwapBuffers(window); // front buffer is now back
glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf)
Body b2;
create_new_sphere(&b2);
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 +
// glm::radians(90.0f)), 1.0 } / 2.0f); time = glfwGetTime();
@@ -193,9 +191,7 @@ int main() {
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glm::mat4 projection_t
= glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 2000.0f);
glm::mat4 projection_t = glm::infinitePerspective(glm::radians(45.0f), (float)width / (float)height, 0.1f);
pthread_t thread_id;
pthread_create(&thread_id, NULL, process_cin, NULL);
@@ -203,6 +199,15 @@ int main() {
if (!parse_poses(&camera_pose_axes, "poses.csv")) {
return -1;
}
for (int i = 0; i < camera_pose_axes.len; i++) {
camera_pose_axes[i].shader = shader;
}
Body b;
create_new_sphere(&b);
b.pose = glm::mat4(1);
b.color = glm::vec4(1, 1, 1, 1);
b.shader = shader;
while (!glfwWindowShouldClose(window)) {
process_input();
@@ -210,7 +215,7 @@ int main() {
set_uniform(shader, "camera_t", world_to_camera);
set_uniform(shader, "projection_t", projection_t);
draw_body(b2);
draw_body(b);
for (int i = 0; i < camera_pose_axes.len; i++) {
draw_body(camera_pose_axes[i]);