Introduction and Problem/Project Description
The goal of this project was to simulate fluids in 3D, in real-time using a simple particle system to represent the fluid behavior. The particle system would be robust enough to handle a lot of particles at once on the screen and not affect the performance of the fluid simulation. The secondary goal was also to achieve additional performance by simulating the fluids on the GPU through the use of GPU hardware acceleration via compute shaders in Microsoft’s DirectX 11 API.
Approach
The approach to the project was simple. First figure out how fluids are typically modeled in computer graphics, and then to apply the same algorithm used in my project. I searched through many existing research papers and books and eventually found an implementation based on a particle approach that satisfied what I had in mind for the results.
Technical Details
The method used to model fluids in my demo is called Smoothed Particle Hydrodynamics. SPH is a technique developed a few years ago to model the consistency and behavior of fluids by applying the typical Navier-Stokes equation to a particle system. The basic concept involves a few steps.
The first step is to obtain a density for each particle based on surrounding particles. By using a special statistical technique called a smoothing kernel, we can obtain an average density around that particle based on the surrounding particles. Without the use of a grid, we must check against all other particles in the scene, making it an n^2 algorithm, but it can be cut down considerably by using sorting and grid techniques to reduce the number of particles to check against to the ones around it.
Then, once we have density, we can apply this density to calculate a force based on the densities of the surrounding particles. This is also where we calculate the viscosity component of the Navier-Stokes equation and output a final overall force for the particle.
Finally, we must integrate the position of the particle based on this calculated force (in addition to any added in external forces). To do this, I used a numerical integration technique called Verlet integration which was able to handle the fluid time-steps relatively well without making the fluids behave strangely on large time-steps.
The resulting calculations were then rendered to screen using an efficient sprite batching technique to watch the flow of the fluids.
Here is a link to the technical paper used for creating the fluid simulation.
Results
A screenshot of the fluids filling a small glass box (left) and spilling over the sides (right)
Future Enhancements
Given additional time, the first thing I would like to improve is the efficiency of the algorithm. I have taken a lot of time to make the current algorithm as efficient as I could, however a grid+sort algorithm would considerably reduce the complex from the current n^2 algorithm I’m using now. Doing this could increase the efficiency enough to increase the particle count ten fold without impacting performance from how it is now.
The next step would be a nicer rendering method. The current particle system does a great job showing the flow of the particles and fluid, but lacks in visual stimulation. Improving the technique to provide a 3D isosurface extraction method (marching cubes to create metaball rendering) could provide a much nicer looking fluid, though potentially at the cost of visualizing flow).
Project
Upon request, I can ship the source code and project files for the demo. The requirements are a DirectX10 or higher video card which support compute shader 4.0, 4.1, or 5.0, and Windows Vista or 7. The project is built in Visual Studio 2010, and there are no special requirements for compilation.