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"
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, vertices[i].Normal,
50 vertices[i].Color, scale * vertices[i].TCoords);
52 for (u32 i = 0; i < numIndices; i++)
53 p.indices.push_back(indices[i] + vertex_count);
56 void MeshCollector::append(const TileSpec &tile, const video::S3DVertex *vertices,
57 u32 numVertices, const u16 *indices, u32 numIndices, v3f pos,
58 video::SColor c, u8 light_source)
60 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
61 const TileLayer *layer = &tile.layers[layernum];
62 if (layer->texture_id == 0)
64 append(*layer, vertices, numVertices, indices, numIndices, pos, c,
65 light_source, layernum, tile.world_aligned);
69 void MeshCollector::append(const TileLayer &layer, const video::S3DVertex *vertices,
70 u32 numVertices, const u16 *indices, u32 numIndices, v3f pos,
71 video::SColor c, u8 light_source, u8 layernum, bool use_scale)
73 PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
77 scale = 1.0f / layer.scale;
79 u32 vertex_count = p.vertices.size();
80 for (u32 i = 0; i < numVertices; i++) {
81 video::SColor color = c;
83 applyFacesShading(color, vertices[i].Normal);
84 p.vertices.emplace_back(vertices[i].Pos + pos, vertices[i].Normal, color,
85 scale * vertices[i].TCoords);
88 for (u32 i = 0; i < numIndices; i++)
89 p.indices.push_back(indices[i] + vertex_count);
92 PreMeshBuffer &MeshCollector::findBuffer(
93 const TileLayer &layer, u8 layernum, u32 numVertices)
95 if (numVertices > U16_MAX)
96 throw std::invalid_argument(
97 "Mesh can't contain more than 65536 vertices");
98 std::vector<PreMeshBuffer> &buffers = prebuffers[layernum];
99 for (PreMeshBuffer &p : buffers)
100 if (p.layer == layer && p.vertices.size() + numVertices <= U16_MAX)
102 buffers.emplace_back(layer);
103 return buffers.back();