1
0

116 lines
4.2 KiB
C
Raw Normal View History

2020-03-20 16:33:43 +01:00
#include <malloc.h>
#include "particlesystem.h"
#include "initOpenGL.h"
2020-03-18 12:29:08 +01:00
#include "utils.h"
2020-03-30 18:39:57 +02:00
#define PARTICLE_AMOUNT 1000000
int main()
{
/************* INIT *************/
// Init OpenGL and GLFW
initGLFW();
setErrorCallbackGL();
int width = WIDTH, height = HEIGHT;
2020-03-30 18:39:57 +02:00
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow *window = createGLFWWindow(WIDTH, HEIGHT, "Informatikprojekt - OpenGL");
setCurrentContextGL(window);
setFramebufferSizeCallbackGL(window);
// glad
initGlad();
/************* PARTICLE SYSTEM *************/
vector3f *epos1 = initVector3f(0, 0, 0);
emitter *e1 = initEmitter(epos1, PARTICLE_AMOUNT);
particle_system *ps = initParticleSystem(1);
(ps->emitters)[0] = e1;
initRandomParticles(e1);
/************* SHADER *************/
2020-03-18 16:55:27 +01:00
const GLchar *computeShaderSource = readFile("./opengl/ComputeShader.glsl", "r", NULL);
const GLchar *vertexShaderSource = readFile("./opengl/VertexShader.glsl", "r", NULL);
const GLchar *fragmentShaderSource = readFile("./opengl/FragmentShader.glsl", "r", NULL);
GLuint computeShader = compileShader(computeShaderSource, GL_COMPUTE_SHADER);
GLuint vertexShader = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
GLuint fragmentShader = compileShader(fragmentShaderSource, GL_FRAGMENT_SHADER);
GLuint computeShaders[] = { computeShader };
GLuint computeShaderProgram = linkShaders(computeShaders, 1);
glUseProgram(computeShaderProgram);
GLint dtUniformLocation = glGetUniformLocation(computeShaderProgram, "dt");
GLint resetPosUniformLocation = glGetUniformLocation(computeShaderProgram, "resetPos");
glUniform3f(resetPosUniformLocation, e1->position->x, e1->position->y, e1->position->z);
GLint maxParticlesUniformLocation = glGetUniformLocation(computeShaderProgram, "maxParticles");
glUniform1ui(maxParticlesUniformLocation, PARTICLE_AMOUNT);
GLuint renderShaders[] = { vertexShader, fragmentShader };
GLuint renderShaderProgram = linkShaders(renderShaders, 2);
float *particles = serializeParticlesystem(ps);
2020-03-20 16:33:43 +01:00
freeParticleSystem(ps);
GLsizeiptr sizeOfParticle = 3 * sizeof(vector3f) + sizeof(float);
GLuint particleBuffer;
glGenBuffers(1, &particleBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, particleBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, PARTICLE_AMOUNT * sizeOfParticle, particles, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, particleBuffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
2020-03-20 16:33:43 +01:00
free(particles);
GLuint vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, particleBuffer);
glBindBuffer(GL_ARRAY_BUFFER, particleBuffer);
// position
glEnableVertexAttribArray(0);
2020-04-01 14:40:21 +02:00
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (GLsizei) sizeOfParticle, (GLvoid *)0);
// color
glEnableVertexAttribArray(1);
2020-04-01 14:40:21 +02:00
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, (GLsizei) sizeOfParticle, (GLvoid *)24);
glBindVertexArray(0);
/************* RENDER LOOP *************/
double time, tFrame, tLast = 0;
while (!glfwWindowShouldClose(window))
{
time = glfwGetTime();
tFrame = time - tLast;
tLast = time;
/*** RENDER ***/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glfwGetFramebufferSize(window, &width, &height);
glBindVertexArray(vertexArray);
glUseProgram(renderShaderProgram);
glDrawArrays(GL_POINTS, 0, PARTICLE_AMOUNT);
glBindVertexArray(0);
2020-03-31 18:38:58 +02:00
/*** UPDATE ***/
glUseProgram(computeShaderProgram);
2020-04-01 14:40:21 +02:00
glUniform1f(dtUniformLocation, (GLfloat) tFrame);
2020-03-31 18:38:58 +02:00
glDispatchCompute(PARTICLE_AMOUNT / WORKGROUP_SIZE_X, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
2020-04-01 22:17:35 +02:00
//END
deleteShaders(renderShaders, 2);
2020-03-20 16:33:43 +01:00
glDeleteProgram(renderShaderProgram);
deleteShaders(computeShaders, 1);
2020-03-20 16:33:43 +01:00
glDeleteProgram(computeShaderProgram);
glDeleteBuffers(1, &particleBuffer);
glDeleteVertexArrays(1, &vertexArray);
terminateGLFW(window);
return 0;
}