terrData.h 13 KB

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