]> git.lizzy.rs Git - minetest.git/blob - src/mapblock_mesh.h
Optimize headers (part 2) (#6272)
[minetest.git] / src / 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 <map>
26
27 class Client;
28 class IShaderSource;
29
30 /*
31         Mesh making stuff
32 */
33
34
35 class MapBlock;
36 struct MinimapMapblock;
37
38 struct MeshMakeData
39 {
40         VoxelManipulator m_vmanip;
41         v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
42         v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
43         bool m_smooth_lighting = false;
44
45         Client *m_client;
46         bool m_use_shaders;
47         bool m_use_tangent_vertices;
48
49         MeshMakeData(Client *client, bool use_shaders,
50                         bool use_tangent_vertices = false);
51
52         /*
53                 Copy block data manually (to allow optimizations by the caller)
54         */
55         void fillBlockDataBegin(const v3s16 &blockpos);
56         void fillBlockData(const v3s16 &block_offset, MapNode *data);
57
58         /*
59                 Copy central data directly from block, and other data from
60                 parent of block.
61         */
62         void fill(MapBlock *block);
63
64         /*
65                 Set up with only a single node at (1,1,1)
66         */
67         void fillSingleNode(MapNode *node);
68
69         /*
70                 Set the (node) position of a crack
71         */
72         void setCrack(int crack_level, v3s16 crack_pos);
73
74         /*
75                 Enable or disable smooth lighting
76         */
77         void setSmoothLighting(bool smooth_lighting);
78 };
79
80 /*
81         Holds a mesh for a mapblock.
82
83         Besides the SMesh*, this contains information used for animating
84         the vertex positions, colors and texture coordinates of the mesh.
85         For example:
86         - cracks [implemented]
87         - day/night transitions [implemented]
88         - animated flowing liquids [not implemented]
89         - animating vertex positions for e.g. axles [not implemented]
90 */
91 class MapBlockMesh
92 {
93 public:
94         // Builds the mesh given
95         MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
96         ~MapBlockMesh();
97
98         // Main animation function, parameters:
99         //   faraway: whether the block is far away from the camera (~50 nodes)
100         //   time: the global animation time, 0 .. 60 (repeats every minute)
101         //   daynight_ratio: 0 .. 1000
102         //   crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
103         // Returns true if anything has been changed.
104         bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
105
106         scene::IMesh *getMesh()
107         {
108                 return m_mesh[0];
109         }
110
111         scene::IMesh *getMesh(u8 layer)
112         {
113                 return m_mesh[layer];
114         }
115
116         MinimapMapblock *moveMinimapMapblock()
117         {
118                 MinimapMapblock *p = m_minimap_mapblock;
119                 m_minimap_mapblock = NULL;
120                 return p;
121         }
122
123         bool isAnimationForced() const
124         {
125                 return m_animation_force_timer == 0;
126         }
127
128         void decreaseAnimationForceTimer()
129         {
130                 if(m_animation_force_timer > 0)
131                         m_animation_force_timer--;
132         }
133
134         void updateCameraOffset(v3s16 camera_offset);
135
136 private:
137         scene::IMesh *m_mesh[MAX_TILE_LAYERS];
138         MinimapMapblock *m_minimap_mapblock;
139         ITextureSource *m_tsrc;
140         IShaderSource *m_shdrsrc;
141
142         bool m_enable_shaders;
143         bool m_use_tangent_vertices;
144         bool m_enable_vbo;
145
146         // Must animate() be called before rendering?
147         bool m_has_animation;
148         int m_animation_force_timer;
149
150         // Animation info: cracks
151         // Last crack value passed to animate()
152         int m_last_crack;
153         // Maps mesh and mesh buffer (i.e. material) indices to base texture names
154         std::map<std::pair<u8, u32>, std::string> m_crack_materials;
155
156         // Animation info: texture animationi
157         // Maps mesh and mesh buffer indices to TileSpecs
158         // Keys are pairs of (mesh index, buffer index in the mesh)
159         std::map<std::pair<u8, u32>, TileLayer> m_animation_tiles;
160         std::map<std::pair<u8, u32>, int> m_animation_frames; // last animation frame
161         std::map<std::pair<u8, u32>, int> m_animation_frame_offsets;
162
163         // Animation info: day/night transitions
164         // Last daynight_ratio value passed to animate()
165         u32 m_last_daynight_ratio;
166         // For each mesh and mesh buffer, stores pre-baked colors
167         // of sunlit vertices
168         // Keys are pairs of (mesh index, buffer index in the mesh)
169         std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
170
171         // Camera offset info -> do we have to translate the mesh?
172         v3s16 m_camera_offset;
173 };
174
175
176
177 /*
178         This is used because CMeshBuffer::append() is very slow
179 */
180 struct PreMeshBuffer
181 {
182         TileLayer layer;
183         std::vector<u16> indices;
184         std::vector<video::S3DVertex> vertices;
185         std::vector<video::S3DVertexTangents> tangent_vertices;
186 };
187
188 struct MeshCollector
189 {
190         std::vector<PreMeshBuffer> prebuffers[MAX_TILE_LAYERS];
191         bool m_use_tangent_vertices;
192
193         MeshCollector(bool use_tangent_vertices):
194                 m_use_tangent_vertices(use_tangent_vertices)
195         {
196         }
197
198         void append(const TileSpec &material,
199                                 const video::S3DVertex *vertices, u32 numVertices,
200                                 const u16 *indices, u32 numIndices);
201         void append(const TileLayer &material,
202                         const video::S3DVertex *vertices, u32 numVertices,
203                         const u16 *indices, u32 numIndices, u8 layernum);
204         void append(const TileSpec &material,
205                                 const video::S3DVertex *vertices, u32 numVertices,
206                                 const u16 *indices, u32 numIndices, v3f pos,
207                                 video::SColor c, u8 light_source);
208         void append(const TileLayer &material,
209                         const video::S3DVertex *vertices, u32 numVertices,
210                         const u16 *indices, u32 numIndices, v3f pos,
211                         video::SColor c, u8 light_source, u8 layernum);
212         /*!
213          * Colorizes all vertices in the collector.
214          */
215         void applyTileColors();
216 };
217
218 /*!
219  * Encodes light of a node.
220  * The result is not the final color, but a
221  * half-baked vertex color.
222  * You have to multiply the resulting color
223  * with the node's color.
224  *
225  * \param light the first 8 bits are day light,
226  * the last 8 bits are night light
227  * \param emissive_light amount of light the surface emits,
228  * from 0 to LIGHT_SUN.
229  */
230 video::SColor encode_light(u16 light, u8 emissive_light);
231
232 // Compute light at node
233 u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
234 u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
235 u16 getSmoothLight(v3s16 p, v3s16 corner, MeshMakeData *data);
236
237 /*!
238  * Returns the sunlight's color from the current
239  * day-night ratio.
240  */
241 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
242
243 /*!
244  * Gives the final  SColor shown on screen.
245  *
246  * \param result output color
247  * \param light first 8 bits are day light, second 8 bits are
248  * night light
249  */
250 void final_color_blend(video::SColor *result,
251                 u16 light, u32 daynight_ratio);
252
253 /*!
254  * Gives the final  SColor shown on screen.
255  *
256  * \param result output color
257  * \param data the half-baked vertex color
258  * \param dayLight color of the sunlight
259  */
260 void final_color_blend(video::SColor *result,
261                 const video::SColor &data, const video::SColorf &dayLight);
262
263 // Retrieves the TileSpec of a face of a node
264 // Adds MATERIAL_FLAG_CRACK if the node is cracked
265 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
266 // TileFrame vector copy cost very much to client
267 void getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
268 void getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data, TileSpec &tile);