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
146 * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
147 * indices with the same vertex buffer.
149 * Irrlicht does not currently support this: `CMeshBuffer` ties together a single vertex buffer
150 * and a single index buffer. There's no way to share these between mesh buffers.
153 class PartialMeshBuffer
156 PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indexes) :
157 m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes))
160 scene::IMeshBuffer *getBuffer() const { return m_buffer; }
161 const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
163 void beforeDraw() const;
164 void afterDraw() const;
166 scene::SMeshBuffer *m_buffer;
167 mutable std::vector<u16> m_vertex_indexes;
171 Holds a mesh for a mapblock.
173 Besides the SMesh*, this contains information used for animating
174 the vertex positions, colors and texture coordinates of the mesh.
176 - cracks [implemented]
177 - day/night transitions [implemented]
178 - animated flowing liquids [not implemented]
179 - animating vertex positions for e.g. axles [not implemented]
184 // Builds the mesh given
185 MapBlockMesh(MeshMakeData *data, v3s16 camera_offset);
188 // Main animation function, parameters:
189 // faraway: whether the block is far away from the camera (~50 nodes)
190 // time: the global animation time, 0 .. 60 (repeats every minute)
191 // daynight_ratio: 0 .. 1000
192 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
193 // Returns true if anything has been changed.
194 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
196 scene::IMesh *getMesh()
201 scene::IMesh *getMesh(u8 layer)
203 return m_mesh[layer];
206 MinimapMapblock *moveMinimapMapblock()
208 MinimapMapblock *p = m_minimap_mapblock;
209 m_minimap_mapblock = NULL;
213 bool isAnimationForced() const
215 return m_animation_force_timer == 0;
218 void decreaseAnimationForceTimer()
220 if(m_animation_force_timer > 0)
221 m_animation_force_timer--;
224 /// update transparent buffers to render towards the camera
225 void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
226 void consolidateTransparentBuffers();
228 /// get the list of transparent buffers
229 const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
231 return this->m_transparent_buffers;
235 struct AnimationInfo {
236 int frame; // last animation frame
241 scene::IMesh *m_mesh[MAX_TILE_LAYERS];
242 MinimapMapblock *m_minimap_mapblock;
243 ITextureSource *m_tsrc;
244 IShaderSource *m_shdrsrc;
246 bool m_enable_shaders;
249 // Must animate() be called before rendering?
250 bool m_has_animation;
251 int m_animation_force_timer;
253 // Animation info: cracks
254 // Last crack value passed to animate()
256 // Maps mesh and mesh buffer (i.e. material) indices to base texture names
257 std::map<std::pair<u8, u32>, std::string> m_crack_materials;
259 // Animation info: texture animation
260 // Maps mesh and mesh buffer indices to TileSpecs
261 // Keys are pairs of (mesh index, buffer index in the mesh)
262 std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
264 // Animation info: day/night transitions
265 // Last daynight_ratio value passed to animate()
266 u32 m_last_daynight_ratio;
267 // For each mesh and mesh buffer, stores pre-baked colors
268 // of sunlit vertices
269 // Keys are pairs of (mesh index, buffer index in the mesh)
270 std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
272 // list of all semitransparent triangles in the mapblock
273 std::vector<MeshTriangle> m_transparent_triangles;
274 // Binary Space Partitioning tree for the block
275 MapBlockBspTree m_bsp_tree;
276 // Ordered list of references to parts of transparent buffers to draw
277 std::vector<PartialMeshBuffer> m_transparent_buffers;
281 * Encodes light of a node.
282 * The result is not the final color, but a
283 * half-baked vertex color.
284 * You have to multiply the resulting color
285 * with the node's color.
287 * \param light the first 8 bits are day light,
288 * the last 8 bits are night light
289 * \param emissive_light amount of light the surface emits,
290 * from 0 to LIGHT_SUN.
292 video::SColor encode_light(u16 light, u8 emissive_light);
294 // Compute light at node
295 u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
296 u16 getFaceLight(MapNode n, MapNode n2, const v3s16 &face_dir,
297 const NodeDefManager *ndef);
298 u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
299 u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
302 * Returns the sunlight's color from the current
305 void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
308 * Gives the final SColor shown on screen.
310 * \param result output color
311 * \param light first 8 bits are day light, second 8 bits are
314 void final_color_blend(video::SColor *result,
315 u16 light, u32 daynight_ratio);
318 * Gives the final SColor shown on screen.
320 * \param result output color
321 * \param data the half-baked vertex color
322 * \param dayLight color of the sunlight
324 void final_color_blend(video::SColor *result,
325 const video::SColor &data, const video::SColorf &dayLight);
327 // Retrieves the TileSpec of a face of a node
328 // Adds MATERIAL_FLAG_CRACK if the node is cracked
329 // TileSpec should be passed as reference due to the underlying TileFrame and its vector
330 // TileFrame vector copy cost very much to client
331 void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
332 void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);