generated from AfonsoCMSousa/CPP-OpenGLTemplate
feat: added multithreading
This commit is contained in:
+62
-8
@@ -9,18 +9,24 @@
|
||||
#include "imgui/imgui.h"
|
||||
#include "imgui/imgui_impl_glfw.h"
|
||||
#include "imgui/imgui_impl_opengl3.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
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 = 4;
|
||||
|
||||
const unsigned int SCR_WIDTH = 1920;
|
||||
const unsigned int SCR_HEIGHT = 1080;
|
||||
const char *glsl_version;
|
||||
|
||||
const unsigned int PARTICLE_COUNT = 1000;
|
||||
const unsigned int PARTICLE_COUNT = 100000;
|
||||
|
||||
GLFWwindow *window;
|
||||
unsigned int VBO, VAO;
|
||||
@@ -31,6 +37,10 @@ Camera *g_camera = nullptr;
|
||||
AxisRenderer g_axis;
|
||||
GridRenderer g_grid;
|
||||
|
||||
pthread_mutex_t mutex;
|
||||
pthread_cond_t cond;
|
||||
pthread_barrier_t barrier;
|
||||
|
||||
static void cursor_callback(GLFWwindow *w, double xpos, double ypos) {
|
||||
#ifndef NDEBUG
|
||||
// Let ImGui eat mouse when cursor is free
|
||||
@@ -57,6 +67,8 @@ static void mouse_button_callback(GLFWwindow *w, int button, int action, int mod
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void init_glfw(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
if (!glfwInit()) {
|
||||
fprintf(stderr, "GLFW initialization failed\n");
|
||||
abort(); // program cannot continue
|
||||
@@ -97,17 +109,25 @@ __attribute__((constructor)) static void init_glfw(void) {
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Position: 3 floats, starts at 0
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void *)0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
// Color: 4 floats, starts after 6 floats (Position + Velocity)
|
||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void *)(6 * sizeof(float)));
|
||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Life: 1 float, starts after 10 floats (Position + Velocity + Color)
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 11 * sizeof(float), (void *)(10 * sizeof(float)));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(10 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
// Dampening: 1 float, starts after 11 floars (Positon + Velocity + Color + Life)
|
||||
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(11 * sizeof(float)));
|
||||
glEnableVertexAttribArray(3);
|
||||
|
||||
// Size: 1 float, starts after 11 floars (Positon + Velocity + Color + Life + Dampening)
|
||||
glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 13 * sizeof(float), (void *)(12 * sizeof(float)));
|
||||
glEnableVertexAttribArray(4);
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(Particle) * PARTICLE_COUNT, NULL, GL_DYNAMIC_DRAW);
|
||||
|
||||
char *vertex_shader_source = read_file(VERTEX_SHADER_SOURCE);
|
||||
@@ -179,6 +199,9 @@ int main() {
|
||||
init_imgui();
|
||||
#endif
|
||||
|
||||
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * THREAD_COUNT);
|
||||
Physics_params *thread_params = (Physics_params *)malloc(sizeof(Physics_params) * THREAD_COUNT);
|
||||
|
||||
Camera camera = camera_init(glm::vec3(2.0f, 2.0f, 5.0f), SCR_WIDTH, SCR_HEIGHT);
|
||||
g_camera = &camera;
|
||||
|
||||
@@ -197,8 +220,16 @@ int main() {
|
||||
snprintf(errLog, 128, "There was a issue allocating memory for the particle list\n");
|
||||
} else {
|
||||
for (unsigned long long i = 0; i < PARTICLE_COUNT; i++) {
|
||||
particles[i] = particle_init({0.0, 0.0, 0.0}, {1.0, 1.0, 1.0, 1.0});
|
||||
particles[i].life = -1.0f; // Start with imortal particles
|
||||
|
||||
float x, y, z;
|
||||
|
||||
x = (float)(rand() % 200) / 100; // [0.0 , 2.0]
|
||||
y = (float)(rand() % 200) / 100; // [0.0 , 2.0]
|
||||
z = (float)(rand() % 200) / 100; // [0.0 , 2.0]
|
||||
|
||||
particles[i] = particle_init({x, y, z}, {x, y, z, 1.0});
|
||||
particles[i].life = -2.0f; // Start with imortal particles
|
||||
particles[i].size = 20;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,6 +238,24 @@ int main() {
|
||||
float delta_time;
|
||||
int alive_count = 0;
|
||||
|
||||
pthread_mutex_init(&mutex, NULL);
|
||||
pthread_cond_init(&cond, NULL);
|
||||
pthread_barrier_init(&barrier, NULL, THREAD_COUNT + 1);
|
||||
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
thread_params[i].particle = particles;
|
||||
thread_params[i].interval_start = (PARTICLE_COUNT / THREAD_COUNT) * i;
|
||||
thread_params[i].interval_end = (PARTICLE_COUNT / THREAD_COUNT) * (i + 1);
|
||||
|
||||
thread_params[i].time = 0;
|
||||
thread_params[i].exit = 0;
|
||||
thread_params[i].alive_count = 0;
|
||||
|
||||
thread_params[i].thread_id = i;
|
||||
thread_params[i].mutex = global_mutex;
|
||||
pthread_create(&threads[i], NULL, t_update, &thread_params[i]);
|
||||
}
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
current_time = static_cast<float>(glfwGetTime());
|
||||
delta_time = current_time - last_time;
|
||||
@@ -235,7 +284,11 @@ int main() {
|
||||
glm::mat4 projection = camera_projection(camera, aspect);
|
||||
glm::mat4 mvp = projection * view; // model is identity
|
||||
|
||||
alive_count = particle_update(particles, PARTICLE_COUNT, delta_time);
|
||||
alive_count = 0;
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
thread_params->time = delta_time;
|
||||
thread_params->run = 1;
|
||||
}
|
||||
|
||||
// Render
|
||||
glClearColor(0.03f, 0.03f, 0.03f, 1.0f);
|
||||
@@ -264,7 +317,8 @@ int main() {
|
||||
ImGui::Text("Camera: %.2f %.2f %.2f", (double)camera.position.x, (double)camera.position.y, (double)camera.position.z);
|
||||
ImGui::Text("Alive particles: %d", alive_count);
|
||||
if (particles)
|
||||
ImGui::Text("P[0]: %.2f %.2f %.2f", (double)particles[0].position.x, (double)particles[0].position.y, (double)particles[0].position.z);
|
||||
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);
|
||||
ImGui::Separator();
|
||||
ImGui::Text("Left-click to capture mouse");
|
||||
ImGui::Text("ESC = release mouse / quit");
|
||||
|
||||
@@ -4,7 +4,10 @@ in vec4 vColor;
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
// Soft circular point — discard corners to make round dots
|
||||
vec2 coord = gl_PointCoord - vec2(0.5);
|
||||
if (dot(coord, coord) > 0.25)
|
||||
discard;
|
||||
|
||||
FragColor = vColor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+18
-10
@@ -1,18 +1,26 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec3 aPos; // Position
|
||||
layout(location = 1) in vec4 aColor; // Color (RGBA)
|
||||
layout(location = 2) in float aLife; // Life
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec4 aColor;
|
||||
layout(location = 2) in float aLife;
|
||||
layout(location = 4) in float size;
|
||||
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uMVP;
|
||||
uniform float uScreenHeight;
|
||||
|
||||
void main() {
|
||||
float alpha = aLife;
|
||||
if (aLife > 0.0) {
|
||||
alpha = aLife / 10.0; // Fade mortal particles
|
||||
} else if (aLife < 0.0) {
|
||||
alpha = 1.0; // Eternal particles are fully opaque
|
||||
float alpha;
|
||||
if (aLife < 0.0) {
|
||||
alpha = 1.0; // immortal — fully opaque
|
||||
} else {
|
||||
alpha = clamp(aLife / 2.0, 0.0, 1.0); // fade over last 2 seconds of life
|
||||
}
|
||||
|
||||
vColor = vec4(aColor.rgb, aColor.a * alpha);
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
gl_PointSize = 10.0; // Make them big enough to see!
|
||||
vec4 clip_pos = uMVP * vec4(aPos, 1.0);
|
||||
gl_Position = clip_pos;
|
||||
|
||||
float base_size = 0.2 + size;
|
||||
gl_PointSize = max(1.0, base_size / clip_pos.w);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user