From e5a8f88556f2c551464b4b50bdbb1558ff23eeb4 Mon Sep 17 00:00:00 2001 From: Seth Hamilton Date: Mon, 25 Aug 2025 03:43:30 -0500 Subject: [PATCH] Enable alpha blending. main reads from stdin --- src/live_plotter.cpp | 8 ++++- src/main.cpp | 62 +++++++++++++++------------------------ src/shaders/fragment.glsl | 2 +- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/live_plotter.cpp b/src/live_plotter.cpp index e7785a3..9511973 100644 --- a/src/live_plotter.cpp +++ b/src/live_plotter.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "util.hpp" #include "shaders.hpp" @@ -143,6 +144,7 @@ DllExport void __cdecl update_point(pointid id, float x, float y, float z) { Point* p = id_to_point[id]; p->targetloc = { x, y, z }; p->startloc = p->b.pose[3]; + p->start_s = _time(); LeaveCriticalSection(&cs); } @@ -218,6 +220,8 @@ DWORD _win_thread(LPVOID args) { glClear(GL_COLOR_BUFFER_BIT); // Write to back buffer again (former front buf) glDisable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); while (!(glfwWindowShouldClose(win) || stop_flag)) _refresh_win(); @@ -264,10 +268,12 @@ void _refresh_win() { // Perform fade double elapsed_time_s = _time() - p->start_s; float t = elapsed_time_s / p->lifetime_s; - p->b.color[3] = lerp(1.0, 0.0, t); + t = std::clamp(t, 0.0f, 1.0f); + p->b.color.w = lerp(1.0, 0.0, t); // Lerp position t = elapsed_time_s / LERP_MOVE_PERIOD_S; + t = std::clamp(t, 0.0f, 1.0f); p->b.pose[3] = glm::vec4(lerp(p->startloc, p->targetloc, t), 1); draw_body(p->b); } diff --git a/src/main.cpp b/src/main.cpp index 3229801..50493b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,43 +1,11 @@ +#include +#include +#include #include "camera_poses.hpp" #include "live_plotter.hpp" -//void* process_cin(void* args) { -// std::string line; -// while (true) { -// std::getline(std::cin, line); -// Array words = split_str(line.c_str()); -// if (!words.len == 4) -// return NULL; -// printf("Received: %s, %s, %s, %s\n", words[0], words[1], words[2], words[3]); // echo for debugging -// float x = atof(words[1]); -// float y = atof(words[2]); -// float z = atof(words[3]); -// glm::vec3 new_loc = glm::vec3(x, y, z); -// bool found_match = false; -// for (int i = 0; i < camera_bodies.size(); i++) { -// if (strcmp(words[0], camera_bodies[i].name) == 0 && camera_bodies[i].b) { -// Body& b = *camera_bodies[i].b; -// glm::vec4& transl = b.pose[3]; -// transl = (0.8f * transl) + (0.2f * glm::vec4(new_loc, 1)); // lp filter -// int color_i = i + 1; -// b.color = glm::vec4(color_i & 0x4, color_i & 0x2, color_i & 0x1, 1); // reset alpha to 1 -// camera_bodies[i].hp = max_hp; -// found_match = true; -// } else if (camera_bodies[i].b) { -// float& cur_hp = camera_bodies[i].hp; -// if (cur_hp > 0) -// cur_hp -= 1; -// camera_bodies[i].b->color = glm::vec4(i & 0x4, i & 0x2, i & 0x1, cur_hp / max_hp); -// } -// } -// if (!found_match) { -// auto read = BarcodeRead { words[0], (Body*)NULL, max_hp }; -// camera_bodies.push_back(read); -// } -// } -//} - +static std::map name_to_id; int main() { Array camera_pose_axes = { NULL, 0 }; @@ -55,6 +23,24 @@ int main() { set_scale(id, 10); } - while (true) - ; + while (true) { + std::string line; + std::getline(std::cin, line); + Array words = split_str(line.c_str()); + if (!words.len == 4) + return NULL; + printf("Received: %s, %s, %s, %s\n", words[0], words[1], words[2], words[3]); // echo for debugging + std::string name = std::string(words[0]); + float x = atof(words[1]); + float y = atof(words[2]); + float z = atof(words[3]); + + if (auto it = name_to_id.find(name); it != name_to_id.end()) { + update_point(it->second, x, y, z); + } else { + name_to_id[name] = create_point(x, y, z); + set_lifetime(name_to_id[name], 0.2); + set_scale(name_to_id[name], 15); + } + } } diff --git a/src/shaders/fragment.glsl b/src/shaders/fragment.glsl index 323267a..d64db91 100644 --- a/src/shaders/fragment.glsl +++ b/src/shaders/fragment.glsl @@ -10,7 +10,7 @@ void main() { vec3 dx = dFdx(frag_pos) * 500; vec3 dy = dFdy(frag_pos) * 500; vec3 N = normalize(cross(dFdx(frag_pos), dFdy(frag_pos))); - frag_color = color + vec4(N/2, 1)/2; + frag_color = color; //frag_color = vec4(length(dx), length(dy), length(dy), 1.0); //frag_color += color; }