From 6f6073be5de8c8598c4af7f38c90c3f83b5bf1bf Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 18 Dec 2018 21:08:20 +0100 Subject: Hacky mouse-based fluid interaction --- src/buffer/vertex/lattice_cell_buffer.cc | 4 ++-- src/glfw/window.cc | 15 ++++++++++++++- src/glfw/window.h | 5 ++++- src/main.cc | 16 +++++++++++++--- src/shader/code/collide.glsl | 10 +++++++++- src/shader/wrap/compute_shader.cc | 12 +++++++++--- src/shader/wrap/compute_shader.h | 3 ++- 7 files changed, 53 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/buffer/vertex/lattice_cell_buffer.cc b/src/buffer/vertex/lattice_cell_buffer.cc index ea4e103..e8cfc08 100644 --- a/src/buffer/vertex/lattice_cell_buffer.cc +++ b/src/buffer/vertex/lattice_cell_buffer.cc @@ -7,7 +7,7 @@ LatticeCellBuffer::LatticeCellBuffer(GLuint nX, GLuint nY) { glGenBuffers(1, &_buffer); std::vector data(9*nX*nY, GLfloat{1./9.}); - const int insetX = 0.45*nX; + /*const int insetX = 0.45*nX; const int insetY = 0.45*nY; for (int x = insetX; x < nX-insetX; x++) { @@ -18,7 +18,7 @@ LatticeCellBuffer::LatticeCellBuffer(GLuint nX, GLuint nY) { } } } - } + }*/ glBindVertexArray(_array); glBindBuffer(GL_ARRAY_BUFFER, _buffer); diff --git a/src/glfw/window.cc b/src/glfw/window.cc index 9e76b57..98890b7 100644 --- a/src/glfw/window.cc +++ b/src/glfw/window.cc @@ -37,6 +37,19 @@ int Window::getHeight() const { return _height; } -KeyWatcher Window::getKeyWatcher(int key) { +std::tuple Window::getMouse() const { + const bool clicked = glfwGetMouseButton(_handle, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS; + + double x, y; + glfwGetCursorPos(_handle, &x, &y); + + return std::make_tuple( + clicked, + x - int(getWidth()/2), + int(getHeight()/2 - y) + ); +} + +KeyWatcher Window::getKeyWatcher(int key) const { return KeyWatcher(_handle, key); } diff --git a/src/glfw/window.h b/src/glfw/window.h index ea6075b..af7ba4d 100644 --- a/src/glfw/window.h +++ b/src/glfw/window.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -26,7 +27,9 @@ public: int getWidth() const; int getHeight() const; - KeyWatcher getKeyWatcher(int key); + std::tuple getMouse() const; + + KeyWatcher getKeyWatcher(int key) const; template void init(F f); diff --git a/src/main.cc b/src/main.cc index 51bc650..067af40 100644 --- a/src/main.cc +++ b/src/main.cc @@ -23,10 +23,10 @@ #include "timer.h" -constexpr GLuint nX = 128; -constexpr GLuint nY = 128; +constexpr GLuint nX = 256; +constexpr GLuint nY = 256; -constexpr int lups = 25; // max lattice updates per second +constexpr int lups = 30; // max lattice updates per second float getWorldHeight(int window_width, int window_height, float world_width) { return world_width / window_width * window_height; @@ -121,6 +121,16 @@ int renderWindow() { { auto guard = collide_shader->use(); + + const auto m = window.getMouse(); + collide_shader->setUniform("mouseClicked", std::get<0>(m)); + collide_shader->setUniform("mouseX", int( + float(std::get<1>(m)) / window.getWidth() * world_width + nX/2-1 + )); + collide_shader->setUniform("mouseY", int( + float(std::get<2>(m)) / window.getHeight() * world_height + nY/2 + )); + collide_shader->dispatch(nX, nY); } { diff --git a/src/shader/code/collide.glsl b/src/shader/code/collide.glsl index 78d3936..2626c70 100644 --- a/src/shader/code/collide.glsl +++ b/src/shader/code/collide.glsl @@ -7,6 +7,10 @@ layout (std430, binding=1) buffer bufferCollide{ float collideCells[]; }; layout (std430, binding=2) buffer bufferStream{ float streamCells[]; }; layout (std430, binding=3) buffer bufferFluid{ float fluidCells[]; }; +uniform int mouseClicked; +uniform int mouseX; +uniform int mouseY; + /// LBM constants uniform uint nX; @@ -103,7 +107,11 @@ void main() { return; } - const float d = density(x,y); + float d = density(x,y); + if ( mouseClicked == 1 && abs(x - mouseX) < 3 && abs(y - mouseY) < 3 ) { + d = 1.5; + } + const vec2 v = velocity(x,y,d); setFluid(x,y,v,d); diff --git a/src/shader/wrap/compute_shader.cc b/src/shader/wrap/compute_shader.cc index 54f50f8..134db2d 100644 --- a/src/shader/wrap/compute_shader.cc +++ b/src/shader/wrap/compute_shader.cc @@ -34,18 +34,24 @@ bool ComputeShader::isGood() const { return _good; } -GLuint ComputeShader::setUniform(const std::string& name, float x, float y) const { +GLuint ComputeShader::setUniform(const std::string& name, int value) const { GLuint id = util::getUniform(_id, name); - glUniform2f(id, x, y); + glUniform1i(id, value); return id; } -GLuint ComputeShader::setUniform(const std::string& name, unsigned int value) const { +GLuint ComputeShader::setUniform(const std::string& name, GLuint value) const { GLuint id = util::getUniform(_id, name); glUniform1ui(id, value); return 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(const std::vector& buffers) const { glBindBuffersBase(GL_SHADER_STORAGE_BUFFER, 1, buffers.size(), buffers.data()); } diff --git a/src/shader/wrap/compute_shader.h b/src/shader/wrap/compute_shader.h index f2ab182..d7ef729 100644 --- a/src/shader/wrap/compute_shader.h +++ b/src/shader/wrap/compute_shader.h @@ -26,8 +26,9 @@ public: bool isGood() const; + GLuint setUniform(const std::string& name, int value) const; + GLuint setUniform(const std::string& name, GLuint value) const; GLuint setUniform(const std::string& name, float x, float y) const; - GLuint setUniform(const std::string& name, unsigned int value) const; void workOn(const std::vector& buffers) const; -- cgit v1.2.3