mapblock_mesh.h 9.6 KB

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