]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/mapblock_mesh.h
72d12803825701a4b613a80453999950b6d4c12d
[dragonfireclient.git] / src / client / mapblock_mesh.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "client/tile.h"
24 #include "voxel.h"
25 #include <array>
26 #include <map>
27
28 class Client;
29 class IShaderSource;
30
31 /*
32         Mesh making stuff
33 */
34
35
36 class MapBlock;
37 struct MinimapMapblock;
38
39 struct MeshMakeData
40 {
41         VoxelManipulator m_vmanip;
42         v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
43         v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
44         bool m_smooth_lighting = false;
45
46         Client *m_client;
47         bool m_use_shaders;
48
49         MeshMakeData(Client *client, bool use_shaders);
50
51         /*
52                 Copy block data manually (to allow optimizations by the caller)
53         */
54         void fillBlockDataBegin(const v3s16 &blockpos);
55         void fillBlockData(const v3s16 &block_offset, MapNode *data);
56
57         /*
58                 Copy central data directly from block, and other data from
59                 parent of block.
60         */
61         void fill(MapBlock *block);
62
63         /*
64                 Set the (node) position of a crack
65         */
66         void setCrack(int crack_level, v3s16 crack_pos);
67
68         /*
69                 Enable or disable smooth lighting
70         */
71         void setSmoothLighting(bool smooth_lighting);
72 };
73
74 // represents a triangle as indexes into the vertex buffer in SMeshBuffer
75 class MeshTriangle
76 {
77 public:
78         scene::SMeshBuffer *buffer;
79         u16 p1, p2, p3;
80         v3f centroid;
81         float areaSQ;
82
83         void updateAttributes()
84         {
85                 v3f v1 = buffer->getPosition(p1);
86                 v3f v2 = buffer->getPosition(p2);
87                 v3f v3 = buffer->getPosition(p3);
88
89                 centroid = (v1 + v2 + v3) / 3;
90                 areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
91         }
92
93         v3f getNormal() const {
94                 v3f v1 = buffer->getPosition(p1);
95                 v3f v2 = buffer->getPosition(p2);
96                 v3f v3 = buffer->getPosition(p3);
97
98                 return (v2-v1).crossProduct(v3-v1);
99         }
100 };
101
102 /**
103  * Implements a binary space partitioning tree 
104  * See also: https://en.wikipedia.org/wiki/Binary_space_partitioning
105  */
106 class MapBlockBspTree
107 {
108 public:
109         MapBlockBspTree() {}
110
111         void buildTree(const std::vector<MeshTriangle> *triangles);
112
113         void traverse(v3f viewpoint, std::vector<s32> &output) const
114         {
115                 traverse(root, viewpoint, output);
116         }
117
118 private:
119         // Tree node definition;
120         struct TreeNode
121         {
122                 v3f normal;
123                 v3f origin;
124                 std::vector<s32> triangle_refs;
125                 s32 front_ref;
126                 s32 back_ref;
127
128                 TreeNode() = default;
129                 TreeNode(v3f normal, v3f origin, const std::vector<s32> &triangle_refs, s32 front_ref, s32 back_ref) :
130                                 normal(normal), origin(origin), triangle_refs(triangle_refs), front_ref(front_ref), back_ref(back_ref)
131                 {}
132         };
133
134
135         s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
136         void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
137
138         const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
139         std::vector<TreeNode> nodes; // list of nodes
140         s32 root = -1; // index of the root node
141 };
142
143 class PartialMeshBuffer
144 {
145 public:
146         PartialMeshBuffer(scene::SMeshBuffer *buffer, const std::vector<u16> &vertex_indexes) :
147                         m_buffer(buffer), m_vertex_indexes(vertex_indexes)
148         {}
149
150         scene::IMeshBuffer *getBuffer() const { return m_buffer; }
151         const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
152
153         void beforeDraw() const;
154 private:
155         scene::SMeshBuffer *m_buffer;
156         std::vector<u16> m_vertex_indexes;
157 };
158
159 /*
160         Holds a mesh for a mapblock.
161
162         Besides the SMesh*, this contains information used for animating
163         the vertex positions, colors and texture coordinates of the mesh.
164         For example:
165         - cracks [implemented]
166         - day/night transitions [implemented]
167         - animated flowing liquids [not implemented]
168         - animating vertex positions for e.g. axles [not implemented]
169 */
170 class MapBlockMesh
171 {
172 public:
173         // Builds the mesh given
174         MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
175         ~MapBlockMesh();
176
177         // Main animation function, parameters:
178         //   faraway: whether the block is far away from the camera (~50 nodes)
179         //   time: the global animation time, 0 .. 60 (repeats every minute)
180         //   daynight_ratio: 0 .. 1000
181         //   crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
182         // Returns true if anything has been changed.
183         bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
184
185         scene::IMesh *getMesh()
186         {
187                 return m_mesh[0];
188         }
189
190         scene::IMesh *getMesh(u8 layer)
191         {
192                 return m_mesh[layer];
193         }
194
195         MinimapMapblock *moveMinimapMapblock()
196         {
197                 MinimapMapblock *p = m_minimap_mapblock;
198                 m_minimap_mapblock = NULL;
199                 return p;
200         }
201
202         bool isAnimationForced() const
203         {
204                 return m_animation_force_timer == 0;
205         }
206
207         void decreaseAnimationForceTimer()
208         {
209                 if(m_animation_force_timer > 0)
210                         m_animation_force_timer--;
211         }
212
213         /// update transparent buffers to render towards the camera
214         void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
215         void consolidateTransparentBuffers();
216
217         /// get the list of transparent buffers
218         const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
219         {
220                 return this->m_transparent_buffers;
221         }
222
223 private:
224         struct AnimationInfo {
225                 int frame; // last animation frame
226                 int frame_offset;
227                 TileLayer tile;
228         };
229
230         scene::IMesh *m_mesh[MAX_TILE_LAYERS];
231         MinimapMapblock *m_minimap_mapblock;
232         ITextureSource *m_tsrc;
233         IShaderSource *m_shdrsrc;
234
235         bool m_enable_shaders;
236         bool m_enable_vbo;
237
238         // Must animate() be called before rendering?
239         bool m_has_animation;
240         int m_animation_force_timer;
241
242         // Animation info: cracks
243         // Last crack value passed to animate()
244         int m_last_crack;
245         // Maps mesh and mesh buffer (i.e. material) indices to base texture names
246         std::map<std::pair<u8, u32>, std::string> m_crack_materials;
247
248         // Animation info: texture animation
249         // Maps mesh and mesh buffer indices to TileSpecs
250         // Keys are pairs of (mesh index, buffer index in the mesh)
251         std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
252
253         // Animation info: day/night transitions
254         // Last daynight_ratio value passed to animate()
255         u32 m_last_daynight_ratio;
256         // For each mesh and mesh buffer, stores pre-baked colors
257         // of sunlit vertices
258         // Keys are pairs of (mesh index, buffer index in the mesh)
259         std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
260
261         // list of all semitransparent triangles in the mapblock
262         std::vector<MeshTriangle> m_transparent_triangles;
263         // Binary Space Partitioning tree for the block
264         MapBlockBspTree m_bsp_tree;
265         // Ordered list of references to parts of transparent buffers to draw
266         std::vector<PartialMeshBuffer> m_transparent_buffers;
267 };
268
269 /*!
270  * Encodes light of a node.
271  * The result is not the final color, but a
272  * half-baked vertex color.
273  * You have to multiply the resulting color
274  * with the node's color.
275  *
276  * \param light the first 8 bits are day light,
277  * the last 8 bits are night light
278  * \param emissive_light amount of light the surface emits,
279  * from 0 to LIGHT_SUN.
280  */
281 video::SColor encode_light(u16 light, u8 emissive_light);
282
283 // Compute light at node
284 u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
285 u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
286         const NodeDefManager *ndef);
287 u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
288 u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
289
290 /*!
291  * Returns the sunlight's color from the current
292  * day-night ratio.
293  */
294 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
295
296 /*!
297  * Gives the final  SColor shown on screen.
298  *
299  * \param result output color
300  * \param light first 8 bits are day light, second 8 bits are
301  * night light
302  */
303 void final_color_blend(video::SColor *result,
304                 u16 light, u32 daynight_ratio);
305
306 /*!
307  * Gives the final  SColor shown on screen.
308  *
309  * \param result output color
310  * \param data the half-baked vertex color
311  * \param dayLight color of the sunlight
312  */
313 void final_color_blend(video::SColor *result,
314                 const video::SColor &data, const video::SColorf &dayLight);
315
316 // Retrieves the TileSpec of a face of a node
317 // Adds MATERIAL_FLAG_CRACK if the node is cracked
318 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
319 // TileFrame vector copy cost very much to client
320 void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
321 void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);