terrData.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. class GBitmap;
  56. class TerrainBlock;
  57. class TerrCell;
  58. class PhysicsBody;
  59. class TerrainCellMaterial;
  60. class TerrainBlock : public SceneObject
  61. {
  62. typedef SceneObject Parent;
  63. friend class TerrainEditor;
  64. friend class TerrainCellMaterial;
  65. protected:
  66. enum
  67. {
  68. TransformMask = Parent::NextFreeMask,
  69. FileMask = Parent::NextFreeMask << 1,
  70. SizeMask = Parent::NextFreeMask << 2,
  71. MaterialMask = Parent::NextFreeMask << 3,
  72. HeightMapChangeMask = Parent::NextFreeMask << 4,
  73. MiscMask = Parent::NextFreeMask << 5,
  74. NextFreeMask = Parent::NextFreeMask << 6,
  75. };
  76. public:
  77. enum BaseTexFormat
  78. {
  79. NONE, DDS, PNG
  80. };
  81. static const char* formatToExtension(BaseTexFormat format)
  82. {
  83. switch (format)
  84. {
  85. case DDS:
  86. return "dds";
  87. case PNG:
  88. return "png";
  89. default:
  90. return "";
  91. }
  92. };
  93. protected:
  94. Box3F mBounds;
  95. ///
  96. GBitmap *mLightMap;
  97. /// The lightmap dimensions in pixels.
  98. U32 mLightMapSize;
  99. /// The lightmap texture.
  100. GFXTexHandle mLightMapTex;
  101. /// The terrain data file.
  102. Resource<TerrainFile> mFile;
  103. /// The TerrainFile CRC sent from the server.
  104. U32 mCRC;
  105. ///
  106. StringTableEntry mTerrFileName;
  107. AssetPtr<TerrainAsset> mTerrainAsset;
  108. StringTableEntry mTerrainAssetId;
  109. /// The maximum detail distance found in the material list.
  110. F32 mMaxDetailDistance;
  111. ///
  112. Vector<GFXTexHandle> mBaseTextures;
  113. GFXTextureArrayHandle mDetailTextureArray;
  114. GFXTextureArrayHandle mMacroTextureArray;
  115. GFXTextureArrayHandle mNormalTextureArray;
  116. GFXTextureArrayHandle mOrmTextureArray;
  117. ///
  118. GFXTexHandle mLayerTex;
  119. /// The shader used to generate the base texture map.
  120. GFXShaderRef mBaseShader;
  121. ///
  122. GFXStateBlockRef mBaseShaderSB;
  123. ///
  124. GFXShaderConstBufferRef mBaseShaderConsts;
  125. ///
  126. GFXShaderConstHandle *mBaseTexScaleConst;
  127. GFXShaderConstHandle *mBaseTexIdConst;
  128. GFXShaderConstHandle *mBaseLayerSizeConst;
  129. ///
  130. GFXTextureTargetRef mBaseTarget;
  131. /// The base texture.
  132. GFXTexHandle mBaseTex;
  133. ///
  134. bool mDetailsDirty;
  135. ///
  136. bool mLayerTexDirty;
  137. /// The desired size for the base texture.
  138. U32 mBaseTexSize;
  139. BaseTexFormat mBaseTexFormat;
  140. ///
  141. TerrCell *mCell;
  142. /// The shared base material which is used to render
  143. /// cells that are outside the detail map range.
  144. TerrainCellMaterial *mBaseMaterial;
  145. /// A dummy material only used for shadow
  146. /// material generation.
  147. BaseMatInstance *mDefaultMatInst;
  148. F32 mSquareSize;
  149. PhysicsBody *mPhysicsRep;
  150. U32 mScreenError;
  151. /// The shared primitive buffer used in rendering.
  152. GFXPrimitiveBufferHandle mPrimBuffer;
  153. /// The cells used in the last render pass
  154. /// when doing debug rendering.
  155. /// @see _renderDebug
  156. Vector<TerrCell*> mDebugCells;
  157. /// Set to enable debug rendering of the terrain. It
  158. /// is exposed to the console via $terrain::debugRender.
  159. static bool smDebugRender;
  160. /// Allows the terrain to cast shadows onto itself and other objects.
  161. bool mCastShadows;
  162. /// A global LOD scale used to tweak the default
  163. /// terrain screen error value.
  164. static F32 smLODScale;
  165. /// A global detail scale used to tweak the
  166. /// material detail distances.
  167. static F32 smDetailScale;
  168. /// True if the zoning needs to be recalculated for the terrain.
  169. bool mZoningDirty;
  170. String _getBaseTexCacheFileName() const;
  171. void _rebuildQuadtree();
  172. void _updatePhysics();
  173. void _renderBlock( SceneRenderState *state );
  174. void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  175. /// The callback used to get texture events.
  176. /// @see GFXTextureManager::addEventDelegate
  177. void _onTextureEvent( GFXTexCallbackCode code );
  178. /// Used to release terrain materials when
  179. /// the material manager flushes them.
  180. /// @see MaterialManager::getFlushSignal
  181. void _onFlushMaterials();
  182. ///
  183. bool _initBaseShader();
  184. ///
  185. void _updateMaterials();
  186. ///
  187. void _updateBaseTexture( bool writeToCache );
  188. void _updateLayerTexture();
  189. void _updateBounds();
  190. void _onZoningChanged( SceneZoneSpaceManager *zoneManager );
  191. void _updateZoning();
  192. // Protected fields
  193. static bool _setTerrainFile( void *obj, const char *index, const char *data );
  194. static bool _setTerrainAsset(void* obj, const char* index, const char* data);
  195. static bool _setSquareSize( void *obj, const char *index, const char *data );
  196. static bool _setBaseTexSize(void *obj, const char *index, const char *data);
  197. static bool _setBaseTexFormat(void *obj, const char *index, const char *data);
  198. static bool _setLightMapSize( void *obj, const char *index, const char *data );
  199. public:
  200. enum
  201. {
  202. LightmapUpdate = BIT(0),
  203. HeightmapUpdate = BIT(1),
  204. LayersUpdate = BIT(2),
  205. EmptyUpdate = BIT(3)
  206. };
  207. static Signal<void(U32,TerrainBlock*,const Point2I& ,const Point2I&)> smUpdateSignal;
  208. ///
  209. bool import( const GBitmap &heightMap,
  210. F32 heightScale,
  211. F32 metersPerPixel,
  212. const Vector<U8> &layerMap,
  213. const Vector<String> &materials,
  214. bool flipYAxis = true );
  215. #ifdef TORQUE_TOOLS
  216. bool exportHeightMap( const UTF8 *filePath, const String &format ) const;
  217. bool exportLayerMaps( const UTF8 *filePrefix, const String &format ) const;
  218. #endif
  219. public:
  220. TerrainBlock();
  221. virtual ~TerrainBlock();
  222. U32 getCRC() const { return(mCRC); }
  223. Resource<TerrainFile> getFile() const { return mFile; };
  224. bool onAdd();
  225. void onRemove();
  226. void onEditorEnable();
  227. void onEditorDisable();
  228. /// Adds a new material as the top layer or
  229. /// inserts it at the specified index.
  230. void addMaterial( const String &name, U32 insertAt = -1 );
  231. /// Removes the material at the index.
  232. void removeMaterial( U32 index );
  233. /// Updates the material at the index.
  234. void updateMaterial( U32 index, const String &name );
  235. /// Deletes all the materials on the terrain.
  236. void deleteAllMaterials();
  237. void setMaterialsDirty() { mDetailsDirty = true; };
  238. //void setMaterialName( U32 index, const String &name );
  239. /// Accessors and mutators for TerrainMaterialUndoAction.
  240. /// @{
  241. const Vector<TerrainMaterial*>& getMaterials() const { return mFile->mMaterials; }
  242. const Vector<U8>& getLayerMap() const { return mFile->mLayerMap; }
  243. void setMaterials( const Vector<TerrainMaterial*> &materials ) { mFile->mMaterials = materials; }
  244. void setLayerMap( const Vector<U8> &layers ) { mFile->mLayerMap = layers; }
  245. /// @}
  246. TerrainMaterial* getMaterial( U32 index ) const;
  247. const char* getMaterialName( U32 index ) const;
  248. U32 getMaterialCount() const;
  249. GFXTextureArrayHandle getDetailTextureArray() const { return mDetailTextureArray; }
  250. GFXTextureArrayHandle getMacroTextureArray() const { return mMacroTextureArray; }
  251. GFXTextureArrayHandle getNormalTextureArray() const { return mNormalTextureArray; }
  252. GFXTextureArrayHandle getOrmTextureArray() const { return mOrmTextureArray; }
  253. //BaseMatInstance* getMaterialInst( U32 x, U32 y );
  254. void setHeight( const Point2I &pos, F32 height );
  255. F32 getHeight( const Point2I &pos );
  256. // Performs an update to the selected range of the terrain
  257. // grid including the collision and rendering structures.
  258. void updateGrid( const Point2I &minPt,
  259. const Point2I &maxPt,
  260. bool updateClient = false );
  261. void updateGridMaterials( const Point2I &minPt, const Point2I &maxPt );
  262. Point2I getGridPos( const Point3F &worldPos ) const;
  263. /// This returns true and the terrain z height for
  264. /// a 2d position in the terrains object space.
  265. ///
  266. /// If the terrain at that point is within an empty block
  267. /// or the 2d position is outside of the terrain area then
  268. /// it returns false.
  269. ///
  270. bool getHeight( const Point2F &pos, F32 *height ) const;
  271. void getMinMaxHeight( F32 *minHeight, F32 *maxHeight ) const;
  272. /// This returns true and the terrain normal for a
  273. /// 2d position in the terrains object space.
  274. ///
  275. /// If the terrain at that point is within an empty block
  276. /// or the 2d position is outside of the terrain area then
  277. /// it returns false.
  278. ///
  279. bool getNormal( const Point2F &pos,
  280. Point3F *normal,
  281. bool normalize = true,
  282. bool skipEmpty = true ) const;
  283. /// This returns true and the smoothed terrain normal
  284. // for a 2d position in the terrains object space.
  285. ///
  286. /// If the terrain at that point is within an empty block
  287. /// or the 2d position is outside of the terrain area then
  288. /// it returns false.
  289. ///
  290. bool getSmoothNormal( const Point2F &pos,
  291. Point3F *normal,
  292. bool normalize = true,
  293. bool skipEmpty = true ) const;
  294. /// This returns true and the terrain normal and z height
  295. /// for a 2d position in the terrains object space.
  296. ///
  297. /// If the terrain at that point is within an empty block
  298. /// or the 2d position is outside of the terrain area then
  299. /// it returns false.
  300. ///
  301. bool getNormalAndHeight( const Point2F &pos,
  302. Point3F *normal,
  303. F32 *height,
  304. bool normalize = true ) const;
  305. /// This returns true and the terrain normal, z height, and
  306. /// material name for a 2d position in the terrains object
  307. /// space.
  308. ///
  309. /// If the terrain at that point is within an empty block
  310. /// or the 2d position is outside of the terrain area then
  311. /// it returns false.
  312. ///
  313. bool getNormalHeightMaterial( const Point2F &pos,
  314. Point3F *normal,
  315. F32 *height,
  316. StringTableEntry &matName ) const;
  317. // only the editor currently uses this method - should always be using a ray to collide with
  318. bool collideBox( const Point3F &start, const Point3F &end, RayInfo* info )
  319. {
  320. return castRay( start, end, info );
  321. }
  322. ///
  323. void setLightMap( GBitmap *newLightMap );
  324. /// Fills the lightmap with white.
  325. void clearLightMap();
  326. /// Retuns the dimensions of the light map.
  327. U32 getLightMapSize() const { return mLightMapSize; }
  328. const GBitmap* getLightMap() const { return mLightMap; }
  329. GBitmap* getLightMap() { return mLightMap; }
  330. ///
  331. GFXTextureObject* getLightMapTex();
  332. public:
  333. bool setFile( const FileName& terrFileName );
  334. void setFile(const Resource<TerrainFile>& file);
  335. bool setTerrainAsset(const StringTableEntry terrainAssetId);
  336. bool save(const char* filename);
  337. bool saveAsset();
  338. F32 getSquareSize() const { return mSquareSize; }
  339. /// Returns the dimensions of the terrain in world space.
  340. F32 getWorldBlockSize() const { return mSquareSize * (F32)mFile->mSize; }
  341. /// Retuns the dimensions of the terrain in samples.
  342. U32 getBlockSize() const { return mFile->mSize; }
  343. U32 getScreenError() const { return smLODScale * mScreenError; }
  344. // SceneObject
  345. void setTransform( const MatrixF &mat );
  346. void setScale( const VectorF &scale );
  347. void prepRenderImage ( SceneRenderState* state );
  348. void buildConvex(const Box3F& box,Convex* convex);
  349. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  350. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  351. bool castRayI(const Point3F &start, const Point3F &end, RayInfo* info, bool emptyCollide);
  352. bool castRayBlock( const Point3F &pStart,
  353. const Point3F &pEnd,
  354. const Point2I &blockPos,
  355. U32 level,
  356. F32 invDeltaX,
  357. F32 invDeltaY,
  358. F32 startT,
  359. F32 endT,
  360. RayInfo *info,
  361. bool collideEmpty );
  362. const StringTableEntry getTerrainFile() const { return mTerrFileName; }
  363. void postLight(Vector<TerrainBlock *> &terrBlocks) {};
  364. DECLARE_CONOBJECT(TerrainBlock);
  365. static void initPersistFields();
  366. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  367. void unpackUpdate(NetConnection *conn, BitStream *stream);
  368. void inspectPostApply();
  369. virtual void getUtilizedAssets(Vector<StringTableEntry>* usedAssetsList);
  370. const StringTableEntry getTerrain() const
  371. {
  372. if (mTerrainAsset && (mTerrainAsset->getTerrainFilePath() != StringTable->EmptyString()))
  373. return mTerrainAsset->getTerrainFilePath();
  374. else if (mTerrainAssetId != StringTable->EmptyString())
  375. return mTerrainAssetId;
  376. else if (mTerrFileName != StringTable->EmptyString())
  377. return mTerrFileName;
  378. else
  379. return StringTable->EmptyString();
  380. }
  381. const StringTableEntry getTerrainAssetId() const
  382. {
  383. if (mTerrainAssetId != StringTable->EmptyString())
  384. return mTerrainAssetId;
  385. else
  386. return StringTable->EmptyString();
  387. }
  388. bool _setTerrain(StringTableEntry terrain)
  389. {
  390. if (terrain == StringTable->EmptyString())
  391. return false;
  392. if (AssetDatabase.isDeclaredAsset(terrain))
  393. setTerrainAsset(terrain);
  394. else
  395. mTerrFileName = terrain;
  396. return true;
  397. }
  398. protected:
  399. bool mUpdateBasetex;
  400. bool mIgnoreZodiacs;
  401. U16* zode_primBuffer;
  402. void deleteZodiacPrimitiveBuffer();
  403. public:
  404. const U16* getZodiacPrimitiveBuffer();
  405. };
  406. #endif // _TERRDATA_H_