terrData.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /// For disabling of Basetexture generation
  157. bool mUpdateBasetex;
  158. String _getBaseTexCacheFileName() const;
  159. void _rebuildQuadtree();
  160. void _updatePhysics();
  161. void _renderBlock( SceneRenderState *state );
  162. void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  163. /// The callback used to get texture events.
  164. /// @see GFXTextureManager::addEventDelegate
  165. void _onTextureEvent( GFXTexCallbackCode code );
  166. /// Used to release terrain materials when
  167. /// the material manager flushes them.
  168. /// @see MaterialManager::getFlushSignal
  169. void _onFlushMaterials();
  170. ///
  171. bool _initBaseShader();
  172. ///
  173. void _updateMaterials();
  174. ///
  175. void _updateBaseTexture( bool writeToCache );
  176. void _updateLayerTexture();
  177. void _updateBounds();
  178. void _onZoningChanged( SceneZoneSpaceManager *zoneManager );
  179. void _updateZoning();
  180. // Protected fields
  181. static bool _setTerrainFile( void *obj, const char *index, const char *data );
  182. static bool _setSquareSize( void *obj, const char *index, const char *data );
  183. static bool _setBaseTexSize(void *obj, const char *index, const char *data);
  184. static bool _setBaseTexFormat(void *obj, const char *index, const char *data);
  185. static bool _setLightMapSize( void *obj, const char *index, const char *data );
  186. public:
  187. enum
  188. {
  189. LightmapUpdate = BIT(0),
  190. HeightmapUpdate = BIT(1),
  191. LayersUpdate = BIT(2),
  192. EmptyUpdate = BIT(3)
  193. };
  194. static Signal<void(U32,TerrainBlock*,const Point2I& ,const Point2I&)> smUpdateSignal;
  195. ///
  196. bool import( const GBitmap &heightMap,
  197. F32 heightScale,
  198. F32 metersPerPixel,
  199. const Vector<U8> &layerMap,
  200. const Vector<String> &materials,
  201. bool flipYAxis = true );
  202. #ifdef TORQUE_TOOLS
  203. bool exportHeightMap( const UTF8 *filePath, const String &format ) const;
  204. bool exportLayerMaps( const UTF8 *filePrefix, const String &format ) const;
  205. #endif
  206. public:
  207. TerrainBlock();
  208. virtual ~TerrainBlock();
  209. U32 getCRC() const { return(mCRC); }
  210. Resource<TerrainFile> getFile() const { return mFile; };
  211. bool onAdd();
  212. void onRemove();
  213. void onEditorEnable();
  214. void onEditorDisable();
  215. /// Adds a new material as the top layer or
  216. /// inserts it at the specified index.
  217. void addMaterial( const String &name, U32 insertAt = -1 );
  218. /// Removes the material at the index.
  219. void removeMaterial( U32 index );
  220. /// Updates the material at the index.
  221. void updateMaterial( U32 index, const String &name );
  222. /// Deletes all the materials on the terrain.
  223. void deleteAllMaterials();
  224. //void setMaterialName( U32 index, const String &name );
  225. /// Accessors and mutators for TerrainMaterialUndoAction.
  226. /// @{
  227. const Vector<TerrainMaterial*>& getMaterials() const { return mFile->mMaterials; }
  228. const Vector<U8>& getLayerMap() const { return mFile->mLayerMap; }
  229. void setMaterials( const Vector<TerrainMaterial*> &materials ) { mFile->mMaterials = materials; }
  230. void setLayerMap( const Vector<U8> &layers ) { mFile->mLayerMap = layers; }
  231. /// @}
  232. TerrainMaterial* getMaterial( U32 index ) const;
  233. const char* getMaterialName( U32 index ) const;
  234. U32 getMaterialCount() const;
  235. //BaseMatInstance* getMaterialInst( U32 x, U32 y );
  236. void setHeight( const Point2I &pos, F32 height );
  237. F32 getHeight( const Point2I &pos );
  238. // Performs an update to the selected range of the terrain
  239. // grid including the collision and rendering structures.
  240. void updateGrid( const Point2I &minPt,
  241. const Point2I &maxPt,
  242. bool updateClient = false );
  243. void updateGridMaterials( const Point2I &minPt, const Point2I &maxPt );
  244. Point2I getGridPos( const Point3F &worldPos ) const;
  245. /// This returns true and the terrain z height for
  246. /// a 2d position in the terrains object space.
  247. ///
  248. /// If the terrain at that point is within an empty block
  249. /// or the 2d position is outside of the terrain area then
  250. /// it returns false.
  251. ///
  252. bool getHeight( const Point2F &pos, F32 *height ) const;
  253. void getMinMaxHeight( F32 *minHeight, F32 *maxHeight ) const;
  254. /// This returns true and the terrain normal for a
  255. /// 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 getNormal( const Point2F &pos,
  262. Point3F *normal,
  263. bool normalize = true,
  264. bool skipEmpty = true ) const;
  265. /// This returns true and the smoothed terrain normal
  266. // for a 2d position in the terrains object space.
  267. ///
  268. /// If the terrain at that point is within an empty block
  269. /// or the 2d position is outside of the terrain area then
  270. /// it returns false.
  271. ///
  272. bool getSmoothNormal( const Point2F &pos,
  273. Point3F *normal,
  274. bool normalize = true,
  275. bool skipEmpty = true ) const;
  276. /// This returns true and the terrain normal and z height
  277. /// for a 2d position in the terrains object space.
  278. ///
  279. /// If the terrain at that point is within an empty block
  280. /// or the 2d position is outside of the terrain area then
  281. /// it returns false.
  282. ///
  283. bool getNormalAndHeight( const Point2F &pos,
  284. Point3F *normal,
  285. F32 *height,
  286. bool normalize = true ) const;
  287. /// This returns true and the terrain normal, z height, and
  288. /// material name for a 2d position in the terrains object
  289. /// 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 getNormalHeightMaterial( const Point2F &pos,
  296. Point3F *normal,
  297. F32 *height,
  298. StringTableEntry &matName ) const;
  299. // only the editor currently uses this method - should always be using a ray to collide with
  300. bool collideBox( const Point3F &start, const Point3F &end, RayInfo* info )
  301. {
  302. return castRay( start, end, info );
  303. }
  304. ///
  305. void setLightMap( GBitmap *newLightMap );
  306. /// Fills the lightmap with white.
  307. void clearLightMap();
  308. /// Retuns the dimensions of the light map.
  309. U32 getLightMapSize() const { return mLightMapSize; }
  310. const GBitmap* getLightMap() const { return mLightMap; }
  311. GBitmap* getLightMap() { return mLightMap; }
  312. ///
  313. GFXTextureObject* getLightMapTex();
  314. public:
  315. bool setFile( const FileName& terrFileName );
  316. void setFile(const Resource<TerrainFile>& file);
  317. bool save(const char* filename);
  318. F32 getSquareSize() const { return mSquareSize; }
  319. /// Returns the dimensions of the terrain in world space.
  320. F32 getWorldBlockSize() const { return mSquareSize * (F32)mFile->mSize; }
  321. /// Retuns the dimensions of the terrain in samples.
  322. U32 getBlockSize() const { return mFile->mSize; }
  323. U32 getScreenError() const { return smLODScale * mScreenError; }
  324. // SceneObject
  325. void setTransform( const MatrixF &mat );
  326. void setScale( const VectorF &scale );
  327. void prepRenderImage ( SceneRenderState* state );
  328. void buildConvex(const Box3F& box,Convex* convex);
  329. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  330. bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  331. bool castRayI(const Point3F &start, const Point3F &end, RayInfo* info, bool emptyCollide);
  332. bool castRayBlock( const Point3F &pStart,
  333. const Point3F &pEnd,
  334. const Point2I &blockPos,
  335. U32 level,
  336. F32 invDeltaX,
  337. F32 invDeltaY,
  338. F32 startT,
  339. F32 endT,
  340. RayInfo *info,
  341. bool collideEmpty );
  342. const FileName& getTerrainFile() const { return mTerrFileName; }
  343. void postLight(Vector<TerrainBlock *> &terrBlocks) {};
  344. DECLARE_CONOBJECT(TerrainBlock);
  345. static void initPersistFields();
  346. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  347. void unpackUpdate(NetConnection *conn, BitStream *stream);
  348. void inspectPostApply();
  349. };
  350. #endif // _TERRDATA_H_