1
0

256 lines
8.8 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);
2020-02-11 19:01:50 +01:00
float *serializeParticles(const particle_system *ps);
2019-12-16 19:12:24 +01:00
int main()
{
2020-02-11 19:01:50 +01:00
/************* INIT *************/
// 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
2020-02-11 19:01:50 +01:00
/************* PARTICLE SYSTEM *************/
int particelAmount = 10000;
vector3f *epos1 = initVector3f(0, 0, 0);
emitter *e1 = initEmitter(epos1, particelAmount);
2019-12-20 14:24:52 +01:00
particle_system *ps = initParticleSystem(1);
2020-02-11 19:01:50 +01:00
(ps->emitters)[0] = e1;
initRandomParticles(e1);
/************* COMPILING SHADER *************/
const char *vertexShaderSource = "#version 460 core\n"
"\n"
"layout (location = 0) in vec3 pos; // the position variable has attribute position 0\n"
"layout (location = 1) in vec3 dir; // the direction variable has attribute position 1\n"
"layout (location = 2) in vec3 col; // the color variable has attribute position 2\n"
"layout (location = 3) in float age; // the age variable has attribute position 3\n"
"\n"
"//in vec3 emitterPos; // the emitter pos variable\n"
"//in float newAge; // the age variable\n"
"\n"
"out vec3 outCol; // output a color to the fragment shader\n"
"\n"
"void main()\n"
"{\n"
" if (age < 0)\n"
" {\n"
" //pos = vec3(0, 0, 0); //emitterPos;\n"
" //age = 200; //newAge;\n"
" }\n"
"\n"
" age -= 0.1f;\n"
" vec3 newPos = pos.xyz + dir.xyz;\n"
" gl_Position = vec4(newPos, 1.0);\n"
"\n"
" outCol = col; // set ourColor to the input color we got from the vertex data\n"
"}";
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
int successCompileVertex;
char infoLogCompileVertex[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &successCompileVertex);
if (!successCompileVertex)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLogCompileVertex);
printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s", infoLogCompileVertex);
}
const char *fragmentShaderSource = "#version 460 core\n"
"\n"
"in vec3 col; // the input variable from the vertex shader (same name and same type)\n"
"\n"
"out vec4 outCol;\n"
"\n"
"void main()\n"
"{\n"
" outCol = vec4(col, 1.0);\n"
"}";
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
int successCompileFragment;
char infoLogCompileFragment[512];
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &successCompileFragment);
if (!successCompileFragment)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLogCompileFragment);
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s", infoLogCompileFragment);
}
2019-12-20 14:24:52 +01:00
2020-02-11 19:01:50 +01:00
/************* LINKING SHADER *************/
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
int successLink;
char infoLogLink[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &successLink);
if (!successLink)
{
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLogLink);
printf("ERROR::SHADER::LINKING_FAILED\n%s", infoLogLink);
}
2019-12-20 14:24:52 +01:00
2020-02-11 19:01:50 +01:00
/*************** VAO / VBO ***************/
// Init vertex data
unsigned int vao, vbo;
float *vertexData = serializeParticlesystem(ps);
initVertexArrayBuffer(&vao);
initVertexBufferObject(&vbo, vertexData);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// direction attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// color attribute
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// age attribute
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 10 * sizeof(float), (void*)(9 * sizeof(float)));
glEnableVertexAttribArray(3);
/************* RENDER LOOP *************/
2019-12-28 13:54:53 +01:00
double time, tFrame, tLast = 0;
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
2020-02-11 19:01:50 +01:00
glUseProgram(shaderProgram);
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, particelAmount);
2019-12-20 14:24:52 +01:00
2020-02-11 19:01:50 +01:00
/*updateParticles((float) tFrame, ps, calcPos, calcCol);
2019-12-18 18:18:55 +01:00
2020-02-11 19:01:50 +01:00
emitter *e;
2019-12-28 13:54:53 +01:00
particle *p;
vector3f *pos;
2020-02-11 19:01:50 +01:00
for (int j = 0; j < ps->eamount; j++)
2019-12-20 14:24:52 +01:00
{
2020-02-11 19:01:50 +01:00
e = (ps->emitters)[j];
for (int i = 0; i < e->pamount; i++)
{
p = (e->particles)[i];
pos = p->position;
glColor3f(p->color->x, p->color->y, p->color->z);
glBegin(GL_POINTS);
glVertex3f(pos->x, pos->y, pos->z);
glEnd();
}
}*/
2019-12-18 18:18:55 +01:00
glfwSwapBuffers(window);
glfwPollEvents();
}
//END
2020-02-11 19:01:50 +01:00
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
terminateGLFW(window);
2019-12-18 18:18:55 +01:00
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-02-11 19:01:50 +01:00
// p->color->x -= 0.01f;
// p->color->y -= 0.01f;
// p->color->z -= 0.01f;
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);
2020-02-11 19:01:50 +01:00
vector3f *color = initVector3f(((float) (rand() % 255)) / 255,
((float) (rand() % 255)) / 255,
((float) (rand() % 255)) / 255);
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}");
}