pxFluid.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/physics/physx/pxFluid.h"
  24. #include "console/consoleTypes.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "renderInstance/renderPassManager.h"
  27. #include "T3D/physics/physicsPlugin.h"
  28. #include "T3D/physics/physx/pxWorld.h"
  29. #include "T3D/physics/physx/pxCasts.h"
  30. #include "gfx/gfxDrawUtil.h"
  31. #include "math/mathIO.h"
  32. #include "core/stream/bitStream.h"
  33. IMPLEMENT_CO_NETOBJECT_V1( PxFluid );
  34. ConsoleDocClass( PxFluid,
  35. "@brief Experimental and unfinished Torque wrapper class for NxFluid.\n\n"
  36. "@internal\n"
  37. );
  38. PxFluid::PxFluid()
  39. : mWorld( NULL ),
  40. mScene( NULL ),
  41. mParticles( NULL ),
  42. mFluid( NULL ),
  43. mEmitter( NULL ),
  44. mParticleCount( 0 )
  45. {
  46. mNetFlags.set( Ghostable | ScopeAlways );
  47. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  48. }
  49. PxFluid::~PxFluid()
  50. {
  51. }
  52. bool PxFluid::onAdd()
  53. {
  54. if ( !Parent::onAdd() )
  55. return false;
  56. mWorld = dynamic_cast<PxWorld*>( PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" ) );
  57. if ( !mWorld || !mWorld->getScene() )
  58. {
  59. Con::errorf( "PxMultiActor::onAdd() - PhysXWorld not initialized!" );
  60. return false;
  61. }
  62. mScene = mWorld->getScene();
  63. if ( isClientObject() )
  64. _createFluid();
  65. Point3F halfScale = Point3F::One * 0.5f;
  66. mObjBox.minExtents = -halfScale;
  67. mObjBox.maxExtents = halfScale;
  68. resetWorldBox();
  69. addToScene();
  70. return true;
  71. }
  72. void PxFluid::onRemove()
  73. {
  74. if ( isClientObject() )
  75. _destroyFluid();
  76. removeFromScene();
  77. Parent::onRemove();
  78. }
  79. void PxFluid::initPersistFields()
  80. {
  81. Parent::initPersistFields();
  82. }
  83. void PxFluid::inspectPostApply()
  84. {
  85. Parent::inspectPostApply();
  86. setMaskBits( UpdateMask );
  87. }
  88. U32 PxFluid::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  89. {
  90. U32 retMask = Parent::packUpdate( conn, mask, stream );
  91. if ( stream->writeFlag( mask & UpdateMask ) )
  92. {
  93. mathWrite( *stream, getTransform() );
  94. mathWrite( *stream, getScale() );
  95. stream->write( mEmitter ? mEmitter->getRate() : 0 );
  96. }
  97. stream->writeFlag( isProperlyAdded() && mask & ResetMask );
  98. return retMask;
  99. }
  100. void PxFluid::unpackUpdate( NetConnection *conn, BitStream *stream )
  101. {
  102. Parent::unpackUpdate( conn, stream );
  103. // UpdateMask
  104. if ( stream->readFlag() )
  105. {
  106. MatrixF mat;
  107. mathRead( *stream, &mat );
  108. Point3F scale;
  109. mathRead( *stream, &scale );
  110. setScale( scale );
  111. setTransform( mat );
  112. F32 rate;
  113. stream->read( &rate );
  114. setRate( rate );
  115. }
  116. // ResetMask
  117. if ( stream->readFlag() )
  118. resetParticles();
  119. }
  120. void PxFluid::setTransform( const MatrixF &mat )
  121. {
  122. Parent::setTransform( mat );
  123. if ( mEmitter )
  124. {
  125. NxMat34 nxMat;
  126. nxMat.setRowMajor44( mat );
  127. mEmitter->setGlobalPose( nxMat );
  128. }
  129. }
  130. void PxFluid::setScale( const VectorF &scale )
  131. {
  132. Point3F lastScale = getScale();
  133. Point3F halfScale = Point3F::One * 0.5f;
  134. mObjBox.minExtents = -halfScale;
  135. mObjBox.maxExtents = halfScale;
  136. resetWorldBox();
  137. Parent::setScale( scale );
  138. if ( lastScale != getScale() &&
  139. mEmitter )
  140. {
  141. _destroyFluid();
  142. _createFluid();
  143. }
  144. }
  145. void PxFluid::prepRenderImage( SceneRenderState *state )
  146. {
  147. if ( !state->isDiffusePass() )
  148. return;
  149. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  150. ri->renderDelegate.bind( this, &PxFluid::renderObject );
  151. ri->type = RenderPassManager::RIT_Object;
  152. state->getRenderPass()->addInst( ri );
  153. }
  154. void PxFluid::resetParticles()
  155. {
  156. if ( mEmitter )
  157. mEmitter->resetEmission( MAX_PARTICLES );
  158. setMaskBits( ResetMask );
  159. }
  160. void PxFluid::setRate( F32 rate )
  161. {
  162. if ( mEmitter )
  163. mEmitter->setRate( rate );
  164. setMaskBits( UpdateMask );
  165. }
  166. void PxFluid::renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  167. {
  168. GFXStateBlockDesc desc;
  169. desc.setBlend( true );
  170. desc.setZReadWrite( true, false );
  171. for ( U32 i = 0; i < mParticleCount; i++ )
  172. {
  173. FluidParticle &particle = mParticles[i];
  174. Point3F pnt = pxCast<Point3F>( particle.position );
  175. Box3F box( 0.2f );
  176. box.minExtents += pnt;
  177. box.maxExtents += pnt;
  178. GFX->getDrawUtil()->drawCube( desc, box, ColorI::BLUE );
  179. }
  180. }
  181. void PxFluid::_createFluid()
  182. {
  183. /*
  184. // Set structure to pass particles, and receive them after every simulation step
  185. NxParticleData particleData;
  186. particleData.numParticlesPtr = &mParticleCount;
  187. particleData.bufferPos = &mParticles[0].position.x;
  188. particleData.bufferPosByteStride = sizeof(FluidParticle);
  189. particleData.bufferVel = &mParticles[0].velocity.x;
  190. particleData.bufferVelByteStride = sizeof(FluidParticle);
  191. particleData.bufferLife = &mParticles[0].lifetime;
  192. particleData.bufferLifeByteStride = sizeof(FluidParticle);
  193. // Create a fluid descriptor
  194. NxFluidDesc fluidDesc;
  195. fluidDesc.kernelRadiusMultiplier = 2.3f;
  196. fluidDesc.restParticlesPerMeter = 10.0f;
  197. fluidDesc.stiffness = 200.0f;
  198. fluidDesc.viscosity = 22.0f;
  199. fluidDesc.restDensity = 1000.0f;
  200. fluidDesc.damping = 0.0f;
  201. fluidDesc.simulationMethod = NX_F_SPH;
  202. fluidDesc.initialParticleData = particleData;
  203. fluidDesc.particlesWriteData = particleData;
  204. */
  205. NxFluidDesc fluidDesc;
  206. fluidDesc.setToDefault();
  207. fluidDesc.simulationMethod = NX_F_SPH;
  208. fluidDesc.maxParticles = MAX_PARTICLES;
  209. fluidDesc.restParticlesPerMeter = 50;
  210. fluidDesc.stiffness = 1;
  211. fluidDesc.viscosity = 6;
  212. fluidDesc.flags = NX_FF_VISUALIZATION|NX_FF_ENABLED;
  213. mParticles = new FluidParticle[MAX_PARTICLES];
  214. dMemset( mParticles, 0, sizeof(FluidParticle) * MAX_PARTICLES );
  215. NxParticleData &particleData = fluidDesc.particlesWriteData;
  216. particleData.numParticlesPtr = &mParticleCount;
  217. particleData.bufferPos = &mParticles[0].position.x;
  218. particleData.bufferPosByteStride = sizeof(FluidParticle);
  219. particleData.bufferVel = &mParticles[0].velocity.x;
  220. particleData.bufferVelByteStride = sizeof(FluidParticle);
  221. particleData.bufferLife = &mParticles[0].lifetime;
  222. particleData.bufferLifeByteStride = sizeof(FluidParticle);
  223. mFluid = mScene->createFluid( fluidDesc );
  224. //Create Emitter.
  225. NxFluidEmitterDesc emitterDesc;
  226. emitterDesc.setToDefault();
  227. emitterDesc.dimensionX = getScale().x;
  228. emitterDesc.dimensionY = getScale().y;
  229. emitterDesc.relPose.setColumnMajor44( getTransform() );
  230. emitterDesc.rate = 5.0f;
  231. emitterDesc.randomAngle = 0.1f;
  232. emitterDesc.fluidVelocityMagnitude = 6.5f;
  233. emitterDesc.maxParticles = 0;
  234. emitterDesc.particleLifetime = 4.0f;
  235. emitterDesc.type = NX_FE_CONSTANT_FLOW_RATE;
  236. emitterDesc.shape = NX_FE_ELLIPSE;
  237. mEmitter = mFluid->createEmitter(emitterDesc);
  238. }
  239. void PxFluid::_destroyFluid()
  240. {
  241. delete[] mParticles;
  242. mScene->releaseFluid( *mFluid );
  243. mEmitter = NULL;
  244. }
  245. ConsoleMethod( PxFluid, resetParticles, void, 2, 2, "" )
  246. {
  247. object->resetParticles();
  248. }
  249. ConsoleMethod( PxFluid, setRate, void, 2, 2, "" )
  250. {
  251. object->setRate( dAtof(argv[2]) );
  252. }