aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Kummerlaender2018-05-21 14:35:43 +0200
committerAdrian Kummerlaender2018-05-21 14:35:43 +0200
commita8feed9d7951c9a947562ed687c704851a31ea9b (patch)
tree77fc9debb4e72ec9f72ba2cb992b164ae4e63b08 /src
parentffa662ec5acdaae36f0ffeaf0cee78a4200d897b (diff)
downloadcomputicle-a8feed9d7951c9a947562ed687c704851a31ea9b.tar
computicle-a8feed9d7951c9a947562ed687c704851a31ea9b.tar.gz
computicle-a8feed9d7951c9a947562ed687c704851a31ea9b.tar.bz2
computicle-a8feed9d7951c9a947562ed687c704851a31ea9b.tar.lz
computicle-a8feed9d7951c9a947562ed687c704851a31ea9b.tar.xz
computicle-a8feed9d7951c9a947562ed687c704851a31ea9b.tar.zst
computicle-a8feed9d7951c9a947562ed687c704851a31ea9b.zip
Fix VAO, VBO setup, compute shader coupling
Diffstat (limited to 'src')
-rw-r--r--src/compute_shader.h6
-rw-r--r--src/graphic_shader.h2
-rw-r--r--src/main.cc113
-rw-r--r--src/particle_vertex_buffer.h44
-rw-r--r--src/shader/compute.glsl6
-rw-r--r--src/texture_buffer.h2
-rw-r--r--src/util.h2
7 files changed, 102 insertions, 73 deletions
diff --git a/src/compute_shader.h b/src/compute_shader.h
index 298a454..a7e997a 100644
--- a/src/compute_shader.h
+++ b/src/compute_shader.h
@@ -1,3 +1,5 @@
+#pragma once
+
#include "util.h"
class ComputeShader {
@@ -35,6 +37,10 @@ public:
return id;
}
+ void workOn(GLuint buffer) {
+ glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer);
+ }
+
void dispatch(std::size_t dimX) {
glDispatchCompute(dimX, 1, 1);
}
diff --git a/src/graphic_shader.h b/src/graphic_shader.h
index 2dd8c41..0422ec9 100644
--- a/src/graphic_shader.h
+++ b/src/graphic_shader.h
@@ -1,3 +1,5 @@
+#pragma once
+
#include "util.h"
class GraphicShader {
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;
diff --git a/src/particle_vertex_buffer.h b/src/particle_vertex_buffer.h
new file mode 100644
index 0000000..58daf38
--- /dev/null
+++ b/src/particle_vertex_buffer.h
@@ -0,0 +1,44 @@
+#pragma once
+
+#include <vector>
+
+class ParticleVertexBuffer {
+private:
+ std::vector<GLfloat> _data;
+
+ GLuint _array;
+ GLuint _buffer;
+
+public:
+ ParticleVertexBuffer(std::vector<GLfloat>&& data):
+ _data{ std::move(data) } {
+ glGenVertexArrays(1, &_array);
+ glGenBuffers(1, &_buffer);
+
+ glBindVertexArray(_array);
+ glBindBuffer(GL_ARRAY_BUFFER, _buffer);
+ glBufferData(
+ GL_ARRAY_BUFFER,
+ _data.size() * sizeof(GLfloat),
+ _data.data(),
+ GL_STATIC_DRAW
+ );
+
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
+ }
+
+ ~ParticleVertexBuffer() {
+ glDeleteBuffers(1, &_buffer);
+ glDeleteVertexArrays(1, &_buffer);
+ }
+
+ void draw() {
+ glBindVertexArray(_array);
+ glDrawArrays(GL_POINTS, 0, 3*_data.size());
+ }
+
+ GLuint getBuffer() const {
+ return _buffer;
+ }
+};
diff --git a/src/shader/compute.glsl b/src/shader/compute.glsl
index 58bf08b..e322a08 100644
--- a/src/shader/compute.glsl
+++ b/src/shader/compute.glsl
@@ -12,9 +12,9 @@ float rand(vec2 co){
bool insideWorld(vec2 v) {
return v.x > -world.x/2.
- && v.x < world.x/2.
- && v.y > -world.y/2.
- && v.y < world.y/2.;
+ && v.x < world.x/2.
+ && v.y > -world.y/2.
+ && v.y < world.y/2.;
}
void main() {
diff --git a/src/texture_buffer.h b/src/texture_buffer.h
index 851833c..d6cce53 100644
--- a/src/texture_buffer.h
+++ b/src/texture_buffer.h
@@ -1,3 +1,5 @@
+#pragma once
+
class TextureBuffer {
private:
GLuint _id;
diff --git a/src/util.h b/src/util.h
index ce8d012..7aec674 100644
--- a/src/util.h
+++ b/src/util.h
@@ -1,5 +1,7 @@
#pragma once
+#include <iostream>
+
namespace util {
GLint getUniform(GLuint program, const std::string& name) {