From ecaf665a05bdfcd10937152c378cfaec7cdf1836 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 28 Apr 2019 12:53:27 +0200 Subject: Experimental visualization of the velocity curl Calculating the curl of our simulated velocity field requires an additional compute shader step. Handling of buffer and shader switching depending on the display mode is implemented rudimentarily for now. Most of this commit is scaffolding, the actual computation is more or less trivial: ``` const float dxvy = (getFluidVelocity(x+1,y).y - getFluidVelocity(x-1,y).y) / (2*convLength); const float dyvx = (getFluidVelocity(x,y+1).x - getFluidVelocity(x,y-1).x) / (2*convLength); setFluidExtra(x, y, dxvy - dyvx); ``` This implements the following discretization of the 2d curl operator: Let $V : \mathbb{N}^2 \to \mathbb{R}^2$ be the simulated velocity field at discrete lattice points spaced by $\Delta x \in \mathbb{R}_{\gt 0}$. We want to approximate the $z$-component of the curl for visualization: $$\omega := \partial_x F_y - \partial_y F_x$$ As we do not possess the actual function $F$ but only its values at a set of discrete points we approximate the two partial derivatives using a second order central difference scheme: $$\overline{\omega}(i,j) := \frac{F_y(i+1,j) - F_y(i-1,j)}{2 \Delta x} - \frac{F_x(i,j+1) - F_x(i,j-1)}{2 \Delta x}$$ Note that the scene shader does some further rescaling of the curl to better fit the color palette. One issue that irks me is the emergence of some artefacts near boundaries as well as isolated "single-cell-vortices". This might be caused by running the simulation too close to divergence but as I am currently mostly interested in building an interactive fluid playground it could be worth it to try running an additional smoothening shader pass to straighten things out. --- src/buffer/vertex/fluid_cell_buffer.cc | 12 +++++++----- src/buffer/vertex/fluid_cell_buffer.h | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src/buffer/vertex') diff --git a/src/buffer/vertex/fluid_cell_buffer.cc b/src/buffer/vertex/fluid_cell_buffer.cc index 8fdc579..d9af989 100644 --- a/src/buffer/vertex/fluid_cell_buffer.cc +++ b/src/buffer/vertex/fluid_cell_buffer.cc @@ -5,11 +5,8 @@ FluidCellBuffer::FluidCellBuffer(GLuint nX, GLuint nY, std::function&& geometry): _nX(nX), _nY(nY) { glGenBuffers(1, &_buffer); - glBindVertexArray(_array); + enable(); init(std::forward(geometry)); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); - } FluidCellBuffer::~FluidCellBuffer() { @@ -17,9 +14,14 @@ FluidCellBuffer::~FluidCellBuffer() { glDeleteVertexArrays(1, &_array); } -void FluidCellBuffer::init(std::function&& geometry) { +void FluidCellBuffer::enable() { + glBindVertexArray(_array); glBindBuffer(GL_ARRAY_BUFFER, _buffer); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); +} +void FluidCellBuffer::init(std::function&& geometry) { std::vector data(3*_nX*_nY, GLfloat{}); for ( int x = 0; x < _nX; ++x ) { diff --git a/src/buffer/vertex/fluid_cell_buffer.h b/src/buffer/vertex/fluid_cell_buffer.h index 791ebea..5148606 100644 --- a/src/buffer/vertex/fluid_cell_buffer.h +++ b/src/buffer/vertex/fluid_cell_buffer.h @@ -16,6 +16,7 @@ public: FluidCellBuffer(GLuint nX, GLuint nY, std::function&& geometry); ~FluidCellBuffer(); + void enable(); void init(std::function&& geometry); GLuint getBuffer() const; -- cgit v1.2.3