generated from AfonsoCMSousa/CPP-OpenGLTemplate
148 lines
2.9 KiB
C
148 lines
2.9 KiB
C
#ifndef PARTICLE_H
|
|
#define PARTICLE_H
|
|
|
|
#include <pthread.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <stdatomic.h>
|
|
|
|
#define G_CONSTANT 0.207f
|
|
|
|
typedef struct {
|
|
float x, y, z;
|
|
} Vec3;
|
|
|
|
typedef struct {
|
|
float r, g, b, a;
|
|
} Color;
|
|
|
|
typedef struct {
|
|
Vec3 position;
|
|
Vec3 velocity;
|
|
Color color;
|
|
|
|
float dampening;
|
|
float life;
|
|
float size;
|
|
} Particle;
|
|
|
|
typedef struct {
|
|
Particle *particle;
|
|
unsigned long long thread_id;
|
|
unsigned long long interval_start;
|
|
unsigned long long interval_end;
|
|
int alive_count;
|
|
float time;
|
|
|
|
pthread_mutex_t *mutex;
|
|
pthread_cond_t *cond;
|
|
int *barrier;
|
|
atomic_int exit;
|
|
} Physics_params;
|
|
|
|
// INFO: This adds adder to out, basicaly out + adder
|
|
void vec_add(Vec3 &out, Vec3 adder) {
|
|
out.x += adder.x;
|
|
out.y += adder.y;
|
|
out.z += adder.z;
|
|
}
|
|
|
|
Particle particle_init(Vec3 position, Color color) {
|
|
Particle p;
|
|
p.position = position;
|
|
p.color = color;
|
|
p.velocity = {0.0f, 0.0f, 0.0f};
|
|
|
|
p.life = 10;
|
|
p.dampening = 0.97f;
|
|
return p;
|
|
}
|
|
|
|
int particle_update(Particle *particles, unsigned long long size, float time) {
|
|
int alive_count = 0;
|
|
for (unsigned long long i = 0; i < size; i++) {
|
|
Particle p = particles[i];
|
|
if (p.life == 0) {
|
|
continue;
|
|
}
|
|
|
|
// p.velocity.y -= (G_CONSTANT * time);
|
|
|
|
p.position.x += p.velocity.x * time;
|
|
p.position.y += p.velocity.y * time;
|
|
p.position.z += p.velocity.z * time;
|
|
|
|
if (p.position.y <= 0) {
|
|
p.position.y = 0;
|
|
p.velocity.y *= (-1 * p.dampening);
|
|
}
|
|
|
|
if (p.life > 0)
|
|
p.life -= time;
|
|
if (p.life < 0 && p.life >= -1.0f)
|
|
p.life = 0;
|
|
|
|
particles[i] = p;
|
|
alive_count++;
|
|
}
|
|
|
|
return alive_count;
|
|
}
|
|
|
|
void *t_update(void *args) {
|
|
Physics_params *params = (Physics_params *)args;
|
|
while (params->exit != 1) {
|
|
pthread_mutex_lock(params->mutex);
|
|
if (params->(missing) == 0) {
|
|
sleep(100);
|
|
}
|
|
pthread_mutex_unlock(params->mutex);
|
|
|
|
params->alive_count = 0;
|
|
for (unsigned long long i = params->interval_start; i < params->interval_end; i++) {
|
|
Particle p = params->particle[i];
|
|
|
|
if (p.life == 0) {
|
|
continue;
|
|
}
|
|
|
|
p.velocity.y -= (G_CONSTANT * params->time);
|
|
|
|
p.position.x += p.velocity.x * params->time;
|
|
p.position.y += p.velocity.y * params->time;
|
|
p.position.z += p.velocity.z * params->time;
|
|
|
|
if (p.position.y <= 0) {
|
|
p.position.y = 0;
|
|
p.velocity.y *= (-1 * p.dampening);
|
|
}
|
|
|
|
if (p.life > 0)
|
|
p.life -= params->time;
|
|
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};
|
|
;
|
|
}
|
|
|
|
params->particle[i] = p;
|
|
params->alive_count++;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
#endif // PARTICLE_H
|