terrData.h 13 KB

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