Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
mapblock_mesh.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
23#include "util/numeric.h"
24#include "client/tile.h"
25#include "voxel.h"
26#include <array>
27#include <map>
28#include <unordered_map>
29
30class Client;
31class NodeDefManager;
32class IShaderSource;
33class ITextureSource;
34
35/*
36 Mesh making stuff
37*/
38
39
40class MapBlock;
41struct MinimapMapblock;
42
44{
46 v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
47 v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
48 bool m_smooth_lighting = false;
50
53
54 MeshMakeData(const NodeDefManager *ndef, u16 side_length, bool use_shaders);
55
56 /*
57 Copy block data manually (to allow optimizations by the caller)
58 */
59 void fillBlockDataBegin(const v3s16 &blockpos);
60 void fillBlockData(const v3s16 &bp, MapNode *data);
61
62 /*
63 Set the (node) position of a crack
64 */
65 void setCrack(int crack_level, v3s16 crack_pos);
66
67 /*
68 Enable or disable smooth lighting
69 */
70 void setSmoothLighting(bool smooth_lighting);
71};
72
73// represents a triangle as indexes into the vertex buffer in SMeshBuffer
75{
76public:
77 scene::SMeshBuffer *buffer;
78 u16 p1, p2, p3;
80 float areaSQ;
81
83 {
84 v3f v1 = buffer->getPosition(p1);
85 v3f v2 = buffer->getPosition(p2);
86 v3f v3 = buffer->getPosition(p3);
87
88 centroid = (v1 + v2 + v3) / 3;
89 areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
90 }
91
92 v3f getNormal() const {
93 v3f v1 = buffer->getPosition(p1);
94 v3f v2 = buffer->getPosition(p2);
95 v3f v3 = buffer->getPosition(p3);
96
97 return (v2-v1).crossProduct(v3-v1);
98 }
99};
100
106{
107public:
109
110 void buildTree(const std::vector<MeshTriangle> *triangles, u16 side_lingth);
111
112 void traverse(v3f viewpoint, std::vector<s32> &output) const
113 {
114 traverse(root, viewpoint, output);
115 }
116
117private:
118 // Tree node definition;
132
133
134 s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
135 void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
136
137 const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
138 std::vector<TreeNode> nodes; // list of nodes
139 s32 root = -1; // index of the root node
140};
141
142/*
143 * PartialMeshBuffer
144 *
145 * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
146 * indices with the same vertex buffer.
147 *
148 * Irrlicht does not currently support this: `CMeshBuffer` ties together a single vertex buffer
149 * and a single index buffer. There's no way to share these between mesh buffers.
150 *
151 */
153{
154public:
155 PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indexes) :
156 m_buffer(buffer), m_vertex_indexes(std::move(vertex_indexes))
157 {}
158
159 scene::IMeshBuffer *getBuffer() const { return m_buffer; }
160 const std::vector<u16> &getVertexIndexes() const { return m_vertex_indexes; }
161
162 void beforeDraw() const;
163 void afterDraw() const;
164private:
165 scene::SMeshBuffer *m_buffer;
166 mutable std::vector<u16> m_vertex_indexes;
167};
168
169/*
170 Holds a mesh for a mapblock.
171
172 Besides the SMesh*, this contains information used for animating
173 the vertex positions, colors and texture coordinates of the mesh.
174 For example:
175 - cracks [implemented]
176 - day/night transitions [implemented]
177 - animated flowing liquids [not implemented]
178 - animating vertex positions for e.g. axles [not implemented]
179*/
181{
182public:
183 // Builds the mesh given
184 MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset);
186
187 // Main animation function, parameters:
188 // faraway: whether the block is far away from the camera (~50 nodes)
189 // time: the global animation time, 0 .. 60 (repeats every minute)
190 // daynight_ratio: 0 .. 1000
191 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
192 // Returns true if anything has been changed.
193 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
194
195 scene::IMesh *getMesh()
196 {
197 return m_mesh[0];
198 }
199
200 scene::IMesh *getMesh(u8 layer)
201 {
202 return m_mesh[layer];
203 }
204
205 std::vector<MinimapMapblock*> moveMinimapMapblocks()
206 {
207 std::vector<MinimapMapblock*> minimap_mapblocks;
208 minimap_mapblocks.swap(m_minimap_mapblocks);
209 return minimap_mapblocks;
210 }
211
212 bool isAnimationForced() const
213 {
214 return m_animation_force_timer == 0;
215 }
216
222
224 f32 getBoundingRadius() const { return m_bounding_radius; }
225
228
230 void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
232
234 const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
235 {
236 return this->m_transparent_buffers;
237 }
238
239private:
241 int frame; // last animation frame
244 };
245
246 scene::IMesh *m_mesh[MAX_TILE_LAYERS];
247 std::vector<MinimapMapblock*> m_minimap_mapblocks;
250
253
255
256 // Must animate() be called before rendering?
259
260 // Animation info: cracks
261 // Last crack value passed to animate()
263 // Maps mesh and mesh buffer (i.e. material) indices to base texture names
264 std::map<std::pair<u8, u32>, std::string> m_crack_materials;
265
266 // Animation info: texture animation
267 // Maps mesh and mesh buffer indices to TileSpecs
268 // Keys are pairs of (mesh index, buffer index in the mesh)
269 std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
270
271 // Animation info: day/night transitions
272 // Last daynight_ratio value passed to animate()
274 // For each mesh and mesh buffer, stores pre-baked colors
275 // of sunlit vertices
276 // Keys are pairs of (mesh index, buffer index in the mesh)
277 std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
278
279 // list of all semitransparent triangles in the mapblock
280 std::vector<MeshTriangle> m_transparent_triangles;
281 // Binary Space Partitioning tree for the block
283 // Ordered list of references to parts of transparent buffers to draw
284 std::vector<PartialMeshBuffer> m_transparent_buffers;
285 // Is m_transparent_buffers currently in consolidated form?
287};
288
301video::SColor encode_light(u16 light, u8 emissive_light);
302
303// Compute light at node
304u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
305u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
306u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
307u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
308
313void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
314
322void final_color_blend(video::SColor *result,
323 u16 light, u32 daynight_ratio);
324
332void final_color_blend(video::SColor *result,
333 const video::SColor &data, const video::SColorf &dayLight);
334
335// Retrieves the TileSpec of a face of a node
336// Adds MATERIAL_FLAG_CRACK if the node is cracked
337// TileSpec should be passed as reference due to the underlying TileFrame and its vector
338// TileFrame vector copy cost very much to client
339void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
340void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);
341
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:205
Definition client.h:119
Definition shader.h:216
Definition texturesource.h:45
Implements a binary space partitioning tree See also: https://en.wikipedia.org/wiki/Binary_space_part...
Definition mapblock_mesh.h:106
std::vector< TreeNode > nodes
Definition mapblock_mesh.h:138
void buildTree(const std::vector< MeshTriangle > *triangles, u16 side_lingth)
Definition mapblock_mesh.cpp:439
const std::vector< MeshTriangle > * triangles
Definition mapblock_mesh.h:137
s32 root
Definition mapblock_mesh.h:139
void traverse(v3f viewpoint, std::vector< s32 > &output) const
Definition mapblock_mesh.h:112
MapBlockBspTree()
Definition mapblock_mesh.h:108
Definition mapblock_mesh.h:181
std::vector< MinimapMapblock * > m_minimap_mapblocks
Definition mapblock_mesh.h:247
~MapBlockMesh()
Definition mapblock_mesh.cpp:804
void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos)
update transparent buffers to render towards the camera
Definition mapblock_mesh.cpp:899
MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset)
Definition mapblock_mesh.cpp:612
u32 m_last_daynight_ratio
Definition mapblock_mesh.h:273
f32 getBoundingRadius() const
Radius of the bounding-sphere, in BS-space.
Definition mapblock_mesh.h:224
bool isAnimationForced() const
Definition mapblock_mesh.h:212
const std::vector< PartialMeshBuffer > & getTransparentBuffers() const
get the list of transparent buffers
Definition mapblock_mesh.h:234
bool m_enable_shaders
Definition mapblock_mesh.h:254
v3f getBoundingSphereCenter() const
Center of the bounding-sphere, in BS-space, relative to block pos.
Definition mapblock_mesh.h:227
int m_animation_force_timer
Definition mapblock_mesh.h:258
std::map< std::pair< u8, u32 >, std::string > m_crack_materials
Definition mapblock_mesh.h:264
MapBlockBspTree m_bsp_tree
Definition mapblock_mesh.h:282
std::vector< MinimapMapblock * > moveMinimapMapblocks()
Definition mapblock_mesh.h:205
void consolidateTransparentBuffers()
Definition mapblock_mesh.cpp:935
f32 m_bounding_radius
Definition mapblock_mesh.h:251
scene::IMesh * getMesh()
Definition mapblock_mesh.h:195
bool animate(bool faraway, float time, int crack, u32 daynight_ratio)
Definition mapblock_mesh.cpp:818
void decreaseAnimationForceTimer()
Definition mapblock_mesh.h:217
std::map< std::pair< u8, u32 >, std::map< u32, video::SColor > > m_daynight_diffs
Definition mapblock_mesh.h:277
std::vector< MeshTriangle > m_transparent_triangles
Definition mapblock_mesh.h:280
bool m_transparent_buffers_consolidated
Definition mapblock_mesh.h:286
scene::IMesh * m_mesh[MAX_TILE_LAYERS]
Definition mapblock_mesh.h:246
IShaderSource * m_shdrsrc
Definition mapblock_mesh.h:249
ITextureSource * m_tsrc
Definition mapblock_mesh.h:248
int m_last_crack
Definition mapblock_mesh.h:262
bool m_has_animation
Definition mapblock_mesh.h:257
scene::IMesh * getMesh(u8 layer)
Definition mapblock_mesh.h:200
std::map< std::pair< u8, u32 >, AnimationInfo > m_animation_info
Definition mapblock_mesh.h:269
v3f m_bounding_sphere_center
Definition mapblock_mesh.h:252
std::vector< PartialMeshBuffer > m_transparent_buffers
Definition mapblock_mesh.h:284
Definition mapblock.h:73
Definition mapblock_mesh.h:75
scene::SMeshBuffer * buffer
Definition mapblock_mesh.h:77
u16 p3
Definition mapblock_mesh.h:78
float areaSQ
Definition mapblock_mesh.h:80
v3f getNormal() const
Definition mapblock_mesh.h:92
u16 p2
Definition mapblock_mesh.h:78
v3f centroid
Definition mapblock_mesh.h:79
void updateAttributes()
Definition mapblock_mesh.h:82
u16 p1
Definition mapblock_mesh.h:78
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:556
Definition mapblock_mesh.h:153
void afterDraw() const
Definition mapblock_mesh.cpp:602
PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector< u16 > &&vertex_indexes)
Definition mapblock_mesh.h:155
std::vector< u16 > m_vertex_indexes
Definition mapblock_mesh.h:166
void beforeDraw() const
Definition mapblock_mesh.cpp:595
scene::SMeshBuffer * m_buffer
Definition mapblock_mesh.h:165
scene::IMeshBuffer * getBuffer() const
Definition mapblock_mesh.h:159
const std::vector< u16 > & getVertexIndexes() const
Definition mapblock_mesh.h:160
Definition voxel.h:381
core::vector3d< s16 > v3s16
Definition irr_v3d.h:28
core::vector3df v3f
Definition irr_v3d.h:26
void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile)
Definition mapblock_mesh.cpp:335
u8 get_solid_sides(MeshMakeData *data)
Return bitset of the sides of the mesh that consist of solid nodes only Bits: 0 0 -Z +Z -X +X -Y +Y.
Definition mapblock_mesh.cpp:993
u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data)
Definition mapblock_mesh.cpp:266
void final_color_blend(video::SColor *result, u16 light, u32 daynight_ratio)
Definition mapblock_mesh.cpp:292
video::SColor encode_light(u16 light, u8 emissive_light)
Definition mapblock_mesh.cpp:965
u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef)
Definition mapblock_mesh.cpp:100
u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef)
Definition mapblock_mesh.cpp:136
void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile)
Definition mapblock_mesh.cpp:355
void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio)
Definition mapblock_mesh.cpp:284
u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data)
Definition mapblock_mesh.cpp:256
Definition activeobjectmgr.cpp:26
Definition mapblock_mesh.h:120
v3f normal
Definition mapblock_mesh.h:121
std::vector< s32 > triangle_refs
Definition mapblock_mesh.h:123
v3f origin
Definition mapblock_mesh.h:122
s32 front_ref
Definition mapblock_mesh.h:124
s32 back_ref
Definition mapblock_mesh.h:125
TreeNode(v3f normal, v3f origin, const std::vector< s32 > &triangle_refs, s32 front_ref, s32 back_ref)
Definition mapblock_mesh.h:128
Definition mapblock_mesh.h:240
int frame_offset
Definition mapblock_mesh.h:242
TileLayer tile
Definition mapblock_mesh.h:243
int frame
Definition mapblock_mesh.h:241
Definition mapnode.h:139
Definition mapblock_mesh.h:44
void fillBlockDataBegin(const v3s16 &blockpos)
Definition mapblock_mesh.cpp:48
v3s16 m_crack_pos_relative
Definition mapblock_mesh.h:47
const NodeDefManager * nodedef
Definition mapblock_mesh.h:51
bool m_smooth_lighting
Definition mapblock_mesh.h:48
bool m_use_shaders
Definition mapblock_mesh.h:52
MeshMakeData(const NodeDefManager *ndef, u16 side_length, bool use_shaders)
Definition mapblock_mesh.cpp:42
VoxelManipulator m_vmanip
Definition mapblock_mesh.h:45
void setSmoothLighting(bool smooth_lighting)
Definition mapblock_mesh.cpp:75
void setCrack(int crack_level, v3s16 crack_pos)
Definition mapblock_mesh.cpp:69
u16 side_length
Definition mapblock_mesh.h:49
void fillBlockData(const v3s16 &bp, MapNode *data)
Definition mapblock_mesh.cpp:60
v3s16 m_blockpos
Definition mapblock_mesh.h:46
Definition minimap.h:65
Defines a layer of a tile.
Definition tile.h:73
Definition tile.h:159
static std::string p(std::string path)
Definition test_filesys.cpp:66
#define MAX_TILE_LAYERS
Definition tile.h:69