aboutsummaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc113
1 files changed, 43 insertions, 70 deletions
diff --git a/src/main.cc b/src/main.cc
index c6945fb..7b4415b 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -4,8 +4,6 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
-#include <vector>
-#include <iostream>
#include <random>
#include <chrono>
#include <memory>
@@ -13,6 +11,7 @@
#include "graphic_shader.h"
#include "compute_shader.h"
#include "texture_buffer.h"
+#include "particle_vertex_buffer.h"
#include "shader/vertex.glsl"
#include "shader/fragment.glsl"
@@ -24,7 +23,9 @@ int window_width = 800;
int window_height = 600;
float world_width, world_height;
glm::mat4 MVP;
-std::unique_ptr<TextureBuffer> textureBuffer;
+
+std::unique_ptr<TextureBuffer> textureBuffer;
+std::unique_ptr<ParticleVertexBuffer> particleBuffer;
void updateMVP() {
world_width = 20.f;
@@ -101,48 +102,38 @@ int main() {
updateMVP();
- textureBuffer = std::make_unique<TextureBuffer>(window_width, window_height);
-
- {
- auto guard = textureBuffer->use();
-
- glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
- }
-
GraphicShader sceneShader(VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE);
- sceneShader.setUniform("MVP", MVP);
-
- ComputeShader computeShader(COMPUTE_SHADER_CODE);
-
- auto vertex_buffer_data = makeInitialParticles(particle_count);
+ GraphicShader displayShader(R"(
+ #version 330 core
+ layout (location = 0) in vec2 screen_vertex;
+ layout (location = 1) in vec2 texture_vertex;
+ out vec2 TexCoords;
- GLuint VertexArrayID;
- GLuint VertexBufferID;
+ void main() {
+ gl_Position = vec4(screen_vertex, 0.0, 1.0);
+ TexCoords = texture_vertex;
+ }
+ )", R"(
+ #version 330 core
+ out vec4 FragColor;
+ in vec2 TexCoords;
+ uniform sampler2D screen_texture;
- {
- auto guard = textureBuffer->use();
+ void main() {
+ FragColor = texture(screen_texture, TexCoords);
+ }
+ )");
- glGenVertexArrays(1, &VertexArrayID);
- glBindVertexArray(VertexArrayID);
- glEnableVertexAttribArray(0);
+ sceneShader.setUniform("MVP", MVP);
+ displayShader.setUniform("screen_texture", 0);
- glGenBuffers(1, &VertexBufferID);
- glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, VertexBufferID);
+ textureBuffer = std::make_unique<TextureBuffer>(window_width, window_height);
+ particleBuffer = std::make_unique<ParticleVertexBuffer>(
+ makeInitialParticles(particle_count));
- glBufferData(
- GL_SHADER_STORAGE_BUFFER,
- vertex_buffer_data.size() * sizeof(GLfloat),
- vertex_buffer_data.data(),
- GL_STATIC_DRAW
- );
- }
+ ComputeShader computeShader(COMPUTE_SHADER_CODE);
+ computeShader.workOn(particleBuffer->getBuffer());
- GLuint QuadVertexArrayID;
- glGenVertexArrays(1, &QuadVertexArrayID);
- glBindVertexArray(QuadVertexArrayID);
- GLuint QuadVertexBufferID;
- glGenBuffers(1, &QuadVertexBufferID);
- glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBufferID);
const std::vector<GLfloat> quad_buffer_data{
-1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 0.f, 0.f,
@@ -152,36 +143,26 @@ int main() {
1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f
};
+
+ GLuint QuadVertexArrayID;
+ GLuint QuadVertexBufferID;
+
+ glGenVertexArrays(1, &QuadVertexArrayID);
+ glGenBuffers(1, &QuadVertexBufferID);
+
+ glBindVertexArray(QuadVertexArrayID);
+ glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBufferID);
glBufferData(
GL_ARRAY_BUFFER,
quad_buffer_data.size() * sizeof(GLfloat),
quad_buffer_data.data(),
GL_STATIC_DRAW
);
+
glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(1);
-
- GraphicShader displayShader(R"(
- #version 330 core
- layout (location = 0) in vec2 screen_vertex;
- layout (location = 1) in vec2 texture_vertex;
- out vec2 TexCoords;
-
- void main() {
- gl_Position = vec4(screen_vertex, 0.0, 1.0);
- TexCoords = texture_vertex;
- }
- )", R"(
- #version 330 core
- out vec4 FragColor;
- in vec2 TexCoords;
- uniform sampler2D screen_texture;
-
- void main() {
- FragColor = texture(screen_texture, TexCoords);
- }
- )");
- displayShader.setUniform("screen_texture", 0);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
auto lastFrame = std::chrono::high_resolution_clock::now();
@@ -204,18 +185,14 @@ int main() {
sceneShader.setUniform("MVP", MVP);
- glBindBuffer(GL_ARRAY_BUFFER, VertexBufferID);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
- glDrawArrays(GL_POINTS, 0, 3*particle_count);
+ particleBuffer->draw();
}
{
auto guard = displayShader.use();
+ glBindVertexArray(QuadVertexArrayID);
glBindTexture(GL_TEXTURE_2D, textureBuffer->getTexture());
- glBindBuffer(GL_ARRAY_BUFFER, QuadVertexBufferID);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 6);
@@ -227,10 +204,6 @@ int main() {
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
- glDeleteBuffers(1, &VertexBufferID);
- glDisableVertexAttribArray(0);
- glDeleteVertexArrays(1, &VertexArrayID);
-
glfwTerminate();
return 0;