diff options
author | Adrian Kummerlaender | 2019-02-24 12:25:08 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2019-02-24 12:25:08 +0100 |
commit | 1ab5f3db018a3169ad6074fc2b75d44c3a45dd16 (patch) | |
tree | d3229fb41d119db2a19810d4f625e0e7d3421e24 | |
parent | 389da8159978571e8156ff7692bc595d957e846e (diff) | |
download | compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.tar compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.tar.gz compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.tar.bz2 compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.tar.lz compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.tar.xz compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.tar.zst compustream-1ab5f3db018a3169ad6074fc2b75d44c3a45dd16.zip |
Smoothen mouse interaction
-rw-r--r-- | src/main.cc | 15 | ||||
-rw-r--r-- | src/shader/code/collide.glsl | 36 |
2 files changed, 44 insertions, 7 deletions
diff --git a/src/main.cc b/src/main.cc index c9fb347..4621d30 100644 --- a/src/main.cc +++ b/src/main.cc @@ -106,6 +106,10 @@ int renderWindow() { auto tick_buffers = { lattice_a->getBuffer(), lattice_b->getBuffer(), fluid->getBuffer() }; auto tock_buffers = { lattice_b->getBuffer(), lattice_a->getBuffer(), fluid->getBuffer() }; + int prevLatticeMouseState = 0; + int prevLatticeMouseX = 0; + int prevLatticeMouseY = 0; + window.render([&](bool window_size_changed) { if ( pause_key.wasClicked() ) { update_lattice = !update_lattice; @@ -131,12 +135,19 @@ int renderWindow() { { auto guard = collide_shader->use(); + collide_shader->setUniform("prevMouseState", prevLatticeMouseState); + collide_shader->setUniform("prevMousePos", prevLatticeMouseX, prevLatticeMouseY); + const auto m = window.getMouse(); const float latticeMouseX = float(std::get<1>(m)) / window.getWidth() * world_width + nX/2; const float latticeMouseY = float(std::get<2>(m)) / window.getHeight() * world_height + nY/2; - collide_shader->setUniform("mouseState", std::get<0>(m)); - collide_shader->setUniform("mousePos", latticeMouseX, latticeMouseY); + collide_shader->setUniform("currMouseState", std::get<0>(m)); + collide_shader->setUniform("currMousePos", latticeMouseX, latticeMouseY); + + prevLatticeMouseState = std::get<0>(m); + prevLatticeMouseX = latticeMouseX; + prevLatticeMouseY = latticeMouseY; collide_shader->dispatch(nX, nY); } diff --git a/src/shader/code/collide.glsl b/src/shader/code/collide.glsl index 3f4e2c5..a73024e 100644 --- a/src/shader/code/collide.glsl +++ b/src/shader/code/collide.glsl @@ -9,8 +9,10 @@ layout (std430, binding=3) buffer bufferFluid { float fluidCells[]; }; /// external influence -uniform int mouseState; -uniform vec2 mousePos; +uniform int prevMouseState; +uniform vec2 prevMousePos; +uniform int currMouseState; +uniform vec2 currMousePos; /// LBM constants @@ -38,7 +40,23 @@ float sq(float x) { } float norm(vec2 v) { - return sqrt(sq(v.x)+sq(v.y)); + return sqrt(dot(v,v)); +} + +float distanceToLineSegment(vec2 a, vec2 b, vec2 p) { + const vec2 ab = b - a; + + const vec2 pa = a - p; + if ( dot(ab, pa) > 0.0 ) { + return norm(pa); + } + + const vec2 bp = p - b; + if ( dot(ab, bp) > 0.0 ) { + return norm(bp); + } + + return norm(pa - ab * (dot(ab, pa) / dot(ab, ab))); } /// Array indexing @@ -130,8 +148,16 @@ void disableWallInterior(uint x, uint y) { /// Determine external influence +bool isNearMouse(uint x, uint y, float eps) { + if ( prevMouseState == currMouseState ) { + return distanceToLineSegment(prevMousePos, currMousePos, vec2(x,y)) < eps; + } else { + return norm(vec2(x,y) - currMousePos) < eps; + } +} + float getExternalPressureInflux(uint x, uint y) { - if ( mouseState == 1 && norm(vec2(x,y) - mousePos) < 2 ) { + if ( currMouseState == 1 && isNearMouse(x, y, 3) ) { return 1.5; } else { return 0.0; @@ -139,7 +165,7 @@ float getExternalPressureInflux(uint x, uint y) { } bool isWallRequestedAt(uint x, uint y) { - if ( mouseState == 2 && norm(vec2(x,y) - mousePos) < 2 ) { + if ( currMouseState == 2 && isNearMouse(x, y, 3) ) { return true; } else { return false; |