sceneObject.cpp 45 KB

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