ITerrainSceneNode.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. // The code for the TerrainSceneNode is based on the terrain renderer by
  5. // Soconne and the GeoMipMapSceneNode developed by Spintz. They made their
  6. // code available for Irrlicht and allowed it to be distributed under this
  7. // licence. I only modified some parts. A lot of thanks go to them.
  8. #ifndef __I_TERRAIN_SCENE_NODE_H__
  9. #define __I_TERRAIN_SCENE_NODE_H__
  10. #include "ETerrainElements.h"
  11. #include "ISceneNode.h"
  12. #include "IDynamicMeshBuffer.h"
  13. #include "irrArray.h"
  14. namespace irr
  15. {
  16. namespace io
  17. {
  18. class IReadFile;
  19. } // end namespace io
  20. namespace scene
  21. {
  22. class IMesh;
  23. //! A scene node for displaying terrain using the geo mip map algorithm.
  24. /** The code for the TerrainSceneNode is based on the Terrain renderer by Soconne and
  25. * the GeoMipMapSceneNode developed by Spintz. They made their code available for Irrlicht
  26. * and allowed it to be distributed under this licence. I only modified some parts.
  27. * A lot of thanks go to them.
  28. *
  29. * This scene node is capable of very quickly loading
  30. * terrains and updating the indices at runtime to enable viewing very large terrains. It uses a
  31. * CLOD (Continuous Level of Detail) algorithm which updates the indices for each patch based on
  32. * a LOD (Level of Detail) which is determined based on a patch's distance from the camera.
  33. *
  34. * The Patch Size of the terrain must always be a size of ( 2^N+1, i.e. 8+1(9), 16+1(17), etc. ).
  35. * The MaxLOD available is directly dependent on the patch size of the terrain. LOD 0 contains all
  36. * of the indices to draw all the triangles at the max detail for a patch. As each LOD goes up by 1
  37. * the step taken, in generating indices increases by - 2^LOD, so for LOD 1, the step taken is 2, for
  38. * LOD 2, the step taken is 4, LOD 3 - 8, etc. The step can be no larger than the size of the patch,
  39. * so having a LOD of 8, with a patch size of 17, is asking the algorithm to generate indices every
  40. * 2^8 ( 256 ) vertices, which is not possible with a patch size of 17. The maximum LOD for a patch
  41. * size of 17 is 2^4 ( 16 ). So, with a MaxLOD of 5, you'll have LOD 0 ( full detail ), LOD 1 ( every
  42. * 2 vertices ), LOD 2 ( every 4 vertices ), LOD 3 ( every 8 vertices ) and LOD 4 ( every 16 vertices ).
  43. **/
  44. class ITerrainSceneNode : public ISceneNode
  45. {
  46. public:
  47. //! Constructor
  48. ITerrainSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id,
  49. const core::vector3df& position = core::vector3df(0.0f, 0.0f, 0.0f),
  50. const core::vector3df& rotation = core::vector3df(0.0f, 0.0f, 0.0f),
  51. const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f) )
  52. : ISceneNode (parent, mgr, id, position, rotation, scale) {}
  53. //! Get the bounding box of the terrain.
  54. /** \return The bounding box of the entire terrain. */
  55. virtual const core::aabbox3d<f32>& getBoundingBox() const =0;
  56. //! Get the bounding box of a patch
  57. /** \return The bounding box of the chosen patch. */
  58. virtual const core::aabbox3d<f32>& getBoundingBox(s32 patchX, s32 patchZ) const =0;
  59. //! Get the number of indices currently in the meshbuffer
  60. /** \return The index count. */
  61. virtual u32 getIndexCount() const =0;
  62. //! Get pointer to the mesh
  63. /** \return Pointer to the mesh. */
  64. virtual IMesh* getMesh() =0;
  65. //! Get pointer to the buffer used by the terrain (most users will not need this)
  66. virtual IMeshBuffer* getRenderBuffer() =0;
  67. //! Gets the meshbuffer data based on a specified level of detail.
  68. /** \param mb A reference to an IDynamicMeshBuffer object
  69. \param LOD The level of detail you want the indices from. */
  70. virtual void getMeshBufferForLOD(IDynamicMeshBuffer& mb, s32 LOD=0) const =0;
  71. //! Gets the indices for a specified patch at a specified Level of Detail.
  72. /** \param indices A reference to an array of u32 indices.
  73. \param patchX Patch x coordinate.
  74. \param patchZ Patch z coordinate.
  75. \param LOD The level of detail to get for that patch. If -1,
  76. then get the CurrentLOD. If the CurrentLOD is set to -1,
  77. meaning it's not shown, then it will retrieve the triangles at
  78. the highest LOD (0).
  79. \return Number of indices put into the buffer. */
  80. virtual s32 getIndicesForPatch(core::array<u32>& indices,
  81. s32 patchX, s32 patchZ, s32 LOD=0) =0;
  82. //! Populates an array with the CurrentLOD of each patch.
  83. /** \param LODs A reference to a core::array<s32> to hold the
  84. values
  85. \return Number of elements in the array */
  86. virtual s32 getCurrentLODOfPatches(core::array<s32>& LODs) const =0;
  87. //! Manually sets the LOD of a patch
  88. /** NOTE: Any values set here are overwritten again in the automatic
  89. recalculations when the camera changes.
  90. \param patchX Patch x coordinate.
  91. \param patchZ Patch z coordinate.
  92. \param LOD The level of detail to set the patch to. */
  93. virtual void setLODOfPatch(s32 patchX, s32 patchZ, s32 LOD=0) =0;
  94. //! Get center of terrain.
  95. virtual const core::vector3df& getTerrainCenter() const =0;
  96. //! Get height of a point of the terrain.
  97. virtual f32 getHeight(f32 x, f32 y) const =0;
  98. //! Sets the movement camera threshold.
  99. /** It is used to determine when to recalculate
  100. indices for the scene node. The default value is 10.0f. */
  101. virtual void setCameraMovementDelta(f32 delta) =0;
  102. //! Sets the rotation camera threshold.
  103. /** It is used to determine when to recalculate
  104. indices for the scene node. The default value is 1.0f. */
  105. virtual void setCameraRotationDelta(f32 delta) =0;
  106. //! Sets whether or not the node should dynamically update its associated selector when the geomipmap data changes.
  107. /** \param bVal: Boolean value representing whether or not to update selector dynamically. */
  108. virtual void setDynamicSelectorUpdate(bool bVal) =0;
  109. //! Override the default generation of distance thresholds.
  110. /** For determining the LOD a patch is rendered at. If any LOD
  111. is overridden, then the scene node will no longer apply scaling
  112. factors to these values. If you override these distances, and
  113. then apply a scale to the scene node, it is your responsibility
  114. to update the new distances to work best with your new terrain
  115. size. */
  116. virtual bool overrideLODDistance(s32 LOD, f64 newDistance) =0;
  117. //! Scales the base texture, similar to makePlanarTextureMapping.
  118. /** \param scale The scaling amount. Values above 1.0
  119. increase the number of time the texture is drawn on the
  120. terrain. Values below 0 will decrease the number of times the
  121. texture is drawn on the terrain. Using negative values will
  122. flip the texture, as well as still scaling it.
  123. \param scale2 If set to 0 (default value), this will set the
  124. second texture coordinate set to the same values as in the
  125. first set. If this is another value than zero, it will scale
  126. the second texture coordinate set by this value. */
  127. virtual void scaleTexture(f32 scale = 1.0f, f32 scale2=0.0f) =0;
  128. //! Initializes the terrain data. Loads the vertices from the heightMapFile.
  129. /** The file must contain a loadable image of the heightmap. The heightmap
  130. must be square.
  131. \param file The file to read the image from. File is not rewinded.
  132. \param vertexColor Color of all vertices.
  133. \param smoothFactor Number of smoothing passes. */
  134. virtual bool loadHeightMap(io::IReadFile* file,
  135. video::SColor vertexColor=video::SColor(255,255,255,255),
  136. s32 smoothFactor=0) =0;
  137. //! Initializes the terrain data. Loads the vertices from the heightMapFile.
  138. /** The data is interpreted as (signed) integers of the given bit size or
  139. floats (with 32bits, signed). Allowed bitsizes for integers are
  140. 8, 16, and 32. The heightmap must be square.
  141. \param file The file to read the RAW data from. File is not rewinded.
  142. \param bitsPerPixel Size of data if integers used, for floats always use 32.
  143. \param signedData Whether we use signed or unsigned ints, ignored for floats.
  144. \param floatVals Whether the data is float or int.
  145. \param width Width (and also Height, as it must be square) of the heightmap. Use 0 for autocalculating from the filesize.
  146. \param vertexColor Color of all vertices.
  147. \param smoothFactor Number of smoothing passes. */
  148. virtual bool loadHeightMapRAW(io::IReadFile* file, s32 bitsPerPixel=16,
  149. bool signedData=false, bool floatVals=false, s32 width=0,
  150. video::SColor vertexColor=video::SColor(255,255,255,255),
  151. s32 smoothFactor=0) =0;
  152. //! Force node to use a fixed LOD level at the borders of the terrain.
  153. /** This can be useful when several TerrainSceneNodes are connected.
  154. \param borderLOD When >= 0 all patches at the 4 borders will use the
  155. given LOD. When < 0 borders are just regular patches (that's default). */
  156. virtual void setFixedBorderLOD(irr::s32 borderLOD=0) = 0;
  157. };
  158. } // end namespace scene
  159. } // end namespace irr
  160. #endif // __I_TERRAIN_SCENE_NODE_H__