1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
- // Copyright (C) 2015 Faust Logic, Inc.
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- #include "platform/platform.h"
- #include "scene/sceneObject.h"
- #include "platform/profiler.h"
- #include "console/consoleTypes.h"
- #include "console/engineAPI.h"
- #include "console/simPersistID.h"
- #include "sim/netConnection.h"
- #include "core/stream/bitStream.h"
- #include "scene/sceneManager.h"
- #include "scene/sceneTracker.h"
- #include "scene/sceneRenderState.h"
- #include "scene/zones/sceneZoneSpace.h"
- #include "collision/extrudedPolyList.h"
- #include "collision/earlyOutPolyList.h"
- #include "collision/optimizedPolyList.h"
- #include "math/mPolyhedron.h"
- #include "gfx/bitmap/gBitmap.h"
- #include "math/util/frustum.h"
- #include "math/mathIO.h"
- #include "math/mTransform.h"
- #include "T3D/gameBase/gameProcess.h"
- #include "T3D/gameBase/gameConnection.h"
- #include "T3D/accumulationVolume.h"
- IMPLEMENT_CONOBJECT(SceneObject);
- ConsoleDocClass( SceneObject,
- "@brief A networkable object that exists in the 3D world.\n\n"
- "The SceneObject class provides the foundation for 3D objects in the Engine. It "
- "exposes the functionality for:\n\n"
- "<ul><li>Position, rotation and scale within the world.</li>"
- "<li>Working with a scene graph (in the Zone and Portal sections), allowing efficient "
- "and robust rendering of the game scene.</li>"
- "<li>Various helper functions, including functions to get bounding information "
- "and momentum/velocity.</li>"
- "<li>Mounting one SceneObject to another.</li>"
- "<li>An interface for collision detection, as well as ray casting.</li>"
- "<li>Lighting. SceneObjects can register lights both at lightmap generation "
- "time, and dynamic lights at runtime (for special effects, such as from flame "
- "or a projectile, or from an explosion).</li></ul>\n\n"
- "You do not typically work with SceneObjects themselves. The SceneObject provides a reference "
- "within the game world (the scene), but does not render to the client on its own. The "
- "same is true of collision detection beyond that of the bounding box. Instead you "
- "use one of the many classes that derrive from SceneObject, such as TSStatic.\n\n"
- "@section SceneObject_Hiding Difference Between setHidden() and isRenderEnabled\n\n"
- "When it comes time to decide if a SceneObject should render or not, there are two "
- "methods that can stop the SceneObject from rendering at all. You need to be aware of "
- "the differences between these two methods as they impact how the SceneObject is networked "
- "from the server to the client.\n\n"
- "The first method of manually controlling if a SceneObject is rendered is through its "
- "SceneObject::isRenderEnabled property. When set to false the SceneObject is considered invisible but "
- "still present within the scene. This means it still takes part in collisions and continues "
- "to be networked.\n\n"
- "The second method is using the setHidden() method. This will actually remove a SceneObject "
- "from the scene and it will no longer be networked from the server to the cleint. Any client-side "
- "ghost of the object will be deleted as the server no longer considers the object to be in scope.\n\n"
- "@ingroup gameObjects\n"
- );
- #ifdef TORQUE_TOOLS
- extern bool gEditingMission;
- #endif
- Signal< void( SceneObject* ) > SceneObject::smSceneObjectAdd;
- Signal< void( SceneObject* ) > SceneObject::smSceneObjectRemove;
- //-----------------------------------------------------------------------------
- SceneObject::SceneObject()
- {
- mContainer = 0;
- mTypeMask = DefaultObjectType;
- mCollisionCount = 0;
- mGlobalBounds = false;
- mObjScale.set(1,1,1);
- mObjToWorld.identity();
- mLastXform.identity();
- mWorldToObj.identity();
- mObjBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
- mWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
- mWorldSphere = SphereF(Point3F(0, 0, 0), 0);
- mRenderObjToWorld.identity();
- mRenderWorldToObj.identity();
- mRenderWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
- mRenderWorldSphere = SphereF(Point3F(0, 0, 0), 0);
- mContainerSeqKey = 0;
- mBinRefHead = NULL;
- mSceneManager = NULL;
- mNumCurrZones = 0;
- mZoneRefHead = NULL;
- mZoneRefDirty = false;
- mBinMinX = 0xFFFFFFFF;
- mBinMaxX = 0xFFFFFFFF;
- mBinMinY = 0xFFFFFFFF;
- mBinMaxY = 0xFFFFFFFF;
- mLightPlugin = NULL;
- mMount.object = NULL;
- mMount.link = NULL;
- mMount.list = NULL;
- mMount.node = -1;
- mMount.xfm = MatrixF::Identity;
- mMountPID = NULL;
- mSceneObjectLinks = NULL;
- mObjectFlags.set( RenderEnabledFlag | SelectionEnabledFlag );
- // PATHSHAPE
- // init the scenegraph relationships to indicate no parent, no children, and no siblings
- mGraph.parent = NULL;
- mGraph.nextSibling = NULL;
- mGraph.firstChild = NULL;
- mGraph.objToParent.identity();
- // PATHSHAPE END
- mIsScopeAlways = false;
- mAccuTex = NULL;
- mSelectionFlags = 0;
- mPathfindingIgnore = false;
- mGameObjectAssetId = StringTable->insert("");
- mDirtyGameObject = false;
- }
- //-----------------------------------------------------------------------------
- SceneObject::~SceneObject()
- {
- AssertFatal( mZoneRefHead == NULL && mBinRefHead == NULL,
- "SceneObject::~SceneObject - Object still linked in reference lists!");
- AssertFatal( !mSceneObjectLinks,
- "SceneObject::~SceneObject() - object is still linked to SceneTrackers" );
- mAccuTex = NULL;
- unlink();
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::castRayRendered(const Point3F &start, const Point3F &end, RayInfo *info)
- {
- // By default, all ray checking against the rendered mesh will be passed
- // on to the collision mesh. This saves having to define both methods
- // for simple objects.
- return castRay( start, end, info );
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::containsPoint( const Point3F& point )
- {
- // If it's not in the AABB, then it can't be in the OBB either,
- // so early out.
- if( !mWorldBox.isContained( point ) )
- return false;
- // Transform point into object space and test it against
- // our object space bounding box.
- Point3F objPoint( 0, 0, 0 );
- getWorldTransform().mulP( point, &objPoint );
- objPoint.convolveInverse( getScale() );
- return ( mObjBox.isContained( objPoint ) );
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::collideBox(const Point3F &start, const Point3F &end, RayInfo *info)
- {
- const F32 * pStart = (const F32*)start;
- const F32 * pEnd = (const F32*)end;
- const F32 * pMin = (const F32*)mObjBox.minExtents;
- const F32 * pMax = (const F32*)mObjBox.maxExtents;
- F32 maxStartTime = -1;
- F32 minEndTime = 1;
- F32 startTime;
- F32 endTime;
- // used for getting normal
- U32 hitIndex = 0xFFFFFFFF;
- U32 side;
- // walk the axis
- for(U32 i = 0; i < 3; i++)
- {
- //
- if(pStart[i] < pEnd[i])
- {
- if(pEnd[i] < pMin[i] || pStart[i] > pMax[i])
- return(false);
- F32 dist = pEnd[i] - pStart[i];
- startTime = (pStart[i] < pMin[i]) ? (pMin[i] - pStart[i]) / dist : -1;
- endTime = (pEnd[i] > pMax[i]) ? (pMax[i] - pStart[i]) / dist : 1;
- side = 1;
- }
- else
- {
- if(pStart[i] < pMin[i] || pEnd[i] > pMax[i])
- return(false);
- F32 dist = pStart[i] - pEnd[i];
- startTime = (pStart[i] > pMax[i]) ? (pStart[i] - pMax[i]) / dist : -1;
- endTime = (pEnd[i] < pMin[i]) ? (pStart[i] - pMin[i]) / dist : 1;
- side = 0;
- }
- //
- if(startTime > maxStartTime)
- {
- maxStartTime = startTime;
- hitIndex = i * 2 + side;
- }
- if(endTime < minEndTime)
- minEndTime = endTime;
- if(minEndTime < maxStartTime)
- return(false);
- }
- // fail if inside
- if(maxStartTime < 0.f)
- return(false);
- //
- static Point3F boxNormals[] = {
- Point3F( 1, 0, 0),
- Point3F(-1, 0, 0),
- Point3F( 0, 1, 0),
- Point3F( 0,-1, 0),
- Point3F( 0, 0, 1),
- Point3F( 0, 0,-1),
- };
- //
- AssertFatal(hitIndex != 0xFFFFFFFF, "SceneObject::collideBox");
- info->t = maxStartTime;
- info->object = this;
- mObjToWorld.mulV(boxNormals[hitIndex], &info->normal);
- info->material = 0;
- return(true);
- }
- //-----------------------------------------------------------------------------
- void SceneObject::disableCollision()
- {
- for (SceneObject* ptr = getMountList(); ptr; ptr = ptr->getMountLink())
- ptr->disableCollision();
- mCollisionCount++;
- AssertFatal(mCollisionCount < 50, "SceneObject::disableCollision called 50 times on the same object. Is this inside a circular loop?" );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::enableCollision()
- {
- for (SceneObject* ptr = getMountList(); ptr; ptr = ptr->getMountLink())
- ptr->enableCollision();
- if (mCollisionCount)
- --mCollisionCount;
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::onAdd()
- {
- if ( !Parent::onAdd() )
- return false;
- mIsScopeAlways = mNetFlags.test( ScopeAlways );
- mWorldToObj = mObjToWorld;
- mWorldToObj.affineInverse();
- resetWorldBox();
- setRenderTransform(mObjToWorld);
- resolveMountPID();
- smSceneObjectAdd.trigger(this);
- return true;
- }
- //-----------------------------------------------------------------------------
- void SceneObject::onRemove()
- {
- smSceneObjectRemove.trigger(this);
- unmount();
- plUnlink();
- Parent::onRemove();
- // PATHSHAPE
- if ( getParent() != NULL) attachToParent( NULL);
- // PATHSHAPE END
- }
- //-----------------------------------------------------------------------------
- void SceneObject::addToScene()
- {
- if( mSceneManager )
- return;
- if( isClientObject() )
- gClientSceneGraph->addObjectToScene( this );
- else
- gServerSceneGraph->addObjectToScene( this );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::removeFromScene()
- {
- if( !mSceneManager )
- return;
- mSceneManager->removeObjectFromScene( this );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::onDeleteNotify( SimObject *obj )
- {
- // We are comparing memory addresses so even if obj really is not a
- // ProcessObject this cast shouldn't break anything.
- if ( obj == mAfterObject )
- mAfterObject = NULL;
- if ( obj == mMount.object )
- unmount();
- Parent::onDeleteNotify( obj );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::inspectPostApply()
- {
- if( isServerObject() )
- setMaskBits( MountedMask );
- Parent::inspectPostApply();
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setGlobalBounds()
- {
- mGlobalBounds = true;
- mObjBox.minExtents.set( -1e10, -1e10, -1e10 );
- mObjBox.maxExtents.set( 1e10, 1e10, 1e10 );
- if( mSceneManager )
- mSceneManager->notifyObjectDirty( this );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setTransform( const MatrixF& mat )
- {
- // This test is a bit expensive so turn it off in release.
- #ifdef TORQUE_DEBUG
- //AssertFatal( mat.isAffine(), "SceneObject::setTransform() - Bad transform (non affine)!" );
- #endif
- PROFILE_SCOPE( SceneObject_setTransform );
- // PATHSHAPE
- UpdateXformChange(mat);
- PerformUpdatesForChildren(mat);
- // PATHSHAPE END
- // Update the transforms.
- mObjToWorld = mWorldToObj = mat;
- mWorldToObj.affineInverse();
- // Update the world-space AABB.
- resetWorldBox();
- // If we're in a SceneManager, sync our scene state.
- if( mSceneManager != NULL )
- mSceneManager->notifyObjectDirty( this );
- setRenderTransform( mat );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setScale( const VectorF &scale )
- {
- AssertFatal( !mIsNaN( scale ), "SceneObject::setScale() - The scale is NaN!" );
- // Avoid unnecessary scaling operations.
- if ( mObjScale.equal( scale ) )
- return;
- mObjScale = scale;
- setTransform(MatrixF(mObjToWorld));
- // Make sure that any subclasses of me get a chance to react to the
- // scale being changed.
- onScaleChanged();
- setMaskBits( ScaleMask );
- }
- void SceneObject::setForwardVector(VectorF newForward, VectorF upVector)
- {
- MatrixF mat = getTransform();
- VectorF up(0.0f, 0.0f, 1.0f);
- VectorF axisX;
- VectorF axisY = newForward;
- VectorF axisZ;
- if (upVector != VectorF::Zero)
- up = upVector;
- // Validate and normalize input:
- F32 lenSq;
- lenSq = axisY.lenSquared();
- if (lenSq < 0.000001f)
- {
- axisY.set(0.0f, 1.0f, 0.0f);
- Con::errorf("SceneObject::setForwardVector() - degenerate forward vector");
- }
- else
- {
- axisY /= mSqrt(lenSq);
- }
- lenSq = up.lenSquared();
- if (lenSq < 0.000001f)
- {
- up.set(0.0f, 0.0f, 1.0f);
- Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
- }
- else
- {
- up /= mSqrt(lenSq);
- }
- if (fabsf(mDot(up, axisY)) > 0.9999f)
- {
- Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
- // I haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
- F32 tmp = up.x;
- up.x = -up.y;
- up.y = up.z;
- up.z = tmp;
- }
- // construct the remaining axes:
- mCross(axisY, up, &axisX);
- mCross(axisX, axisY, &axisZ);
- mat.setColumn(0, axisX);
- mat.setColumn(1, axisY);
- mat.setColumn(2, axisZ);
- setTransform(mat);
- }
- //-----------------------------------------------------------------------------
- void SceneObject::resetWorldBox()
- {
- AssertFatal(mObjBox.isValidBox(), "SceneObject::resetWorldBox - Bad object box!");
- mWorldBox = mObjBox;
- mWorldBox.minExtents.convolve(mObjScale);
- mWorldBox.maxExtents.convolve(mObjScale);
- mObjToWorld.mul(mWorldBox);
- AssertFatal(mWorldBox.isValidBox(), "SceneObject::resetWorldBox - Bad world box!");
- // Create mWorldSphere from mWorldBox
- mWorldBox.getCenter(&mWorldSphere.center);
- mWorldSphere.radius = (mWorldBox.maxExtents - mWorldSphere.center).len();
- // Update tracker links.
-
- for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
- link = link->getNextLink() )
- link->update();
- }
- //-----------------------------------------------------------------------------
- void SceneObject::resetObjectBox()
- {
- AssertFatal( mWorldBox.isValidBox(), "SceneObject::resetObjectBox - Bad world box!" );
- mObjBox = mWorldBox;
- mWorldToObj.mul( mObjBox );
- Point3F objScale( mObjScale );
- objScale.setMax( Point3F( (F32)POINT_EPSILON, (F32)POINT_EPSILON, (F32)POINT_EPSILON ) );
- mObjBox.minExtents.convolveInverse( objScale );
- mObjBox.maxExtents.convolveInverse( objScale );
- AssertFatal( mObjBox.isValidBox(), "SceneObject::resetObjectBox - Bad object box!" );
- // Update the mWorldSphere from mWorldBox
- mWorldBox.getCenter( &mWorldSphere.center );
- mWorldSphere.radius = ( mWorldBox.maxExtents - mWorldSphere.center ).len();
- // Update scene managers.
-
- for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
- link = link->getNextLink() )
- link->update();
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setRenderTransform(const MatrixF& mat)
- {
- PROFILE_START(SceneObj_setRenderTransform);
- mRenderObjToWorld = mRenderWorldToObj = mat;
- mRenderWorldToObj.affineInverse();
- AssertFatal(mObjBox.isValidBox(), "Bad object box!");
- resetRenderWorldBox();
- PROFILE_END();
- }
- //-----------------------------------------------------------------------------
- void SceneObject::resetRenderWorldBox()
- {
- AssertFatal( mObjBox.isValidBox(), "Bad object box!" );
- mRenderWorldBox = mObjBox;
- mRenderWorldBox.minExtents.convolve( mObjScale );
- mRenderWorldBox.maxExtents.convolve( mObjScale );
- mRenderObjToWorld.mul( mRenderWorldBox );
- AssertFatal( mRenderWorldBox.isValidBox(), "Bad world box!" );
- // Create mRenderWorldSphere from mRenderWorldBox.
- mRenderWorldBox.getCenter( &mRenderWorldSphere.center );
- mRenderWorldSphere.radius = ( mRenderWorldBox.maxExtents - mRenderWorldSphere.center ).len();
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setHidden( bool hidden )
- {
- if( hidden != isHidden() )
- {
- // Add/remove the object from the scene. Removing it
- // will also cause the NetObject to go out of scope since
- // the container query will not find it anymore. However,
- // ScopeAlways objects need to be treated separately as we
- // do next.
- if( !hidden )
- addToScene();
- else
- removeFromScene();
- // ScopeAlways objects stay in scope no matter what, i.e. even
- // if they aren't in the scene query anymore. So, to force ghosts
- // to go away, we need to clear ScopeAlways while we are hidden.
- if( hidden && mIsScopeAlways )
- clearScopeAlways();
- else if( !hidden && mIsScopeAlways )
- setScopeAlways();
- Parent::setHidden( hidden );
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::initPersistFields()
- {
- //Disabled temporarily
- /*addGroup("GameObject");
- addField("GameObject", TypeGameObjectAssetPtr, Offset(mGameObjectAsset, SceneObject), "The asset Id used for the game object this entity is based on.");
- addField("dirtyGameObject", TypeBool, Offset(mDirtyGameObject, SceneObject), "If this entity is a GameObject, it flags if this instance delinates from the template.",
- AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
- endGroup("GameObject");*/
- addGroup( "Transform" );
- addProtectedField( "position", TypeMatrixPosition, Offset( mObjToWorld, SceneObject ),
- &_setFieldPosition, &defaultProtectedGetFn,
- "Object world position." );
- addProtectedField( "rotation", TypeMatrixRotation, Offset( mObjToWorld, SceneObject ),
- &_setFieldRotation, &defaultProtectedGetFn,
- "Object world orientation." );
- addProtectedField( "scale", TypePoint3F, Offset( mObjScale, SceneObject ),
- &_setFieldScale, &defaultProtectedGetFn,
- "Object world scale." );
- endGroup( "Transform" );
- addGroup( "Editing" );
- addProtectedField( "isRenderEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
- &_setRenderEnabled, &_getRenderEnabled,
- "Controls client-side rendering of the object.\n"
- "@see isRenderable()\n" );
- addProtectedField( "isSelectionEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
- &_setSelectionEnabled, &_getSelectionEnabled,
- "Determines if the object may be selected from wihin the Tools.\n"
- "@see isSelectable()\n" );
- endGroup( "Editing" );
- addGroup( "Mounting" );
- addProtectedField( "mountPID", TypePID, Offset( mMountPID, SceneObject ), &_setMountPID, &defaultProtectedGetFn,
- "@brief PersistentID of object we are mounted to.\n\n"
- "Unlike the SimObjectID that is determined at run time, the PersistentID of an object is saved with the level/mission and "
- "may be used to form a link between objects." );
- addField( "mountNode", TypeS32, Offset( mMount.node, SceneObject ), "Node we are mounted to." );
- addField( "mountPos", TypeMatrixPosition, Offset( mMount.xfm, SceneObject ), "Position we are mounted at ( object space of our mount object )." );
- addField( "mountRot", TypeMatrixRotation, Offset( mMount.xfm, SceneObject ), "Rotation we are mounted at ( object space of our mount object )." );
- endGroup( "Mounting" );
- Parent::initPersistFields();
- }
- bool SceneObject::_setGameObject(void* object, const char* index, const char* data)
- {
- // Sanity!
- AssertFatal(data != NULL, "Cannot use a NULL asset Id.");
- return true; //rbI->setMeshAsset(data);
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::_setFieldPosition( void *object, const char *index, const char *data )
- {
- SceneObject* so = static_cast<SceneObject*>( object );
- if ( so )
- {
- MatrixF txfm( so->getTransform() );
- Con::setData( TypeMatrixPosition, &txfm, 0, 1, &data );
- so->setTransform( txfm );
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::_setFieldRotation( void *object, const char *index, const char *data )
- {
- SceneObject* so = static_cast<SceneObject*>( object );
- if ( so )
- {
- MatrixF txfm( so->getTransform() );
- Con::setData( TypeMatrixRotation, &txfm, 0, 1, &data );
- so->setTransform( txfm );
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::_setFieldScale( void *object, const char *index, const char *data )
- {
- SceneObject* so = static_cast<SceneObject*>( object );
- if ( so )
- {
- Point3F scale;
- Con::setData( TypePoint3F, &scale, 0, 1, &data );
- so->setScale( scale );
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::writeField( StringTableEntry fieldName, const char* value )
- {
- if( !Parent::writeField( fieldName, value ) )
- return false;
-
- static StringTableEntry sIsRenderEnabled = StringTable->insert( "isRenderEnabled" );
- static StringTableEntry sIsSelectionEnabled = StringTable->insert( "isSelectionEnabled" );
- static StringTableEntry sMountNode = StringTable->insert( "mountNode" );
- static StringTableEntry sMountPos = StringTable->insert( "mountPos" );
- static StringTableEntry sMountRot = StringTable->insert( "mountRot" );
-
- // Don't write flag fields if they are at their default values.
-
- if( fieldName == sIsRenderEnabled && dAtob( value ) )
- return false;
- else if( fieldName == sIsSelectionEnabled && dAtob( value ) )
- return false;
- else if ( mMountPID == NULL && ( fieldName == sMountNode ||
- fieldName == sMountPos ||
- fieldName == sMountRot ) )
- {
- return false;
- }
-
- return true;
- }
- //-----------------------------------------------------------------------------
- static void scopeCallback( SceneObject* obj, void* conPtr )
- {
- NetConnection* ptr = reinterpret_cast< NetConnection* >( conPtr );
- if( obj->isScopeable() )
- ptr->objectInScope(obj);
- }
- void SceneObject::onCameraScopeQuery( NetConnection* connection, CameraScopeQuery* query )
- {
- SceneManager* sceneManager = getSceneManager();
- GameConnection* conn = dynamic_cast<GameConnection*> (connection);
- if (conn && (query->visibleDistance = conn->getVisibleGhostDistance()) == 0.0f)
- if ((query->visibleDistance = sceneManager->getVisibleGhostDistance()) == 0.0f)
- query->visibleDistance = sceneManager->getVisibleDistance();
- // Object itself is in scope.
- if( this->isScopeable() )
- connection->objectInScope( this );
- // If we're mounted to something, that object is in scope too.
- if( isMounted() )
- connection->objectInScope( mMount.object );
- // If we're added to a scene graph, let the graph do the scene scoping.
- // Otherwise just put everything in the server container in scope.
- if( getSceneManager() )
- getSceneManager()->scopeScene( query, connection );
- else
- gServerContainer.findObjects( 0xFFFFFFFF, scopeCallback, connection );
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::isRenderEnabled() const
- {
- #ifdef TORQUE_TOOLS
- if (gEditingMission)
- {
- AbstractClassRep *classRep = getClassRep();
- return (mObjectFlags.test(RenderEnabledFlag) && classRep->isRenderEnabled());
- }
- #endif
- return (mObjectFlags.test(RenderEnabledFlag));
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setRenderEnabled( bool value )
- {
- if( value )
- mObjectFlags.set( RenderEnabledFlag );
- else
- mObjectFlags.clear( RenderEnabledFlag );
-
- setMaskBits( FlagMask );
- }
- //-----------------------------------------------------------------------------
- const char* SceneObject::_getRenderEnabled( void* object, const char* data )
- {
- SceneObject* obj = reinterpret_cast< SceneObject* >( object );
- if( obj->mObjectFlags.test( RenderEnabledFlag ) )
- return "1";
- else
- return "0";
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::_setRenderEnabled( void *object, const char *index, const char *data )
- {
- SceneObject* obj = reinterpret_cast< SceneObject* >( object );
- obj->setRenderEnabled( dAtob( data ) );
- return false;
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::isSelectionEnabled() const
- {
- AbstractClassRep *classRep = getClassRep();
- return ( mObjectFlags.test( SelectionEnabledFlag ) && classRep->isSelectionEnabled() );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setSelectionEnabled( bool value )
- {
- if( value )
- mObjectFlags.set( SelectionEnabledFlag );
- else
- mObjectFlags.clear( SelectionEnabledFlag );
-
- // Not synchronized on network so don't set dirty bit.
- }
- //-----------------------------------------------------------------------------
- const char* SceneObject::_getSelectionEnabled( void* object, const char* data )
- {
- SceneObject* obj = reinterpret_cast< SceneObject* >( object );
- if( obj->mObjectFlags.test( SelectionEnabledFlag ) )
- return "true";
- else
- return "false";
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::_setSelectionEnabled( void *object, const char *index, const char *data )
- {
- SceneObject* obj = reinterpret_cast< SceneObject* >( object );
- obj->setSelectionEnabled( dAtob( data ) );
- return false;
- }
- //--------------------------------------------------------------------------
- U32 SceneObject::packUpdate( NetConnection* conn, U32 mask, BitStream* stream )
- {
- U32 retMask = Parent::packUpdate( conn, mask, stream );
- if ( stream->writeFlag( mask & FlagMask ) )
- stream->writeRangedU32( (U32)mObjectFlags, 0, getObjectFlagMax() );
- // PATHSHAPE
- //Begin attachment
- retMask = 0; //retry mask
- if (stream->writeFlag(getParent() != NULL)) {
- stream->writeAffineTransform(mGraph.objToParent);
- }
- if (stream->writeFlag(mask & MountedMask))
- {
- // Check to see if we need to write an object ID
- if (stream->writeFlag(mGraph.parent)) {
- S32 t = conn->getGhostIndex(mGraph.parent);
- // Check to see if we can actually ghost this...
- if (t == -1) {
- // Cant, try again later
- retMask |= MountedMask;
- stream->writeFlag(false);
- }
- else {
- // Can, write it.
- stream->writeFlag(true);
- stream->writeRangedU32(U32(t), 0, NetConnection::MaxGhostCount);
- stream->writeAffineTransform(mGraph.objToParent);
- //Con::errorf("%d: sent mounted on %d", getId(), mGraph.parent->getId());
- }
- }
- }
- // End of Attachment
- // PATHSHAPE END
- if ( mask & MountedMask )
- {
- if ( mMount.object )
- {
- S32 gIndex = conn->getGhostIndex( mMount.object );
- if ( stream->writeFlag( gIndex != -1 ) )
- {
- stream->writeFlag( true );
- stream->writeInt( gIndex, NetConnection::GhostIdBitSize );
- if ( stream->writeFlag( mMount.node != -1 ) )
- stream->writeInt( mMount.node, NumMountPointBits );
- mathWrite( *stream, mMount.xfm );
- }
- else
- // Will have to try again later
- retMask |= MountedMask;
- }
- else
- // Unmount if this isn't the initial packet
- if ( stream->writeFlag( !(mask & InitialUpdateMask) ) )
- stream->writeFlag( false );
- }
- else
- stream->writeFlag( false );
-
- return retMask;
- }
- //-----------------------------------------------------------------------------
- void SceneObject::unpackUpdate( NetConnection* conn, BitStream* stream )
- {
- Parent::unpackUpdate( conn, stream );
-
- // FlagMask
- if ( stream->readFlag() )
- mObjectFlags = stream->readRangedU32( 0, getObjectFlagMax() );
- // PATHSHAPE
- // begin of attachment
- if (stream->readFlag())
- {
- MatrixF m;
- stream->readAffineTransform(&m);
- mGraph.objToParent = m;
- }
- if (stream->readFlag())
- {
- // Check to see if we need to read an object ID
- if (stream->readFlag())
- {
- // Check to see if we can actually ghost this...
- if (stream->readFlag())
- {
- GameBase *newParent = static_cast<GameBase*>(conn->resolveGhost(stream->readRangedU32(0, NetConnection::MaxGhostCount)));
- MatrixF m;
- stream->readAffineTransform(&m);
- if (getParent() != newParent)
- {
- clearProcessAfter();
- processAfter(newParent);
- }
- attachToParent(newParent, &m);
- //Con::errorf("%d: got mounted on %d", getId(), mParentObject->getId());
- }
- }
- else
- {
- attachToParent(NULL);
- }
- }
- // End of attachment
- // PATHSHAPE END
- // MountedMask
- if ( stream->readFlag() )
- {
- if ( stream->readFlag() )
- {
- S32 gIndex = stream->readInt( NetConnection::GhostIdBitSize );
- SceneObject* obj = dynamic_cast<SceneObject*>( conn->resolveGhost( gIndex ) );
- S32 node = -1;
- if ( stream->readFlag() ) // node != -1
- node = stream->readInt( NumMountPointBits );
- MatrixF xfm;
- mathRead( *stream, &xfm );
- if ( !obj )
- {
- conn->setLastError( "Invalid packet from server." );
- return;
- }
- obj->mountObject( this, node, xfm );
- }
- else
- unmount();
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::_updateZoningState() const
- {
- if( mZoneRefDirty )
- {
- SceneZoneSpaceManager* manager = getSceneManager()->getZoneManager();
- if( manager )
- manager->updateObject( const_cast< SceneObject* >( this ) );
- else
- mZoneRefDirty = false;
- }
- }
- //-----------------------------------------------------------------------------
- U32 SceneObject::getCurrZone( const U32 index ) const
- {
- _updateZoningState();
- // Not the most efficient way to do this, walking the list,
- // but it's an uncommon call...
- ZoneRef* walk = mZoneRefHead;
- for( U32 i = 0; i < index; ++ i )
- {
- walk = walk->nextInObj;
- AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
- }
- AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
- return walk->zone;
- }
- //-----------------------------------------------------------------------------
- Point3F SceneObject::getPosition() const
- {
- Point3F pos;
- mObjToWorld.getColumn(3, &pos);
- return pos;
- }
- //-----------------------------------------------------------------------------
- Point3F SceneObject::getRenderPosition() const
- {
- Point3F pos;
- mRenderObjToWorld.getColumn(3, &pos);
- return pos;
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setPosition(const Point3F &pos)
- {
- AssertFatal( !mIsNaN( pos ), "SceneObject::setPosition() - The position is NaN!" );
- MatrixF xform = mObjToWorld;
- xform.setColumn(3, pos);
- setTransform(xform);
- }
- //-----------------------------------------------------------------------------
- F32 SceneObject::distanceTo(const Point3F &pnt) const
- {
- return mWorldBox.getDistanceToPoint( pnt );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::processAfter( ProcessObject *obj )
- {
- AssertFatal( dynamic_cast<SceneObject*>( obj ), "SceneObject::processAfter - Got non-SceneObject!" );
- mAfterObject = (SceneObject*)obj;
- if ( mAfterObject->mAfterObject == this )
- mAfterObject->mAfterObject = NULL;
- getProcessList()->markDirty();
- }
- //-----------------------------------------------------------------------------
- void SceneObject::clearProcessAfter()
- {
- mAfterObject = NULL;
- }
- //-----------------------------------------------------------------------------
- void SceneObject::setProcessTick( bool t )
- {
- if ( t == mProcessTick )
- return;
- if ( mProcessTick )
- {
- if ( !getMountedObjectCount() )
- plUnlink(); // Only unlink if there is nothing mounted to us
- mProcessTick = false;
- }
- else
- {
- // Just to be sure...
- plUnlink();
- getProcessList()->addObject( this );
- mProcessTick = true;
- }
- }
- //-----------------------------------------------------------------------------
- ProcessList* SceneObject::getProcessList() const
- {
- if ( isClientObject() )
- return ClientProcessList::get();
- else
- return ServerProcessList::get();
- }
- //-------------------------------------------------------------------------
- bool SceneObject::isMounted()
- {
- resolveMountPID();
- return mMount.object != NULL;
- }
- //-----------------------------------------------------------------------------
- S32 SceneObject::getMountedObjectCount()
- {
- S32 count = 0;
- for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
- count++;
- return count;
- }
- //-----------------------------------------------------------------------------
- SceneObject* SceneObject::getMountedObject(S32 idx)
- {
- if (idx >= 0) {
- S32 count = 0;
- for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
- if (count++ == idx)
- return itr;
- }
- return NULL;
- }
- //-----------------------------------------------------------------------------
- S32 SceneObject::getMountedObjectNode(S32 idx)
- {
- if (idx >= 0) {
- S32 count = 0;
- for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
- if (count++ == idx)
- return itr->mMount.node;
- }
- return -1;
- }
- //-----------------------------------------------------------------------------
- SceneObject* SceneObject::getMountNodeObject(S32 node)
- {
- for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
- if (itr->mMount.node == node)
- return itr;
- return NULL;
- }
- //-----------------------------------------------------------------------------
- bool SceneObject::_setMountPID( void* object, const char* index, const char* data )
- {
- SceneObject* so = static_cast<SceneObject*>( object );
- if ( so )
- {
- // Unmount old object (PID reference is released even if it had been resolved yet)
- if ( so->mMountPID )
- {
- so->mMountPID->decRefCount();
- so->mMountPID = NULL;
- }
- so->unmount();
- // Get the new PID (new object will be mounted on demand)
- Con::setData( TypePID, &so->mMountPID, 0, 1, &data );
- if ( so->mMountPID )
- so->mMountPID->incRefCount(); // Prevent PID from being deleted out from under us!
- }
- return false;
- }
- void SceneObject::resolveMountPID()
- {
- if ( mMountPID && !mMount.object )
- {
- SceneObject *obj = dynamic_cast< SceneObject* >( mMountPID->getObject() );
- if ( obj )
- obj->mountObject( this, mMount.node, mMount.xfm );
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
- {
- if ( obj->mMount.object == this )
- {
- // Already mounted to this
- // So update our node and xfm which may have changed.
- obj->mMount.node = node;
- obj->mMount.xfm = xfm;
- }
- else
- {
- if ( obj->mMount.object )
- obj->unmount();
- obj->mMount.object = this;
- obj->mMount.node = node;
- obj->mMount.link = mMount.list;
- obj->mMount.xfm = xfm;
- mMount.list = obj;
- // Assign PIDs to both objects
- if ( isServerObject() )
- {
- obj->getOrCreatePersistentId();
- if ( !obj->mMountPID )
- {
- obj->mMountPID = getOrCreatePersistentId();
- obj->mMountPID->incRefCount();
- }
- }
- obj->onMount( this, node );
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::unmountObject( SceneObject *obj )
- {
- if ( obj->mMount.object == this )
- {
- // Find and unlink the object
- for ( SceneObject **ptr = &mMount.list; *ptr; ptr = &(*ptr)->mMount.link )
- {
- if ( *ptr == obj )
- {
- *ptr = obj->mMount.link;
- break;
- }
- }
- obj->mMount.object = NULL;
- obj->mMount.link = NULL;
- if( obj->mMountPID != NULL ) // Only on server.
- {
- obj->mMountPID->decRefCount();
- obj->mMountPID = NULL;
- }
- obj->onUnmount( this, obj->mMount.node );
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::unmount()
- {
- if (mMount.object)
- mMount.object->unmountObject(this);
- }
- //-----------------------------------------------------------------------------
- void SceneObject::onMount( SceneObject *obj, S32 node )
- {
- deleteNotify( obj );
- if ( !isGhost() )
- {
- setMaskBits( MountedMask );
- //onMount_callback( node );
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::onUnmount( SceneObject *obj, S32 node )
- {
- clearNotify(obj);
- if ( !isGhost() )
- {
- setMaskBits( MountedMask );
- //onUnmount_callback( node );
- }
- }
- //-----------------------------------------------------------------------------
- void SceneObject::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
- {
- MatrixF mountTransform( xfm );
- const Point3F &scale = getScale();
- Point3F position = mountTransform.getPosition();
- position.convolve( scale );
- mountTransform.setPosition( position );
- outMat->mul( mObjToWorld, mountTransform );
- }
- //-----------------------------------------------------------------------------
- void SceneObject::getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat )
- {
- MatrixF mountTransform( xfm );
- const Point3F &scale = getScale();
- Point3F position = mountTransform.getPosition();
- position.convolve( scale );
- mountTransform.setPosition( position );
- outMat->mul( mRenderObjToWorld, mountTransform );
- }
- //=============================================================================
- // Console API.
- //=============================================================================
- // MARK: ---- Console API ----
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getType, S32, (),,
- "Return the type mask for this object.\n"
- "@return The numeric type mask for the object." )
- {
- return object->getTypeMask();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, mountObject, bool,
- ( SceneObject* objB, S32 slot, TransformF txfm ), ( TransformF::Identity ),
- "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
- "@param objB Object to mount onto us\n"
- "@param slot Mount slot ID\n"
- "@param txfm (optional) mount offset transform\n"
- "@return true if successful, false if failed (objB is not valid)" )
- {
- if ( objB )
- {
- object->mountObject( objB, slot, txfm.getMatrix() );
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, unmountObject, bool, ( SceneObject* target ),,
- "@brief Unmount an object from ourselves.\n\n"
- "@param target object to unmount\n"
- "@return true if successful, false if failed\n" )
- {
- if ( target )
- {
- object->unmountObject(target);
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, unmount, void, (),,
- "Unmount us from the currently mounted object if any.\n" )
- {
- object->unmount();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, isMounted, bool, (),,
- "@brief Check if we are mounted to another object.\n\n"
- "@return true if mounted to another object, false if not mounted." )
- {
- return object->isMounted();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getObjectMount, S32, (),,
- "@brief Get the object we are mounted to.\n\n"
- "@return the SimObjectID of the object we're mounted to, or 0 if not mounted." )
- {
- return object->isMounted()? object->getObjectMount()->getId(): 0;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getMountedObjectCount, S32, (),,
- "Get the number of objects mounted to us.\n"
- "@return the number of mounted objects." )
- {
- return object->getMountedObjectCount();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getMountedObject, S32, ( S32 slot ),,
- "Get the object mounted at a particular slot.\n"
- "@param slot mount slot index to query\n"
- "@return ID of the object mounted in the slot, or 0 if no object." )
- {
- SceneObject* mobj = object->getMountedObject( slot );
- return mobj? mobj->getId(): 0;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getMountedObjectNode, S32, ( S32 slot ),,
- "@brief Get the mount node index of the object mounted at our given slot.\n\n"
- "@param slot mount slot index to query\n"
- "@return index of the mount node used by the object mounted in this slot." )
- {
- return object->getMountedObjectNode( slot );
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getMountNodeObject, S32, ( S32 node ),,
- "@brief Get the object mounted at our given node index.\n\n"
- "@param node mount node index to query\n"
- "@return ID of the first object mounted at the node, or 0 if none found." )
- {
- SceneObject* mobj = object->getMountNodeObject( node );
- return mobj? mobj->getId(): 0;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getTransform, TransformF, (),,
- "Get the object's transform.\n"
- "@return the current transform of the object\n" )
- {
- return object->getTransform();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getInverseTransform, TransformF, (),,
- "Get the object's inverse transform.\n"
- "@return the inverse transform of the object\n" )
- {
- return object->getWorldTransform();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getPosition, Point3F, (),,
- "Get the object's world position.\n"
- "@return the current world position of the object\n" )
- {
- return object->getTransform().getPosition();
- }
- DefineEngineMethod( SceneObject, setPosition, void, (Point3F pos),,
- "Set the object's world position.\n"
- "@param pos the new world position of the object\n" )
- {
- return object->setPosition(pos);
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getEulerRotation, Point3F, (),,
- "Get Euler rotation of this object.\n"
- "@return the orientation of the object in the form of rotations around the "
- "X, Y and Z axes in degrees.\n" )
- {
- Point3F euler = object->getTransform().toEuler();
-
- // Convert to degrees.
- euler.x = mRadToDeg( euler.x );
- euler.y = mRadToDeg( euler.y );
- euler.z = mRadToDeg( euler.z );
-
- return euler;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getForwardVector, VectorF, (),,
- "Get the direction this object is facing.\n"
- "@return a vector indicating the direction this object is facing.\n"
- "@note This is the object's y axis." )
- {
- return object->getTransform().getForwardVector();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getRightVector, VectorF, (),,
- "Get the right vector of the object.\n"
- "@return a vector indicating the right direction of this object."
- "@note This is the object's x axis." )
- {
- return object->getTransform().getRightVector();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getUpVector, VectorF, (),,
- "Get the up vector of the object.\n"
- "@return a vector indicating the up direction of this object."
- "@note This is the object's z axis." )
- {
- return object->getTransform().getUpVector();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, setTransform, void, ( TransformF txfm ),,
- "Set the object's transform (orientation and position)."
- "@param txfm object transform to set" )
- {
- // PATHSHAPE
- object->PerformUpdatesForChildren(txfm.getMatrix());
- // PATHSHAPE END
- if ( !txfm.hasRotation() )
- object->setPosition( txfm.getPosition() );
- else
- object->setTransform( txfm.getMatrix() );
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getScale, Point3F, (),,
- "Get the object's scale.\n"
- "@return object scale as a Point3F" )
- {
- return object->getScale();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, setScale, void, ( Point3F scale ),,
- "Set the object's scale.\n"
- "@param scale object scale to set\n" )
- {
- object->setScale( scale );
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getWorldBox, Box3F, (),,
- "Get the object's world bounding box.\n"
- "@return six fields, two Point3Fs, containing the min and max points of the "
- "worldbox." )
- {
- return object->getWorldBox();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getWorldBoxCenter, Point3F, (),,
- "Get the center of the object's world bounding box.\n"
- "@return the center of the world bounding box for this object." )
- {
- Point3F center;
- object->getWorldBox().getCenter( ¢er );
- return center;
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, getObjectBox, Box3F, (),,
- "Get the object's bounding box (relative to the object's origin).\n"
- "@return six fields, two Point3Fs, containing the min and max points of the "
- "objectbox." )
- {
- return object->getObjBox();
- }
- //-----------------------------------------------------------------------------
- DefineEngineMethod( SceneObject, isGlobalBounds, bool, (),,
- "Check if this object has a global bounds set.\n"
- "If global bounds are set to be true, then the object is assumed to have an "
- "infinitely large bounding box for collision and rendering purposes.\n"
- "@return true if the object has a global bounds." )
- {
- return object->isGlobalBounds();
- }
- DefineEngineMethod(SceneObject, setForwardVector, void, (VectorF newForward, VectorF upVector), (VectorF(0, 0, 0), VectorF(0, 0, 1)),
- "Sets the forward vector of a scene object, making it face Y+ along the new vector.\n"
- "@param The new forward vector to set.\n"
- "@param (Optional) The up vector to use to help orient the rotation.")
- {
- object->setForwardVector(newForward, upVector);
- }
- // PATHSHAPE
- // Move RenderTransform by set amount
- // no longer used
- void SceneObject::moveRender(const Point3F &delta)
- {
- Point3F pos;
- const MatrixF& tmat = getRenderTransform();
- tmat.getColumn(3,&pos);
- AngAxisF aa(tmat);
- pos += delta;
- MatrixF mat;
- aa.setMatrix(&mat);
- mat.setColumn(3,pos);
- setRenderTransform(mat);
- }
- void SceneObject::PerformUpdatesForChildren(MatrixF mat){
- for (U32 i=0; i < getNumChildren(); i++) {
- SceneObject *o = getChild(i);
- o->updateChildTransform(); //update the position of the child object
- }
- }
- // This function will move the players based on how much it's
- // parent have moved
- void SceneObject::updateChildTransform(){
- if (getParent() != NULL){
- MatrixF one;
- MatrixF two;
- MatrixF three;
- MatrixF four;
- MatrixF mat;
- one= getTransform();
- two = getParent()->getTransform();
- one.affineInverse();
- four.mul(two,one);
- mat.mul(getParent()->mLastXform,getTransform());
- setTransform(mat);
- }
- }
- // This function will move the rendered image based on how much it's
- // parent have moved since the processtick.
- // For some reason the player object must be updated via it's GetRenderTransform seen below,
- // Other objects seem to require getTransform() only
- void SceneObject::updateRenderChangesByParent(){
- if (getParent() != NULL){
- MatrixF renderXform = getParent()->getRenderTransform();
- MatrixF xform = getParent()->getTransform();
- xform.affineInverse();
- MatrixF offset;
- offset.mul(renderXform, xform);
- MatrixF mat;
-
- //add the "offset" caused by the parents change, and add it to it's own
- // This is needed by objects that update their own render transform thru interpolate tick
- // Mostly for stationary objects.
- if (getClassName() == StringTable->insert("Player"))
- mat.mul(offset,getRenderTransform());
- else
- mat.mul(offset,getTransform());
- setRenderTransform(mat);
- }
- }
- //Ramen - Move Transform by set amount
- //written by Anthony Lovell
- void SceneObject::move(F32 x, F32 y, F32 z)
- {
- Point3F delta;
- delta.x = x;
- delta.y = y;
- delta.z = z;
- move(delta);
- }
- // move by a specified delta in root coordinate space
- void SceneObject::move(const Point3F &delta)
- {
- Point3F pos;
- const MatrixF& tmat = getTransform();
- tmat.getColumn(3,&pos);
- AngAxisF aa(tmat);
-
- pos += delta;
- MatrixF mat;
- aa.setMatrix(&mat);
- mat.setColumn(3,pos);
- setTransform(mat);
- }
- //written by Anthony Lovell ----------------------------------------------------------
- U32
- SceneObject::getNumChildren() const
- {
- U32 num = 0;
- for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling)
- num++;
- return num;
- }
- //written by Anthony Lovell ----------------------------------------------------------
- SceneObject *
- SceneObject::getChild(U32 index) const
- {
- SceneObject *cur = mGraph.firstChild;
- for (U32 i = 0;
- cur && i < index;
- i++)
- cur = cur->mGraph.nextSibling;
- return cur;
- }
- void SceneObject::UpdateXformChange(const MatrixF &mat){
- // This function gets the difference between the Transform and current Render transform
- // Used for Interpolation matching with the child objects who rely on this data.
- MatrixF oldxform = getTransform();
- oldxform.affineInverse();
- mLastXform.mul(mat,oldxform);
- }
- //----------------------------------------------------------
- bool
- SceneObject::attachChildAt(SceneObject *subObject, MatrixF atThisOffset, S32 node)
- {
- AssertFatal(subObject, "attaching a null subObject");
- AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
- bool b = subObject->attachToParent(this, &atThisOffset, node);
- if (!b)
- return false;
-
- return true;
- }
- //----------------------------------------------------------
- bool
- SceneObject::attachChildAt(SceneObject *subObject, Point3F atThisPosition)
- {
- AssertFatal(subObject, "attaching a null subObject");
- AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
- bool b = subObject->attachToParent(this);
- if (!b)
- return false;
-
- subObject->mGraph.objToParent.setColumn(3, atThisPosition);
- // calcTransformFromLocalTransform();
- return true;
- }
- //----------------------------------------------------------
- bool
- SceneObject::attachChild(SceneObject *child)
- {
- AssertFatal(child, "attaching a null subObject");
- AssertFatal(!isChildOf(child), "cyclic attachChild()");
-
- return child->attachToParent(this);
- }
- //----------------------------------------------------------
- /// returns a count of children plus their children, recursively
- U32
- SceneObject::getNumProgeny() const
- {
- U32 num = 0;
- for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling) {
- num += 1 + cur->getNumProgeny();
- }
- return num;
- }
- DefineEngineMethod(SceneObject, getNumChildren, S32, (),, "returns number of direct child objects")
- {
- return object->getNumChildren();
- }
- DefineEngineMethod(SceneObject, getNumProgeny, S32, (),, "returns number of recursively-nested child objects")
- {
- return object->getNumProgeny();
- }
- DefineEngineMethod(SceneObject, getChild, S32, (S32 _index), (0), "getChild(S32 index) -- returns child SceneObject at given index")
- {
- SceneObject *s = object->getChild(_index);
- return s ? s->getId() : 0;
- }
- DefineEngineMethod(SceneObject, attachChildAt, bool, (SceneObject* _subObject, MatrixF _offset, S32 _node), (nullAsType<SceneObject*>(), MatrixF::Identity, 0), "(SceneObject subObject, MatrixF offset, S32 offset)"
- "Mount object to this one with the specified offset expressed in our coordinate space.")
- {
- if (_subObject != nullptr)
- {
- return object->attachChildAt(_subObject, _offset, _node);
- }
- else
- {
- Con::errorf("Couldn't addObject()!");
- return false;
- }
- }
- DefineEngineMethod(SceneObject, attachToParent, bool, (const char*_sceneObject), ,"attachToParent(SceneObject)"
- "specify a null or non-null parent")
- {
- SceneObject * t;
-
- if(Sim::findObject(_sceneObject, t))
- {
- return object->attachToParent(t);
- }
- else
- {
- if ((!String::compare("0", _sceneObject))|| (!String::compare("", _sceneObject)))
- return object->attachToParent(NULL);
- else
- {
- Con::errorf("Couldn't setParent()!");
- return false;
- }
- }
- }
- DefineEngineMethod(SceneObject, getParent, S32, (),, "returns ID of parent SceneObject")
- {
- SceneObject *p = object->getParent();
- return p ? p->getId() : -1;
- }
- DefineEngineMethod(SceneObject, attachChild, bool, (const char*_subObject),, "(SceneObject subObject)"
- "attach an object to this one, preserving its present transform.")
- {
- SceneObject * t;
- MatrixF m;
- if(Sim::findObject(_subObject, t))
- return object->attachChild(t);
- Con::errorf("Couldn't addObject()!");
- return false;
- }
- bool SceneObject::isChildOf(SceneObject *so)
- {
- SceneObject *p = mGraph.parent;
- if (p) {
- if (p == so)
- return true;
- else
- return p->isChildOf(so);
- } else
- return false;
- }
- bool SceneObject::attachToParent(SceneObject *newParent, MatrixF *atThisOffset/* = NULL */, S32 node )
- {
- SceneObject *oldParent = mGraph.parent;
- if (oldParent == newParent)
- return true;
- // cycles in the scene hierarchy are forbidden!
- // that is: a SceneObject cannot be a child of its progeny
- if (newParent && newParent->isChildOf(this))
- return false;
-
- mGraph.parent = newParent;
- if (oldParent) {
- clearNotify(oldParent);
- // remove this SceneObject from the list of children of oldParent
- SceneObject *cur = oldParent->mGraph.firstChild;
- if (cur == this) { // if we are the first child, this is easy
- oldParent->mGraph.firstChild = mGraph.nextSibling;
- } else {
- while (cur->mGraph.nextSibling != this) {
- cur = cur->mGraph.nextSibling;
- // ASSERT cur != NULL;
- }
- cur->mGraph.nextSibling = mGraph.nextSibling;
- }
- oldParent->onLostChild(this);
- }
-
- if (newParent) {
- deleteNotify(newParent); // if we are deleted, inform our parent
- // add this SceneObject to the list of children of oldParent
- mGraph.nextSibling = newParent->mGraph.firstChild;
- newParent->mGraph.firstChild = this;
- mGraph.parent = newParent;
-
- newParent->onNewChild(this);
- if (atThisOffset)
- mGraph.objToParent = *atThisOffset;
- } else {
- mGraph.parent = NULL;
- mGraph.nextSibling = NULL;
- mGraph.objToParent = mObjToWorld;
- }
- onLostParent(oldParent);
- onNewParent(newParent);
- setMaskBits(MountedMask);
- return true;
- }
- DefineEngineMethod(SceneObject, detachChild, bool, (const char*_subObject),, "SceneObject subObject")
- {
- SceneObject * t;
- if(Sim::findObject(_subObject, t)) {
- return t->attachToParent(NULL);
- } else
- return false;
- }
- IMPLEMENT_CALLBACK(SceneObject, onNewParent, void, (SceneObject *newParent), (newParent), "");
- IMPLEMENT_CALLBACK(SceneObject, onLostParent, void, (SceneObject *oldParent), (oldParent), "");
- IMPLEMENT_CALLBACK(SceneObject, onNewChild, void, (SceneObject *newKid), (newKid), "");
- IMPLEMENT_CALLBACK(SceneObject, onLostChild, void, (SceneObject *lostKid), (lostKid), "");
- void SceneObject::onNewParent(SceneObject *newParent) { onNewParent_callback(newParent); }
- void SceneObject::onLostParent(SceneObject *oldParent) { onLostParent_callback(oldParent); }
- void SceneObject::onNewChild(SceneObject *newKid) { onNewChild_callback(newKid); }
- void SceneObject::onLostChild(SceneObject *lostKid) { onLostChild_callback(lostKid); }
|