⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sceneobject.h

📁 五行MMORPG引擎系统V1.0
💻 H
📖 第 1 页 / 共 2 页
字号:
   /// Gets the index of the first zone this object manages in the collection of zones.
   U32  getZoneRangeStart() const                         { return mZoneRangeStart;   }

   /// Gets the number of zones containing this object.
   U32  getNumCurrZones() const                           { return mNumCurrZones;     }

   /// Returns the nth zone containing this object.
   U32  getCurrZone(const U32 index) const;

   /// If an object exists in multiple zones, this method will give you the
   /// number and indices of these zones (storing them in the provided variables).
   ///
   /// @param   obj      Object in question.
   /// @param   zones    Indices of zones containing the object. (out)
   /// @param   numZones Number of elements in the returned array.  (out)
   virtual bool getOverlappingZones(SceneObject* obj, U32* zones, U32* numZones);

   /// Returns the zone containing p.
   ///
   /// @param   p   Point to test.
   virtual U32  getPointZone(const Point3F& p);

   /// This is called on a zone managing object to scope all the zones managed.
   ///
   /// @param   rootPosition   Camera position
   /// @param   rootDistance   Camera visible distance
   /// @param   zoneScopeState Array of booleans which line up with the collection of zones, marked true if that zone is scoped (out)
   virtual bool scopeObject(const Point3F&        rootPosition,
                            const F32             rootDistance,
                            bool*                 zoneScopeState);
   /// @}

   /// Called when the object is supposed to render itself.
   ///
   /// @param   state   Current rendering state.
   ///                  @see SceneState
   /// @param   image   Image associated with this object to render.
   ///                  @see SceneRenderImage
   virtual void renderObject(SceneState *state, SceneRenderImage *image);

   /// Called when the SceneGraph is ready for the registration of RenderImages.
   ///
   /// @see SceneState
   ///
   /// @param   state               SceneState
   /// @param   stateKey            State key of the current SceneState
   /// @param   startZone           Base zone index
   /// @param   modifyBaseZoneState If true, the object needs to modify the zone state.
   virtual bool prepRenderImage(SceneState *state, const U32 stateKey, const U32 startZone,
                                const bool modifyBaseZoneState = false);

   /// Adds object to the client or server container depending on the object
   void addToScene();

   /// Removes the object from the client/server container
   void removeFromScene();

   //-------------------------------------- Derived class interface
   // Overrides
protected:
   bool onAdd();
   void onRemove();

   // Overrideables
protected:
   /// Called when this is added to the SceneGraph.
   ///
   /// @param   graph   SceneGraph this is getting added to
   virtual bool onSceneAdd(SceneGraph *graph);

   /// Called when this is removed from the SceneGraph
   virtual void onSceneRemove();

   /// Called when the size of the object changes
   virtual void onScaleChanged();

   /// @name Portals
   /// @{

   /// This is used by a portal controling object to transform the base-modelview
   /// used by the scenegraph for rendering to the modelview it needs to render correctly.
   ///
   /// @see MirrorSubObject
   ///
   /// @param   portalIndex   Index of portal in the list of portals controlled by the object.
   /// @param   oldMV         Current modelview matrix used by the SceneGraph (in)
   /// @param   newMV         New modelview to be used by the SceneGraph (out)
   virtual void transformModelview(const U32 portalIndex, const MatrixF& oldMV, MatrixF* newMV);

   /// Used to tranform the position of a point based on a portal.
   ///
   /// @param   portalIndex   Index of a portal to transform by.
   /// @param   point         Point to transform.
   virtual void transformPosition(const U32 portalIndex, Point3F& point);

   /// Returns a new view frustum for the portal.
   ///
   /// @param   portalIndex   Which portal in the list of portals the object controls
   /// @param   oldFrustum    Current frustum.
   /// @param   nearPlane     Near clipping plane.
   /// @param   farPlane      Far clipping plane.
   /// @param   oldViewport   Current viewport.
   /// @param   newFrustum    New view frustum to use. (out)
   /// @param   newViewport   New viewport to use. (out)
   /// @param   flippedMatrix Should the object should use a flipped matrix to calculate viewport and frustum?
   virtual bool computeNewFrustum(const U32      portalIndex,
                                  const F64*     oldFrustum,
                                  const F64      nearPlane,
                                  const F64      farPlane,
                                  const RectI&   oldViewport,
                                  F64*           newFrustum,
                                  RectI&         newViewport,
                                  const bool     flippedMatrix);

   /// Called before things are to be rendered from the portals point of view, to set up
   /// everything the portal needs to render correctly.
   ///
   /// @param   portalIndex   Index of portal to use.
   /// @param   pCurrState    Current SceneState
   /// @param   pParentState  SceneState used before this portal was activated
   virtual void openPortal(const U32   portalIndex,
                           SceneState* pCurrState,
                           SceneState* pParentState);

   /// Called after rendering of a portal is complete, this resets the states
   /// the previous call to openPortal() changed.
   ///
   /// @param   portalIndex    Index of portal to use.
   /// @param   pCurrState     Current SceneState
   /// @param   pParentState   SceneState used before this portal was activated
   virtual void closePortal(const U32   portalIndex,
                            SceneState* pCurrState,
                            SceneState* pParentState);
