terrData.h 14 KB

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