3 Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
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.
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.
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.
20 #include "collector.h"
23 #include "client/mesh.h"
25 void MeshCollector::append(const TileSpec &tile, const video::S3DVertex *vertices,
26 u32 numVertices, const u16 *indices, u32 numIndices)
28 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
29 const TileLayer *layer = &tile.layers[layernum];
30 if (layer->texture_id == 0)
32 append(*layer, vertices, numVertices, indices, numIndices, layernum,
37 void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *vertices,
38 u32 numVertices, const u16 *indices, u32 numIndices, u8 layernum,
41 PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
45 scale = 1.0f / layer.scale;
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());
55 for (u32 i = 0; i < numIndices; i++)
56 p.indices.push_back(indices[i] + vertex_count);
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)
63 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
64 const TileLayer *layer = &tile.layers[layernum];
65 if (layer->texture_id == 0)
67 append(*layer, vertices, numVertices, indices, numIndices, pos, c,
68 light_source, layernum, tile.world_aligned);
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)
76 PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
80 scale = 1.0f / layer.scale;
82 u32 vertex_count = p.vertices.size();
83 for (u32 i = 0; i < numVertices; i++) {
84 video::SColor color = c;
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());
94 for (u32 i = 0; i < numIndices; i++)
95 p.indices.push_back(indices[i] + vertex_count);
98 PreMeshBuffer &MeshCollector::findBuffer(
99 const TileLayer &layer, u8 layernum, u32 numVertices)
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)
108 buffers.emplace_back(layer);
109 return buffers.back();