cool article on fluid dynamics with code

damit! I was just cleaning up my fluid dynamics stuff to release it. Oh well. I’ll take a look at yours and maybe learn something. I haven’t looked at you code yet but mine has a separate forces control so you can add forces and affect the densities in a negative way (like pushing away the fluid) I also made a way to add walls and such where the fluid cannot penetrate. But your has a very cool optical flow option that is totally outrageous. Ah, I’m just joking about the damit stuff. I loved your piece at Glastonbury 2008. It is totally cool that you are releasing the fluid stuff. I’ll take a look at your code and I know it will help to optimize my fluid stuff. I am interested to see how you managed multiple densities in your fluid. I was using a vector and have been going back and forth about using a map.



hey ding, I"ve been meaning to upload some of the source code for the glastonbury app for a while actually… there were just some areas I was embarrassed of (some global variables etc. :P) so wanted to get rid of them and add a few comments before uploading… so this thread just reminded me to do that.

the optical flow stuff is actually not part of the fluid solver at all, i just feed the motion vectors from the optical flow analysis into the fluid every frame in the main update()… so you should be able to do the same with your code too using the Optical Flow classes.

For the color I just use 3 arrays of density, r, g, b which I treat the same as Jos Stam treats his d[] array (density). Theoretically its generally faster to have 3 simple linear arrays, as opposed to multidimensional or arrays of objects. Normally the difference is negligible, but since in this case the density arrays are parsed millions of times every second, I thought I might as well keep them seperate to gain every nanosecond I can!

My fluid system stuff is a bit tailored for the glastonbury app. e.g. after uploading I remembered I have code in there to ‘disintegrate’ the fluid over time, depending on how full or ‘uniform’ the fluid is, and theres no interface to disable or edit that without editing the fluid class code. Also the fluid has upper and lower boundaries (it bounces back), but left/right it wraps around instead of bouncing back. These are really easy to change in the fluid class code… but there is no interface to adjust them… so you can’t really call the class a proper addon. I put the code up not as a potential all-purpose addon, but just cos I had the code, and it could be useful to some with the integration of fluids and camera and particles etc…

So if yours is more general purpose it could be a good addon… the walls etc. stuff sounds good too!! For glastonbury I wanted to code it so the fluid flowed around the silhouettes, but didn’t have time. looking forward to it!

P.S. for glastonbury I was using a colormap for the color of the fluid. This was just used when rgb density was being injected into the fluid. For the updates and advecting etc I didn’t use the colormap at all. In the source code I posted I’m just injecting timebased sinusoidally modulating colors (if you use random colors you just get a greyish mucky color cos you get all the different colors very quickly blending into each other).

I think that the more people post code the better other people can learn so even though your code is not tailored as a standalone addon it is a source of learning and inspiration. I just peeked at your code and it is very involved. It is going to take me a little bit to chew thru it but it looks really cool. I would love for you to look at my fluid stuff before I release it into the wild and give me some thoughts on it.


hey nice! I was just working on some fluids code as well, but your code looks really nice.

hey memo, this is massive!!! thanks a lot :slight_smile:

Thanks! great example!! I’m trying to run the project “_fluids+particles_Stripped” in Xcode but I have problems compiling. I have some errors like this:

V/apps/_fluids+particles_Stripped/…/…/libs/openFrameworks/sound/ofSoundPlayer.h:8:26: error: fmod_errors.h: No such file or directory
/OpenFrameworks V/apps/_fluids+particles_Stripped/…/…/libs/openFrameworks/graphics/ofImage.h:60: error: ISO C++ forbids declaration of ‘FIBITMAP’ with no type

Somebody have a suggestion to compile?
Thanks a lot for the help

this is the same problem as :


has it been solved?


Hi Zach!
yes it’s working now. Amazing examples!

on visual studio 2008, winxp:

Balls.h> uint lastTriggerCounter; changed to int… because it doesn’t compile.

and after compilation, when i move in front of webcam, it crashes.

on debug:
Unhandled exception at 0x00460449 in fluids_particles_debug.exe: 0xC0000005: Access violation reading location 0x06a52d57.

that is on GlitterManager::getNoiseAt()

float ret = noisePixels[y * noiseMap.width + x];

This has been happening a bit lately with VC2008 in debug. I bet if you compile in release it will be fine.…-6&start=60


no, it crashes also in release mode.

same as debug mode.
i can see the webcam feed, and a few seconds later it crashes.

Hi Guys, firstly I should say I’m surprised no one posted about problems compiling (other than the FreeImage stuff), because I know I"ve made quite a few little changes to the core here and there… but when I posted the source I couldn’t be bothered to go through it all and find it… I thought it would be quite straightforward for someone else to find with a clean version of OF because the compiler would just give an error at the relevant place (and I had the disclaimer on my site :oops:)

Then I remembered I marked all my changes with a comment so was actually quite easy to go through and see what I’d done - and it turns out there’s only one tiny change needed for this fluids/optical flow stuff to work - and unfortunately the change doesn’t break the compile, it just makes it act a bit weird. I know I shouldn’t have changed the core, but I was in a rush etc, etc, and was a quick fix. The change is:

in /libs/addons/ofxOpenCv/src/ofxCvGrayscaleImage.h change

    void threshold( int value, bool invert = false);  


    void threshold( int value, int type = CV_THRESH_BINARY); // MSA  

and in /libs/addons/ofxOpenCv/src/ofxCvGrayscaleImage.cpp change

