mapblock_mesh.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef MAPBLOCK_MESH_HEADER
  17. #define MAPBLOCK_MESH_HEADER
  18. #include "irrlichttypes_extrabloated.h"
  19. #include "client/tile.h"
  20. #include "voxel.h"
  21. #include <map>
  22. class Client;
  23. class IShaderSource;
  24. /*
  25. Mesh making stuff
  26. */
  27. class MapBlock;
  28. struct MinimapMapblock;
  29. struct MeshMakeData
  30. {
  31. VoxelManipulator m_vmanip;
  32. v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
  33. v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
  34. bool m_smooth_lighting = false;
  35. bool m_show_hud = false;
  36. Client *m_client;
  37. bool m_use_shaders;
  38. bool m_use_tangent_vertices;
  39. MeshMakeData(Client *client, bool use_shaders,
  40. bool use_tangent_vertices = false);
  41. /*
  42. Copy block data manually (to allow optimizations by the caller)
  43. */
  44. void fillBlockDataBegin(const v3s16 &blockpos);
  45. void fillBlockData(const v3s16 &block_offset, MapNode *data);
  46. /*
  47. Copy central data directly from block, and other data from
  48. parent of block.
  49. */
  50. void fill(MapBlock *block);
  51. /*
  52. Set up with only a single node at (1,1,1)
  53. */
  54. void fillSingleNode(MapNode *node);
  55. /*
  56. Set the (node) position of a crack
  57. */
  58. void setCrack(int crack_level, v3s16 crack_pos);
  59. /*
  60. Enable or disable smooth lighting
  61. */
  62. void setSmoothLighting(bool smooth_lighting);
  63. };
  64. /*
  65. Holds a mesh for a mapblock.
  66. Besides the SMesh*, this contains information used for animating
  67. the vertex positions, colors and texture coordinates of the mesh.
  68. For example:
  69. - cracks [implemented]
  70. - day/night transitions [implemented]
  71. - animated flowing liquids [not implemented]
  72. - animating vertex positions for e.g. axles [not implemented]
  73. */
  74. class MapBlockMesh
  75. {
  76. public:
  77. // Builds the mesh given
  78. MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
  79. ~MapBlockMesh();
  80. // Main animation function, parameters:
  81. // faraway: whether the block is far away from the camera (~50 nodes)
  82. // time: the global animation time, 0 .. 60 (repeats every minute)
  83. // daynight_ratio: 0 .. 1000
  84. // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
  85. // Returns true if anything has been changed.
  86. bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
  87. scene::IMesh *getMesh()
  88. {
  89. return m_mesh[0];
  90. }
  91. scene::IMesh *getMesh(u8 layer)
  92. {
  93. return m_mesh[layer];
  94. }
  95. MinimapMapblock *moveMinimapMapblock()
  96. {
  97. MinimapMapblock *p = m_minimap_mapblock;
  98. m_minimap_mapblock = NULL;
  99. return p;
  100. }
  101. bool isAnimationForced() const
  102. {
  103. return m_animation_force_timer == 0;
  104. }
  105. void decreaseAnimationForceTimer()
  106. {
  107. if(m_animation_force_timer > 0)
  108. m_animation_force_timer--;
  109. }
  110. void updateCameraOffset(v3s16 camera_offset);
  111. private:
  112. scene::IMesh *m_mesh[MAX_TILE_LAYERS];
  113. MinimapMapblock *m_minimap_mapblock;
  114. Client *m_client;
  115. video::IVideoDriver *m_driver;
  116. ITextureSource *m_tsrc;
  117. IShaderSource *m_shdrsrc;
  118. bool m_enable_shaders;
  119. bool m_use_tangent_vertices;
  120. bool m_enable_vbo;
  121. // Must animate() be called before rendering?
  122. bool m_has_animation;
  123. int m_animation_force_timer;
  124. // Animation info: cracks
  125. // Last crack value passed to animate()
  126. int m_last_crack;
  127. // Maps mesh and mesh buffer (i.e. material) indices to base texture names
  128. std::map<std::pair<u8, u32>, std::string> m_crack_materials;
  129. // Animation info: texture animationi
  130. // Maps mesh and mesh buffer indices to TileSpecs
  131. // Keys are pairs of (mesh index, buffer index in the mesh)
  132. std::map<std::pair<u8, u32>, TileLayer> m_animation_tiles;
  133. std::map<std::pair<u8, u32>, int> m_animation_frames; // last animation frame
  134. std::map<std::pair<u8, u32>, int> m_animation_frame_offsets;
  135. // Animation info: day/night transitions
  136. // Last daynight_ratio value passed to animate()
  137. u32 m_last_daynight_ratio;
  138. // For each mesh and mesh buffer, stores pre-baked colors
  139. // of sunlit vertices
  140. // Keys are pairs of (mesh index, buffer index in the mesh)
  141. std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
  142. // Camera offset info -> do we have to translate the mesh?
  143. v3s16 m_camera_offset;
  144. };
  145. /*
  146. This is used because CMeshBuffer::append() is very slow
  147. */
  148. struct PreMeshBuffer
  149. {
  150. TileLayer layer;
  151. std::vector<u16> indices;
  152. std::vector<video::S3DVertex> vertices;
  153. std::vector<video::S3DVertexTangents> tangent_vertices;
  154. };
  155. struct MeshCollector
  156. {
  157. std::vector<PreMeshBuffer> prebuffers[MAX_TILE_LAYERS];
  158. bool m_use_tangent_vertices;
  159. MeshCollector(bool use_tangent_vertices):
  160. m_use_tangent_vertices(use_tangent_vertices)
  161. {
  162. }
  163. void append(const TileSpec &material,
  164. const video::S3DVertex *vertices, u32 numVertices,
  165. const u16 *indices, u32 numIndices);
  166. void append(const TileLayer &material,
  167. const video::S3DVertex *vertices, u32 numVertices,
  168. const u16 *indices, u32 numIndices, u8 layernum);
  169. void append(const TileSpec &material,
  170. const video::S3DVertex *vertices, u32 numVertices,
  171. const u16 *indices, u32 numIndices, v3f pos,
  172. video::SColor c, u8 light_source);
  173. void append(const TileLayer &material,
  174. const video::S3DVertex *vertices, u32 numVertices,
  175. const u16 *indices, u32 numIndices, v3f pos,
  176. video::SColor c, u8 light_source, u8 layernum);
  177. /*!
  178. * Colorizes all vertices in the collector.
  179. */
  180. void applyTileColors();
  181. };
  182. /*!
  183. * Encodes light of a node.
  184. * The result is not the final color, but a
  185. * half-baked vertex color.
  186. * You have to multiply the resulting color
  187. * with the node's color.
  188. *
  189. * \param light the first 8 bits are day light,
  190. * the last 8 bits are night light
  191. * \param emissive_light amount of light the surface emits,
  192. * from 0 to LIGHT_SUN.
  193. */
  194. video::SColor encode_light(u16 light, u8 emissive_light);
  195. // Compute light at node
  196. u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
  197. u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
  198. u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
  199. /*!
  200. * Returns the sunlight's color from the current
  201. * day-night ratio.
  202. */
  203. void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
  204. /*!
  205. * Gives the final SColor shown on screen.
  206. *
  207. * \param result output color
  208. * \param light first 8 bits are day light, second 8 bits are
  209. * night light
  210. */
  211. void final_color_blend(video::SColor *result,
  212. u16 light, u32 daynight_ratio);
  213. /*!
  214. * Gives the final SColor shown on screen.
  215. *
  216. * \param result output color
  217. * \param data the half-baked vertex color
  218. * \param dayLight color of the sunlight
  219. */
  220. void final_color_blend(video::SColor *result,
  221. const video::SColor &data, const video::SColorf &dayLight);
  222. // Retrieves the TileSpec of a face of a node
  223. // Adds MATERIAL_FLAG_CRACK if the node is cracked
  224. // TileSpec should be passed as reference due to the underlying TileFrame and its vector
  225. // TileFrame vector copy cost very much to client
  226. void getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
  227. void getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data, TileSpec &tile);
  228. #endif