aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/glfw/key_watcher.cc24
-rw-r--r--src/glfw/key_watcher.h17
-rw-r--r--src/glfw/window.cc4
-rw-r--r--src/glfw/window.h8
-rw-r--r--src/main.cc64
5 files changed, 88 insertions, 29 deletions
diff --git a/src/glfw/key_watcher.cc b/src/glfw/key_watcher.cc
new file mode 100644
index 0000000..21a570a
--- /dev/null
+++ b/src/glfw/key_watcher.cc
@@ -0,0 +1,24 @@
+#include "key_watcher.h"
+
+KeyWatcher::KeyWatcher(GLFWwindow* handle, int key):
+ _handle(handle),
+ _key(key),
+ _last_state(glfwGetKey(_handle, _key))
+{ }
+
+bool KeyWatcher::wasClicked() {
+ switch ( glfwGetKey(_handle, _key) ) {
+ case GLFW_RELEASE:
+ _last_state = GLFW_RELEASE;
+ return false;
+ case GLFW_PRESS:
+ if ( _last_state == GLFW_RELEASE ) {
+ _last_state = GLFW_PRESS;
+ return true;
+ } else {
+ return false;
+ }
+ default:
+ return false;
+ }
+}
diff --git a/src/glfw/key_watcher.h b/src/glfw/key_watcher.h
new file mode 100644
index 0000000..538829f
--- /dev/null
+++ b/src/glfw/key_watcher.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <GLFW/glfw3.h>
+
+class KeyWatcher {
+private:
+ GLFWwindow* const _handle;
+
+ int _key;
+ int _last_state;
+
+public:
+ KeyWatcher(GLFWwindow* handle, int key);
+
+ bool wasClicked();
+
+};
diff --git a/src/glfw/window.cc b/src/glfw/window.cc
index 17fc36c..6fbbfee 100644
--- a/src/glfw/window.cc
+++ b/src/glfw/window.cc
@@ -22,3 +22,7 @@ int Window::getWidth() const {
int Window::getHeight() const {
return _height;
}
+
+KeyWatcher Window::getKeyWatcher(int key) {
+ return KeyWatcher(_handle, key);
+}
diff --git a/src/glfw/window.h b/src/glfw/window.h
index b858e9a..520b5e8 100644
--- a/src/glfw/window.h
+++ b/src/glfw/window.h
@@ -5,6 +5,8 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
+#include "key_watcher.h"
+
class Window {
private:
bool _good = false;
@@ -21,6 +23,8 @@ public:
int getWidth() const;
int getHeight() const;
+ KeyWatcher getKeyWatcher(int key);
+
template <class F>
void init(F f);
@@ -39,8 +43,8 @@ template <class F>
void Window::render(F loop) {
glfwMakeContextCurrent(_handle);
- while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
- glfwWindowShouldClose(_handle) == 0 ) {
+ while ( glfwGetKey(_handle, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
+ glfwWindowShouldClose(_handle) == 0 ) {
glfwGetWindowSize(_handle, &_width, &_height);
loop();
diff --git a/src/main.cc b/src/main.cc
index cd8668e..5236f4c 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -26,7 +26,7 @@
#include "timer.h"
-const unsigned int particle_count = 2500;
+const unsigned int particle_count = 5000;
const unsigned int max_ups = 100;
const unsigned int texture_count = 20;
@@ -94,7 +94,7 @@ int main() {
int window_width = window.getWidth();
int window_height = window.getHeight();
- float world_width = 20.0;
+ float world_width = 10.0;
float world_height = getWorldHeight(window_width, window_height, world_width);
glm::mat4 MVP = getMVP(world_width, world_height);
@@ -122,7 +122,7 @@ int main() {
VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE);
compute_shader = std::make_unique<ComputeShader>(
getShaderFunction("cos(v.x*sin(v.y))",
- "sin(v.x-v.y)"));
+ "sin(v.x-v.y)"));
compute_shader->workOn(particle_buffer->getBuffer());
display_shader = std::make_unique<GraphicShader>(
DISPLAY_VERTEX_SHADER_CODE, DISPLAY_FRAGMENT_SHADER_CODE);
@@ -136,16 +136,24 @@ int main() {
return -1;
}
- auto lastFrame = timer::now();
- auto lastRotate = timer::now();
- bool justRotated = true;
+ auto last_frame = timer::now();
+ auto last_rotate = timer::now();
+
+ bool just_rotated = true;
+ bool update_particles = true;
std::vector<GLuint> textures;
for ( const auto& texture_buffer : texture_framebuffers ) {
textures.emplace_back(texture_buffer->getTexture());
}
+ auto pause_key = window.getKeyWatcher(GLFW_KEY_SPACE);
+
window.render([&]() {
+ if ( pause_key.wasClicked() ) {
+ update_particles = !update_particles;
+ }
+
if ( window.getWidth() != window_width
|| window.getHeight() != window_height ) {
window_width = window.getWidth();
@@ -159,35 +167,37 @@ int main() {
}
}
- if ( timer::millisecondsSince(lastFrame) >= 1000/max_ups ) {
- auto guard = compute_shader->use();
+ if ( update_particles ) {
+ if ( timer::millisecondsSince(last_frame) >= 1000/max_ups ) {
+ auto guard = compute_shader->use();
- compute_shader->setUniform("world", world_width, world_height);
- compute_shader->dispatch(particle_count);
+ compute_shader->setUniform("world", world_width, world_height);
+ compute_shader->dispatch(particle_count);
- lastFrame = timer::now();
- }
+ last_frame = timer::now();
+ }
- 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;
+ if ( timer::millisecondsSince(last_rotate) >= 1000/10 ) {
+ std::rotate(textures.begin(), textures.end()-1, textures.end());
+ std::rotate(texture_framebuffers.begin(), texture_framebuffers.end()-1, texture_framebuffers.end());
+ just_rotated = true;
- lastRotate = timer::now();
- }
+ last_rotate = timer::now();
+ }
- {
- auto texGuard = texture_framebuffers[0]->use();
- auto sdrGuard = scene_shader->use();
+ {
+ auto texGuard = texture_framebuffers[0]->use();
+ auto sdrGuard = scene_shader->use();
- scene_shader->setUniform("MVP", MVP);
+ scene_shader->setUniform("MVP", MVP);
- if ( justRotated ) {
- glClear(GL_COLOR_BUFFER_BIT);
- justRotated = false;
- }
+ if ( just_rotated ) {
+ glClear(GL_COLOR_BUFFER_BIT);
+ just_rotated = false;
+ }
- particle_buffer->draw();
+ particle_buffer->draw();
+ }
}
{