public:

   /// Returns the plane of the portal in world space.
   ///
   /// @param   portalIndex   Index of portal to use.
   /// @param   plane         Plane of the portal in world space (out)
   virtual void getWSPortalPlane(const U32 portalIndex, PlaneF *plane);

   /// @}

protected:
   /// Sets the mLastState and mLastStateKey.
   ///
   /// @param   state   SceneState to set as the last state
   /// @param   key     Key to set as the last state key
   void          setLastState(SceneState *state, U32 key);

   /// Returns true if the provided SceneState and key are set as this object's
   /// last state and key.
   ///
   /// @param   state    SceneState in question
   /// @param   key      State key in question
   bool          isLastState(SceneState *state, U32 key) const;


   /// @name Traversal State
   ///
   /// The SceneGraph traversal is recursive and the traversal state of an object
   /// can be one of three things:
   ///           - Pending - The object has not yet been examined for zone traversal.
   ///           - Working - The object is currently having its zones traversed.
   ///           - Done    - The object has had all of its zones traversed or doesn't manage zones.
   ///
   /// @note These states were formerly referred to as TraverseColor, with White, Black, and
   ///       Gray; this was changed in Torque 1.2 by Pat "KillerBunny" Wilson. This code is
   ///       only used internal to this class
   /// @{

   // These two replaced by TraversalState because that makes more sense -KB
   //void          setTraverseColor(TraverseColor);
   //TraverseColor getTraverseColor() const;
   // ph34r teh muskrat! - Travis Colure

   /// This sets the traversal state of the object.
   ///
   /// @note This is used internally; you should not normally need to call it.
   /// @param   s   Traversal state to assign
   void           setTraversalState( TraversalState s );

   /// Returns the traversal state of this object
   TraversalState getTraversalState() const;

   /// @}

   /// @name Lighting
   /// @{

   struct LightingInfo
   {
      LightingInfo();

      bool                       mUseInfo;
      bool                       mDirty;
      ColorF                     mDefaultColor;
      ColorF                     mAlarmColor;

      SimObjectPtr<SceneObject>  mInterior;

      bool                       mHasLastColor;
      ColorF                     mLastColor;
      U32                        mLastTime;

      static LightInfo           smAmbientLight;

      enum 
      {
         Interior = 0,
         Terrain,
      };
      U32                        mLightSource;
   };

   /// Sets up lighting for the rendering of this object
   virtual void installLights();

   /// Removes lighting for the rendering of this object
   virtual void uninstallLights();

   /// Gets the color of the ambient light in the area of the object and
   /// stores it in the provided ColorF.
   ///
   /// @param   col   Ambient color (out)
   virtual bool getLightingAmbientColor(ColorF * col);

   LightingInfo      mLightingInfo; ///< Lighting info for this object

   /// @}

protected:

   /// @name Transform and Collision Members
   /// @{

   ///
   Container* mContainer;

   MatrixF mObjToWorld;   ///< Transform from object space to world space
   MatrixF mWorldToObj;   ///< Transform from world space to object space (inverse)
   Point3F mObjScale;     ///< Object scale

   Box3F   mObjBox;       ///< Bounding box in object space
   Box3F   mWorldBox;     ///< Bounding box in world space
   SphereF mWorldSphere;  ///< Bounding sphere in world space

   MatrixF mRenderObjToWorld;    ///< Render matrix to transform object space to world space
   MatrixF mRenderWorldToObj;    ///< Render matrix to transform world space to object space
   Box3F   mRenderWorldBox;      ///< Render bounding box in world space
   SphereF mRenderWorldSphere;   ///< Render bounxing sphere in world space

   /// Regenerates the world-space bounding box and bounding sphere
   void    resetWorldBox();

   /// Regenerates the render-world-space bounding box and sphere
   void    resetRenderWorldBox();

   SceneObjectRef* mZoneRefHead;
   SceneObjectRef* mBinRefHead;

   U32 mBinMinX;
   U32 mBinMaxX;
   U32 mBinMinY;
   U32 mBinMaxY;

   /// @}

   /// @name Container Interface
   ///
   /// When objects are searched, we go through all the zones and ask them for
   /// all of their objects. Because an object can exist in multiple zones, the
   /// container sequence key is set to the id of the current search. Then, while
   /// searching, we check to see if an object's sequence key is the same as the
   /// current search key. If it is, it will NOT be added to the list of returns
   /// since it has already been processed.
   ///
   /// @{

   U32  mContainerSeqKey;  ///< Container sequence key

   /// Returns the container sequence key
   U32  getContainerSeqKey() const        { return mContainerSeqKey; }

   /// Sets the container sequence key
   void setContainerSeqKey(const U32 key) { mContainerSeqKey = key;  }
   /// @}

