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"
37 struct MinimapMapblock;
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;
49 MeshMakeData(Client *client, bool use_shaders);
52 Copy block data manually (to allow optimizations by the caller)
54 void fillBlockDataBegin(const v3s16 &blockpos);
55 void fillBlockData(const v3s16 &block_offset, MapNode *data);
58 Copy central data directly from block, and other data from
61 void fill(MapBlock *block);
64 Set the (node) position of a crack
66 void setCrack(int crack_level, v3s16 crack_pos);
69 Enable or disable smooth lighting
71 void setSmoothLighting(bool smooth_lighting);
74 // represents a triangle as indexes into the vertex buffer in SMeshBuffer
78 scene::SMeshBuffer *buffer;
83 void updateAttributes()
85 v3f v1 = buffer->getPosition(p1);
86 v3f v2 = buffer->getPosition(p2);
87 v3f v3 = buffer->getPosition(p3);
89 centroid = (v1 + v2 + v3) / 3;
90 areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
93 v3f getNormal() const {
94 v3f v1 = buffer->getPosition(p1);
95 v3f v2 = buffer->getPosition(p2);
96 v3f v3 = buffer->getPosition(p3);
98 return (v2-v1).crossProduct(v3-v1);
103 * Implements a binary space partitioning tree
104 * See also: https://en.wikipedia.org/wiki/Binary_space_partitioning
106 class MapBlockBspTree
111 void buildTree(const std::vector<MeshTriangle> *triangles);
113 void traverse(v3f viewpoint, std::vector<s32> &output) const
115 traverse(root, viewpoint, output);
119 // Tree node definition;
124 std::vector<s32> triangle_refs;
128 TreeNode() = default;
129 TreeNode(v3f normal, v3f origin, const std::vector<s32> &triangle_refs, s32 front_ref, s32 back_ref) :
130 normal(normal), origin(origin), triangle_refs(triangle_refs), front_ref(front_ref), back_ref(back_ref)
135 s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
136 void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
138 const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
139 std::vector<TreeNode> nodes; // list of nodes
140 s32 root = -1; // index of the root node
143 class PartialMeshBuffer
146 PartialMeshBuffer(scene::SMeshBuffer *buffer, const std::vector<u16> &vertex_indexes) :
147 m_buffer(buffer), m_vertex_indexes(vertex_indexes)
150 scene::IMeshBuffer *getBuffer() const { return m_buffer; }
151 const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
153 void beforeDraw() const;
155 scene::SMeshBuffer *m_buffer;
156 std::vector<u16> m_vertex_indexes;
160 Holds a mesh for a mapblock.
162 Besides the SMesh*, this contains information used for animating
163 the vertex positions, colors and texture coordinates of the mesh.
165 - cracks [implemented]
166 - day/night transitions [implemented]
167 - animated flowing liquids [not implemented]
168 - animating vertex positions for e.g. axles [not implemented]
173 // Builds the mesh given
174 MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
177 // Main animation function, parameters:
178 // faraway: whether the block is far away from the camera (~50 nodes)
179 // time: the global animation time, 0 .. 60 (repeats every minute)
180 // daynight_ratio: 0 .. 1000
181 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
182 // Returns true if anything has been changed.
183 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
185 scene::IMesh *getMesh()
190 scene::IMesh *getMesh(u8 layer)
192 return m_mesh[layer];
195 MinimapMapblock *moveMinimapMapblock()
197 MinimapMapblock *p = m_minimap_mapblock;
198 m_minimap_mapblock = NULL;
202 bool isAnimationForced() const
204 return m_animation_force_timer == 0;
207 void decreaseAnimationForceTimer()
209 if(m_animation_force_timer > 0)
210 m_animation_force_timer--;
213 /// update transparent buffers to render towards the camera
214 void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
215 void consolidateTransparentBuffers();
217 /// get the list of transparent buffers
218 const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
220 return this->m_transparent_buffers;
224 struct AnimationInfo {
225 int frame; // last animation frame
230 scene::IMesh *m_mesh[MAX_TILE_LAYERS];
231 MinimapMapblock *m_minimap_mapblock;
232 ITextureSource *m_tsrc;
233 IShaderSource *m_shdrsrc;
235 bool m_enable_shaders;
238 // Must animate() be called before rendering?
239 bool m_has_animation;
240 int m_animation_force_timer;
242 // Animation info: cracks
243 // Last crack value passed to animate()
245 // Maps mesh and mesh buffer (i.e. material) indices to base texture names
246 std::map<std::pair<u8, u32>, std::string> m_crack_materials;
248 // Animation info: texture animation
249 // Maps mesh and mesh buffer indices to TileSpecs
250 // Keys are pairs of (mesh index, buffer index in the mesh)
251 std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
253 // Animation info: day/night transitions
254 // Last daynight_ratio value passed to animate()
255 u32 m_last_daynight_ratio;
256 // For each mesh and mesh buffer, stores pre-baked colors
257 // of sunlit vertices
258 // Keys are pairs of (mesh index, buffer index in the mesh)
259 std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
261 // list of all semitransparent triangles in the mapblock
262 std::vector<MeshTriangle> m_transparent_triangles;
263 // Binary Space Partitioning tree for the block
264 MapBlockBspTree m_bsp_tree;
265 // Ordered list of references to parts of transparent buffers to draw
266 std::vector<PartialMeshBuffer> m_transparent_buffers;
270 * Encodes light of a node.
271 * The result is not the final color, but a
272 * half-baked vertex color.
273 * You have to multiply the resulting color
274 * with the node's color.
276 * \param light the first 8 bits are day light,
277 * the last 8 bits are night light
278 * \param emissive_light amount of light the surface emits,
279 * from 0 to LIGHT_SUN.
281 video::SColor encode_light(u16 light, u8 emissive_light);
283 // Compute light at node
284 u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
285 u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
286 const NodeDefManager *ndef);
287 u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
288 u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
291 * Returns the sunlight's color from the current
294 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
297 * Gives the final SColor shown on screen.
299 * \param result output color
300 * \param light first 8 bits are day light, second 8 bits are
303 void final_color_blend(video::SColor *result,
304 u16 light, u32 daynight_ratio);
307 * Gives the final SColor shown on screen.
309 * \param result output color
310 * \param data the half-baked vertex color
311 * \param dayLight color of the sunlight
313 void final_color_blend(video::SColor *result,
314 const video::SColor &data, const video::SColorf &dayLight);
316 // Retrieves the TileSpec of a face of a node
317 // Adds MATERIAL_FLAG_CRACK if the node is cracked
318 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
319 // TileFrame vector copy cost very much to client
320 void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
321 void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);