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