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