terrData.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _TERRDATA_H_
  27. #define _TERRDATA_H_
  28. #ifndef _MPOINT3_H_
  29. #include "math/mPoint3.h"
  30. #endif
  31. #ifndef _SCENEOBJECT_H_
  32. #include "scene/sceneObject.h"
  33. #endif
  34. #ifndef __RESOURCE_H__
  35. #include "core/resource.h"
  36. #endif
  37. #ifndef _RENDERPASSMANAGER_H_
  38. #include "renderInstance/renderPassManager.h"
  39. #endif
  40. #ifndef _TSIGNAL_H_
  41. #include "core/util/tSignal.h"
  42. #endif
  43. #ifndef _TERRFILE_H_
  44. #include "terrain/terrFile.h"
  45. #endif
  46. #ifndef _GFXPRIMITIVEBUFFER_H_
  47. #include "gfx/gfxPrimitiveBuffer.h"
  48. #endif
  49. #ifndef _ASSET_PTR_H_
  50. #include "assets/assetPtr.h"
  51. #endif
  52. #ifndef TERRAINASSET_H
  53. #include "T3D/assets/TerrainAsset.h"
  54. #endif
  55. #ifndef _CONVEX_H_
  56. #include "collision/convex.h"
  57. #endif
  58. class GBitmap;
  59. class TerrainBlock;
  60. class TerrCell;
  61. class PhysicsBody;
  62. class TerrainCellMaterial;
  63. class TerrainBlock : public SceneObject
  64. {
  65. typedef SceneObject Parent;
  66. friend class TerrainEditor;
  67. friend class TerrainCellMaterial;
  68. protected:
  69. enum
  70. {
  71. TransformMask = Parent::NextFreeMask,
  72. FileMask = Parent::NextFreeMask << 1,
  73. SizeMask = Parent::NextFreeMask << 2,
  74. MaterialMask = Parent::NextFreeMask << 3,
  75. HeightMapChangeMask = Parent::NextFreeMask << 4,
  76. MiscMask = Parent::NextFreeMask << 5,
  77. NextFreeMask = Parent::NextFreeMask << 6,
  78. };
  79. public:
  80. enum BaseTexFormat
  81. {
  82. NONE, DDS, PNG
  83. };
  84. static const char* formatToExtension(BaseTexFormat format)
  85. {
  86. switch (format)
  87. {
  88. case DDS:
  89. return "dds";
  90. case PNG:
  91. return "png";
  92. default:
  93. return "";
  94. }
  95. };
  96. protected:
  97. Box3F mBounds;
  98. ///
  99. GBitmap *mLightMap;
  100. /// The lightmap dimensions in pixels.
  101. U32 mLightMapSize;
  102. /// The lightmap texture.
  103. GFXTexHandle mLightMapTex;
  104. /// The terrain data file.
  105. Resource<TerrainFile> mFile;
  106. /// The TerrainFile CRC sent from the server.
  107. U32 mCRC;
  108. ///
  109. StringTableEntry mTerrFileName;
  110. AssetPtr<TerrainAsset> mTerrainAsset;
  111. StringTableEntry mTerrainAssetId;
  112. /// The maximum detail distance found in the material list.
  113. F32 mMaxDetailDistance;
  114. ///
  115. Vector<GFXTexHandle> mBaseTextures;
  116. GFXTextureArrayHandle mDetailTextureArray;
  117. GFXTextureArrayHandle mMacroTextureArray;
  118. GFXTextureArrayHandle mNormalTextureArray;
  119. GFXTextureArrayHandle mOrmTextureArray;
  120. ///
  121. GFXTexHandle mLayerTex;
  122. /// The shader used to generate the base texture map.
  123. GFXShaderRef mBaseShader;
  124. ///
  125. GFXStateBlockRef mBaseShaderSB;
  126. ///
  127. GFXShaderConstBufferRef mBaseShaderConsts;
  128. ///
  129. GFXShaderConstHandle *mBaseTexScaleConst;
  130. GFXShaderConstHandle *mBaseTexIdConst;
  131. GFXShaderConstHandle *mBaseLayerSizeConst;
  132. ///
  133. GFXTextureTargetRef mBaseTarget;
  134. /// The base texture.
  135. GFXTexHandle mBaseTex;
  136. ///
  137. bool mDetailsDirty;
  138. ///
  139. bool mLayerTexDirty;
  140. /// The desired size for the base texture.
  141. U32 mBaseTexSize;
  142. BaseTexFormat mBaseTexFormat;
  143. ///
  144. TerrCell *mCell;
  145. /// The shared base material which is used to render
  146. /// cells that are outside the detail map range.
  147. TerrainCellMaterial *mBaseMaterial;
  148. /// A dummy material only used for shadow
  149. /// material generation.
  150. BaseMatInstance *mDefaultMatInst;
  151. F32 mSquareSize;
  152. PhysicsBody *mPhysicsRep;
  153. U32 mScreenError;
  154. /// The shared primitive buffer used in rendering.
  155. GFXPrimitiveBufferHandle mPrimBuffer;
  156. /// The cells used in the last render pass
  157. /// when doing debug rendering.
  158. /// @see _renderDebug
  159. Vector<TerrCell*> mDebugCells;
  160. /// Set to enable debug rendering of the terrain. It
  161. /// is exposed to the console via $terrain::debugRender.
  162. static bool smDebugRender;
  163. /// Allows the terrain to cast shadows onto itself and other objects.
  164. bool mCastShadows;
  165. /// A global LOD scale used to tweak the default
  166. /// terrain screen error value.
  167. static F32 smLODScale;
  168. /// A global detail scale used to tweak the
  169. /// material detail distances.
  170. static F32 smDetailScale;
  171. /// True if the zoning needs to be recalculated for the terrain.
  172. bool mZoningDirty;
  173. /// Holds the generated convex list stuff for this terrain
  174. Convex mTerrainConvexList;
  175. String _getBaseTexCacheFileName() const;
  176. void _rebuildQuadtree();
  177. void _updatePhysics();
  178. void _renderBlock( SceneRenderState *state );
  179. void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  180. /// The callback used to get texture events.
  181. /// @see GFXTextureManager::addEventDelegate
  182. void _onTextureEvent( GFXTexCallbackCode code );
  183. /// Used to release terrain materials when
  184. /// the material manager flushes them.
  185. /// @see MaterialManager::getFlushSignal
  186. void _onFlushMaterials();
  187. ///
  188. bool _initBaseShader();
  189. ///
  190. void _updateMaterials();
  191. ///
  192. void _updateBaseTexture( bool writeToCache );
  193. void _updateLayerTexture();
  194. void _updateBounds();
  195. void _onZoningChanged( SceneZoneSpaceManager *zoneManager );
  196. void _updateZoning();
  197. // Protected fields
  198. static bool _setTerrainFile( void *obj, const char *index, const char *data );
  199. static bool _setTerrainAsset(void* obj, const char* index, const char* data);
  200. static bool _setSquareSize( void *obj, const char *index, const char *data );
  201. static bool _setBaseTexSize(void *obj, const char *index, const char *data);
  202. static bool _setBaseTexFormat(void *obj, const char *index, const char *data);
  203. static bool _setLightMapSize( void *obj, const char *index, const char *data );
  204. public:
  205. enum
  206. {
  207. LightmapUpdate = BIT(0),
  208. HeightmapUpdate = BIT(1),
  209. LayersUpdate = BIT(2),
  210. EmptyUpdate = BIT(3)
  211. };
  212. static Signal<void(U32,TerrainBlock*,const Point2I& ,const Point2I&)> smUpdateSignal;
  213. ///
  214. bool import( const GBitmap &heightMap,
  215. F32 heightScale,
  216. F32 metersPerPixel,
  217. const Vector<U8> &layerMap,
  218. const Vector<String> &materials,
  219. bool flipYAxis = true );
  220. #ifdef TORQUE_TOOLS
  221. bool exportHeightMap( const UTF8 *filePath, const String &format ) const;
  222. bool exportLayerMaps( const UTF8 *filePrefix, const String &format ) const;
  223. #endif
  224. public:
  225. TerrainBlock();
  226. virtual ~TerrainBlock();
  227. U32 getCRC() const { return(mCRC); }
  228. Resource<TerrainFile> getFile() const { return mFile; };
  229. bool onAdd();
  230. void onRemove();
  231. void onEditorEnable();
  232. void onEditorDisable();
  233. /// Adds a new material as the top layer or
  234. /// inserts it at the specified index.
  235. void addMaterial( const String &name, U32 insertAt = -1 );
  236. /// Removes the material at the index.
  237. void removeMaterial( U32 index );
  238. /// Updates the material at the index.
  239. void updateMaterial( U32 index, const String &name );
  240. /// Deletes all the materials on the terrain.
  241. void deleteAllMaterials();
  242. void setMaterialsDirty() { mDetailsDirty = true; };
  243. //void setMaterialName( U32 index, const String &name );
  244. /// Accessors and mutators for TerrainMaterialUndoAction.
  245. /// @{
  246. const Vector<TerrainMaterial*>& getMaterials() const { return mFile->mMaterials; }
  247. const Vector<U8>& getLayerMap() const { return mFile->mLayerMap; }
  248. void setMaterials( const Vector<TerrainMaterial*> &materials ) { mFile->mMaterials = materials; }
  249. void setLayerMap( const Vector<U8> &layers ) { mFile->mLayerMap = layers; }
  250. /// @}
  251. TerrainMaterial* getMaterial( U32 index ) const;
  252. const char* getMaterialName( U32 index ) const;
  253. U32 getMaterialCount() const;
  254. GFXTextureArrayHandle getDetailTextureArray() const { return mDetailTextureArray; }
  255. GFXTextureArrayHandle getMacroTextureArray() const { return mMacroTextureArray; }
  256. GFXTextureArrayHandle getNormalTextureArray() const { return mNormalTextureArray; }
  257. GFXTextureArrayHandle getOrmTextureArray() const { return mOrmTextureArray; }
  258. //BaseMatInstance* getMaterialInst( U32 x, U32 y );
  259. void setHeight( const Point2I &pos, F32 height );
  260. F32 getHeight( const Point2I &pos );
  261. // Performs an update to the selected range of the terrain
  262. // grid including the collision and rendering structures.
  263. void updateGrid( const Point2I &minPt,
  264. const Point2I &maxPt,
  265. bool updateClient = false );
  266. void updateGridMaterials( const Point2I &minPt, const Point2I &maxPt );
  267. Point2I getGridPos( const Point3F &worldPos ) const;
  268. /// This returns true and the terrain z height for
  269. /// a 2d position in the terrains object space.
  270. ///
  271. /// If the terrain at that point is within an empty block
  272. /// or the 2d position is outside of the terrain area then
  273. /// it returns false.
  274. ///
  275. bool getHeight( const Point2F &pos, F32 *height ) const;
  276. void getMinMaxHeight( F32 *minHeight, F32 *maxHeight ) const;
  277. /// This returns true and the terrain normal for a
  278. /// 2d position in the terrains object space.
  279. ///
  280. /// If the terrain at that point is within an empty block
  281. /// or the 2d position is outside of the terrain area then
  282. /// it returns false.
  283. ///
  284. bool getNormal( const Point2F &pos,
  285. Point3F *normal,
  286. bool normalize = true,
  287. bool skipEmpty = true ) const;
  288. /// This returns true and the smoothed terrain normal
  289. // for a 2d position in the terrains object space.
  290. ///
  291. /// If the terrain at that point is within an empty block
  292. /// or the 2d position is outside of the terrain area then
  293. /// it returns false.
  294. ///
  295. bool getSmoothNormal( const Point2F &pos,
  296. Point3F *normal,
  297. bool normalize = true,
  298. bool skipEmpty = true ) const;
  299. /// This returns true and the terrain normal and z height
  300. /// for a 2d position in the terrains object space.
  301. ///
  302. /// If the terrain at that point is within an empty block
  303. /// or the 2d position is outside of the terrain area then
  304. /// it returns false.
  305. ///
  306. bool getNormalAndHeight( const Point2F &pos,
  307. Point3F *normal,
  308. F32 *height,
  309. bool normalize = true ) const;
  310. /// This returns true and the terrain normal, z height, and
  311. /// material name for a 2d position in the terrains object
  312. /// space.
  313. ///
  314. /// If the terrain at that point is within an empty block
  315. /// or the 2d position is outside of the terrain area then
  316. /// it returns false.
  317. ///
  318. bool getNormalHeightMaterial( const Point2F &pos,
  319. Point3F *normal,
  320. F32 *height,
  321. StringTableEntry &matName ) const;
  322. // only the editor currently uses this method - should always be using a ray to collide with
  323. bool collideBox( const Point3F &start, const Point3F &end, RayInfo* info )
  324. {
  325. return castRay( start, end, info );
  326. }
  327. ///
  328. void setLightMap( GBitmap *newLightMap );
  329. /// Fills the lightmap with white.
  330. void clearLightMap();
  331. /// Retuns the dimensions of the light map.
  332. U32 getLightMapSize() const { return mLightMapSize; }
  333. const GBitmap* getLightMap() const { return mLightMap; }
  334. GBitmap* getLightMap() { return mLightMap; }
  335. ///
  336. GFXTextureObject* getLightMapTex();
  337. public:
  338. bool setFile( const FileName& terrFileName );
  339. void setFile(const Resource<TerrainFile>& file);
  340. bool setTerrainAsset(const StringTableEntry terrainAssetId);
  341. bool save(const char* filename);
  342. bool saveAsset();
  343. F32 getSquareSize() const { return mSquareSize; }
  344. /// Returns the dimensions of the terrain in world space.
  345. F32 getWorldBlockSize() const { return mSquareSize * (F32)mFile->mSize; }
  346. /// Retuns the dimensions of the terrain in samples.
  347. U32 getBlockSize() const { return mFile->mSize; }
  348. U32 getScreenError() const { return smLODScale * mScreenError; }
  349. // SceneObject
  350. void setTransform( const MatrixF &mat );
  351. void setScale( const VectorF &scale );
  352. void prepRenderImage ( SceneRenderState* state );
  353. void buildConvex(const Box3F& box,Convex* convex);
  354. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  355. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  356. bool castRayI(const Point3F &start, const Point3F &end, RayInfo* info, bool emptyCollide);
  357. bool castRayBlock( const Point3F &pStart,
  358. const Point3F &pEnd,
  359. const Point2I &blockPos,
  360. U32 level,
  361. F32 invDeltaX,
  362. F32 invDeltaY,
  363. F32 startT,
  364. F32 endT,
  365. RayInfo *info,
  366. bool collideEmpty );
  367. const StringTableEntry getTerrainFile() const { return mTerrFileName; }
  368. void postLight(Vector<TerrainBlock *> &terrBlocks) {};
  369. DECLARE_CONOBJECT(TerrainBlock);
  370. static void initPersistFields();
  371. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  372. void unpackUpdate(NetConnection *conn, BitStream *stream);
  373. void inspectPostApply();
  374. virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
  375. const StringTableEntry getTerrain() const
  376. {
  377. if (mTerrainAsset && (mTerrainAsset->getTerrainFilePath() != StringTable->EmptyString()))
  378. return mTerrainAsset->getTerrainFilePath();
  379. else if (mTerrainAssetId != StringTable->EmptyString())
  380. return mTerrainAssetId;
  381. else if (mTerrFileName != StringTable->EmptyString())
  382. return mTerrFileName;
  383. else
  384. return StringTable->EmptyString();
  385. }
  386. const StringTableEntry getTerrainAssetId() const
  387. {
  388. if (mTerrainAssetId != StringTable->EmptyString())
  389. return mTerrainAssetId;
  390. else
  391. return StringTable->EmptyString();
  392. }
  393. bool _setTerrain(StringTableEntry terrain)
  394. {
  395. if (terrain == StringTable->EmptyString())
  396. return false;
  397. if (AssetDatabase.isDeclaredAsset(terrain))
  398. setTerrainAsset(terrain);
  399. else
  400. mTerrFileName = terrain;
  401. return true;
  402. }
  403. bool renameTerrainMaterial(StringTableEntry oldMatName, StringTableEntry newMatName);
  404. S32 getTerrainMaterialCount() {
  405. if (mFile)
  406. return mFile->mMaterials.size();
  407. return 0;
  408. }
  409. StringTableEntry getTerrainMaterialName(S32 index) {
  410. if (mFile)
  411. return mFile->mMaterials[index]->getInternalName();
  412. return StringTable->EmptyString();
  413. }
  414. protected:
  415. bool mUpdateBasetex;
  416. bool mIgnoreZodiacs;
  417. U16* zode_primBuffer;
  418. void deleteZodiacPrimitiveBuffer();
  419. public:
  420. const U16* getZodiacPrimitiveBuffer();
  421. };
  422. #endif // _TERRDATA_H_