From 9779fd7484f7af6d10ae28ca3763c6d938c341e3 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 22 Feb 2019 22:24:30 +0100 Subject: Tidy up wall drawing and geometry initialization --- src/buffer/vertex/material_buffer.cc | 20 ++++------------- src/buffer/vertex/material_buffer.h | 4 +++- src/glfw/window.cc | 21 +++++++++--------- src/main.cc | 10 ++++++++- src/shader/code/collide.glsl | 42 +++++++++++++++++++++--------------- src/shader/code/vertex.glsl | 2 +- 6 files changed, 52 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/buffer/vertex/material_buffer.cc b/src/buffer/vertex/material_buffer.cc index 82ccea2..d193318 100644 --- a/src/buffer/vertex/material_buffer.cc +++ b/src/buffer/vertex/material_buffer.cc @@ -2,7 +2,7 @@ #include -MaterialBuffer::MaterialBuffer(GLuint nX, GLuint nY): +MaterialBuffer::MaterialBuffer(GLuint nX, GLuint nY, std::function&& geometry): _nX(nX), _nY(nY) { glGenVertexArrays(1, &_array); glGenBuffers(1, &_buffer); @@ -13,21 +13,9 @@ MaterialBuffer::MaterialBuffer(GLuint nX, GLuint nY): std::vector data(nX*nY, GLint{1}); for ( int x = 0; x < nX; ++x ) { - data[ 0*nX + x] = 0; - data[(nY-1)*nX + x] = 0; - } - for ( int y = 0; y < nY; ++y ) { - data[y*nX + 0] = 0; - data[y*nX + nX-1] = 0; - } - - for ( int x = 1; x < nX-1; ++x ) { - data[ 1*nX + x] = 2; - data[(nY-2)*nX + x] = 2; - } - for ( int y = 1; y < nY-1; ++y ) { - data[y*nX + 1] = 2; - data[y*nX + nX-2] = 2; + for ( int y = 0; y < nY; ++y ) { + data[y*nX + x] = geometry(x,y); + } } glBufferData( diff --git a/src/buffer/vertex/material_buffer.h b/src/buffer/vertex/material_buffer.h index eccf008..4c61c55 100644 --- a/src/buffer/vertex/material_buffer.h +++ b/src/buffer/vertex/material_buffer.h @@ -2,6 +2,8 @@ #include +#include + class MaterialBuffer { private: const GLuint _nX; @@ -11,7 +13,7 @@ private: GLuint _buffer; public: - MaterialBuffer(GLuint nX, GLuint nY); + MaterialBuffer(GLuint nX, GLuint nY, std::function&& geometry); ~MaterialBuffer(); GLuint getBuffer() const; diff --git a/src/glfw/window.cc b/src/glfw/window.cc index 4f14501..c0074ad 100644 --- a/src/glfw/window.cc +++ b/src/glfw/window.cc @@ -38,21 +38,20 @@ int Window::getHeight() const { } std::tuple Window::getMouse() const { - int state = 0; + double x, y; + glfwGetCursorPos(_handle, &x, &y); + x = int(x - getWidth()/2); + y = int(getHeight()/2 - y); + if ( glfwGetMouseButton(_handle, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS ) { - state = 1; - } else if ( glfwGetMouseButton(_handle, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS ) { - state = 2; + return std::make_tuple(1, x, y); } - double x, y; - glfwGetCursorPos(_handle, &x, &y); + if ( glfwGetMouseButton(_handle, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS ) { + return std::make_tuple(2, x, y); + } - return std::make_tuple( - state, - x - int(getWidth()/2), - int(getHeight()/2 - y) - ); + return std::make_tuple(0, x, y); } KeyWatcher Window::getKeyWatcher(int key) const { diff --git a/src/main.cc b/src/main.cc index 0a400a5..f685c8f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -79,7 +79,15 @@ int renderWindow() { lattice_a = std::make_unique(nX, nY); lattice_b = std::make_unique(nX, nY); fluid = std::make_unique< FluidCellBuffer>(nX, nY); - geometry = std::make_unique< MaterialBuffer>(nX, nY); + geometry = std::make_unique< MaterialBuffer>(nX, nY, [](int x, int y) -> int { + if ( x == 0 || y == 0 || x == nX-1 || y == nY-1 ) { + return 0; // disable end of world + } + if ( (x == 1 || x == nX-2) && (y == 1 || y == nY-2) ) { + return 2; // bounce back outer walls + } + return 1; // everything shall be fluid + }); collide_shader = std::make_unique(COLLIDE_SHADER_CODE); stream_shader = std::make_unique(STREAM_SHADER_CODE); diff --git a/src/shader/code/collide.glsl b/src/shader/code/collide.glsl index b58bec3..182f5bd 100644 --- a/src/shader/code/collide.glsl +++ b/src/shader/code/collide.glsl @@ -117,18 +117,37 @@ float equilibrium(float d, vec2 v, int i, int j) { return w(i,j) * d * (1 + 3*comp(i,j,v) + 4.5*sq(comp(i,j,v)) - 1.5*sq(norm(v))); } +/// Disable wall interior + +void disableWallInterior(uint x, uint y) { + int wallNeighbors = 0; + + for ( int i = -1; i <= 1; ++i ) { + for ( int j = -1; j <= 1; ++j ) { + const int material = getMaterial(x+i,y+j); + if ( material == 0 || material == 2 ) { + ++wallNeighbors; + } + } + } + + if ( wallNeighbors == 9 ) { + setMaterial(x,y,0); + } +} + /// Determine external influence float getExternalPressureInflux(uint x, uint y) { - if ( mouseState == 1 && norm(vec2(x,y) - mousePos) < 2 ) { + if ( mouseState == 1 && norm(vec2(x,y) - mousePos) < 3 ) { return 1.5; } else { return 0.0; } } -bool getExternalWallRequest(uint x, uint y) { - if ( mouseState == 2 && norm(vec2(x,y) - mousePos) < 4 ) { +bool isWallRequestedAt(uint x, uint y) { + if ( mouseState == 2 && norm(vec2(x,y) - mousePos) < 3 ) { return true; } else { return false; @@ -145,26 +164,15 @@ void main() { return; } - if ( getExternalWallRequest(x,y) ) { + if ( isWallRequestedAt(x,y) ) { setMaterial(x,y,2); disableFluid(x,y); } const int material = getMaterial(x,y); - if ( material == 2 ) { - int wallNeighbors = 0; - for ( int i = -1; i <= 1; ++i ) { - for ( int j = -1; j <= 1; ++j ) { - const int material = getMaterial(x+i,y+j); - if ( material == 0 || material == 2 ) { - ++wallNeighbors; - } - } - } - if ( wallNeighbors == 9 ) { - setMaterial(x,y,0); - } + if ( material == 2 ) { // wall + disableWallInterior(x,y); } if ( material == 1 ) { // fluid diff --git a/src/shader/code/vertex.glsl b/src/shader/code/vertex.glsl index 52d9403..69bbaef 100644 --- a/src/shader/code/vertex.glsl +++ b/src/shader/code/vertex.glsl @@ -10,7 +10,7 @@ out VS_OUT { uniform uint nX; uniform uint nY; -const float displayAmplifier = 50.0; +const float displayAmplifier = 10.0; float unit(float x) { return 1.0/(1.0+exp(-x)); -- cgit v1.2.3