aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2018-05-26 13:20:47 +0200
committerAdrian Kummerlaender2018-05-26 13:21:40 +0200
commit34052b51e00c939a35294d7085cadb5111484dd3 (patch)
tree03a5b1e350e47e2aa8551393ef67b2124ad50ddb
parentf728e4c8d202de241673a13ce61570b6acb4bba7 (diff)
downloadcomputicle-34052b51e00c939a35294d7085cadb5111484dd3.tar
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.gz
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.bz2
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.lz
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.xz
computicle-34052b51e00c939a35294d7085cadb5111484dd3.tar.zst
computicle-34052b51e00c939a35294d7085cadb5111484dd3.zip
Separate headers into compilation units
-rw-r--r--CMakeLists.txt13
-rw-r--r--shell.nix2
-rw-r--r--src/buffer/frame/texture_framebuffer.cc51
-rw-r--r--src/buffer/frame/texture_framebuffer.h54
-rw-r--r--src/buffer/vertex/particle_vertex_buffer.cc33
-rw-r--r--src/buffer/vertex/particle_vertex_buffer.h35
-rw-r--r--src/buffer/vertex/texture_display_vertex_buffer.cc46
-rw-r--r--src/buffer/vertex/texture_display_vertex_buffer.h48
-rw-r--r--src/glfw/guard.cc16
-rw-r--r--src/glfw/guard.h15
-rw-r--r--src/glfw/window.cc24
-rw-r--r--src/glfw/window.h63
-rw-r--r--src/main.cc23
-rw-r--r--src/shader/util.cc47
-rw-r--r--src/shader/util.h43
-rw-r--r--src/shader/wrap/compute_shader.cc40
-rw-r--r--src/shader/wrap/compute_shader.h45
-rw-r--r--src/shader/wrap/graphic_shader.cc45
-rw-r--r--src/shader/wrap/graphic_shader.h51
-rw-r--r--src/timer.cc16
-rw-r--r--src/timer.h12
-rw-r--r--src/util.h13
22 files changed, 441 insertions, 294 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6415420..9268050 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
-project(shader)
+project(computicle)
set(
CMAKE_CXX_FLAGS
@@ -13,11 +13,20 @@ include_directories(
add_executable(
computicle
src/main.cc
+ src/timer.cc
+ src/glfw/guard.cc
+ src/glfw/window.cc
+ src/shader/util.cc
+ src/shader/wrap/compute_shader.cc
+ src/shader/wrap/graphic_shader.cc
+ src/buffer/frame/texture_framebuffer.cc
+ src/buffer/vertex/particle_vertex_buffer.cc
+ src/buffer/vertex/texture_display_vertex_buffer.cc
)
target_link_libraries(
computicle
GL
- GLEW
glfw
+ GLEW
)
diff --git a/shell.nix b/shell.nix
index 2a16377..c57c8a5 100644
--- a/shell.nix
+++ b/shell.nix
@@ -5,8 +5,8 @@ stdenv.mkDerivation rec {
env = buildEnv { name = name; paths = buildInputs; };
buildInputs = [
git cmake gcc gdb cgdb
- glew
glfw3
+ glew
glm
];
}
diff --git a/src/buffer/frame/texture_framebuffer.cc b/src/buffer/frame/texture_framebuffer.cc
new file mode 100644
index 0000000..b585985
--- /dev/null
+++ b/src/buffer/frame/texture_framebuffer.cc
@@ -0,0 +1,51 @@
+#include "texture_framebuffer.h"
+
+TextureFramebuffer::Guard::Guard(GLuint id):
+ _id(id) {
+ glBindFramebuffer(GL_FRAMEBUFFER, _id);
+}
+
+TextureFramebuffer::Guard::~Guard() {
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+}
+
+TextureFramebuffer::Guard TextureFramebuffer::use() const {
+ return Guard(_id);
+}
+
+TextureFramebuffer::TextureFramebuffer(std::size_t width, std::size_t height) {
+ glGenFramebuffers(1, &_id);
+
+ auto guard = use();
+
+ glGenTextures(1, &_texture);
+ glBindTexture(GL_TEXTURE_2D, _texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);
+
+ if ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE ) {
+ _good = true;
+ }
+}
+
+TextureFramebuffer::~TextureFramebuffer() {
+ glDeleteFramebuffers(1, &_id);
+}
+
+bool TextureFramebuffer::isGood() const {
+ return _good;
+}
+
+void TextureFramebuffer::resize(std::size_t width, std::size_t height) const {
+ auto guard = use();
+
+ glViewport(0, 0, width, height);
+ glBindTexture(GL_TEXTURE_2D, _texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0);
+}
+
+GLuint TextureFramebuffer::getTexture() const {
+ return _texture;
+}
diff --git a/src/buffer/frame/texture_framebuffer.h b/src/buffer/frame/texture_framebuffer.h
index 31153ff..f6b20e6 100644
--- a/src/buffer/frame/texture_framebuffer.h
+++ b/src/buffer/frame/texture_framebuffer.h
@@ -1,5 +1,9 @@
#pragma once
+#include <cstdint>
+
+#include <GL/glew.h>
+
class TextureFramebuffer {
private:
GLuint _id;
@@ -11,52 +15,18 @@ public:
struct Guard {
const GLuint _id;
- Guard(GLuint id): _id(id) {
- glBindFramebuffer(GL_FRAMEBUFFER, _id);
- }
- ~Guard() {
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- }
+ Guard(GLuint id);
+ ~Guard();
};
- Guard use() const {
- return Guard(_id);
- }
-
- TextureFramebuffer(std::size_t width, std::size_t height) {
- glGenFramebuffers(1, &_id);
-
- auto guard = use();
-
- glGenTextures(1, &_texture);
- glBindTexture(GL_TEXTURE_2D, _texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);
-
- if ( glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE ) {
- _good = true;
- }
- }
-
- ~TextureFramebuffer() {
- glDeleteFramebuffers(1, &_id);
- }
+ Guard use() const;
- bool isGood() const {
- return _good;
- }
+ TextureFramebuffer(std::size_t width, std::size_t height);
+ ~TextureFramebuffer();
- void resize(std::size_t width, std::size_t height) const {
- auto guard = use();
+ bool isGood() const;
- glViewport(0, 0, width, height);
- glBindTexture(GL_TEXTURE_2D, _texture);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (void*)0);
- }
+ void resize(std::size_t width, std::size_t height) const;
- GLuint getTexture() const {
- return _texture;
- }
+ GLuint getTexture() const;
};
diff --git a/src/buffer/vertex/particle_vertex_buffer.cc b/src/buffer/vertex/particle_vertex_buffer.cc
new file mode 100644
index 0000000..fc61cc5
--- /dev/null
+++ b/src/buffer/vertex/particle_vertex_buffer.cc
@@ -0,0 +1,33 @@
+#include "particle_vertex_buffer.h"
+
+ParticleVertexBuffer::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::~ParticleVertexBuffer() {
+ glDeleteBuffers(1, &_buffer);
+ glDeleteVertexArrays(1, &_array);
+}
+
+GLuint ParticleVertexBuffer::getBuffer() const {
+ return _buffer;
+}
+
+void ParticleVertexBuffer::draw() const {
+ glBindVertexArray(_array);
+ glDrawArrays(GL_POINTS, 0, 3*_data.size());
+}
diff --git a/src/buffer/vertex/particle_vertex_buffer.h b/src/buffer/vertex/particle_vertex_buffer.h
index 25855a2..8fb96a4 100644
--- a/src/buffer/vertex/particle_vertex_buffer.h
+++ b/src/buffer/vertex/particle_vertex_buffer.h
@@ -2,6 +2,8 @@
#include <vector>
+#include <GL/glew.h>
+
class ParticleVertexBuffer {
private:
std::vector<GLfloat> _data;
@@ -10,35 +12,10 @@ private:
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, &_array);
- }
+ ParticleVertexBuffer(std::vector<GLfloat>&& data);
+ ~ParticleVertexBuffer();
- void draw() const {
- glBindVertexArray(_array);
- glDrawArrays(GL_POINTS, 0, 3*_data.size());
- }
+ GLuint getBuffer() const;
- GLuint getBuffer() const {
- return _buffer;
- }
+ void draw() const;
};
diff --git a/src/buffer/vertex/texture_display_vertex_buffer.cc b/src/buffer/vertex/texture_display_vertex_buffer.cc
new file mode 100644
index 0000000..005ea76
--- /dev/null
+++ b/src/buffer/vertex/texture_display_vertex_buffer.cc
@@ -0,0 +1,46 @@
+#include "texture_display_vertex_buffer.h"
+
+TextureDisplayVertexBuffer::TextureDisplayVertexBuffer():
+ _data{
+ -1.f, 1.f, 0.f, 1.f,
+ -1.f, -1.f, 0.f, 0.f,
+ 1.f, -1.f, 1.f, 0.f,
+
+ -1.f, 1.f, 0.f, 1.f,
+ 1.f, -1.f, 1.f, 0.f,
+ 1.f, 1.f, 1.f, 1.f
+ } {
+ 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, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0);
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(
+ 1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
+}
+
+TextureDisplayVertexBuffer::~TextureDisplayVertexBuffer() {
+ glDeleteBuffers(1, &_buffer);
+ glDeleteVertexArrays(1, &_array);
+}
+
+GLuint TextureDisplayVertexBuffer::getBuffer() const {
+ return _buffer;
+}
+
+void TextureDisplayVertexBuffer::draw(const std::vector<GLuint>& textures) const {
+ glBindVertexArray(_array);
+ glBindTextures(textures[0], textures.size(), textures.data());
+ glDrawArrays(GL_TRIANGLES, 0, 6);
+}
diff --git a/src/buffer/vertex/texture_display_vertex_buffer.h b/src/buffer/vertex/texture_display_vertex_buffer.h
index 6d4eec2..6febb1e 100644
--- a/src/buffer/vertex/texture_display_vertex_buffer.h
+++ b/src/buffer/vertex/texture_display_vertex_buffer.h
@@ -2,6 +2,8 @@
#include <vector>
+#include <GL/glew.h>
+
class TextureDisplayVertexBuffer {
private:
const std::vector<GLfloat> _data;
@@ -10,48 +12,10 @@ private:
GLuint _buffer;
public:
- TextureDisplayVertexBuffer():
- _data{
- -1.f, 1.f, 0.f, 1.f,
- -1.f, -1.f, 0.f, 0.f,
- 1.f, -1.f, 1.f, 0.f,
-
- -1.f, 1.f, 0.f, 1.f,
- 1.f, -1.f, 1.f, 0.f,
- 1.f, 1.f, 1.f, 1.f
- } {
- 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, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(
- 1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (void*)(2*sizeof(GLfloat)));
- }
-
- ~TextureDisplayVertexBuffer() {
- glDeleteBuffers(1, &_buffer);
- glDeleteVertexArrays(1, &_array);
- }
+ TextureDisplayVertexBuffer();
+ ~TextureDisplayVertexBuffer();
- void draw(const std::vector<GLuint>& textures) const {
- glBindVertexArray(_array);
- glBindTextures(textures[0], textures.size(), textures.data());
- glDrawArrays(GL_TRIANGLES, 0, 6);
- }
+ GLuint getBuffer() const;
- GLuint getBuffer() const {
- return _buffer;
- }
+ void draw(const std::vector<GLuint>& textures) const;
};
diff --git a/src/glfw/guard.cc b/src/glfw/guard.cc
new file mode 100644
index 0000000..5ba853f
--- /dev/null
+++ b/src/glfw/guard.cc
@@ -0,0 +1,16 @@
+#include "guard.h"
+
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+GlfwGuard::GlfwGuard() {
+ _good = glfwInit();
+}
+
+GlfwGuard::~GlfwGuard() {
+ glfwTerminate();
+}
+
+bool GlfwGuard::isGood() const {
+ return _good;
+}
diff --git a/src/glfw/guard.h b/src/glfw/guard.h
index 37d3ffb..f68954d 100644
--- a/src/glfw/guard.h
+++ b/src/glfw/guard.h
@@ -1,20 +1,11 @@
#pragma once
-#include <GL/glew.h>
-#include <GLFW/glfw3.h>
-
class GlfwGuard {
private:
bool _good = false;
public:
- GlfwGuard() {
- _good = glfwInit();
- }
- ~GlfwGuard() {
- glfwTerminate();
- }
+ GlfwGuard();
+ ~GlfwGuard();
- bool isGood() const {
- return _good;
- }
+ bool isGood() const;
};
diff --git a/src/glfw/window.cc b/src/glfw/window.cc
new file mode 100644
index 0000000..17fc36c
--- /dev/null
+++ b/src/glfw/window.cc
@@ -0,0 +1,24 @@
+#include "window.h"
+
+Window::Window(const std::string& title):
+ _handle(glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL)) {
+ if ( _handle != nullptr ) {
+ glfwMakeContextCurrent(_handle);
+ if ( glewInit() == GLEW_OK ) {
+ _good = true;
+ }
+ glfwMakeContextCurrent(nullptr);
+ }
+}
+
+bool Window::isGood() const {
+ return _good;
+}
+
+int Window::getWidth() const {
+ return _width;
+}
+
+int Window::getHeight() const {
+ return _height;
+}
diff --git a/src/glfw/window.h b/src/glfw/window.h
index 794dd5f..b858e9a 100644
--- a/src/glfw/window.h
+++ b/src/glfw/window.h
@@ -1,5 +1,7 @@
#pragma once
+#include <string>
+
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@@ -12,51 +14,40 @@ private:
GLFWwindow* const _handle;
public:
- Window(const std::string& title):
- _handle(glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL)) {
- if ( _handle != nullptr ) {
- glfwMakeContextCurrent(_handle);
- if ( glewInit() == GLEW_OK ) {
- _good = true;
- }
- glfwMakeContextCurrent(nullptr);
- }
- }
+ Window(const std::string& title);
- bool isGood() const {
- return _good;
- }
-
- int getWidth() const {
- return _width;
- }
+ bool isGood() const;
- int getHeight() const {
- return _height;
- }
+ int getWidth() const;
+ int getHeight() const;
template <class F>
- void init(F f) {
- glfwMakeContextCurrent(_handle);
- f();
- glfwMakeContextCurrent(nullptr);
- }
+ void init(F f);
template <class F>
- void render(F loop) {
- glfwMakeContextCurrent(_handle);
+ void render(F loop);
+};
- while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
- glfwWindowShouldClose(_handle) == 0 ) {
- glfwGetWindowSize(_handle, &_width, &_height);
+template <class F>
+void Window::init(F f) {
+ glfwMakeContextCurrent(_handle);
+ f();
+ glfwMakeContextCurrent(nullptr);
+}
- loop();
+template <class F>
+void Window::render(F loop) {
+ glfwMakeContextCurrent(_handle);
- glfwSwapBuffers(_handle);
- glfwPollEvents();
- }
+ while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
+ glfwWindowShouldClose(_handle) == 0 ) {
+ glfwGetWindowSize(_handle, &_width, &_height);
- glfwMakeContextCurrent(nullptr);
+ loop();
+
+ glfwSwapBuffers(_handle);
+ glfwPollEvents();
}
-};
+ glfwMakeContextCurrent(nullptr);
+}
diff --git a/src/main.cc b/src/main.cc
index 9f32bc3..cd8668e 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -1,9 +1,10 @@
-#include <glm/glm.hpp>
-#include <glm/gtc/matrix_transform.hpp>
-
#include <random>
#include <memory>
#include <algorithm>
+#include <iostream>
+
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
#include "glfw/guard.h"
#include "glfw/window.h"
@@ -23,7 +24,7 @@
#include "shader/code/display_vertex.glsl"
#include "shader/code/display_fragment.glsl"
-#include "util.h"
+#include "timer.h"
const unsigned int particle_count = 2500;
const unsigned int max_ups = 100;
@@ -86,7 +87,7 @@ int main() {
Window window("computicle");
if ( !window.isGood() ) {
- std::cerr << "Failed to open window." << std::endl;
+ std::cerr << "Failed to open GLFW window." << std::endl;
return -1;
}
@@ -135,8 +136,8 @@ int main() {
return -1;
}
- auto lastFrame = std::chrono::high_resolution_clock::now();
- auto lastRotate = std::chrono::high_resolution_clock::now();
+ auto lastFrame = timer::now();
+ auto lastRotate = timer::now();
bool justRotated = true;
std::vector<GLuint> textures;
@@ -158,21 +159,21 @@ int main() {
}
}
- if ( util::millisecondsSince(lastFrame) >= 1000/max_ups ) {
+ if ( timer::millisecondsSince(lastFrame) >= 1000/max_ups ) {
auto guard = compute_shader->use();
compute_shader->setUniform("world", world_width, world_height);
compute_shader->dispatch(particle_count);
- lastFrame = std::chrono::high_resolution_clock::now();
+ lastFrame = timer::now();
}
- if ( util::millisecondsSince(lastRotate) >= 1000/10 ) {
+ if ( timer::millisecondsSince(lastRotate) >= 1000/10 ) {
std::rotate(textures.begin(), textures.end()-1, textures.end());
std::rotate(texture_framebuffers.begin(), texture_framebuffers.end()-1, texture_framebuffers.end());
justRotated = true;
- lastRotate = std::chrono::high_resolution_clock::now();
+ lastRotate = timer::now();
}
{
diff --git a/src/shader/util.cc b/src/shader/util.cc
new file mode 100644
index 0000000..07a1128
--- /dev/null
+++ b/src/shader/util.cc
@@ -0,0 +1,47 @@
+#include "util.h"
+
+#include <iostream>
+#include <vector>
+
+namespace util {
+
+GLint getUniform(GLuint program, const std::string& name) {
+ const GLint uniform = glGetUniformLocation(program, name.c_str());
+ if ( uniform == -1 ) {
+ std::cerr << "Could not bind uniform " << name << std::endl;
+ }
+ return uniform;
+}
+
+GLint compileShader(const std::string& source, GLenum type) {
+ GLint shader = glCreateShader(type);
+
+ if ( !shader ) {
+ std::cerr << "Cannot create a shader of type " << type << std::endl;
+ exit(-1);
+ }
+
+ const char* source_data = source.c_str();
+ const int source_length = source.size();
+
+ glShaderSource(shader, 1, &source_data, &source_length);
+ glCompileShader(shader);
+
+ GLint compiled;
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
+ if ( !compiled ) {
+ std::cerr << "Cannot compile shader" << std::endl;
+ GLint maxLength = 0;
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
+ std::vector<GLchar> errorLog(maxLength);
+ glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
+ for( auto c : errorLog ) {
+ std::cerr << c;
+ }
+ std::cerr << std::endl;
+ }
+
+ return shader;
+}
+
+}
diff --git a/src/shader/util.h b/src/shader/util.h
index 7aec674..31224e9 100644
--- a/src/shader/util.h
+++ b/src/shader/util.h
@@ -1,46 +1,13 @@
#pragma once
-#include <iostream>
+#include <string>
-namespace util {
-
-GLint getUniform(GLuint program, const std::string& name) {
- const GLint uniform = glGetUniformLocation(program, name.c_str());
- if ( uniform == -1 ) {
- std::cerr << "Could not bind uniform " << name << std::endl;
- }
- return uniform;
-}
-
-GLint compileShader(const std::string& source, GLenum type) {
- GLint shader = glCreateShader(type);
+#include <GL/glew.h>
- if ( !shader ) {
- std::cerr << "Cannot create a shader of type " << type << std::endl;
- exit(-1);
- }
-
- const char* source_data = source.c_str();
- const int source_length = source.size();
-
- glShaderSource(shader, 1, &source_data, &source_length);
- glCompileShader(shader);
+namespace util {
- GLint compiled;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
- if ( !compiled ) {
- std::cerr << "Cannot compile shader" << std::endl;
- GLint maxLength = 0;
- glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<GLchar> errorLog(maxLength);
- glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
- for( auto c : errorLog ) {
- std::cerr << c;
- }
- std::cerr << std::endl;
- }
+GLint getUniform(GLuint program, const std::string& name);
- return shader;
-}
+GLint compileShader(const std::string& source, GLenum type);
}
diff --git a/src/shader/wrap/compute_shader.cc b/src/shader/wrap/compute_shader.cc
new file mode 100644
index 0000000..97ce52a
--- /dev/null
+++ b/src/shader/wrap/compute_shader.cc
@@ -0,0 +1,40 @@
+#include "compute_shader.h"
+
+#include "shader/util.h"
+
+ComputeShader::Guard::Guard(GLuint id):
+ _id(id) {
+ glUseProgram(_id);
+}
+
+ComputeShader::Guard::~Guard() {
+ glUseProgram(0);
+}
+
+ComputeShader::Guard ComputeShader::use() const {
+ return Guard(_id);
+}
+
+ComputeShader::ComputeShader(const std::string& src):
+ _id(glCreateProgram()) {
+ glAttachShader(_id, util::compileShader(src, GL_COMPUTE_SHADER));
+ glLinkProgram(_id);
+}
+
+ComputeShader::~ComputeShader() {
+ glDeleteProgram(_id);
+}
+
+GLuint ComputeShader::setUniform(const std::string& name, float x, float y) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniform2f(id, x, y);
+ return id;
+}
+
+void ComputeShader::workOn(GLuint buffer) const {
+ glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer);
+}
+
+void ComputeShader::dispatch(std::size_t dimX) const {
+ glDispatchCompute(dimX, 1, 1);
+}
diff --git a/src/shader/wrap/compute_shader.h b/src/shader/wrap/compute_shader.h
index 9f5c5cb..e9e88ca 100644
--- a/src/shader/wrap/compute_shader.h
+++ b/src/shader/wrap/compute_shader.h
@@ -1,6 +1,8 @@
#pragma once
-#include "util.h"
+#include <string>
+
+#include <GL/glew.h>
class ComputeShader {
private:
@@ -10,38 +12,17 @@ public:
struct Guard {
const GLuint _id;
- Guard(GLuint id): _id(id) {
- glUseProgram(_id);
- }
- ~Guard() {
- glUseProgram(0);
- }
+ Guard(GLuint id);
+ ~Guard();
};
- Guard use() const {
- return Guard(_id);
- }
+ Guard use() const;
- ComputeShader(const std::string& src):
- _id(glCreateProgram()) {
- glAttachShader(_id, util::compileShader(src, GL_COMPUTE_SHADER));
- glLinkProgram(_id);
- };
- ~ComputeShader() {
- glDeleteProgram(_id);
- }
-
- GLuint setUniform(const std::string& name, float x, float y) const {
- GLuint id = util::getUniform(_id, name);
- glUniform2f(id, x, y);
- return id;
- }
-
- void workOn(GLuint buffer) const {
- glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer);
- }
-
- void dispatch(std::size_t dimX) const {
- glDispatchCompute(dimX, 1, 1);
- }
+ ComputeShader(const std::string& src);
+ ~ComputeShader();
+
+ GLuint setUniform(const std::string& name, float x, float y) const;
+
+ void workOn(GLuint buffer) const;
+ void dispatch(std::size_t dimX) const;
};
diff --git a/src/shader/wrap/graphic_shader.cc b/src/shader/wrap/graphic_shader.cc
new file mode 100644
index 0000000..0ed37ff
--- /dev/null
+++ b/src/shader/wrap/graphic_shader.cc
@@ -0,0 +1,45 @@
+#include "graphic_shader.h"
+
+#include "shader/util.h"
+
+GraphicShader::Guard::Guard(GLuint id):
+ _id(id) {
+ glUseProgram(_id);
+}
+
+GraphicShader::Guard::~Guard() {
+ glUseProgram(0);
+}
+
+GraphicShader::Guard GraphicShader::use() const {
+ return Guard(_id);
+}
+
+GraphicShader::GraphicShader(const std::string& vertex, const std::string fragment):
+ _id(glCreateProgram()) {
+ glAttachShader(_id, util::compileShader(vertex, GL_VERTEX_SHADER));
+ glAttachShader(_id, util::compileShader(fragment, GL_FRAGMENT_SHADER));
+ glLinkProgram(_id);
+}
+
+GraphicShader::~GraphicShader() {
+ glDeleteProgram(_id);
+}
+
+GLuint GraphicShader::setUniform(const std::string& name, int value) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniform1i(id, value);
+ return id;
+}
+
+GLuint GraphicShader::setUniform(const std::string& name, const std::vector<GLuint>& v) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniform1iv(id, v.size(), reinterpret_cast<const GLint*>(v.data()));
+ return id;
+}
+
+GLuint GraphicShader::setUniform(const std::string& name, glm::mat4& M) const {
+ GLuint id = util::getUniform(_id, name);
+ glUniformMatrix4fv(id, 1, GL_FALSE, &M[0][0]);
+ return id;
+}
diff --git a/src/shader/wrap/graphic_shader.h b/src/shader/wrap/graphic_shader.h
index 03249d5..82f7e7a 100644
--- a/src/shader/wrap/graphic_shader.h
+++ b/src/shader/wrap/graphic_shader.h
@@ -1,5 +1,11 @@
#pragma once
+#include <vector>
+#include <string>
+
+#include <GL/glew.h>
+#include <glm/glm.hpp>
+
#include "shader/util.h"
class GraphicShader {
@@ -10,43 +16,16 @@ public:
struct Guard {
const GLuint _id;
- Guard(GLuint id): _id(id) {
- glUseProgram(_id);
- }
- ~Guard() {
- glUseProgram(0);
- }
+ Guard(GLuint id);
+ ~Guard();
};
- Guard use() const {
- return Guard(_id);
- }
+ Guard use() const;
- GraphicShader(const std::string& vertex, const std::string fragment):
- _id(glCreateProgram()) {
- glAttachShader(_id, util::compileShader(vertex, GL_VERTEX_SHADER));
- glAttachShader(_id, util::compileShader(fragment, GL_FRAGMENT_SHADER));
- glLinkProgram(_id);
- };
- ~GraphicShader() {
- glDeleteProgram(_id);
- }
-
- GLuint setUniform(const std::string& name, int value) const {
- GLuint id = util::getUniform(_id, name);
- glUniform1i(id, value);
- return id;
- }
-
- GLuint setUniform(const std::string& name, const std::vector<GLuint>& v) const {
- GLuint id = util::getUniform(_id, name);
- glUniform1iv(id, v.size(), reinterpret_cast<const GLint*>(v.data()));
- return id;
- }
-
- GLuint setUniform(const std::string& name, glm::mat4& M) const {
- GLuint id = util::getUniform(_id, name);
- glUniformMatrix4fv(id, 1, GL_FALSE, &M[0][0]);
- return id;
- }
+ GraphicShader(const std::string& vertex, const std::string fragment);
+ ~GraphicShader();
+
+ GLuint setUniform(const std::string& name, int value) const;
+ GLuint setUniform(const std::string& name, const std::vector<GLuint>& v) const;
+ GLuint setUniform(const std::string& name, glm::mat4& M) const;
};
diff --git a/src/timer.cc b/src/timer.cc
new file mode 100644
index 0000000..c46017c
--- /dev/null
+++ b/src/timer.cc
@@ -0,0 +1,16 @@
+#include "timer.h"
+
+namespace timer {
+
+std::chrono::time_point<std::chrono::high_resolution_clock> now() {
+ return std::chrono::high_resolution_clock::now();
+}
+
+double millisecondsSince(
+ std::chrono::time_point<std::chrono::high_resolution_clock>& pit) {
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ now() - pit
+ ).count();
+}
+
+}
diff --git a/src/timer.h b/src/timer.h
new file mode 100644
index 0000000..aed1b61
--- /dev/null
+++ b/src/timer.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <chrono>
+
+namespace timer {
+
+std::chrono::time_point<std::chrono::high_resolution_clock> now();
+
+double millisecondsSince(
+ std::chrono::time_point<std::chrono::high_resolution_clock>& pit);
+
+}
diff --git a/src/util.h b/src/util.h
deleted file mode 100644
index 1863563..0000000
--- a/src/util.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#pragma once
-
-#include <chrono>
-