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

@@ -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]);