1
0
This commit is contained in:
Niklas Birk 2020-01-08 17:33:01 +01:00
parent 8eb0a12b83
commit e8ecec7cef
3 changed files with 45 additions and 20 deletions

33
main.c
View File

@ -46,7 +46,6 @@ int main()
tFrame = time - tLast; tFrame = time - tLast;
tLast = time; tLast = time;
glClearColor(0.05f, 0.05f, 0.05f, 0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
@ -62,12 +61,16 @@ int main()
glColor3f(p->color->x, p->color->y, p->color->z); glColor3f(p->color->x, p->color->y, p->color->z);
pos = p->position; pos = p->position;
glBegin(GL_QUADS); glBegin(GL_POINTS);
glVertex3f(pos->x, pos->y, pos->z); glVertex3f(pos->x, pos->y, pos->z);
glVertex3f(pos->x+.1, pos->y, pos->z);
glVertex3f(pos->x+.1, pos->y-.1, pos->z);
glVertex3f(pos->x, pos->y-.1, pos->z);
glEnd(); glEnd();
/*glBegin(GL_QUADS);
glVertex3f(pos->x, pos->y, pos->z);
glVertex3f(pos->x+.01, pos->y, pos->z);
glVertex3f(pos->x+.01, pos->y-.01, pos->z);
glVertex3f(pos->x, pos->y-.01, pos->z);
glEnd();*/
} }
glfwSwapBuffers(window); glfwSwapBuffers(window);
@ -92,28 +95,22 @@ void calcPos(particle *p, float dt)
void calcCol(particle *p) void calcCol(particle *p)
{ {
p->color->x = 0.5f; p->color->x = 1;
p->color->y = 0.01f; p->color->y = 1;
p->color->z = 0.9f; p->color->z = 1;
} }
/*************************************************************************************************************/ /*************************************************************************************************************/
/*************************/
float rv()
{
int i = rand()%2 ? -1 : 1;
return (float) i * rand() / RAND_MAX;
}
/*************************/
void initRandomParticles(emitter *e) void initRandomParticles(emitter *e)
{ {
for (int i = 0; i < e->pamount; i++) for (int i = 0; i < e->pamount; i++)
{ {
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(((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX,
((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX,
((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX);
vector3f *color = initVector3f(1, 1, 1); vector3f *color = initVector3f(1, 1, 1);
(e->particles)[i] = initParticle(pos, dir, color, 100.f); (e->particles)[i] = initParticle(pos, dir, color, rand() / 100.0f);
} }
} }

View File

@ -1,4 +1,5 @@
#include <malloc.h> #include <malloc.h>
#include <stdlib.h>
#include "particlesystem.h" #include "particlesystem.h"
@ -51,12 +52,34 @@ int updateParticles(float dt, particle_system *ps, CalculatePositionFunction cal
for (int j = 0; j < e->pamount; j++) for (int j = 0; j < e->pamount; j++)
{ {
p = (e->particles)[j]; p = (e->particles)[j];
calculatePosition(p, dt);
calculateColor(p); if (p->age < 0)
{
resetParticle(e, p);
}
else
{
calculatePosition(p, dt);
calculateColor(p);
p->age -= 0.1f;
}
} }
} }
} }
void resetParticle(emitter *e, particle *p)
{
p->position->x = e->position->x;
p->position->y = e->position->y;
p->position->z = e->position->z;
p->direction->x = ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX;
p->direction->y = ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX;
p->direction->z = ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX;
p->age = rand() / 100.0f;
}
/* /*
* Draws particles * Draws particles
*/ */

View File

@ -68,6 +68,11 @@ particle_system *initParticleSystem(int eamount);
*/ */
int updateParticles(float dt, particle_system *particleSystem, CalculatePositionFunction calculatePosition, CalculateColorFunction calculateColor); int updateParticles(float dt, particle_system *particleSystem, CalculatePositionFunction calculatePosition, CalculateColorFunction calculateColor);
/*
* Resets a particle to seed at emitter's position
*/
void resetParticle(emitter *e, particle *p);
/* /*
* Updates particle * Updates particle
*/ */