Fluid in NxOgre

doboszsite

29-04-2010 19:43:55

I have a simple question. How to get some fluids to render. I would like to get some water like effect, but I just couldn’t find any tutorial on fluids. Can someone give me a link to reference or even a tip how should I do it. I don't really know how to start. There are like 2 or 3 class in NxOgre which have to do something with fluids, but I have no idea where to look for right functions. Please help me out.

betajaen

29-04-2010 20:49:00

Have a look at Detritus, and Critter.

Once you have that up and running, then take a look at the Critter::RenderSystem::createFluid function.

RenderSystem::createFluid(const NxOgre::FluidDescription&, const Ogre::String& material = "BaseWhiteNoLighting", ::Critter::Enums::FluidRenderableType = ::Critter::Enums::FluidType_Position);

[Edit]

This isn't much, but this is the code to create that gas I demonstrated in the video I posted a few months ago.

mScene->createSceneGeometry(new Box(16,16,1), Vec3(0,0,-8));
mScene->createSceneGeometry(new Box(16,16,1), Vec3(0,0, 8));
mScene->createSceneGeometry(new Box(1,16,16), Vec3(-8,0, 0));
mScene->createSceneGeometry(new Box(1,16,16), Vec3(8,0, 0));
mScene->createSceneGeometry(new Box(16,1,18), Vec3(0,8, 0));


NxOgre::FluidDescription desc;
desc.mMaxParticles = 25000;
desc.mKernelRadiusMultiplier = 2.0f;
desc.mRestParticlesPerMetre = 7.0f;
desc.mMotionLimitMultiplier = 3.0f;
desc.mPacketSizeMultiplier = 8;
desc.mCollisionDistanceMultiplier = 0.1f;
desc.mStiffness = 50.0f;
desc.mViscosity = 10.0f;
desc.mRestDensity = 1000.0f;
desc.mSimulationMethod = Enums::FluidSimulationMethod_SPH;
desc.mFlags |= Enums::FluidFlags_Hardware;
desc.mFlags |= Enums::FluidFlags_DisableGravity;

fluid = mRenderSystem->createFluid(desc, "Gas", OGRE3DFluidType_OgreParticle);

FluidEmitterDescription edesc;
edesc.mPose.set(0, 4, 0);
edesc.mShape = Enums::FluidEmitterShape_Ellipse;
edesc.mParticleLifetime = 5.0;
edesc.mRate = 5000;
edesc.mFluidSpeed = 15;
edesc.mType = Enums::FluidEmitterType_Pressure;
edesc.mRandomAngle = 4.5f;
edesc.mRandomPosition.set(0.05f, 0.05f, 1.0f);
edesc.mReplusionCoefficient = 0.8f;
edesc.mDimensionX = 4.0f;
edesc.mDimensionY = 4.0f;

emitter = fluid->createEmitter(edesc);


Basically, it's split into a three parts.

1. Create the invisible box to hold the gas.
2. Create the fluid.
2a. It uses the "Gas" ogre material.
2b. It uses the OgreParticle type (it uses the ogre billboard system). Have a look at the OGRE3DFluidType enum, for different ways of rendering the particles.
3. Create an emitter to emit the fluid (think of it like a tap/facet)

The emitter is responsible for the destruction of the fluid particles, via the mParticleLifetime in the emitter description. If you want something more direct then you can use a drain.

You should have a read of the PhysX tutorials on fluids, much of it applies to NxOgre. Even the class names/methods are similar in names.

doboszsite

29-04-2010 22:49:55

This will be useful. I will read some more about fluid in PhysX as you told me and try to play some with the code you've give me. Thanks a lot.

[EDIT]

Well, I have some problems with the code you've give me. "fluid" is undeclared identifier and I'm not quite sure what it should be. I know no object in NxOgre with createFluid function. Then there is "edesc" which uses undefined class 'NxOgre::FluidEmitterDescription', I don’t quite get it. If it is possible I would like to see whole code of your gas fluid simulation. If I'm aiming to height, please tell me.

cybereality

29-05-2010 03:45:10

I am also looking to do some fluid simulations with Ogre. I am hoping to build something like this:

http://www.youtube.com/watch?v=UYIPg8TEMmU

Is there any way NxOgre is going to help with this or should I be looking to do something custom with CUDA?

betajaen

29-05-2010 09:28:25

PhysX used to give out an API that would generate the fluid mesh that could be rendered by the graphics API. Sadly for some reason they took it out, now they just give the position of each particle, and that's it. I was thinking of implementing volume rendering such as marching cubes, but it seems a bit complicated to implement, so I haven't gotten around to doing it yet.

Although it rendering it isn't too terribly important, I'm concerned if the fluid api can actually do what was in the video. Although I'm confident that it will fill the container, I'm not sure if the PhysX fluids will be as accurate as shown in the video - assuming it wasn't the PhysX Fluids API in the video (although it seems silly for Nvidia to write their own, since they own PhysX now), but I could be wrong.

You could do it all in CUDA yourself, but you would have to write the fluid code, and the volume rendering code. Otherwise if you want to try it through NxOgre and want to write some volume rendering code for me, your welcome to try. ;)

Karan

29-05-2010 11:50:52

I'm pretty sure they use the PhysX fluids API, as it doesn't look much different than the older official PhysX Fluid Demo. But the simulation code likely is a bit more advanced than the current public version*. As for the rendering, they use (at least some variant of) what Simon Green presented at the GDC 2010:
Screen Space Fluid Rendering for Games
Also check out the referenced paper "Screen Space Fluid Rendering with Curvature Flow" if you can get a hold of it (I got it because I have access at the university, but the only public link I found is dead).

By the way, I'm implementing (and hopefully improving) this technique as part of my master's thesis and I'll probably release the code afterwards (which will probably be more towards the end of the year). Interested, betajaen?

As for the old PhysX Fluid Mesh API, I think it was based on Screen Space Meshes and if so it was probably good to remove it as it was implemented on the CPU and wouldn't be able to handle all the particles one GPU can simulate today^^


* EDIT: Found some confirmation: New fluid simulation video posted

betajaen

29-05-2010 12:02:13

By the way, I'm implementing (and hopefully improving) this technique as part of my master's thesis and I'll probably release the code afterwards (which will probably be more towards the end of the year). Interested, betajaen?

Very much so!

You are now the un-official expert in Fluids around here. ;)

cybereality

31-05-2010 01:06:38

Cool, thanks guys. Looking at the old PhysX fluid demo (which I hadn't seen before) I think that does basically what I need. So I guess PhysX itself it capable, its more a matter if I'm capable of writing the volume rendering code. Maybe I might hold off and see what Karan releases. Or I might torture myself and try to do it anyway.

Karan

04-06-2010 13:36:45

Cool, thanks guys. Looking at the old PhysX fluid demo (which I hadn't seen before) I think that does basically what I need. So I guess PhysX itself it capable, its more a matter if I'm capable of writing the volume rendering code. Maybe I might hold off and see what Karan releases. Or I might torture myself and try to do it anyway.
But don't get impatient with me, it might take quite a while until I release it (I don't know yet when I'll finish my thesis and I may also be busy with other stuff then for a while...). If you want to do it yourself and need help with the shaders, you can extract the GLSL shaders from the PhysX fluid demo - just open the .exe with a text editor and search for the shader code (but it's quite some work as there are 32 shaders and they are not easy to understand...).
I'm using Cg for the shaders because I want them to work in DirectX as well.