void ofxCvGrayscaleImage::threshold( int value, bool invert) {  
    if(invert) cvThreshold( cvImage, cvImageTemp, value, 255, CV_THRESH_BINARY_INV );  
    else cvThreshold( cvImage, cvImageTemp, value, 255, CV_THRESH_BINARY );  


void ofxCvGrayscaleImage::threshold( int value, int type) {            // MSA  
    cvThreshold( cvImage, cvImageTemp, value, 255, type );  

(this does mean that other apps using invert==true on threshold will not work properly, so bear that in mind)

I have no idea what this FreeImage error is about! I’m getting quite a few emails about this, so if someone can shed any iight on the matter it would be great.

And ungaro I have no idea what your crash is related to I’m afraid. Are you sure the noisemap.png is loaded? Can you have a look at what noiseMap.width and noiseMap.height are? (they should be 512x128).

Actually all that stuff isn’t that vital. All thats going on there, is instead of calculating perlin noise every frame for every particle (there are thousands of them), I’m using a pre-calculated array of perlin noise. And instead of pre-calculating the perlin noise at the beginning of the app, I’m just loading it in as an image. So as a temp workaround you could remove everying in getNoiseAt and replace it with:

	return ofRandom(-PI, PI);  

The glitter won’t behave the same, but will still have a random movement. Its weird that it doesn’t work though. Even weirder that it doesn’t compile with uint!

Yeah I figured something was up. I got it to compile but it didnt do much so I figured you where in a hurry and didnt adjust things but since my interest only lies within the solver that is all I really wanted out of it. So I like what you did with the solver. I think I will combine my rgb’s into one iteration like you did.


with everything you said & getNoiseAt to return a random number, i got it to run like 10 seconds and saw all the fluids and particles for a while.

but then again, everything became unresponsive.
(noiseMap.width & .height are just fine)

i’m running on core2 duo 1.8, geforce 7800 go, vista, vs2008. will try on a mac monday. i’m willing to combine it with ofxTouch. :slight_smile:

it’s really cool, thank you.

Thanks memo for the super cool code example.

I’ve recently downloaded it to try and get it working in VS2008 as well but i am experiencing exactly the same problem as ungaro as well. Would like to know if ungaro has made any progress as well with the errors?

Hey Progen, I really have no idea what the problem with VC2008 is I"m afraid, I only tested it on a mac. Did you see this thread ? dunno if its related or not…

hi memo,
just wanted to thank you for the code.

all the best.


I’m running windows using code::blocks and I have everything compiling, but I didn’t realise that Quicktime wasn’t set to grab video so I’ve gone back to change this and now I’m getting an undefined reference to ofVideoGrabber (See below)

obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0x5da)||undefined reference to ofVideoGrabber::qtInitSeqGrabber()'| obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0x807)||undefined reference toofVideoGrabber::qtCloseSeqGrabber()’|
obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0x93d)||undefined reference to ofVideoGrabber::qtCloseSeqGrabber()'| obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0xa92)||undefined reference toofVideoGrabber::qtInitSeqGrabber()’|
obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0xc08)||undefined reference to ofVideoGrabber::qtSelectDevice(int, bool)'| obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0xc7a)||undefined reference toofVideoGrabber::qtSelectDevice(int, bool)’|
obj\release\libs\openFrameworks\video\ofVideoGrabber.o:ofVideoGrabber.cpp:(.text+0xe37)||undefined reference to `ofVideoGrabber::qtCloseSeqGrabber()’|

I comment out “loadsettings()” in VideoGrabber.cpp to get rid of another error and that is what causes this error.

Before that I’m being told that loadsettings() isn’t defined in the scope (Hasn’t been initialised) so I go back and add it manually altering the ifdef (Seeing as loadsettings() is contained within the ifdef for a MACOSX platform). But then I get an error to do with the CFStringRef cameraString being unable to define the variable cameraString (Guessing this is not compatible with QT or windows?)

I’ve also tried getting rid of the ifdef as it looks wrong, so that all the “undefined functions” get called and put into the build (This works fine) but then I get many repeated errors for CFString.

Will try adding CFString, but it gets messy from there on in, any advice much appreciated.



I tried the CFPreferences header files and added those accordingly with linker options etc.

But now I’m getting undefined references for all the CF variables defined in CFPReferences.h. These are for MACOSX right? So I’m not sure if quicktime needs them to run with WIN32 or whether I am just barking up the wrong tree completely?

I added:

#include <CFBase.h>
#include <CFString.h>
#include <CFStringEncodingExt.h>
#include <CFPropertyList.h>
#include <CFPreferences.h>

“extern const CFStringRef kCFPreferencesCurrentApplication;” to CFPreferences because it looked like it was supposed to be there, even though I know in the apple page it isn’t…

Any ideas what I might be doing wrong?




C:\Program Files\CodeBlocks\OFX_00573-CB-WindowsWithAddons\libs\openFrameworks\video\ofQtUtils.cpp||In function OSErr GetSettingsPreference(const \_\_CFString\*, UserDataRecord\*\*)':| C:\Program Files\CodeBlocks\OFX\_00573-CB-WindowsWithAddons\libs\openFrameworks\video\ofQtUtils.cpp|306|error:kCFPreferencesCurrentApplication’ was not declared in this scope|
C:\Program Files\CodeBlocks\OFX_00573-CB-WindowsWithAddons\libs\openFrameworks\video\ofQtUtils.cpp||In function OSErr SaveSettingsPreference(const \_\_CFString\*, UserDataRecord\*)':| C:\Program Files\CodeBlocks\OFX\_00573-CB-WindowsWithAddons\libs\openFrameworks\video\ofQtUtils.cpp|349|error:kCFPreferencesCurrentApplication’ was not declared in this scope|

hey guys,

i did not read through the whole thread and i don’t know if this has been posted somewhere else already, but since this topic came up while i was searching the forums it thought i should post it since it fits the topic.

checkout Multi-Threaded-Fluid-Simulation-for-Games

have fun