mapblock_mesh.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #pragma once
  17. #include "irrlichttypes_extrabloated.h"
  18. #include "client/tile.h"
  19. #include "voxel.h"
  20. #include <array>
  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. Client *m_client;
  36. bool m_use_shaders;
  37. bool m_use_tangent_vertices;
  38. MeshMakeData(Client *client, bool use_shaders,
  39. bool use_tangent_vertices = false);
  40. /*
  41. Copy block data manually (to allow optimizations by the caller)
  42. */
  43. void fillBlockDataBegin(const v3s16 &blockpos);
  44. void fillBlockData(const v3s16 &block_offset, MapNode *data);
  45. /*
  46. Copy central data directly from block, and other data from
  47. parent of block.
  48. */
  49. void fill(MapBlock *block);
  50. /*
  51. Set up with only a single node at (1,1,1)
  52. */
  53. void fillSingleNode(MapNode *node);
  54. /*
  55. Set the (node) position of a crack
  56. */
  57. void setCrack(int crack_level, v3s16 crack_pos);
  58. /*
  59. Enable or disable smooth lighting
  60. */
  61. void setSmoothLighting(bool smooth_lighting);
  62. };
  63. /*
  64. Holds a mesh for a mapblock.
  65. Besides the SMesh*, this contains information used for animating
  66. the vertex positions, colors and texture coordinates of the mesh.
  67. For example:
  68. - cracks [implemented]
  69. - day/night transitions [implemented]
  70. - animated flowing liquids [not implemented]
  71. - animating vertex positions for e.g. axles [not implemented]
  72. */
  73. class MapBlockMesh
  74. {
  75. public:
  76. // Builds the mesh given
  77. MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
  78. ~MapBlockMesh();
  79. // Main animation function, parameters:
  80. // faraway: whether the block is far away from the camera (~50 nodes)
  81. // time: the global animation time, 0 .. 60 (repeats every minute)
  82. // daynight_ratio: 0 .. 1000
  83. // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
  84. // Returns true if anything has been changed.
  85. bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
  86. scene::IMesh *getMesh()
  87. {
  88. return m_mesh[0];
  89. }
  90. scene::IMesh *getMesh(u8 layer)
  91. {
  92. return m_mesh[layer];
  93. }
  94. MinimapMapblock *moveMinimapMapblock()
  95. {
  96. MinimapMapblock *p = m_minimap_mapblock;
  97. m_minimap_mapblock = NULL;
  98. return p;
  99. }
  100. bool isAnimationForced() const
  101. {
  102. return m_animation_force_timer == 0;
  103. }
  104. void decreaseAnimationForceTimer()
  105. {
  106. if(m_animation_force_timer > 0)
  107. m_animation_force_timer--;
  108. }
  109. void updateCameraOffset(v3s16 camera_offset);
  110. private:
  111. scene::IMesh *m_mesh[MAX_TILE_LAYERS];
  112. MinimapMapblock *m_minimap_mapblock;
  113. ITextureSource *m_tsrc;
  114. IShaderSource *m_shdrsrc;
  115. bool m_enable_shaders;
  116. bool m_use_tangent_vertices;
  117. bool m_enable_vbo;
  118. // Must animate() be called before rendering?
  119. bool m_has_animation;
  120. int m_animation_force_timer;
  121. // Animation info: cracks
  122. // Last crack value passed to animate()
  123. int m_last_crack;
  124. // Maps mesh and mesh buffer (i.e. material) indices to base texture names
  125. std::map<std::pair<u8, u32>, std::string> m_crack_materials;
  126. // Animation info: texture animationi
  127. // Maps mesh and mesh buffer indices to TileSpecs
  128. // Keys are pairs of (mesh index, buffer index in the mesh)
  129. std::map<std::pair<u8, u32>, TileLayer> m_animation_tiles;
  130. std::map<std::pair<u8, u32>, int> m_animation_frames; // last animation frame
  131. std::map<std::pair<u8, u32>, int> m_animation_frame_offsets;
  132. // Animation info: day/night transitions
  133. // Last daynight_ratio value passed to animate()
  134. u32 m_last_daynight_ratio;
  135. // For each mesh and mesh buffer, stores pre-baked colors
  136. // of sunlit vertices
  137. // Keys are pairs of (mesh index, buffer index in the mesh)
  138. std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
  139. // Camera offset info -> do we have to translate the mesh?
  140. v3s16 m_camera_offset;
  141. };
  142. /*!
  143. * Encodes light of a node.
  144. * The result is not the final color, but a
  145. * half-baked vertex color.
  146. * You have to multiply the resulting color
  147. * with the node's color.
  148. *
  149. * \param light the first 8 bits are day light,
  150. * the last 8 bits are night light
  151. * \param emissive_light amount of light the surface emits,
  152. * from 0 to LIGHT_SUN.
  153. */
  154. video::SColor encode_light(u16 light, u8 emissive_light);
  155. // Compute light at node
  156. u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
  157. u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir,
  158. const NodeDefManager *ndef);
  159. u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
  160. u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
  161. /*!
  162. * Returns the sunlight's color from the current
  163. * day-night ratio.
  164. */
  165. void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
  166. /*!
  167. * Gives the final SColor shown on screen.
  168. *
  169. * \param result output color
  170. * \param light first 8 bits are day light, second 8 bits are
  171. * night light
  172. */
  173. void final_color_blend(video::SColor *result,
  174. u16 light, u32 daynight_ratio);
  175. /*!
  176. * Gives the final SColor shown on screen.
  177. *
  178. * \param result output color
  179. * \param data the half-baked vertex color
  180. * \param dayLight color of the sunlight
  181. */
  182. void final_color_blend(video::SColor *result,
  183. const video::SColor &data, const video::SColorf &dayLight);
  184. // Retrieves the TileSpec of a face of a node
  185. // Adds MATERIAL_FLAG_CRACK if the node is cracked
  186. // TileSpec should be passed as reference due to the underlying TileFrame and its vector
  187. // TileFrame vector copy cost very much to client
  188. void getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
  189. void getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data, TileSpec &tile);