sceneObject.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _SCENEOBJECT_H_
  27. #define _SCENEOBJECT_H_
  28. #ifndef _NETOBJECT_H_
  29. #include "sim/netObject.h"
  30. #endif
  31. #ifndef _COLLISION_H_
  32. #include "collision/collision.h"
  33. #endif
  34. #ifndef _OBJECTTYPES_H_
  35. #include "T3D/objectTypes.h"
  36. #endif
  37. #ifndef _COLOR_H_
  38. #include "core/color.h"
  39. #endif
  40. #ifndef _BITSET_H_
  41. #include "core/bitSet.h"
  42. #endif
  43. #ifndef _PROCESSLIST_H_
  44. #include "T3D/gameBase/processList.h"
  45. #endif
  46. #ifndef _SCENECONTAINER_H_
  47. #include "scene/sceneContainer.h"
  48. #endif
  49. #ifndef _GFXDEVICE_H_
  50. #include "gfx/gfxDevice.h"
  51. #endif
  52. #ifndef _TSRENDERDATA_H_
  53. #include "ts/tsRenderState.h"
  54. #endif
  55. #ifndef _COLLADA_UTILS_H_
  56. #include "ts/collada/colladaUtils.h"
  57. #endif
  58. #ifndef _ASSET_PTR_H_
  59. #include "assets/assetPtr.h"
  60. #endif
  61. #ifndef GAME_OBJECT_ASSET_H
  62. #include "T3D/assets/GameObjectAsset.h"
  63. #endif
  64. class SceneManager;
  65. class SceneRenderState;
  66. class SceneTraversalState;
  67. class SceneCameraState;
  68. class SceneObjectLink;
  69. class SceneObjectLightingPlugin;
  70. class Convex;
  71. class LightInfo;
  72. class SFXAmbience;
  73. struct ObjectRenderInst;
  74. struct Move;
  75. /// A 3D object.
  76. ///
  77. /// @section SceneObject_intro Introduction
  78. ///
  79. /// SceneObject exists as a foundation for 3D objects in Torque. It provides the
  80. /// basic functionality for:
  81. /// - A scene graph (in the Zones and Portals sections), allowing efficient
  82. /// and robust rendering of the game scene.
  83. /// - Various helper functions, including functions to get bounding information
  84. /// and momentum/velocity.
  85. /// - Collision detection, as well as ray casting.
  86. /// - Lighting. SceneObjects can register lights both at lightmap generation time,
  87. /// and dynamic lights at runtime (for special effects, such as from flame or
  88. /// a projectile, or from an explosion).
  89. /// - Manipulating scene objects, for instance varying scale.
  90. ///
  91. /// @section SceneObject_example An Example
  92. ///
  93. /// Melv May has written a most marvelous example object deriving from SceneObject.
  94. /// Unfortunately this page is too small to contain it.
  95. ///
  96. /// @see http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=3217
  97. /// for a copy of Melv's example.
  98. class SceneObject : public NetObject, private SceneContainer::Link, public ProcessObject
  99. {
  100. public:
  101. typedef NetObject Parent;
  102. friend class SceneManager;
  103. friend class SceneContainer;
  104. friend class SceneZoneSpaceManager;
  105. friend class SceneCullingState; // _getZoneRefHead
  106. friend class SceneObjectLink; // mSceneObjectLinks
  107. enum
  108. {
  109. /// Maximum number of zones that an object can concurrently be assigned to.
  110. MaxObjectZones = 128,
  111. NumMountPoints = 32,
  112. NumMountPointBits = 5,
  113. };
  114. /// Networking dirty mask.
  115. enum SceneObjectMasks
  116. {
  117. InitialUpdateMask = BIT( 0 ),
  118. ScaleMask = BIT( 1 ),
  119. FlagMask = BIT( 2 ),
  120. MountedMask = BIT( 3 ),
  121. NextFreeMask = BIT( 4 )
  122. };
  123. /// Bit-flags stored in mObjectFlags.
  124. /// If a derived class adds more flags they must overload
  125. /// getObjectFlagMax to ensure those flags will be transmitted over
  126. /// the network.
  127. /// @see getObjectFlagMax
  128. enum SceneObjectFlags
  129. {
  130. /// If set, the object can be rendered.
  131. /// @note The per-class render disable flag can override the per-object flag.
  132. RenderEnabledFlag = BIT( 0 ),
  133. /// If set, the object can be selected in the editor.
  134. /// @note The per-class selection disable flag can override the per-object flag.
  135. SelectionEnabledFlag = BIT( 1 ),
  136. /// If set, object will not be subjected to culling when in the editor.
  137. /// This is useful to bypass zone culling and always render certain editor-only
  138. /// visual elements (like the zones themselves).
  139. DisableCullingInEditorFlag = BIT( 2 ),
  140. /// If set, object will be used as a visual occluder. In this case,
  141. /// the object should implement buildSilhouette() and return a
  142. /// *convex* silhouette polygon.
  143. VisualOccluderFlag = BIT( 3 ),
  144. /// If set, object will be used as a sound occluder.
  145. SoundOccluderFlag = BIT( 4 ),
  146. NextFreeFlag = BIT( 5 )
  147. };
  148. protected:
  149. /// Combination of SceneObjectFlags.
  150. BitSet32 mObjectFlags;
  151. /// SceneManager to which this SceneObject belongs.
  152. SceneManager* mSceneManager;
  153. /// Links installed by SceneTrackers attached to this object.
  154. SceneObjectLink* mSceneObjectLinks;
  155. /// SceneObjectLightingPlugin attached to this object.
  156. SceneObjectLightingPlugin* mLightPlugin;
  157. /// Object type mask.
  158. /// @see SimObjectTypes
  159. U32 mTypeMask;
  160. /// @name Mounting
  161. /// @{
  162. /// Mounted object.
  163. struct MountInfo
  164. {
  165. SceneObject* list; ///< Linked-list of objects mounted on this object
  166. SceneObject* object; ///< Object this object is mounted on.
  167. SceneObject* link; ///< Link to next object mounted to this object's mount
  168. S32 node; ///< Node point we are mounted to.
  169. MatrixF xfm;
  170. };
  171. ///
  172. MountInfo mMount;
  173. ///
  174. SimPersistID* mMountPID;
  175. StringTableEntry mGameObjectAssetId;
  176. AssetPtr<GameObjectAsset> mGameObjectAsset;
  177. //Marked if this entity is a GameObject and deliniates from the parent GO asset
  178. bool mDirtyGameObject;
  179. /// @}
  180. /// @name Zoning
  181. /// @{
  182. /// Bidirectional link between a zone manager and its objects.
  183. struct ZoneRef : public SceneObjectRefBase< ZoneRef >
  184. {
  185. /// ID of zone.
  186. U32 zone;
  187. };
  188. /// Iterator over the zones that the object is assigned to.
  189. /// @note This iterator expects a clean zoning state. It will not update the
  190. /// zoning state in case it is dirty.
  191. struct ObjectZonesIterator
  192. {
  193. ObjectZonesIterator( SceneObject* object )
  194. : mCurrent( object->_getZoneRefHead() ) {}
  195. bool isValid() const
  196. {
  197. return ( mCurrent != NULL );
  198. }
  199. ObjectZonesIterator& operator ++()
  200. {
  201. AssertFatal( isValid(), "SceneObject::ObjectZonesIterator::operator++ - Invalid iterator!" );
  202. mCurrent = mCurrent->nextInObj;
  203. return *this;
  204. }
  205. U32 operator *() const
  206. {
  207. AssertFatal( isValid(), "SceneObject::ObjectZonesIterator::operator* - Invalid iterator!" );
  208. return mCurrent->zone;
  209. }
  210. private:
  211. ZoneRef* mCurrent;
  212. };
  213. friend struct ObjectZonesIterator;
  214. /// If an object moves, its zoning state needs to be updated. This is deferred
  215. /// to when the state is actually needed and this flag indicates a refresh
  216. /// is necessary.
  217. mutable bool mZoneRefDirty;
  218. /// Number of zones this object is assigned to.
  219. /// @note If #mZoneRefDirty is set, this might be outdated.
  220. mutable U32 mNumCurrZones;
  221. /// List of zones that this object is part of.
  222. /// @note If #mZoneRefDirty is set, this might be outdated.
  223. mutable ZoneRef* mZoneRefHead;
  224. /// Refresh the zoning state of this object, if it isn't up-to-date anymore.
  225. void _updateZoningState() const;
  226. /// Return the first link in the zone list of this object. Each link represents
  227. /// a single zone that the object is assigned to.
  228. ///
  229. /// @note This method will return the zoning list as is. In case the zoning state
  230. /// of the object is dirty, the list contents may be outdated.
  231. ZoneRef* _getZoneRefHead() const { return mZoneRefHead; }
  232. /// @}
  233. /// @name Transform and Collision Members
  234. /// @{
  235. // PATHSHAPE
  236. MatrixF mLastXform;
  237. // PATHSHAPE END
  238. /// Transform from object space to world space.
  239. MatrixF mObjToWorld;
  240. /// Transform from world space to object space (inverse).
  241. MatrixF mWorldToObj;
  242. /// Object scale.
  243. Point3F mObjScale;
  244. /// Bounding box in object space.
  245. Box3F mObjBox;
  246. /// Bounding box (AABB) in world space.
  247. Box3F mWorldBox;
  248. /// Bounding sphere in world space.
  249. SphereF mWorldSphere;
  250. /// Render matrix to transform object space to world space.
  251. MatrixF mRenderObjToWorld;
  252. /// Render matrix to transform world space to object space.
  253. MatrixF mRenderWorldToObj;
  254. /// Render bounding box in world space.
  255. Box3F mRenderWorldBox;
  256. /// Render bounding sphere in world space.
  257. SphereF mRenderWorldSphere;
  258. /// Whether this object is considered to have an infinite bounding box.
  259. bool mGlobalBounds;
  260. ///
  261. S32 mCollisionCount;
  262. /// Regenerates the world-space bounding box and bounding sphere.
  263. void resetWorldBox();
  264. /// Regenerates the render-world-space bounding box and sphere.
  265. void resetRenderWorldBox();
  266. /// Regenerates the object-space bounding box from the world-space
  267. /// bounding box, the world space to object space transform, and
  268. /// the object scale.
  269. void resetObjectBox();
  270. /// Called when the size of the object changes.
  271. virtual void onScaleChanged() {}
  272. /// @}
  273. /// Object which must be ticked before this object.
  274. SimObjectPtr< SceneObject > mAfterObject;
  275. /// @name SceneContainer Interface
  276. ///
  277. /// When objects are searched, we go through all the zones and ask them for
  278. /// all of their objects. Because an object can exist in multiple zones, the
  279. /// container sequence key is set to the id of the current search. Then, while
  280. /// searching, we check to see if an object's sequence key is the same as the
  281. /// current search key. If it is, it will NOT be added to the list of returns
  282. /// since it has already been processed.
  283. ///
  284. /// @{
  285. /// Container database that the object is assigned to.
  286. SceneContainer* mContainer;
  287. /// SceneContainer sequence key.
  288. U32 mContainerSeqKey;
  289. ///
  290. SceneObjectRef* mBinRefHead;
  291. U32 mBinMinX;
  292. U32 mBinMaxX;
  293. U32 mBinMinY;
  294. U32 mBinMaxY;
  295. /// Returns the container sequence key.
  296. U32 getContainerSeqKey() const { return mContainerSeqKey; }
  297. /// Sets the container sequence key.
  298. void setContainerSeqKey( const U32 key ) { mContainerSeqKey = key; }
  299. /// @}
  300. /// Called when this is added to a SceneManager.
  301. virtual bool onSceneAdd() { return true; }
  302. /// Called when this is removed from its current SceneManager.
  303. virtual void onSceneRemove() {}
  304. /// Returns the greatest object flag bit defined.
  305. /// Only bits within this range will be transmitted over the network.
  306. virtual U32 getObjectFlagMax() const { return NextFreeFlag - 1; }
  307. public:
  308. SceneObject();
  309. virtual ~SceneObject();
  310. bool mPathfindingIgnore;
  311. /// Triggered when a SceneObject onAdd is called.
  312. static Signal< void( SceneObject* ) > smSceneObjectAdd;
  313. /// Triggered when a SceneObject onRemove is called.
  314. static Signal< void( SceneObject* ) > smSceneObjectRemove;
  315. /// Return the type mask that indicates to which broad object categories
  316. /// this object belongs.
  317. U32 getTypeMask() const { return mTypeMask; }
  318. /// @name SceneManager Functionality
  319. /// @{
  320. /// Return the SceneManager that this SceneObject belongs to.
  321. SceneManager* getSceneManager() const { return mSceneManager; }
  322. /// Adds object to the client or server container depending on the object
  323. void addToScene();
  324. /// Removes the object from the client/server container
  325. void removeFromScene();
  326. /// Returns a pointer to the container that contains this object
  327. SceneContainer* getContainer() { return mContainer; }
  328. /// @}
  329. /// @name Flags
  330. /// @{
  331. /// Return true if this object is rendered.
  332. bool isRenderEnabled() const;
  333. /// Set whether the object gets rendered.
  334. void setRenderEnabled( bool value );
  335. /// Return true if this object can be selected in the editor.
  336. bool isSelectionEnabled() const;
  337. /// Set whether the object can be selected in the editor.
  338. void setSelectionEnabled( bool value );
  339. /// Return true if the object doesn't want to be subjected to culling
  340. /// when in the editor.
  341. bool isCullingDisabledInEditor() const { return mObjectFlags.test( DisableCullingInEditorFlag ); }
  342. /// Return true if the object should be taken into account for visual occlusion.
  343. bool isVisualOccluder() const { return mObjectFlags.test( VisualOccluderFlag ); }
  344. /// @}
  345. /// @name Collision and transform related interface
  346. ///
  347. /// The Render Transform is the interpolated transform with respect to the
  348. /// frame rate. The Render Transform will differ from the object transform
  349. /// because the simulation is updated in fixed intervals, which controls the
  350. /// object transform. The framerate is, most likely, higher than this rate,
  351. /// so that is why the render transform is interpolated and will differ slightly
  352. /// from the object transform.
  353. ///
  354. /// @{
  355. /// Disables collisions for this object including raycasts
  356. virtual void disableCollision();
  357. /// Enables collisions for this object
  358. virtual void enableCollision();
  359. /// Returns true if collisions are enabled
  360. bool isCollisionEnabled() const { return mCollisionCount == 0; }
  361. /// This gets called when an object collides with this object.
  362. /// @param object Object colliding with this object
  363. /// @param vec Vector along which collision occurred
  364. virtual void onCollision( SceneObject *object, const VectorF &vec ) {}
  365. /// Returns true if this object allows itself to be displaced
  366. /// @see displaceObject
  367. virtual bool isDisplacable() const { return false; }
  368. /// Returns the momentum of this object
  369. virtual Point3F getMomentum() const { return Point3F( 0, 0, 0 ); }
  370. /// Sets the momentum of this object
  371. /// @param momentum Momentum
  372. virtual void setMomentum( const Point3F& momentum ) {}
  373. /// Returns the mass of this object
  374. virtual F32 getMass() const { return 1.f; }
  375. /// Displaces this object by a vector
  376. /// @param displaceVector Displacement vector
  377. virtual bool displaceObject( const Point3F& displaceVector ) { return false; }
  378. /// Returns the transform which can be used to convert object space
  379. /// to world space
  380. virtual const MatrixF& getTransform() const { return mObjToWorld; }
  381. /// Returns the transform which can be used to convert world space
  382. /// into object space
  383. const MatrixF& getWorldTransform() const { return mWorldToObj; }
  384. /// Returns the scale of the object
  385. virtual const VectorF& getScale() const { return mObjScale; }
  386. /// Returns the bounding box for this object in local coordinates.
  387. const Box3F& getObjBox() const { return mObjBox; }
  388. /// Returns the bounding box for this object in world coordinates.
  389. const Box3F& getWorldBox() const { return mWorldBox; }
  390. /// Returns the bounding sphere for this object in world coordinates.
  391. const SphereF& getWorldSphere() const { return mWorldSphere; }
  392. /// Returns the center of the bounding box in world coordinates
  393. Point3F getBoxCenter() const { return ( mWorldBox.minExtents + mWorldBox.maxExtents ) * 0.5f; }
  394. /// Sets the Object -> World transform
  395. ///
  396. /// @param mat New transform matrix
  397. virtual void setTransform( const MatrixF &mat );
  398. /// Sets the scale for the object
  399. /// @param scale Scaling values
  400. virtual void setScale( const VectorF &scale );
  401. /// Sets the forward vector of the object
  402. void setForwardVector(VectorF newForward, VectorF upVector = VectorF(0, 0, 1));
  403. /// This sets the render transform for this object
  404. /// @param mat New render transform
  405. virtual void setRenderTransform(const MatrixF &mat);
  406. /// Returns the render transform
  407. const MatrixF& getRenderTransform() const { return mRenderObjToWorld; }
  408. /// Returns the render transform to convert world to local coordinates
  409. const MatrixF& getRenderWorldTransform() const { return mRenderWorldToObj; }
  410. /// Returns the render world box
  411. const Box3F& getRenderWorldBox() const { return mRenderWorldBox; }
  412. /// Sets the state of this object as hidden or not. If an object is hidden
  413. /// it is removed entirely from collisions, it is not ghosted and is
  414. /// essentially "non existant" as far as simulation is concerned.
  415. /// @param hidden True if object is to be hidden
  416. virtual void setHidden( bool hidden );
  417. /// Builds a convex hull for this object.
  418. ///
  419. /// Think of a convex hull as a low-res mesh which covers, as tightly as
  420. /// possible, the object mesh, and is used as a collision mesh.
  421. /// @param box
  422. /// @param convex Convex mesh generated (out)
  423. virtual void buildConvex( const Box3F& box,Convex* convex ) {}
  424. /// Builds a list of polygons which intersect a bounding volume.
  425. ///
  426. /// This will use either the sphere or the box, not both, the
  427. /// SceneObject implementation ignores sphere.
  428. ///
  429. /// @see AbstractPolyList
  430. /// @param context A contentual hint as to the type of polylist to build.
  431. /// @param polyList Poly list build (out)
  432. /// @param box Box bounding volume
  433. /// @param sphere Sphere bounding volume
  434. ///
  435. virtual bool buildPolyList( PolyListContext context,
  436. AbstractPolyList* polyList,
  437. const Box3F& box,
  438. const SphereF& sphere ) { return false; }
  439. /// Builds a list of polygons which intersect a bounding volume for exporting
  440. ///
  441. /// This will use either the sphere or the box, not both, the
  442. /// SceneObject implementation ignores sphere.
  443. ///
  444. /// @see AbstractPolyList
  445. /// @param context A contentual hint as to the type of polylist to build.
  446. /// @param polyList Poly list build (out)
  447. /// @param box Box bounding volume
  448. /// @param sphere Sphere bounding volume
  449. ///
  450. virtual bool buildExportPolyList(ColladaUtils::ExportData *exportData,
  451. const Box3F& box,
  452. const SphereF& sphere) {
  453. return false;
  454. }
  455. /// Casts a ray and obtain collision information, returns true if RayInfo is modified.
  456. ///
  457. /// @param start Start point of ray
  458. /// @param end End point of ray
  459. /// @param info Collision information obtained (out)
  460. virtual bool castRay( const Point3F& start, const Point3F& end, RayInfo* info ) { return false; }
  461. /// Casts a ray against rendered geometry, returns true if RayInfo is modified.
  462. ///
  463. /// @param start Start point of ray
  464. /// @param end End point of ray
  465. /// @param info Collision information obtained (out)
  466. virtual bool castRayRendered( const Point3F& start, const Point3F& end, RayInfo* info );
  467. /// Build a world-space silhouette polygon for the object for the given camera settings.
  468. /// This is used for occlusion.
  469. ///
  470. /// @param cameraState Camera view parameters.
  471. /// @param outPoints Vector to store the resulting polygon points in. Leave untouched
  472. /// if method is not implemented.
  473. virtual void buildSilhouette( const SceneCameraState& cameraState, Vector< Point3F >& outPoints ) {}
  474. /// Return true if the given point is contained by the object's (collision) shape.
  475. ///
  476. /// The default implementation will return true if the point is within the object's
  477. /// bounding box. Subclasses should implement more precise tests.
  478. virtual bool containsPoint( const Point3F &point );
  479. virtual bool collideBox( const Point3F& start, const Point3F& end, RayInfo* info );
  480. /// Returns the position of the object.
  481. virtual Point3F getPosition() const;
  482. /// Returns the render-position of the object.
  483. ///
  484. /// @see getRenderTransform
  485. Point3F getRenderPosition() const;
  486. /// Sets the position of the object
  487. void setPosition ( const Point3F& pos );
  488. /// Gets the velocity of the object.
  489. virtual Point3F getVelocity() const { return Point3F::Zero; }
  490. /// Sets the velocity of the object
  491. /// @param v Velocity
  492. virtual void setVelocity( const Point3F &v ) {}
  493. /// Applies an impulse force to this object
  494. /// @param pos Position where impulse came from in world space
  495. /// @param vec Velocity vector (Impulse force F = m * v)
  496. virtual void applyImpulse( const Point3F &pos, const VectorF &vec ) {}
  497. /// Applies a radial impulse to the object
  498. /// using the impulse origin and force.
  499. /// @param origin Point of origin of the radial impulse.
  500. /// @param radius The radius of the impulse area.
  501. /// @param magnitude The strength of the impulse.
  502. virtual void applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude ) {}
  503. /// Returns the distance from this object to a point
  504. /// @param pnt World space point to measure to
  505. virtual F32 distanceTo( const Point3F &pnt ) const;
  506. /// @}
  507. /// @name Mounting
  508. /// @{
  509. /// ex: Mount B to A at A's node N
  510. /// A.mountObject( B, N )
  511. ///
  512. /// @param obj Object to mount
  513. /// @param node Mount node ID
  514. virtual void mountObject( SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity );
  515. /// Remove an object mounting
  516. /// @param obj Object to unmount
  517. virtual void unmountObject( SceneObject *obj );
  518. /// Unmount this object from it's mount
  519. virtual void unmount();
  520. /// Callback when this object is mounted.
  521. /// @param obj Object we are mounting to.
  522. /// @param node Node we are unmounting from.
  523. virtual void onMount( SceneObject *obj, S32 node );
  524. /// Callback when this object is unmounted. This should be overridden to
  525. /// set maskbits or do other object type specific work.
  526. /// @param obj Object we are unmounting from.
  527. /// @param node Node we are unmounting from.
  528. virtual void onUnmount( SceneObject *obj, S32 node );
  529. // Returns mount point to world space transform at tick time.
  530. virtual void getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat );
  531. // Returns mount point to world space transform at render time.
  532. // Note this will only be correct if called after this object has interpolated.
  533. virtual void getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat );
  534. /// Return the object that this object is mounted to.
  535. virtual SceneObject* getObjectMount() { return mMount.object; }
  536. /// Return object link of next object mounted to this object's mount
  537. virtual SceneObject* getMountLink() { return mMount.link; }
  538. /// Returns object list of objects mounted to this object.
  539. virtual SceneObject* getMountList() { return mMount.list; }
  540. /// Returns the mount id that this is mounted to.
  541. virtual U32 getMountNode() { return mMount.node; }
  542. /// Returns true if this object is mounted to anything at all
  543. /// Also try to resolve the PID to objectId here if it is pending.
  544. virtual bool isMounted();
  545. /// Returns the number of object mounted along with this
  546. virtual S32 getMountedObjectCount();
  547. /// Returns the object mounted at a position in the mount list
  548. /// @param idx Position on the mount list
  549. virtual SceneObject* getMountedObject( S32 idx );
  550. /// Returns the node the object at idx is mounted to
  551. /// @param idx Index
  552. virtual S32 getMountedObjectNode( S32 idx );
  553. /// Returns the object a object on the mount list is mounted to
  554. /// @param node
  555. virtual SceneObject* getMountNodeObject( S32 node );
  556. void resolveMountPID();
  557. /// @}
  558. /// @name Sound
  559. /// @{
  560. /// Return whether the object's collision shape is blocking sound.
  561. bool isOccludingSound() const { return mObjectFlags.test( SoundOccluderFlag ); }
  562. /// Return the ambient sound space active inside the volume of this object or NULL if the object does
  563. /// not have its own ambient space.
  564. virtual SFXAmbience* getSoundAmbience() const { return NULL; }
  565. /// @}
  566. /// @name Rendering
  567. /// @{
  568. /// Called when the SceneManager is ready for the registration of render instances.
  569. /// @param state Rendering state.
  570. virtual void prepRenderImage( SceneRenderState* state ) {}
  571. /// @}
  572. /// @name Lighting
  573. /// @{
  574. void setLightingPlugin( SceneObjectLightingPlugin* plugin ) { mLightPlugin = plugin; }
  575. SceneObjectLightingPlugin* getLightingPlugin() { return mLightPlugin; }
  576. /// Gets the number of zones containing this object.
  577. U32 getNumCurrZones() const { return mNumCurrZones; }
  578. /// Returns the nth zone containing this object.
  579. U32 getCurrZone(const U32 index) const;
  580. /// @}
  581. /// @name Global Bounds
  582. /// @{
  583. const bool isGlobalBounds() const
  584. {
  585. return mGlobalBounds;
  586. }
  587. /// If global bounds are set to be true, then the object is assumed to
  588. /// have an infinitely large bounding box for collision and rendering
  589. /// purposes.
  590. ///
  591. /// They can't be toggled currently.
  592. void setGlobalBounds();
  593. /// @}
  594. /// Return the ProcessList for this object to use.
  595. ProcessList* getProcessList() const;
  596. // ProcessObject,
  597. virtual void processAfter( ProcessObject *obj );
  598. virtual void clearProcessAfter();
  599. virtual ProcessObject* getAfterObject() const { return mAfterObject; }
  600. virtual void setProcessTick( bool t );
  601. // NetObject.
  602. virtual U32 packUpdate( NetConnection* conn, U32 mask, BitStream* stream );
  603. virtual void unpackUpdate( NetConnection* conn, BitStream* stream );
  604. virtual void onCameraScopeQuery( NetConnection* connection, CameraScopeQuery* query );
  605. // SimObject.
  606. virtual bool onAdd();
  607. virtual void onRemove();
  608. virtual void onDeleteNotify( SimObject *object );
  609. virtual void inspectPostApply();
  610. virtual bool writeField( StringTableEntry fieldName, const char* value );
  611. static void initPersistFields();
  612. static bool _setGameObject(void* object, const char* index, const char* data);
  613. DECLARE_CONOBJECT( SceneObject );
  614. private:
  615. SceneObject( const SceneObject& ); ///< @deprecated disallowed
  616. /// For ScopeAlways objects to be able to properly implement setHidden(), they
  617. /// need to temporarily give up ScopeAlways status while being hidden. Otherwise
  618. /// the client-side ghost will not disappear as the server-side object will be
  619. /// forced to stay in scope.
  620. bool mIsScopeAlways;
  621. /// @name Protected field getters/setters
  622. /// @{
  623. static const char* _getRenderEnabled( void *object, const char *data );
  624. static bool _setRenderEnabled( void *object, const char *index, const char *data );
  625. static const char* _getSelectionEnabled( void *object, const char *data );
  626. static bool _setSelectionEnabled( void *object, const char *index, const char *data );
  627. static bool _setFieldPosition( void *object, const char *index, const char *data );
  628. static bool _setFieldRotation( void *object, const char *index, const char *data );
  629. static bool _setFieldScale( void *object, const char *index, const char *data );
  630. static bool _setMountPID( void* object, const char* index, const char* data );
  631. static bool _setAccuEnabled( void *object, const char *index, const char *data );
  632. /// @}
  633. // PATHSHAPE
  634. /// @}
  635. //Anthony's Original Code, still used so i keep it here
  636. /// TGE uses the term "mount" in a quirky, staticky way relating to its limited use to have
  637. /// riders and guns mounted on a vehicle (and similar)
  638. /// I did not alter that code at all (yet) and did not want to keep its terminology for other reasons
  639. /// I decided to support a hierarchy of scene objects and dubbed the operations
  640. /// attaching and removing child SceneObjects
  641. protected:
  642. // this member struct tracks the relationship to parent and children
  643. // sceneObjects in a hierarchical scene graph whose root is the entire Scene
  644. struct AttachInfo {
  645. SceneObject* firstChild; ///< Objects mounted on this object
  646. SimObjectPtr<SceneObject> parent; ///< Object this object is mounted on.
  647. SceneObject* nextSibling; ///< Link to next child object of this object's parent
  648. MatrixF objToParent; ///< this obects transformation in the parent object's space
  649. MatrixF RenderobjToParent; ///< this obects Render Offset transformation to the parent object
  650. AttachInfo() {
  651. firstChild = NULL;
  652. parent = NULL;
  653. nextSibling = NULL;
  654. objToParent.identity();
  655. RenderobjToParent.identity();
  656. };
  657. } mGraph;
  658. // PATHSHAPE END
  659. // Accumulation Texture
  660. // Note: This was placed in SceneObject to both ShapeBase and TSStatic could support it.
  661. public:
  662. GFXTextureObject* mAccuTex;
  663. // mSelectionFlags field keeps track of flags related to object selection.
  664. // PRE_SELECTED marks an object as pre-selected (object under cursor)
  665. // SELECTED marks an object as selected (a target)
  666. protected:
  667. U8 mSelectionFlags;
  668. public:
  669. enum {
  670. SELECTED = BIT(0),
  671. PRE_SELECTED = BIT(1),
  672. };
  673. virtual void setSelectionFlags(U8 flags) { mSelectionFlags = flags; }
  674. U8 getSelectionFlags() const { return mSelectionFlags; }
  675. bool needsSelectionHighlighting() const { return (mSelectionFlags != 0); }
  676. // This should only return true if the object represents an independent camera
  677. // as opposed to something like a Player that has a built-in camera that requires
  678. // special calculations to determine the view transform.
  679. virtual bool isCamera() const { return false; }
  680. // AFX CODE BLOCK (is-camera) >>
  681. // PATHSHAPE
  682. // Added for dynamic attaching
  683. void UpdateXformChange(const MatrixF &mat);
  684. /// this is useful for setting NULL parent (making SceneObject a root object)
  685. virtual bool attachToParent(SceneObject *parent, MatrixF *atThisOffset = NULL, S32 node=0);
  686. SceneObject *getParent() { return mGraph.parent; };
  687. /// attach a subobject, but do not alter the subObject's present absolute position or orientation
  688. bool attachChild(SceneObject* subObject);
  689. /// attach a subobject, at the specified offset expressed in our local coordinate space
  690. bool attachChildAt(SceneObject* subObject, MatrixF atThisTransform, S32 node);
  691. /// attach a subobject, at the specified position expressed in our local coordinate space
  692. bool attachChildAt(SceneObject* subObject, Point3F atThisPosition);
  693. /// how many child SceneObjects are (directly) attached to this one?
  694. U32 getNumChildren() const;
  695. /// how many child objects does this SceneObject have when we count them recursively?
  696. U32 getNumProgeny() const;
  697. /// returns the (direct) child SceneObject at the given index (0 <= index <= getNumChildren() - 1)
  698. SceneObject *getChild(U32 index) const;
  699. /// is this SceneObject a child (directly or indirectly) of the given object?
  700. bool isChildOf(SceneObject *);
  701. /// set position in parent SceneObject's coordinate space (or in world space if no parent)
  702. //void setLocalPosition(const Point3F &pos);
  703. /// move the object in parent SceneObject's coordinate space (or in world space if no parent)
  704. //void localMove(const Point3F &delta);
  705. /// as localMove(const Point3F &delta), with different signature
  706. //void localMove(F32 x, F32 y, F32 z);
  707. /// move the object in world space, without altering place in scene hierarchy
  708. void move(const Point3F &delta);
  709. // Does checks for children objects and updates their positions
  710. void PerformUpdatesForChildren(MatrixF mat);
  711. // Move the RenderTransform
  712. void moveRender(const Point3F &delta);
  713. //Calculate how much to adjust the render transform - Called by the child objects
  714. void updateRenderChangesByParent();
  715. //Calculate how much to adjust the transform - Called by the parent object
  716. void updateChildTransform();
  717. /// as move(const Point3F &delta), with different signature
  718. void move(F32 x, F32 y, F32 z);
  719. /// returns the transform relative to parent SceneObject transform (or world transform if no parent)
  720. //const MatrixF& getLocalTransform() const;
  721. /// returns the position within parent SceneObject space (or world space if no parent)
  722. //Point3F getLocalPosition() const;
  723. // virtual void onParentScaleChanged();
  724. // virtual void onParentTransformChanged();
  725. /// Sets the Object -> Parent transform. If no parent SceneObject, this is equivalent to
  726. /// setTransform()
  727. ///
  728. /// @param mat New transform matrix
  729. //virtual void setLocalTransform(const MatrixF & mat);
  730. /// Called to let instance specific code happen
  731. virtual void onLostParent(SceneObject *oldParent);
  732. DECLARE_CALLBACK(void, onLostParent, (SceneObject *oldParent));
  733. /// Called to let instance specific code happen
  734. virtual void onNewParent(SceneObject *newParent);
  735. DECLARE_CALLBACK(void, onNewParent, (SceneObject *oldParent));
  736. /// notification that a direct child object has been attached
  737. virtual void onNewChild(SceneObject *subObject);
  738. DECLARE_CALLBACK(void, onNewChild, (SceneObject *subObject));
  739. /// notification that a direct child object has been detached
  740. virtual void onLostChild(SceneObject *subObject);
  741. DECLARE_CALLBACK(void, onLostChild, (SceneObject *subObject));
  742. // PATHSHAPE END
  743. virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList) {}
  744. };
  745. #endif // _SCENEOBJECT_H_