From c6514225f8273df2e02898aacd050933deeee331 Mon Sep 17 00:00:00 2001 From: Desour Date: Wed, 8 Jan 2025 10:01:13 +0100 Subject: [PATCH] MeshMakeData: Explain members, and add grid size and minimap flag --- src/client/content_mapblock.cpp | 8 ++--- src/client/mapblock_mesh.cpp | 50 +++++++++++++------------- src/client/mapblock_mesh.h | 16 +++++++-- src/client/mesh_generator_thread.cpp | 4 ++- src/client/wieldmesh.cpp | 3 +- src/unittest/test_content_mapblock.cpp | 3 +- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/client/content_mapblock.cpp b/src/client/content_mapblock.cpp index 56d0bd4985c6..5b69b4a5da18 100644 --- a/src/client/content_mapblock.cpp +++ b/src/client/content_mapblock.cpp @@ -62,7 +62,7 @@ const std::string MapblockMeshGenerator::raillike_groupname = "connect_to_railli MapblockMeshGenerator::MapblockMeshGenerator(MeshMakeData *input, MeshCollector *output): data(input), collector(output), - nodedef(data->nodedef), + nodedef(data->m_nodedef), blockpos_nodes(data->m_blockpos * MAP_BLOCKSIZE) { } @@ -1773,9 +1773,9 @@ void MapblockMeshGenerator::generate() { ZoneScoped; - for (cur_node.p.Z = 0; cur_node.p.Z < data->side_length; cur_node.p.Z++) - for (cur_node.p.Y = 0; cur_node.p.Y < data->side_length; cur_node.p.Y++) - for (cur_node.p.X = 0; cur_node.p.X < data->side_length; cur_node.p.X++) { + for (cur_node.p.Z = 0; cur_node.p.Z < data->m_side_length; cur_node.p.Z++) + for (cur_node.p.Y = 0; cur_node.p.Y < data->m_side_length; cur_node.p.Y++) + for (cur_node.p.X = 0; cur_node.p.X < data->m_side_length; cur_node.p.X++) { cur_node.n = data->m_vmanip.getNodeNoEx(blockpos_nodes + cur_node.p); cur_node.f = &nodedef->get(cur_node.n); drawNode(); diff --git a/src/client/mapblock_mesh.cpp b/src/client/mapblock_mesh.cpp index 572226cdfa62..a3a1131758e6 100644 --- a/src/client/mapblock_mesh.cpp +++ b/src/client/mapblock_mesh.cpp @@ -26,9 +26,11 @@ MeshMakeData */ -MeshMakeData::MeshMakeData(const NodeDefManager *ndef, u16 side_length): - side_length(side_length), - nodedef(ndef) +MeshMakeData::MeshMakeData(const NodeDefManager *ndef, + u16 side_length, MeshGrid mesh_grid) : + m_side_length(side_length), + m_mesh_grid(mesh_grid), + m_nodedef(ndef) {} void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos) @@ -38,8 +40,9 @@ void MeshMakeData::fillBlockDataBegin(const v3s16 &blockpos) v3s16 blockpos_nodes = m_blockpos*MAP_BLOCKSIZE; m_vmanip.clear(); + // extra 1 block thick layer around the mesh VoxelArea voxel_area(blockpos_nodes - v3s16(1,1,1) * MAP_BLOCKSIZE, - blockpos_nodes + v3s16(1,1,1) * (side_length + MAP_BLOCKSIZE /* extra layer of blocks around the mesh */) - v3s16(1,1,1)); + blockpos_nodes + v3s16(1,1,1) * (m_side_length + MAP_BLOCKSIZE) - v3s16(1,1,1)); m_vmanip.addArea(voxel_area); } @@ -128,7 +131,7 @@ u16 getFaceLight(MapNode n, MapNode n2, const NodeDefManager *ndef) static u16 getSmoothLightCombined(const v3s16 &p, const std::array &dirs, MeshMakeData *data) { - const NodeDefManager *ndef = data->nodedef; + const NodeDefManager *ndef = data->m_nodedef; u16 ambient_occlusion = 0; u16 light_count = 0; @@ -316,7 +319,7 @@ void final_color_blend(video::SColor *result, */ void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, TileSpec &tile) { - const NodeDefManager *ndef = data->nodedef; + const NodeDefManager *ndef = data->m_nodedef; const ContentFeatures &f = ndef->get(mn); tile = f.tiles[tileindex]; bool has_crack = p == data->m_crack_pos_relative; @@ -336,7 +339,7 @@ void getNodeTileN(MapNode mn, const v3s16 &p, u8 tileindex, MeshMakeData *data, */ void getNodeTile(MapNode mn, const v3s16 &p, const v3s16 &dir, MeshMakeData *data, TileSpec &tile) { - const NodeDefManager *ndef = data->nodedef; + const NodeDefManager *ndef = data->m_nodedef; // Direction must be (1,0,0), (-1,0,0), (0,1,0), (0,-1,0), // (0,0,1), (0,0,-1) or (0,0,0) @@ -588,7 +591,7 @@ void PartialMeshBuffer::draw(video::IVideoDriver *driver) const MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offset): m_tsrc(client->getTextureSource()), m_shdrsrc(client->getShaderSource()), - m_bounding_sphere_center((data->side_length * 0.5f - 0.5f) * BS), + m_bounding_sphere_center((data->m_side_length * 0.5f - 0.5f) * BS), m_animation_force_timer(0), // force initial animation m_last_crack(-1) { @@ -597,10 +600,12 @@ MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offs for (auto &m : m_mesh) m = make_irr(); - auto mesh_grid = client->getMeshGrid(); + auto mesh_grid = data->m_mesh_grid; v3s16 bp = data->m_blockpos; - // Only generate minimap mapblocks at even coordinates. - if (mesh_grid.isMeshPos(bp) && client->getMinimap()) { + // Only generate minimap mapblocks at grid aligned coordinates. + // FIXME: ^ doesn't really make sense. and in practice, bp is always aligned + if (mesh_grid.isMeshPos(bp) && data->m_generate_minimap) { + // meshgen area always fits into a grid cell m_minimap_mapblocks.resize(mesh_grid.getCellVolume(), nullptr); v3s16 ofs; @@ -617,15 +622,10 @@ MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offs } } + // algin vertices to mesh grid, not meshgen area v3f offset = intToFloat((data->m_blockpos - mesh_grid.getMeshPos(data->m_blockpos)) * MAP_BLOCKSIZE, BS); + MeshCollector collector(m_bounding_sphere_center, offset); - /* - Add special graphics: - - torches - - flowing water - - fences - - whatever - */ { MapblockMeshGenerator(data, &collector).generate(); @@ -743,7 +743,7 @@ MapBlockMesh::MapBlockMesh(Client *client, MeshMakeData *data, v3s16 camera_offs } } - m_bsp_tree.buildTree(&m_transparent_triangles, data->side_length); + m_bsp_tree.buildTree(&m_transparent_triangles, data->m_side_length); // Check if animation is required for this mesh m_has_animation = @@ -954,19 +954,19 @@ u8 get_solid_sides(MeshMakeData *data) { std::unordered_map results; v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE; - const NodeDefManager *ndef = data->nodedef; + const NodeDefManager *ndef = data->m_nodedef; u8 result = 0x3F; // all sides solid; - for (s16 i = 0; i < data->side_length && result != 0; i++) - for (s16 j = 0; j < data->side_length && result != 0; j++) { + for (s16 i = 0; i < data->m_side_length && result != 0; i++) + for (s16 j = 0; j < data->m_side_length && result != 0; j++) { v3s16 positions[6] = { v3s16(0, i, j), - v3s16(data->side_length - 1, i, j), + v3s16(data->m_side_length - 1, i, j), v3s16(i, 0, j), - v3s16(i, data->side_length - 1, j), + v3s16(i, data->m_side_length - 1, j), v3s16(i, j, 0), - v3s16(i, j, data->side_length - 1) + v3s16(i, j, data->m_side_length - 1) }; for (u8 k = 0; k < 6; k++) { diff --git a/src/client/mapblock_mesh.h b/src/client/mapblock_mesh.h index c4380432fff2..47941386ea78 100644 --- a/src/client/mapblock_mesh.h +++ b/src/client/mapblock_mesh.h @@ -36,15 +36,25 @@ struct MinimapMapblock; struct MeshMakeData { VoxelManipulator m_vmanip; + + // base pos of meshgen area, in blocks v3s16 m_blockpos = v3s16(-1337,-1337,-1337); + // size of meshgen area, in nodes. + // vmanip will have at least an extra 1 node onion layer. + // area is expected to fit into mesh grid cell. + u16 m_side_length; + // vertex positions will be relative to this grid + MeshGrid m_mesh_grid; + + // relative to blockpos v3s16 m_crack_pos_relative = v3s16(-1337,-1337,-1337); + bool m_generate_minimap = false; bool m_smooth_lighting = false; bool m_enable_water_reflections = false; - u16 side_length; - const NodeDefManager *nodedef; + const NodeDefManager *m_nodedef; - MeshMakeData(const NodeDefManager *ndef, u16 side_length); + MeshMakeData(const NodeDefManager *ndef, u16 side_lingth, MeshGrid mesh_grid); /* Copy block data manually (to allow optimizations by the caller) diff --git a/src/client/mesh_generator_thread.cpp b/src/client/mesh_generator_thread.cpp index 0b85a1bc96d6..70f4287ab5ab 100644 --- a/src/client/mesh_generator_thread.cpp +++ b/src/client/mesh_generator_thread.cpp @@ -177,7 +177,8 @@ void MeshUpdateQueue::done(v3s16 pos) void MeshUpdateQueue::fillDataFromMapBlocks(QueuedMeshUpdate *q) { auto mesh_grid = m_client->getMeshGrid(); - MeshMakeData *data = new MeshMakeData(m_client->ndef(), MAP_BLOCKSIZE * mesh_grid.cell_size); + MeshMakeData *data = new MeshMakeData(m_client->ndef(), + MAP_BLOCKSIZE * mesh_grid.cell_size, mesh_grid); q->data = data; data->fillBlockDataBegin(q->p); @@ -192,6 +193,7 @@ void MeshUpdateQueue::fillDataFromMapBlocks(QueuedMeshUpdate *q) } data->setCrack(q->crack_level, q->crack_pos); + data->m_generate_minimap = !!m_client->getMinimap(); data->m_smooth_lighting = m_cache_smooth_lighting; data->m_enable_water_reflections = m_cache_enable_water_reflections; } diff --git a/src/client/wieldmesh.cpp b/src/client/wieldmesh.cpp index a557fc2a2a84..4c4042ad48ab 100644 --- a/src/client/wieldmesh.cpp +++ b/src/client/wieldmesh.cpp @@ -309,8 +309,9 @@ void WieldMeshSceneNode::setExtruded(const std::string &imagename, static scene::SMesh *createSpecialNodeMesh(Client *client, MapNode n, std::vector *colors, const ContentFeatures &f) { - MeshMakeData mesh_make_data(client->ndef(), 1); + MeshMakeData mesh_make_data(client->ndef(), 1, client->getMeshGrid()); MeshCollector collector(v3f(0.0f * BS), v3f()); + mesh_make_data.m_generate_minimap = false; mesh_make_data.m_smooth_lighting = false; mesh_make_data.m_enable_water_reflections = false; MapblockMeshGenerator gen(&mesh_make_data, &collector); diff --git a/src/unittest/test_content_mapblock.cpp b/src/unittest/test_content_mapblock.cpp index e7148a69f673..68f66eabf060 100644 --- a/src/unittest/test_content_mapblock.cpp +++ b/src/unittest/test_content_mapblock.cpp @@ -38,7 +38,8 @@ class MockGameDef : public DummyGameDef { MeshMakeData makeSingleNodeMMD(bool smooth_lighting = true) { - MeshMakeData data{ndef(), 1}; + MeshMakeData data{ndef(), 1, MeshGrid{1}}; + data.m_generate_minimap = false; data.m_smooth_lighting = smooth_lighting; data.m_enable_water_reflections = false; data.m_blockpos = {0, 0, 0};