Luanti 5.10.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
8#include "irr_ptr.h"
9#include "util/numeric.h"
10#include "client/tile.h"
11#include "voxel.h"
12#include <array>
13#include <map>
14#include <unordered_map>
15
16class Client;
17class NodeDefManager;
18class IShaderSource;
19class ITextureSource;
20
21/*
22 Mesh making stuff
23*/
24
25
26class MapBlock;
27struct MinimapMapblock;
28
30{
32 v3s16 m_blockpos = v3s16(-1337,-1337,-1337);
33 v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337);
34 bool m_smooth_lighting = false;
36
39
40 MeshMakeData(const NodeDefManager *ndef, u16 side_length, bool use_shaders);
41
42 /*
43 Copy block data manually (to allow optimizations by the caller)
44 */
45 void fillBlockDataBegin(const v3s16 &blockpos);
46 void fillBlockData(const v3s16 &bp, MapNode *data);
47
48 /*
49 Set the (node) position of a crack
50 */
51 void setCrack(int crack_level, v3s16 crack_pos);
52
53 /*
54 Enable or disable smooth lighting
55 */
56 void setSmoothLighting(bool smooth_lighting);
57};
58
59// represents a triangle as indexes into the vertex buffer in SMeshBuffer
61{
62public:
63 scene::SMeshBuffer *buffer;
64 u16 p1, p2, p3;
66 float areaSQ;
67
69 {
70 v3f v1 = buffer->getPosition(p1);
71 v3f v2 = buffer->getPosition(p2);
72 v3f v3 = buffer->getPosition(p3);
73
74 centroid = (v1 + v2 + v3) / 3;
75 areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
76 }
77
78 v3f getNormal() const {
79 v3f v1 = buffer->getPosition(p1);
80 v3f v2 = buffer->getPosition(p2);
81 v3f v3 = buffer->getPosition(p3);
82
83 return (v2-v1).crossProduct(v3-v1);
84 }
85};
86
92{
93public:
95
96 void buildTree(const std::vector<MeshTriangle> *triangles, u16 side_lingth);
97
98 void traverse(v3f viewpoint, std::vector<s32> &output) const
99 {
100 traverse(root, viewpoint, output);
101 }
102
103private:
104 // Tree node definition;
118
119
120 s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
121 void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
122
123 const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
124 std::vector<TreeNode> nodes; // list of nodes
125 s32 root = -1; // index of the root node
126};
127
128/*
129 * PartialMeshBuffer
130 *
131 * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
132 * indices with the same vertex buffer.
133 */
135{
136public:
137 PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indices) :
138 m_buffer(buffer), m_indices(make_irr<scene::SIndexBuffer>())
139 {
140 m_indices->Data = std::move(vertex_indices);
141 m_indices->setHardwareMappingHint(scene::EHM_STATIC);
142 }
143
144 auto *getBuffer() const { return m_buffer; }
145
146 void draw(video::IVideoDriver *driver) const;
147
148private:
149 scene::SMeshBuffer *m_buffer;
150 irr_ptr<scene::SIndexBuffer> m_indices;
151};
152
153/*
154 Holds a mesh for a mapblock.
155
156 Besides the SMesh*, this contains information used for animating
157 the vertex positions, colors and texture coordinates of the mesh.
158 For example:
159 - cracks [implemented]
160 - day/night transitions [implemented]
161 - animated flowing liquids [not implemented]
162 - animating vertex positions for e.g. axles [not implemented]
163*/
165{
166public:
167 // Builds the mesh given
168 MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset);
170
171 // Main animation function, parameters:
172 // faraway: whether the block is far away from the camera (~50 nodes)
173 // time: the global animation time, 0 .. 60 (repeats every minute)
174 // daynight_ratio: 0 .. 1000
175 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
176 // Returns true if anything has been changed.
177 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
178
179 scene::IMesh *getMesh()
180 {
181 return m_mesh[0].get();
182 }
183
184 scene::IMesh *getMesh(u8 layer)
185 {
186 return m_mesh[layer].get();
187 }
188
189 std::vector<MinimapMapblock*> moveMinimapMapblocks()
190 {
191 std::vector<MinimapMapblock*> minimap_mapblocks;
192 minimap_mapblocks.swap(m_minimap_mapblocks);
193 return minimap_mapblocks;
194 }
195
196 bool isAnimationForced() const
197 {
198 return m_animation_force_timer == 0;
199 }
200
206
208 f32 getBoundingRadius() const { return m_bounding_radius; }
209
212
214 void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos);
216
218 const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
219 {
220 return this->m_transparent_buffers;
221 }
222
223private:
225 int frame; // last animation frame
228 };
229
230 irr_ptr<scene::IMesh> m_mesh[MAX_TILE_LAYERS];
231 std::vector<MinimapMapblock*> m_minimap_mapblocks;
234
237
239
240 // Must animate() be called before rendering?
243
244 // Animation info: cracks
245 // Last crack value passed to animate()
247 // Maps mesh and mesh buffer (i.e. material) indices to base texture names
248 std::map<std::pair<u8, u32>, std::string> m_crack_materials;
249
250 // Animation info: texture animation
251 // Maps mesh and mesh buffer indices to TileSpecs
252 // Keys are pairs of (mesh index, buffer index in the mesh)
253 std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
254
255 // Animation info: day/night transitions
256 // Last daynight_ratio value passed to animate()
258 // For each mesh and mesh buffer, stores pre-baked colors
259 // of sunlit vertices
260 // Keys are pairs of (mesh index, buffer index in the mesh)
261 std::map<std::pair<u8, u32>, std::map<u32, video::SColor > > m_daynight_diffs;
262
263 // list of all semitransparent triangles in the mapblock
264 std::vector<MeshTriangle> m_transparent_triangles;
265 // Binary Space Partitioning tree for the block
267 // Ordered list of references to parts of transparent buffers to draw
268 std::vector<PartialMeshBuffer> m_transparent_buffers;
269 // Is m_transparent_buffers currently in consolidated form?
271};
272
285video::SColor encode_light(u16 light, u8 emissive_light);
286
287// Compute light at node
288u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
289u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
290u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
291u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
292
297void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
298
306void final_color_blend(video::SColor *result,
307 u16 light, u32 daynight_ratio);
308
316void final_color_blend(video::SColor *result,
317 const video::SColor &data, const video::SColorf &dayLight);
318
319// Retrieves the TileSpec of a face of a node
320// Adds MATERIAL_FLAG_CRACK if the node is cracked
321// TileSpec should be passed as reference due to the underlying TileFrame and its vector
322// TileFrame vector copy cost very much to client
323void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
324void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);
325
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:191
Definition client.h:105
Definition shader.h:201
Definition texturesource.h:36
Implements a binary space partitioning tree See also: https://en.wikipedia.org/wiki/Binary_space_part...
Definition mapblock_mesh.h:92
std::vector< TreeNode > nodes
Definition mapblock_mesh.h:124
void buildTree(const std::vector< MeshTriangle > *triangles, u16 side_lingth)
Definition mapblock_mesh.cpp:425
const std::vector< MeshTriangle > * triangles
Definition mapblock_mesh.h:123
s32 root
Definition mapblock_mesh.h:125
void traverse(v3f viewpoint, std::vector< s32 > &output) const
Definition mapblock_mesh.h:98
MapBlockBspTree()
Definition mapblock_mesh.h:94
Definition mapblock_mesh.h:165
std::vector< MinimapMapblock * > m_minimap_mapblocks
Definition mapblock_mesh.h:231
~MapBlockMesh()
Definition mapblock_mesh.cpp:778
void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos)
update transparent buffers to render towards the camera
Definition mapblock_mesh.cpp:868
MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset)
Definition mapblock_mesh.cpp:592
u32 m_last_daynight_ratio
Definition mapblock_mesh.h:257
f32 getBoundingRadius() const
Radius of the bounding-sphere, in BS-space.
Definition mapblock_mesh.h:208
bool isAnimationForced() const
Definition mapblock_mesh.h:196
const std::vector< PartialMeshBuffer > & getTransparentBuffers() const
get the list of transparent buffers
Definition mapblock_mesh.h:218
bool m_enable_shaders
Definition mapblock_mesh.h:238
v3f getBoundingSphereCenter() const
Center of the bounding-sphere, in BS-space, relative to block pos.
Definition mapblock_mesh.h:211
int m_animation_force_timer
Definition mapblock_mesh.h:242
std::map< std::pair< u8, u32 >, std::string > m_crack_materials
Definition mapblock_mesh.h:248
MapBlockBspTree m_bsp_tree
Definition mapblock_mesh.h:266
std::vector< MinimapMapblock * > moveMinimapMapblocks()
Definition mapblock_mesh.h:189
void consolidateTransparentBuffers()
Definition mapblock_mesh.cpp:904
f32 m_bounding_radius
Definition mapblock_mesh.h:235
scene::IMesh * getMesh()
Definition mapblock_mesh.h:179
bool animate(bool faraway, float time, int crack, u32 daynight_ratio)
Definition mapblock_mesh.cpp:792
void decreaseAnimationForceTimer()
Definition mapblock_mesh.h:201
std::map< std::pair< u8, u32 >, std::map< u32, video::SColor > > m_daynight_diffs
Definition mapblock_mesh.h:261
std::vector< MeshTriangle > m_transparent_triangles
Definition mapblock_mesh.h:264
irr_ptr< scene::IMesh > m_mesh[MAX_TILE_LAYERS]
Definition mapblock_mesh.h:230
bool m_transparent_buffers_consolidated
Definition mapblock_mesh.h:270
IShaderSource * m_shdrsrc
Definition mapblock_mesh.h:233
ITextureSource * m_tsrc
Definition mapblock_mesh.h:232
int m_last_crack
Definition mapblock_mesh.h:246
bool m_has_animation
Definition mapblock_mesh.h:241
scene::IMesh * getMesh(u8 layer)
Definition mapblock_mesh.h:184
std::map< std::pair< u8, u32 >, AnimationInfo > m_animation_info
Definition mapblock_mesh.h:253
v3f m_bounding_sphere_center
Definition mapblock_mesh.h:236
std::vector< PartialMeshBuffer > m_transparent_buffers
Definition mapblock_mesh.h:268
Definition mapblock.h:58
Definition mapblock_mesh.h:61
scene::SMeshBuffer * buffer
Definition mapblock_mesh.h:63
u16 p3
Definition mapblock_mesh.h:64
float areaSQ
Definition mapblock_mesh.h:66
v3f getNormal() const
Definition mapblock_mesh.h:78
u16 p2
Definition mapblock_mesh.h:64
v3f centroid
Definition mapblock_mesh.h:65
void updateAttributes()
Definition mapblock_mesh.h:68
u16 p1
Definition mapblock_mesh.h:64
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:541
Definition mapblock_mesh.h:135
void draw(video::IVideoDriver *driver) const
Definition mapblock_mesh.cpp:581
irr_ptr< scene::SIndexBuffer > m_indices
Definition mapblock_mesh.h:150
scene::SMeshBuffer * m_buffer
Definition mapblock_mesh.h:149
PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector< u16 > &&vertex_indices)
Definition mapblock_mesh.h:137
auto * getBuffer() const
Definition mapblock_mesh.h:144
Definition voxel.h:360
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:321
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:962
u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data)
Definition mapblock_mesh.cpp:252
void final_color_blend(video::SColor *result, u16 light, u32 daynight_ratio)
Definition mapblock_mesh.cpp:278
video::SColor encode_light(u16 light, u8 emissive_light)
Definition mapblock_mesh.cpp:934
u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef)
Definition mapblock_mesh.cpp:86
u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef)
Definition mapblock_mesh.cpp:122
void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile)
Definition mapblock_mesh.cpp:341
void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio)
Definition mapblock_mesh.cpp:270
u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data)
Definition mapblock_mesh.cpp:242
Definition activeobjectmgr.cpp:11
Definition mapblock_mesh.h:106
v3f normal
Definition mapblock_mesh.h:107
std::vector< s32 > triangle_refs
Definition mapblock_mesh.h:109
v3f origin
Definition mapblock_mesh.h:108
s32 front_ref
Definition mapblock_mesh.h:110
s32 back_ref
Definition mapblock_mesh.h:111
TreeNode(v3f normal, v3f origin, const std::vector< s32 > &triangle_refs, s32 front_ref, s32 back_ref)
Definition mapblock_mesh.h:114
Definition mapblock_mesh.h:224
int frame_offset
Definition mapblock_mesh.h:226
TileLayer tile
Definition mapblock_mesh.h:227
int frame
Definition mapblock_mesh.h:225
Definition mapnode.h:124
Definition mapblock_mesh.h:30
void fillBlockDataBegin(const v3s16 &blockpos)
Definition mapblock_mesh.cpp:34
v3s16 m_crack_pos_relative
Definition mapblock_mesh.h:33
const NodeDefManager * nodedef
Definition mapblock_mesh.h:37
bool m_smooth_lighting
Definition mapblock_mesh.h:34
bool m_use_shaders
Definition mapblock_mesh.h:38
MeshMakeData(const NodeDefManager *ndef, u16 side_length, bool use_shaders)
Definition mapblock_mesh.cpp:28
VoxelManipulator m_vmanip
Definition mapblock_mesh.h:31
void setSmoothLighting(bool smooth_lighting)
Definition mapblock_mesh.cpp:61
void setCrack(int crack_level, v3s16 crack_pos)
Definition mapblock_mesh.cpp:55
u16 side_length
Definition mapblock_mesh.h:35
void fillBlockData(const v3s16 &bp, MapNode *data)
Definition mapblock_mesh.cpp:46
v3s16 m_blockpos
Definition mapblock_mesh.h:32
Definition minimap.h:51
Defines a layer of a tile.
Definition tile.h:56
Definition tile.h:140
static std::string p(std::string path)
Definition test_filesys.cpp:53
#define MAX_TILE_LAYERS
Definition tile.h:52