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-2013 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 #include "OgreTerrainLodManager.h" 00041 00042 namespace Ogre 00043 { 00262 class _OgreTerrainExport Terrain : public SceneManager::Listener, 00263 public WorkQueue::RequestHandler, public WorkQueue::ResponseHandler, public TerrainAlloc 00264 { 00265 public: 00266 friend class TerrainLodManager; 00267 00271 Terrain(SceneManager* sm); 00272 virtual ~Terrain(); 00273 00274 static const uint32 TERRAIN_CHUNK_ID; 00275 static const uint16 TERRAIN_CHUNK_VERSION; 00276 static const uint16 TERRAIN_MAX_BATCH_SIZE; 00277 static const uint64 TERRAIN_GENERATE_MATERIAL_INTERVAL_MS; 00278 00279 static const uint32 TERRAINLAYERDECLARATION_CHUNK_ID; 00280 static const uint16 TERRAINLAYERDECLARATION_CHUNK_VERSION; 00281 static const uint32 TERRAINLAYERSAMPLER_CHUNK_ID; 00282 static const uint16 TERRAINLAYERSAMPLER_CHUNK_VERSION; 00283 static const uint32 TERRAINLAYERSAMPLERELEMENT_CHUNK_ID; 00284 static const uint16 TERRAINLAYERSAMPLERELEMENT_CHUNK_VERSION; 00285 static const uint32 TERRAINLAYERINSTANCE_CHUNK_ID; 00286 static const uint16 TERRAINLAYERINSTANCE_CHUNK_VERSION; 00287 static const uint32 TERRAINDERIVEDDATA_CHUNK_ID; 00288 static const uint16 TERRAINDERIVEDDATA_CHUNK_VERSION; 00289 static const uint32 TERRAINGENERALINFO_CHUNK_ID; 00290 static const uint16 TERRAINGENERALINFO_CHUNK_VERSION; 00291 00292 static const size_t LOD_MORPH_CUSTOM_PARAM; 00293 00294 typedef vector<Real>::type RealVector; 00295 00298 struct _OgreTerrainExport LayerInstance 00299 { 00301 Real worldSize; 00303 StringVector textureNames; 00304 00305 LayerInstance() 00306 : worldSize(100) {} 00307 }; 00308 typedef vector<LayerInstance>::type LayerInstanceList; 00309 00311 enum Alignment 00312 { 00314 ALIGN_X_Z = 0, 00316 ALIGN_X_Y = 1, 00318 ALIGN_Y_Z = 2 00319 }; 00320 00324 struct ImportData 00325 { 00327 Alignment terrainAlign; 00329 uint16 terrainSize; 00335 uint16 maxBatchSize; 00346 uint16 minBatchSize; 00347 00352 Vector3 pos; 00353 00355 Real worldSize; 00356 00362 Image* inputImage; 00363 00368 float* inputFloat; 00369 00373 float constantHeight; 00374 00382 bool deleteInputData; 00383 00385 Real inputScale; 00387 Real inputBias; 00388 00393 TerrainLayerDeclaration layerDeclaration; 00398 LayerInstanceList layerList; 00399 00400 ImportData() 00401 : terrainAlign(ALIGN_X_Z) 00402 , terrainSize(1025) 00403 , maxBatchSize(65) 00404 , minBatchSize(17) 00405 , pos(Vector3::ZERO) 00406 , worldSize(1000) 00407 , inputImage(0) 00408 , inputFloat(0) 00409 , constantHeight(0) 00410 , deleteInputData(false) 00411 , inputScale(1.0) 00412 , inputBias(0.0) 00413 { 00414 00415 } 00416 00417 ImportData(const ImportData& rhs) 00418 : terrainAlign(ALIGN_X_Z) 00419 , terrainSize(1025) 00420 , maxBatchSize(65) 00421 , minBatchSize(17) 00422 , pos(Vector3::ZERO) 00423 , worldSize(1000) 00424 , inputImage(0) 00425 , inputFloat(0) 00426 , constantHeight(0) 00427 , deleteInputData(false) 00428 , inputScale(1.0) 00429 , inputBias(0.0) 00430 { 00431 *this = rhs; 00432 } 00433 00434 ImportData& operator=(const ImportData& rhs) 00435 { 00436 // basic copy 00437 terrainAlign = rhs.terrainAlign; 00438 terrainSize = rhs.terrainSize; 00439 maxBatchSize = rhs.maxBatchSize; 00440 minBatchSize = rhs.minBatchSize; 00441 pos = rhs.pos; 00442 worldSize = rhs.worldSize; 00443 constantHeight = rhs.constantHeight; 00444 deleteInputData = rhs.deleteInputData; 00445 inputScale = rhs.inputScale; 00446 inputBias = rhs.inputBias; 00447 layerDeclaration = rhs.layerDeclaration; 00448 layerList = rhs.layerList; 00449 00450 // By-value copies in ownership cases 00451 if (rhs.deleteInputData) 00452 { 00453 if (rhs.inputImage) 00454 inputImage = OGRE_NEW Image(*rhs.inputImage); 00455 else 00456 inputImage = 0; 00457 00458 if (rhs.inputFloat) 00459 { 00460 inputFloat = OGRE_ALLOC_T(float, terrainSize*terrainSize, MEMCATEGORY_GEOMETRY); 00461 memcpy(inputFloat, rhs.inputFloat, sizeof(float) * terrainSize*terrainSize); 00462 } 00463 else 00464 inputFloat = 0; 00465 } 00466 else 00467 { 00468 // re-use pointers 00469 inputImage = rhs.inputImage; 00470 inputFloat = rhs.inputFloat; 00471 } 00472 return *this; 00473 } 00474 00476 void destroy() 00477 { 00478 if (deleteInputData) 00479 { 00480 OGRE_DELETE inputImage; 00481 OGRE_FREE(inputFloat, MEMCATEGORY_GEOMETRY); 00482 inputImage = 0; 00483 inputFloat = 0; 00484 } 00485 00486 } 00487 00488 ~ImportData() 00489 { 00490 destroy(); 00491 } 00492 00493 }; 00494 00496 enum NeighbourIndex 00497 { 00498 NEIGHBOUR_EAST = 0, 00499 NEIGHBOUR_NORTHEAST = 1, 00500 NEIGHBOUR_NORTH = 2, 00501 NEIGHBOUR_NORTHWEST = 3, 00502 NEIGHBOUR_WEST = 4, 00503 NEIGHBOUR_SOUTHWEST = 5, 00504 NEIGHBOUR_SOUTH = 6, 00505 NEIGHBOUR_SOUTHEAST = 7, 00506 00507 NEIGHBOUR_COUNT = 8 00508 }; 00509 00510 SceneManager* getSceneManager() const { return mSceneMgr; } 00511 00513 enum Space 00514 { 00516 WORLD_SPACE = 0, 00518 LOCAL_SPACE = 1, 00522 TERRAIN_SPACE = 2, 00526 POINT_SPACE = 3 00527 }; 00528 00533 class _OgreTerrainExport GpuBufferAllocator : public TerrainAlloc 00534 { 00535 public: 00536 GpuBufferAllocator() {} 00537 virtual ~GpuBufferAllocator() {} 00538 00544 virtual void allocateVertexBuffers(Terrain* forTerrain, size_t numVertices, HardwareVertexBufferSharedPtr& destPos, HardwareVertexBufferSharedPtr& destDelta) = 0; 00547 virtual void freeVertexBuffers(const HardwareVertexBufferSharedPtr& posbuf, const HardwareVertexBufferSharedPtr& deltabuf) = 0; 00548 00563 virtual HardwareIndexBufferSharedPtr getSharedIndexBuffer(uint16 batchSize, 00564 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 00565 uint16 skirtRowColSkip) = 0; 00566 00568 virtual void freeAllBuffers() = 0; 00569 00570 }; 00572 class _OgreTerrainExport DefaultGpuBufferAllocator : public GpuBufferAllocator 00573 { 00574 public: 00575 DefaultGpuBufferAllocator(); 00576 virtual ~DefaultGpuBufferAllocator(); 00577 void allocateVertexBuffers(Terrain* forTerrain, size_t numVertices, HardwareVertexBufferSharedPtr& destPos, HardwareVertexBufferSharedPtr& destDelta); 00578 void freeVertexBuffers(const HardwareVertexBufferSharedPtr& posbuf, const HardwareVertexBufferSharedPtr& deltabuf); 00579 HardwareIndexBufferSharedPtr getSharedIndexBuffer(uint16 batchSize, 00580 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 00581 uint16 skirtRowColSkip); 00582 void freeAllBuffers(); 00583 00587 void warmStart(size_t numInstances, uint16 terrainSize, uint16 maxBatchSize, 00588 uint16 minBatchSize); 00589 00590 protected: 00591 typedef list<HardwareVertexBufferSharedPtr>::type VBufList; 00592 VBufList mFreePosBufList; 00593 VBufList mFreeDeltaBufList; 00594 typedef map<uint32, HardwareIndexBufferSharedPtr>::type IBufMap; 00595 IBufMap mSharedIBufMap; 00596 00597 uint32 hashIndexBuffer(uint16 batchSize, 00598 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 00599 uint16 skirtRowColSkip); 00600 HardwareVertexBufferSharedPtr getVertexBuffer(VBufList& list, size_t vertexSize, size_t numVertices); 00601 00602 }; 00603 00608 void setGpuBufferAllocator(GpuBufferAllocator* alloc); 00609 00611 GpuBufferAllocator* getGpuBufferAllocator(); 00612 00614 static size_t _getNumIndexesForBatchSize(uint16 batchSize); 00626 static void _populateIndexBuffer(uint16* pIndexes, uint16 batchSize, 00627 uint16 vdatasize, size_t vertexIncrement, uint16 xoffset, uint16 yoffset, uint16 numSkirtRowsCols, 00628 uint16 skirtRowColSkip); 00629 00631 static uint16 _calcSkirtVertexIndex(uint16 mainIndex, uint16 vdatasize, bool isCol, 00632 uint16 numSkirtRowsCols, uint16 skirtRowColSkip); 00633 00640 void convertPosition(Space inSpace, const Vector3& inPos, Space outSpace, Vector3& outPos) const; 00647 Vector3 convertPosition(Space inSpace, const Vector3& inPos, Space outSpace) const; 00654 void convertDirection(Space inSpace, const Vector3& inDir, Space outSpace, Vector3& outDir) const; 00661 Vector3 convertDirection(Space inSpace, const Vector3& inDir, Space outSpace) const; 00662 00667 void setResourceGroup(const String& resGroup) { mResourceGroup = resGroup; } 00668 00672 const String& getResourceGroup() const { return mResourceGroup; } 00673 00676 const String& _getDerivedResourceGroup() const; 00677 00686 void save(const String& filename); 00692 void save(StreamSerialiser& stream); 00693 00700 bool prepare(const String& filename); 00707 bool prepare(DataStreamPtr& stream); 00714 bool prepare(StreamSerialiser& stream); 00715 00721 bool prepare(const ImportData& importData); 00722 00728 void load(const String& filename); 00729 00735 void load(StreamSerialiser& stream); 00736 00743 void load(int lodLevel = 0, bool synchronous = true); 00744 00750 bool isLoaded() const { return mIsLoaded; } 00751 00756 bool isModified() const { return mModified; } 00757 00758 00763 bool isHeightDataModified() const { return mHeightDataModified; } 00764 00765 00770 void unload(); 00771 00776 void unprepare(); 00777 00778 00788 float* getHeightData() const; 00789 00792 float* getHeightData(long x, long y) const; 00793 00798 float getHeightAtPoint(long x, long y) const; 00799 00806 void setHeightAtPoint(long x, long y, float h); 00807 00811 float getHeightAtTerrainPosition(Real x, Real y) const; 00812 00818 float getHeightAtWorldPosition(Real x, Real y, Real z) const; 00819 00825 float getHeightAtWorldPosition(const Vector3& pos) const; 00826 00833 const float* getDeltaData() const; 00834 00837 const float* getDeltaData(long x, long y) const; 00838 00843 void getPoint(long x, long y, Vector3* outpos) const; 00844 00850 void getPointFromSelfOrNeighbour(long x, long y, Vector3* outpos) const; 00851 00856 void getPoint(long x, long y, float height, Vector3* outpos) const; 00860 void getPointTransform(Matrix4* outXform) const; 00865 void getTerrainVector(const Vector3& inVec, Vector3* outVec) const; 00870 void getTerrainVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec) const; 00871 00876 void getTerrainVector(Real x, Real y, Real z, Vector3* outVec) const; 00881 void getTerrainVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec) const; 00882 00887 void getVector(const Vector3& inVec, Vector3* outVec) const; 00892 void getVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec) const; 00893 00898 void getVector(Real x, Real y, Real z, Vector3* outVec) const; 00903 void getVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec) const; 00904 00905 00913 void getPosition(const Vector3& TSpos, Vector3* outWSpos) const; 00921 void getPosition(Real x, Real y, Real z, Vector3* outWSpos) const; 00922 00929 void getTerrainPosition(const Vector3& WSpos, Vector3* outTSpos) const; 00936 void getTerrainPosition(Real x, Real y, Real z, Vector3* outTSpos) const; 00943 void getPositionAlign(const Vector3& TSpos, Alignment align, Vector3* outWSpos) const; 00950 void getPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outWSpos) const; 00951 00958 void getTerrainPositionAlign(const Vector3& WSpos, Alignment align, Vector3* outTSpos) const; 00965 void getTerrainPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outTSpos) const; 00966 00967 00969 Alignment getAlignment() const; 00971 uint16 getSize() const; 00976 void setSize(uint16 newSize); 00978 uint16 getMaxBatchSize() const; 00980 uint16 getMinBatchSize() const; 00982 Real getWorldSize() const; 00986 void setWorldSize(Real newWorldSize); 00987 00989 uint8 getLayerCount() const { return static_cast<uint8>(mLayers.size()); } 00990 00992 const TerrainLayerDeclaration& getLayerDeclaration() const { return mLayerDecl; } 00993 01000 void addLayer(Real worldSize = 0, const StringVector* textureNames = 0); 01001 01009 void addLayer(uint8 index, Real worldSize = 0, const StringVector* textureNames = 0); 01010 01013 void removeLayer(uint8 index); 01014 01024 void replaceLayer(uint8 index, bool keepBlends, Real worldSize = 0, const StringVector* textureNames = 0); 01025 01029 uint8 getMaxLayers() const; 01030 01035 Real getLayerWorldSize(uint8 index) const; 01041 void setLayerWorldSize(uint8 index, Real size); 01042 01051 Real getLayerUVMultiplier(uint8 index) const; 01052 01058 const String& getLayerTextureName(uint8 layerIndex, uint8 samplerIndex) const; 01065 void setLayerTextureName(uint8 layerIndex, uint8 samplerIndex, const String& textureName); 01066 01073 uint16 getLayerBlendMapSize() const { return mLayerBlendMapSize; } 01074 01080 uint16 getLightmapSize() const { return mLightmapSize; } 01081 01083 const TexturePtr& getLightmap() const { return mLightmap; } 01084 01090 uint16 getCompositeMapSize() const { return mCompositeMapSize; } 01091 01093 const TexturePtr& getCompositeMap() const { return mCompositeMap; } 01094 01096 const Vector3& getPosition() const { return mPos; } 01098 void setPosition(const Vector3& pos); 01100 SceneNode* _getRootSceneNode() const; 01107 void dirty(); 01108 01117 void dirtyRect(const Rect& rect); 01118 01124 void _dirtyCompositeMapRect(const Rect& rect); 01125 01136 void dirtyLightmapRect(const Rect& rect); 01137 01148 void dirtyLightmap(); 01149 01172 void update(bool synchronous = false); 01173 01178 void updateGeometry(); 01184 void updateGeometryWithoutNotifyNeighbours(); 01185 01186 // Used as a type mask for updateDerivedData 01187 static const uint8 DERIVED_DATA_DELTAS; 01188 static const uint8 DERIVED_DATA_NORMALS; 01189 static const uint8 DERIVED_DATA_LIGHTMAP; 01190 static const uint8 DERIVED_DATA_ALL; 01191 01203 void updateDerivedData(bool synchronous = false, uint8 typeMask = 0xFF); 01204 01213 void updateCompositeMap(); 01214 01228 void updateCompositeMapWithDelay(Real delay = 2); 01229 01230 01234 Real getSkirtSize() const { return mSkirtSize; } 01235 01237 uint16 getNumLodLevels() const { return mNumLodLevels; } 01238 01240 uint16 getNumLodLevelsPerLeaf() const { return mNumLodLevelsPerLeafNode; } 01241 01249 Rect calculateHeightDeltas(const Rect& rect); 01250 01258 void finaliseHeightDeltas(const Rect& rect, bool cpuData); 01259 01265 PixelBox* calculateNormals(const Rect& rect, Rect& outFinalRect); 01266 01274 void finaliseNormals(const Rect& rect, PixelBox* normalsBox); 01275 01283 PixelBox* calculateLightmap(const Rect& rect, const Rect& extraTargetRect, Rect& outFinalRect); 01284 01292 void finaliseLightmap(const Rect& rect, PixelBox* lightmapBox); 01293 01297 uint16 getResolutionAtLod(uint16 lodLevel) const; 01298 01310 std::pair<bool, Vector3> rayIntersects(const Ray& ray, 01311 bool cascadeToNeighbours = false, Real distanceLimit = 0); //const; 01312 01314 const AxisAlignedBox& getAABB() const; 01316 AxisAlignedBox getWorldAABB() const; 01318 Real getMinHeight() const; 01320 Real getMaxHeight() const; 01322 Real getBoundingRadius() const; 01323 01325 const MaterialPtr& getMaterial() const; 01327 const MaterialPtr& _getMaterial() const { return mMaterial; } 01329 const MaterialPtr& getCompositeMapMaterial() const; 01331 const MaterialPtr& _getCompositeMapMaterial() const { return mCompositeMapMaterial; } 01332 01334 const String& getMaterialName() const { return mMaterialName; } 01335 01337 void preFindVisibleObjects(SceneManager* source, 01338 SceneManager::IlluminationRenderStage irs, Viewport* v); 01340 void sceneManagerDestroyed(SceneManager* source); 01341 01343 uint8 getRenderQueueGroup(void) const { return mRenderQueueGroup; } 01347 void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; } 01348 01350 uint32 getVisibilityFlags(void) const { return mVisibilityFlags; } 01354 void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; } 01355 01357 uint32 getQueryFlags(void) const { return mQueryFlags; } 01361 void setQueryFlags(uint32 flags) { mQueryFlags = flags; } 01362 01364 void addQueryFlags(uint32 flags) { mQueryFlags |= flags; } 01365 01366 /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */ 01367 void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; } 01368 01369 01382 TerrainLayerBlendMap* getLayerBlendMap(uint8 layerIndex); 01383 01389 uint8 getBlendTextureIndex(uint8 layerIndex) const; 01390 01392 uint8 getBlendTextureCount() const; 01394 uint8 getBlendTextureCount(uint8 numLayers) const; 01395 01396 01401 const String& getBlendTextureName(uint8 textureIndex) const; 01402 01415 void setGlobalColourMapEnabled(bool enabled, uint16 size = 0); 01417 bool getGlobalColourMapEnabled() const { return mGlobalColourMapEnabled; } 01419 uint16 getGlobalColourMapSize() const { return mGlobalColourMapSize; } 01421 const TexturePtr& getGlobalColourMap() const { return mColourMap; } 01422 01428 void widenRectByVector(const Vector3& vec, const Rect& inRect, Rect& outRect); 01429 01437 void widenRectByVector(const Vector3& vec, const Rect& inRect, 01438 Real minHeight, Real maxHeight, Rect& outRect); 01439 01449 void freeTemporaryResources(); 01450 01455 const TexturePtr& getLayerBlendTexture(uint8 index) const; 01456 01463 std::pair<uint8,uint8> getLayerBlendTextureIndex(uint8 layerIndex) const; 01464 01475 void _setMorphRequired(bool morph) { mLodMorphRequired = morph; } 01477 bool _getMorphRequired() const { return mLodMorphRequired; } 01478 01490 void _setNormalMapRequired(bool normalMap); 01491 01505 void _setLightMapRequired(bool lightMap, bool shadowsOnly = false); 01506 01525 void _setCompositeMapRequired(bool compositeMap); 01526 01528 bool _getUseVertexCompression() const; 01529 01531 bool canHandleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ); 01533 WorkQueue::Response* handleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ); 01535 bool canHandleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ); 01537 void handleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ); 01539 void handleGenerateMaterialResponse(const WorkQueue::Response* res, const WorkQueue* srcQ); 01540 01541 static const uint16 WORKQUEUE_DERIVED_DATA_REQUEST; 01542 static const uint16 WORKQUEUE_GENERATE_MATERIAL_REQUEST; 01543 01545 uint16 getLODLevelWhenVertexEliminated(long x, long y) const; 01547 uint16 getLODLevelWhenVertexEliminated(long rowOrColulmn) const; 01548 01549 01551 TerrainQuadTreeNode* getQuadTree() { return mQuadTree; } 01552 01554 TexturePtr getTerrainNormalMap() const { return mTerrainNormalMap; } 01555 01563 Terrain* getNeighbour(NeighbourIndex index) const; 01564 01582 void setNeighbour(NeighbourIndex index, Terrain* neighbour, bool recalculate = false, bool notifyOther = true); 01583 01588 static NeighbourIndex getOppositeNeighbour(NeighbourIndex index); 01589 01592 static NeighbourIndex getNeighbourIndex(long offsetx, long offsety); 01593 01601 void notifyNeighbours(); 01602 01610 void neighbourModified(NeighbourIndex index, const Rect& edgerect, const Rect& shadowrect); 01611 01617 Terrain* raySelectNeighbour(const Ray& ray, Real distanceLimit = 0); 01618 01623 void _dumpTextures(const String& prefix, const String& suffix); 01624 01626 bool isDerivedDataUpdateInProgress() const { return mDerivedDataUpdateInProgress; } 01627 01628 01630 static void convertWorldToTerrainAxes(Alignment align, const Vector3& worldVec, Vector3* terrainVec); 01632 static void convertTerrainToWorldAxes(Alignment align, const Vector3& terrainVec, Vector3* worldVec); 01633 01635 static void writeLayerDeclaration(const TerrainLayerDeclaration& decl, StreamSerialiser& ser); 01637 static bool readLayerDeclaration(StreamSerialiser& ser, TerrainLayerDeclaration& targetdecl); 01639 static void writeLayerInstanceList(const Terrain::LayerInstanceList& lst, StreamSerialiser& ser); 01641 static bool readLayerInstanceList(StreamSerialiser& ser, size_t numSamplers, Terrain::LayerInstanceList& targetlst); 01642 01643 // This mutex is write-locked by neighbours if they are in the process of deleting themselves. 01644 // It should be read-locked whenever using neighbours in calculations which are possibly running in a 01645 // background thread. 01646 OGRE_RW_MUTEX(mNeighbourMutex); 01647 01648 protected: 01651 uint getGeoDataSizeAtLod(uint16 lodLevel) const; 01657 inline int getPositiveLodLevel( int lodLevel ) const 01658 { 01659 return (lodLevel>=0) ? lodLevel : mNumLodLevels+lodLevel; 01660 } 01661 void freeLodData(); 01662 01663 void freeCPUResources(); 01664 void freeGPUResources(); 01665 void determineLodLevels(); 01666 void distributeVertexData(); 01667 void updateBaseScale(); 01668 void createGPUBlendTextures(); 01669 void createLayerBlendMaps(); 01670 void createOrDestroyGPUNormalMap(); 01671 void createOrDestroyGPUColourMap(); 01672 void createOrDestroyGPULightmap(); 01673 void createOrDestroyGPUCompositeMap(); 01674 void waitForDerivedProcesses(); 01675 void convertSpace(Space inSpace, const Vector3& inVec, Space outSpace, Vector3& outVec, bool translation) const; 01676 Vector3 convertWorldToTerrainAxes(const Vector3& inVec) const; 01677 Vector3 convertTerrainToWorldAxes(const Vector3& inVec) const; 01681 void getPointAlign(long x, long y, Alignment align, Vector3* outpos) const; 01686 void getPointAlign(long x, long y, float height, Alignment align, Vector3* outpos) const; 01687 void calculateCurrentLod(Viewport* vp); 01689 std::pair<bool, Vector3> checkQuadIntersection(int x, int y, const Ray& ray); //const; 01690 01692 void deleteBlendMaps(uint8 lowIndex); 01694 void shiftUpGPUBlendChannels(uint8 index); 01696 void shiftDownGPUBlendChannels(uint8 index); 01698 void copyBlendTextureChannel(uint8 srcIndex, uint8 srcChannel, uint8 destIndex, uint8 destChannel ); 01700 void clearGPUBlendChannel(uint8 index, uint channel); 01701 01702 void copyGlobalOptions(); 01703 void checkLayers(bool includeGPUResources); 01704 void checkDeclaration(); 01705 void deriveUVMultipliers(); 01706 PixelFormat getBlendTextureFormat(uint8 textureIndex, uint8 numLayers) const; 01707 01708 void updateDerivedDataImpl(const Rect& rect, const Rect& lightmapExtraRect, bool synchronous, uint8 typeMask); 01709 01710 void getEdgeRect(NeighbourIndex index, long range, Rect* outRect) const; 01711 // get the equivalent of the passed in edge rectangle in neighbour 01712 void getNeighbourEdgeRect(NeighbourIndex index, const Rect& inRect, Rect* outRect) const; 01713 // get the equivalent of the passed in edge point in neighbour 01714 void getNeighbourPoint(NeighbourIndex index, long x, long y, long *outx, long *outy) const; 01715 // overflow a point into a neighbour index and point 01716 void getNeighbourPointOverflow(long x, long y, NeighbourIndex *outindex, long *outx, long *outy) const; 01717 01719 void removeFromNeighbours(); 01720 01721 uint16 mWorkQueueChannel; 01722 SceneManager* mSceneMgr; 01723 SceneNode* mRootNode; 01724 String mResourceGroup; 01725 bool mIsLoaded; 01726 bool mModified; 01727 bool mHeightDataModified; 01728 01730 float* mHeightData; 01732 float* mDeltaData; 01733 Alignment mAlign; 01734 Real mWorldSize; 01735 uint16 mSize; 01736 uint16 mMaxBatchSize; 01737 uint16 mMinBatchSize; 01738 Vector3 mPos; 01739 TerrainQuadTreeNode* mQuadTree; 01740 uint16 mNumLodLevels; 01741 uint16 mNumLodLevelsPerLeafNode; 01742 uint16 mTreeDepth; 01744 Real mBase; 01746 Real mScale; 01747 TerrainLayerDeclaration mLayerDecl; 01748 LayerInstanceList mLayers; 01749 RealVector mLayerUVMultiplier; 01750 01751 Real mSkirtSize; 01752 uint8 mRenderQueueGroup; 01753 uint32 mVisibilityFlags; 01754 uint32 mQueryFlags; 01755 01756 Rect mDirtyGeometryRect; 01757 Rect mDirtyDerivedDataRect; 01758 Rect mDirtyGeometryRectForNeighbours; 01759 Rect mDirtyLightmapFromNeighboursRect; 01760 bool mDerivedDataUpdateInProgress; 01762 uint8 mDerivedUpdatePendingMask; 01763 01764 bool mGenerateMaterialInProgress; 01766 mutable bool mPrepareInProgress; 01768 struct DerivedDataRequest 01769 { 01770 Terrain* terrain; 01771 // types requested 01772 uint8 typeMask; 01773 Rect dirtyRect; 01774 Rect lightmapExtraDirtyRect; 01775 _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const DerivedDataRequest& r) 01776 { return o; } 01777 }; 01778 01780 struct DerivedDataResponse 01781 { 01782 Terrain* terrain; 01784 uint8 remainingTypeMask; 01786 Rect deltaUpdateRect; 01788 Rect normalUpdateRect; 01790 Rect lightmapUpdateRect; 01792 PixelBox* normalMapBox; 01793 PixelBox* lightMapBox; 01794 _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const DerivedDataResponse& r) 01795 { return o; } 01796 }; 01797 01798 enum GenerateMaterialStage{ 01799 GEN_MATERIAL, 01800 GEN_COMPOSITE_MAP_MATERIAL 01801 }; 01803 struct GenerateMaterialRequest 01804 { 01805 Terrain* terrain; 01806 unsigned long startTime; 01807 GenerateMaterialStage stage; 01808 bool synchronous; 01809 _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const GenerateMaterialRequest& r) 01810 { return o; } 01811 }; 01812 01813 String mMaterialName; 01814 mutable MaterialPtr mMaterial; 01815 mutable TerrainMaterialGeneratorPtr mMaterialGenerator; 01816 mutable unsigned long long int mMaterialGenerationCount; 01817 mutable bool mMaterialDirty; 01818 mutable bool mMaterialParamsDirty; 01819 01820 uint16 mLayerBlendMapSize; 01821 uint16 mLayerBlendMapSizeActual; 01822 typedef vector<uint8*>::type BytePointerList; 01824 BytePointerList mCpuBlendMapStorage; 01825 typedef vector<TexturePtr>::type TexturePtrList; 01826 TexturePtrList mBlendTextureList; 01827 TerrainLayerBlendMapList mLayerBlendMapList; 01828 01829 uint16 mGlobalColourMapSize; 01830 bool mGlobalColourMapEnabled; 01831 TexturePtr mColourMap; 01832 uint8* mCpuColourMapStorage; 01833 01834 uint16 mLightmapSize; 01835 uint16 mLightmapSizeActual; 01836 TexturePtr mLightmap; 01837 uint8* mCpuLightmapStorage; 01838 01839 uint16 mCompositeMapSize; 01840 uint16 mCompositeMapSizeActual; 01841 TexturePtr mCompositeMap; 01842 uint8* mCpuCompositeMapStorage; 01843 Rect mCompositeMapDirtyRect; 01844 unsigned long mCompositeMapUpdateCountdown; 01845 unsigned long mLastMillis; 01847 bool mCompositeMapDirtyRectLightmapUpdate; 01848 mutable MaterialPtr mCompositeMapMaterial; 01849 01850 01851 static NameGenerator msBlendTextureGenerator; 01852 static NameGenerator msNormalMapNameGenerator; 01853 static NameGenerator msLightmapNameGenerator; 01854 static NameGenerator msCompositeMapNameGenerator; 01855 01856 bool mLodMorphRequired; 01857 bool mNormalMapRequired; 01858 bool mLightMapRequired; 01859 bool mLightMapShadowsOnly; 01860 bool mCompositeMapRequired; 01862 TexturePtr mTerrainNormalMap; 01863 01865 PixelBox* mCpuTerrainNormalMap; 01866 01867 const Camera* mLastLODCamera; 01868 unsigned long mLastLODFrame; 01869 int mLastViewportHeight; 01870 01871 Terrain* mNeighbours[NEIGHBOUR_COUNT]; 01872 01873 GpuBufferAllocator* mCustomGpuBufferAllocator; 01874 DefaultGpuBufferAllocator mDefaultGpuBufferAllocator; 01875 01876 size_t getPositionBufVertexSize() const; 01877 size_t getDeltaBufVertexSize() const; 01878 01879 TerrainLodManager* mLodManager; 01880 01881 public: 01885 void increaseLodLevel(bool synchronous = false); 01889 void decreaseLodLevel(); 01890 01891 int getHighestLodPrepared() const { return (mLodManager) ? mLodManager->getHighestLodPrepared() : -1; }; 01892 int getHighestLodLoaded() const { return (mLodManager) ? mLodManager->getHighestLodLoaded() : -1; }; 01893 int getTargetLodLevel() const { return (mLodManager) ? mLodManager->getTargetLodLevel() : -1; }; 01894 }; 01895 01896 01906 class _OgreTerrainExport TerrainGlobalOptions : public TerrainAlloc, public Singleton<TerrainGlobalOptions> 01907 { 01908 protected: 01909 01910 Real mSkirtSize; 01911 Vector3 mLightMapDir; 01912 bool mCastsShadows; 01913 Real mMaxPixelError; 01914 uint8 mRenderQueueGroup; 01915 uint32 mVisibilityFlags; 01916 uint32 mQueryFlags; 01917 bool mUseRayBoxDistanceCalculation; 01918 TerrainMaterialGeneratorPtr mDefaultMaterialGenerator; 01919 uint16 mLayerBlendMapSize; 01920 Real mDefaultLayerTextureWorldSize; 01921 uint16 mDefaultGlobalColourMapSize; 01922 uint16 mLightmapSize; 01923 uint16 mCompositeMapSize; 01924 ColourValue mCompositeMapAmbient; 01925 ColourValue mCompositeMapDiffuse; 01926 Real mCompositeMapDistance; 01927 String mResourceGroup; 01928 bool mUseVertexCompressionWhenAvailable; 01929 01930 public: 01931 TerrainGlobalOptions(); 01932 virtual ~TerrainGlobalOptions() {} 01933 01934 01938 Real getSkirtSize() const { return mSkirtSize; } 01944 void setSkirtSize(Real skirtSz) { mSkirtSize = skirtSz; } 01946 const Vector3& getLightMapDirection() const { return mLightMapDir; } 01948 void setLightMapDirection(const Vector3& v) { mLightMapDir = v; } 01950 const ColourValue& getCompositeMapAmbient() const { return mCompositeMapAmbient; } 01952 void setCompositeMapAmbient(const ColourValue& c) { mCompositeMapAmbient = c; } 01954 const ColourValue& getCompositeMapDiffuse() const { return mCompositeMapDiffuse; } 01956 void setCompositeMapDiffuse(const ColourValue& c) { mCompositeMapDiffuse = c; } 01958 Real getCompositeMapDistance() const { return mCompositeMapDistance; } 01960 void setCompositeMapDistance(Real c) { mCompositeMapDistance = c; } 01961 01962 01966 bool getCastsDynamicShadows() const { return mCastsShadows; } 01967 01973 void setCastsDynamicShadows(bool s) { mCastsShadows = s; } 01974 01976 Real getMaxPixelError() const { return mMaxPixelError; } 01977 01983 void setMaxPixelError(Real pixerr) { mMaxPixelError = pixerr; } 01984 01986 uint8 getRenderQueueGroup(void) const { return mRenderQueueGroup; } 01991 void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; } 01992 01994 uint32 getVisibilityFlags(void) const { return mVisibilityFlags; } 01999 void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; } 02000 02005 void setQueryFlags(uint32 flags) { mQueryFlags = flags; } 02008 uint32 getQueryFlags(void) const { return mQueryFlags; } 02009 02011 void addQueryFlags(uint32 flags) { mQueryFlags |= flags; } 02012 02013 /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */ 02014 void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; } 02015 02020 bool getUseRayBoxDistanceCalculation() const { return mUseRayBoxDistanceCalculation; } 02021 02033 void setUseRayBoxDistanceCalculation(bool rb) { mUseRayBoxDistanceCalculation = rb; } 02034 02037 TerrainMaterialGeneratorPtr getDefaultMaterialGenerator(); 02038 02041 void setDefaultMaterialGenerator(TerrainMaterialGeneratorPtr gen); 02042 02045 uint16 getLayerBlendMapSize() const { return mLayerBlendMapSize; } 02046 02051 void setLayerBlendMapSize(uint16 sz) { mLayerBlendMapSize = sz;} 02052 02055 Real getDefaultLayerTextureWorldSize() const { return mDefaultLayerTextureWorldSize; } 02056 02059 void setDefaultLayerTextureWorldSize(Real sz) { mDefaultLayerTextureWorldSize = sz; } 02060 02063 uint16 getDefaultGlobalColourMapSize() const { return mDefaultGlobalColourMapSize; } 02064 02068 void setDefaultGlobalColourMapSize(uint16 sz) { mDefaultGlobalColourMapSize = sz;} 02069 02070 02073 uint16 getLightMapSize() const { return mLightmapSize; } 02074 02077 void setLightMapSize(uint16 sz) { mLightmapSize = sz;} 02078 02081 uint16 getCompositeMapSize() const { return mCompositeMapSize; } 02082 02085 void setCompositeMapSize(uint16 sz) { mCompositeMapSize = sz;} 02086 02089 void setDefaultResourceGroup(const String& grp) { mResourceGroup = grp; } 02090 02093 const String& getDefaultResourceGroup() const { return mResourceGroup; } 02094 02098 bool getUseVertexCompressionWhenAvailable() const { return mUseVertexCompressionWhenAvailable; } 02099 02107 void setUseVertexCompressionWhenAvailable(bool enable) { mUseVertexCompressionWhenAvailable = enable; } 02108 02124 static TerrainGlobalOptions& getSingleton(void); 02140 static TerrainGlobalOptions* getSingletonPtr(void); 02141 02142 02143 }; 02144 02145 02148 } 02149 02150 02151 02152 02153 #endif
Copyright © 2012 Torus Knot Software Ltd

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:48