diff --git a/LivePlotter.vcxproj b/LivePlotter.vcxproj index 1e30ef5..e13aa13 100644 --- a/LivePlotter.vcxproj +++ b/LivePlotter.vcxproj @@ -116,7 +116,7 @@ Console true - pthreadVC3d.lib;glfw3.lib;opengl32.lib;%(AdditionalDependencies) + ext\pthreads4w-code\build\Debug\pthreadVC3d.lib;glfw3.lib;opengl32.lib;%(AdditionalDependencies) @@ -145,4 +145,10 @@ + + + + + + \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0d8694e..03cb685 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -42,46 +41,45 @@ body create_new_sphere() { static bool stop = false; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; const float max_hp = 10; // Number of scans (without a particular barcode) for which the sphere will still be visible -array> camera_bodies; +std::vector> camera_bodies; // I would use my array here, but was getting a linking error -//void process_cin(void* args) { -// size_t len = 256; -// std::string line; -// while (true) { -// std::getline(std::cin, line); -// array words = split_str(line.c_str()); -// assert(words.len == 4); -// printf("Received: %s, %s, %s, %s\n", words[0], words[1], words[2], words[3]); // echo for debugging -// pthread_mutex_lock(&lock); -// 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.len; i++) { -// if (strcmp(words[0], std::get<0>(camera_bodies[i])) == 0) { -// glm::vec4 &transl = std::get<1>(camera_bodies[i]).pose[4]; -// transl = 0.9f*transl + 0.1f*glm::vec4(new_loc, 1); // filter -// -// std::get<1>(camera_bodies[i]).color = glm::vec4(i&0x4, i&0x2, i&0x1, 1); -// std::get<2>(camera_bodies[i]) = max_hp; -// found_match = true; -// } else { -// float &cur_hp = std::get<2>(camera_bodies[i]); -// if (cur_hp > 0) cur_hp -= 1; -// std::get<1>(camera_bodies[i]).color = glm::vec4(i&0x4, i&0x2, i&0x1, cur_hp/max_hp); -// } -// } -// if (!found_match) { -// body b = create_new_sphere(); -// int i = camera_bodies.len; -// b.color = glm::vec4(i&0x4, i&0x2, i&0x1, max_hp); -// auto t = std::make_tuple(words[0], b, max_hp); -// append(camera_bodies, t); -// } -// pthread_mutex_unlock(&lock); -// } -//} +void *process_cin(void* args) { + std::string line; + while (true) { + std::getline(std::cin, line); + array words = split_str(line.c_str()); + assert(words.len == 4); + printf("Received: %s, %s, %s, %s\n", words[0], words[1], words[2], words[3]); // echo for debugging + pthread_mutex_lock(&lock); + 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], std::get<0>(camera_bodies[i])) == 0) { + glm::vec4 &transl = std::get<1>(camera_bodies[i]).pose[4]; + transl = 0.9f*transl + 0.1f*glm::vec4(new_loc, 1); // filter + + std::get<1>(camera_bodies[i]).color = glm::vec4(i&0x4, i&0x2, i&0x1, 1); + std::get<2>(camera_bodies[i]) = max_hp; + found_match = true; + } else { + float &cur_hp = std::get<2>(camera_bodies[i]); + if (cur_hp > 0) cur_hp -= 1; + std::get<1>(camera_bodies[i]).color = glm::vec4(i&0x4, i&0x2, i&0x1, cur_hp/max_hp); + } + } + if (!found_match) { + body b = create_new_sphere(); + int i = camera_bodies.size(); + b.color = glm::vec4(i&0x4, i&0x2, i&0x1, max_hp); + auto t = std::make_tuple(words[0], b, max_hp); + camera_bodies.push_back(t); + } + pthread_mutex_unlock(&lock); + } +} static bool mouse_pressed = false; static bool scroll_pressed = false; @@ -199,10 +197,12 @@ int main() { glDisable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); - float time; glm::mat4 projection_t = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 1300.0f); + pthread_t thread_id; + pthread_create(&thread_id, NULL, process_cin, NULL); + while (!glfwWindowShouldClose(window)) { process_input(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -211,6 +211,14 @@ int main() { draw_body(b2); + pthread_mutex_lock(&lock); + + for (int i = 0; i < camera_bodies.size(); i++) { + draw_body(std::get<1>(camera_bodies[i])); + } + + pthread_mutex_unlock(&lock); + glfwSwapBuffers(window); glfwPollEvents(); }