mapblock_mesh.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "tile.h"
  20. #include "voxel.h"
  21. #include <map>
  22. class IGameDef;
  23. /*
  24. Mesh making stuff
  25. */
  26. class MapBlock;
  27. struct MeshMakeData
  28. {
  29. VoxelManipulator m_vmanip;
  30. v3s16 m_blockpos;
  31. v3s16 m_crack_pos_relative;
  32. v3s16 m_highlighted_pos_relative;
  33. bool m_smooth_lighting;
  34. bool m_show_hud;
  35. video::SColor m_highlight_mesh_color;
  36. IGameDef *m_gamedef;
  37. MeshMakeData(IGameDef *gamedef);
  38. /*
  39. Copy central data directly from block, and other data from
  40. parent of block.
  41. */
  42. void fill(MapBlock *block);
  43. /*
  44. Set up with only a single node at (1,1,1)
  45. */
  46. void fillSingleNode(MapNode *node);
  47. /*
  48. Set the (node) position of a crack
  49. */
  50. void setCrack(int crack_level, v3s16 crack_pos);
  51. /*
  52. Set the highlighted node position
  53. */
  54. void setHighlighted(v3s16 highlighted_pos, bool show_hud);
  55. /*
  56. Enable or disable smooth lighting
  57. */
  58. void setSmoothLighting(bool smooth_lighting);
  59. };
  60. /*
  61. Holds a mesh for a mapblock.
  62. Besides the SMesh*, this contains information used for animating
  63. the vertex positions, colors and texture coordinates of the mesh.
  64. For example:
  65. - cracks [implemented]
  66. - day/night transitions [implemented]
  67. - animated flowing liquids [not implemented]
  68. - animating vertex positions for e.g. axles [not implemented]
  69. */
  70. class MapBlockMesh
  71. {
  72. public:
  73. // Builds the mesh given
  74. MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
  75. ~MapBlockMesh();
  76. // Main animation function, parameters:
  77. // faraway: whether the block is far away from the camera (~50 nodes)
  78. // time: the global animation time, 0 .. 60 (repeats every minute)
  79. // daynight_ratio: 0 .. 1000
  80. // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
  81. // Returns true if anything has been changed.
  82. bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
  83. scene::SMesh* getMesh()
  84. {
  85. return m_mesh;
  86. }
  87. bool isAnimationForced() const
  88. {
  89. return m_animation_force_timer == 0;
  90. }
  91. void decreaseAnimationForceTimer()
  92. {
  93. if(m_animation_force_timer > 0)
  94. m_animation_force_timer--;
  95. }
  96. void updateCameraOffset(v3s16 camera_offset);
  97. private:
  98. scene::SMesh *m_mesh;
  99. IGameDef *m_gamedef;
  100. bool m_enable_shaders;
  101. bool m_enable_highlighting;
  102. video::SColor m_highlight_mesh_color;
  103. // Must animate() be called before rendering?
  104. bool m_has_animation;
  105. int m_animation_force_timer;
  106. // Animation info: cracks
  107. // Last crack value passed to animate()
  108. int m_last_crack;
  109. // Maps mesh buffer (i.e. material) indices to base texture names
  110. std::map<u32, std::string> m_crack_materials;
  111. std::list<u32> m_highlighted_materials;
  112. // Animation info: texture animationi
  113. // Maps meshbuffers to TileSpecs
  114. std::map<u32, TileSpec> m_animation_tiles;
  115. std::map<u32, int> m_animation_frames; // last animation frame
  116. std::map<u32, int> m_animation_frame_offsets;
  117. // Animation info: day/night transitions
  118. // Last daynight_ratio value passed to animate()
  119. u32 m_last_daynight_ratio;
  120. // For each meshbuffer, maps vertex indices to (day,night) pairs
  121. std::map<u32, std::map<u32, std::pair<u8, u8> > > m_daynight_diffs;
  122. // Camera offset info -> do we have to translate the mesh?
  123. v3s16 m_camera_offset;
  124. };
  125. /*
  126. This is used because CMeshBuffer::append() is very slow
  127. */
  128. struct PreMeshBuffer
  129. {
  130. TileSpec tile;
  131. std::vector<u16> indices;
  132. std::vector<video::S3DVertex> vertices;
  133. };
  134. struct MeshCollector
  135. {
  136. std::vector<PreMeshBuffer> prebuffers;
  137. void append(const TileSpec &material,
  138. const video::S3DVertex *vertices, u32 numVertices,
  139. const u16 *indices, u32 numIndices);
  140. };
  141. // This encodes
  142. // alpha in the A channel of the returned SColor
  143. // day light (0-255) in the R channel of the returned SColor
  144. // night light (0-255) in the G channel of the returned SColor
  145. // light source (0-255) in the B channel of the returned SColor
  146. inline video::SColor MapBlock_LightColor(u8 alpha, u16 light, u8 light_source=0)
  147. {
  148. return video::SColor(alpha, (light & 0xff), (light >> 8), light_source);
  149. }
  150. // Compute light at node
  151. u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
  152. u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
  153. u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
  154. // Retrieves the TileSpec of a face of a node
  155. // Adds MATERIAL_FLAG_CRACK if the node is cracked
  156. TileSpec getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data);
  157. TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data);
  158. #endif