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-2012 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); 00845 void getPointTransform(Matrix4* outXform) const; 00850 void getTerrainVector(const Vector3& inVec, Vector3* outVec); 00855 void getTerrainVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec); 00856 00861 void getTerrainVector(Real x, Real y, Real z, Vector3* outVec); 00866 void getTerrainVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec); 00867 00872 void getVector(const Vector3& inVec, Vector3* outVec); 00877 void getVectorAlign(const Vector3& inVec, Alignment align, Vector3* outVec); 00878 00883 void getVector(Real x, Real y, Real z, Vector3* outVec); 00888 void getVectorAlign(Real x, Real y, Real z, Alignment align, Vector3* outVec); 00889 00890 00898 void getPosition(const Vector3& TSpos, Vector3* outWSpos); 00906 void getPosition(Real x, Real y, Real z, Vector3* outWSpos); 00907 00914 void getTerrainPosition(const Vector3& WSpos, Vector3* outTSpos); 00921 void getTerrainPosition(Real x, Real y, Real z, Vector3* outTSpos); 00928 void getPositionAlign(const Vector3& TSpos, Alignment align, Vector3* outWSpos); 00935 void getPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outWSpos); 00936 00943 void getTerrainPositionAlign(const Vector3& WSpos, Alignment align, Vector3* outTSpos); 00950 void getTerrainPositionAlign(Real x, Real y, Real z, Alignment align, Vector3* outTSpos); 00951 00952 00954 Alignment getAlignment() const; 00956 uint16 getSize() const; 00961 void setSize(uint16 newSize); 00963 uint16 getMaxBatchSize() const; 00965 uint16 getMinBatchSize() const; 00967 Real getWorldSize() const; 00971 void setWorldSize(Real newWorldSize); 00972 00974 uint8 getLayerCount() const { return static_cast<uint8>(mLayers.size()); } 00975 00977 const TerrainLayerDeclaration& getLayerDeclaration() const { return mLayerDecl; } 00978 00985 void addLayer(Real worldSize = 0, const StringVector* textureNames = 0); 00986 00994 void addLayer(uint8 index, Real worldSize = 0, const StringVector* textureNames = 0); 00995 00998 void removeLayer(uint8 index); 00999 01009 void replaceLayer(uint8 index, bool keepBlends, Real worldSize = 0, const StringVector* textureNames = 0); 01010 01014 uint8 getMaxLayers() const; 01015 01020 Real getLayerWorldSize(uint8 index) const; 01026 void setLayerWorldSize(uint8 index, Real size); 01027 01036 Real getLayerUVMultiplier(uint8 index) const; 01037 01043 const String& getLayerTextureName(uint8 layerIndex, uint8 samplerIndex) const; 01050 void setLayerTextureName(uint8 layerIndex, uint8 samplerIndex, const String& textureName); 01051 01058 uint16 getLayerBlendMapSize() const { return mLayerBlendMapSize; } 01059 01065 uint16 getLightmapSize() const { return mLightmapSize; } 01066 01068 const TexturePtr& getLightmap() const { return mLightmap; } 01069 01075 uint16 getCompositeMapSize() const { return mCompositeMapSize; } 01076 01078 const TexturePtr& getCompositeMap() const { return mCompositeMap; } 01079 01081 const Vector3& getPosition() const { return mPos; } 01083 void setPosition(const Vector3& pos); 01085 SceneNode* _getRootSceneNode() const; 01092 void dirty(); 01093 01102 void dirtyRect(const Rect& rect); 01103 01109 void _dirtyCompositeMapRect(const Rect& rect); 01110 01121 void dirtyLightmapRect(const Rect& rect); 01122 01133 void dirtyLightmap(); 01134 01157 void update(bool synchronous = false); 01158 01163 void updateGeometry(); 01164 01165 // Used as a type mask for updateDerivedData 01166 static const uint8 DERIVED_DATA_DELTAS; 01167 static const uint8 DERIVED_DATA_NORMALS; 01168 static const uint8 DERIVED_DATA_LIGHTMAP; 01169 static const uint8 DERIVED_DATA_ALL; 01170 01182 void updateDerivedData(bool synchronous = false, uint8 typeMask = 0xFF); 01183 01192 void updateCompositeMap(); 01193 01207 void updateCompositeMapWithDelay(Real delay = 2); 01208 01209 01213 Real getSkirtSize() const { return mSkirtSize; } 01214 01216 uint16 getNumLodLevels() const { return mNumLodLevels; } 01217 01219 uint16 getNumLodLevelsPerLeaf() const { return mNumLodLevelsPerLeafNode; } 01220 01228 Rect calculateHeightDeltas(const Rect& rect); 01229 01237 void finaliseHeightDeltas(const Rect& rect, bool cpuData); 01238 01244 PixelBox* calculateNormals(const Rect& rect, Rect& outFinalRect); 01245 01253 void finaliseNormals(const Rect& rect, PixelBox* normalsBox); 01254 01262 PixelBox* calculateLightmap(const Rect& rect, const Rect& extraTargetRect, Rect& outFinalRect); 01263 01271 void finaliseLightmap(const Rect& rect, PixelBox* lightmapBox); 01272 01276 uint16 getResolutionAtLod(uint16 lodLevel); 01277 01289 std::pair<bool, Vector3> rayIntersects(const Ray& ray, 01290 bool cascadeToNeighbours = false, Real distanceLimit = 0); //const; 01291 01293 const AxisAlignedBox& getAABB() const; 01295 AxisAlignedBox getWorldAABB() const; 01297 Real getMinHeight() const; 01299 Real getMaxHeight() const; 01301 Real getBoundingRadius() const; 01302 01304 const MaterialPtr& getMaterial() const; 01306 const MaterialPtr& _getMaterial() const { return mMaterial; } 01308 const MaterialPtr& getCompositeMapMaterial() const; 01310 const MaterialPtr& _getCompositeMapMaterial() const { return mCompositeMapMaterial; } 01311 01313 const String& getMaterialName() const { return mMaterialName; } 01314 01316 void preFindVisibleObjects(SceneManager* source, 01317 SceneManager::IlluminationRenderStage irs, Viewport* v); 01319 void sceneManagerDestroyed(SceneManager* source); 01320 01322 uint8 getRenderQueueGroup(void) const { return mRenderQueueGroup; } 01326 void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; } 01327 01329 uint32 getVisibilityFlags(void) const { return mVisibilityFlags; } 01333 void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; } 01334 01336 uint32 getQueryFlags(void) const { return mQueryFlags; } 01340 void setQueryFlags(uint32 flags) { mQueryFlags = flags; } 01341 01343 void addQueryFlags(uint32 flags) { mQueryFlags |= flags; } 01344 01345 /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */ 01346 void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; } 01347 01348 01361 TerrainLayerBlendMap* getLayerBlendMap(uint8 layerIndex); 01362 01368 uint8 getBlendTextureIndex(uint8 layerIndex) const; 01369 01371 uint8 getBlendTextureCount() const; 01373 uint8 getBlendTextureCount(uint8 numLayers) const; 01374 01375 01380 const String& getBlendTextureName(uint8 textureIndex) const; 01381 01394 void setGlobalColourMapEnabled(bool enabled, uint16 size = 0); 01396 bool getGlobalColourMapEnabled() const { return mGlobalColourMapEnabled; } 01398 uint16 getGlobalColourMapSize() const { return mGlobalColourMapSize; } 01400 const TexturePtr& getGlobalColourMap() const { return mColourMap; } 01401 01407 void widenRectByVector(const Vector3& vec, const Rect& inRect, Rect& outRect); 01408 01416 void widenRectByVector(const Vector3& vec, const Rect& inRect, 01417 Real minHeight, Real maxHeight, Rect& outRect); 01418 01428 void freeTemporaryResources(); 01429 01434 const TexturePtr& getLayerBlendTexture(uint8 index); 01435 01442 std::pair<uint8,uint8> getLayerBlendTextureIndex(uint8 layerIndex); 01443 01454 void _setMorphRequired(bool morph) { mLodMorphRequired = morph; } 01456 bool _getMorphRequired() const { return mLodMorphRequired; } 01457 01469 void _setNormalMapRequired(bool normalMap); 01470 01484 void _setLightMapRequired(bool lightMap, bool shadowsOnly = false); 01485 01504 void _setCompositeMapRequired(bool compositeMap); 01505 01507 bool _getUseVertexCompression() const; 01508 01510 bool canHandleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ); 01512 WorkQueue::Response* handleRequest(const WorkQueue::Request* req, const WorkQueue* srcQ); 01514 bool canHandleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ); 01516 void handleResponse(const WorkQueue::Response* res, const WorkQueue* srcQ); 01517 01518 static const uint16 WORKQUEUE_DERIVED_DATA_REQUEST; 01519 01520 01522 uint16 getLODLevelWhenVertexEliminated(long x, long y); 01524 uint16 getLODLevelWhenVertexEliminated(long rowOrColulmn); 01525 01526 01528 TerrainQuadTreeNode* getQuadTree() { return mQuadTree; } 01529 01531 TexturePtr getTerrainNormalMap() const { return mTerrainNormalMap; } 01532 01540 Terrain* getNeighbour(NeighbourIndex index); 01541 01559 void setNeighbour(NeighbourIndex index, Terrain* neighbour, bool recalculate = false, bool notifyOther = true); 01560 01565 static NeighbourIndex getOppositeNeighbour(NeighbourIndex index); 01566 01569 static NeighbourIndex getNeighbourIndex(long offsetx, long offsety); 01570 01578 void notifyNeighbours(); 01579 01587 void neighbourModified(NeighbourIndex index, const Rect& edgerect, const Rect& shadowrect); 01588 01594 Terrain* raySelectNeighbour(const Ray& ray, Real distanceLimit = 0); 01595 01600 void _dumpTextures(const String& prefix, const String& suffix); 01601 01603 bool isDerivedDataUpdateInProgress() const { return mDerivedDataUpdateInProgress; } 01604 01605 01607 static void convertWorldToTerrainAxes(Alignment align, const Vector3& worldVec, Vector3* terrainVec); 01609 static void convertTerrainToWorldAxes(Alignment align, const Vector3& terrainVec, Vector3* worldVec); 01610 01612 static void writeLayerDeclaration(const TerrainLayerDeclaration& decl, StreamSerialiser& ser); 01614 static bool readLayerDeclaration(StreamSerialiser& ser, TerrainLayerDeclaration& targetdecl); 01616 static void writeLayerInstanceList(const Terrain::LayerInstanceList& lst, StreamSerialiser& ser); 01618 static bool readLayerInstanceList(StreamSerialiser& ser, size_t numSamplers, Terrain::LayerInstanceList& targetlst); 01619 protected: 01620 01621 void freeCPUResources(); 01622 void freeGPUResources(); 01623 void determineLodLevels(); 01624 void distributeVertexData(); 01625 void updateBaseScale(); 01626 void createGPUBlendTextures(); 01627 void createLayerBlendMaps(); 01628 void createOrDestroyGPUNormalMap(); 01629 void createOrDestroyGPUColourMap(); 01630 void createOrDestroyGPULightmap(); 01631 void createOrDestroyGPUCompositeMap(); 01632 void waitForDerivedProcesses(); 01633 void convertSpace(Space inSpace, const Vector3& inVec, Space outSpace, Vector3& outVec, bool translation) const; 01634 Vector3 convertWorldToTerrainAxes(const Vector3& inVec) const; 01635 Vector3 convertTerrainToWorldAxes(const Vector3& inVec) const; 01639 void getPointAlign(long x, long y, Alignment align, Vector3* outpos); 01644 void getPointAlign(long x, long y, float height, Alignment align, Vector3* outpos); 01645 void calculateCurrentLod(Viewport* vp); 01647 std::pair<bool, Vector3> checkQuadIntersection(int x, int y, const Ray& ray); //const; 01648 01650 void deleteBlendMaps(uint8 lowIndex); 01652 void shiftUpGPUBlendChannels(uint8 index); 01654 void shiftDownGPUBlendChannels(uint8 index); 01656 void copyBlendTextureChannel(uint8 srcIndex, uint8 srcChannel, uint8 destIndex, uint8 destChannel ); 01658 void clearGPUBlendChannel(uint8 index, uint channel); 01659 01660 void copyGlobalOptions(); 01661 void checkLayers(bool includeGPUResources); 01662 void checkDeclaration(); 01663 void deriveUVMultipliers(); 01664 PixelFormat getBlendTextureFormat(uint8 textureIndex, uint8 numLayers); 01665 01666 void updateDerivedDataImpl(const Rect& rect, const Rect& lightmapExtraRect, bool synchronous, uint8 typeMask); 01667 01668 void getEdgeRect(NeighbourIndex index, long range, Rect* outRect); 01669 // get the equivalent of the passed in edge rectangle in neighbour 01670 void getNeighbourEdgeRect(NeighbourIndex index, const Rect& inRect, Rect* outRect); 01671 // get the equivalent of the passed in edge point in neighbour 01672 void getNeighbourPoint(NeighbourIndex index, long x, long y, long *outx, long *outy); 01673 // overflow a point into a neighbour index and point 01674 void getNeighbourPointOverflow(long x, long y, NeighbourIndex *outindex, long *outx, long *outy); 01675 01676 01677 01678 uint16 mWorkQueueChannel; 01679 SceneManager* mSceneMgr; 01680 SceneNode* mRootNode; 01681 String mResourceGroup; 01682 bool mIsLoaded; 01683 bool mModified; 01684 bool mHeightDataModified; 01685 01687 float* mHeightData; 01689 float* mDeltaData; 01690 Alignment mAlign; 01691 Real mWorldSize; 01692 uint16 mSize; 01693 uint16 mMaxBatchSize; 01694 uint16 mMinBatchSize; 01695 Vector3 mPos; 01696 TerrainQuadTreeNode* mQuadTree; 01697 uint16 mNumLodLevels; 01698 uint16 mNumLodLevelsPerLeafNode; 01699 uint16 mTreeDepth; 01701 Real mBase; 01703 Real mScale; 01704 TerrainLayerDeclaration mLayerDecl; 01705 LayerInstanceList mLayers; 01706 RealVector mLayerUVMultiplier; 01707 01708 Real mSkirtSize; 01709 uint8 mRenderQueueGroup; 01710 uint32 mVisibilityFlags; 01711 uint32 mQueryFlags; 01712 01713 Rect mDirtyGeometryRect; 01714 Rect mDirtyDerivedDataRect; 01715 Rect mDirtyGeometryRectForNeighbours; 01716 Rect mDirtyLightmapFromNeighboursRect; 01717 bool mDerivedDataUpdateInProgress; 01718 uint8 mDerivedUpdatePendingMask; // if another update is requested while one is already running 01719 01721 struct DerivedDataRequest 01722 { 01723 Terrain* terrain; 01724 // types requested 01725 uint8 typeMask; 01726 Rect dirtyRect; 01727 Rect lightmapExtraDirtyRect; 01728 _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const DerivedDataRequest& r) 01729 { return o; } 01730 }; 01731 01733 struct DerivedDataResponse 01734 { 01735 Terrain* terrain; 01736 // remaining types not yet processed 01737 uint8 remainingTypeMask; 01738 // The area of deltas that was updated 01739 Rect deltaUpdateRect; 01740 // the area of normals that was updated 01741 Rect normalUpdateRect; 01742 // the area of lightmap that was updated 01743 Rect lightmapUpdateRect; 01744 // all CPU-side data, independent of textures; to be blitted in main thread 01745 PixelBox* normalMapBox; 01746 PixelBox* lightMapBox; 01747 _OgreTerrainExport friend std::ostream& operator<<(std::ostream& o, const DerivedDataResponse& r) 01748 { return o; } 01749 }; 01750 01751 String mMaterialName; 01752 mutable MaterialPtr mMaterial; 01753 mutable TerrainMaterialGeneratorPtr mMaterialGenerator; 01754 mutable unsigned long long int mMaterialGenerationCount; 01755 mutable bool mMaterialDirty; 01756 mutable bool mMaterialParamsDirty; 01757 01758 uint16 mLayerBlendMapSize; 01759 uint16 mLayerBlendMapSizeActual; 01760 typedef vector<uint8*>::type BytePointerList; 01762 BytePointerList mCpuBlendMapStorage; 01763 typedef vector<TexturePtr>::type TexturePtrList; 01764 TexturePtrList mBlendTextureList; 01765 TerrainLayerBlendMapList mLayerBlendMapList; 01766 01767 uint16 mGlobalColourMapSize; 01768 bool mGlobalColourMapEnabled; 01769 TexturePtr mColourMap; 01770 uint8* mCpuColourMapStorage; 01771 01772 uint16 mLightmapSize; 01773 uint16 mLightmapSizeActual; 01774 TexturePtr mLightmap; 01775 uint8* mCpuLightmapStorage; 01776 01777 uint16 mCompositeMapSize; 01778 uint16 mCompositeMapSizeActual; 01779 TexturePtr mCompositeMap; 01780 uint8* mCpuCompositeMapStorage; 01781 Rect mCompositeMapDirtyRect; 01782 unsigned long mCompositeMapUpdateCountdown; 01783 unsigned long mLastMillis; 01785 bool mCompositeMapDirtyRectLightmapUpdate; 01786 mutable MaterialPtr mCompositeMapMaterial; 01787 01788 01789 static NameGenerator msBlendTextureGenerator; 01790 static NameGenerator msNormalMapNameGenerator; 01791 static NameGenerator msLightmapNameGenerator; 01792 static NameGenerator msCompositeMapNameGenerator; 01793 01794 bool mLodMorphRequired; 01795 bool mNormalMapRequired; 01796 bool mLightMapRequired; 01797 bool mLightMapShadowsOnly; 01798 bool mCompositeMapRequired; 01800 TexturePtr mTerrainNormalMap; 01801 01803 PixelBox* mCpuTerrainNormalMap; 01804 01805 const Camera* mLastLODCamera; 01806 unsigned long mLastLODFrame; 01807 int mLastViewportHeight; 01808 01809 Terrain* mNeighbours[NEIGHBOUR_COUNT]; 01810 01811 GpuBufferAllocator* mCustomGpuBufferAllocator; 01812 DefaultGpuBufferAllocator mDefaultGpuBufferAllocator; 01813 01814 size_t getPositionBufVertexSize() const; 01815 size_t getDeltaBufVertexSize() const; 01816 01817 }; 01818 01819 01829 class _OgreTerrainExport TerrainGlobalOptions : public TerrainAlloc, public Singleton<TerrainGlobalOptions> 01830 { 01831 protected: 01832 01833 Real mSkirtSize; 01834 Vector3 mLightMapDir; 01835 bool mCastsShadows; 01836 Real mMaxPixelError; 01837 uint8 mRenderQueueGroup; 01838 uint32 mVisibilityFlags; 01839 uint32 mQueryFlags; 01840 bool mUseRayBoxDistanceCalculation; 01841 TerrainMaterialGeneratorPtr mDefaultMaterialGenerator; 01842 uint16 mLayerBlendMapSize; 01843 Real mDefaultLayerTextureWorldSize; 01844 uint16 mDefaultGlobalColourMapSize; 01845 uint16 mLightmapSize; 01846 uint16 mCompositeMapSize; 01847 ColourValue mCompositeMapAmbient; 01848 ColourValue mCompositeMapDiffuse; 01849 Real mCompositeMapDistance; 01850 String mResourceGroup; 01851 bool mUseVertexCompressionWhenAvailable; 01852 01853 public: 01854 TerrainGlobalOptions(); 01855 virtual ~TerrainGlobalOptions() {} 01856 01857 01861 Real getSkirtSize() { return mSkirtSize; } 01867 void setSkirtSize(Real skirtSz) { mSkirtSize = skirtSz; } 01869 const Vector3& getLightMapDirection() { return mLightMapDir; } 01871 void setLightMapDirection(const Vector3& v) { mLightMapDir = v; } 01873 const ColourValue& getCompositeMapAmbient() { return mCompositeMapAmbient; } 01875 void setCompositeMapAmbient(const ColourValue& c) { mCompositeMapAmbient = c; } 01877 const ColourValue& getCompositeMapDiffuse() { return mCompositeMapDiffuse; } 01879 void setCompositeMapDiffuse(const ColourValue& c) { mCompositeMapDiffuse = c; } 01881 Real getCompositeMapDistance() { return mCompositeMapDistance; } 01883 void setCompositeMapDistance(Real c) { mCompositeMapDistance = c; } 01884 01885 01889 bool getCastsDynamicShadows() { return mCastsShadows; } 01890 01896 void setCastsDynamicShadows(bool s) { mCastsShadows = s; } 01897 01899 Real getMaxPixelError() { return mMaxPixelError; } 01900 01906 void setMaxPixelError(Real pixerr) { mMaxPixelError = pixerr; } 01907 01909 uint8 getRenderQueueGroup(void) { return mRenderQueueGroup; } 01914 void setRenderQueueGroup(uint8 grp) { mRenderQueueGroup = grp; } 01915 01917 uint32 getVisibilityFlags(void) { return mVisibilityFlags; } 01922 void setVisibilityFlags(uint32 flags) { mVisibilityFlags = flags; } 01923 01928 void setQueryFlags(uint32 flags) { mQueryFlags = flags; } 01931 uint32 getQueryFlags(void) { return mQueryFlags; } 01932 01934 void addQueryFlags(uint32 flags) { mQueryFlags |= flags; } 01935 01936 /* As setQueryFlags, except the flags passed as parameters are removed from the existing flags on this object. */ 01937 void removeQueryFlags(uint32 flags) { mQueryFlags &= ~flags; } 01938 01943 bool getUseRayBoxDistanceCalculation() { return mUseRayBoxDistanceCalculation; } 01944 01956 void setUseRayBoxDistanceCalculation(bool rb) { mUseRayBoxDistanceCalculation = rb; } 01957 01960 TerrainMaterialGeneratorPtr getDefaultMaterialGenerator(); 01961 01964 void setDefaultMaterialGenerator(TerrainMaterialGeneratorPtr gen); 01965 01968 uint16 getLayerBlendMapSize() { return mLayerBlendMapSize; } 01969 01974 void setLayerBlendMapSize(uint16 sz) { mLayerBlendMapSize = sz;} 01975 01978 Real getDefaultLayerTextureWorldSize() { return mDefaultLayerTextureWorldSize; } 01979 01982 void setDefaultLayerTextureWorldSize(Real sz) { mDefaultLayerTextureWorldSize = sz; } 01983 01986 uint16 getDefaultGlobalColourMapSize() { return mDefaultGlobalColourMapSize; } 01987 01991 void setDefaultGlobalColourMapSize(uint16 sz) { mDefaultGlobalColourMapSize = sz;} 01992 01993 01996 uint16 getLightMapSize() { return mLightmapSize; } 01997 02000 void setLightMapSize(uint16 sz) { mLightmapSize = sz;} 02001 02004 uint16 getCompositeMapSize() { return mCompositeMapSize; } 02005 02008 void setCompositeMapSize(uint16 sz) { mCompositeMapSize = sz;} 02009 02012 void setDefaultResourceGroup(const String& grp) { mResourceGroup = grp; } 02013 02016 const String& getDefaultResourceGroup() { return mResourceGroup; } 02017 02021 bool getUseVertexCompressionWhenAvailable() const { return mUseVertexCompressionWhenAvailable; } 02022 02030 void setUseVertexCompressionWhenAvailable(bool enable) { mUseVertexCompressionWhenAvailable = enable; } 02031 02047 static TerrainGlobalOptions& getSingleton(void); 02063 static TerrainGlobalOptions* getSingletonPtr(void); 02064 02065 02066 }; 02067 02068 02071 } 02072 02073 02074 02075 02076 #endif
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:28 2012