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