diff --git a/inc/util.hpp b/inc/util.hpp index 82ee7ae..fc7eb23 100644 --- a/inc/util.hpp +++ b/inc/util.hpp @@ -18,7 +18,7 @@ template struct array { template void append(array& a, T el); template T pop(array& a); template void resize(array& a, size_t new_cap); -template void free(array& a); +template void clear(array& a); bool read_file(array* out, const char* filepath); array split_str(const char *s, char delimiter); array split_str(const char *s); diff --git a/src/body.cpp b/src/body.cpp index 7186ba1..ad3fb2d 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -81,6 +81,9 @@ bool load_body(body* out_body, const char* obj_filepath) { } uint vao, vbo, ebo; + vao = 0; + vbo = 0; + ebo = 0; glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); diff --git a/src/main.cpp b/src/main.cpp index 03cb685..a4e347f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,18 +30,16 @@ void process_input() { } static uint shader; -body create_new_sphere() { - body b; - assert(load_body(&b, "Icosphere.obj")); - b.shader = shader; - b.pose = glm::translate(b.pose, glm::vec3(0, 0, 0)); - return b; +void create_new_sphere(body* b) { + assert(load_body(b, "Icosphere.obj")); + b->shader = shader; + b->pose = glm::translate(b->pose, glm::vec3(0, 0, 0)); } 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 -std::vector> camera_bodies; // I would use my array here, but was getting a linking error +static std::vector> camera_bodies; // I would use my array here, but was getting a linking error void *process_cin(void* args) { std::string line; @@ -50,31 +48,28 @@ void *process_cin(void* args) { 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; + pthread_mutex_lock(&lock); 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]; + if (strcmp(words[0], std::get<0>(camera_bodies[i])) == 0 && std::get<1>(camera_bodies[i])) { + glm::vec4 &transl = std::get<1>(camera_bodies[i])->pose[3]; 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<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 { + } else if (std::get<1>(camera_bodies[i])) { 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); + 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); + auto t = std::make_tuple(words[0], (body*)NULL, max_hp); camera_bodies.push_back(t); } pthread_mutex_unlock(&lock); @@ -187,8 +182,15 @@ int main() { 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.color = glm::vec4(0, 0.5, 0, 1); + /* body b; + assert(load_body(&b, "Icosphere.obj")); + b.shader = shader; + b.pose = glm::translate(b.pose, glm::vec3(5, 5, 5)); + b.color = glm::vec4(1, 0, 0, 1); + + body b2; + create_new_sphere(&b2); + b2.color = glm::vec4(0, 0.5, 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 + @@ -198,7 +200,7 @@ int main() { glEnable(GL_DEPTH_TEST); glm::mat4 projection_t - = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 1300.0f); + = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1300.0f); pthread_t thread_id; pthread_create(&thread_id, NULL, process_cin, NULL); @@ -209,12 +211,19 @@ int main() { set_uniform(shader, "camera_t", camera_t); set_uniform(shader, "projection_t", projection_t); - draw_body(b2); + /*draw_body(b); + draw_body(b2);*/ pthread_mutex_lock(&lock); for (int i = 0; i < camera_bodies.size(); i++) { - draw_body(std::get<1>(camera_bodies[i])); + if (!std::get<1>(camera_bodies[i])) { + body* b = (body*)malloc(sizeof(body)); + create_new_sphere(b); + b->color = glm::vec4(i & 0x4, i& 0x2, i & 0x1, max_hp); + std::get<1>(camera_bodies[i]) = b; + } + draw_body(*std::get<1>(camera_bodies[i])); } pthread_mutex_unlock(&lock); diff --git a/src/util.cpp b/src/util.cpp index 14fbb02..4030da2 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -10,7 +10,7 @@ array _split_str_inner(const char* s, char delim, bool just_check_ws); template void append(array& a, T el) { if (a.len == a.cap) { - resize(a, a.cap * 2); + resize(a, max(8, a.cap * 2)); } a[a.len] = el; a.len++; @@ -23,20 +23,18 @@ template T pop(array& a) { } template void resize(array& a, size_t new_cap) { - T* new_data = (T*)malloc(new_cap); + T* new_data = (T*)malloc(sizeof(T) * new_cap); if (a.len > 0) { memcpy(new_data, a.data, min(a.len, new_cap)); } free(a.data); + a.data = new_data; a.len = min(a.len, new_cap); a.cap = new_cap; } -template void free(array& a) { - if (a.len > 0) - free(a.data); +template void clear(array& a) { a.len = 0; - a.cap = 0; } bool read_file(array* out, const char* filepath) { @@ -76,7 +74,7 @@ array _split_str_inner(const char* s, char delim, bool just_check_ws) { memcpy(word, cur_word.data, cur_word.len); word[cur_word.len] = '\0'; append(res, word); - free(cur_word); + clear(cur_word); if (c == '\0') { break; } @@ -89,6 +87,5 @@ array _split_str_inner(const char* s, char delim, bool just_check_ws) { break; } } - free(cur_word); return res; } diff --git a/tester.py b/tester.py new file mode 100644 index 0000000..6490329 --- /dev/null +++ b/tester.py @@ -0,0 +1,23 @@ +import time +import numpy as np +import subprocess +from pathlib import Path + +p = subprocess.Popen(Path("bin", "x64", "Debug", "LivePlotter.exe"), stdin=subprocess.PIPE) + +x = 0 +y = 0 +z = 0 +t = 0 +period = 0.1 +while True: + time.sleep(period) + x = 10*np.cos(t) + y = 10*np.sin(t) + z = 0 + t += period + p.stdin.write(f"test {x} {y} {z}\n".encode("utf8")) + p.stdin.write(f"test2 {x} {-y} {z}\n".encode("utf8")) + p.stdin.write(f"test3 {-x} {y} {z}\n".encode("utf8")) + p.stdin.write(f"test4 {-x} {-y} {z}\n".encode("utf8")) + p.stdin.flush()