123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013 |
- //-----------------------------------------------------------------------------
- // 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()
- {
- docsURL;
- //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, setEulerRotation, void, (Point3F inRot), ,
- "set Euler rotation of this object.\n"
- "@set the orientation of the object in the form of rotations around the "
- "X, Y and Z axes in degrees.\n")
- {
- MatrixF curMat = object->getTransform();
- Point3F curPos = curMat.getPosition();
- Point3F curScale = curMat.getScale();
- EulerF inRotRad = inRot * M_PI_F / 180.0;
- curMat.set(inRotRad, curPos);
- curMat.scale(curScale);
- object->setTransform(curMat);
- }
- //-----------------------------------------------------------------------------
- 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) { if (isServerObject()) onNewParent_callback(newParent); }
- void SceneObject::onLostParent(SceneObject *oldParent) { if (isServerObject()) onLostParent_callback(oldParent); }
- void SceneObject::onNewChild(SceneObject *newKid) { if (isServerObject()) onNewChild_callback(newKid); }
- void SceneObject::onLostChild(SceneObject *lostKid) { if (isServerObject()) onLostChild_callback(lostKid); }
|