sceneObject.cpp 43 KB

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