generated from AfonsoCMSousa/CPP-OpenGLTemplate
feat: COLISIONS
This commit is contained in:
+26
-17
@@ -20,13 +20,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 = 4;
|
||||
const unsigned int THREAD_COUNT = 8;
|
||||
|
||||
const unsigned int SCR_WIDTH = 1920;
|
||||
const unsigned int SCR_HEIGHT = 1080;
|
||||
const char *glsl_version;
|
||||
|
||||
const unsigned int PARTICLE_COUNT = 100000;
|
||||
const unsigned int PARTICLE_COUNT = 2000;
|
||||
|
||||
GLFWwindow *window;
|
||||
unsigned int VBO, VAO;
|
||||
@@ -100,6 +100,7 @@ __attribute__((constructor)) static void init_glfw(void) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glEnable(GL_PROGRAM_POINT_SIZE);
|
||||
glEnable(GL_BLEND);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
@@ -220,16 +221,11 @@ 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++) {
|
||||
float x = ((float)rand() / RAND_MAX) * 2.0f - 1.0f; // Random x between -1 and 1
|
||||
float y = ((float)rand() / RAND_MAX) * 2.0f - 1.0f; // Random y between -1 and 1
|
||||
float z = ((float)rand() / RAND_MAX) * 2.0f - 1.0f; // Random z between -1 and 1
|
||||
|
||||
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;
|
||||
particles[i] = particle_init({x, 2, z}, {1.0f, 0.5f, 0.2f, 1.0f});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,10 +245,13 @@ int main() {
|
||||
|
||||
thread_params[i].time = 0;
|
||||
thread_params[i].exit = 0;
|
||||
thread_params[i].run = 0;
|
||||
thread_params[i].alive_count = 0;
|
||||
|
||||
thread_params[i].thread_id = i;
|
||||
thread_params[i].mutex = global_mutex;
|
||||
thread_params[i].mutex = &mutex;
|
||||
thread_params[i].cond = &cond;
|
||||
thread_params[i].barrier = &barrier;
|
||||
pthread_create(&threads[i], NULL, t_update, &thread_params[i]);
|
||||
}
|
||||
|
||||
@@ -284,15 +283,25 @@ int main() {
|
||||
glm::mat4 projection = camera_projection(camera, aspect);
|
||||
glm::mat4 mvp = projection * view; // model is identity
|
||||
|
||||
alive_count = 0;
|
||||
pthread_mutex_lock(&mutex);
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
thread_params->time = delta_time;
|
||||
thread_params->run = 1;
|
||||
thread_params[i].time = delta_time;
|
||||
thread_params[i].run = 1;
|
||||
}
|
||||
pthread_cond_broadcast(&cond);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
pthread_barrier_wait(&barrier);
|
||||
|
||||
alive_count = 0;
|
||||
for (int i = 0; i < THREAD_COUNT; i++)
|
||||
alive_count += thread_params[i].alive_count;
|
||||
|
||||
collision_update(particles, alive_count, 0.06f);
|
||||
|
||||
// Render
|
||||
glClearColor(0.03f, 0.03f, 0.03f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
grid_draw(g_grid, mvp);
|
||||
axis_draw(g_axis, mvp);
|
||||
@@ -318,7 +327,7 @@ 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");
|
||||
|
||||
@@ -21,6 +21,6 @@ void main() {
|
||||
vec4 clip_pos = uMVP * vec4(aPos, 1.0);
|
||||
gl_Position = clip_pos;
|
||||
|
||||
float base_size = 0.2 + size;
|
||||
float base_size = 0.5 + (size * 2000);
|
||||
gl_PointSize = max(1.0, base_size / clip_pos.w);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user