mapblock_mesh.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "util/numeric.h"
  19. #include "client/tile.h"
  20. #include "voxel.h"
  21. #include <array>
  22. #include <map>
  23. #include <unordered_map>
  24. class Client;
  25. class NodeDefManager;
  26. class IShaderSource;
  27. class ITextureSource;
  28. /*
  29. Mesh making stuff
  30. */
  31. class MapBlock;
  32. struct MinimapMapblock;
  33. struct MeshMakeData
  34. {
  35. VoxelManipulator m_vmanip;
  36. v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
  37. v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
  38. bool m_smooth_lighting = false;
  39. u16 side_length;
  40. const NodeDefManager *nodedef;
  41. bool m_use_shaders;
  42. MeshMakeData(const NodeDefManager *ndef, u16 side_length, bool use_shaders);
  43. /*
  44. Copy block data manually (to allow optimizations by the caller)
  45. */
  46. void fillBlockDataBegin(const v3s16 &blockpos);
  47. void fillBlockData(const v3s16 &bp, MapNode *data);
  48. /*
  49. Set the (node) position of a crack
  50. */
  51. void setCrack(int crack_level, v3s16 crack_pos);
  52. /*
  53. Enable or disable smooth lighting
  54. */
  55. void setSmoothLighting(bool smooth_lighting);
  56. };
  57. // represents a triangle as indexes into the vertex buffer in SMeshBuffer
  58. class MeshTriangle
  59. {
  60. public:
  61. scene::SMeshBuffer *buffer;
  62. u16 p1, p2, p3;
  63. v3f centroid;
  64. float areaSQ;
  65. void updateAttributes()
  66. {
  67. v3f v1 = buffer->getPosition(p1);
  68. v3f v2 = buffer->getPosition(p2);
  69. v3f v3 = buffer->getPosition(p3);
  70. centroid = (v1 + v2 + v3) / 3;
  71. areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
  72. }
  73. v3f getNormal() const {
  74. v3f v1 = buffer->getPosition(p1);
  75. v3f v2 = buffer->getPosition(p2);
  76. v3f v3 = buffer->getPosition(p3);
  77. return (v2-v1).crossProduct(v3-v1);
  78. }
  79. };
  80. /**
  81. * Implements a binary space partitioning tree
  82. * See also: https://en.wikipedia.org/wiki/Binary_space_partitioning
  83. */
  84. class MapBlockBspTree
  85. {
  86. public:
  87. MapBlockBspTree() {}
  88. void buildTree(const std::vector<MeshTriangle> *triangles, u16 side_lingth);
  89. void traverse(v3f viewpoint, std::vector<s32> &output) const
  90. {
  91. traverse(root, viewpoint, output);
  92. }
  93. private:
  94. // Tree node definition;
  95. struct TreeNode
  96. {
  97. v3f normal;
  98. v3f origin;
  99. std::vector<s32> triangle_refs;
  100. s32 front_ref;
  101. s32 back_ref;
  102. TreeNode() = default;
  103. TreeNode(v3f normal, v3f origin, const std::vector<s32> &triangle_refs, s32 front_ref, s32 back_ref) :
  104. normal(normal), origin(origin), triangle_refs(triangle_refs), front_ref(front_ref), back_ref(back_ref)
  105. {}
  106. };
  107. s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
  108. void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
  109. const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
  110. std::vector<TreeNode> nodes; // list of nodes
  111. s32 root = -1; // index of the root node
  112. };
  113. /*
  114. * PartialMeshBuffer
  115. *
  116. * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
  117. * indices with the same vertex buffer.
  118. *
  119. * Irrlicht does not currently support this: `CMeshBuffer` ties together a single vertex buffer
  120. * and a single index buffer. There's no way to share these between mesh buffers.
  121. *
  122. */
  123. class PartialMeshBuffer
  124. {
  125. public:
  126. PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indexes) :
  127. m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes))
  128. {}
  129. scene::IMeshBuffer *getBuffer() const { return m_buffer; }
  130. const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
  131. void beforeDraw() const;
  132. void afterDraw() const;
  133. private:
  134. scene::SMeshBuffer *m_buffer;
  135. mutable std::vector<u16> m_vertex_indexes;
  136. };
  137. /*
  138. Holds a mesh for a mapblock.
  139. Besides the SMesh*, this contains information used for animating
  140. the vertex positions, colors and texture coordinates of the mesh.
  141. For example:
  142. - cracks [implemented]
  143. - day/night transitions [implemented]
  144. - animated flowing liquids [not implemented]
  145. - animating vertex positions for e.g. axles [not implemented]
  146. */
  147. class MapBlockMesh
  148. {
  149. public:
  150. // Builds the mesh given
  151. MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset);
  152. ~MapBlockMesh();
  153. // Main animation function, parameters:
  154. // faraway: whether the block is far away from the camera (~50 nodes)
  155. // time: the global animation time, 0 .. 60 (repeats every minute)
  156. // daynight_ratio: 0 .. 1000
  157. // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
  158. // Returns true if anything has been changed.
  159. bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
  160. scene::IMesh *getMesh()
  161. {
  162. return m_mesh[0];
  163. }
  164. scene::IMesh *getMesh(u8 layer)
  165. {
  166. return m_mesh[layer];
  167. }
  168. std::vector<MinimapMapblock*> moveMinimapMapblocks()
  169. {
  170. std::vector<MinimapMapblock*> minimap_mapblocks;
  171. minimap_mapblocks.swap(m_minimap_mapblocks);
  172. return minimap_mapblocks;
  173. }
  174. bool isAnimationForced() const
  175. {
  176. return m_animation_force_timer == 0;
  177. }
  178. void decreaseAnimationForceTimer()
  179. {
  180. if(m_animation_force_timer > 0)
  181. m_animation_force_timer--;
  182. }
  183. /// Radius of the bounding-sphere, in BS-space.
  184. f32 getBoundingRadius() const { return m_bounding_radius; }
  185. /// Center of the bounding-sphere, in BS-space, relative to block pos.
  186. v3f getBoundingSphereCenter() const { return m_bounding_sphere_center; }
  187. /// update transparent buffers to render towards the camera
  188. void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
  189. void consolidateTransparentBuffers();
  190. /// get the list of transparent buffers
  191. const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
  192. {
  193. return this->m_transparent_buffers;
  194. }
  195. private:
  196. struct AnimationInfo {
  197. int frame; // last animation frame
  198. int frame_offset;
  199. TileLayer tile;
  200. };
  201. scene::IMesh *m_mesh[MAX_TILE_LAYERS];
  202. std::vector<MinimapMapblock*> m_minimap_mapblocks;
  203. ITextureSource *m_tsrc;
  204. IShaderSource *m_shdrsrc;
  205. f32 m_bounding_radius;
  206. v3f m_bounding_sphere_center;
  207. bool m_enable_shaders;
  208. // Must animate() be called before rendering?
  209. bool m_has_animation;
  210. int m_animation_force_timer;
  211. // Animation info: cracks
  212. // Last crack value passed to animate()
  213. int m_last_crack;
  214. // Maps mesh and mesh buffer (i.e. material) indices to base texture names
  215. std::map<std::pair<u8, u32>, std::string> m_crack_materials;
  216. // Animation info: texture animation
  217. // Maps mesh and mesh buffer indices to TileSpecs
  218. // Keys are pairs of (mesh index, buffer index in the mesh)
  219. std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
  220. // Animation info: day/night transitions
  221. // Last daynight_ratio value passed to animate()
  222. u32 m_last_daynight_ratio;
  223. // For each mesh and mesh buffer, stores pre-baked colors
  224. // of sunlit vertices
  225. // Keys are pairs of (mesh index, buffer index in the mesh)
  226. std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
  227. // list of all semitransparent triangles in the mapblock
  228. std::vector<MeshTriangle> m_transparent_triangles;
  229. // Binary Space Partitioning tree for the block
  230. MapBlockBspTree m_bsp_tree;
  231. // Ordered list of references to parts of transparent buffers to draw
  232. std::vector<PartialMeshBuffer> m_transparent_buffers;
  233. };
  234. /*!
  235. * Encodes light of a node.
  236. * The result is not the final color, but a
  237. * half-baked vertex color.
  238. * You have to multiply the resulting color
  239. * with the node's color.
  240. *
  241. * \param light the first 8 bits are day light,
  242. * the last 8 bits are night light
  243. * \param emissive_light amount of light the surface emits,
  244. * from 0 to LIGHT_SUN.
  245. */
  246. video::SColor encode_light(u16 light, u8 emissive_light);
  247. // Compute light at node
  248. u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
  249. u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
  250. u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
  251. u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
  252. /*!
  253. * Returns the sunlight's color from the current
  254. * day-night ratio.
  255. */
  256. void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
  257. /*!
  258. * Gives the final SColor shown on screen.
  259. *
  260. * \param result output color
  261. * \param light first 8 bits are day light, second 8 bits are
  262. * night light
  263. */
  264. void final_color_blend(video::SColor *result,
  265. u16 light, u32 daynight_ratio);
  266. /*!
  267. * Gives the final SColor shown on screen.
  268. *
  269. * \param result output color
  270. * \param data the half-baked vertex color
  271. * \param dayLight color of the sunlight
  272. */
  273. void final_color_blend(video::SColor *result,
  274. const video::SColor &data, const video::SColorf &dayLight);
  275. // Retrieves the TileSpec of a face of a node
  276. // Adds MATERIAL_FLAG_CRACK if the node is cracked
  277. // TileSpec should be passed as reference due to the underlying TileFrame and its vector
  278. // TileFrame vector copy cost very much to client
  279. void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
  280. void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);
  281. /// Return bitset of the sides of the mesh that consist of solid nodes only
  282. /// Bits:
  283. /// 0 0 -Z +Z -X +X -Y +Y
  284. u8 get_solid_sides(MeshMakeData *data);