# OgreTerrain.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028
    00029 #ifndef __Ogre_Terrain_H__
    00030 #define __Ogre_Terrain_H__
    00031
    00032 #include "OgreTerrainPrerequisites.h"
    00033 #include "OgreCommon.h"
    00034 #include "OgreVector3.h"
    00035 #include "OgreAxisAlignedBox.h"
    00036 #include "OgreSceneManager.h"
    00037 #include "OgreTerrainMaterialGenerator.h"
    00038 #include "OgreTerrainLayerBlendMap.h"
    00039 #include "OgreWorkQueue.h"
    00040
    00041 namespace Ogre
    00042 {
    00261     class _OgreTerrainExport Terrain : public SceneManager::Listener,
    00262         public WorkQueue::RequestHandler, public WorkQueue::ResponseHandler, public TerrainAlloc
    00263     {
    00264     public:
    00268         Terrain(SceneManager* sm);
    00269         virtual ~Terrain();
    00270
    00271         static const uint32 TERRAIN_CHUNK_ID;
    00272         static const uint16 TERRAIN_CHUNK_VERSION;
    00273         static const uint16 TERRAIN_MAX_BATCH_SIZE;
    00274
    00275         static const uint32 TERRAINLAYERDECLARATION_CHUNK_ID;
    00276         static const uint16 TERRAINLAYERDECLARATION_CHUNK_VERSION;
    00277         static const uint32 TERRAINLAYERSAMPLER_CHUNK_ID;
    00278         static const uint16 TERRAINLAYERSAMPLER_CHUNK_VERSION;
    00279         static const uint32 TERRAINLAYERSAMPLERELEMENT_CHUNK_ID;
    00280         static const uint16 TERRAINLAYERSAMPLERELEMENT_CHUNK_VERSION;
    00281         static const uint32 TERRAINLAYERINSTANCE_CHUNK_ID;
    00282         static const uint16 TERRAINLAYERINSTANCE_CHUNK_VERSION;
    00283         static const uint32 TERRAINDERIVEDDATA_CHUNK_ID;
    00284         static const uint16 TERRAINDERIVEDDATA_CHUNK_VERSION;
    00285
    00286         static const size_t LOD_MORPH_CUSTOM_PARAM;
    00287
    00288         typedef vector<Real>::type RealVector;
    00289
    00292         struct _OgreTerrainExport LayerInstance
    00293         {
    00295             Real worldSize;
    00297             StringVector textureNames;
    00298
    00299             LayerInstance()
    00300                 : worldSize(100) {}
    00301         };
    00302         typedef vector<LayerInstance>::type LayerInstanceList;
    00303
    00305         enum Alignment
    00306         {
    00308             ALIGN_X_Z = 0,
    00310             ALIGN_X_Y = 1,
    00312             ALIGN_Y_Z = 2
    00313         };
    00314
    00318         struct ImportData
    00319         {
    00321             Alignment terrainAlign;
    00323             uint16 terrainSize;
    00329             uint16 maxBatchSize;
    00340             uint16 minBatchSize;
    00341
    00346             Vector3 pos;
    00347
    00349             Real worldSize;
    00350
    00356             Image* inputImage;
    00357
    00362             float* inputFloat;
    00363
    00367             float constantHeight;
    00368
    00376             bool deleteInputData;
    00377
    00379             Real inputScale;
    00381             Real inputBias;
    00382
    00387             TerrainLayerDeclaration layerDeclaration;
    00392             LayerInstanceList layerList;
    00393
    00394             ImportData()
    00395                 : terrainAlign(ALIGN_X_Z)
    00396                 , terrainSize(1025)
    00397                 , maxBatchSize(65)
    00398                 , minBatchSize(17)
    00399                 , pos(Vector3::ZERO)
    00400                 , worldSize(1000)
    00401                 , inputImage(0)
    00402                 , inputFloat(0)
    00403                 , constantHeight(0)
    00404                 , deleteInputData(false)
    00405                 , inputScale(1.0)
    00406                 , inputBias(0.0)
    00407             {
    00408
    00409             }
    00410
    00411             ImportData(const ImportData& rhs)
    00412                 : terrainAlign(ALIGN_X_Z)
    00413                 , terrainSize(1025)
    00414                 , maxBatchSize(65)
    00415                 , minBatchSize(17)
    00416                 , pos(Vector3::ZERO)
    00417                 , worldSize(1000)
    00418                 , inputImage(0)
    00419                 , inputFloat(0)
    00420                 , constantHeight(0)
    00421                 , deleteInputData(false)
    00422                 , inputScale(1.0)
    00423                 , inputBias(0.0)
    00424             {
    00425                 *this = rhs;
    00426             }
    00427
    00428             ImportData& operator=(const ImportData& rhs)
    00429             {
    00430                 // basic copy
    00431                 terrainAlign = rhs.terrainAlign;
    00432                 terrainSize = rhs.terrainSize;
    00433                 maxBatchSize = rhs.maxBatchSize;
    00434                 minBatchSize = rhs.minBatchSize;
    00435                 pos = rhs.pos;
    00436                 worldSize = rhs.worldSize;
    00437                 constantHeight = rhs.constantHeight;
    00438                 deleteInputData = rhs.deleteInputData;
    00439                 inputScale = rhs.inputScale;
    00440                 inputBias = rhs.inputBias;
    00441                 layerDeclaration = rhs.layerDeclaration;
    00442                 layerList = rhs.layerList;
    00443
    00444                 // By-value copies in ownership cases
    00445                 if (rhs.deleteInputData)
    00446                 {
    00447                     if (rhs.inputImage)
    00448                         inputImage = OGRE_NEW Image(*rhs.inputImage);
    00449                     else
    00450                         inputImage = 0;
    00451
    00452                     if (rhs.inputFloat)
    00453                     {
    00454                         inputFloat = OGRE_ALLOC_T(float, terrainSize*terrainSize, MEMCATEGORY_GEOMETRY);
    00455                         memcpy(inputFloat, rhs.inputFloat, sizeof(float) * terrainSize*terrainSize);
    00456                     }
    00457                     else
    00458                         inputFloat = 0;
    00459                 }
    00460                 else
    00461                 {
    00462                     // re-use pointers
    00463                     inputImage = rhs.inputImage;
    00464                     inputFloat = rhs.inputFloat;
    00465                 }
    00466                 return *this;
    00467             }
    00468
    00470             void destroy()
    00471             {
    00472                 if (deleteInputData)
    00473                 {
    00474                     OGRE_DELETE inputImage;
    00475                     OGRE_FREE(inputFloat, MEMCATEGORY_GEOMETRY);
    00476                     inputImage = 0;
    00477                     inputFloat = 0;
    00478                 }
    00479
    00480             }
    00481
    00482             ~ImportData()
    00483             {
    00484                 destroy();
    00485             }
    00486
    00487         };
    00488
    00490         enum NeighbourIndex
    00491         {
    00492             NEIGHBOUR_EAST = 0,
    00493             NEIGHBOUR_NORTHEAST = 1,
    00494             NEIGHBOUR_NORTH = 2,
    00495             NEIGHBOUR_NORTHWEST = 3,
    00496             NEIGHBOUR_WEST = 4,
    00497             NEIGHBOUR_SOUTHWEST = 5,
    00498             NEIGHBOUR_SOUTH = 6,
    00499             NEIGHBOUR_SOUTHEAST = 7,
    00500
    00501             NEIGHBOUR_COUNT = 8
    00502         };
    00503
    00504         SceneManager* getSceneManager() const { return mSceneMgr; }
    00505
    00507         enum Space
    00508         {
    00510             WORLD_SPACE = 0,
    00512             LOCAL_SPACE = 1,
    00516             TERRAIN_SPACE = 2,
    00520             POINT_SPACE = 3
    00521         };
    00522
    00527         class _OgreTerrainExport GpuBufferAllocator : public TerrainAlloc
    00528         {
    00529         public:
    00530             GpuBufferAllocator() {}
    00531             virtual ~GpuBufferAllocator() {}
    00532
    00538             virtual void allocateVertexBuffers(Terrain* forTerrain, size_t numVertices, HardwareVertexBufferSharedPtr& destPos, HardwareVertexBufferSharedPtr& destDelta) = 0;
    00541             virtual void freeVertexBuffers(const HardwareVertexBufferSharedPtr& posbuf, const HardwareVertexBufferSharedPtr& deltabuf) = 0;
    00542
    00557             virtual HardwareIndexBufferSharedPtr getSharedIndexBuffer(uint16 batchSize,
    00558                 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols,
    00559                 uint16 skirtRowColSkip) = 0;
    00560
    00562             virtual void freeAllBuffers() = 0;
    00563
    00564         };
    00566         class _OgreTerrainExport DefaultGpuBufferAllocator : public GpuBufferAllocator
    00567         {
    00568         public:
    00569             DefaultGpuBufferAllocator();
    00570             ~DefaultGpuBufferAllocator();
    00571             void allocateVertexBuffers(Terrain* forTerrain, size_t numVertices, HardwareVertexBufferSharedPtr& destPos, HardwareVertexBufferSharedPtr& destDelta);
    00572             void freeVertexBuffers(const HardwareVertexBufferSharedPtr& posbuf, const HardwareVertexBufferSharedPtr& deltabuf);
    00573             HardwareIndexBufferSharedPtr getSharedIndexBuffer(uint16 batchSize,
    00574                 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols,
    00575                 uint16 skirtRowColSkip);
    00576             void freeAllBuffers();
    00577
    00581             void warmStart(size_t numInstances, uint16 terrainSize, uint16 maxBatchSize,
    00582                 uint16 minBatchSize);
    00583
    00584         protected:
    00585             typedef list<HardwareVertexBufferSharedPtr>::type VBufList;
    00586             VBufList mFreePosBufList;
    00587             VBufList mFreeDeltaBufList;
    00588             typedef map<uint32, HardwareIndexBufferSharedPtr>::type IBufMap;
    00589             IBufMap mSharedIBufMap;
    00590
    00591             uint32 hashIndexBuffer(uint16 batchSize,
    00592                 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols,
    00593                 uint16 skirtRowColSkip);
    00594             HardwareVertexBufferSharedPtr getVertexBuffer(VBufList& list, size_t vertexSize, size_t numVertices);
    00595
    00596         };
    00597
    00602         void setGpuBufferAllocator(GpuBufferAllocator* alloc);
    00603
    00605         GpuBufferAllocator* getGpuBufferAllocator();
    00606
    00608         static size_t _getNumIndexesForBatchSize(uint16 batchSize);
    00620         static void _populateIndexBuffer(uint16* pIndexes, uint16 batchSize,
    00621             uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols,
    00622             uint16 skirtRowColSkip);
    00623
    00625         static uint16 _calcSkirtVertexIndex(uint16 mainIndex, uint16 vdatasize, bool isCol,
    00626             uint16 numSkirtRowsCols, uint16 skirtRowColSkip);
    00627
    00634         void convertPosition(Space inSpace, const Vector3& inPos, Space outSpace, Vector3& outPos) const;
    00641         Vector3 convertPosition(Space inSpace, const Vector3& inPos, Space outSpace) const;
    00648         void convertDirection(Space inSpace, const Vector3& inDir, Space outSpace, Vector3& outDir) const;
    00655         Vector3 convertDirection(Space inSpace, const Vector3& inDir, Space outSpace) const;
    00656
    00661         void setResourceGroup(const String& resGroup) { mResourceGroup = resGroup; }
    00662
    00666         const String& getResourceGroup() const { return mResourceGroup; }
    00667
    00670         const String& _getDerivedResourceGroup() const;
    00671
    00680         void save(const String& filename);
    00686         void save(StreamSerialiser& stream);
    00687
    00694         bool prepare(const String& filename);
    00701         bool prepare(StreamSerialiser& stream);
    00702
    00708         bool prepare(const ImportData& importData);
    00709
    00715         void load(const String& filename);
    00716
    00722         void load(StreamSerialiser& stream);
    00723
    00728         void load();
    00729
    00735         bool isLoaded() const { return mIsLoaded; }
    00736
    00741         bool isModified() const { return mModified; }
    00742
    00743
    00748         bool isHeightDataModified() const { return mHeightDataModified; }
    00749
    00750
    00755         void unload();
    00756
    00761         void unprepare();
    00762
    00763
    00773         float* getHeightData() const;
    00774
    00777         float* getHeightData(long x, long y) const;
    00778
    00783         float getHeightAtPoint(long x, long y) const;
    00784
    00791         void setHeightAtPoint(long x, long y, float h);
    00792
    00796         float getHeightAtTerrainPosition(Real x, Real y);
    00797
    00803         float getHeightAtWorldPosition(Real x, Real y, Real z);
    00804
    00810         float getHeightAtWorldPosition(const Vector3& pos);
    00811
    00818         const float* getDeltaData();
    00819
    00822         const float* getDeltaData(long x, long y);
    00823
    00828         void getPoint(long x, long y, Vector3* outpos);
    00829
    00835         void getPointFromSelfOrNeighbour(long x, long y, Vector3* outpos);
    00836
    00841         void getPoint(long x, long y, float height, Vector3* outpos);
    00846         void getTerrainVector(const Vector3& inVec, Vector3* outVec);
    00851         void getTerrainVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec);
    00852
    00857         void getTerrainVector(Real x, Real y, Real z, Vector3* outVec);
    00862         void getTerrainVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec);
    00863
    00868         void getVector(const Vector3& inVec, Vector3* outVec);
    00873         void getVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec);
    00874
    00879         void getVector(Real x, Real y, Real z, Vector3* outVec);
    00884         void getVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec);
    00885
    00886
    00894         void getPosition(const Vector3& TSpos, Vector3* outWSpos);
    00902         void getPosition(Real x, Real y, Real z, Vector3* outWSpos);
    00903
    00910         void getTerrainPosition(const Vector3& WSpos, Vector3* outTSpos);
    00917         void getTerrainPosition(Real x, Real y, Real z, Vector3* outTSpos);
    00924         void getPositionAlign(const Vector3& TSpos, Alignment align, Vector3* outWSpos);
    00931         void getPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outWSpos);
    00932
    00939         void getTerrainPositionAlign(const Vector3& WSpos, Alignment align, Vector3* outTSpos);
    00946         void getTerrainPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outTSpos);
    00947
    00948
    00950         Alignment getAlignment() const;
    00952         uint16 getSize() const;
    00954         uint16 getMaxBatchSize() const;
    00956         uint16 getMinBatchSize() const;
    00958         Real getWorldSize() const;
    00959
    00961         uint8 getLayerCount() const { return static_cast<uint8>(mLayers.size()); }
    00962
    00964         const TerrainLayerDeclaration& getLayerDeclaration() const { return mLayerDecl; }
    00965
    00972         void addLayer(Real worldSize = 0, const StringVector* textureNames = 0);
    00973
    00981         void addLayer(uint8 index, Real worldSize = 0, const StringVector* textureNames = 0);
    00982
    00985         void removeLayer(uint8 index);
    00986
    00996         void replaceLayer(uint8 index, bool keepBlends, Real worldSize = 0, const StringVector* textureNames = 0);
    00997
    01001         uint8 getMaxLayers() const;
    01002
    01007         Real getLayerWorldSize(uint8 index) const;
    01013         void setLayerWorldSize(uint8 index, Real size);
    01014
    01023         Real getLayerUVMultiplier(uint8 index) const;
    01024
    01030         const String& getLayerTextureName(uint8 layerIndex, uint8 samplerIndex) const;
    01037         void setLayerTextureName(uint8 layerIndex, uint8 samplerIndex, const String& textureName);
    01038
    01045         uint16 getLayerBlendMapSize() const { return mLayerBlendMapSize; }
    01046
    01052         uint16 getLightmapSize() const { return mLightmapSize; }
    01053
    01055         const TexturePtr& getLightmap() const { return mLightmap; }
    01056
    01062         uint16 getCompositeMapSize() const { return mCompositeMapSize; }
    01063
    01065         const TexturePtr& getCompositeMap() const { return mCompositeMap; }
    01066
    01068         const Vector3& getPosition() const { return mPos; }
    01070         void setPosition(const Vector3& pos);
    01072         SceneNode* _getRootSceneNode() const;
    01079         void dirty();
    01080
    01089         void dirtyRect(const Rect& rect);
    01090
    01096         void _dirtyCompositeMapRect(const Rect& rect);
    01097
    01108         void dirtyLightmapRect(const Rect& rect);
    01109
    01120         void dirtyLightmap();
    01121
    01144         void update(bool synchronous = false);
    01145
    01150         void updateGeometry();
    01151
    01152         // Used as a type mask for updateDerivedData
    01153         static const uint8 DERIVED_DATA_DELTAS;
    01154         static const uint8 DERIVED_DATA_NORMALS;
    01155         static const uint8 DERIVED_DATA_LIGHTMAP;
    01156         static const uint8 DERIVED_DATA_ALL;
    01157
    01169         void updateDerivedData(bool synchronous = false, uint8 typeMask = 0xFF);
    01170
    01179         void updateCompositeMap();
    01180
    01194         void updateCompositeMapWithDelay(Real delay = 2);
    01195
    01196
    01200         Real getSkirtSize() const { return mSkirtSize; }
    01201
    01203         uint16 getNumLodLevels() const { return mNumLodLevels; }
    01204
    01206         uint16 getNumLodLevelsPerLeaf() const { return mNumLodLevelsPerLeafNode; }
    01207
    01215         Rect calculateHeightDeltas(const Rect& rect);
    01216
    01224         void finaliseHeightDeltas(const Rect& rect, bool cpuData);
    01225
    01231         PixelBox* calculateNormals(const Rect& rect, Rect& outFinalRect);
    01232
    01240         void finaliseNormals(const Rect& rect, PixelBox* normalsBox);
    01241
    01249         PixelBox* calculateLightmap(const Rect& rect, const Rect& extraTargetRect, Rect& outFinalRect);
    01250
    01258         void finaliseLightmap(const Rect& rect, PixelBox* lightmapBox);
    01259
    01263         uint16 getResolutionAtLod(uint16 lodLevel);
    01264
    01276         std::pair<bool, Vector3> rayIntersects(const Ray& ray,
    01277             bool cascadeToNeighbours = false, Real distanceLimit = 0); //const;
    01278
    01280         const AxisAlignedBox& getAABB() const;
    01282         AxisAlignedBox getWorldAABB() const;
    01284         Real getMinHeight() const;
    01286         Real getMaxHeight() const;
    01288         Real getBoundingRadius() const;
    01289
    01291         const MaterialPtr& getMaterial() const;
    01293         const MaterialPtr& _getMaterial() const { return mMaterial; }
    01295         const MaterialPtr& getCompositeMapMaterial() const;
    01297         const MaterialPtr& _getCompositeMapMaterial() const { return mCompositeMapMaterial; }
    01298
    01300         const String& getMaterialName() const { return mMaterialName; }
    01301
    01303         void preFindVisibleObjects(SceneManager* source,
    01304             SceneManager::IlluminationRenderStage irs, Viewport* v);
    01306         void sceneManagerDestroyed(SceneManager* source);
    01307
    01309         uint8 getRenderQueueGroup(void) const { return mRenderQueueGroup; }
    01313         void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; }
    01314
    01316         uint32 getVisibilityFlags(void) const { return mVisibilityFlags; }
    01320         void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; }
    01321
    01323         uint32 getQueryFlags(void) const { return mQueryFlags; }
    01327         void setQueryFlags(uint32 flags) { mQueryFlags = flags; }
    01328
    01330         void addQueryFlags(uint32 flags) { mQueryFlags |= flags; }
    01331
    01332         /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */
    01333         void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; }
    01334
    01335
    01348         TerrainLayerBlendMap* getLayerBlendMap(uint8 layerIndex);
    01349
    01355         uint8 getBlendTextureIndex(uint8 layerIndex) const;
    01356
    01358         uint8 getBlendTextureCount() const;
    01360         uint8 getBlendTextureCount(uint8 numLayers) const;
    01361
    01362
    01367         const String& getBlendTextureName(uint8 textureIndex) const;
    01368
    01381         void setGlobalColourMapEnabled(bool enabled, uint16 size = 0);
    01383         bool getGlobalColourMapEnabled() const { return mGlobalColourMapEnabled; }
    01385         uint16 getGlobalColourMapSize() const { return mGlobalColourMapSize; }
    01387         const TexturePtr& getGlobalColourMap() const { return mColourMap; }
    01388
    01394         void widenRectByVector(const Vector3& vec, const Rect& inRect, Rect& outRect);
    01395
    01403         void widenRectByVector(const Vector3& vec, const Rect& inRect,
    01404             Real minHeight, Real maxHeight, Rect& outRect);
    01405
    01415         void freeTemporaryResources();
    01416
    01421         const TexturePtr& getLayerBlendTexture(uint8 index);
    01422
    01429         std::pair<uint8,uint8> getLayerBlendTextureIndex(uint8 layerIndex);
    01430
    01441         void _setMorphRequired(bool morph) { mLodMorphRequired = morph; }
    01443         bool _getMorphRequired() const { return mLodMorphRequired; }
    01444
    01456         void _setNormalMapRequired(bool normalMap);
    01457
    01471         void _setLightMapRequired(bool lightMap, bool shadowsOnly = false);
    01472
    01491         void _setCompositeMapRequired(bool compositeMap);
    01492
    01494         bool canHandleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ);
    01496         WorkQueue::Response* handleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ);
    01498         bool canHandleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ);
    01500         void handleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ);
    01501
    01502         static const uint16 WORKQUEUE_DERIVED_DATA_REQUEST;
    01503
    01504
    01506         uint16 getLODLevelWhenVertexEliminated(long x, long y);
    01508         uint16 getLODLevelWhenVertexEliminated(long rowOrColulmn);
    01509
    01510
    01512         TerrainQuadTreeNode* getQuadTree() { return mQuadTree; }
    01513
    01515         TexturePtr getTerrainNormalMap() const { return mTerrainNormalMap; }
    01516
    01524         Terrain* getNeighbour(NeighbourIndex index);
    01525
    01543         void setNeighbour(NeighbourIndex index, Terrain* neighbour, bool recalculate = false, bool notifyOther = true);
    01544
    01549         static NeighbourIndex getOppositeNeighbour(NeighbourIndex index);
    01550
    01553         static NeighbourIndex getNeighbourIndex(long offsetx, long offsety);
    01554
    01562         void notifyNeighbours();
    01563
    01571         void neighbourModified(NeighbourIndex index, const Rect& edgerect, const Rect& shadowrect);
    01572
    01578         Terrain* raySelectNeighbour(const Ray& ray, Real distanceLimit = 0);
    01579
    01584         void _dumpTextures(const String& prefix, const String& suffix);
    01585
    01587         bool isDerivedDataUpdateInProgress() const { return mDerivedDataUpdateInProgress; }
    01588
    01589
    01591         static void convertWorldToTerrainAxes(Alignment align, const Vector3& worldVec, Vector3* terrainVec);
    01593         static void convertTerrainToWorldAxes(Alignment align, const Vector3& terrainVec, Vector3* worldVec);
    01594
    01596         static void writeLayerDeclaration(const TerrainLayerDeclaration& decl, StreamSerialiser& ser);
    01598         static bool readLayerDeclaration(StreamSerialiser& ser, TerrainLayerDeclaration& targetdecl);
    01600         static void writeLayerInstanceList(const Terrain::LayerInstanceList& lst, StreamSerialiser& ser);
    01602         static bool readLayerInstanceList(StreamSerialiser& ser, size_t numSamplers, Terrain::LayerInstanceList& targetlst);
    01603     protected:
    01604
    01605         void freeCPUResources();
    01606         void freeGPUResources();
    01607         void determineLodLevels();
    01608         void distributeVertexData();
    01609         void updateBaseScale();
    01610         void createGPUBlendTextures();
    01611         void createLayerBlendMaps();
    01612         void createOrDestroyGPUNormalMap();
    01613         void createOrDestroyGPUColourMap();
    01614         void createOrDestroyGPULightmap();
    01615         void createOrDestroyGPUCompositeMap();
    01616         void waitForDerivedProcesses();
    01617         void convertSpace(Space inSpace, const Vector3& inVec, Space outSpace, Vector3& outVec, bool translation) const;
    01618         Vector3 convertWorldToTerrainAxes(const Vector3& inVec) const;
    01619         Vector3 convertTerrainToWorldAxes(const Vector3& inVec) const;
    01623         void getPointAlign(long x, long y, Alignment align, Vector3* outpos);
    01628         void getPointAlign(long x, long y, float height, Alignment align, Vector3* outpos);
    01629         void calculateCurrentLod(Viewport* vp);
    01631         std::pair<bool, Vector3> checkQuadIntersection(int x, int y, const Ray& ray); //const;
    01632
    01634         void deleteBlendMaps(uint8 lowIndex);
    01636         void shiftUpGPUBlendChannels(uint8 index);
    01638         void shiftDownGPUBlendChannels(uint8 index);
    01640         void copyBlendTextureChannel(uint8 srcIndex, uint8 srcChannel, uint8 destIndex, uint8 destChannel );
    01642         void clearGPUBlendChannel(uint8 index, uint channel);
    01643
    01644         void copyGlobalOptions();
    01645         void checkLayers(bool includeGPUResources);
    01646         void checkDeclaration();
    01647         void deriveUVMultipliers();
    01648         PixelFormat getBlendTextureFormat(uint8 textureIndex, uint8 numLayers);
    01649
    01650         void updateDerivedDataImpl(const Rect& rect, const Rect& lightmapExtraRect, bool synchronous, uint8 typeMask);
    01651
    01652         void getEdgeRect(NeighbourIndex index, long range, Rect* outRect);
    01653         // get the equivalent of the passed in edge rectangle in neighbour
    01654         void getNeighbourEdgeRect(NeighbourIndex index, const Rect& inRect, Rect* outRect);
    01655         // get the equivalent of the passed in edge point in neighbour
    01656         void getNeighbourPoint(NeighbourIndex index, long x, long y, long *outx, long *outy);
    01657         // overflow a point into a neighbour index and point
    01658         void getNeighbourPointOverflow(long x, long y, NeighbourIndex *outindex, long *outx, long *outy);
    01659
    01660
    01661
    01662         uint16 mWorkQueueChannel;
    01663         SceneManager* mSceneMgr;
    01664         SceneNode* mRootNode;
    01665         String mResourceGroup;
    01666         bool mIsLoaded;
    01667         bool mModified;
    01668         bool mHeightDataModified;
    01669
    01671         float* mHeightData;
    01673         float* mDeltaData;
    01674         Alignment mAlign;
    01675         Real mWorldSize;
    01676         uint16 mSize;
    01677         uint16 mMaxBatchSize;
    01678         uint16 mMinBatchSize;
    01679         Vector3 mPos;
    01680         TerrainQuadTreeNode* mQuadTree;
    01681         uint16 mNumLodLevels;
    01682         uint16 mNumLodLevelsPerLeafNode;
    01683         uint16 mTreeDepth;
    01685         Real mBase;
    01687         Real mScale;
    01688         TerrainLayerDeclaration mLayerDecl;
    01689         LayerInstanceList mLayers;
    01690         RealVector mLayerUVMultiplier;
    01691
    01692         Real mSkirtSize;
    01693         uint8 mRenderQueueGroup;
    01694         uint32 mVisibilityFlags;
    01695         uint32 mQueryFlags;
    01696
    01697         Rect mDirtyGeometryRect;
    01698         Rect mDirtyDerivedDataRect;
    01699         Rect mDirtyGeometryRectForNeighbours;
    01700         Rect mDirtyLightmapFromNeighboursRect;
    01701         bool mDerivedDataUpdateInProgress;
    01702         uint8 mDerivedUpdatePendingMask; // if another update is requested while one is already running
    01703
    01705         struct DerivedDataRequest
    01706         {
    01707             Terrain* terrain;
    01708             // types requested
    01709             uint8 typeMask;
    01710             Rect dirtyRect;
    01711             Rect lightmapExtraDirtyRect;
    01712             _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const DerivedDataRequest& r)
    01713             { return o; }
    01714         };
    01715
    01717         struct DerivedDataResponse
    01718         {
    01719             Terrain* terrain;
    01720             // remaining types not yet processed
    01721             uint8 remainingTypeMask;
    01722             // The area of deltas that was updated
    01723             Rect deltaUpdateRect;
    01724             // the area of normals that was updated
    01725             Rect normalUpdateRect;
    01726             // the area of lightmap that was updated
    01727             Rect lightmapUpdateRect;
    01728             // all CPU-side data, independent of textures; to be blitted in main thread
    01729             PixelBox* normalMapBox;
    01730             PixelBox* lightMapBox;
    01731             _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const DerivedDataResponse& r)
    01732             { return o; }
    01733         };
    01734
    01735         String mMaterialName;
    01736         mutable MaterialPtr mMaterial;
    01737         mutable TerrainMaterialGeneratorPtr mMaterialGenerator;
    01738         mutable unsigned long long int mMaterialGenerationCount;
    01739         mutable bool mMaterialDirty;
    01740         mutable bool mMaterialParamsDirty;
    01741
    01742         uint16 mLayerBlendMapSize;
    01743         uint16 mLayerBlendMapSizeActual;
    01744         typedef vector<uint8*>::type BytePointerList;
    01746         BytePointerList mCpuBlendMapStorage;
    01747         typedef vector<TexturePtr>::type TexturePtrList;
    01748         TexturePtrList mBlendTextureList;
    01749         TerrainLayerBlendMapList mLayerBlendMapList;
    01750
    01751         uint16 mGlobalColourMapSize;
    01752         bool mGlobalColourMapEnabled;
    01753         TexturePtr mColourMap;
    01754         uint8* mCpuColourMapStorage;
    01755
    01756         uint16 mLightmapSize;
    01757         uint16 mLightmapSizeActual;
    01758         TexturePtr mLightmap;
    01759         uint8* mCpuLightmapStorage;
    01760
    01761         uint16 mCompositeMapSize;
    01762         uint16 mCompositeMapSizeActual;
    01763         TexturePtr mCompositeMap;
    01764         uint8* mCpuCompositeMapStorage;
    01765         Rect mCompositeMapDirtyRect;
    01766         unsigned long mCompositeMapUpdateCountdown;
    01767         unsigned long mLastMillis;
    01769         bool mCompositeMapDirtyRectLightmapUpdate;
    01770         mutable MaterialPtr mCompositeMapMaterial;
    01771
    01772
    01773         static NameGenerator msBlendTextureGenerator;
    01774         static NameGenerator msNormalMapNameGenerator;
    01775         static NameGenerator msLightmapNameGenerator;
    01776         static NameGenerator msCompositeMapNameGenerator;
    01777
    01778         bool mLodMorphRequired;
    01779         bool mNormalMapRequired;
    01780         bool mLightMapRequired;
    01781         bool mLightMapShadowsOnly;
    01782         bool mCompositeMapRequired;
    01784         TexturePtr mTerrainNormalMap;
    01785
    01787         PixelBox* mCpuTerrainNormalMap;
    01788
    01789         const Camera* mLastLODCamera;
    01790         unsigned long mLastLODFrame;
    01791         int mLastViewportHeight;
    01792
    01793         Terrain* mNeighbours[NEIGHBOUR_COUNT];
    01794
    01795         GpuBufferAllocator* mCustomGpuBufferAllocator;
    01796         DefaultGpuBufferAllocator mDefaultGpuBufferAllocator;
    01797
    01798         size_t getPositionBufVertexSize() const;
    01799         size_t getDeltaBufVertexSize() const;
    01800
    01801     };
    01802
    01803
    01813     class _OgreTerrainExport TerrainGlobalOptions : public TerrainAlloc, public Singleton<TerrainGlobalOptions>
    01814     {
    01815     protected:
    01816
    01817         Real mSkirtSize;
    01818         Vector3 mLightMapDir;
    01819         bool mCastsShadows;
    01820         Real mMaxPixelError;
    01821         uint8 mRenderQueueGroup;
    01822         uint32 mVisibilityFlags;
    01823         uint32 mQueryFlags;
    01824         bool mUseRayBoxDistanceCalculation;
    01825         TerrainMaterialGeneratorPtr mDefaultMaterialGenerator;
    01826         uint16 mLayerBlendMapSize;
    01827         Real mDefaultLayerTextureWorldSize;
    01828         uint16 mDefaultGlobalColourMapSize;
    01829         uint16 mLightmapSize;
    01830         uint16 mCompositeMapSize;
    01831         ColourValue mCompositeMapAmbient;
    01832         ColourValue mCompositeMapDiffuse;
    01833         Real mCompositeMapDistance;
    01834         String mResourceGroup;
    01835
    01836     public:
    01837         TerrainGlobalOptions();
    01838         virtual ~TerrainGlobalOptions() {}
    01839
    01840
    01844         Real getSkirtSize() { return mSkirtSize; }
    01850         void setSkirtSize(Real skirtSz) { mSkirtSize = skirtSz; }
    01852         const Vector3& getLightMapDirection() { return mLightMapDir; }
    01854         void setLightMapDirection(const Vector3& v) { mLightMapDir = v; }
    01856         const ColourValue& getCompositeMapAmbient() { return mCompositeMapAmbient; }
    01858         void setCompositeMapAmbient(const ColourValue& c) { mCompositeMapAmbient = c; }
    01860         const ColourValue& getCompositeMapDiffuse() { return mCompositeMapDiffuse; }
    01862         void setCompositeMapDiffuse(const ColourValue& c) { mCompositeMapDiffuse = c; }
    01864         Real getCompositeMapDistance() { return mCompositeMapDistance; }
    01866         void setCompositeMapDistance(Real c) { mCompositeMapDistance = c; }
    01867
    01868
    01872         bool getCastsDynamicShadows() { return mCastsShadows; }
    01873
    01879         void setCastsDynamicShadows(bool s) { mCastsShadows = s; }
    01880
    01882         Real getMaxPixelError() { return mMaxPixelError; }
    01883
    01889         void setMaxPixelError(Real pixerr) { mMaxPixelError = pixerr; }
    01890
    01892         uint8 getRenderQueueGroup(void) { return mRenderQueueGroup; }
    01897         void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; }
    01898
    01900         uint32 getVisibilityFlags(void) { return mVisibilityFlags; }
    01905         void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; }
    01906
    01911         void  setQueryFlags(uint32 flags) { mQueryFlags = flags; }
    01914         uint32 getQueryFlags(void) { return mQueryFlags; }
    01915
    01917         void addQueryFlags(uint32 flags) { mQueryFlags |= flags; }
    01918
    01919         /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */
    01920         void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; }
    01921
    01926         bool getUseRayBoxDistanceCalculation() { return mUseRayBoxDistanceCalculation; }
    01927
    01939         void setUseRayBoxDistanceCalculation(bool rb) { mUseRayBoxDistanceCalculation = rb; }
    01940
    01943         TerrainMaterialGeneratorPtr getDefaultMaterialGenerator();
    01944
    01947         void setDefaultMaterialGenerator(TerrainMaterialGeneratorPtr gen);
    01948
    01951         uint16 getLayerBlendMapSize() { return mLayerBlendMapSize; }
    01952
    01957         void setLayerBlendMapSize(uint16 sz) { mLayerBlendMapSize = sz;}
    01958
    01961         Real getDefaultLayerTextureWorldSize() { return mDefaultLayerTextureWorldSize; }
    01962
    01965         void setDefaultLayerTextureWorldSize(Real sz) { mDefaultLayerTextureWorldSize = sz; }
    01966
    01969         uint16 getDefaultGlobalColourMapSize() { return mDefaultGlobalColourMapSize; }
    01970
    01974         void setDefaultGlobalColourMapSize(uint16 sz) { mDefaultGlobalColourMapSize = sz;}
    01975
    01976
    01979         uint16 getLightMapSize() { return mLightmapSize; }
    01980
    01983         void setLightMapSize(uint16 sz) { mLightmapSize = sz;}
    01984
    01987         uint16 getCompositeMapSize() { return mCompositeMapSize; }
    01988
    01991         void setCompositeMapSize(uint16 sz) { mCompositeMapSize = sz;}
    01992
    01995         void setDefaultResourceGroup(const String& grp) { mResourceGroup = grp; }
    01996
    01999         const String& getDefaultResourceGroup() { return mResourceGroup; }
    02000
    02016         static TerrainGlobalOptions& getSingleton(void);
    02032         static TerrainGlobalOptions* getSingletonPtr(void);
    02033
    02034
    02035     };
    02036
    02037
    02040 }
    02041
    02042
    02043
    02044
    02045 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun May 8 2011 20:28:09