3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "irrlichttypes_extrabloated.h"
23 #include "client/tile.h"
36 struct MinimapMapblock;
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;
47 bool m_use_tangent_vertices;
49 MeshMakeData(Client *client, bool use_shaders,
50 bool use_tangent_vertices = false);
53 Copy block data manually (to allow optimizations by the caller)
55 void fillBlockDataBegin(const v3s16 &blockpos);
56 void fillBlockData(const v3s16 &block_offset, MapNode *data);
59 Copy central data directly from block, and other data from
62 void fill(MapBlock *block);
65 Set up with only a single node at (1,1,1)
67 void fillSingleNode(MapNode *node);
70 Set the (node) position of a crack
72 void setCrack(int crack_level, v3s16 crack_pos);
75 Enable or disable smooth lighting
77 void setSmoothLighting(bool smooth_lighting);
81 Holds a mesh for a mapblock.
83 Besides the SMesh*, this contains information used for animating
84 the vertex positions, colors and texture coordinates of the mesh.
86 - cracks [implemented]
87 - day/night transitions [implemented]
88 - animated flowing liquids [not implemented]
89 - animating vertex positions for e.g. axles [not implemented]
94 // Builds the mesh given
95 MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
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);
106 scene::IMesh *getMesh()
111 scene::IMesh *getMesh(u8 layer)
113 return m_mesh[layer];
116 MinimapMapblock *moveMinimapMapblock()
118 MinimapMapblock *p = m_minimap_mapblock;
119 m_minimap_mapblock = NULL;
123 bool isAnimationForced() const
125 return m_animation_force_timer == 0;
128 void decreaseAnimationForceTimer()
130 if(m_animation_force_timer > 0)
131 m_animation_force_timer--;
134 void updateCameraOffset(v3s16 camera_offset);
137 scene::IMesh *m_mesh[MAX_TILE_LAYERS];
138 MinimapMapblock *m_minimap_mapblock;
139 ITextureSource *m_tsrc;
140 IShaderSource *m_shdrsrc;
142 bool m_enable_shaders;
143 bool m_use_tangent_vertices;
146 // Must animate() be called before rendering?
147 bool m_has_animation;
148 int m_animation_force_timer;
150 // Animation info: cracks
151 // Last crack value passed to animate()
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;
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;
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;
171 // Camera offset info -> do we have to translate the mesh?
172 v3s16 m_camera_offset;
178 This is used because CMeshBuffer::append() is very slow
183 std::vector<u16> indices;
184 std::vector<video::S3DVertex> vertices;
185 std::vector<video::S3DVertexTangents> tangent_vertices;
190 std::vector<PreMeshBuffer> prebuffers[MAX_TILE_LAYERS];
191 bool m_use_tangent_vertices;
193 MeshCollector(bool use_tangent_vertices):
194 m_use_tangent_vertices(use_tangent_vertices)
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 bool use_scale = false);
205 void append(const TileSpec &material,
206 const video::S3DVertex *vertices, u32 numVertices,
207 const u16 *indices, u32 numIndices, v3f pos,
208 video::SColor c, u8 light_source);
209 void append(const TileLayer &material,
210 const video::S3DVertex *vertices, u32 numVertices,
211 const u16 *indices, u32 numIndices, v3f pos,
212 video::SColor c, u8 light_source, u8 layernum,
213 bool use_scale = false);
215 * Colorizes all vertices in the collector.
217 void applyTileColors();
221 * Encodes light of a node.
222 * The result is not the final color, but a
223 * half-baked vertex color.
224 * You have to multiply the resulting color
225 * with the node's color.
227 * \param light the first 8 bits are day light,
228 * the last 8 bits are night light
229 * \param emissive_light amount of light the surface emits,
230 * from 0 to LIGHT_SUN.
232 video::SColor encode_light(u16 light, u8 emissive_light);
234 // Compute light at node
235 u16 getInteriorLight(MapNode n, s32 increment, INodeDefManager *ndef);
236 u16 getFaceLight(MapNode n, MapNode n2, v3s16 face_dir, INodeDefManager *ndef);
237 u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
238 u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
241 * Returns the sunlight's color from the current
244 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
247 * Gives the final SColor shown on screen.
249 * \param result output color
250 * \param light first 8 bits are day light, second 8 bits are
253 void final_color_blend(video::SColor *result,
254 u16 light, u32 daynight_ratio);
257 * Gives the final SColor shown on screen.
259 * \param result output color
260 * \param data the half-baked vertex color
261 * \param dayLight color of the sunlight
263 void final_color_blend(video::SColor *result,
264 const video::SColor &data, const video::SColorf &dayLight);
266 // Retrieves the TileSpec of a face of a node
267 // Adds MATERIAL_FLAG_CRACK if the node is cracked
268 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
269 // TileFrame vector copy cost very much to client
270 void getNodeTileN(MapNode mn, v3s16 p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
271 void getNodeTile(MapNode mn, v3s16 p, v3s16 dir, MeshMakeData *data, TileSpec &tile);