1
0

further tests

This commit is contained in:
Niklas Birk 2019-12-20 14:24:52 +01:00
parent 50788d8351
commit a5b69a7dad
5 changed files with 107 additions and 36 deletions

View File

@ -3,9 +3,11 @@ project(Informatikprojekt C)
set(CMAKE_C_STANDARD 11)
add_executable(Informatikprojekt main.c particlesystem.h particlesystem.c)
add_executable(Informatikprojekt main.c particlesystem.h particlesystem.c init.h)
# GLFW
add_subdirectory(./glfw-3.3)
include_directories(./glfw-3.3/include)
target_link_libraries(Informatikprojekt -lOpenGL32 glfw ${GLFW_LIBRARIES})

0
init.h Normal file
View File

72
main.c
View File

@ -20,7 +20,7 @@ int main()
glfwSetErrorCallback(error_callback);
GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
GLFWwindow* window = glfwCreateWindow(1920, 1080, "My Title", NULL, NULL);
if (!window)
{
@ -30,32 +30,46 @@ int main()
glfwMakeContextCurrent(window);
vector3f *epos = initVector3f(0, 0, 0);
emitter *e = initEmitter(epos, 1000);
particle_system *ps = initParticleSystem(1);
(ps->emitters)[0] = e;
initRandomParticles(e);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
int b = 0;
for (int i = 0; i < e->pamount; i++)
{
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)
{
b=1;
break;
}
}
if (!b)
{
updateParticles(4000, ps);
}
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_POINTS);
for (int i = 0; i < e->pamount; i++)
{
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glColor3f((float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX);
vector3f *p = (e->particles)[i]->position;
glVertex3f(p->x, p->y, p->z);
}
glEnd();
glfwSwapBuffers(window);
@ -66,20 +80,8 @@ int main()
glfwDestroyWindow(window);
glfwTerminate();
/*vector3f *epos = initVector3f(0, 0, 0);
emitter *e = initEmitter(epos, 10);
initRandomParticles(e);
printEmitter(e);
for (int i = 0; i < e->pamount; i++)
{
free((e->particles)[i]);
}
free(epos);
free(e);*/
freeEmitter(e);
return 0;
}
@ -89,12 +91,20 @@ void error_callback(int error, const char* description)
fputs(description, stderr);
}
/*************************/
float rv()
{
int i = rand()%2 ? -1 : 1;
return (float) i * rand() / RAND_MAX;
}
/*************************/
void initRandomParticles(emitter *e)
{
for (int i = 0; i < e->pamount; i++)
{
vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z);
vector3f *dir = initVector3f((float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX);
vector3f *dir = initVector3f(rv(), rv(), rv());
(e->particles)[i] = initParticle(pos, dir);
}
}

View File

@ -40,9 +40,22 @@ particle_system *initParticleSystem(int eamount)
/*
* Updates particles
*/
int updateParticles(float dt, particle_system *particleSystem)
int updateParticles(float dt, particle_system *ps)
{
emitter *e;
float t = 1 / dt;
for (int i = 0; i < ps->eamount; i++)
{
e = (ps->emitters)[i];
for (int j = 0; j < e->pamount; j++)
{
vector3f *p = (e->particles)[j]->position;
vector3f *d = (e->particles)[j]->direction;
p->x += d->x * t;
p->y += d->y * t;
p->z += d->z * t;
}
}
}
/*
@ -65,3 +78,36 @@ vector3f *initVector3f(float x, float y, float z)
vector->z = z;
return vector;
}
/*
* Frees a given emitter and all corresponding particles
*/
void freeEmitter(emitter *e)
{
for (int j = 0; j < e->pamount; j++)
{
free((e->particles)[j]);
}
free(e);
}
/*
* Frees all emitters within a particle system
*/
void freeEmitters(particle_system *ps)
{
for (int i = 0; i < ps->eamount; i++)
{
freeEmitter((ps->emitters)[i]);
}
}
/*
* Frees all emitters and particles within a particle system
*/
void freeParticleSystem(particle_system *ps)
{
freeEmitters(ps);
free(ps);
}

View File

@ -67,4 +67,17 @@ int drawParticles(particle_system *particleSystem);
*/
vector3f *initVector3f(float x, float y, float z);
/*
* Frees an given emitter with all particles
*/
void freeEmitter(emitter *e);
/*
* Frees all emitters within an particle system
*/
void freeEmitters(particle_system *ps);
/*
* Frees all emitter and particles within a particle system
*/
void freeParticleSystem(particle_system *ps);