sceneObject.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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. // PATHSHAPE
  124. // init the scenegraph relationships to indicate no parent, no children, and no siblings
  125. mGraph.parent = NULL;
  126. mGraph.nextSibling = NULL;
  127. mGraph.firstChild = NULL;
  128. mGraph.objToParent.identity();
  129. // PATHSHAPE END
  130. mIsScopeAlways = false;
  131. mAccuTex = NULL;
  132. mSelectionFlags = 0;
  133. mPathfindingIgnore = false;
  134. mGameObjectAssetId = StringTable->insert("");
  135. mDirtyGameObject = false;
  136. }
  137. //-----------------------------------------------------------------------------
  138. SceneObject::~SceneObject()
  139. {
  140. AssertFatal( mZoneRefHead == NULL && mBinRefHead == NULL,
  141. "SceneObject::~SceneObject - Object still linked in reference lists!");
  142. AssertFatal( !mSceneObjectLinks,
  143. "SceneObject::~SceneObject() - object is still linked to SceneTrackers" );
  144. mAccuTex = NULL;
  145. unlink();
  146. }
  147. //-----------------------------------------------------------------------------
  148. bool SceneObject::castRayRendered(const Point3F &start, const Point3F &end, RayInfo *info)
  149. {
  150. // By default, all ray checking against the rendered mesh will be passed
  151. // on to the collision mesh. This saves having to define both methods
  152. // for simple objects.
  153. return castRay( start, end, info );
  154. }
  155. //-----------------------------------------------------------------------------
  156. bool SceneObject::containsPoint( const Point3F& point )
  157. {
  158. // If it's not in the AABB, then it can't be in the OBB either,
  159. // so early out.
  160. if( !mWorldBox.isContained( point ) )
  161. return false;
  162. // Transform point into object space and test it against
  163. // our object space bounding box.
  164. Point3F objPoint( 0, 0, 0 );
  165. getWorldTransform().mulP( point, &objPoint );
  166. objPoint.convolveInverse( getScale() );
  167. return ( mObjBox.isContained( objPoint ) );
  168. }
  169. //-----------------------------------------------------------------------------
  170. bool SceneObject::collideBox(const Point3F &start, const Point3F &end, RayInfo *info)
  171. {
  172. const F32 * pStart = (const F32*)start;
  173. const F32 * pEnd = (const F32*)end;
  174. const F32 * pMin = (const F32*)mObjBox.minExtents;
  175. const F32 * pMax = (const F32*)mObjBox.maxExtents;
  176. F32 maxStartTime = -1;
  177. F32 minEndTime = 1;
  178. F32 startTime;
  179. F32 endTime;
  180. // used for getting normal
  181. U32 hitIndex = 0xFFFFFFFF;
  182. U32 side;
  183. // walk the axis
  184. for(U32 i = 0; i < 3; i++)
  185. {
  186. //
  187. if(pStart[i] < pEnd[i])
  188. {
  189. if(pEnd[i] < pMin[i] || pStart[i] > pMax[i])
  190. return(false);
  191. F32 dist = pEnd[i] - pStart[i];
  192. startTime = (pStart[i] < pMin[i]) ? (pMin[i] - pStart[i]) / dist : -1;
  193. endTime = (pEnd[i] > pMax[i]) ? (pMax[i] - pStart[i]) / dist : 1;
  194. side = 1;
  195. }
  196. else
  197. {
  198. if(pStart[i] < pMin[i] || pEnd[i] > pMax[i])
  199. return(false);
  200. F32 dist = pStart[i] - pEnd[i];
  201. startTime = (pStart[i] > pMax[i]) ? (pStart[i] - pMax[i]) / dist : -1;
  202. endTime = (pEnd[i] < pMin[i]) ? (pStart[i] - pMin[i]) / dist : 1;
  203. side = 0;
  204. }
  205. //
  206. if(startTime > maxStartTime)
  207. {
  208. maxStartTime = startTime;
  209. hitIndex = i * 2 + side;
  210. }
  211. if(endTime < minEndTime)
  212. minEndTime = endTime;
  213. if(minEndTime < maxStartTime)
  214. return(false);
  215. }
  216. // fail if inside
  217. if(maxStartTime < 0.f)
  218. return(false);
  219. //
  220. static Point3F boxNormals[] = {
  221. Point3F( 1, 0, 0),
  222. Point3F(-1, 0, 0),
  223. Point3F( 0, 1, 0),
  224. Point3F( 0,-1, 0),
  225. Point3F( 0, 0, 1),
  226. Point3F( 0, 0,-1),
  227. };
  228. //
  229. AssertFatal(hitIndex != 0xFFFFFFFF, "SceneObject::collideBox");
  230. info->t = maxStartTime;
  231. info->object = this;
  232. mObjToWorld.mulV(boxNormals[hitIndex], &info->normal);
  233. info->material = 0;
  234. return(true);
  235. }
  236. //-----------------------------------------------------------------------------
  237. void SceneObject::disableCollision()
  238. {
  239. mCollisionCount++;
  240. AssertFatal(mCollisionCount < 50, "SceneObject::disableCollision called 50 times on the same object. Is this inside a circular loop?" );
  241. }
  242. //-----------------------------------------------------------------------------
  243. void SceneObject::enableCollision()
  244. {
  245. if (mCollisionCount)
  246. --mCollisionCount;
  247. }
  248. //-----------------------------------------------------------------------------
  249. bool SceneObject::onAdd()
  250. {
  251. if ( !Parent::onAdd() )
  252. return false;
  253. mIsScopeAlways = mNetFlags.test( ScopeAlways );
  254. mWorldToObj = mObjToWorld;
  255. mWorldToObj.affineInverse();
  256. resetWorldBox();
  257. setRenderTransform(mObjToWorld);
  258. resolveMountPID();
  259. smSceneObjectAdd.trigger(this);
  260. return true;
  261. }
  262. //-----------------------------------------------------------------------------
  263. void SceneObject::onRemove()
  264. {
  265. smSceneObjectRemove.trigger(this);
  266. unmount();
  267. plUnlink();
  268. Parent::onRemove();
  269. // PATHSHAPE
  270. if ( getParent() != NULL) attachToParent( NULL);
  271. // PATHSHAPE END
  272. }
  273. //-----------------------------------------------------------------------------
  274. void SceneObject::addToScene()
  275. {
  276. if( mSceneManager )
  277. return;
  278. if( isClientObject() )
  279. gClientSceneGraph->addObjectToScene( this );
  280. else
  281. gServerSceneGraph->addObjectToScene( this );
  282. }
  283. //-----------------------------------------------------------------------------
  284. void SceneObject::removeFromScene()
  285. {
  286. if( !mSceneManager )
  287. return;
  288. mSceneManager->removeObjectFromScene( this );
  289. }
  290. //-----------------------------------------------------------------------------
  291. void SceneObject::onDeleteNotify( SimObject *obj )
  292. {
  293. // We are comparing memory addresses so even if obj really is not a
  294. // ProcessObject this cast shouldn't break anything.
  295. if ( obj == mAfterObject )
  296. mAfterObject = NULL;
  297. if ( obj == mMount.object )
  298. unmount();
  299. Parent::onDeleteNotify( obj );
  300. }
  301. //-----------------------------------------------------------------------------
  302. void SceneObject::inspectPostApply()
  303. {
  304. if( isServerObject() )
  305. setMaskBits( MountedMask );
  306. Parent::inspectPostApply();
  307. }
  308. //-----------------------------------------------------------------------------
  309. void SceneObject::setGlobalBounds()
  310. {
  311. mGlobalBounds = true;
  312. mObjBox.minExtents.set( -1e10, -1e10, -1e10 );
  313. mObjBox.maxExtents.set( 1e10, 1e10, 1e10 );
  314. if( mSceneManager )
  315. mSceneManager->notifyObjectDirty( this );
  316. }
  317. //-----------------------------------------------------------------------------
  318. void SceneObject::setTransform( const MatrixF& mat )
  319. {
  320. // This test is a bit expensive so turn it off in release.
  321. #ifdef TORQUE_DEBUG
  322. //AssertFatal( mat.isAffine(), "SceneObject::setTransform() - Bad transform (non affine)!" );
  323. #endif
  324. PROFILE_SCOPE( SceneObject_setTransform );
  325. // PATHSHAPE
  326. PerformUpdatesForChildren(mat);
  327. // PATHSHAPE END
  328. // Update the transforms.
  329. mObjToWorld = mWorldToObj = mat;
  330. mWorldToObj.affineInverse();
  331. // Update the world-space AABB.
  332. resetWorldBox();
  333. // If we're in a SceneManager, sync our scene state.
  334. if( mSceneManager != NULL )
  335. mSceneManager->notifyObjectDirty( this );
  336. setRenderTransform( mat );
  337. }
  338. //-----------------------------------------------------------------------------
  339. void SceneObject::setScale( const VectorF &scale )
  340. {
  341. AssertFatal( !mIsNaN( scale ), "SceneObject::setScale() - The scale is NaN!" );
  342. // Avoid unnecessary scaling operations.
  343. if ( mObjScale.equal( scale ) )
  344. return;
  345. mObjScale = scale;
  346. setTransform(MatrixF(mObjToWorld));
  347. // Make sure that any subclasses of me get a chance to react to the
  348. // scale being changed.
  349. onScaleChanged();
  350. setMaskBits( ScaleMask );
  351. }
  352. void SceneObject::setForwardVector(VectorF newForward, VectorF upVector)
  353. {
  354. MatrixF mat = getTransform();
  355. VectorF up(0.0f, 0.0f, 1.0f);
  356. VectorF axisX;
  357. VectorF axisY = newForward;
  358. VectorF axisZ;
  359. if (upVector != VectorF::Zero)
  360. up = upVector;
  361. // Validate and normalize input:
  362. F32 lenSq;
  363. lenSq = axisY.lenSquared();
  364. if (lenSq < 0.000001f)
  365. {
  366. axisY.set(0.0f, 1.0f, 0.0f);
  367. Con::errorf("SceneObject::setForwardVector() - degenerate forward vector");
  368. }
  369. else
  370. {
  371. axisY /= mSqrt(lenSq);
  372. }
  373. lenSq = up.lenSquared();
  374. if (lenSq < 0.000001f)
  375. {
  376. up.set(0.0f, 0.0f, 1.0f);
  377. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
  378. }
  379. else
  380. {
  381. up /= mSqrt(lenSq);
  382. }
  383. if (fabsf(mDot(up, axisY)) > 0.9999f)
  384. {
  385. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
  386. // I haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
  387. F32 tmp = up.x;
  388. up.x = -up.y;
  389. up.y = up.z;
  390. up.z = tmp;
  391. }
  392. // construct the remaining axes:
  393. mCross(axisY, up, &axisX);
  394. mCross(axisX, axisY, &axisZ);
  395. mat.setColumn(0, axisX);
  396. mat.setColumn(1, axisY);
  397. mat.setColumn(2, axisZ);
  398. setTransform(mat);
  399. }
  400. //-----------------------------------------------------------------------------
  401. void SceneObject::resetWorldBox()
  402. {
  403. AssertFatal(mObjBox.isValidBox(), "SceneObject::resetWorldBox - Bad object box!");
  404. mWorldBox = mObjBox;
  405. mWorldBox.minExtents.convolve(mObjScale);
  406. mWorldBox.maxExtents.convolve(mObjScale);
  407. mObjToWorld.mul(mWorldBox);
  408. AssertFatal(mWorldBox.isValidBox(), "SceneObject::resetWorldBox - Bad world box!");
  409. // Create mWorldSphere from mWorldBox
  410. mWorldBox.getCenter(&mWorldSphere.center);
  411. mWorldSphere.radius = (mWorldBox.maxExtents - mWorldSphere.center).len();
  412. // Update tracker links.
  413. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  414. link = link->getNextLink() )
  415. link->update();
  416. }
  417. //-----------------------------------------------------------------------------
  418. void SceneObject::resetObjectBox()
  419. {
  420. AssertFatal( mWorldBox.isValidBox(), "SceneObject::resetObjectBox - Bad world box!" );
  421. mObjBox = mWorldBox;
  422. mWorldToObj.mul( mObjBox );
  423. Point3F objScale( mObjScale );
  424. objScale.setMax( Point3F( (F32)POINT_EPSILON, (F32)POINT_EPSILON, (F32)POINT_EPSILON ) );
  425. mObjBox.minExtents.convolveInverse( objScale );
  426. mObjBox.maxExtents.convolveInverse( objScale );
  427. AssertFatal( mObjBox.isValidBox(), "SceneObject::resetObjectBox - Bad object box!" );
  428. // Update the mWorldSphere from mWorldBox
  429. mWorldBox.getCenter( &mWorldSphere.center );
  430. mWorldSphere.radius = ( mWorldBox.maxExtents - mWorldSphere.center ).len();
  431. // Update scene managers.
  432. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  433. link = link->getNextLink() )
  434. link->update();
  435. }
  436. //-----------------------------------------------------------------------------
  437. void SceneObject::setRenderTransform(const MatrixF& mat)
  438. {
  439. PROFILE_START(SceneObj_setRenderTransform);
  440. mRenderObjToWorld = mRenderWorldToObj = mat;
  441. mRenderWorldToObj.affineInverse();
  442. AssertFatal(mObjBox.isValidBox(), "Bad object box!");
  443. resetRenderWorldBox();
  444. PROFILE_END();
  445. }
  446. //-----------------------------------------------------------------------------
  447. void SceneObject::resetRenderWorldBox()
  448. {
  449. AssertFatal( mObjBox.isValidBox(), "Bad object box!" );
  450. mRenderWorldBox = mObjBox;
  451. mRenderWorldBox.minExtents.convolve( mObjScale );
  452. mRenderWorldBox.maxExtents.convolve( mObjScale );
  453. mRenderObjToWorld.mul( mRenderWorldBox );
  454. AssertFatal( mRenderWorldBox.isValidBox(), "Bad world box!" );
  455. // Create mRenderWorldSphere from mRenderWorldBox.
  456. mRenderWorldBox.getCenter( &mRenderWorldSphere.center );
  457. mRenderWorldSphere.radius = ( mRenderWorldBox.maxExtents - mRenderWorldSphere.center ).len();
  458. }
  459. //-----------------------------------------------------------------------------
  460. void SceneObject::setHidden( bool hidden )
  461. {
  462. if( hidden != isHidden() )
  463. {
  464. // Add/remove the object from the scene. Removing it
  465. // will also cause the NetObject to go out of scope since
  466. // the container query will not find it anymore. However,
  467. // ScopeAlways objects need to be treated separately as we
  468. // do next.
  469. if( !hidden )
  470. addToScene();
  471. else
  472. removeFromScene();
  473. // ScopeAlways objects stay in scope no matter what, i.e. even
  474. // if they aren't in the scene query anymore. So, to force ghosts
  475. // to go away, we need to clear ScopeAlways while we are hidden.
  476. if( hidden && mIsScopeAlways )
  477. clearScopeAlways();
  478. else if( !hidden && mIsScopeAlways )
  479. setScopeAlways();
  480. Parent::setHidden( hidden );
  481. }
  482. }
  483. //-----------------------------------------------------------------------------
  484. void SceneObject::initPersistFields()
  485. {
  486. addGroup("GameObject");
  487. addField("GameObject", TypeGameObjectAssetPtr, Offset(mGameObjectAsset, SceneObject), "The asset Id used for the game object this entity is based on.");
  488. addField("dirtyGameObject", TypeBool, Offset(mDirtyGameObject, SceneObject), "If this entity is a GameObject, it flags if this instance delinates from the template.",
  489. AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  490. endGroup("GameObject");
  491. addGroup( "Transform" );
  492. addProtectedField( "position", TypeMatrixPosition, Offset( mObjToWorld, SceneObject ),
  493. &_setFieldPosition, &defaultProtectedGetFn,
  494. "Object world position." );
  495. addProtectedField( "rotation", TypeMatrixRotation, Offset( mObjToWorld, SceneObject ),
  496. &_setFieldRotation, &defaultProtectedGetFn,
  497. "Object world orientation." );
  498. addProtectedField( "scale", TypePoint3F, Offset( mObjScale, SceneObject ),
  499. &_setFieldScale, &defaultProtectedGetFn,
  500. "Object world scale." );
  501. endGroup( "Transform" );
  502. addGroup( "Editing" );
  503. addProtectedField( "isRenderEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  504. &_setRenderEnabled, &_getRenderEnabled,
  505. "Controls client-side rendering of the object.\n"
  506. "@see isRenderable()\n" );
  507. addProtectedField( "isSelectionEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  508. &_setSelectionEnabled, &_getSelectionEnabled,
  509. "Determines if the object may be selected from wihin the Tools.\n"
  510. "@see isSelectable()\n" );
  511. endGroup( "Editing" );
  512. addGroup( "Mounting" );
  513. addProtectedField( "mountPID", TypePID, Offset( mMountPID, SceneObject ), &_setMountPID, &defaultProtectedGetFn,
  514. "@brief PersistentID of object we are mounted to.\n\n"
  515. "Unlike the SimObjectID that is determined at run time, the PersistentID of an object is saved with the level/mission and "
  516. "may be used to form a link between objects." );
  517. addField( "mountNode", TypeS32, Offset( mMount.node, SceneObject ), "Node we are mounted to." );
  518. addField( "mountPos", TypeMatrixPosition, Offset( mMount.xfm, SceneObject ), "Position we are mounted at ( object space of our mount object )." );
  519. addField( "mountRot", TypeMatrixRotation, Offset( mMount.xfm, SceneObject ), "Rotation we are mounted at ( object space of our mount object )." );
  520. endGroup( "Mounting" );
  521. Parent::initPersistFields();
  522. }
  523. bool SceneObject::_setGameObject(void* object, const char* index, const char* data)
  524. {
  525. // Sanity!
  526. AssertFatal(data != NULL, "Cannot use a NULL asset Id.");
  527. return true; //rbI->setMeshAsset(data);
  528. }
  529. //-----------------------------------------------------------------------------
  530. bool SceneObject::_setFieldPosition( void *object, const char *index, const char *data )
  531. {
  532. SceneObject* so = static_cast<SceneObject*>( object );
  533. if ( so )
  534. {
  535. MatrixF txfm( so->getTransform() );
  536. Con::setData( TypeMatrixPosition, &txfm, 0, 1, &data );
  537. so->setTransform( txfm );
  538. }
  539. return false;
  540. }
  541. //-----------------------------------------------------------------------------
  542. bool SceneObject::_setFieldRotation( void *object, const char *index, const char *data )
  543. {
  544. SceneObject* so = static_cast<SceneObject*>( object );
  545. if ( so )
  546. {
  547. MatrixF txfm( so->getTransform() );
  548. Con::setData( TypeMatrixRotation, &txfm, 0, 1, &data );
  549. so->setTransform( txfm );
  550. }
  551. return false;
  552. }
  553. //-----------------------------------------------------------------------------
  554. bool SceneObject::_setFieldScale( void *object, const char *index, const char *data )
  555. {
  556. SceneObject* so = static_cast<SceneObject*>( object );
  557. if ( so )
  558. {
  559. Point3F scale;
  560. Con::setData( TypePoint3F, &scale, 0, 1, &data );
  561. so->setScale( scale );
  562. }
  563. return false;
  564. }
  565. //-----------------------------------------------------------------------------
  566. bool SceneObject::writeField( StringTableEntry fieldName, const char* value )
  567. {
  568. if( !Parent::writeField( fieldName, value ) )
  569. return false;
  570. static StringTableEntry sIsRenderEnabled = StringTable->insert( "isRenderEnabled" );
  571. static StringTableEntry sIsSelectionEnabled = StringTable->insert( "isSelectionEnabled" );
  572. static StringTableEntry sMountNode = StringTable->insert( "mountNode" );
  573. static StringTableEntry sMountPos = StringTable->insert( "mountPos" );
  574. static StringTableEntry sMountRot = StringTable->insert( "mountRot" );
  575. // Don't write flag fields if they are at their default values.
  576. if( fieldName == sIsRenderEnabled && dAtob( value ) )
  577. return false;
  578. else if( fieldName == sIsSelectionEnabled && dAtob( value ) )
  579. return false;
  580. else if ( mMountPID == NULL && ( fieldName == sMountNode ||
  581. fieldName == sMountPos ||
  582. fieldName == sMountRot ) )
  583. {
  584. return false;
  585. }
  586. return true;
  587. }
  588. //-----------------------------------------------------------------------------
  589. static void scopeCallback( SceneObject* obj, void* conPtr )
  590. {
  591. NetConnection* ptr = reinterpret_cast< NetConnection* >( conPtr );
  592. if( obj->isScopeable() )
  593. ptr->objectInScope(obj);
  594. }
  595. void SceneObject::onCameraScopeQuery( NetConnection* connection, CameraScopeQuery* query )
  596. {
  597. SceneManager* sceneManager = getSceneManager();
  598. GameConnection* conn = dynamic_cast<GameConnection*> (connection);
  599. if (conn && (query->visibleDistance = conn->getVisibleGhostDistance()) == 0.0f)
  600. if ((query->visibleDistance = sceneManager->getVisibleGhostDistance()) == 0.0f)
  601. query->visibleDistance = sceneManager->getVisibleDistance();
  602. // Object itself is in scope.
  603. if( this->isScopeable() )
  604. connection->objectInScope( this );
  605. // If we're mounted to something, that object is in scope too.
  606. if( isMounted() )
  607. connection->objectInScope( mMount.object );
  608. // If we're added to a scene graph, let the graph do the scene scoping.
  609. // Otherwise just put everything in the server container in scope.
  610. if( getSceneManager() )
  611. getSceneManager()->scopeScene( query, connection );
  612. else
  613. gServerContainer.findObjects( 0xFFFFFFFF, scopeCallback, connection );
  614. }
  615. //-----------------------------------------------------------------------------
  616. bool SceneObject::isRenderEnabled() const
  617. {
  618. #ifdef TORQUE_TOOLS
  619. if (gEditingMission)
  620. {
  621. AbstractClassRep *classRep = getClassRep();
  622. return (mObjectFlags.test(RenderEnabledFlag) && classRep->isRenderEnabled());
  623. }
  624. #endif
  625. return (mObjectFlags.test(RenderEnabledFlag));
  626. }
  627. //-----------------------------------------------------------------------------
  628. void SceneObject::setRenderEnabled( bool value )
  629. {
  630. if( value )
  631. mObjectFlags.set( RenderEnabledFlag );
  632. else
  633. mObjectFlags.clear( RenderEnabledFlag );
  634. setMaskBits( FlagMask );
  635. }
  636. //-----------------------------------------------------------------------------
  637. const char* SceneObject::_getRenderEnabled( void* object, const char* data )
  638. {
  639. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  640. if( obj->mObjectFlags.test( RenderEnabledFlag ) )
  641. return "1";
  642. else
  643. return "0";
  644. }
  645. //-----------------------------------------------------------------------------
  646. bool SceneObject::_setRenderEnabled( void *object, const char *index, const char *data )
  647. {
  648. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  649. obj->setRenderEnabled( dAtob( data ) );
  650. return false;
  651. }
  652. //-----------------------------------------------------------------------------
  653. bool SceneObject::isSelectionEnabled() const
  654. {
  655. AbstractClassRep *classRep = getClassRep();
  656. return ( mObjectFlags.test( SelectionEnabledFlag ) && classRep->isSelectionEnabled() );
  657. }
  658. //-----------------------------------------------------------------------------
  659. void SceneObject::setSelectionEnabled( bool value )
  660. {
  661. if( value )
  662. mObjectFlags.set( SelectionEnabledFlag );
  663. else
  664. mObjectFlags.clear( SelectionEnabledFlag );
  665. // Not synchronized on network so don't set dirty bit.
  666. }
  667. //-----------------------------------------------------------------------------
  668. const char* SceneObject::_getSelectionEnabled( void* object, const char* data )
  669. {
  670. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  671. if( obj->mObjectFlags.test( SelectionEnabledFlag ) )
  672. return "true";
  673. else
  674. return "false";
  675. }
  676. //-----------------------------------------------------------------------------
  677. bool SceneObject::_setSelectionEnabled( void *object, const char *index, const char *data )
  678. {
  679. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  680. obj->setSelectionEnabled( dAtob( data ) );
  681. return false;
  682. }
  683. //--------------------------------------------------------------------------
  684. U32 SceneObject::packUpdate( NetConnection* conn, U32 mask, BitStream* stream )
  685. {
  686. U32 retMask = Parent::packUpdate( conn, mask, stream );
  687. if ( stream->writeFlag( mask & FlagMask ) )
  688. stream->writeRangedU32( (U32)mObjectFlags, 0, getObjectFlagMax() );
  689. // PATHSHAPE
  690. //Begin attachment
  691. retMask = 0; //retry mask
  692. if (stream->writeFlag(getParent() != NULL)) {
  693. stream->writeAffineTransform(mGraph.objToParent);
  694. }
  695. if (stream->writeFlag(mask & MountedMask))
  696. {
  697. // Check to see if we need to write an object ID
  698. if (stream->writeFlag(mGraph.parent)) {
  699. S32 t = conn->getGhostIndex(mGraph.parent);
  700. // Check to see if we can actually ghost this...
  701. if (t == -1) {
  702. // Cant, try again later
  703. retMask |= MountedMask;
  704. stream->writeFlag(false);
  705. }
  706. else {
  707. // Can, write it.
  708. stream->writeFlag(true);
  709. stream->writeRangedU32(U32(t), 0, NetConnection::MaxGhostCount);
  710. stream->writeAffineTransform(mGraph.objToParent);
  711. //Con::errorf("%d: sent mounted on %d", getId(), mGraph.parent->getId());
  712. }
  713. }
  714. }
  715. // End of Attachment
  716. // PATHSHAPE END
  717. if ( mask & MountedMask )
  718. {
  719. if ( mMount.object )
  720. {
  721. S32 gIndex = conn->getGhostIndex( mMount.object );
  722. if ( stream->writeFlag( gIndex != -1 ) )
  723. {
  724. stream->writeFlag( true );
  725. stream->writeInt( gIndex, NetConnection::GhostIdBitSize );
  726. if ( stream->writeFlag( mMount.node != -1 ) )
  727. stream->writeInt( mMount.node, NumMountPointBits );
  728. mathWrite( *stream, mMount.xfm );
  729. }
  730. else
  731. // Will have to try again later
  732. retMask |= MountedMask;
  733. }
  734. else
  735. // Unmount if this isn't the initial packet
  736. if ( stream->writeFlag( !(mask & InitialUpdateMask) ) )
  737. stream->writeFlag( false );
  738. }
  739. else
  740. stream->writeFlag( false );
  741. return retMask;
  742. }
  743. //-----------------------------------------------------------------------------
  744. void SceneObject::unpackUpdate( NetConnection* conn, BitStream* stream )
  745. {
  746. Parent::unpackUpdate( conn, stream );
  747. // FlagMask
  748. if ( stream->readFlag() )
  749. mObjectFlags = stream->readRangedU32( 0, getObjectFlagMax() );
  750. // PATHSHAPE
  751. // begin of attachment
  752. if (stream->readFlag())
  753. {
  754. MatrixF m;
  755. stream->readAffineTransform(&m);
  756. mGraph.objToParent = m;
  757. }
  758. if (stream->readFlag())
  759. {
  760. // Check to see if we need to read an object ID
  761. if (stream->readFlag())
  762. {
  763. // Check to see if we can actually ghost this...
  764. if (stream->readFlag())
  765. {
  766. GameBase *newParent = static_cast<GameBase*>(conn->resolveGhost(stream->readRangedU32(0, NetConnection::MaxGhostCount)));
  767. MatrixF m;
  768. stream->readAffineTransform(&m);
  769. if (getParent() != newParent)
  770. {
  771. clearProcessAfter();
  772. processAfter(newParent);
  773. }
  774. attachToParent(newParent, &m);
  775. //Con::errorf("%d: got mounted on %d", getId(), mParentObject->getId());
  776. }
  777. }
  778. else
  779. {
  780. attachToParent(NULL);
  781. }
  782. }
  783. // End of attachment
  784. // PATHSHAPE END
  785. // MountedMask
  786. if ( stream->readFlag() )
  787. {
  788. if ( stream->readFlag() )
  789. {
  790. S32 gIndex = stream->readInt( NetConnection::GhostIdBitSize );
  791. SceneObject* obj = dynamic_cast<SceneObject*>( conn->resolveGhost( gIndex ) );
  792. S32 node = -1;
  793. if ( stream->readFlag() ) // node != -1
  794. node = stream->readInt( NumMountPointBits );
  795. MatrixF xfm;
  796. mathRead( *stream, &xfm );
  797. if ( !obj )
  798. {
  799. conn->setLastError( "Invalid packet from server." );
  800. return;
  801. }
  802. obj->mountObject( this, node, xfm );
  803. }
  804. else
  805. unmount();
  806. }
  807. }
  808. //-----------------------------------------------------------------------------
  809. void SceneObject::_updateZoningState() const
  810. {
  811. if( mZoneRefDirty )
  812. {
  813. SceneZoneSpaceManager* manager = getSceneManager()->getZoneManager();
  814. if( manager )
  815. manager->updateObject( const_cast< SceneObject* >( this ) );
  816. else
  817. mZoneRefDirty = false;
  818. }
  819. }
  820. //-----------------------------------------------------------------------------
  821. U32 SceneObject::getCurrZone( const U32 index ) const
  822. {
  823. _updateZoningState();
  824. // Not the most efficient way to do this, walking the list,
  825. // but it's an uncommon call...
  826. ZoneRef* walk = mZoneRefHead;
  827. for( U32 i = 0; i < index; ++ i )
  828. {
  829. walk = walk->nextInObj;
  830. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  831. }
  832. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  833. return walk->zone;
  834. }
  835. //-----------------------------------------------------------------------------
  836. Point3F SceneObject::getPosition() const
  837. {
  838. Point3F pos;
  839. mObjToWorld.getColumn(3, &pos);
  840. return pos;
  841. }
  842. //-----------------------------------------------------------------------------
  843. Point3F SceneObject::getRenderPosition() const
  844. {
  845. Point3F pos;
  846. mRenderObjToWorld.getColumn(3, &pos);
  847. return pos;
  848. }
  849. //-----------------------------------------------------------------------------
  850. void SceneObject::setPosition(const Point3F &pos)
  851. {
  852. AssertFatal( !mIsNaN( pos ), "SceneObject::setPosition() - The position is NaN!" );
  853. MatrixF xform = mObjToWorld;
  854. xform.setColumn(3, pos);
  855. setTransform(xform);
  856. }
  857. //-----------------------------------------------------------------------------
  858. F32 SceneObject::distanceTo(const Point3F &pnt) const
  859. {
  860. return mWorldBox.getDistanceToPoint( pnt );
  861. }
  862. //-----------------------------------------------------------------------------
  863. void SceneObject::processAfter( ProcessObject *obj )
  864. {
  865. AssertFatal( dynamic_cast<SceneObject*>( obj ), "SceneObject::processAfter - Got non-SceneObject!" );
  866. mAfterObject = (SceneObject*)obj;
  867. if ( mAfterObject->mAfterObject == this )
  868. mAfterObject->mAfterObject = NULL;
  869. getProcessList()->markDirty();
  870. }
  871. //-----------------------------------------------------------------------------
  872. void SceneObject::clearProcessAfter()
  873. {
  874. mAfterObject = NULL;
  875. }
  876. //-----------------------------------------------------------------------------
  877. void SceneObject::setProcessTick( bool t )
  878. {
  879. if ( t == mProcessTick )
  880. return;
  881. if ( mProcessTick )
  882. {
  883. if ( !getMountedObjectCount() )
  884. plUnlink(); // Only unlink if there is nothing mounted to us
  885. mProcessTick = false;
  886. }
  887. else
  888. {
  889. // Just to be sure...
  890. plUnlink();
  891. getProcessList()->addObject( this );
  892. mProcessTick = true;
  893. }
  894. }
  895. //-----------------------------------------------------------------------------
  896. ProcessList* SceneObject::getProcessList() const
  897. {
  898. if ( isClientObject() )
  899. return ClientProcessList::get();
  900. else
  901. return ServerProcessList::get();
  902. }
  903. //-------------------------------------------------------------------------
  904. bool SceneObject::isMounted()
  905. {
  906. resolveMountPID();
  907. return mMount.object != NULL;
  908. }
  909. //-----------------------------------------------------------------------------
  910. S32 SceneObject::getMountedObjectCount()
  911. {
  912. S32 count = 0;
  913. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  914. count++;
  915. return count;
  916. }
  917. //-----------------------------------------------------------------------------
  918. SceneObject* SceneObject::getMountedObject(S32 idx)
  919. {
  920. if (idx >= 0) {
  921. S32 count = 0;
  922. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  923. if (count++ == idx)
  924. return itr;
  925. }
  926. return NULL;
  927. }
  928. //-----------------------------------------------------------------------------
  929. S32 SceneObject::getMountedObjectNode(S32 idx)
  930. {
  931. if (idx >= 0) {
  932. S32 count = 0;
  933. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  934. if (count++ == idx)
  935. return itr->mMount.node;
  936. }
  937. return -1;
  938. }
  939. //-----------------------------------------------------------------------------
  940. SceneObject* SceneObject::getMountNodeObject(S32 node)
  941. {
  942. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  943. if (itr->mMount.node == node)
  944. return itr;
  945. return NULL;
  946. }
  947. //-----------------------------------------------------------------------------
  948. bool SceneObject::_setMountPID( void* object, const char* index, const char* data )
  949. {
  950. SceneObject* so = static_cast<SceneObject*>( object );
  951. if ( so )
  952. {
  953. // Unmount old object (PID reference is released even if it had been resolved yet)
  954. if ( so->mMountPID )
  955. {
  956. so->mMountPID->decRefCount();
  957. so->mMountPID = NULL;
  958. }
  959. so->unmount();
  960. // Get the new PID (new object will be mounted on demand)
  961. Con::setData( TypePID, &so->mMountPID, 0, 1, &data );
  962. if ( so->mMountPID )
  963. so->mMountPID->incRefCount(); // Prevent PID from being deleted out from under us!
  964. }
  965. return false;
  966. }
  967. void SceneObject::resolveMountPID()
  968. {
  969. if ( mMountPID && !mMount.object )
  970. {
  971. SceneObject *obj = dynamic_cast< SceneObject* >( mMountPID->getObject() );
  972. if ( obj )
  973. obj->mountObject( this, mMount.node, mMount.xfm );
  974. }
  975. }
  976. //-----------------------------------------------------------------------------
  977. void SceneObject::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  978. {
  979. if ( obj->mMount.object == this )
  980. {
  981. // Already mounted to this
  982. // So update our node and xfm which may have changed.
  983. obj->mMount.node = node;
  984. obj->mMount.xfm = xfm;
  985. }
  986. else
  987. {
  988. if ( obj->mMount.object )
  989. obj->unmount();
  990. obj->mMount.object = this;
  991. obj->mMount.node = node;
  992. obj->mMount.link = mMount.list;
  993. obj->mMount.xfm = xfm;
  994. mMount.list = obj;
  995. // Assign PIDs to both objects
  996. if ( isServerObject() )
  997. {
  998. obj->getOrCreatePersistentId();
  999. if ( !obj->mMountPID )
  1000. {
  1001. obj->mMountPID = getOrCreatePersistentId();
  1002. obj->mMountPID->incRefCount();
  1003. }
  1004. }
  1005. obj->onMount( this, node );
  1006. }
  1007. }
  1008. //-----------------------------------------------------------------------------
  1009. void SceneObject::unmountObject( SceneObject *obj )
  1010. {
  1011. if ( obj->mMount.object == this )
  1012. {
  1013. // Find and unlink the object
  1014. for ( SceneObject **ptr = &mMount.list; *ptr; ptr = &(*ptr)->mMount.link )
  1015. {
  1016. if ( *ptr == obj )
  1017. {
  1018. *ptr = obj->mMount.link;
  1019. break;
  1020. }
  1021. }
  1022. obj->mMount.object = NULL;
  1023. obj->mMount.link = NULL;
  1024. if( obj->mMountPID != NULL ) // Only on server.
  1025. {
  1026. obj->mMountPID->decRefCount();
  1027. obj->mMountPID = NULL;
  1028. }
  1029. obj->onUnmount( this, obj->mMount.node );
  1030. }
  1031. }
  1032. //-----------------------------------------------------------------------------
  1033. void SceneObject::unmount()
  1034. {
  1035. if (mMount.object)
  1036. mMount.object->unmountObject(this);
  1037. }
  1038. //-----------------------------------------------------------------------------
  1039. void SceneObject::onMount( SceneObject *obj, S32 node )
  1040. {
  1041. deleteNotify( obj );
  1042. if ( !isGhost() )
  1043. {
  1044. setMaskBits( MountedMask );
  1045. //onMount_callback( node );
  1046. }
  1047. }
  1048. //-----------------------------------------------------------------------------
  1049. void SceneObject::onUnmount( SceneObject *obj, S32 node )
  1050. {
  1051. clearNotify(obj);
  1052. if ( !isGhost() )
  1053. {
  1054. setMaskBits( MountedMask );
  1055. //onUnmount_callback( node );
  1056. }
  1057. }
  1058. //-----------------------------------------------------------------------------
  1059. void SceneObject::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  1060. {
  1061. MatrixF mountTransform( xfm );
  1062. const Point3F &scale = getScale();
  1063. Point3F position = mountTransform.getPosition();
  1064. position.convolve( scale );
  1065. mountTransform.setPosition( position );
  1066. outMat->mul( mObjToWorld, mountTransform );
  1067. }
  1068. //-----------------------------------------------------------------------------
  1069. void SceneObject::getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat )
  1070. {
  1071. MatrixF mountTransform( xfm );
  1072. const Point3F &scale = getScale();
  1073. Point3F position = mountTransform.getPosition();
  1074. position.convolve( scale );
  1075. mountTransform.setPosition( position );
  1076. outMat->mul( mRenderObjToWorld, mountTransform );
  1077. }
  1078. //=============================================================================
  1079. // Console API.
  1080. //=============================================================================
  1081. // MARK: ---- Console API ----
  1082. //-----------------------------------------------------------------------------
  1083. DefineEngineMethod( SceneObject, getType, S32, (),,
  1084. "Return the type mask for this object.\n"
  1085. "@return The numeric type mask for the object." )
  1086. {
  1087. return object->getTypeMask();
  1088. }
  1089. //-----------------------------------------------------------------------------
  1090. DefineEngineMethod( SceneObject, mountObject, bool,
  1091. ( SceneObject* objB, S32 slot, TransformF txfm ), ( TransformF::Identity ),
  1092. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  1093. "@param objB Object to mount onto us\n"
  1094. "@param slot Mount slot ID\n"
  1095. "@param txfm (optional) mount offset transform\n"
  1096. "@return true if successful, false if failed (objB is not valid)" )
  1097. {
  1098. if ( objB )
  1099. {
  1100. object->mountObject( objB, slot, txfm.getMatrix() );
  1101. return true;
  1102. }
  1103. return false;
  1104. }
  1105. //-----------------------------------------------------------------------------
  1106. DefineEngineMethod( SceneObject, unmountObject, bool, ( SceneObject* target ),,
  1107. "@brief Unmount an object from ourselves.\n\n"
  1108. "@param target object to unmount\n"
  1109. "@return true if successful, false if failed\n" )
  1110. {
  1111. if ( target )
  1112. {
  1113. object->unmountObject(target);
  1114. return true;
  1115. }
  1116. return false;
  1117. }
  1118. //-----------------------------------------------------------------------------
  1119. DefineEngineMethod( SceneObject, unmount, void, (),,
  1120. "Unmount us from the currently mounted object if any.\n" )
  1121. {
  1122. object->unmount();
  1123. }
  1124. //-----------------------------------------------------------------------------
  1125. DefineEngineMethod( SceneObject, isMounted, bool, (),,
  1126. "@brief Check if we are mounted to another object.\n\n"
  1127. "@return true if mounted to another object, false if not mounted." )
  1128. {
  1129. return object->isMounted();
  1130. }
  1131. //-----------------------------------------------------------------------------
  1132. DefineEngineMethod( SceneObject, getObjectMount, S32, (),,
  1133. "@brief Get the object we are mounted to.\n\n"
  1134. "@return the SimObjectID of the object we're mounted to, or 0 if not mounted." )
  1135. {
  1136. return object->isMounted()? object->getObjectMount()->getId(): 0;
  1137. }
  1138. //-----------------------------------------------------------------------------
  1139. DefineEngineMethod( SceneObject, getMountedObjectCount, S32, (),,
  1140. "Get the number of objects mounted to us.\n"
  1141. "@return the number of mounted objects." )
  1142. {
  1143. return object->getMountedObjectCount();
  1144. }
  1145. //-----------------------------------------------------------------------------
  1146. DefineEngineMethod( SceneObject, getMountedObject, S32, ( S32 slot ),,
  1147. "Get the object mounted at a particular slot.\n"
  1148. "@param slot mount slot index to query\n"
  1149. "@return ID of the object mounted in the slot, or 0 if no object." )
  1150. {
  1151. SceneObject* mobj = object->getMountedObject( slot );
  1152. return mobj? mobj->getId(): 0;
  1153. }
  1154. //-----------------------------------------------------------------------------
  1155. DefineEngineMethod( SceneObject, getMountedObjectNode, S32, ( S32 slot ),,
  1156. "@brief Get the mount node index of the object mounted at our given slot.\n\n"
  1157. "@param slot mount slot index to query\n"
  1158. "@return index of the mount node used by the object mounted in this slot." )
  1159. {
  1160. return object->getMountedObjectNode( slot );
  1161. }
  1162. //-----------------------------------------------------------------------------
  1163. DefineEngineMethod( SceneObject, getMountNodeObject, S32, ( S32 node ),,
  1164. "@brief Get the object mounted at our given node index.\n\n"
  1165. "@param node mount node index to query\n"
  1166. "@return ID of the first object mounted at the node, or 0 if none found." )
  1167. {
  1168. SceneObject* mobj = object->getMountNodeObject( node );
  1169. return mobj? mobj->getId(): 0;
  1170. }
  1171. //-----------------------------------------------------------------------------
  1172. DefineEngineMethod( SceneObject, getTransform, TransformF, (),,
  1173. "Get the object's transform.\n"
  1174. "@return the current transform of the object\n" )
  1175. {
  1176. return object->getTransform();
  1177. }
  1178. //-----------------------------------------------------------------------------
  1179. DefineEngineMethod( SceneObject, getInverseTransform, TransformF, (),,
  1180. "Get the object's inverse transform.\n"
  1181. "@return the inverse transform of the object\n" )
  1182. {
  1183. return object->getWorldTransform();
  1184. }
  1185. //-----------------------------------------------------------------------------
  1186. DefineEngineMethod( SceneObject, getPosition, Point3F, (),,
  1187. "Get the object's world position.\n"
  1188. "@return the current world position of the object\n" )
  1189. {
  1190. return object->getTransform().getPosition();
  1191. }
  1192. DefineEngineMethod( SceneObject, setPosition, void, (Point3F pos),,
  1193. "Set the object's world position.\n"
  1194. "@param pos the new world position of the object\n" )
  1195. {
  1196. return object->setPosition(pos);
  1197. }
  1198. //-----------------------------------------------------------------------------
  1199. DefineEngineMethod( SceneObject, getEulerRotation, Point3F, (),,
  1200. "Get Euler rotation of this object.\n"
  1201. "@return the orientation of the object in the form of rotations around the "
  1202. "X, Y and Z axes in degrees.\n" )
  1203. {
  1204. Point3F euler = object->getTransform().toEuler();
  1205. // Convert to degrees.
  1206. euler.x = mRadToDeg( euler.x );
  1207. euler.y = mRadToDeg( euler.y );
  1208. euler.z = mRadToDeg( euler.z );
  1209. return euler;
  1210. }
  1211. //-----------------------------------------------------------------------------
  1212. DefineEngineMethod( SceneObject, getForwardVector, VectorF, (),,
  1213. "Get the direction this object is facing.\n"
  1214. "@return a vector indicating the direction this object is facing.\n"
  1215. "@note This is the object's y axis." )
  1216. {
  1217. return object->getTransform().getForwardVector();
  1218. }
  1219. //-----------------------------------------------------------------------------
  1220. DefineEngineMethod( SceneObject, getRightVector, VectorF, (),,
  1221. "Get the right vector of the object.\n"
  1222. "@return a vector indicating the right direction of this object."
  1223. "@note This is the object's x axis." )
  1224. {
  1225. return object->getTransform().getRightVector();
  1226. }
  1227. //-----------------------------------------------------------------------------
  1228. DefineEngineMethod( SceneObject, getUpVector, VectorF, (),,
  1229. "Get the up vector of the object.\n"
  1230. "@return a vector indicating the up direction of this object."
  1231. "@note This is the object's z axis." )
  1232. {
  1233. return object->getTransform().getUpVector();
  1234. }
  1235. //-----------------------------------------------------------------------------
  1236. DefineEngineMethod( SceneObject, setTransform, void, ( TransformF txfm ),,
  1237. "Set the object's transform (orientation and position)."
  1238. "@param txfm object transform to set" )
  1239. {
  1240. // PATHSHAPE
  1241. object->PerformUpdatesForChildren(txfm.getMatrix());
  1242. // PATHSHAPE END
  1243. if ( !txfm.hasRotation() )
  1244. object->setPosition( txfm.getPosition() );
  1245. else
  1246. object->setTransform( txfm.getMatrix() );
  1247. }
  1248. //-----------------------------------------------------------------------------
  1249. DefineEngineMethod( SceneObject, getScale, Point3F, (),,
  1250. "Get the object's scale.\n"
  1251. "@return object scale as a Point3F" )
  1252. {
  1253. return object->getScale();
  1254. }
  1255. //-----------------------------------------------------------------------------
  1256. DefineEngineMethod( SceneObject, setScale, void, ( Point3F scale ),,
  1257. "Set the object's scale.\n"
  1258. "@param scale object scale to set\n" )
  1259. {
  1260. object->setScale( scale );
  1261. }
  1262. //-----------------------------------------------------------------------------
  1263. DefineEngineMethod( SceneObject, getWorldBox, Box3F, (),,
  1264. "Get the object's world bounding box.\n"
  1265. "@return six fields, two Point3Fs, containing the min and max points of the "
  1266. "worldbox." )
  1267. {
  1268. return object->getWorldBox();
  1269. }
  1270. //-----------------------------------------------------------------------------
  1271. DefineEngineMethod( SceneObject, getWorldBoxCenter, Point3F, (),,
  1272. "Get the center of the object's world bounding box.\n"
  1273. "@return the center of the world bounding box for this object." )
  1274. {
  1275. Point3F center;
  1276. object->getWorldBox().getCenter( &center );
  1277. return center;
  1278. }
  1279. //-----------------------------------------------------------------------------
  1280. DefineEngineMethod( SceneObject, getObjectBox, Box3F, (),,
  1281. "Get the object's bounding box (relative to the object's origin).\n"
  1282. "@return six fields, two Point3Fs, containing the min and max points of the "
  1283. "objectbox." )
  1284. {
  1285. return object->getObjBox();
  1286. }
  1287. //-----------------------------------------------------------------------------
  1288. DefineEngineMethod( SceneObject, isGlobalBounds, bool, (),,
  1289. "Check if this object has a global bounds set.\n"
  1290. "If global bounds are set to be true, then the object is assumed to have an "
  1291. "infinitely large bounding box for collision and rendering purposes.\n"
  1292. "@return true if the object has a global bounds." )
  1293. {
  1294. return object->isGlobalBounds();
  1295. }
  1296. DefineEngineMethod(SceneObject, setForwardVector, void, (VectorF newForward, VectorF upVector), (VectorF(0, 0, 0), VectorF(0, 0, 1)),
  1297. "Sets the forward vector of a scene object, making it face Y+ along the new vector.\n"
  1298. "@param The new forward vector to set.\n"
  1299. "@param (Optional) The up vector to use to help orient the rotation.")
  1300. {
  1301. object->setForwardVector(newForward, upVector);
  1302. }
  1303. // PATHSHAPE
  1304. // Move RenderTransform by set amount
  1305. // no longer used
  1306. void SceneObject::moveRender(const Point3F &delta)
  1307. {
  1308. Point3F pos;
  1309. const MatrixF& tmat = getRenderTransform();
  1310. tmat.getColumn(3,&pos);
  1311. AngAxisF aa(tmat);
  1312. pos += delta;
  1313. MatrixF mat;
  1314. aa.setMatrix(&mat);
  1315. mat.setColumn(3,pos);
  1316. setRenderTransform(mat);
  1317. }
  1318. void SceneObject::PerformUpdatesForChildren(MatrixF mat){
  1319. UpdateXformChange(mat);
  1320. for (U32 i=0; i < getNumChildren(); i++) {
  1321. SceneObject *o = getChild(i);
  1322. o->updateChildTransform(); //update the position of the child object
  1323. }
  1324. }
  1325. // This function will move the players based on how much it's
  1326. // parent have moved
  1327. void SceneObject::updateChildTransform(){
  1328. if (getParent() != NULL){
  1329. MatrixF one;
  1330. MatrixF two;
  1331. MatrixF three;
  1332. MatrixF four;
  1333. MatrixF mat;
  1334. one= getTransform();
  1335. two = getParent()->getTransform();
  1336. one.affineInverse();
  1337. four.mul(two,one);
  1338. mat.mul(getParent()->mLastXform,getTransform());
  1339. setTransform(mat);
  1340. }
  1341. }
  1342. // This function will move the rendered image based on how much it's
  1343. // parent have moved since the processtick.
  1344. // For some reason the player object must be updated via it's GetRenderTransform seen below,
  1345. // Other objects seem to require getTransform() only
  1346. void SceneObject::updateRenderChangesByParent(){
  1347. if (getParent() != NULL){
  1348. MatrixF renderXform = getParent()->getRenderTransform();
  1349. MatrixF xform = getParent()->getTransform();
  1350. xform.affineInverse();
  1351. MatrixF offset;
  1352. offset.mul(renderXform, xform);
  1353. MatrixF mat;
  1354. //add the "offset" caused by the parents change, and add it to it's own
  1355. // This is needed by objects that update their own render transform thru interpolate tick
  1356. // Mostly for stationary objects.
  1357. if (getClassName() == StringTable->insert("Player"))
  1358. mat.mul(offset,getRenderTransform());
  1359. else
  1360. mat.mul(offset,getTransform());
  1361. setRenderTransform(mat);
  1362. }
  1363. }
  1364. //Ramen - Move Transform by set amount
  1365. //written by Anthony Lovell
  1366. void SceneObject::move(F32 x, F32 y, F32 z)
  1367. {
  1368. Point3F delta;
  1369. delta.x = x;
  1370. delta.y = y;
  1371. delta.z = z;
  1372. move(delta);
  1373. }
  1374. // move by a specified delta in root coordinate space
  1375. void SceneObject::move(const Point3F &delta)
  1376. {
  1377. Point3F pos;
  1378. const MatrixF& tmat = getTransform();
  1379. tmat.getColumn(3,&pos);
  1380. AngAxisF aa(tmat);
  1381. pos += delta;
  1382. MatrixF mat;
  1383. aa.setMatrix(&mat);
  1384. mat.setColumn(3,pos);
  1385. setTransform(mat);
  1386. }
  1387. //written by Anthony Lovell ----------------------------------------------------------
  1388. U32
  1389. SceneObject::getNumChildren() const
  1390. {
  1391. U32 num = 0;
  1392. for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling)
  1393. num++;
  1394. return num;
  1395. }
  1396. //written by Anthony Lovell ----------------------------------------------------------
  1397. SceneObject *
  1398. SceneObject::getChild(U32 index) const
  1399. {
  1400. SceneObject *cur = mGraph.firstChild;
  1401. for (U32 i = 0;
  1402. cur && i < index;
  1403. i++)
  1404. cur = cur->mGraph.nextSibling;
  1405. return cur;
  1406. }
  1407. void SceneObject::UpdateXformChange(const MatrixF &mat){
  1408. // This function gets the difference between the Transform and current Render transform
  1409. // Used for Interpolation matching with the child objects who rely on this data.
  1410. MatrixF oldxform = getTransform();
  1411. oldxform.affineInverse();
  1412. mLastXform.mul(mat,oldxform);
  1413. }
  1414. //----------------------------------------------------------
  1415. bool
  1416. SceneObject::attachChildAt(SceneObject *subObject, MatrixF atThisOffset, S32 node)
  1417. {
  1418. AssertFatal(subObject, "attaching a null subObject");
  1419. AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
  1420. bool b = subObject->attachToParent(this, &atThisOffset, node);
  1421. if (!b)
  1422. return false;
  1423. return true;
  1424. }
  1425. //----------------------------------------------------------
  1426. bool
  1427. SceneObject::attachChildAt(SceneObject *subObject, Point3F atThisPosition)
  1428. {
  1429. AssertFatal(subObject, "attaching a null subObject");
  1430. AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
  1431. bool b = subObject->attachToParent(this);
  1432. if (!b)
  1433. return false;
  1434. subObject->mGraph.objToParent.setColumn(3, atThisPosition);
  1435. // calcTransformFromLocalTransform();
  1436. return true;
  1437. }
  1438. //----------------------------------------------------------
  1439. bool
  1440. SceneObject::attachChild(SceneObject *child)
  1441. {
  1442. AssertFatal(child, "attaching a null subObject");
  1443. AssertFatal(!isChildOf(child), "cyclic attachChild()");
  1444. return child->attachToParent(this);
  1445. }
  1446. //----------------------------------------------------------
  1447. /// returns a count of children plus their children, recursively
  1448. U32
  1449. SceneObject::getNumProgeny() const
  1450. {
  1451. U32 num = 0;
  1452. for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling) {
  1453. num += 1 + cur->getNumProgeny();
  1454. }
  1455. return num;
  1456. }
  1457. DefineEngineMethod(SceneObject, getNumChildren, S32, (),, "returns number of direct child objects")
  1458. {
  1459. return object->getNumChildren();
  1460. }
  1461. DefineEngineMethod(SceneObject, getNumProgeny, S32, (),, "returns number of recursively-nested child objects")
  1462. {
  1463. return object->getNumProgeny();
  1464. }
  1465. DefineEngineMethod(SceneObject, getChild, S32, (S32 _index), (0), "getChild(S32 index) -- returns child SceneObject at given index")
  1466. {
  1467. SceneObject *s = object->getChild(_index);
  1468. return s ? s->getId() : 0;
  1469. }
  1470. DefineEngineMethod(SceneObject, attachChildAt, bool, (SceneObject* _subObject, MatrixF _offset, S32 _node), (nullAsType<SceneObject*>(), MatrixF::Identity, 0), "(SceneObject subObject, MatrixF offset, S32 offset)"
  1471. "Mount object to this one with the specified offset expressed in our coordinate space.")
  1472. {
  1473. if (_subObject != nullptr)
  1474. {
  1475. return object->attachChildAt(_subObject, _offset, _node);
  1476. }
  1477. else
  1478. {
  1479. Con::errorf("Couldn't addObject()!");
  1480. return false;
  1481. }
  1482. }
  1483. DefineEngineMethod(SceneObject, attachToParent, bool, (const char*_sceneObject), ,"attachToParent(SceneObject)"
  1484. "specify a null or non-null parent")
  1485. {
  1486. SceneObject * t;
  1487. if(Sim::findObject(_sceneObject, t))
  1488. {
  1489. return object->attachToParent(t);
  1490. }
  1491. else
  1492. {
  1493. if ((!String::compare("0", _sceneObject))|| (!String::compare("", _sceneObject)))
  1494. return object->attachToParent(NULL);
  1495. else
  1496. {
  1497. Con::errorf("Couldn't setParent()!");
  1498. return false;
  1499. }
  1500. }
  1501. }
  1502. DefineEngineMethod(SceneObject, getParent, S32, (),, "returns ID of parent SceneObject")
  1503. {
  1504. SceneObject *p = object->getParent();
  1505. return p ? p->getId() : -1;
  1506. }
  1507. DefineEngineMethod(SceneObject, attachChild, bool, (const char*_subObject),, "(SceneObject subObject)"
  1508. "attach an object to this one, preserving its present transform.")
  1509. {
  1510. SceneObject * t;
  1511. MatrixF m;
  1512. if(Sim::findObject(_subObject, t))
  1513. return object->attachChild(t);
  1514. Con::errorf("Couldn't addObject()!");
  1515. return false;
  1516. }
  1517. bool SceneObject::isChildOf(SceneObject *so)
  1518. {
  1519. SceneObject *p = mGraph.parent;
  1520. if (p) {
  1521. if (p == so)
  1522. return true;
  1523. else
  1524. return p->isChildOf(so);
  1525. } else
  1526. return false;
  1527. }
  1528. bool SceneObject::attachToParent(SceneObject *newParent, MatrixF *atThisOffset/* = NULL */, S32 node )
  1529. {
  1530. SceneObject *oldParent = mGraph.parent;
  1531. if (oldParent == newParent)
  1532. return true;
  1533. // cycles in the scene hierarchy are forbidden!
  1534. // that is: a SceneObject cannot be a child of its progeny
  1535. if (newParent && newParent->isChildOf(this))
  1536. return false;
  1537. mGraph.parent = newParent;
  1538. if (oldParent) {
  1539. clearNotify(oldParent);
  1540. // remove this SceneObject from the list of children of oldParent
  1541. SceneObject *cur = oldParent->mGraph.firstChild;
  1542. if (cur == this) { // if we are the first child, this is easy
  1543. oldParent->mGraph.firstChild = mGraph.nextSibling;
  1544. } else {
  1545. while (cur->mGraph.nextSibling != this) {
  1546. cur = cur->mGraph.nextSibling;
  1547. // ASSERT cur != NULL;
  1548. }
  1549. cur->mGraph.nextSibling = mGraph.nextSibling;
  1550. }
  1551. oldParent->onLostChild(this);
  1552. }
  1553. if (newParent) {
  1554. deleteNotify(newParent); // if we are deleted, inform our parent
  1555. // add this SceneObject to the list of children of oldParent
  1556. mGraph.nextSibling = newParent->mGraph.firstChild;
  1557. newParent->mGraph.firstChild = this;
  1558. mGraph.parent = newParent;
  1559. newParent->onNewChild(this);
  1560. if (atThisOffset)
  1561. mGraph.objToParent = *atThisOffset;
  1562. } else {
  1563. mGraph.parent = NULL;
  1564. mGraph.nextSibling = NULL;
  1565. mGraph.objToParent = mObjToWorld;
  1566. }
  1567. onLostParent(oldParent);
  1568. onNewParent(newParent);
  1569. setMaskBits(MountedMask);
  1570. return true;
  1571. }
  1572. DefineEngineMethod(SceneObject, detachChild, bool, (const char*_subObject),, "SceneObject subObject")
  1573. {
  1574. SceneObject * t;
  1575. if(Sim::findObject(_subObject, t)) {
  1576. return t->attachToParent(NULL);
  1577. } else
  1578. return false;
  1579. }
  1580. // subclasses can do something with these if they care to
  1581. void SceneObject::onNewParent(SceneObject *newParent) {}
  1582. void SceneObject::onLostParent(SceneObject *oldParent){}
  1583. void SceneObject::onNewChild(SceneObject *newKid){}
  1584. void SceneObject::onLostChild(SceneObject *lostKid){}