1
0

144 lines
3.5 KiB
C
Raw Normal View History

2019-12-16 19:12:24 +01:00
#include <stdio.h>
2019-12-17 22:29:36 +01:00
#include <malloc.h>
#include <stdlib.h>
2019-12-18 18:18:55 +01:00
2019-12-17 22:29:36 +01:00
#include "particlesystem.h"
#include "initOpenGL.h"
2019-12-17 22:29:36 +01:00
char *printVector(vector3f *v);
void printParticle(particle *v);
void printEmitter(emitter *e);
void initRandomParticles(emitter *e);
2019-12-18 18:18:55 +01:00
void error_callback(int error, const char* description);
2019-12-16 19:12:24 +01:00
void calcPos(particle *p, float dt);
void calcCol(particle *p);
2019-12-16 19:12:24 +01:00
int main()
{
// Init OpenGL and GLFW
initGLFW();
setErrorCallbackGL();
2019-12-18 18:18:55 +01:00
int width = 800, height = 800;
GLFWwindow *window = createGLFWWindow(width, height, "Informatikprojekt - OpenGL");
2019-12-18 18:18:55 +01:00
setCurrentContextGL(window);
setFramebufferSizeCallbackGL(window);
2019-12-18 18:18:55 +01:00
// glad
initGlad();
2019-12-18 18:18:55 +01:00
2019-12-20 14:24:52 +01:00
vector3f *epos = initVector3f(0, 0, 0);
emitter *e = initEmitter(epos, 1000);
2019-12-20 14:24:52 +01:00
particle_system *ps = initParticleSystem(1);
(ps->emitters)[0] = e;
initRandomParticles(e);
2019-12-28 13:54:53 +01:00
double time, tFrame, tLast = 0;
unsigned int vbo;
float vert[3] = {0.1f, 0.2f, 0.3f};
2019-12-28 13:54:53 +01:00
2019-12-18 18:18:55 +01:00
while (!glfwWindowShouldClose(window))
{
2019-12-28 13:54:53 +01:00
time = glfwGetTime();
tFrame = time - tLast;
tLast = time;
2019-12-18 18:18:55 +01:00
glClear(GL_COLOR_BUFFER_BIT);
2019-12-18 18:18:55 +01:00
glfwGetFramebufferSize(window, &width, &height);
2019-12-20 14:24:52 +01:00
initVertexBuffer(&vbo, vert);
2019-12-20 14:24:52 +01:00
updateParticles((float) tFrame, ps, calcPos, calcCol);
2019-12-18 18:18:55 +01:00
2019-12-28 13:54:53 +01:00
particle *p;
vector3f *pos;
2019-12-20 14:24:52 +01:00
for (int i = 0; i < e->pamount; i++)
{
2019-12-28 13:54:53 +01:00
p = (e->particles)[i];
glColor3f(p->color->x, p->color->y, p->color->z);
pos = p->position;
2020-01-08 17:33:01 +01:00
glBegin(GL_POINTS);
2019-12-28 13:54:53 +01:00
glVertex3f(pos->x, pos->y, pos->z);
glEnd();
2020-01-08 17:33:01 +01:00
/*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();*/
2019-12-20 14:24:52 +01:00
}
2019-12-18 18:18:55 +01:00
glfwSwapBuffers(window);
glfwPollEvents();
}
//END
terminateGLFW(window);
2019-12-18 18:18:55 +01:00
2019-12-17 22:29:36 +01:00
free(epos);
2019-12-28 13:54:53 +01:00
freeParticleSystem(ps);
2019-12-18 18:18:55 +01:00
2019-12-16 19:12:24 +01:00
return 0;
}
2019-12-17 22:29:36 +01:00
void calcPos(particle *p, float dt)
{
p->position->x += p->direction->x * dt;
p->position->y += p->direction->y * dt;
p->position->z += p->direction->z * dt;
}
void calcCol(particle *p)
2019-12-18 18:18:55 +01:00
{
2020-01-08 17:33:01 +01:00
p->color->x = 1;
p->color->y = 1;
p->color->z = 1;
2019-12-18 18:18:55 +01:00
}
/*************************************************************************************************************/
2019-12-17 22:29:36 +01:00
void initRandomParticles(emitter *e)
{
for (int i = 0; i < e->pamount; i++)
{
vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z);
2020-01-08 17:33:01 +01:00
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);
2020-01-08 17:33:01 +01:00
(e->particles)[i] = initParticle(pos, dir, color, rand() / 100.0f);
2019-12-17 22:29:36 +01:00
}
}
char *printVector(vector3f *v)
{
char *c = malloc(100);
sprintf(c, "(%f, %f, %f)", v->x, v->y, v->z);
return c;
}
void printParticle(particle *v)
{
printf(" Particle {\n");
printf(" position = %s", printVector(v->position));
printf("\n direction = %s", printVector(v->direction));
printf("\n }");
}
void printEmitter(emitter *e)
{
printf("Emitter {\n");
for (int i = 0; i < e->pamount; i++)
{
printParticle((e->particles)[i]);
printf("\n");
}
printf("\n}");
}