]> git.lizzy.rs Git - minetest.git/blob - src/client/meshgen/collector.cpp
8x block meshes (#13133)
[minetest.git] / src / client / meshgen / collector.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "collector.h"
21 #include <stdexcept>
22 #include "log.h"
23 #include "client/mesh.h"
24
25 void MeshCollector::append(const TileSpec &tile, const video::S3DVertex *vertices,
26                 u32 numVertices, const u16 *indices, u32 numIndices)
27 {
28         for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
29                 const TileLayer *layer = &tile.layers[layernum];
30                 if (layer->texture_id == 0)
31                         continue;
32                 append(*layer, vertices, numVertices, indices, numIndices, layernum,
33                                 tile.world_aligned);
34         }
35 }
36
37 void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *vertices,
38                 u32 numVertices, const u16 *indices, u32 numIndices, u8 layernum,
39                 bool use_scale)
40 {
41         PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
42
43         f32 scale = 1.0f;
44         if (use_scale)
45                 scale = 1.0f / layer.scale;
46
47         u32 vertex_count = p.vertices.size();
48         for (u32 i = 0; i < numVertices; i++) {
49                 p.vertices.emplace_back(vertices[i].Pos + offset, vertices[i].Normal,
50                                 vertices[i].Color, scale * vertices[i].TCoords);
51                 m_bounding_radius_sq = std::max(m_bounding_radius_sq,
52                                 (vertices[i].Pos - m_center_pos).getLengthSQ());
53         }
54
55         for (u32 i = 0; i < numIndices; i++)
56                 p.indices.push_back(indices[i] + vertex_count);
57 }
58
59 void MeshCollector::append(const TileSpec &tile, const video::S3DVertex *vertices,
60                 u32 numVertices, const u16 *indices, u32 numIndices, v3f pos,
61                 video::SColor c, u8 light_source)
62 {
63         for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
64                 const TileLayer *layer = &tile.layers[layernum];
65                 if (layer->texture_id == 0)
66                         continue;
67                 append(*layer, vertices, numVertices, indices, numIndices, pos, c,
68                                 light_source, layernum, tile.world_aligned);
69         }
70 }
71
72 void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *vertices,
73                 u32 numVertices, const u16 *indices, u32 numIndices, v3f pos,
74                 video::SColor c, u8 light_source, u8 layernum, bool use_scale)
75 {
76         PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
77
78         f32 scale = 1.0f;
79         if (use_scale)
80                 scale = 1.0f / layer.scale;
81
82         u32 vertex_count = p.vertices.size();
83         for (u32 i = 0; i < numVertices; i++) {
84                 video::SColor color = c;
85                 if (!light_source)
86                         applyFacesShading(color, vertices[i].Normal);
87                 auto vpos = vertices[i].Pos + pos + offset;
88                 p.vertices.emplace_back(vpos, vertices[i].Normal, color,
89                                 scale * vertices[i].TCoords);
90                 m_bounding_radius_sq = std::max(m_bounding_radius_sq,
91                                 (vpos - m_center_pos).getLengthSQ());
92         }
93
94         for (u32 i = 0; i < numIndices; i++)
95                 p.indices.push_back(indices[i] + vertex_count);
96 }
97
98 PreMeshBuffer &MeshCollector::findBuffer(
99                 const TileLayer &layer, u8 layernum, u32 numVertices)
100 {
101         if (numVertices > U16_MAX)
102                 throw std::invalid_argument(
103                                 "Mesh can't contain more than 65536 vertices");
104         std::vector<PreMeshBuffer> &buffers = prebuffers[layernum];
105         for (PreMeshBuffer &p : buffers)
106                 if (p.layer == layer && p.vertices.size() + numVertices <= U16_MAX)
107                         return p;
108         buffers.emplace_back(layer);
109         return buffers.back();
110 }