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