From 389da8159978571e8156ff7692bc595d957e846e Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 23 Feb 2019 16:10:08 +0100 Subject: Store material in fluid buffer and improve visualization Replaces the density value which is actually not that useful for visualization. Encoding integer values as floats by casting and comparing them using exact floating point comparison is not very safe but works out for now. --- CMakeLists.txt | 1 - src/buffer/vertex/fluid_cell_buffer.cc | 11 ++++++++-- src/buffer/vertex/fluid_cell_buffer.h | 4 +++- src/buffer/vertex/material_buffer.cc | 39 ---------------------------------- src/buffer/vertex/material_buffer.h | 20 ----------------- src/main.cc | 12 +++++------ src/shader/code/collide.glsl | 36 ++++++++++++------------------- src/shader/code/stream.glsl | 21 +++++++----------- src/shader/code/vertex.glsl | 16 ++++++++++++-- 9 files changed, 53 insertions(+), 107 deletions(-) delete mode 100644 src/buffer/vertex/material_buffer.cc delete mode 100644 src/buffer/vertex/material_buffer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 70f0107..5b8b141 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,6 @@ add_executable( src/shader/wrap/graphic_shader.cc src/buffer/vertex/lattice_cell_buffer.cc src/buffer/vertex/fluid_cell_buffer.cc - src/buffer/vertex/material_buffer.cc ) target_link_libraries( diff --git a/src/buffer/vertex/fluid_cell_buffer.cc b/src/buffer/vertex/fluid_cell_buffer.cc index a0bfcc3..4930d63 100644 --- a/src/buffer/vertex/fluid_cell_buffer.cc +++ b/src/buffer/vertex/fluid_cell_buffer.cc @@ -2,7 +2,7 @@ #include -FluidCellBuffer::FluidCellBuffer(GLuint nX, GLuint nY): +FluidCellBuffer::FluidCellBuffer(GLuint nX, GLuint nY, std::function&& geometry): _nX(nX), _nY(nY) { glGenVertexArrays(1, &_array); glGenBuffers(1, &_buffer); @@ -10,7 +10,14 @@ FluidCellBuffer::FluidCellBuffer(GLuint nX, GLuint nY): glBindVertexArray(_array); glBindBuffer(GL_ARRAY_BUFFER, _buffer); - const std::vector data(3*nX*nY, GLfloat{}); + std::vector data(3*nX*nY, GLfloat{}); + + for ( int x = 0; x < nX; ++x ) { + for ( int y = 0; y < nY; ++y ) { + data[3*nX*y + 3*x + 2] = geometry(x,y); + } + } + glBufferData( GL_ARRAY_BUFFER, data.size() * sizeof(GLfloat), diff --git a/src/buffer/vertex/fluid_cell_buffer.h b/src/buffer/vertex/fluid_cell_buffer.h index 9e535cb..8afb342 100644 --- a/src/buffer/vertex/fluid_cell_buffer.h +++ b/src/buffer/vertex/fluid_cell_buffer.h @@ -2,6 +2,8 @@ #include +#include + class FluidCellBuffer { private: const GLuint _nX; @@ -11,7 +13,7 @@ private: GLuint _buffer; public: - FluidCellBuffer(GLuint nX, GLuint nY); + FluidCellBuffer(GLuint nX, GLuint nY, std::function&& geometry); ~FluidCellBuffer(); GLuint getBuffer() const; diff --git a/src/buffer/vertex/material_buffer.cc b/src/buffer/vertex/material_buffer.cc deleted file mode 100644 index d193318..0000000 --- a/src/buffer/vertex/material_buffer.cc +++ /dev/null @@ -1,39 +0,0 @@ -#include "material_buffer.h" - -#include - -MaterialBuffer::MaterialBuffer(GLuint nX, GLuint nY, std::function&& geometry): - _nX(nX), _nY(nY) { - glGenVertexArrays(1, &_array); - glGenBuffers(1, &_buffer); - - glBindVertexArray(_array); - glBindBuffer(GL_ARRAY_BUFFER, _buffer); - - std::vector data(nX*nY, GLint{1}); - - for ( int x = 0; x < nX; ++x ) { - for ( int y = 0; y < nY; ++y ) { - data[y*nX + x] = geometry(x,y); - } - } - - glBufferData( - GL_ARRAY_BUFFER, - data.size() * sizeof(GLint), - data.data(), - GL_STATIC_DRAW - ); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 1, GL_INT, GL_FALSE, 0, nullptr); -} - -MaterialBuffer::~MaterialBuffer() { - glDeleteBuffers(1, &_buffer); - glDeleteVertexArrays(1, &_array); -} - -GLuint MaterialBuffer::getBuffer() const { - return _buffer; -} diff --git a/src/buffer/vertex/material_buffer.h b/src/buffer/vertex/material_buffer.h deleted file mode 100644 index 4c61c55..0000000 --- a/src/buffer/vertex/material_buffer.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -#include - -class MaterialBuffer { -private: - const GLuint _nX; - const GLuint _nY; - - GLuint _array; - GLuint _buffer; - -public: - MaterialBuffer(GLuint nX, GLuint nY, std::function&& geometry); - ~MaterialBuffer(); - - GLuint getBuffer() const; -}; diff --git a/src/main.cc b/src/main.cc index f685c8f..c9fb347 100644 --- a/src/main.cc +++ b/src/main.cc @@ -10,7 +10,6 @@ #include "buffer/vertex/fluid_cell_buffer.h" #include "buffer/vertex/lattice_cell_buffer.h" -#include "buffer/vertex/material_buffer.h" #include "shader/wrap/graphic_shader.h" #include "shader/wrap/compute_shader.h" @@ -67,7 +66,6 @@ int renderWindow() { std::unique_ptr lattice_a; std::unique_ptr lattice_b; std::unique_ptr fluid; - std::unique_ptr geometry; std::unique_ptr collide_shader; std::unique_ptr stream_shader; @@ -78,12 +76,12 @@ 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, [](int x, int y) -> int { + fluid = std::make_unique< FluidCellBuffer>(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) ) { + if ( ((x == 1 || x == nX-2) && (y > 0 && y < nY-1)) + || ((y == 1 || y == nY-2) && (x > 0 && x < nX-1)) ) { return 2; // bounce back outer walls } return 1; // everything shall be fluid @@ -105,8 +103,8 @@ int renderWindow() { auto pause_key = window.getKeyWatcher(GLFW_KEY_SPACE); - auto tick_buffers = { lattice_a->getBuffer(), lattice_b->getBuffer(), fluid->getBuffer(), geometry->getBuffer() }; - auto tock_buffers = { lattice_b->getBuffer(), lattice_a->getBuffer(), fluid->getBuffer(), geometry->getBuffer() }; + auto tick_buffers = { lattice_a->getBuffer(), lattice_b->getBuffer(), fluid->getBuffer() }; + auto tock_buffers = { lattice_b->getBuffer(), lattice_a->getBuffer(), fluid->getBuffer() }; window.render([&](bool window_size_changed) { if ( pause_key.wasClicked() ) { diff --git a/src/shader/code/collide.glsl b/src/shader/code/collide.glsl index 182f5bd..3f4e2c5 100644 --- a/src/shader/code/collide.glsl +++ b/src/shader/code/collide.glsl @@ -6,7 +6,6 @@ layout (local_size_x = 1, local_size_y = 1) in; layout (std430, binding=1) buffer bufferCollide { float collideCells[]; }; layout (std430, binding=2) buffer bufferStream { float streamCells[]; }; layout (std430, binding=3) buffer bufferFluid { float fluidCells[]; }; -layout (std430, binding=4) buffer bufferGeometry { int materialCells[]; }; /// external influence @@ -70,29 +69,22 @@ void set(uint x, uint y, int i, int j, float v) { collideCells[indexOfLatticeCell(x,y) + indexOfDirection(i,j)] = v; } -void setFluid(uint x, uint y, vec2 v, float d) { +void setFluid(uint x, uint y, vec2 v) { const uint idx = indexOfFluidVertex(x, y); fluidCells[idx + 0] = v.x; fluidCells[idx + 1] = v.y; - fluidCells[idx + 2] = d; } int getMaterial(uint x, uint y) { - return materialCells[nX*y + x]; + const uint idx = indexOfFluidVertex(x, y); + return int(fluidCells[idx + 2]); } void setMaterial(uint x, uint y, int m) { - materialCells[nX*y + x] = m; -} - -void disableFluid(uint x, uint y) { const uint idx = indexOfFluidVertex(x, y); - fluidCells[idx + 0] = 0.0; - fluidCells[idx + 1] = 0.0; - fluidCells[idx + 2] = -2.0; + fluidCells[idx + 2] = m; } - /// Moments float density(uint x, uint y) { @@ -125,7 +117,7 @@ void disableWallInterior(uint x, uint y) { 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 ) { + if ( material == 0 || material == 2 || material == 3 ) { ++wallNeighbors; } } @@ -139,7 +131,7 @@ void disableWallInterior(uint x, uint y) { /// Determine external influence float getExternalPressureInflux(uint x, uint y) { - if ( mouseState == 1 && norm(vec2(x,y) - mousePos) < 3 ) { + if ( mouseState == 1 && norm(vec2(x,y) - mousePos) < 2 ) { return 1.5; } else { return 0.0; @@ -147,7 +139,7 @@ float getExternalPressureInflux(uint x, uint y) { } bool isWallRequestedAt(uint x, uint y) { - if ( mouseState == 2 && norm(vec2(x,y) - mousePos) < 3 ) { + if ( mouseState == 2 && norm(vec2(x,y) - mousePos) < 2 ) { return true; } else { return false; @@ -164,14 +156,14 @@ void main() { return; } - if ( isWallRequestedAt(x,y) ) { - setMaterial(x,y,2); - disableFluid(x,y); - } - const int material = getMaterial(x,y); - if ( material == 2 ) { // wall + if ( isWallRequestedAt(x,y) && material == 1 ) { + setMaterial(x,y,3); + return; + } + + if ( material == 3 ) { // manually added wall disableWallInterior(x,y); } @@ -179,7 +171,7 @@ void main() { const float d = max(getExternalPressureInflux(x,y), density(x,y)); const vec2 v = velocity(x,y,d); - setFluid(x,y,v,d); + setFluid(x,y,v); for ( int i = -1; i <= 1; ++i ) { for ( int j = -1; j <= 1; ++j ) { diff --git a/src/shader/code/stream.glsl b/src/shader/code/stream.glsl index a8aec1d..59aed98 100644 --- a/src/shader/code/stream.glsl +++ b/src/shader/code/stream.glsl @@ -5,7 +5,7 @@ layout (local_size_x = 1, local_size_y = 1) in; layout (std430, binding=1) buffer bufferCollide { float collideCells[]; }; layout (std430, binding=2) buffer bufferStream { float streamCells[]; }; -layout (std430, binding=4) buffer bufferGeometry { int materialCells[]; }; +layout (std430, binding=3) buffer bufferFluid { float fluidCells[]; }; /// LBM constants @@ -24,6 +24,10 @@ uint indexOfLatticeCell(uint x, uint y) { return q*nX*y + q*x; } +uint indexOfFluidVertex(uint x, uint y) { + return 3*nX*y + 3*x; +} + /// Data access float get(uint x, uint y, int i, int j) { @@ -35,17 +39,8 @@ void set(uint x, uint y, int i, int j, float v) { } int getMaterial(uint x, uint y) { - return materialCells[nX*y + x]; -} - -/// Domain description - -bool isEndOfWorld(uint x, uint y) { - return x == 0 || x == nX-1 || y == 0 || y == nY-1; -} - -bool isOuterWall(uint x, uint y) { - return x == 1 || x == nX-2 || y == 1 || y == nY-2; + const uint idx = indexOfFluidVertex(x, y); + return int(fluidCells[idx + 2]); } /// Boundary conditions @@ -74,7 +69,7 @@ void main() { return; } - if ( material == 2 ) { + if ( material == 2 || material == 3 ) { bounceBack(x,y); } diff --git a/src/shader/code/vertex.glsl b/src/shader/code/vertex.glsl index 69bbaef..e0ce9a8 100644 --- a/src/shader/code/vertex.glsl +++ b/src/shader/code/vertex.glsl @@ -32,6 +32,14 @@ vec2 fluidVertexAtIndex(uint i) { ); } +bool isInactive(int material) { + return material == 0; +} + +bool isWallFrontier(int material) { + return material == 2 || material == 3; +} + void main() { const vec2 idx = fluidVertexAtIndex(gl_VertexID); @@ -42,13 +50,17 @@ void main() { 1. ); - if ( VertexPosition.z < -1.0 ) { + const int material = int(round(VertexPosition.z)); + + if ( isInactive(material) ) { + vs_out.color = vec3(0.5, 0.5, 0.5); + } else if ( isWallFrontier(material) ) { vs_out.color = vec3(0.0, 0.0, 0.0); } else { vs_out.color = mix( vec3(-0.5, 0.0, 1.0), vec3( 1.0, 0.0, 0.0), - displayAmplifier * VertexPosition.z * norm(VertexPosition.xy) + displayAmplifier * norm(VertexPosition.xy) ); } } -- cgit v1.2.3