Luanti 5.11.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
38
39 MeshMakeData(const NodeDefManager *ndef, u16 side_length);
40
41 /*
42 Copy block data manually (to allow optimizations by the caller)
43 */
44 void fillBlockDataBegin(const v3s16 &blockpos);
45 void fillBlockData(const v3s16 &bp, MapNode *data);
46
47 /*
48 Set the (node) position of a crack
49 */
50 void setCrack(int crack_level, v3s16 crack_pos);
51
52 /*
53 Enable or disable smooth lighting
54 */
55 void setSmoothLighting(bool smooth_lighting);
56};
57
58// represents a triangle as indexes into the vertex buffer in SMeshBuffer
60{
61public:
62 scene::SMeshBuffer *buffer;
63 u16 p1, p2, p3;
65 float areaSQ;
66
68 {
69 v3f v1 = buffer->getPosition(p1);
70 v3f v2 = buffer->getPosition(p2);
71 v3f v3 = buffer->getPosition(p3);
72
73 centroid = (v1 + v2 + v3) / 3;
74 areaSQ = (v2-v1).crossProduct(v3-v1).getLengthSQ() / 4;
75 }
76
77 v3f getNormal() const {
78 v3f v1 = buffer->getPosition(p1);
79 v3f v2 = buffer->getPosition(p2);
80 v3f v3 = buffer->getPosition(p3);
81
82 return (v2-v1).crossProduct(v3-v1);
83 }
84};
85
91{
92public:
94
95 void buildTree(const std::vector<MeshTriangle> *triangles, u16 side_lingth);
96
97 void traverse(v3f viewpoint, std::vector<s32> &output) const
98 {
99 traverse(root, viewpoint, output);
100 }
101
102private:
103 // Tree node definition;
117
118
119 s32 buildTree(v3f normal, v3f origin, float delta, const std::vector<s32> &list, u32 depth);
120 void traverse(s32 node, v3f viewpoint, std::vector<s32> &output) const;
121
122 const std::vector<MeshTriangle> *triangles = nullptr; // this reference is managed externally
123 std::vector<TreeNode> nodes; // list of nodes
124 s32 root = -1; // index of the root node
125};
126
127/*
128 * PartialMeshBuffer
129 *
130 * Attach alternate `Indices` to an existing mesh buffer, to make it possible to use different
131 * indices with the same vertex buffer.
132 */
134{
135public:
136 PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector<u16> &&vertex_indices) :
137 m_buffer(buffer), m_indices(make_irr<scene::SIndexBuffer>())
138 {
139 m_indices->Data = std::move(vertex_indices);
140 m_indices->setHardwareMappingHint(scene::EHM_STATIC);
141 }
142
143 auto *getBuffer() const { return m_buffer; }
144
145 void draw(video::IVideoDriver *driver) const;
146
147private:
148 scene::SMeshBuffer *m_buffer;
149 irr_ptr<scene::SIndexBuffer> m_indices;
150};
151
152/*
153 Holds a mesh for a mapblock.
154
155 Besides the SMesh*, this contains information used for animating
156 the vertex positions, colors and texture coordinates of the mesh.
157 For example:
158 - cracks [implemented]
159 - day/night transitions [implemented]
160 - animated flowing liquids [not implemented]
161 - animating vertex positions for e.g. axles [not implemented]
162*/
164{
165public:
166 // Builds the mesh given
167 MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset);
169
170 // Main animation function, parameters:
171 // faraway: whether the block is far away from the camera (~50 nodes)
172 // time: the global animation time, 0 .. 60 (repeats every minute)
173 // daynight_ratio: 0 .. 1000
174 // crack: -1 .. CRACK_ANIMATION_LENGTH-1 (-1 for off)
175 // Returns true if anything has been changed.
176 bool animate(bool faraway, float time, int crack, u32 daynight_ratio);
177
178 scene::IMesh *getMesh()
179 {
180 return m_mesh[0].get();
181 }
182
183 scene::IMesh *getMesh(u8 layer)
184 {
185 return m_mesh[layer].get();
186 }
187
188 std::vector<MinimapMapblock*> moveMinimapMapblocks()
189 {
190 std::vector<MinimapMapblock*> minimap_mapblocks;
191 minimap_mapblocks.swap(m_minimap_mapblocks);
192 return minimap_mapblocks;
193 }
194
195 bool isAnimationForced() const
196 {
197 return m_animation_force_timer == 0;
198 }
199
205
207 f32 getBoundingRadius() const { return m_bounding_radius; }
208
211
219 void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos, bool group_by_buffers);
221
223 const std::vector<PartialMeshBuffer> &getTransparentBuffers() const
224 {
225 return this->m_transparent_buffers;
226 }
227
228private:
230 int frame; // last animation frame
233 };
234
235 irr_ptr<scene::IMesh> m_mesh[MAX_TILE_LAYERS];
236 std::vector<MinimapMapblock*> m_minimap_mapblocks;
239
242
243 // Must animate() be called before rendering?
246
247 // Animation info: cracks
248 // Last crack value passed to animate()
250 // Maps mesh and mesh buffer (i.e. material) indices to base texture names
251 std::map<std::pair<u8, u32>, std::string> m_crack_materials;
252
253 // Animation info: texture animation
254 // Maps mesh and mesh buffer indices to TileSpecs
255 // Keys are pairs of (mesh index, buffer index in the mesh)
256 std::map<std::pair<u8, u32>, AnimationInfo> m_animation_info;
257
258 // list of all semitransparent triangles in the mapblock
259 std::vector<MeshTriangle> m_transparent_triangles;
260 // Binary Space Partitioning tree for the block
262 // Ordered list of references to parts of transparent buffers to draw
263 std::vector<PartialMeshBuffer> m_transparent_buffers;
264 // Is m_transparent_buffers currently in consolidated form?
266};
267
280video::SColor encode_light(u16 light, u8 emissive_light);
281
282// Compute light at node
283u16 getInteriorLight(MapNode n, s32 increment, const NodeDefManager *ndef);
284u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef);
285u16 getSmoothLightSolid(const v3s16 &p, const v3s16 &face_dir, const v3s16 &corner, MeshMakeData *data);
286u16 getSmoothLightTransparent(const v3s16 &p, const v3s16 &corner, MeshMakeData *data);
287
292void get_sunlight_color(video::SColorf *sunlight, u32 daynight_ratio);
293
301void final_color_blend(video::SColor *result,
302 u16 light, u32 daynight_ratio);
303
311void final_color_blend(video::SColor *result,
312 const video::SColor &data, const video::SColorf &dayLight);
313
314// Retrieves the TileSpec of a face of a node
315// Adds MATERIAL_FLAG_CRACK if the node is cracked
316// TileSpec should be passed as reference due to the underlying TileFrame and its vector
317// TileFrame vector copy cost very much to client
318void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile);
319void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile);
320
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:91
std::vector< TreeNode > nodes
Definition mapblock_mesh.h:123
void buildTree(const std::vector< MeshTriangle > *triangles, u16 side_lingth)
Definition mapblock_mesh.cpp:426
const std::vector< MeshTriangle > * triangles
Definition mapblock_mesh.h:122
s32 root
Definition mapblock_mesh.h:124
void traverse(v3f viewpoint, std::vector< s32 > &output) const
Definition mapblock_mesh.h:97
MapBlockBspTree()
Definition mapblock_mesh.h:93
Definition mapblock_mesh.h:164
std::vector< MinimapMapblock * > m_minimap_mapblocks
Definition mapblock_mesh.h:236
~MapBlockMesh()
Definition mapblock_mesh.cpp:750
MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset)
Definition mapblock_mesh.cpp:593
f32 getBoundingRadius() const
Radius of the bounding-sphere, in BS-space.
Definition mapblock_mesh.h:207
bool isAnimationForced() const
Definition mapblock_mesh.h:195
const std::vector< PartialMeshBuffer > & getTransparentBuffers() const
get the list of transparent buffers
Definition mapblock_mesh.h:223
v3f getBoundingSphereCenter() const
Center of the bounding-sphere, in BS-space, relative to block pos.
Definition mapblock_mesh.h:210
int m_animation_force_timer
Definition mapblock_mesh.h:245
std::map< std::pair< u8, u32 >, std::string > m_crack_materials
Definition mapblock_mesh.h:251
MapBlockBspTree m_bsp_tree
Definition mapblock_mesh.h:261
std::vector< MinimapMapblock * > moveMinimapMapblocks()
Definition mapblock_mesh.h:188
void consolidateTransparentBuffers()
Definition mapblock_mesh.cpp:891
f32 m_bounding_radius
Definition mapblock_mesh.h:240
scene::IMesh * getMesh()
Definition mapblock_mesh.h:178
bool animate(bool faraway, float time, int crack, u32 daynight_ratio)
Definition mapblock_mesh.cpp:764
void decreaseAnimationForceTimer()
Definition mapblock_mesh.h:200
void updateTransparentBuffers(v3f camera_pos, v3s16 block_pos, bool group_by_buffers)
Update transparent buffers to render towards the camera.
Definition mapblock_mesh.cpp:822
std::vector< MeshTriangle > m_transparent_triangles
Definition mapblock_mesh.h:259
irr_ptr< scene::IMesh > m_mesh[MAX_TILE_LAYERS]
Definition mapblock_mesh.h:235
bool m_transparent_buffers_consolidated
Definition mapblock_mesh.h:265
IShaderSource * m_shdrsrc
Definition mapblock_mesh.h:238
ITextureSource * m_tsrc
Definition mapblock_mesh.h:237
int m_last_crack
Definition mapblock_mesh.h:249
bool m_has_animation
Definition mapblock_mesh.h:244
scene::IMesh * getMesh(u8 layer)
Definition mapblock_mesh.h:183
std::map< std::pair< u8, u32 >, AnimationInfo > m_animation_info
Definition mapblock_mesh.h:256
v3f m_bounding_sphere_center
Definition mapblock_mesh.h:241
std::vector< PartialMeshBuffer > m_transparent_buffers
Definition mapblock_mesh.h:263
Definition mapblock.h:58
Definition mapblock_mesh.h:60
scene::SMeshBuffer * buffer
Definition mapblock_mesh.h:62
u16 p3
Definition mapblock_mesh.h:63
float areaSQ
Definition mapblock_mesh.h:65
v3f getNormal() const
Definition mapblock_mesh.h:77
u16 p2
Definition mapblock_mesh.h:63
v3f centroid
Definition mapblock_mesh.h:64
void updateAttributes()
Definition mapblock_mesh.h:67
u16 p1
Definition mapblock_mesh.h:63
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:540
Definition mapblock_mesh.h:134
void draw(video::IVideoDriver *driver) const
Definition mapblock_mesh.cpp:582
irr_ptr< scene::SIndexBuffer > m_indices
Definition mapblock_mesh.h:149
scene::SMeshBuffer * m_buffer
Definition mapblock_mesh.h:148
PartialMeshBuffer(scene::SMeshBuffer *buffer, std::vector< u16 > &&vertex_indices)
Definition mapblock_mesh.h:136
auto * getBuffer() const
Definition mapblock_mesh.h:143
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:322
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:949
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:279
video::SColor encode_light(u16 light, u8 emissive_light)
Definition mapblock_mesh.cpp:921
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:342
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:105
v3f normal
Definition mapblock_mesh.h:106
std::vector< s32 > triangle_refs
Definition mapblock_mesh.h:108
v3f origin
Definition mapblock_mesh.h:107
s32 front_ref
Definition mapblock_mesh.h:109
s32 back_ref
Definition mapblock_mesh.h:110
TreeNode(v3f normal, v3f origin, const std::vector< s32 > &triangle_refs, s32 front_ref, s32 back_ref)
Definition mapblock_mesh.h:113
Definition mapblock_mesh.h:229
int frame_offset
Definition mapblock_mesh.h:231
TileLayer tile
Definition mapblock_mesh.h:232
int frame
Definition mapblock_mesh.h:230
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
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
MeshMakeData(const NodeDefManager *ndef, u16 side_length)
Definition mapblock_mesh.cpp:29
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:139
static std::string p(std::string path)
Definition test_filesys.cpp:53
#define MAX_TILE_LAYERS
Definition tile.h:52