/* ----------------------------------------------------------------------------- This source file is part of the GameTools Project http://www.gametools.org Author: Martin Szydlowski ----------------------------------------------------------------------------- */ #ifndef _KdTreeTerrainSceneManager_H__ #define _KdTreeTerrainSceneManager_H__ #include #include "OgreKdTerrainPrerequisites.h" #include "OgreKdTerrainPageSource.h" #include "OgreKdTerrainRenderable.h" #include "OgreKdTreeSceneManager.h" namespace Ogre { typedef std::vector < KdTerrainPage * > KdTerrainPageRow; typedef std::vector < KdTerrainPageRow > KdTerrainPage2D; /** Default implementation of RaySceneQuery. */ // TODO: all scene queries in kd-tree class _OgreKdTerrainExport KdTerrainRaySceneQuery : public DefaultRaySceneQuery { protected: WorldFragment mWorldFrag; public: KdTerrainRaySceneQuery(SceneManager* creator); ~KdTerrainRaySceneQuery(); /** See RayScenQuery. */ void execute(RaySceneQueryListener* listener); }; class KdTerrainSceneManager : public KdTreeSceneManager { public: KdTerrainSceneManager(const String& name, GtpVisibility::VisibilityManager *vm); ~KdTerrainSceneManager(void); virtual const String& getTypeName(void) const; /************************************************************************/ /* Functions overriden from (KdTree)SceneManager */ /************************************************************************/ /** Loads the terrain using parameters int he given config file. */ void setWorldGeometry( const String& filename ); /** Loads the terrain using parameters in the given config file (contained in a stream). */ virtual void setWorldGeometry(DataStreamPtr& stream, const String& typeName = StringUtil::BLANK); /** Sets the given option for the SceneManager. @remarks Options are (in addition to those supported by superclasses): "PageSize", int*; "TileSize", int*; "PrimaryCamera, Camera*; "MaxMipMapLevel", int*; "Scale", Vector3 *; "MaxPixelError", int*; "UseTriStrips", bool*; "VertexProgramMorph", bool*; "DetailTile", int*; "LodMorphStart", Real*; "VertexNormals", bool*; "VertexColours", bool*; "MorphLODFactorParamName", String*; "MorphLODFactorParamIndex", size_t*; "CustomMaterialName", String*; "WorldTexture", String*; "DetailTexture", String*; */ virtual bool setOption(const String& strKey, const void* pValue); // Overridden from kdtree scene mananger //void _findVisibleObjects(Camera *cam, bool onlyShadowCasters); // Overridden from kdtree scene manager void _renderScene(Camera *cam, Viewport *vp, bool includeOverlays); /** Overridden to user _renderVisibleObjects from KdTreeSceneManager */ //virtual void _renderVisibleObjects( void ); /** Overridden from SceneManager */ void setWorldGeometryRenderQueue(uint8 qid); /** Overridden in order to store the first camera created as the primary one, for determining error metrics and the 'home' terrain page. */ Camera* createCamera( const String &name ); /** Creates a RaySceneQuery for this scene manager. */ RaySceneQuery* createRayQuery(const Ray& ray, unsigned long mask = 0xFFFFFFFF); /** Overide from scene manager to destroy kdtree properly (before the scene graph is destroyed) */ virtual void clearScene(); /************************************************************************/ /* Functions for terrain rendering */ /************************************************************************/ /** Returns the height at the given terrain coordinates. */ float getHeightAt( float x, float y ); bool intersectSegment( const Vector3 & start, const Vector3 & end, Vector3 * result ); /** Sets the texture to use for the main world texture. */ void setWorldTexture(const String& textureName); /** Sets the texture to use for the detail texture. */ void setDetailTexture(const String& textureName); /** Sets the number of times per tile the detail texture should be repeated. */ void setDetailTextureRepeat(int repeat); /** Sets the dimensions of each tile (must be power of 2 + 1) */ void setTileSize(int size); /** Sets the dimensions of each page (must be power of 2 + 1) */ void setPageSize(int size); /** Sets the maximum screen space pixel error. */ void setMaxPixelError(int pixelError); /** Sets how to scale the terrain data. */ void setScale(const Vector3& scale); /** Sets the maximum geomipmap level to allow. */ void setMaxGeoMipMapLevel(int maxMip); /** Gets the texture to use for the main world texture. */ const String& getWorldTexture(void) { return mWorldTextureName; } /** Gets the texture to use for the detail texture. */ const String& getDetailTexture(void) { return mDetailTextureName; } /** Gets the number of times per tile the detail texture should be repeated. */ int getDetailTextureRepeat(void); /** Gets the dimensions of each tile (must be power of 2 + 1) */ int getTileSize(void); /** Gets the dimensions of each page (must be power of 2 + 1) */ int getPageSize(void); /** Gets the maximum screen space pixel error. */ int getMaxPixelError(void); /** Gets how to scale the terrain data. */ const Vector3& getScale(void); /** Gets the maximum geomipmap level to allow. */ int getMaxGeoMipMapLevel(void); /** Sets whether the terrain should use triangle strips or not. */ void setUseTriStrips(bool useStrips); /** Sets whether or not terrain tiles should be morphed between LODs (NB requires vertex program support). */ void setUseLODMorph(bool useMorph); /** Sets whether vertex normals will be generated for the terrain. */ void setUseVertexNormals(bool useNormals); /** Sets whether vertex colours will be used. */ void setUseVertexColours(bool useColours); /** Sets the name of a custom material to use to shade the landcape. */ void setCustomMaterial(const String& materialName); /** Sets the name of the vertex program parameter to which to pass the LOD morph factor. */ void setCustomMaterialMorphFactorParam(const String& paramName); /** Sets the index of the vertex program parameter to which to pass the LOD morph factor. */ void setCustomMaterialMorphFactorParam(uint paramIndex); /** Sets the distance at which the LOD will start to morph downwards, as a proportion of the distance between the LODs. */ void setLODMorphStart(Real morphStart); /** Returns the TerrainRenderable that contains the given pt. If no tile exists at the point, it returns 0; */ virtual KdTerrainRenderable * getTerrainTile( const Vector3 & pt ); /** Returns the TerrainPage that contains the given pt. If no page exists at the point, it returns 0; */ virtual KdTerrainPage* getTerrainPage( const Vector3 & pt ); /// Gets the terrain options const KdTerrainOptions& getOptions(void) { return mOptions; } /** Sets the 'primary' camera, i.e. the one which will be used to determine the 'home' terrain page, and to calculate the error metrics. */ virtual void setPrimaryCamera(const Camera* cam); /// Internal map of page source name to page source typedef std::map KdPageSourceMap; /// Iterator over all page sources typedef ConstMapIterator KdPageSourceIterator; /// Get an iterator over all page sources KdPageSourceIterator getPageSourceIterator(void); /** Registers a TerrainPageSource class and associates it with a named type of source. */ virtual void registerPageSource(const String& typeName, KdTerrainPageSource* source); /** Selects a given page source based on its type name. */ virtual void selectPageSource(const String& typeName, KdTerrainPageSourceOptionList& optionList); /** Attaches a previously built page to the list of available pages. */ virtual void attachPage(ushort pageX, ushort pageZ, KdTerrainPage* page); /// Get a pointer to the material being used for the terrain MaterialPtr& getTerrainMaterial(void); /// Get the SceneNode under which all terrain nodes are attached. SceneNode* getTerrainRootNode(void) const { return mTerrainRoot; } /// Get the shared list of indexes cached (internal use only) KdTerrainBufferCache& _getIndexCache(void) {return mIndexCache;} /// Get the shared level index list (internal use only) LevelArray& _getLevelIndex(void) { return mLevelIndex; } /// Get the current page count (internal use only) uint _getPageCount(void) { return (uint)mTerrainPages.size(); } /// Shutdown cleanly before we get destroyed void shutdown(void); protected: /// Validates that the size picked for the terrain is acceptable bool _checkSize( int s ) { for ( int i = 0; i < 16; i++ ) { printf( "Checking...%d\n", ( 1 << i ) + 1 ); if ( s == ( 1 << i ) + 1 ) return true; } return false; } /// The node to which all terrain tiles are attached SceneNode * mTerrainRoot; /// Terrain size, detail etc KdTerrainOptions mOptions; /// Should we use an externally-defined custom material? bool mUseCustomMaterial; /// The name of the custom material to use String mCustomMaterialName; /// The name of the world texture String mWorldTextureName; /// The name of the detail texture String mDetailTextureName; /// Are we using a named parameter to hook up LOD morph? bool mUseNamedParameterLodMorph; /// The name of the parameter to send the LOD morph to String mLodMorphParamName; /// The index of the parameter to send the LOD morph to uint mLodMorphParamIndex; /// Whether paging is enabled, or whether a single page will be used bool mPagingEnabled; /// The number of pages to render outside the 'home' page unsigned short mLivePageMargin; /// The number of pages to keep loaded outside the 'home' page unsigned short mBufferedPageMargin; /// Grid of buffered pages KdTerrainPage2D mTerrainPages; //-- attributes to share across tiles /// Shared list of index buffers KdTerrainBufferCache mIndexCache; /// Shared array of IndexData (reuse indexes across tiles) LevelArray mLevelIndex; /// Internal method for loading configurations settings void loadConfig(DataStreamPtr& stream); /// Sets up the terrain material void setupTerrainMaterial(void); /// Sets up the terrain page slots void setupTerrainPages(void); /// Initialise level indexes void initLevelIndexes(void); /// Destroy level indexes void destroyLevelIndexes(void); /// Map of source type -> TerrainPageSource KdPageSourceMap mPageSources; /// The currently active page source KdTerrainPageSource* mActivePageSource; }; /// Factory for KdTreeSceneManager class KdTerrainSceneManagerFactory : public SceneManagerFactory { protected: typedef std::vector KdTerrainPageSources; KdTerrainPageSources mTerrainPageSources; GtpVisibility::VisibilityManager *visManager; void initMetaData(void) const; public: KdTerrainSceneManagerFactory(GtpVisibility::VisibilityManager * vm); ~KdTerrainSceneManagerFactory(void); /// Factory type name static const String FACTORY_TYPE_NAME; SceneManager* createInstance(const String& instanceName); void destroyInstance(SceneManager* instance); }; } // namespace Ogre #endif // _KdTreeTerrainSceneManager_H__