]> git.lizzy.rs Git - minetest.git/blob - src/client/meshgen/collector.cpp
Move client-specific files to 'src/client' (#7902)
[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, vertices[i].Normal,
50                                 vertices[i].Color, scale * vertices[i].TCoords);
51
52         for (u32 i = 0; i < numIndices; i++)
53                 p.indices.push_back(indices[i] + vertex_count);
54 }
55
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)
59 {
60         for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
61                 const TileLayer *layer = &tile.layers[layernum];
62                 if (layer->texture_id == 0)
63                         continue;
64                 append(*layer, vertices, numVertices, indices, numIndices, pos, c,
65                                 light_source, layernum, tile.world_aligned);
66         }
67 }
68
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)
72 {
73         PreMeshBuffer &p = findBuffer(layer, layernum, numVertices);
74
75         f32 scale = 1.0f;
76         if (use_scale)
77                 scale = 1.0f / layer.scale;
78
79         u32 vertex_count = p.vertices.size();
80         for (u32 i = 0; i < numVertices; i++) {
81                 video::SColor color = c;
82                 if (!light_source)
83                         applyFacesShading(color, vertices[i].Normal);
84                 p.vertices.emplace_back(vertices[i].Pos + pos, vertices[i].Normal, color,
85                                 scale * vertices[i].TCoords);
86         }
87
88         for (u32 i = 0; i < numIndices; i++)
89                 p.indices.push_back(indices[i] + vertex_count);
90 }
91
92 PreMeshBuffer &MeshCollector::findBuffer(
93                 const TileLayer &layer, u8 layernum, u32 numVertices)
94 {
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)
101                         return p;
102         buffers.emplace_back(layer);
103         return buffers.back();
104 }