Scene.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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<b2Joint*, 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_AABB,
  170. PICK_OOBB,
  171. PICK_COLLISION,
  172. };
  173. /// Debug drawing.
  174. DebugDraw mDebugDraw;
  175. private:
  176. typedef BehaviorComponent Parent;
  177. typedef SceneObject Children;
  178. /// World.
  179. b2World* mpWorld;
  180. WorldQuery* mpWorldQuery;
  181. b2Vec2 mWorldGravity;
  182. S32 mVelocityIterations;
  183. S32 mPositionIterations;
  184. b2BlockAllocator mBlockAllocator;
  185. b2Body* mpGroundBody;
  186. /// Scene occupancy.
  187. typeSceneObjectVector mSceneObjects;
  188. typeSceneObjectVector mTickedSceneObjects;
  189. /// Joint access.
  190. typeJointHash mJoints;
  191. typeReverseJointHash mReverseJoints;
  192. S32 mJointMasterId;
  193. /// Scene controllers.
  194. SimObjectPtr<SimSet> mControllers;
  195. /// Asset pre-loads.
  196. typeAssetPtrVector mAssetPreloads;
  197. /// Scene time.
  198. F32 mSceneTime;
  199. bool mScenePause;
  200. /// Debug and metrics.
  201. DebugStats mDebugStats;
  202. U32 mDebugMask;
  203. SceneObject* mpDebugSceneObject;
  204. /// Layer sorting and draw order.
  205. SceneRenderQueue::RenderSort mLayerSortModes[MAX_LAYERS_SUPPORTED];
  206. /// Batch rendering.
  207. BatchRender mBatchRenderer;
  208. /// Window rendering.
  209. SceneWindow* mpCurrentRenderWindow;
  210. /// Window attachments.
  211. SimSet mAttachedSceneWindows;
  212. /// Delete requests.
  213. typeDeleteVector mDeleteRequests;
  214. typeDeleteVector mDeleteRequestsTemp;
  215. /// Miscellaneous.
  216. S32 mIsEditorScene;
  217. bool mUpdateCallback;
  218. bool mRenderCallback;
  219. typeContactHash mBeginContacts;
  220. typeContactVector mEndContacts;
  221. U32 mSceneIndex;
  222. private:
  223. /// Contacts.
  224. void forwardContacts( void );
  225. void dispatchBeginContactCallbacks( void );
  226. void dispatchEndContactCallbacks( void );
  227. /// Joint definition.
  228. struct CommonJointDefinition
  229. {
  230. CommonJointDefinition()
  231. {
  232. jointType = e_unknownJoint;
  233. collideConnected = false;
  234. pSceneObjectA = NULL;
  235. pSceneObjectB = NULL;
  236. localAnchorA = b2Vec2_zero;
  237. localAnchorB = b2Vec2_zero;
  238. }
  239. b2JointType jointType;
  240. bool collideConnected;
  241. SceneObject* pSceneObjectA;
  242. SceneObject* pSceneObjectB;
  243. b2Vec2 localAnchorA;
  244. b2Vec2 localAnchorB;
  245. };
  246. protected:
  247. /// Taml callbacks.
  248. virtual void onTamlPreRead( void );
  249. virtual void onTamlPostRead( const TamlCustomNodes& customNodes );
  250. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  251. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  252. public:
  253. Scene();
  254. virtual ~Scene();
  255. /// Engine.
  256. virtual bool onAdd();
  257. virtual void onRemove();
  258. virtual void onDeleteNotify( SimObject* object );
  259. static void initPersistFields();
  260. /// Contact processing.
  261. virtual void PreSolve( b2Contact* pContact, const b2Manifold* pOldManifold ) {}
  262. virtual void PostSolve( b2Contact* pContact, const b2ContactImpulse* pImpulse );
  263. virtual void BeginContact( b2Contact* pContact );
  264. virtual void EndContact( b2Contact* pContact );
  265. const typeContactHash& getBeginContacts( void ) const { return mBeginContacts; }
  266. const typeContactVector& getEndContacts( void ) const { return mEndContacts; }
  267. /// Integration.
  268. virtual void processTick();
  269. virtual void interpolateTick( F32 delta );
  270. virtual void advanceTime( F32 timeDelta ) {};
  271. /// Render output.
  272. void sceneRender( const SceneRenderState* pSceneRenderState );
  273. /// World.
  274. inline b2World* getWorld( void ) const { return mpWorld; }
  275. inline WorldQuery* getWorldQuery( const bool clearQuery = false ) { if ( clearQuery ) mpWorldQuery->clearQuery(); return mpWorldQuery; }
  276. b2BlockAllocator* getBlockAllocator( void ) { return &mBlockAllocator; }
  277. inline b2Body* getGroundBody( void ) const { return mpGroundBody; }
  278. virtual ePhysicsProxyType getPhysicsProxyType( void ) const { return PhysicsProxy::PHYSIC_PROXY_GROUNDBODY; }
  279. void setGravity( const b2Vec2& gravity ) { mWorldGravity = gravity; if (mpWorld) mpWorld->SetGravity( gravity ); }
  280. inline b2Vec2 getGravity( void ) { if (mpWorld) mWorldGravity = mpWorld->GetGravity(); return mWorldGravity; }
  281. inline void setVelocityIterations( const S32 iterations ) { mVelocityIterations = iterations; }
  282. inline S32 getVelocityIterations( void ) const { return mVelocityIterations; }
  283. inline void setPositionIterations( const S32 iterations ) { mPositionIterations = iterations; }
  284. inline S32 getPositionIterations( void ) const { return mPositionIterations; }
  285. /// Scene occupancy.
  286. void clearScene( bool deleteObjects = true );
  287. void addToScene( SceneObject* pSceneObject );
  288. void removeFromScene( SceneObject* pSceneObject );
  289. inline typeSceneObjectVectorConstRef getSceneObjects( void ) const { return mSceneObjects; }
  290. inline U32 getSceneObjectCount( void ) const { return mSceneObjects.size(); }
  291. SceneObject* getSceneObject( const U32 objectIndex ) const;
  292. U32 getSceneObjects( typeSceneObjectVector& objects ) const;
  293. U32 getSceneObjects( typeSceneObjectVector& objects, const U32 sceneLayer ) const;
  294. void mergeScene( const Scene* pScene );
  295. inline SimSet* getControllers( void ) { return mControllers; }
  296. inline S32 getAssetPreloadCount( void ) const { return mAssetPreloads.size(); }
  297. const AssetPtr<AssetBase>* getAssetPreload( const S32 index ) const;
  298. void addAssetPreload( const char* pAssetId );
  299. void removeAssetPreload( const char* pAssetId );
  300. void clearAssetPreloads( void );
  301. /// Scene time.
  302. inline F32 getSceneTime( void ) const { return mSceneTime; };
  303. inline void setScenePause( bool status ) { mScenePause = status; }
  304. inline bool getScenePause( void ) const { return mScenePause; };
  305. /// Joint access.
  306. inline U32 getJointCount( void ) const { return mJoints.size(); }
  307. b2JointType getJointType( const S32 jointId );
  308. b2Joint* findJoint( const S32 jointId );
  309. S32 findJointId( b2Joint* pJoint );
  310. S32 createJoint( b2JointDef* pJointDef );
  311. bool deleteJoint( const U32 jointId );
  312. bool hasJoints( SceneObject* pSceneObject );
  313. /// Distance joint.
  314. S32 createDistanceJoint(
  315. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  316. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  317. const F32 length = -1.0f,
  318. const F32 frequency = 0.0f,
  319. const F32 dampingRatio = 0.0f,
  320. const bool collideConnected = false );
  321. void setDistanceJointLength(
  322. const U32 jointId,
  323. const F32 length );
  324. F32 getDistanceJointLength( const U32 jointId );
  325. void setDistanceJointFrequency(
  326. const U32 jointId,
  327. const F32 frequency );
  328. F32 getDistanceJointFrequency( const U32 jointId );
  329. void setDistanceJointDampingRatio(
  330. const U32 jointId,
  331. const F32 dampingRatio );
  332. F32 getDistanceJointDampingRatio( const U32 jointId );
  333. /// Rope joint.
  334. S32 createRopeJoint(
  335. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  336. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  337. const F32 maxLength = -1.0f,
  338. const bool collideConnected = false );
  339. void setRopeJointMaxLength(
  340. const U32 jointId,
  341. const F32 maxLength );
  342. F32 getRopeJointMaxLength( const U32 jointId );
  343. /// Revolute joint.
  344. S32 createRevoluteJoint(
  345. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  346. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  347. const bool collideConnected = false );
  348. void setRevoluteJointLimit(
  349. const U32 jointId,
  350. const bool enableLimit,
  351. const F32 lowerAngle, const F32 upperAngle );
  352. bool getRevoluteJointLimit(
  353. const U32 jointId,
  354. bool& enableLimit,
  355. F32& lowerAngle, F32& upperAngle );
  356. void setRevoluteJointMotor(
  357. const U32 jointId,
  358. const bool enableMotor,
  359. const F32 motorSpeed = b2_pi,
  360. const F32 maxMotorTorque = 0.0f );
  361. bool getRevoluteJointMotor(
  362. const U32 jointId,
  363. bool& enableMotor,
  364. F32& motorSpeed,
  365. F32& maxMotorTorque );
  366. F32 getRevoluteJointAngle( const U32 jointId );
  367. F32 getRevoluteJointSpeed( const U32 jointId );
  368. /// Weld joint.
  369. S32 createWeldJoint(
  370. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  371. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  372. const F32 frequency = 0.0f,
  373. const F32 dampingRatio = 0.0f,
  374. const bool collideConnected = false );
  375. void setWeldJointFrequency(
  376. const U32 jointId,
  377. const F32 frequency );
  378. F32 getWeldJointFrequency( const U32 jointId );
  379. void setWeldJointDampingRatio(
  380. const U32 jointId,
  381. const F32 dampingRatio );
  382. F32 getWeldJointDampingRatio( const U32 jointId );
  383. /// Wheel joint.
  384. S32 createWheelJoint(
  385. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  386. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  387. const b2Vec2& worldAxis,
  388. const bool collideConnected = false );
  389. void setWheelJointMotor(
  390. const U32 jointId,
  391. const bool enableMotor,
  392. const F32 motorSpeed = b2_pi,
  393. const F32 maxMotorTorque = 0.0f );
  394. bool getWheelJointMotor(
  395. const U32 jointId,
  396. bool& enableMotor,
  397. F32& motorSpeed,
  398. F32& maxMotorTorque );
  399. void setWheelJointFrequency(
  400. const U32 jointId,
  401. const F32 frequency );
  402. F32 getWheelJointFrequency( const U32 jointId );
  403. void setWheelJointDampingRatio(
  404. const U32 jointId,
  405. const F32 dampingRatio );
  406. F32 getWheelJointDampingRatio( const U32 jointId );
  407. /// Friction joint.
  408. S32 createFrictionJoint(
  409. const SceneObject* pSceneObjectA,const SceneObject* pSceneObjectB,
  410. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  411. const F32 maxForce = 0.0f,
  412. const F32 maxTorque = 0.0f,
  413. const bool collideConnected = false );
  414. void setFrictionJointMaxForce(
  415. const U32 jointId,
  416. const F32 maxForce );
  417. F32 getFrictionJointMaxForce( const U32 jointId );
  418. void setFrictionJointMaxTorque(
  419. const U32 jointId,
  420. const F32 maxTorque );
  421. F32 getFrictionJointMaxTorque( const U32 jointId );
  422. /// Prismatic joint.
  423. S32 createPrismaticJoint(
  424. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  425. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  426. const b2Vec2& worldAxis,
  427. const bool collideConnected = false );
  428. void setPrismaticJointLimit(
  429. const U32 jointId,
  430. const bool enableLimit,
  431. const F32 lowerTranslation, const F32 upperTranslation );
  432. bool getPrismaticJointLimit(
  433. const U32 jointId,
  434. bool& enableLimit,
  435. F32& lowerTranslation, F32& upperTranslation );
  436. void setPrismaticJointMotor(
  437. const U32 jointId,
  438. const bool enableMotor,
  439. const F32 motorSpeed = b2_pi,
  440. const F32 maxMotorForce = 0.0f );
  441. bool getPrismaticJointMotor(
  442. const U32 jointId,
  443. bool& enableMotor,
  444. F32& motorSpeed,
  445. F32& maxMotorTorque );
  446. /// Pulley joint.
  447. S32 createPulleyJoint(
  448. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  449. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  450. const b2Vec2& worldGroundAnchorA, const b2Vec2& worldGroundAnchorB,
  451. const F32 ratio,
  452. const F32 lengthA = -1.0f, const F32 lengthB = -1.0f,
  453. const bool collideConnected = false );
  454. /// Target (a.k.a Mouse) joint.
  455. S32 createTargetJoint(
  456. const SceneObject* pSceneObject,
  457. const b2Vec2& worldTarget,
  458. const F32 maxForce,
  459. const bool useCenterOfMass = false,
  460. const F32 frequency = 5.0f,
  461. const F32 dampingRatio = 0.7f,
  462. const bool collideConnected = false );
  463. void setTargetJointTarget(
  464. const U32 jointId,
  465. const b2Vec2& worldTarget );
  466. b2Vec2 getTargetJointTarget( const U32 jointId );
  467. void setTargetJointMaxForce(
  468. const U32 jointId,
  469. const F32 maxForce );
  470. F32 getTargetJointMaxForce( const U32 jointId );
  471. void setTargetJointFrequency(
  472. const U32 jointId,
  473. const F32 frequency );
  474. F32 getTargetJointFrequency( const U32 jointId );
  475. void setTargetJointDampingRatio(
  476. const U32 jointId,
  477. const F32 dampingRatio );
  478. F32 getTargetJointDampingRatio( const U32 jointId );
  479. /// Motor Joint.
  480. S32 createMotorJoint(
  481. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  482. const b2Vec2 linearOffset = b2Vec2_zero,
  483. const F32 angularOffset = 0.0f,
  484. const F32 maxForce = 1.0f,
  485. const F32 maxTorque = 1.0f,
  486. const F32 correctionFactor = 0.3f,
  487. const bool collideConnected = false );
  488. void setMotorJointLinearOffset(
  489. const U32 jointId,
  490. const b2Vec2& linearOffset );
  491. b2Vec2 getMotorJointLinearOffset( const U32 jointId );
  492. void setMotorJointAngularOffset(
  493. const U32 jointId,
  494. const F32 angularOffset );
  495. F32 getMotorJointAngularOffset( const U32 jointId );
  496. void setMotorJointMaxForce(
  497. const U32 jointId,
  498. const F32 maxForce );
  499. F32 getMotorJointMaxForce( const U32 jointId );
  500. void setMotorJointMaxTorque(
  501. const U32 jointId,
  502. const F32 maxTorque );
  503. F32 getMotorJointMaxTorque( const U32 jointId );
  504. /// Debug and metrics.
  505. inline void setDebugOn( const U32 debugMask ) { mDebugMask |= debugMask; }
  506. inline void setDebugOff( const U32 debugMask ) { mDebugMask &= ~debugMask; }
  507. inline U32 getDebugMask( void ) const { return mDebugMask; }
  508. DebugStats& getDebugStats( void ) { return mDebugStats; }
  509. inline void resetDebugStats( void ) { mDebugStats.reset(); }
  510. void setDebugSceneObject( SceneObject* pSceneObject );
  511. inline SceneObject* getDebugSceneObject( void ) const { return mpDebugSceneObject; }
  512. /// Layer sorting.
  513. void setLayerSortMode( const U32 layer, const SceneRenderQueue::RenderSort sortMode );
  514. SceneRenderQueue::RenderSort getLayerSortMode( const U32 layer );
  515. /// Window attachments.
  516. void attachSceneWindow( SceneWindow* pSceneWindow2D );
  517. void detachSceneWindow( SceneWindow* pSceneWindow2D );
  518. void detachAllSceneWindows( void );
  519. bool isSceneWindowAttached( SceneWindow* pSceneWindow2D );
  520. inline SimSet& getAttachedSceneWindows( void ) { return mAttachedSceneWindows; }
  521. /// Delete requests.
  522. void addDeleteRequest( SceneObject* pSceneObject );
  523. void processDeleteRequests( const bool forceImmediate );
  524. /// Destruction listeners.
  525. virtual void SayGoodbye( b2Joint* pJoint );
  526. virtual void SayGoodbye( b2Fixture* pFixture ) {}
  527. virtual SceneObject* create( const char* pType );
  528. /// Miscellaneous.
  529. inline void setBatchingEnabled( const bool enabled ) { mBatchRenderer.setBatchEnabled( enabled ); }
  530. inline bool getBatchingEnabled( void ) const { return mBatchRenderer.getBatchEnabled(); }
  531. inline bool getIsEditorScene( void ) const { return ((mIsEditorScene > 0) ? true : false); }
  532. inline void setIsEditorScene( bool status ) { mIsEditorScene += (status ? 1 : -1); }
  533. static U32 getGlobalSceneCount( void );
  534. inline U32 getSceneIndex( void ) const { return mSceneIndex; }
  535. inline void setUpdateCallback( const bool callback ) { mUpdateCallback = callback; }
  536. inline bool getUpdateCallback( void ) const { return mUpdateCallback; }
  537. inline void setRenderCallback( const bool callback ) { mRenderCallback = callback; }
  538. inline bool getRenderCallback( void ) const { return mRenderCallback; }
  539. static SceneRenderRequest* createDefaultRenderRequest( SceneRenderQueue* pSceneRenderQueue, SceneObject* pSceneObject );
  540. /// Taml children.
  541. virtual U32 getTamlChildCount( void ) const { return (U32)mSceneObjects.size(); }
  542. virtual SimObject* getTamlChild( const U32 childIndex ) const;
  543. virtual void addTamlChild( SimObject* pSimObject );
  544. static b2JointType getJointTypeEnum(const char* label);
  545. static const char* getJointTypeDescription( b2JointType jointType );
  546. static PickMode getPickModeEnum(const char* label);
  547. static const char* getPickModeDescription( PickMode pickMode );
  548. static DebugOption getDebugOptionEnum(const char* label);
  549. static const char* getDebugOptionDescription( DebugOption debugOption );
  550. /// Declare Console Object.
  551. DECLARE_CONOBJECT(Scene);
  552. protected:
  553. /// Physics.
  554. static bool setGravity( void* obj, const char* data ) { static_cast<Scene*>(obj)->setGravity( Vector2( data ) ); return false; }
  555. static const char* getGravity(void* obj, const char* data) { return Vector2(static_cast<Scene*>(obj)->getGravity()).scriptThis(); }
  556. static bool writeGravity( void* obj, StringTableEntry pFieldName ) { return Vector2(static_cast<Scene*>(obj)->getGravity()).notEqual( Vector2::getZero() ); }
  557. static bool writeVelocityIterations( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getVelocityIterations() != 8; }
  558. static bool writePositionIterations( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getPositionIterations() != 3; }
  559. static bool writeLayerSortMode( void* obj, StringTableEntry pFieldName )
  560. {
  561. // Find the layer index portion of the layer sort mode field.
  562. const char* pLayerNumber = pFieldName;
  563. while( true )
  564. {
  565. // Fetch character.
  566. char value = *pLayerNumber;
  567. // Finish if end of the field or is numeric.
  568. if ( value == 0 || ( value >= '0' && value <= '9' ) )
  569. break;
  570. // Move to next value.
  571. pLayerNumber++;
  572. };
  573. // Sanity!
  574. AssertFatal( *pLayerNumber != 0, "Scene::writeLayerSortMode() - Could not find the layer index portion of the layer sort mode field." );
  575. // Fetch layer number.
  576. const U32 layer = dAtoi(pLayerNumber);
  577. // Just allow the write if an bad parse.
  578. if ( layer > MAX_LAYERS_SUPPORTED )
  579. return true;
  580. return static_cast<Scene*>(obj)->getLayerSortMode( layer ) != SceneRenderQueue::RENDER_SORT_NEWEST;
  581. }
  582. // Callbacks.
  583. static bool writeUpdateCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getUpdateCallback(); }
  584. static bool writeRenderCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getRenderCallback(); }
  585. public:
  586. static SimObjectPtr<Scene> LoadingScene;
  587. };
  588. //-----------------------------------------------------------------------------
  589. extern void findObjectsCallback(SceneObject* pSceneObject, void* storage);
  590. extern void findLayeredObjectsCallback(SceneObject* pSceneObject, void* storage);
  591. #endif // _SCENE_H_