]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/mapblock_mesh.h
Fixes needed to use irrArray backed by std::vector (#12263)
[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 /*
144  * PartialMeshBuffer
145  *
146  * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
147  * indices with the same vertex buffer.
148  *
149  * Irrlicht does not currently support this: `CMeshBuffer` ties together a single vertex buffer
150  * and a single index buffer. There's no way to share these between mesh buffers.
151  *
152  */
153 class PartialMeshBuffer
154 {
155 public:
156         PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indexes) :
157                         m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes))
158         {}
159
160         scene::IMeshBuffer *getBuffer() const { return m_buffer; }
161         const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
162
163         void beforeDraw() const;
164         void afterDraw() const;
165 private:
166         scene::SMeshBuffer *m_buffer;
167         mutable std::vector<u16> m_vertex_indexes;
168 };
169
170 /*
171         Holds a mesh for a mapblock.
172
173         Besides the SMesh*, this contains information used for animating
174         the vertex positions, colors and texture coordinates of the mesh.
175         For example:
176         - cracks [implemented]
177         - day/night transitions [implemented]
178         - animated flowing liquids [not implemented]
179         - animating vertex positions for e.g. axles [not implemented]
180 */
181 class MapBlockMesh
182 {
183 public:
184         // Builds the mesh given
185         MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
186         ~MapBlockMesh();
187
188         // Main animation function, parameters:
189         //   faraway: whether the block is far away from the camera (~50 nodes)
190         //   time: the global animation time, 0 .. 60 (repeats every minute)
191         //   daynight_ratio: 0 .. 1000
192         //   crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
193         // Returns true if anything has been changed.
194         bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
195
196         scene::IMesh *getMesh()
197         {
198                 return m_mesh[0];
199         }
200
201         scene::IMesh *getMesh(u8 layer)
202         {
203                 return m_mesh[layer];
204         }
205
206         MinimapMapblock *moveMinimapMapblock()
207         {
208                 MinimapMapblock *p = m_minimap_mapblock;
209                 m_minimap_mapblock = NULL;
210                 return p;
211         }
212
213         bool isAnimationForced() const
214         {
215                 return m_animation_force_timer == 0;
216         }
217
218         void decreaseAnimationForceTimer()
219         {
220                 if(m_animation_force_timer > 0)
221                         m_animation_force_timer--;
222         }
223
224         /// update transparent buffers to render towards the camera
225         void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
226         void consolidateTransparentBuffers();
227
228         /// get the list of transparent buffers
229         const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
230         {
231                 return this->m_transparent_buffers;
232         }
233
234 private:
235         struct AnimationInfo {
236                 int frame; // last animation frame
237                 int frame_offset;
238                 TileLayer tile;
239         };
240
241         scene::IMesh *m_mesh[MAX_TILE_LAYERS];
242         MinimapMapblock *m_minimap_mapblock;
243         ITextureSource *m_tsrc;
244         IShaderSource *m_shdrsrc;
245
246         bool m_enable_shaders;
247         bool m_enable_vbo;
248
249         // Must animate() be called before rendering?
250         bool m_has_animation;
251         int m_animation_force_timer;
252
253         // Animation info: cracks
254         // Last crack value passed to animate()
255         int m_last_crack;
256         // Maps mesh and mesh buffer (i.e. material) indices to base texture names
257         std::map<std::pair<u8, u32>, std::string> m_crack_materials;
258
259         // Animation info: texture animation
260         // Maps mesh and mesh buffer indices to TileSpecs
261         // Keys are pairs of (mesh index, buffer index in the mesh)
262         std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
263
264         // Animation info: day/night transitions
265         // Last daynight_ratio value passed to animate()
266         u32 m_last_daynight_ratio;
267         // For each mesh and mesh buffer, stores pre-baked colors
268         // of sunlit vertices
269         // Keys are pairs of (mesh index, buffer index in the mesh)
270         std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
271
272         // list of all semitransparent triangles in the mapblock
273         std::vector<MeshTriangle> m_transparent_triangles;
274         // Binary Space Partitioning tree for the block
275         MapBlockBspTree m_bsp_tree;
276         // Ordered list of references to parts of transparent buffers to draw
277         std::vector<PartialMeshBuffer> m_transparent_buffers;
278 };
279
280 /*!
281  * Encodes light of a node.
282  * The result is not the final color, but a
283  * half-baked vertex color.
284  * You have to multiply the resulting color
285  * with the node's color.
286  *
287  * \param light the first 8 bits are day light,
288  * the last 8 bits are night light
289  * \param emissive_light amount of light the surface emits,
290  * from 0 to LIGHT_SUN.
291  */
292 video::SColor encode_light(u16 light, u8 emissive_light);
293
294 // Compute light at node
295 u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
296 u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
297         const NodeDefManager *ndef);
298 u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
299 u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
300
301 /*!
302  * Returns the sunlight's color from the current
303  * day-night ratio.
304  */
305 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
306
307 /*!
308  * Gives the final  SColor shown on screen.
309  *
310  * \param result output color
311  * \param light first 8 bits are day light, second 8 bits are
312  * night light
313  */
314 void final_color_blend(video::SColor *result,
315                 u16 light, u32 daynight_ratio);
316
317 /*!
318  * Gives the final  SColor shown on screen.
319  *
320  * \param result output color
321  * \param data the half-baked vertex color
322  * \param dayLight color of the sunlight
323  */
324 void final_color_blend(video::SColor *result,
325                 const video::SColor &data, const video::SColorf &dayLight);
326
327 // Retrieves the TileSpec of a face of a node
328 // Adds MATERIAL_FLAG_CRACK if the node is cracked
329 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
330 // TileFrame vector copy cost very much to client
331 void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
332 void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);