1
0

several things

This commit is contained in:
Niklas Birk 2019-12-28 13:54:53 +01:00
parent a5b69a7dad
commit a9609ed770
3 changed files with 34 additions and 20 deletions

30
main.c
View File

@ -31,44 +31,49 @@ int main()
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
vector3f *epos = initVector3f(0, 0, 0); vector3f *epos = initVector3f(0, 0, 0);
emitter *e = initEmitter(epos, 1000); emitter *e = initEmitter(epos, 10000);
particle_system *ps = initParticleSystem(1); particle_system *ps = initParticleSystem(1);
(ps->emitters)[0] = e; (ps->emitters)[0] = e;
initRandomParticles(e); initRandomParticles(e);
double time, tFrame, tLast = 0;
int width, height, b = 0;
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
int width, height; time = glfwGetTime();
tFrame = time - tLast;
tLast = time;
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
int b = 0; for (int i = 0; !b && i < e->pamount; i++)
for (int i = 0; i < e->pamount; i++)
{ {
vector3f *p = (e->particles)[i]->position; vector3f *p = (e->particles)[i]->position;
if (p->x > 1 || p->x < -1 || p->y > 1 || p->y < -1 || p->z > 1 || p->z < -1) if (p->x > 1 || p->x < -1 || p->y > 1 || p->y < -1 || p->z > 1 || p->z < -1)
{ {
b=1; b=1;
break;
} }
} }
if (!b) if (!b)
{ {
updateParticles(4000, ps); updateParticles((float) tFrame, ps);
} }
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS); glBegin(GL_POINTS);
particle *p;
vector3f *pos;
for (int i = 0; i < e->pamount; i++) for (int i = 0; i < e->pamount; i++)
{ {
p = (e->particles)[i];
glColor3f((float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX); glColor3f(p->color->x, p->color->y, p->color->z);
vector3f *p = (e->particles)[i]->position; pos = p->position;
glVertex3f(p->x, p->y, p->z); glVertex3f(pos->x, pos->y, pos->z);
} }
glEnd(); glEnd();
@ -81,7 +86,7 @@ int main()
glfwTerminate(); glfwTerminate();
free(epos); free(epos);
freeEmitter(e); freeParticleSystem(ps);
return 0; return 0;
} }
@ -105,7 +110,8 @@ void initRandomParticles(emitter *e)
{ {
vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z); vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z);
vector3f *dir = initVector3f(rv(), rv(), rv()); vector3f *dir = initVector3f(rv(), rv(), rv());
(e->particles)[i] = initParticle(pos, dir); vector3f *color = initVector3f(255, 255, 255);
(e->particles)[i] = initParticle(pos, dir, color, 100.f);
} }
} }

View File

@ -6,11 +6,13 @@
/* /*
* Initializes a particle * Initializes a particle
*/ */
particle *initParticle(vector3f *pos, vector3f *dir) particle *initParticle(vector3f *pos, vector3f *dir, vector3f *color, float age)
{ {
particle *p = malloc(sizeof(particle)); particle *p = malloc(sizeof(particle));
p->position = pos; p->position = pos;
p->direction = dir; p->direction = dir;
p->color = color;
p->age = age;
return p; return p;
} }
@ -43,17 +45,21 @@ particle_system *initParticleSystem(int eamount)
int updateParticles(float dt, particle_system *ps) int updateParticles(float dt, particle_system *ps)
{ {
emitter *e; emitter *e;
float t = 1 / dt; particle *p;
for (int i = 0; i < ps->eamount; i++) for (int i = 0; i < ps->eamount; i++)
{ {
e = (ps->emitters)[i]; e = (ps->emitters)[i];
for (int j = 0; j < e->pamount; j++) for (int j = 0; j < e->pamount; j++)
{ {
vector3f *p = (e->particles)[j]->position; p = (e->particles)[j];
vector3f *d = (e->particles)[j]->direction;
p->x += d->x * t; p->position->x += p->direction->x * dt;
p->y += d->y * t; p->position->y += p->direction->y * dt;
p->z += d->z * t; p->position->z += p->direction->z * dt;
p->color->x -= 0.25f;
p->color->y -= 0.25f;
p->color->z -= 0.25f;
} }
} }
} }

View File

@ -15,6 +15,8 @@ typedef struct particle
{ {
vector3f *position; vector3f *position;
vector3f *direction; vector3f *direction;
vector3f *color;
float age;
} particle; } particle;
/* /*
@ -39,7 +41,7 @@ typedef struct particle_system
/* /*
* Initializes a particle * Initializes a particle
*/ */
particle *initParticle(vector3f *pos, vector3f *dir); particle *initParticle(vector3f *pos, vector3f *dir, vector3f *color, float age);
/* /*
* Initializes an emitter * Initializes an emitter