2DFluidSimulation
2DFluidSimulation copied to clipboard
A simple eulerian fluid simulation using OpenGL compute shaders
2D Fluid Simulator using OpenGL 
This project is an implementation of an eulerian fluid simulation on GPU using OpenGL 4.3 compute shaders capabilities.
Getting Started
You will first need to clone the repository
git clone https://github.com/cgurps/2DFluidSimulation.git [PROJECT_FOLDER]
and then init the submodules
cd [PROJECT_FOLDER]
git submodule update --init
To compile the project, you will need OpenGL with a version above 4.3 in order to get compute shader capabilities. You will also need Boost installed on your machine. To project uses CMake to generate the Makefile needed for the compilation. You can use these commands to build the executable
mkdir build
cd build
cmake ..
make -j [YOUR_NUMBER_OF_CORES]
You can query the program options using -h.
Numerical Scheme
We solve the Navier-Stokes equation for incompressible fluids:
Implementation
Each quantities is represented by a texture of 16bits floating points on the GPU. For exact texels query, I use the texelFetch method (which runs faster than using texture2D) and then handle the boundary cases by hand. The bilinear interpolation for the advection step is also computed by hand for better accuracy. The implementation contains three main classes:
GLFWHandleris the GLFW wrapper that contains the OpenGL initilization and the main program loopSimulationBasewhich is a pure virtual function that gives the interface for the simulation. The main loop of the program accesses theshared_texturevariable and display the associated texture on screen. This is where the various textures are created and stored.SimulationFactorywhich contains helpers for computing steps of the simulation (like advection, pressure projection, etc). This class does not allocate GPU memory, but is instead feeded by the simulation loop.
If you (ever) wish to play around this simulation, you should create a new class that inherits from SimulationBase and uses the SimulationFactory to compute whatever you need to compute. This new class must overload Init(), Update(), AddSplat(), AddSplat(const int) and RemoveSplat() for the simulation to work.
Note on the Jacobi method
I implemented a variation on the original Jacobi method described in Harris et al. called the Red-Black Jacobi method. The idea is to pack four values into a single texel. The packing is done both on the divergence and on the pressure values. The next figure (reproduced from the Figure 5 of Harris et al.) shows the process
divRB.comp).
References
- @: a simple tutorial on fluid simulation
- @: this awesome books covers a lot of techniques for simulating fluids (classic!)
- @: 2D fluids from GPU gems 1
- @: 3D fluids from GPU gems 3
- @: the Maccormack method
- @: this article explains the reverse method to handle extremas generated by the Maccormack scheme
- @: The Runge Kutta method for the particle advection in the semi-Lagragian approach
Nice github projects
- tunabrain/gpu-fluid: 2D fluid simulation on the GPU using an hydrid approach (FLIP)
- PavelDoGreat/WebGL-Fluid-Simulation: online fluid simulation