Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
mapblock_mesh.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include "irr_ptr.h"
9#include "IMesh.h"
10#include "CMeshBuffer.h"
11
12#include "util/numeric.h"
13#include "client/tile.h"
14#include "voxel.h"
15#include <map>
16
17namespace video {
18 class IVideoDriver;
19}
20
21class Client;
22class NodeDefManager;
23class IShaderSource;
24class ITextureSource;
25
26/*
27 Mesh making stuff
28*/
29
30
31struct MinimapMapblock;
32
34{
36
37 // base pos of meshgen area, in blocks
38 v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
39 // size of meshgen area, in nodes.
40 // vmanip will have at least an extra 1 node onion layer.
41 // area is expected to fit into mesh grid cell.
43 // vertex positions will be relative to this grid
45
46 // relative to blockpos
47 v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
48 bool m_generate_minimap = false;
49 bool m_smooth_lighting = false;
51
53
54 MeshMakeData(const NodeDefManager *ndef, u16 side_lingth, MeshGrid mesh_grid);
55
56 /*
57 Copy block data manually (to allow optimizations by the caller)
58 */
59 void fillBlockDataBegin(const v3s16 &blockpos);
60
61 /*
62 Prepare block data for rendering a single node located at (0,0,0).
63 */
64 void fillSingleNode(MapNode data, MapNode padding = MapNode(CONTENT_AIR));
65
66 /*
67 Set the (node) position of a crack
68 */
69 void setCrack(int crack_level, v3s16 crack_pos);
70};
71
72// represents a triangle as indexes into the vertex buffer in SMeshBuffer
74{
75public:
76 scene::SMeshBuffer *buffer;
77 u16 p1, p2, p3;
79 float areaSQ;
80
82 {
83 v3f v1 = buffer->getPosition(p1);
84 v3f v2 = buffer->getPosition(p2);
85 v3f v3 = buffer->getPosition(p3);
86
87 centroid = (v1 + v2 + v3) / 3;
88 areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
89 }
90
91 v3f getNormal() const {
92 v3f v1 = buffer->getPosition(p1);
93 v3f v2 = buffer->getPosition(p2);
94 v3f v3 = buffer->getPosition(p3);
95
96 return (v2-v1).crossProduct(v3-v1);
97 }
98};
99
105{
106public:
108
109 void buildTree(const std::vector<MeshTriangle> *triangles, u16 side_lingth);
110
111 void traverse(v3f viewpoint, std::vector<s32> &output) const
112 {
113 traverse(root, viewpoint, output);
114 }
115
116private:
117 // Tree node definition;
131
132
133 s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
134 void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
135
136 const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
137 std::vector<TreeNode> nodes; // list of nodes
138 s32 root = -1; // index of the root node
139};
140
141/*
142 * PartialMeshBuffer
143 *
144 * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
145 * indices with the same vertex buffer.
146 */
148{
149public:
150 PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indices) :
151 m_buffer(buffer), m_indices(make_irr<scene::SIndexBuffer>())
152 {
153 m_indices->Data = std::move(vertex_indices);
154 m_indices->setHardwareMappingHint(scene::EHM_STATIC);
155 }
156
157 auto *getBuffer() const { return m_buffer; }
158
159 void draw(video::IVideoDriver *driver) const;
160
161private:
162 scene::SMeshBuffer *m_buffer;
163 irr_ptr<scene::SIndexBuffer> m_indices;
164};
165
166/*
167 Holds a mesh for a mapblock.
168
169 Besides the SMesh*, this contains information used fortransparency sorting
170 and texture animation.
171 For example:
172 - cracks
173 - day/night transitions
174*/
176{
177public:
178 // Builds the mesh given
181
182 // Main animation function, parameters:
183 // faraway: whether the block is far away from the camera (~50 nodes)
184 // time: the global animation time, 0 .. 60 (repeats every minute)
185 // daynight_ratio: 0 .. 1000
186 // crack: -1 .. CRACK_ANIMATION_LENGTH (-1 for off)
187 // Returns true if anything has been changed.
188 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
189
191 scene::IMesh *getMesh()
192 {
193 return m_mesh[0].get();
194 }
195
198 scene::IMesh *getMesh(u8 layer)
199 {
200 assert(layer < MAX_TILE_LAYERS);
201 return m_mesh[layer].get();
202 }
203
204 std::vector<MinimapMapblock*> moveMinimapMapblocks()
205 {
206 std::vector<MinimapMapblock*> minimap_mapblocks;
207 minimap_mapblocks.swap(m_minimap_mapblocks);
208 return minimap_mapblocks;
209 }
210
212 bool isEmpty() const
213 {
214 if (!m_transparent_triangles.empty())
215 return false;
216 for (auto &mesh : m_mesh) {
217 for (u32 i = 0; i < mesh->getMeshBufferCount(); i++) {
218 if (mesh->getMeshBuffer(i)->getIndexCount() != 0)
219 return false;
220 }
221 }
222 return true;
223 }
224
225 bool isAnimationForced() const
226 {
227 return m_animation_force_timer == 0;
228 }
229
235
237 f32 getBoundingRadius() const { return m_bounding_radius; }
238
241
249 void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos, bool group_by_buffers);
251
253 const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
254 {
256 }
257
261 static const int TEXTURE_LAYER_CRACK = 1;
262
263 static float packCrackMaterialParam(int crack, u8 layer_scale)
264 {
265 // +1 so that the default MaterialTypeParam = 0 is a no-op,
266 // since the shader needs to know when to actually apply the crack.
267 u32 n = (layer_scale << 16) | (u16) (crack + 1);
268 return n;
269 }
270 static std::pair<int, u8> unpackCrackMaterialParam(float param)
271 {
272 u32 n = param;
273 return std::make_pair<int, u8>((n & 0xffff) - 1, (n >> 16) & 0xff);
274 }
275
276private:
277
278 typedef std::pair<u8 /* layer index */, u32 /* buffer index */> MeshIndex;
279
280 irr_ptr<scene::IMesh> m_mesh[MAX_TILE_LAYERS];
281 std::vector<MinimapMapblock*> m_minimap_mapblocks;
284
287
288 // Must animate() be called before rendering?
291
292 // Animation info: cracks
293 // Last crack value passed to animate()
295 // Indicates which materials to apply the crack to
296 std::vector<MeshIndex> m_crack_materials;
297
298 // Animation info: texture animation
299 // Maps mesh and mesh buffer indices to TileSpecs
300 std::map<MeshIndex, AnimationInfo> m_animation_info;
301
302 // list of all semitransparent triangles in the mapblock
303 std::vector<MeshTriangle> m_transparent_triangles;
304 // Binary Space Partitioning tree for the block
306 // Ordered list of references to parts of transparent buffers to draw
307 std::vector<PartialMeshBuffer> m_transparent_buffers;
308 // Is m_transparent_buffers currently in consolidated form?
310};
311
324video::SColor encode_light(u16 light, u8 emissive_light);
325
326// Compute light at node
327u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
328u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
329u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
330u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
331
336void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
337
345void final_color_blend(video::SColor *result,
346 u16 light, u32 daynight_ratio);
347
355void final_color_blend(video::SColor *result,
356 const video::SColor &data, const video::SColorf &dayLight);
357
358// Retrieves the TileSpec of a face of a node
359// Adds MATERIAL_FLAG_CRACK if the node is cracked
360// TileSpec should be passed as reference due to the underlying TileFrame and its vector
361// TileFrame vector copy cost very much to client
362void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
363void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);
364
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:207
Definition client.h:106
Definition shader.h:241
Definition texturesource.h:45
Implements a binary space partitioning tree See also: https://en.wikipedia.org/wiki/Binary_space_part...
Definition mapblock_mesh.h:105
std::vector< TreeNode > nodes
Definition mapblock_mesh.h:137
void buildTree(const std::vector< MeshTriangle > *triangles, u16 side_lingth)
Definition mapblock_mesh.cpp:423
const std::vector< MeshTriangle > * triangles
Definition mapblock_mesh.h:136
s32 root
Definition mapblock_mesh.h:138
void traverse(v3f viewpoint, std::vector< s32 > &output) const
Definition mapblock_mesh.h:111
MapBlockBspTree()
Definition mapblock_mesh.h:107
Definition mapblock_mesh.h:176
std::vector< MinimapMapblock * > m_minimap_mapblocks
Definition mapblock_mesh.h:281
~MapBlockMesh()
Definition mapblock_mesh.cpp:759
static const int TEXTURE_LAYER_CRACK
Texture layer in SMaterial where the crack texture is put.
Definition mapblock_mesh.h:261
std::map< MeshIndex, AnimationInfo > m_animation_info
Definition mapblock_mesh.h:300
f32 getBoundingRadius() const
Radius of the bounding-sphere, in BS-space.
Definition mapblock_mesh.h:237
bool isAnimationForced() const
Definition mapblock_mesh.h:225
MapBlockMesh(Client *client, MeshMakeData *data)
Definition mapblock_mesh.cpp:631
const std::vector< PartialMeshBuffer > & getTransparentBuffers() const
get the list of transparent buffers
Definition mapblock_mesh.h:253
v3f getBoundingSphereCenter() const
Center of the bounding-sphere, in BS-space, relative to block pos.
Definition mapblock_mesh.h:240
int m_animation_force_timer
Definition mapblock_mesh.h:290
MapBlockBspTree m_bsp_tree
Definition mapblock_mesh.h:305
std::vector< MinimapMapblock * > moveMinimapMapblocks()
Definition mapblock_mesh.h:204
bool isEmpty() const
Definition mapblock_mesh.h:212
void consolidateTransparentBuffers()
Definition mapblock_mesh.cpp:878
f32 m_bounding_radius
Definition mapblock_mesh.h:285
scene::IMesh * getMesh()
Definition mapblock_mesh.h:191
bool animate(bool faraway, float time, int crack, u32 daynight_ratio)
Definition mapblock_mesh.cpp:773
void decreaseAnimationForceTimer()
Definition mapblock_mesh.h:230
std::pair< u8, u32 > MeshIndex
Definition mapblock_mesh.h:278
void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos, bool group_by_buffers)
Update transparent buffers to render towards the camera.
Definition mapblock_mesh.cpp:809
std::vector< MeshIndex > m_crack_materials
Definition mapblock_mesh.h:296
std::vector< MeshTriangle > m_transparent_triangles
Definition mapblock_mesh.h:303
irr_ptr< scene::IMesh > m_mesh[MAX_TILE_LAYERS]
Definition mapblock_mesh.h:280
static std::pair< int, u8 > unpackCrackMaterialParam(float param)
Definition mapblock_mesh.h:270
bool m_transparent_buffers_consolidated
Definition mapblock_mesh.h:309
static float packCrackMaterialParam(int crack, u8 layer_scale)
Definition mapblock_mesh.h:263
IShaderSource * m_shdrsrc
Definition mapblock_mesh.h:283
ITextureSource * m_tsrc
Definition mapblock_mesh.h:282
int m_last_crack
Definition mapblock_mesh.h:294
bool m_has_animation
Definition mapblock_mesh.h:289
scene::IMesh * getMesh(u8 layer)
Definition mapblock_mesh.h:198
v3f m_bounding_sphere_center
Definition mapblock_mesh.h:286
std::vector< PartialMeshBuffer > m_transparent_buffers
Definition mapblock_mesh.h:307
Definition mapblock_mesh.h:74
scene::SMeshBuffer * buffer
Definition mapblock_mesh.h:76
u16 p3
Definition mapblock_mesh.h:77
float areaSQ
Definition mapblock_mesh.h:79
v3f getNormal() const
Definition mapblock_mesh.h:91
u16 p2
Definition mapblock_mesh.h:77
v3f centroid
Definition mapblock_mesh.h:78
void updateAttributes()
Definition mapblock_mesh.h:81
u16 p1
Definition mapblock_mesh.h:77
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:509
Definition mapblock_mesh.h:148
void draw(video::IVideoDriver *driver) const
Definition mapblock_mesh.cpp:579
irr_ptr< scene::SIndexBuffer > m_indices
Definition mapblock_mesh.h:163
scene::SMeshBuffer * m_buffer
Definition mapblock_mesh.h:162
PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector< u16 > &&vertex_indices)
Definition mapblock_mesh.h:150
auto * getBuffer() const
Definition mapblock_mesh.h:157
Definition voxel.h:365
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile)
Definition mapblock_mesh.cpp:333
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:936
u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data)
Definition mapblock_mesh.cpp:263
void final_color_blend(video::SColor *result, u16 light, u32 daynight_ratio)
Definition mapblock_mesh.cpp:290
video::SColor encode_light(u16 light, u8 emissive_light)
Definition mapblock_mesh.cpp:908
u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef)
Definition mapblock_mesh.cpp:97
u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef)
Definition mapblock_mesh.cpp:133
void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile)
Definition mapblock_mesh.cpp:353
void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio)
Definition mapblock_mesh.cpp:281
u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data)
Definition mapblock_mesh.cpp:253
#define CONTENT_AIR
Definition mapnode.h:45
Definition activeobjectmgr.cpp:11
Definition camera.h:24
Definition clientmap.h:36
Definition mapblock_mesh.h:119
v3f normal
Definition mapblock_mesh.h:120
std::vector< s32 > triangle_refs
Definition mapblock_mesh.h:122
v3f origin
Definition mapblock_mesh.h:121
s32 front_ref
Definition mapblock_mesh.h:123
s32 back_ref
Definition mapblock_mesh.h:124
TreeNode(v3f normal, v3f origin, const std::vector< s32 > &triangle_refs, s32 front_ref, s32 back_ref)
Definition mapblock_mesh.h:127
Definition mapnode.h:123
Describes a grid with given step, oirginating at (0,0,0)
Definition numeric.h:157
Definition mapblock_mesh.h:34
void fillBlockDataBegin(const v3s16 &blockpos)
Definition mapblock_mesh.cpp:40
v3s16 m_crack_pos_relative
Definition mapblock_mesh.h:47
bool m_smooth_lighting
Definition mapblock_mesh.h:49
void fillSingleNode(MapNode data, MapNode padding=MapNode(CONTENT_AIR))
Definition mapblock_mesh.cpp:53
const NodeDefManager * m_nodedef
Definition mapblock_mesh.h:52
VoxelManipulator m_vmanip
Definition mapblock_mesh.h:35
MeshGrid m_mesh_grid
Definition mapblock_mesh.h:44
u16 m_side_length
Definition mapblock_mesh.h:42
bool m_enable_water_reflections
Definition mapblock_mesh.h:50
void setCrack(int crack_level, v3s16 crack_pos)
Definition mapblock_mesh.cpp:71
MeshMakeData(const NodeDefManager *ndef, u16 side_lingth, MeshGrid mesh_grid)
Definition mapblock_mesh.cpp:31
bool m_generate_minimap
Definition mapblock_mesh.h:48
v3s16 m_blockpos
Definition mapblock_mesh.h:38
Definition minimap.h:68
Definition tile.h:233
static std::string p(std::string path)
Definition test_filesys.cpp:64
#define MAX_TILE_LAYERS
We have two tile layers: layer 0 = base layer 1 = overlay.
Definition tile.h:71