balls are moving on my screen

This commit is contained in:
2025-08-20 00:08:26 -05:00
parent 7bf24beb45
commit e7a623b5a5
5 changed files with 63 additions and 31 deletions

View File

@@ -18,7 +18,7 @@ template<class T> struct array {
template<class T> void append(array<T>& a, T el);
template<class T> T pop(array<T>& a);
template<class T> void resize(array<T>& a, size_t new_cap);
template<class T> void free(array<T>& a);
template<class T> void clear(array<T>& a);
bool read_file(array<char>* out, const char* filepath);
array<char*> split_str(const char *s, char delimiter);
array<char*> split_str(const char *s);

View File

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

View File

@@ -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<std::tuple<char*, body, float>> camera_bodies; // I would use my array here, but was getting a linking error
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) {
std::string line;
@@ -50,31 +48,28 @@ void *process_cin(void* args) {
array<char*> 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);

View File

@@ -10,7 +10,7 @@ array<char*> _split_str_inner(const char* s, char delim, bool just_check_ws);
template<class T> void append(array<T>& 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<class T> T pop(array<T>& a) {
}
template<class T> void resize(array<T>& 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<class T> void free(array<T>& a) {
if (a.len > 0)
free(a.data);
template<class T> void clear(array<T>& a) {
a.len = 0;
a.cap = 0;
}
bool read_file(array<char>* out, const char* filepath) {
@@ -76,7 +74,7 @@ array<char*> _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<char*> _split_str_inner(const char* s, char delim, bool just_check_ws) {
break;
}
}
free(cur_word);
return res;
}

23
tester.py Normal file
View File

@@ -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()