sceneObject.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "scene/sceneObject.h"
  24. #include "platform/profiler.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "console/simPersistID.h"
  28. #include "sim/netConnection.h"
  29. #include "core/stream/bitStream.h"
  30. #include "scene/sceneManager.h"
  31. #include "scene/sceneTracker.h"
  32. #include "scene/sceneRenderState.h"
  33. #include "scene/zones/sceneZoneSpace.h"
  34. #include "collision/extrudedPolyList.h"
  35. #include "collision/earlyOutPolyList.h"
  36. #include "collision/optimizedPolyList.h"
  37. #include "math/mPolyhedron.h"
  38. #include "gfx/bitmap/gBitmap.h"
  39. #include "math/util/frustum.h"
  40. #include "math/mathIO.h"
  41. #include "math/mTransform.h"
  42. #include "T3D/gameBase/gameProcess.h"
  43. #include "T3D/gameBase/gameConnection.h"
  44. #include "T3D/accumulationVolume.h"
  45. IMPLEMENT_CONOBJECT(SceneObject);
  46. ConsoleDocClass( SceneObject,
  47. "@brief A networkable object that exists in the 3D world.\n\n"
  48. "The SceneObject class provides the foundation for 3D objects in the Engine. It "
  49. "exposes the functionality for:\n\n"
  50. "<ul><li>Position, rotation and scale within the world.</li>"
  51. "<li>Working with a scene graph (in the Zone and Portal sections), allowing efficient "
  52. "and robust rendering of the game scene.</li>"
  53. "<li>Various helper functions, including functions to get bounding information "
  54. "and momentum/velocity.</li>"
  55. "<li>Mounting one SceneObject to another.</li>"
  56. "<li>An interface for collision detection, as well as ray casting.</li>"
  57. "<li>Lighting. SceneObjects can register lights both at lightmap generation "
  58. "time, and dynamic lights at runtime (for special effects, such as from flame "
  59. "or a projectile, or from an explosion).</li></ul>\n\n"
  60. "You do not typically work with SceneObjects themselves. The SceneObject provides a reference "
  61. "within the game world (the scene), but does not render to the client on its own. The "
  62. "same is true of collision detection beyond that of the bounding box. Instead you "
  63. "use one of the many classes that derrive from SceneObject, such as TSStatic.\n\n"
  64. "@section SceneObject_Hiding Difference Between setHidden() and isRenderEnabled\n\n"
  65. "When it comes time to decide if a SceneObject should render or not, there are two "
  66. "methods that can stop the SceneObject from rendering at all. You need to be aware of "
  67. "the differences between these two methods as they impact how the SceneObject is networked "
  68. "from the server to the client.\n\n"
  69. "The first method of manually controlling if a SceneObject is rendered is through its "
  70. "SceneObject::isRenderEnabled property. When set to false the SceneObject is considered invisible but "
  71. "still present within the scene. This means it still takes part in collisions and continues "
  72. "to be networked.\n\n"
  73. "The second method is using the setHidden() method. This will actually remove a SceneObject "
  74. "from the scene and it will no longer be networked from the server to the cleint. Any client-side "
  75. "ghost of the object will be deleted as the server no longer considers the object to be in scope.\n\n"
  76. "@ingroup gameObjects\n"
  77. );
  78. Signal< void( SceneObject* ) > SceneObject::smSceneObjectAdd;
  79. Signal< void( SceneObject* ) > SceneObject::smSceneObjectRemove;
  80. //-----------------------------------------------------------------------------
  81. SceneObject::SceneObject()
  82. {
  83. mContainer = 0;
  84. mTypeMask = DefaultObjectType;
  85. mCollisionCount = 0;
  86. mGlobalBounds = false;
  87. mObjScale.set(1,1,1);
  88. mObjToWorld.identity();
  89. mWorldToObj.identity();
  90. mObjBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  91. mWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  92. mWorldSphere = SphereF(Point3F(0, 0, 0), 0);
  93. mRenderObjToWorld.identity();
  94. mRenderWorldToObj.identity();
  95. mRenderWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  96. mRenderWorldSphere = SphereF(Point3F(0, 0, 0), 0);
  97. mContainerSeqKey = 0;
  98. mBinRefHead = NULL;
  99. mSceneManager = NULL;
  100. mNumCurrZones = 0;
  101. mZoneRefHead = NULL;
  102. mZoneRefDirty = false;
  103. mBinMinX = 0xFFFFFFFF;
  104. mBinMaxX = 0xFFFFFFFF;
  105. mBinMinY = 0xFFFFFFFF;
  106. mBinMaxY = 0xFFFFFFFF;
  107. mLightPlugin = NULL;
  108. mMount.object = NULL;
  109. mMount.link = NULL;
  110. mMount.list = NULL;
  111. mMount.node = -1;
  112. mMount.xfm = MatrixF::Identity;
  113. mMountPID = NULL;
  114. mSceneObjectLinks = NULL;
  115. mObjectFlags.set( RenderEnabledFlag | SelectionEnabledFlag );
  116. mIsScopeAlways = false;
  117. mAccuTex = NULL;
  118. }
  119. //-----------------------------------------------------------------------------
  120. SceneObject::~SceneObject()
  121. {
  122. AssertFatal( mZoneRefHead == NULL && mBinRefHead == NULL,
  123. "SceneObject::~SceneObject - Object still linked in reference lists!");
  124. AssertFatal( !mSceneObjectLinks,
  125. "SceneObject::~SceneObject() - object is still linked to SceneTrackers" );
  126. mAccuTex = NULL;
  127. unlink();
  128. }
  129. //-----------------------------------------------------------------------------
  130. bool SceneObject::castRayRendered(const Point3F &start, const Point3F &end, RayInfo *info)
  131. {
  132. // By default, all ray checking against the rendered mesh will be passed
  133. // on to the collision mesh. This saves having to define both methods
  134. // for simple objects.
  135. return castRay( start, end, info );
  136. }
  137. //-----------------------------------------------------------------------------
  138. bool SceneObject::containsPoint( const Point3F& point )
  139. {
  140. // If it's not in the AABB, then it can't be in the OBB either,
  141. // so early out.
  142. if( !mWorldBox.isContained( point ) )
  143. return false;
  144. // Transform point into object space and test it against
  145. // our object space bounding box.
  146. Point3F objPoint( 0, 0, 0 );
  147. getWorldTransform().mulP( point, &objPoint );
  148. objPoint.convolveInverse( getScale() );
  149. return ( mObjBox.isContained( objPoint ) );
  150. }
  151. //-----------------------------------------------------------------------------
  152. bool SceneObject::collideBox(const Point3F &start, const Point3F &end, RayInfo *info)
  153. {
  154. const F32 * pStart = (const F32*)start;
  155. const F32 * pEnd = (const F32*)end;
  156. const F32 * pMin = (const F32*)mObjBox.minExtents;
  157. const F32 * pMax = (const F32*)mObjBox.maxExtents;
  158. F32 maxStartTime = -1;
  159. F32 minEndTime = 1;
  160. F32 startTime;
  161. F32 endTime;
  162. // used for getting normal
  163. U32 hitIndex = 0xFFFFFFFF;
  164. U32 side;
  165. // walk the axis
  166. for(U32 i = 0; i < 3; i++)
  167. {
  168. //
  169. if(pStart[i] < pEnd[i])
  170. {
  171. if(pEnd[i] < pMin[i] || pStart[i] > pMax[i])
  172. return(false);
  173. F32 dist = pEnd[i] - pStart[i];
  174. startTime = (pStart[i] < pMin[i]) ? (pMin[i] - pStart[i]) / dist : -1;
  175. endTime = (pEnd[i] > pMax[i]) ? (pMax[i] - pStart[i]) / dist : 1;
  176. side = 1;
  177. }
  178. else
  179. {
  180. if(pStart[i] < pMin[i] || pEnd[i] > pMax[i])
  181. return(false);
  182. F32 dist = pStart[i] - pEnd[i];
  183. startTime = (pStart[i] > pMax[i]) ? (pStart[i] - pMax[i]) / dist : -1;
  184. endTime = (pEnd[i] < pMin[i]) ? (pStart[i] - pMin[i]) / dist : 1;
  185. side = 0;
  186. }
  187. //
  188. if(startTime > maxStartTime)
  189. {
  190. maxStartTime = startTime;
  191. hitIndex = i * 2 + side;
  192. }
  193. if(endTime < minEndTime)
  194. minEndTime = endTime;
  195. if(minEndTime < maxStartTime)
  196. return(false);
  197. }
  198. // fail if inside
  199. if(maxStartTime < 0.f)
  200. return(false);
  201. //
  202. static Point3F boxNormals[] = {
  203. Point3F( 1, 0, 0),
  204. Point3F(-1, 0, 0),
  205. Point3F( 0, 1, 0),
  206. Point3F( 0,-1, 0),
  207. Point3F( 0, 0, 1),
  208. Point3F( 0, 0,-1),
  209. };
  210. //
  211. AssertFatal(hitIndex != 0xFFFFFFFF, "SceneObject::collideBox");
  212. info->t = maxStartTime;
  213. info->object = this;
  214. mObjToWorld.mulV(boxNormals[hitIndex], &info->normal);
  215. info->material = 0;
  216. return(true);
  217. }
  218. //-----------------------------------------------------------------------------
  219. void SceneObject::disableCollision()
  220. {
  221. mCollisionCount++;
  222. AssertFatal(mCollisionCount < 50, "SceneObject::disableCollision called 50 times on the same object. Is this inside a circular loop?" );
  223. }
  224. //-----------------------------------------------------------------------------
  225. void SceneObject::enableCollision()
  226. {
  227. if (mCollisionCount)
  228. --mCollisionCount;
  229. }
  230. //-----------------------------------------------------------------------------
  231. bool SceneObject::onAdd()
  232. {
  233. if ( !Parent::onAdd() )
  234. return false;
  235. mIsScopeAlways = mNetFlags.test( ScopeAlways );
  236. mWorldToObj = mObjToWorld;
  237. mWorldToObj.affineInverse();
  238. resetWorldBox();
  239. setRenderTransform(mObjToWorld);
  240. resolveMountPID();
  241. smSceneObjectAdd.trigger(this);
  242. return true;
  243. }
  244. //-----------------------------------------------------------------------------
  245. void SceneObject::onRemove()
  246. {
  247. smSceneObjectRemove.trigger(this);
  248. unmount();
  249. plUnlink();
  250. Parent::onRemove();
  251. }
  252. //-----------------------------------------------------------------------------
  253. void SceneObject::addToScene()
  254. {
  255. if( mSceneManager )
  256. return;
  257. if( isClientObject() )
  258. gClientSceneGraph->addObjectToScene( this );
  259. else
  260. gServerSceneGraph->addObjectToScene( this );
  261. }
  262. //-----------------------------------------------------------------------------
  263. void SceneObject::removeFromScene()
  264. {
  265. if( !mSceneManager )
  266. return;
  267. mSceneManager->removeObjectFromScene( this );
  268. }
  269. //-----------------------------------------------------------------------------
  270. void SceneObject::onDeleteNotify( SimObject *obj )
  271. {
  272. // We are comparing memory addresses so even if obj really is not a
  273. // ProcessObject this cast shouldn't break anything.
  274. if ( obj == mAfterObject )
  275. mAfterObject = NULL;
  276. if ( obj == mMount.object )
  277. unmount();
  278. Parent::onDeleteNotify( obj );
  279. }
  280. //-----------------------------------------------------------------------------
  281. void SceneObject::inspectPostApply()
  282. {
  283. if( isServerObject() )
  284. setMaskBits( MountedMask );
  285. Parent::inspectPostApply();
  286. }
  287. //-----------------------------------------------------------------------------
  288. void SceneObject::setGlobalBounds()
  289. {
  290. mGlobalBounds = true;
  291. mObjBox.minExtents.set( -1e10, -1e10, -1e10 );
  292. mObjBox.maxExtents.set( 1e10, 1e10, 1e10 );
  293. if( mSceneManager )
  294. mSceneManager->notifyObjectDirty( this );
  295. }
  296. //-----------------------------------------------------------------------------
  297. void SceneObject::setTransform( const MatrixF& mat )
  298. {
  299. // This test is a bit expensive so turn it off in release.
  300. #ifdef TORQUE_DEBUG
  301. //AssertFatal( mat.isAffine(), "SceneObject::setTransform() - Bad transform (non affine)!" );
  302. #endif
  303. PROFILE_SCOPE( SceneObject_setTransform );
  304. // Update the transforms.
  305. mObjToWorld = mWorldToObj = mat;
  306. mWorldToObj.affineInverse();
  307. // Update the world-space AABB.
  308. resetWorldBox();
  309. // If we're in a SceneManager, sync our scene state.
  310. if( mSceneManager != NULL )
  311. mSceneManager->notifyObjectDirty( this );
  312. setRenderTransform( mat );
  313. }
  314. //-----------------------------------------------------------------------------
  315. void SceneObject::setScale( const VectorF &scale )
  316. {
  317. AssertFatal( !mIsNaN( scale ), "SceneObject::setScale() - The scale is NaN!" );
  318. // Avoid unnecessary scaling operations.
  319. if ( mObjScale.equal( scale ) )
  320. return;
  321. mObjScale = scale;
  322. setTransform(MatrixF(mObjToWorld));
  323. // Make sure that any subclasses of me get a chance to react to the
  324. // scale being changed.
  325. onScaleChanged();
  326. setMaskBits( ScaleMask );
  327. }
  328. //-----------------------------------------------------------------------------
  329. void SceneObject::resetWorldBox()
  330. {
  331. AssertFatal(mObjBox.isValidBox(), "SceneObject::resetWorldBox - Bad object box!");
  332. mWorldBox = mObjBox;
  333. mWorldBox.minExtents.convolve(mObjScale);
  334. mWorldBox.maxExtents.convolve(mObjScale);
  335. mObjToWorld.mul(mWorldBox);
  336. AssertFatal(mWorldBox.isValidBox(), "SceneObject::resetWorldBox - Bad world box!");
  337. // Create mWorldSphere from mWorldBox
  338. mWorldBox.getCenter(&mWorldSphere.center);
  339. mWorldSphere.radius = (mWorldBox.maxExtents - mWorldSphere.center).len();
  340. // Update tracker links.
  341. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  342. link = link->getNextLink() )
  343. link->update();
  344. }
  345. //-----------------------------------------------------------------------------
  346. void SceneObject::resetObjectBox()
  347. {
  348. AssertFatal( mWorldBox.isValidBox(), "SceneObject::resetObjectBox - Bad world box!" );
  349. mObjBox = mWorldBox;
  350. mWorldToObj.mul( mObjBox );
  351. Point3F objScale( mObjScale );
  352. objScale.setMax( Point3F( (F32)POINT_EPSILON, (F32)POINT_EPSILON, (F32)POINT_EPSILON ) );
  353. mObjBox.minExtents.convolveInverse( objScale );
  354. mObjBox.maxExtents.convolveInverse( objScale );
  355. AssertFatal( mObjBox.isValidBox(), "SceneObject::resetObjectBox - Bad object box!" );
  356. // Update the mWorldSphere from mWorldBox
  357. mWorldBox.getCenter( &mWorldSphere.center );
  358. mWorldSphere.radius = ( mWorldBox.maxExtents - mWorldSphere.center ).len();
  359. // Update scene managers.
  360. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  361. link = link->getNextLink() )
  362. link->update();
  363. }
  364. //-----------------------------------------------------------------------------
  365. void SceneObject::setRenderTransform(const MatrixF& mat)
  366. {
  367. PROFILE_START(SceneObj_setRenderTransform);
  368. mRenderObjToWorld = mRenderWorldToObj = mat;
  369. mRenderWorldToObj.affineInverse();
  370. AssertFatal(mObjBox.isValidBox(), "Bad object box!");
  371. resetRenderWorldBox();
  372. PROFILE_END();
  373. }
  374. //-----------------------------------------------------------------------------
  375. void SceneObject::resetRenderWorldBox()
  376. {
  377. AssertFatal( mObjBox.isValidBox(), "Bad object box!" );
  378. mRenderWorldBox = mObjBox;
  379. mRenderWorldBox.minExtents.convolve( mObjScale );
  380. mRenderWorldBox.maxExtents.convolve( mObjScale );
  381. mRenderObjToWorld.mul( mRenderWorldBox );
  382. AssertFatal( mRenderWorldBox.isValidBox(), "Bad world box!" );
  383. // Create mRenderWorldSphere from mRenderWorldBox.
  384. mRenderWorldBox.getCenter( &mRenderWorldSphere.center );
  385. mRenderWorldSphere.radius = ( mRenderWorldBox.maxExtents - mRenderWorldSphere.center ).len();
  386. }
  387. //-----------------------------------------------------------------------------
  388. void SceneObject::setHidden( bool hidden )
  389. {
  390. if( hidden != isHidden() )
  391. {
  392. // Add/remove the object from the scene. Removing it
  393. // will also cause the NetObject to go out of scope since
  394. // the container query will not find it anymore. However,
  395. // ScopeAlways objects need to be treated separately as we
  396. // do next.
  397. if( !hidden )
  398. addToScene();
  399. else
  400. removeFromScene();
  401. // ScopeAlways objects stay in scope no matter what, i.e. even
  402. // if they aren't in the scene query anymore. So, to force ghosts
  403. // to go away, we need to clear ScopeAlways while we are hidden.
  404. if( hidden && mIsScopeAlways )
  405. clearScopeAlways();
  406. else if( !hidden && mIsScopeAlways )
  407. setScopeAlways();
  408. Parent::setHidden( hidden );
  409. }
  410. }
  411. //-----------------------------------------------------------------------------
  412. void SceneObject::initPersistFields()
  413. {
  414. addGroup( "Transform" );
  415. addProtectedField( "position", TypeMatrixPosition, Offset( mObjToWorld, SceneObject ),
  416. &_setFieldPosition, &defaultProtectedGetFn,
  417. "Object world position." );
  418. addProtectedField( "rotation", TypeMatrixRotation, Offset( mObjToWorld, SceneObject ),
  419. &_setFieldRotation, &defaultProtectedGetFn,
  420. "Object world orientation." );
  421. addProtectedField( "scale", TypePoint3F, Offset( mObjScale, SceneObject ),
  422. &_setFieldScale, &defaultProtectedGetFn,
  423. "Object world scale." );
  424. endGroup( "Transform" );
  425. addGroup( "Editing" );
  426. addProtectedField( "isRenderEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  427. &_setRenderEnabled, &_getRenderEnabled,
  428. "Controls client-side rendering of the object.\n"
  429. "@see isRenderable()\n" );
  430. addProtectedField( "isSelectionEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  431. &_setSelectionEnabled, &_getSelectionEnabled,
  432. "Determines if the object may be selected from wihin the Tools.\n"
  433. "@see isSelectable()\n" );
  434. endGroup( "Editing" );
  435. addGroup( "Mounting" );
  436. addProtectedField( "mountPID", TypePID, Offset( mMountPID, SceneObject ), &_setMountPID, &defaultProtectedGetFn,
  437. "@brief PersistentID of object we are mounted to.\n\n"
  438. "Unlike the SimObjectID that is determined at run time, the PersistentID of an object is saved with the level/mission and "
  439. "may be used to form a link between objects." );
  440. addField( "mountNode", TypeS32, Offset( mMount.node, SceneObject ), "Node we are mounted to." );
  441. addField( "mountPos", TypeMatrixPosition, Offset( mMount.xfm, SceneObject ), "Position we are mounted at ( object space of our mount object )." );
  442. addField( "mountRot", TypeMatrixRotation, Offset( mMount.xfm, SceneObject ), "Rotation we are mounted at ( object space of our mount object )." );
  443. endGroup( "Mounting" );
  444. Parent::initPersistFields();
  445. }
  446. //-----------------------------------------------------------------------------
  447. bool SceneObject::_setFieldPosition( void *object, const char *index, const char *data )
  448. {
  449. SceneObject* so = static_cast<SceneObject*>( object );
  450. if ( so )
  451. {
  452. MatrixF txfm( so->getTransform() );
  453. Con::setData( TypeMatrixPosition, &txfm, 0, 1, &data );
  454. so->setTransform( txfm );
  455. }
  456. return false;
  457. }
  458. //-----------------------------------------------------------------------------
  459. bool SceneObject::_setFieldRotation( void *object, const char *index, const char *data )
  460. {
  461. SceneObject* so = static_cast<SceneObject*>( object );
  462. if ( so )
  463. {
  464. MatrixF txfm( so->getTransform() );
  465. Con::setData( TypeMatrixRotation, &txfm, 0, 1, &data );
  466. so->setTransform( txfm );
  467. }
  468. return false;
  469. }
  470. //-----------------------------------------------------------------------------
  471. bool SceneObject::_setFieldScale( void *object, const char *index, const char *data )
  472. {
  473. SceneObject* so = static_cast<SceneObject*>( object );
  474. if ( so )
  475. {
  476. Point3F scale;
  477. Con::setData( TypePoint3F, &scale, 0, 1, &data );
  478. so->setScale( scale );
  479. }
  480. return false;
  481. }
  482. //-----------------------------------------------------------------------------
  483. bool SceneObject::writeField( StringTableEntry fieldName, const char* value )
  484. {
  485. if( !Parent::writeField( fieldName, value ) )
  486. return false;
  487. static StringTableEntry sIsRenderEnabled = StringTable->insert( "isRenderEnabled" );
  488. static StringTableEntry sIsSelectionEnabled = StringTable->insert( "isSelectionEnabled" );
  489. static StringTableEntry sMountNode = StringTable->insert( "mountNode" );
  490. static StringTableEntry sMountPos = StringTable->insert( "mountPos" );
  491. static StringTableEntry sMountRot = StringTable->insert( "mountRot" );
  492. // Don't write flag fields if they are at their default values.
  493. if( fieldName == sIsRenderEnabled && dAtob( value ) )
  494. return false;
  495. else if( fieldName == sIsSelectionEnabled && dAtob( value ) )
  496. return false;
  497. else if ( mMountPID == NULL && ( fieldName == sMountNode ||
  498. fieldName == sMountPos ||
  499. fieldName == sMountRot ) )
  500. {
  501. return false;
  502. }
  503. return true;
  504. }
  505. //-----------------------------------------------------------------------------
  506. static void scopeCallback( SceneObject* obj, void* conPtr )
  507. {
  508. NetConnection* ptr = reinterpret_cast< NetConnection* >( conPtr );
  509. if( obj->isScopeable() )
  510. ptr->objectInScope(obj);
  511. }
  512. void SceneObject::onCameraScopeQuery( NetConnection* connection, CameraScopeQuery* query )
  513. {
  514. SceneManager* sceneManager = getSceneManager();
  515. GameConnection* conn = dynamic_cast<GameConnection*> (connection);
  516. if (conn && (query->visibleDistance = conn->getVisibleGhostDistance()) == 0.0f)
  517. if ((query->visibleDistance = sceneManager->getVisibleGhostDistance()) == 0.0f)
  518. query->visibleDistance = sceneManager->getVisibleDistance();
  519. // Object itself is in scope.
  520. if( this->isScopeable() )
  521. connection->objectInScope( this );
  522. // If we're mounted to something, that object is in scope too.
  523. if( isMounted() )
  524. connection->objectInScope( mMount.object );
  525. // If we're added to a scene graph, let the graph do the scene scoping.
  526. // Otherwise just put everything in the server container in scope.
  527. if( getSceneManager() )
  528. getSceneManager()->scopeScene( query, connection );
  529. else
  530. gServerContainer.findObjects( 0xFFFFFFFF, scopeCallback, connection );
  531. }
  532. //-----------------------------------------------------------------------------
  533. bool SceneObject::isRenderEnabled() const
  534. {
  535. AbstractClassRep *classRep = getClassRep();
  536. return ( mObjectFlags.test( RenderEnabledFlag ) && classRep->isRenderEnabled() );
  537. }
  538. //-----------------------------------------------------------------------------
  539. void SceneObject::setRenderEnabled( bool value )
  540. {
  541. if( value )
  542. mObjectFlags.set( RenderEnabledFlag );
  543. else
  544. mObjectFlags.clear( RenderEnabledFlag );
  545. setMaskBits( FlagMask );
  546. }
  547. //-----------------------------------------------------------------------------
  548. const char* SceneObject::_getRenderEnabled( void* object, const char* data )
  549. {
  550. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  551. if( obj->mObjectFlags.test( RenderEnabledFlag ) )
  552. return "1";
  553. else
  554. return "0";
  555. }
  556. //-----------------------------------------------------------------------------
  557. bool SceneObject::_setRenderEnabled( void *object, const char *index, const char *data )
  558. {
  559. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  560. obj->setRenderEnabled( dAtob( data ) );
  561. return false;
  562. }
  563. //-----------------------------------------------------------------------------
  564. bool SceneObject::isSelectionEnabled() const
  565. {
  566. AbstractClassRep *classRep = getClassRep();
  567. return ( mObjectFlags.test( SelectionEnabledFlag ) && classRep->isSelectionEnabled() );
  568. }
  569. //-----------------------------------------------------------------------------
  570. void SceneObject::setSelectionEnabled( bool value )
  571. {
  572. if( value )
  573. mObjectFlags.set( SelectionEnabledFlag );
  574. else
  575. mObjectFlags.clear( SelectionEnabledFlag );
  576. // Not synchronized on network so don't set dirty bit.
  577. }
  578. //-----------------------------------------------------------------------------
  579. const char* SceneObject::_getSelectionEnabled( void* object, const char* data )
  580. {
  581. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  582. if( obj->mObjectFlags.test( SelectionEnabledFlag ) )
  583. return "true";
  584. else
  585. return "false";
  586. }
  587. //-----------------------------------------------------------------------------
  588. bool SceneObject::_setSelectionEnabled( void *object, const char *index, const char *data )
  589. {
  590. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  591. obj->setSelectionEnabled( dAtob( data ) );
  592. return false;
  593. }
  594. //--------------------------------------------------------------------------
  595. U32 SceneObject::packUpdate( NetConnection* conn, U32 mask, BitStream* stream )
  596. {
  597. U32 retMask = Parent::packUpdate( conn, mask, stream );
  598. if ( stream->writeFlag( mask & FlagMask ) )
  599. stream->writeRangedU32( (U32)mObjectFlags, 0, getObjectFlagMax() );
  600. if ( mask & MountedMask )
  601. {
  602. if ( mMount.object )
  603. {
  604. S32 gIndex = conn->getGhostIndex( mMount.object );
  605. if ( stream->writeFlag( gIndex != -1 ) )
  606. {
  607. stream->writeFlag( true );
  608. stream->writeInt( gIndex, NetConnection::GhostIdBitSize );
  609. if ( stream->writeFlag( mMount.node != -1 ) )
  610. stream->writeInt( mMount.node, NumMountPointBits );
  611. mathWrite( *stream, mMount.xfm );
  612. }
  613. else
  614. // Will have to try again later
  615. retMask |= MountedMask;
  616. }
  617. else
  618. // Unmount if this isn't the initial packet
  619. if ( stream->writeFlag( !(mask & InitialUpdateMask) ) )
  620. stream->writeFlag( false );
  621. }
  622. else
  623. stream->writeFlag( false );
  624. return retMask;
  625. }
  626. //-----------------------------------------------------------------------------
  627. void SceneObject::unpackUpdate( NetConnection* conn, BitStream* stream )
  628. {
  629. Parent::unpackUpdate( conn, stream );
  630. // FlagMask
  631. if ( stream->readFlag() )
  632. mObjectFlags = stream->readRangedU32( 0, getObjectFlagMax() );
  633. // MountedMask
  634. if ( stream->readFlag() )
  635. {
  636. if ( stream->readFlag() )
  637. {
  638. S32 gIndex = stream->readInt( NetConnection::GhostIdBitSize );
  639. SceneObject* obj = dynamic_cast<SceneObject*>( conn->resolveGhost( gIndex ) );
  640. S32 node = -1;
  641. if ( stream->readFlag() ) // node != -1
  642. node = stream->readInt( NumMountPointBits );
  643. MatrixF xfm;
  644. mathRead( *stream, &xfm );
  645. if ( !obj )
  646. {
  647. conn->setLastError( "Invalid packet from server." );
  648. return;
  649. }
  650. obj->mountObject( this, node, xfm );
  651. }
  652. else
  653. unmount();
  654. }
  655. }
  656. //-----------------------------------------------------------------------------
  657. void SceneObject::_updateZoningState() const
  658. {
  659. if( mZoneRefDirty )
  660. {
  661. SceneZoneSpaceManager* manager = getSceneManager()->getZoneManager();
  662. if( manager )
  663. manager->updateObject( const_cast< SceneObject* >( this ) );
  664. else
  665. mZoneRefDirty = false;
  666. }
  667. }
  668. //-----------------------------------------------------------------------------
  669. U32 SceneObject::_getCurrZone( const U32 index ) const
  670. {
  671. _updateZoningState();
  672. // Not the most efficient way to do this, walking the list,
  673. // but it's an uncommon call...
  674. ZoneRef* walk = mZoneRefHead;
  675. for( U32 i = 0; i < index; ++ i )
  676. {
  677. walk = walk->nextInObj;
  678. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  679. }
  680. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  681. return walk->zone;
  682. }
  683. //-----------------------------------------------------------------------------
  684. Point3F SceneObject::getPosition() const
  685. {
  686. Point3F pos;
  687. mObjToWorld.getColumn(3, &pos);
  688. return pos;
  689. }
  690. //-----------------------------------------------------------------------------
  691. Point3F SceneObject::getRenderPosition() const
  692. {
  693. Point3F pos;
  694. mRenderObjToWorld.getColumn(3, &pos);
  695. return pos;
  696. }
  697. //-----------------------------------------------------------------------------
  698. void SceneObject::setPosition(const Point3F &pos)
  699. {
  700. AssertFatal( !mIsNaN( pos ), "SceneObject::setPosition() - The position is NaN!" );
  701. MatrixF xform = mObjToWorld;
  702. xform.setColumn(3, pos);
  703. setTransform(xform);
  704. }
  705. //-----------------------------------------------------------------------------
  706. F32 SceneObject::distanceTo(const Point3F &pnt) const
  707. {
  708. return mWorldBox.getDistanceToPoint( pnt );
  709. }
  710. //-----------------------------------------------------------------------------
  711. void SceneObject::processAfter( ProcessObject *obj )
  712. {
  713. AssertFatal( dynamic_cast<SceneObject*>( obj ), "SceneObject::processAfter - Got non-SceneObject!" );
  714. mAfterObject = (SceneObject*)obj;
  715. if ( mAfterObject->mAfterObject == this )
  716. mAfterObject->mAfterObject = NULL;
  717. getProcessList()->markDirty();
  718. }
  719. //-----------------------------------------------------------------------------
  720. void SceneObject::clearProcessAfter()
  721. {
  722. mAfterObject = NULL;
  723. }
  724. //-----------------------------------------------------------------------------
  725. void SceneObject::setProcessTick( bool t )
  726. {
  727. if ( t == mProcessTick )
  728. return;
  729. if ( mProcessTick )
  730. {
  731. plUnlink();
  732. mProcessTick = false;
  733. }
  734. else
  735. {
  736. // Just to be sure...
  737. plUnlink();
  738. getProcessList()->addObject( this );
  739. mProcessTick = true;
  740. }
  741. }
  742. //-----------------------------------------------------------------------------
  743. ProcessList* SceneObject::getProcessList() const
  744. {
  745. if ( isClientObject() )
  746. return ClientProcessList::get();
  747. else
  748. return ServerProcessList::get();
  749. }
  750. //-------------------------------------------------------------------------
  751. bool SceneObject::isMounted()
  752. {
  753. resolveMountPID();
  754. return mMount.object != NULL;
  755. }
  756. //-----------------------------------------------------------------------------
  757. S32 SceneObject::getMountedObjectCount()
  758. {
  759. S32 count = 0;
  760. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  761. count++;
  762. return count;
  763. }
  764. //-----------------------------------------------------------------------------
  765. SceneObject* SceneObject::getMountedObject(S32 idx)
  766. {
  767. if (idx >= 0) {
  768. S32 count = 0;
  769. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  770. if (count++ == idx)
  771. return itr;
  772. }
  773. return NULL;
  774. }
  775. //-----------------------------------------------------------------------------
  776. S32 SceneObject::getMountedObjectNode(S32 idx)
  777. {
  778. if (idx >= 0) {
  779. S32 count = 0;
  780. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  781. if (count++ == idx)
  782. return itr->mMount.node;
  783. }
  784. return -1;
  785. }
  786. //-----------------------------------------------------------------------------
  787. SceneObject* SceneObject::getMountNodeObject(S32 node)
  788. {
  789. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  790. if (itr->mMount.node == node)
  791. return itr;
  792. return NULL;
  793. }
  794. //-----------------------------------------------------------------------------
  795. bool SceneObject::_setMountPID( void* object, const char* index, const char* data )
  796. {
  797. SceneObject* so = static_cast<SceneObject*>( object );
  798. if ( so )
  799. {
  800. // Unmount old object (PID reference is released even if it had been resolved yet)
  801. if ( so->mMountPID )
  802. {
  803. so->mMountPID->decRefCount();
  804. so->mMountPID = NULL;
  805. }
  806. so->unmount();
  807. // Get the new PID (new object will be mounted on demand)
  808. Con::setData( TypePID, &so->mMountPID, 0, 1, &data );
  809. if ( so->mMountPID )
  810. so->mMountPID->incRefCount(); // Prevent PID from being deleted out from under us!
  811. }
  812. return false;
  813. }
  814. void SceneObject::resolveMountPID()
  815. {
  816. if ( mMountPID && !mMount.object )
  817. {
  818. SceneObject *obj = dynamic_cast< SceneObject* >( mMountPID->getObject() );
  819. if ( obj )
  820. obj->mountObject( this, mMount.node, mMount.xfm );
  821. }
  822. }
  823. //-----------------------------------------------------------------------------
  824. void SceneObject::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  825. {
  826. if ( obj->mMount.object == this )
  827. {
  828. // Already mounted to this
  829. // So update our node and xfm which may have changed.
  830. obj->mMount.node = node;
  831. obj->mMount.xfm = xfm;
  832. }
  833. else
  834. {
  835. if ( obj->mMount.object )
  836. obj->unmount();
  837. obj->mMount.object = this;
  838. obj->mMount.node = node;
  839. obj->mMount.link = mMount.list;
  840. obj->mMount.xfm = xfm;
  841. mMount.list = obj;
  842. // Assign PIDs to both objects
  843. if ( isServerObject() )
  844. {
  845. obj->getOrCreatePersistentId();
  846. if ( !obj->mMountPID )
  847. {
  848. obj->mMountPID = getOrCreatePersistentId();
  849. obj->mMountPID->incRefCount();
  850. }
  851. }
  852. obj->onMount( this, node );
  853. }
  854. }
  855. //-----------------------------------------------------------------------------
  856. void SceneObject::unmountObject( SceneObject *obj )
  857. {
  858. if ( obj->mMount.object == this )
  859. {
  860. // Find and unlink the object
  861. for ( SceneObject **ptr = &mMount.list; *ptr; ptr = &(*ptr)->mMount.link )
  862. {
  863. if ( *ptr == obj )
  864. {
  865. *ptr = obj->mMount.link;
  866. break;
  867. }
  868. }
  869. obj->mMount.object = NULL;
  870. obj->mMount.link = NULL;
  871. if( obj->mMountPID != NULL ) // Only on server.
  872. {
  873. obj->mMountPID->decRefCount();
  874. obj->mMountPID = NULL;
  875. }
  876. obj->onUnmount( this, obj->mMount.node );
  877. }
  878. }
  879. //-----------------------------------------------------------------------------
  880. void SceneObject::unmount()
  881. {
  882. if (mMount.object)
  883. mMount.object->unmountObject(this);
  884. }
  885. //-----------------------------------------------------------------------------
  886. void SceneObject::onMount( SceneObject *obj, S32 node )
  887. {
  888. deleteNotify( obj );
  889. if ( !isGhost() )
  890. {
  891. setMaskBits( MountedMask );
  892. //onMount_callback( node );
  893. }
  894. }
  895. //-----------------------------------------------------------------------------
  896. void SceneObject::onUnmount( SceneObject *obj, S32 node )
  897. {
  898. clearNotify(obj);
  899. if ( !isGhost() )
  900. {
  901. setMaskBits( MountedMask );
  902. //onUnmount_callback( node );
  903. }
  904. }
  905. //-----------------------------------------------------------------------------
  906. void SceneObject::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  907. {
  908. MatrixF mountTransform( xfm );
  909. const Point3F &scale = getScale();
  910. Point3F position = mountTransform.getPosition();
  911. position.convolve( scale );
  912. mountTransform.setPosition( position );
  913. outMat->mul( mObjToWorld, mountTransform );
  914. }
  915. //-----------------------------------------------------------------------------
  916. void SceneObject::getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat )
  917. {
  918. MatrixF mountTransform( xfm );
  919. const Point3F &scale = getScale();
  920. Point3F position = mountTransform.getPosition();
  921. position.convolve( scale );
  922. mountTransform.setPosition( position );
  923. outMat->mul( mRenderObjToWorld, mountTransform );
  924. }
  925. //=============================================================================
  926. // Console API.
  927. //=============================================================================
  928. // MARK: ---- Console API ----
  929. //-----------------------------------------------------------------------------
  930. DefineEngineMethod( SceneObject, getType, S32, (),,
  931. "Return the type mask for this object.\n"
  932. "@return The numeric type mask for the object." )
  933. {
  934. return object->getTypeMask();
  935. }
  936. //-----------------------------------------------------------------------------
  937. DefineEngineMethod( SceneObject, mountObject, bool,
  938. ( SceneObject* objB, S32 slot, TransformF txfm ), ( MatrixF::Identity ),
  939. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  940. "@param objB Object to mount onto us\n"
  941. "@param slot Mount slot ID\n"
  942. "@param txfm (optional) mount offset transform\n"
  943. "@return true if successful, false if failed (objB is not valid)" )
  944. {
  945. if ( objB )
  946. {
  947. object->mountObject( objB, slot, txfm.getMatrix() );
  948. return true;
  949. }
  950. return false;
  951. }
  952. //-----------------------------------------------------------------------------
  953. DefineEngineMethod( SceneObject, unmountObject, bool, ( SceneObject* target ),,
  954. "@brief Unmount an object from ourselves.\n\n"
  955. "@param target object to unmount\n"
  956. "@return true if successful, false if failed\n" )
  957. {
  958. if ( target )
  959. {
  960. object->unmountObject(target);
  961. return true;
  962. }
  963. return false;
  964. }
  965. //-----------------------------------------------------------------------------
  966. DefineEngineMethod( SceneObject, unmount, void, (),,
  967. "Unmount us from the currently mounted object if any.\n" )
  968. {
  969. object->unmount();
  970. }
  971. //-----------------------------------------------------------------------------
  972. DefineEngineMethod( SceneObject, isMounted, bool, (),,
  973. "@brief Check if we are mounted to another object.\n\n"
  974. "@return true if mounted to another object, false if not mounted." )
  975. {
  976. return object->isMounted();
  977. }
  978. //-----------------------------------------------------------------------------
  979. DefineEngineMethod( SceneObject, getObjectMount, S32, (),,
  980. "@brief Get the object we are mounted to.\n\n"
  981. "@return the SimObjectID of the object we're mounted to, or 0 if not mounted." )
  982. {
  983. return object->isMounted()? object->getObjectMount()->getId(): 0;
  984. }
  985. //-----------------------------------------------------------------------------
  986. DefineEngineMethod( SceneObject, getMountedObjectCount, S32, (),,
  987. "Get the number of objects mounted to us.\n"
  988. "@return the number of mounted objects." )
  989. {
  990. return object->getMountedObjectCount();
  991. }
  992. //-----------------------------------------------------------------------------
  993. DefineEngineMethod( SceneObject, getMountedObject, S32, ( S32 slot ),,
  994. "Get the object mounted at a particular slot.\n"
  995. "@param slot mount slot index to query\n"
  996. "@return ID of the object mounted in the slot, or 0 if no object." )
  997. {
  998. SceneObject* mobj = object->getMountedObject( slot );
  999. return mobj? mobj->getId(): 0;
  1000. }
  1001. //-----------------------------------------------------------------------------
  1002. DefineEngineMethod( SceneObject, getMountedObjectNode, S32, ( S32 slot ),,
  1003. "@brief Get the mount node index of the object mounted at our given slot.\n\n"
  1004. "@param slot mount slot index to query\n"
  1005. "@return index of the mount node used by the object mounted in this slot." )
  1006. {
  1007. return object->getMountedObjectNode( slot );
  1008. }
  1009. //-----------------------------------------------------------------------------
  1010. DefineEngineMethod( SceneObject, getMountNodeObject, S32, ( S32 node ),,
  1011. "@brief Get the object mounted at our given node index.\n\n"
  1012. "@param node mount node index to query\n"
  1013. "@return ID of the first object mounted at the node, or 0 if none found." )
  1014. {
  1015. SceneObject* mobj = object->getMountNodeObject( node );
  1016. return mobj? mobj->getId(): 0;
  1017. }
  1018. //-----------------------------------------------------------------------------
  1019. DefineEngineMethod( SceneObject, getTransform, TransformF, (),,
  1020. "Get the object's transform.\n"
  1021. "@return the current transform of the object\n" )
  1022. {
  1023. return object->getTransform();
  1024. }
  1025. //-----------------------------------------------------------------------------
  1026. DefineEngineMethod( SceneObject, getInverseTransform, TransformF, (),,
  1027. "Get the object's inverse transform.\n"
  1028. "@return the inverse transform of the object\n" )
  1029. {
  1030. return object->getWorldTransform();
  1031. }
  1032. //-----------------------------------------------------------------------------
  1033. DefineEngineMethod( SceneObject, getPosition, Point3F, (),,
  1034. "Get the object's world position.\n"
  1035. "@return the current world position of the object\n" )
  1036. {
  1037. return object->getTransform().getPosition();
  1038. }
  1039. DefineEngineMethod( SceneObject, setPosition, void, (Point3F pos),,
  1040. "Set the object's world position.\n"
  1041. "@param pos the new world position of the object\n" )
  1042. {
  1043. return object->setPosition(pos);
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. DefineEngineMethod( SceneObject, getEulerRotation, Point3F, (),,
  1047. "Get Euler rotation of this object.\n"
  1048. "@return the orientation of the object in the form of rotations around the "
  1049. "X, Y and Z axes in degrees.\n" )
  1050. {
  1051. Point3F euler = object->getTransform().toEuler();
  1052. // Convert to degrees.
  1053. euler.x = mRadToDeg( euler.x );
  1054. euler.y = mRadToDeg( euler.y );
  1055. euler.z = mRadToDeg( euler.z );
  1056. return euler;
  1057. }
  1058. //-----------------------------------------------------------------------------
  1059. DefineEngineMethod( SceneObject, getForwardVector, VectorF, (),,
  1060. "Get the direction this object is facing.\n"
  1061. "@return a vector indicating the direction this object is facing.\n"
  1062. "@note This is the object's y axis." )
  1063. {
  1064. return object->getTransform().getForwardVector();
  1065. }
  1066. //-----------------------------------------------------------------------------
  1067. DefineEngineMethod( SceneObject, getRightVector, VectorF, (),,
  1068. "Get the right vector of the object.\n"
  1069. "@return a vector indicating the right direction of this object."
  1070. "@note This is the object's x axis." )
  1071. {
  1072. return object->getTransform().getRightVector();
  1073. }
  1074. //-----------------------------------------------------------------------------
  1075. DefineEngineMethod( SceneObject, getUpVector, VectorF, (),,
  1076. "Get the up vector of the object.\n"
  1077. "@return a vector indicating the up direction of this object."
  1078. "@note This is the object's z axis." )
  1079. {
  1080. return object->getTransform().getUpVector();
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. DefineEngineMethod( SceneObject, setTransform, void, ( TransformF txfm ),,
  1084. "Set the object's transform (orientation and position)."
  1085. "@param txfm object transform to set" )
  1086. {
  1087. if ( !txfm.hasRotation() )
  1088. object->setPosition( txfm.getPosition() );
  1089. else
  1090. object->setTransform( txfm.getMatrix() );
  1091. }
  1092. //-----------------------------------------------------------------------------
  1093. DefineEngineMethod( SceneObject, getScale, Point3F, (),,
  1094. "Get the object's scale.\n"
  1095. "@return object scale as a Point3F" )
  1096. {
  1097. return object->getScale();
  1098. }
  1099. //-----------------------------------------------------------------------------
  1100. DefineEngineMethod( SceneObject, setScale, void, ( Point3F scale ),,
  1101. "Set the object's scale.\n"
  1102. "@param scale object scale to set\n" )
  1103. {
  1104. object->setScale( scale );
  1105. }
  1106. //-----------------------------------------------------------------------------
  1107. DefineEngineMethod( SceneObject, getWorldBox, Box3F, (),,
  1108. "Get the object's world bounding box.\n"
  1109. "@return six fields, two Point3Fs, containing the min and max points of the "
  1110. "worldbox." )
  1111. {
  1112. return object->getWorldBox();
  1113. }
  1114. //-----------------------------------------------------------------------------
  1115. DefineEngineMethod( SceneObject, getWorldBoxCenter, Point3F, (),,
  1116. "Get the center of the object's world bounding box.\n"
  1117. "@return the center of the world bounding box for this object." )
  1118. {
  1119. Point3F center;
  1120. object->getWorldBox().getCenter( &center );
  1121. return center;
  1122. }
  1123. //-----------------------------------------------------------------------------
  1124. DefineEngineMethod( SceneObject, getObjectBox, Box3F, (),,
  1125. "Get the object's bounding box (relative to the object's origin).\n"
  1126. "@return six fields, two Point3Fs, containing the min and max points of the "
  1127. "objectbox." )
  1128. {
  1129. return object->getObjBox();
  1130. }
  1131. //-----------------------------------------------------------------------------
  1132. DefineEngineMethod( SceneObject, isGlobalBounds, bool, (),,
  1133. "Check if this object has a global bounds set.\n"
  1134. "If global bounds are set to be true, then the object is assumed to have an "
  1135. "infinitely large bounding box for collision and rendering purposes.\n"
  1136. "@return true if the object has a global bounds." )
  1137. {
  1138. return object->isGlobalBounds();
  1139. }