feat: thread view - change between Normal colors and Thread ID color view

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2026-03-24 17:21:37 +00:00
parent d35d7bff69
commit bf56d3377a
4 changed files with 39 additions and 26 deletions

View File

@ -4,11 +4,13 @@
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define G_CONSTANT 1.98f
#define G_CONSTANT 1.98
extern bool viewThreads;
typedef struct {
float x, y, z;
@ -130,9 +132,9 @@ void collision_update(Particle *particles, unsigned long long count, float resti
float velocity_relative_y = p1.velocity.y - p2.velocity.y;
float velocity_relative_z = p1.velocity.z - p2.velocity.z;
float v_rel = vec_dot({velocity_relative_x, velocity_relative_y, velocity_relative_z}, normal);
float v_rel = vec_dot({velocity_relative_x, velocity_relative_y, velocity_relative_z}, normal);
if (v_rel < 0) {
float impulse = -(1 + restitution) * v_rel / 2.0f;
float impulse = -(1 + restitution) * v_rel / 2.0f;
p1.velocity.x -= impulse * normal.x;
p1.velocity.y -= impulse * normal.y;
@ -180,21 +182,15 @@ void *t_update(void *args) {
if (p.life < 0 && p.life >= -1.0f)
p.life = 0;
// DEBUG
/*
if (params->thread_id == 0) {
p.color = {1.0f, 1.0f, 1.0f, 1.0f};
} else if (params->thread_id == 1) {
p.color = {1.0f, 0.0f, 0.0f, 1.0f};
} else if (params->thread_id == 2) {
p.color = {0.0f, 1.0f, 0.0f, 1.0f};
} else if (params->thread_id == 3) {
p.color = {0.0f, 0.0f, 1.0f, 1.0f};
} else {
p.color = {0.7f, 0.7f, 0.7f, 1.0f};
;
if (viewThreads == true) {
unsigned int id = (params->thread_id + 1) * 2654435761u;
float r = ((id >> 16) & 0xFF) / 255.0f;
float g = ((id >> 8) & 0xFF) / 255.0f;
float b = ((id >> 0) & 0xFF) / 255.0f;
p.color = {r, g, b, 1.0f};
}
*/
params->particle[i] = p;
params->alive_count++;
@ -203,7 +199,7 @@ void *t_update(void *args) {
pthread_barrier_wait(params->barrier);
}
printf("INFO: Thread %llu exiting\n", params->thread_id);
printf("INFO: Thread %llu exiting\n", params->thread_id);
return NULL;
}

BIN
psystem

Binary file not shown.

0
pthread_barrier.h Normal file
View File

View File

@ -25,13 +25,13 @@ const char FRAGMENT_SHADER_SOURCE[] = "./source/shader/fragment.glsl";
const char VERTEX_SHADER_SOURCE[] = "./source/shader/vertex.glsl";
const char PROGRAM_NAME[] = "Particle Sim";
const unsigned int THREAD_COUNT = 16;
const unsigned int THREAD_COUNT = 8;
const unsigned int SCR_WIDTH = 2560;
const unsigned int SCR_HEIGHT = 1440;
const char *glsl_version;
const unsigned int PARTICLE_COUNT = 5000;
const unsigned int PARTICLE_COUNT = 8;
GLFWwindow *window;
unsigned int VBO, VAO;
@ -46,6 +46,9 @@ pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_barrier_t barrier;
// INFO: Debug views toggles
bool viewThreads = 0;
static void cursor_callback(GLFWwindow *w, double xpos, double ypos) {
#ifndef NDEBUG
// Let ImGui eat mouse when cursor is free
@ -216,11 +219,12 @@ int main() {
g_grid = grid_init();
// Register callbacks AFTER ImGui init so ImGui gets them first via chain
glfwSetCursorPosCallback(window, cursor_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
// glfwSetCursorPosCallback(window, cursor_callback);
// glfwSetScrollCallback(window, scroll_callback);
// glfwSetMouseButtonCallback(window, mouse_button_callback);
Particle *particles = (Particle *)malloc(sizeof(Particle) * PARTICLE_COUNT);
Color *default_particle_color = (Color *)malloc(sizeof(Color) * PARTICLE_COUNT);
if (particles == NULL) {
snprintf(errLog, 128, "There was a issue allocating memory for the particle list\n");
@ -240,7 +244,9 @@ int main() {
float y = (float)(rand() % 200) / 100.0f; // Random y between 0 and 2
float z = (float)(rand() % 200) / 100.0f; // Random z between 0 and 2
default_particle_color[i] = {x, y, z, 255.0f};
particles[i] = particle_init({x, y, z}, {x, y, z, 255.0f});
particles[i].radius = (float)(rand() % 50) / 1000.0f;
}
/*
@ -317,7 +323,7 @@ int main() {
for (int i = 0; i < THREAD_COUNT; i++)
alive_count += thread_params[i].alive_count;
collision_update(particles, alive_count, 0.3f);
// collision_update(particles, alive_count, 0.3f);
// Render
glClearColor(0.03f, 0.03f, 0.03f, 1.0f);
@ -347,11 +353,21 @@ int main() {
ImGui::Text("Alive particles: %d", alive_count);
if (particles)
ImGui::Text("P[0]: %3.2f %3.2f %3.2f, Life: %0.2f", (double)particles[0].position.x, (double)particles[0].position.y, (double)particles[0].position.z,
(double)particles[0].life);
(double)particles[0].life);
ImGui::Separator();
ImGui::Text("Left-click to capture mouse");
ImGui::Text("ESC = release mouse / quit");
ImGui::Text("%s", errLog);
if (ImGui::Checkbox("Thread View", &viewThreads)) {
if (!viewThreads) {
snprintf(errLog, 128, "Switched views to default\n");
for (unsigned long long i = 0; i < PARTICLE_COUNT; i++)
particles[i].color = default_particle_color[i];
} else {
snprintf(errLog, 128, "Switched views to thread view\n");
}
}
ImGui::End();
// Render ImGui
@ -373,6 +389,7 @@ int main() {
free(threads);
free(thread_params);
free(particles);
free(default_particle_color);
ImGui_ImplGlfw_Shutdown();
ImGui_ImplOpenGL3_Shutdown();