public:

   /// Returns a pointer to the container that contains this object
   Container* getContainer()         { return mContainer;       }

protected:
   S32     mCollisionCount;

   bool    mGlobalBounds;

public:
   /// Returns the type mask for this object
   U32 getTypeMask() { return(mTypeMask); }

   const bool isGlobalBounds() const
   {
      return mGlobalBounds;
   }

   /// 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.
   ///
   /// They can't be toggled currently.
   void setGlobalBounds()
   { 
      if(mContainer)
         mContainer->removeFromBins(this);

      mGlobalBounds = true;
      mObjBox.min.set(-1e10, -1e10, -1e10);
      mObjBox.max.set( 1e10,  1e10,  1e10);

      if(mContainer)
         mContainer->insertIntoBins(this);
   }


   /// @name Rendering Members
   /// @{
protected:
   SceneGraph* mSceneManager;      ///< SceneGraph that controls this object
   U32         mZoneRangeStart;    ///< Start of range of zones this object controls, 0xFFFFFFFF == no zones

   U32         mNumCurrZones;     ///< Number of zones this object exists in

private:
   TraversalState mTraversalState;  ///< State of this object in the SceneGraph traversal - DON'T MESS WITH THIS
   SceneState*    mLastState;       ///< Last SceneState that was used to render this object.
   U32            mLastStateKey;    ///< Last state key that was used to render this object.

   /// @}

   /// @name Persist and console
   /// @{
public:
   static void initPersistFields();
   void inspectPostApply();
   DECLARE_CONOBJECT(SceneObject);

   /// @}
};


//--------------------------------------------------------------------------
/// container合并
#ifdef TGE_RPGCLIENT2 /// TGE_RPGClient2 
extern Container gClientContainer;
#define	gServerContainer	gClientContainer
#else
extern Container gServerContainer;
extern Container gClientContainer;
#endif

//--------------------------------------------------------------------------
//-------------------------------------- Inlines
//
inline bool SceneObject::isManagingZones() const
{
   return mZoneRangeStart != 0xFFFFFFFF;
}

inline void SceneObject::setLastState(SceneState* state, U32 key)
{
   mLastState    = state;
   mLastStateKey = key;
}

inline bool SceneObject::isLastState(SceneState* state, U32 key) const
{
   return (mLastState == state && mLastStateKey == key);
}

inline void SceneObject::setTraversalState( TraversalState s ) {
   mTraversalState = s;
}

inline SceneObject::TraversalState SceneObject::getTraversalState() const {
   return mTraversalState;
}

inline U32 SceneObject::getCurrZone(const U32 index) const
{
   // Not the most efficient way to do this, walking the list,
   //  but it's an uncommon call...
   SceneObjectRef* walk = mZoneRefHead;
   for (U32 i = 0; i < index; i++) 
   {
      walk = walk->nextInObj;
      AssertFatal(walk!=NULL, "Error, too few object refs!");
   }
   AssertFatal(walk!=NULL, "Error, too few object refs!");

   return walk->zone;
}

//--------------------------------------------------------------------------
inline SceneObjectRef* Container::allocateObjectRef()
{
   if (mFreeRefPool == NULL) 
   {
      addRefPoolBlock();
   }
   AssertFatal(mFreeRefPool!=NULL, "Error, should always have a free reference here!");

   SceneObjectRef* ret = mFreeRefPool;
   mFreeRefPool = mFreeRefPool->nextInObj;

   ret->nextInObj = NULL;
   return ret;
}

inline void Container::freeObjectRef(SceneObjectRef* trash)
{
   trash->object = NULL;
   trash->nextInBin = NULL;
   trash->prevInBin = NULL;
   trash->nextInObj = mFreeRefPool;
   mFreeRefPool     = trash;
}

inline void Container::findObjects(U32 mask, FindCallback callback, void *key)
{
   for (Link* itr = mStart.next; itr != &mEnd; itr = itr->next) {
      SceneObject* ptr = static_cast<SceneObject*>(itr);
      if ((ptr->getType() & mask) != 0 && !ptr->mCollisionCount)
         (*callback)(ptr,key);
   }
}

#endif  // _H_SCENEOBJECT_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -