sceneObject.cpp 46 KB

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