Scene.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _SCENE_H_
  23. #define _SCENE_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. #ifndef _VECTOR2_H_
  28. #include "2d/core/Vector2.h"
  29. #endif
  30. #ifndef _NETOBJECT_H_
  31. #include "network/netObject.h"
  32. #endif
  33. #ifndef _TICKABLE_H_
  34. #include "platform/Tickable.h"
  35. #endif
  36. #ifndef _PHYSICS_PROXY_H_
  37. #include "2d/scene/PhysicsProxy.h"
  38. #endif
  39. #ifndef _WORLD_QUERY_H_
  40. #include "2d/scene/WorldQuery.h"
  41. #endif
  42. #ifndef _DEBUG_DRAW_H_
  43. #include "2d/scene/DebugDraw.h"
  44. #endif
  45. #ifndef _HASHTABLE_H
  46. #include "collection/hashTable.h"
  47. #endif
  48. #ifndef _BATCH_RENDER_H_
  49. #include "2d/core/BatchRender.h"
  50. #endif
  51. #ifndef _SCENE_RENDER_QUEUE_H_
  52. #include "2d/scene/SceneRenderQueue.h"
  53. #endif
  54. #ifndef _SCENE_RENDER_OBJECT_H_
  55. #include "2d/scene/SceneRenderObject.h"
  56. #endif
  57. #ifndef _BEHAVIOR_COMPONENT_H_
  58. #include "component/behaviors/behaviorComponent.h"
  59. #endif
  60. #ifndef _ASSET_PTR_H_
  61. #include "assets/assetPtr.h"
  62. #endif
  63. //-----------------------------------------------------------------------------
  64. extern EnumTable jointTypeTable;
  65. ///-----------------------------------------------------------------------------
  66. class SceneObject;
  67. class SceneWindow;
  68. ///-----------------------------------------------------------------------------
  69. struct tDeleteRequest
  70. {
  71. SimObjectId mObjectId;
  72. SceneObject* mpSceneObject;
  73. bool mSafeDeleteReady;
  74. };
  75. ///-----------------------------------------------------------------------------
  76. struct TickContact
  77. {
  78. TickContact()
  79. {
  80. initialize( NULL, NULL, NULL, NULL, NULL );
  81. }
  82. void initialize(
  83. b2Contact* pContact,
  84. SceneObject* pSceneObjectA,
  85. SceneObject* pSceneObjectB,
  86. b2Fixture* pFixtureA,
  87. b2Fixture* pFixtureB )
  88. {
  89. mpContact = pContact;
  90. mpSceneObjectA = pSceneObjectA;
  91. mpSceneObjectB = pSceneObjectB;
  92. mpFixtureA = pFixtureA;
  93. mpFixtureB = pFixtureB;
  94. // Get world manifold.
  95. if ( mpContact != NULL )
  96. {
  97. mPointCount = pContact->GetManifold()->pointCount;
  98. mpContact->GetWorldManifold( &mWorldManifold );
  99. }
  100. else
  101. {
  102. mPointCount = 0;
  103. }
  104. // Reset impulses.
  105. for (U32 i = 0; i < b2_maxManifoldPoints; i++)
  106. {
  107. mNormalImpulses[i] = 0;
  108. mTangentImpulses[i] = 0;
  109. }
  110. }
  111. inline SceneObject* getCollideWith( SceneObject* pMe ) const
  112. {
  113. return pMe == mpSceneObjectA ? mpSceneObjectB : mpSceneObjectA;
  114. }
  115. inline b2Fixture* getCollideWithFixture( b2Fixture* pMe ) const
  116. {
  117. return pMe == mpFixtureA ? mpFixtureB : mpFixtureA;
  118. }
  119. b2Contact* mpContact;
  120. SceneObject* mpSceneObjectA;
  121. SceneObject* mpSceneObjectB;
  122. b2Fixture* mpFixtureA;
  123. b2Fixture* mpFixtureB;
  124. U32 mPointCount;
  125. b2WorldManifold mWorldManifold;
  126. F32 mNormalImpulses[b2_maxManifoldPoints];
  127. F32 mTangentImpulses[b2_maxManifoldPoints];
  128. };
  129. ///-----------------------------------------------------------------------------
  130. class Scene :
  131. public BehaviorComponent,
  132. public TamlChildren,
  133. public PhysicsProxy,
  134. public b2ContactListener,
  135. public b2DestructionListener,
  136. public virtual Tickable
  137. {
  138. public:
  139. typedef HashMap<S32, b2Joint*> typeJointHash;
  140. typedef HashMap<U32, S32> typeReverseJointHash;
  141. typedef Vector<tDeleteRequest> typeDeleteVector;
  142. typedef Vector<TickContact> typeContactVector;
  143. typedef HashMap<b2Contact*, TickContact> typeContactHash;
  144. typedef Vector<AssetPtr<AssetBase>*> typeAssetPtrVector;
  145. /// Scene Debug Options.
  146. enum DebugOption
  147. {
  148. SCENE_DEBUG_INVALID,
  149. ///
  150. SCENE_DEBUG_METRICS = BIT(0),
  151. SCENE_DEBUG_FPS_METRICS = BIT(1),
  152. SCENE_DEBUG_CONTROLLERS = BIT(2),
  153. SCENE_DEBUG_JOINTS = BIT(3),
  154. SCENE_DEBUG_WIREFRAME_RENDER = BIT(4),
  155. ///
  156. SCENE_DEBUG_AABB = BIT(16),
  157. SCENE_DEBUG_OOBB = BIT(17),
  158. SCENE_DEBUG_SLEEP = BIT(18),
  159. SCENE_DEBUG_COLLISION_SHAPES = BIT(19),
  160. SCENE_DEBUG_POSITION_AND_COM = BIT(20),
  161. SCENE_DEBUG_SORT_POINTS = BIT(21),
  162. };
  163. /// Pick mode.
  164. enum PickMode
  165. {
  166. PICK_INVALID,
  167. ///---
  168. PICK_ANY,
  169. PICK_SIZE,
  170. PICK_COLLISION,
  171. };
  172. /// Debug drawing.
  173. DebugDraw mDebugDraw;
  174. private:
  175. typedef BehaviorComponent Parent;
  176. typedef SceneObject Children;
  177. /// World.
  178. b2World* mpWorld;
  179. WorldQuery* mpWorldQuery;
  180. b2Vec2 mWorldGravity;
  181. S32 mVelocityIterations;
  182. S32 mPositionIterations;
  183. b2BlockAllocator mBlockAllocator;
  184. b2Body* mpGroundBody;
  185. /// Scene occupancy.
  186. typeSceneObjectVector mSceneObjects;
  187. typeSceneObjectVector mTickedSceneObjects;
  188. /// Joint access.
  189. typeJointHash mJoints;
  190. typeReverseJointHash mReverseJoints;
  191. S32 mJointMasterId;
  192. /// Scene controllers.
  193. SimObjectPtr<SimSet> mControllers;
  194. /// Asset pre-loads.
  195. typeAssetPtrVector mAssetPreloads;
  196. /// Scene time.
  197. F32 mSceneTime;
  198. bool mScenePause;
  199. /// Debug and metrics.
  200. DebugStats mDebugStats;
  201. U32 mDebugMask;
  202. SceneObject* mpDebugSceneObject;
  203. /// Layer sorting and draw order.
  204. SceneRenderQueue::RenderSort mLayerSortModes[MAX_LAYERS_SUPPORTED];
  205. /// Batch rendering.
  206. BatchRender mBatchRenderer;
  207. /// Window rendering.
  208. SceneWindow* mpCurrentRenderWindow;
  209. /// Window attachments.
  210. SimSet mAttachedSceneWindows;
  211. /// Delete requests.
  212. typeDeleteVector mDeleteRequests;
  213. typeDeleteVector mDeleteRequestsTemp;
  214. /// Miscellaneous.
  215. S32 mIsEditorScene;
  216. bool mUpdateCallback;
  217. bool mRenderCallback;
  218. typeContactHash mBeginContacts;
  219. typeContactVector mEndContacts;
  220. U32 mSceneIndex;
  221. private:
  222. /// Contacts.
  223. void forwardContacts( void );
  224. void dispatchBeginContactCallbacks( void );
  225. void dispatchEndContactCallbacks( void );
  226. /// Joint definition.
  227. struct CommonJointDefinition
  228. {
  229. CommonJointDefinition()
  230. {
  231. jointType = e_unknownJoint;
  232. collideConnected = false;
  233. pSceneObjectA = NULL;
  234. pSceneObjectB = NULL;
  235. localAnchorA = b2Vec2_zero;
  236. localAnchorB = b2Vec2_zero;
  237. }
  238. b2JointType jointType;
  239. bool collideConnected;
  240. SceneObject* pSceneObjectA;
  241. SceneObject* pSceneObjectB;
  242. b2Vec2 localAnchorA;
  243. b2Vec2 localAnchorB;
  244. };
  245. protected:
  246. /// Taml callbacks.
  247. virtual void onTamlPreRead( void );
  248. virtual void onTamlPostRead( const TamlCustomNodes& customNodes );
  249. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  250. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  251. public:
  252. Scene();
  253. virtual ~Scene();
  254. /// Engine.
  255. virtual bool onAdd();
  256. virtual void onRemove();
  257. virtual void onDeleteNotify( SimObject* object );
  258. static void initPersistFields();
  259. /// Contact processing.
  260. virtual void PreSolve( b2Contact* pContact, const b2Manifold* pOldManifold ) {}
  261. virtual void PostSolve( b2Contact* pContact, const b2ContactImpulse* pImpulse );
  262. virtual void BeginContact( b2Contact* pContact );
  263. virtual void EndContact( b2Contact* pContact );
  264. const typeContactHash& getBeginContacts( void ) const { return mBeginContacts; }
  265. const typeContactVector& getEndContacts( void ) const { return mEndContacts; }
  266. /// Integration.
  267. virtual void processTick();
  268. virtual void interpolateTick( F32 delta );
  269. virtual void advanceTime( F32 timeDelta ) {};
  270. /// Render output.
  271. void sceneRender( const SceneRenderState* pSceneRenderState );
  272. /// World.
  273. inline b2World* getWorld( void ) const { return mpWorld; }
  274. inline WorldQuery* getWorldQuery( const bool clearQuery = false ) { if ( clearQuery ) mpWorldQuery->clearQuery(); return mpWorldQuery; }
  275. b2BlockAllocator* getBlockAllocator( void ) { return &mBlockAllocator; }
  276. inline b2Body* getGroundBody( void ) const { return mpGroundBody; }
  277. virtual ePhysicsProxyType getPhysicsProxyType( void ) const { return PhysicsProxy::PHYSIC_PROXY_GROUNDBODY; }
  278. void setGravity( const b2Vec2& gravity ) { mWorldGravity = gravity; if (mpWorld) mpWorld->SetGravity( gravity ); }
  279. inline b2Vec2 getGravity( void ) { if (mpWorld) mWorldGravity = mpWorld->GetGravity(); return mWorldGravity; }
  280. inline void setVelocityIterations( const S32 iterations ) { mVelocityIterations = iterations; }
  281. inline S32 getVelocityIterations( void ) const { return mVelocityIterations; }
  282. inline void setPositionIterations( const S32 iterations ) { mPositionIterations = iterations; }
  283. inline S32 getPositionIterations( void ) const { return mPositionIterations; }
  284. /// Scene occupancy.
  285. void clearScene( bool deleteObjects = true );
  286. void addToScene( SceneObject* pSceneObject );
  287. void removeFromScene( SceneObject* pSceneObject );
  288. inline typeSceneObjectVectorConstRef getSceneObjects( void ) const { return mSceneObjects; }
  289. inline U32 getSceneObjectCount( void ) const { return mSceneObjects.size(); }
  290. SceneObject* getSceneObject( const U32 objectIndex ) const;
  291. U32 getSceneObjects( typeSceneObjectVector& objects ) const;
  292. U32 getSceneObjects( typeSceneObjectVector& objects, const U32 sceneLayer ) const;
  293. void mergeScene( const Scene* pScene );
  294. inline SimSet* getControllers( void ) { return mControllers; }
  295. inline S32 getAssetPreloadCount( void ) const { return mAssetPreloads.size(); }
  296. const AssetPtr<AssetBase>* getAssetPreload( const S32 index ) const;
  297. void addAssetPreload( const char* pAssetId );
  298. void removeAssetPreload( const char* pAssetId );
  299. void clearAssetPreloads( void );
  300. /// Scene time.
  301. inline F32 getSceneTime( void ) const { return mSceneTime; };
  302. inline void setScenePause( bool status ) { mScenePause = status; }
  303. inline bool getScenePause( void ) const { return mScenePause; };
  304. /// Joint access.
  305. inline U32 getJointCount( void ) const { return mJoints.size(); }
  306. b2JointType getJointType( const S32 jointId );
  307. b2Joint* findJoint( const S32 jointId );
  308. S32 findJointId( b2Joint* pJoint );
  309. S32 createJoint( b2JointDef* pJointDef );
  310. bool deleteJoint( const U32 jointId );
  311. bool hasJoints( SceneObject* pSceneObject );
  312. /// Distance joint.
  313. S32 createDistanceJoint(
  314. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  315. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  316. const F32 length = -1.0f,
  317. const F32 frequency = 0.0f,
  318. const F32 dampingRatio = 0.0f,
  319. const bool collideConnected = false );
  320. void setDistanceJointLength(
  321. const U32 jointId,
  322. const F32 length );
  323. F32 getDistanceJointLength( const U32 jointId );
  324. void setDistanceJointFrequency(
  325. const U32 jointId,
  326. const F32 frequency );
  327. F32 getDistanceJointFrequency( const U32 jointId );
  328. void setDistanceJointDampingRatio(
  329. const U32 jointId,
  330. const F32 dampingRatio );
  331. F32 getDistanceJointDampingRatio( const U32 jointId );
  332. /// Rope joint.
  333. S32 createRopeJoint(
  334. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  335. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  336. const F32 maxLength = -1.0f,
  337. const bool collideConnected = false );
  338. void setRopeJointMaxLength(
  339. const U32 jointId,
  340. const F32 maxLength );
  341. F32 getRopeJointMaxLength( const U32 jointId );
  342. /// Revolute joint.
  343. S32 createRevoluteJoint(
  344. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  345. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  346. const bool collideConnected = false );
  347. void setRevoluteJointLimit(
  348. const U32 jointId,
  349. const bool enableLimit,
  350. const F32 lowerAngle, const F32 upperAngle );
  351. bool getRevoluteJointLimit(
  352. const U32 jointId,
  353. bool& enableLimit,
  354. F32& lowerAngle, F32& upperAngle );
  355. void setRevoluteJointMotor(
  356. const U32 jointId,
  357. const bool enableMotor,
  358. const F32 motorSpeed = b2_pi,
  359. const F32 maxMotorTorque = 0.0f );
  360. bool getRevoluteJointMotor(
  361. const U32 jointId,
  362. bool& enableMotor,
  363. F32& motorSpeed,
  364. F32& maxMotorTorque );
  365. /// Weld joint.
  366. S32 createWeldJoint(
  367. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  368. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  369. const F32 frequency = 0.0f,
  370. const F32 dampingRatio = 0.0f,
  371. const bool collideConnected = false );
  372. void setWeldJointFrequency(
  373. const U32 jointId,
  374. const F32 frequency );
  375. F32 getWeldJointFrequency( const U32 jointId );
  376. void setWeldJointDampingRatio(
  377. const U32 jointId,
  378. const F32 dampingRatio );
  379. F32 getWeldJointDampingRatio( const U32 jointId );
  380. /// Wheel joint.
  381. S32 createWheelJoint(
  382. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  383. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  384. const b2Vec2& worldAxis,
  385. const bool collideConnected = false );
  386. void setWheelJointMotor(
  387. const U32 jointId,
  388. const bool enableMotor,
  389. const F32 motorSpeed = b2_pi,
  390. const F32 maxMotorTorque = 0.0f );
  391. bool getWheelJointMotor(
  392. const U32 jointId,
  393. bool& enableMotor,
  394. F32& motorSpeed,
  395. F32& maxMotorTorque );
  396. void setWheelJointFrequency(
  397. const U32 jointId,
  398. const F32 frequency );
  399. F32 getWheelJointFrequency( const U32 jointId );
  400. void setWheelJointDampingRatio(
  401. const U32 jointId,
  402. const F32 dampingRatio );
  403. F32 getWheelJointDampingRatio( const U32 jointId );
  404. /// Friction joint.
  405. S32 createFrictionJoint(
  406. const SceneObject* pSceneObjectA,const SceneObject* pSceneObjectB,
  407. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  408. const F32 maxForce = 0.0f,
  409. const F32 maxTorque = 0.0f,
  410. const bool collideConnected = false );
  411. void setFrictionJointMaxForce(
  412. const U32 jointId,
  413. const F32 maxForce );
  414. F32 getFrictionJointMaxForce( const U32 jointId );
  415. void setFrictionJointMaxTorque(
  416. const U32 jointId,
  417. const F32 maxTorque );
  418. F32 getFrictionJointMaxTorque( const U32 jointId );
  419. /// Prismatic joint.
  420. S32 createPrismaticJoint(
  421. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  422. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  423. const b2Vec2& worldAxis,
  424. const bool collideConnected = false );
  425. void setPrismaticJointLimit(
  426. const U32 jointId,
  427. const bool enableLimit,
  428. const F32 lowerTranslation, const F32 upperTranslation );
  429. bool getPrismaticJointLimit(
  430. const U32 jointId,
  431. bool& enableLimit,
  432. F32& lowerTranslation, F32& upperTranslation );
  433. void setPrismaticJointMotor(
  434. const U32 jointId,
  435. const bool enableMotor,
  436. const F32 motorSpeed = b2_pi,
  437. const F32 maxMotorForce = 0.0f );
  438. bool getPrismaticJointMotor(
  439. const U32 jointId,
  440. bool& enableMotor,
  441. F32& motorSpeed,
  442. F32& maxMotorTorque );
  443. /// Pulley joint.
  444. S32 createPulleyJoint(
  445. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  446. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  447. const b2Vec2& worldGroundAnchorA, const b2Vec2& worldGroundAnchorB,
  448. const F32 ratio,
  449. const F32 lengthA = -1.0f, const F32 lengthB = -1.0f,
  450. const bool collideConnected = false );
  451. /// Target (a.k.a Mouse) joint.
  452. S32 createTargetJoint(
  453. const SceneObject* pSceneObject,
  454. const b2Vec2& worldTarget,
  455. const F32 maxForce,
  456. const bool useCenterOfMass = false,
  457. const F32 frequency = 5.0f,
  458. const F32 dampingRatio = 0.7f,
  459. const bool collideConnected = false );
  460. void setTargetJointTarget(
  461. const U32 jointId,
  462. const b2Vec2& worldTarget );
  463. b2Vec2 getTargetJointTarget( const U32 jointId );
  464. void setTargetJointMaxForce(
  465. const U32 jointId,
  466. const F32 maxForce );
  467. F32 getTargetJointMaxForce( const U32 jointId );
  468. void setTargetJointFrequency(
  469. const U32 jointId,
  470. const F32 frequency );
  471. F32 getTargetJointFrequency( const U32 jointId );
  472. void setTargetJointDampingRatio(
  473. const U32 jointId,
  474. const F32 dampingRatio );
  475. F32 getTargetJointDampingRatio( const U32 jointId );
  476. /// Motor Joint.
  477. S32 createMotorJoint(
  478. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  479. const b2Vec2 linearOffset = b2Vec2_zero,
  480. const F32 angularOffset = 0.0f,
  481. const F32 maxForce = 1.0f,
  482. const F32 maxTorque = 1.0f,
  483. const F32 correctionFactor = 0.3f,
  484. const bool collideConnected = false );
  485. void setMotorJointLinearOffset(
  486. const U32 jointId,
  487. const b2Vec2& linearOffset );
  488. b2Vec2 getMotorJointLinearOffset( const U32 jointId );
  489. void setMotorJointAngularOffset(
  490. const U32 jointId,
  491. const F32 angularOffset );
  492. F32 getMotorJointAngularOffset( const U32 jointId );
  493. void setMotorJointMaxForce(
  494. const U32 jointId,
  495. const F32 maxForce );
  496. F32 getMotorJointMaxForce( const U32 jointId );
  497. void setMotorJointMaxTorque(
  498. const U32 jointId,
  499. const F32 maxTorque );
  500. F32 getMotorJointMaxTorque( const U32 jointId );
  501. /// Debug and metrics.
  502. inline void setDebugOn( const U32 debugMask ) { mDebugMask |= debugMask; }
  503. inline void setDebugOff( const U32 debugMask ) { mDebugMask &= ~debugMask; }
  504. inline U32 getDebugMask( void ) const { return mDebugMask; }
  505. DebugStats& getDebugStats( void ) { return mDebugStats; }
  506. inline void resetDebugStats( void ) { mDebugStats.reset(); }
  507. void setDebugSceneObject( SceneObject* pSceneObject );
  508. inline SceneObject* getDebugSceneObject( void ) const { return mpDebugSceneObject; }
  509. /// Layer sorting.
  510. void setLayerSortMode( const U32 layer, const SceneRenderQueue::RenderSort sortMode );
  511. SceneRenderQueue::RenderSort getLayerSortMode( const U32 layer );
  512. /// Window attachments.
  513. void attachSceneWindow( SceneWindow* pSceneWindow2D );
  514. void detachSceneWindow( SceneWindow* pSceneWindow2D );
  515. void detachAllSceneWindows( void );
  516. bool isSceneWindowAttached( SceneWindow* pSceneWindow2D );
  517. inline SimSet& getAttachedSceneWindows( void ) { return mAttachedSceneWindows; }
  518. /// Delete requests.
  519. void addDeleteRequest( SceneObject* pSceneObject );
  520. void processDeleteRequests( const bool forceImmediate );
  521. /// Destruction listeners.
  522. virtual void SayGoodbye( b2Joint* pJoint );
  523. virtual void SayGoodbye( b2Fixture* pFixture ) {}
  524. virtual SceneObject* create( const char* pType );
  525. /// Miscellaneous.
  526. inline void setBatchingEnabled( const bool enabled ) { mBatchRenderer.setBatchEnabled( enabled ); }
  527. inline bool getBatchingEnabled( void ) const { return mBatchRenderer.getBatchEnabled(); }
  528. inline bool getIsEditorScene( void ) const { return ((mIsEditorScene > 0) ? true : false); }
  529. inline void setIsEditorScene( bool status ) { mIsEditorScene += (status ? 1 : -1); }
  530. static U32 getGlobalSceneCount( void );
  531. inline U32 getSceneIndex( void ) const { return mSceneIndex; }
  532. inline void setUpdateCallback( const bool callback ) { mUpdateCallback = callback; }
  533. inline bool getUpdateCallback( void ) const { return mUpdateCallback; }
  534. inline void setRenderCallback( const bool callback ) { mRenderCallback = callback; }
  535. inline bool getRenderCallback( void ) const { return mRenderCallback; }
  536. static SceneRenderRequest* createDefaultRenderRequest( SceneRenderQueue* pSceneRenderQueue, SceneObject* pSceneObject );
  537. /// Taml children.
  538. virtual U32 getTamlChildCount( void ) const { return (U32)mSceneObjects.size(); }
  539. virtual SimObject* getTamlChild( const U32 childIndex ) const;
  540. virtual void addTamlChild( SimObject* pSimObject );
  541. static b2JointType getJointTypeEnum(const char* label);
  542. static const char* getJointTypeDescription( b2JointType jointType );
  543. static PickMode getPickModeEnum(const char* label);
  544. static const char* getPickModeDescription( PickMode pickMode );
  545. static DebugOption getDebugOptionEnum(const char* label);
  546. static const char* getDebugOptionDescription( DebugOption debugOption );
  547. /// Declare Console Object.
  548. DECLARE_CONOBJECT(Scene);
  549. protected:
  550. /// Physics.
  551. static bool setGravity( void* obj, const char* data ) { static_cast<Scene*>(obj)->setGravity( Vector2( data ) ); return false; }
  552. static const char* getGravity(void* obj, const char* data) { return Vector2(static_cast<Scene*>(obj)->getGravity()).scriptThis(); }
  553. static bool writeGravity( void* obj, StringTableEntry pFieldName ) { return Vector2(static_cast<Scene*>(obj)->getGravity()).notEqual( Vector2::getZero() ); }
  554. static bool writeVelocityIterations( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getVelocityIterations() != 8; }
  555. static bool writePositionIterations( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getPositionIterations() != 3; }
  556. static bool writeLayerSortMode( void* obj, StringTableEntry pFieldName )
  557. {
  558. // Find the layer index portion of the layer sort mode field.
  559. const char* pLayerNumber = pFieldName;
  560. while( true )
  561. {
  562. // Fetch character.
  563. char value = *pLayerNumber;
  564. // Finish if end of the field or is numeric.
  565. if ( value == 0 || ( value >= '0' && value <= '9' ) )
  566. break;
  567. // Move to next value.
  568. pLayerNumber++;
  569. };
  570. // Sanity!
  571. AssertFatal( *pLayerNumber != 0, "Scene::writeLayerSortMode() - Could not find the layer index portion of the layer sort mode field." );
  572. // Fetch layer number.
  573. const U32 layer = dAtoi(pLayerNumber);
  574. // Just allow the write if an bad parse.
  575. if ( layer > MAX_LAYERS_SUPPORTED )
  576. return true;
  577. return static_cast<Scene*>(obj)->getLayerSortMode( layer ) != SceneRenderQueue::RENDER_SORT_NEWEST;
  578. }
  579. // Callbacks.
  580. static bool writeUpdateCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getUpdateCallback(); }
  581. static bool writeRenderCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getRenderCallback(); }
  582. public:
  583. static SimObjectPtr<Scene> LoadingScene;
  584. };
  585. //-----------------------------------------------------------------------------
  586. extern void findObjectsCallback(SceneObject* pSceneObject, void* storage);
  587. extern void findLayeredObjectsCallback(SceneObject* pSceneObject, void* storage);
  588. #endif // _SCENE_H_