-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmesh_manager.cpp
More file actions
365 lines (301 loc) · 13.2 KB
/
Copy pathmesh_manager.cpp
File metadata and controls
365 lines (301 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "graphics/mesh_manager.h"
#include <cmath>
#include <cstdint>
#include <queue>
#include <sstream>
#include <stack>
#include <vector>
#include "core/colour.h"
#include "core/error_handling.h"
#include "core/transform.h"
#include "core/vector3.h"
#include "graphics/bone.h"
#include "graphics/mesh.h"
#include "graphics/mesh_loader.h"
#include "graphics/skeleton.h"
#include "graphics/texture.h"
#include "graphics/vertex_data.h"
#include "log/log.h"
namespace iris
{
MeshManager::MeshManager(ResourceManager &resource_manager, bool flip_uvs_on_load)
: resource_manager_(resource_manager)
, loaded_meshes_()
, loaded_animations_()
, loaded_skeletons_()
, skeleton_copies_()
, flip_uvs_on_load_(flip_uvs_on_load)
{
}
const Mesh *MeshManager::sprite(const Colour &colour)
{
// create a unique for this mesh
std::stringstream strm{};
strm << "!sprite" << colour;
const auto id = strm.str();
if (loaded_meshes_.count(id) == 0u)
{
std::vector<VertexData> vertices{
{{-1.0, 1.0, 0.0f}, {}, colour, {0.0f, 0.0f, 0.0f}},
{{1.0, 1.0, 0.0f}, {}, colour, {1.0f, 0.0f, 0.0f}},
{{1.0, -1.0, 0.0f}, {}, colour, {1.0f, 1.0f, 0.0f}},
{{-1.0, -1.0, 0.0f}, {}, colour, {0.0f, 1.0f, 0.0f}}};
std::vector<std::uint32_t> indices{0, 2, 1, 3, 2, 0};
loaded_meshes_[id].push_back({.mesh = create_mesh(vertices, indices)});
}
return loaded_meshes_[id].front().mesh.get();
}
const Mesh *MeshManager::cube(const Colour &colour)
{
// create a unique for this mesh
std::stringstream strm{};
strm << "!cube" << colour;
const auto id = strm.str();
if (loaded_meshes_.count(id) == 0u)
{
loaded_meshes_[id].push_back({.mesh = unique_cube(colour)});
}
return loaded_meshes_[id].front().mesh.get();
}
std::unique_ptr<Mesh> MeshManager::unique_cube(const Colour &colour) const
{
std::vector<VertexData> vertices{
{{1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, colour, {0.0f, 0.0f, 0.0f}},
{{-1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, colour, {1.0f, 0.0f, 0.0f}},
{{-1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, colour, {1.0f, 1.0f, 0.0f}},
{{1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, colour, {0.0f, 1.0f, 0.0f}},
{{1.0f, -1.0f, -1.0f}, {0.0f, -1.0f, 0.0f}, colour, {0.0f, 0.0f, 0.0f}},
{{1.0f, -1.0f, 1.0f}, {0.0f, -1.0f, 0.0f}, colour, {1.0f, 0.0f, 0.0f}},
{{-1.0f, -1.0f, 1.0f}, {0.0f, -1.0f, 0.0f}, colour, {1.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, -1.0f}, {0.0f, -1.0f, 0.0f}, colour, {0.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}, colour, {0.0f, 0.0f, 0.0f}},
{{-1.0f, -1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, colour, {1.0f, 0.0f, 0.0f}},
{{-1.0f, 1.0f, 1.0f}, {-1.0f, 0.0f, 0.0f}, colour, {1.0f, 1.0f, 0.0f}},
{{-1.0f, 1.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}, colour, {0.0f, 1.0f, 0.0f}},
{{-1.0f, 1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, colour, {0.0f, 0.0f, 0.0f}},
{{1.0f, 1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, colour, {1.0f, 0.0f, 0.0f}},
{{1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, colour, {1.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, -1.0f}, colour, {0.0f, 1.0f, 0.0f}},
{{1.0f, 1.0f, -1.0f}, {1.0f, 0.0f, 0.0f}, colour, {0.0f, 0.0f, 0.0f}},
{{1.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, colour, {1.0f, 0.0f, 0.0f}},
{{1.0f, -1.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, colour, {1.0f, 1.0f, 0.0f}},
{{1.0f, -1.0f, -1.0f}, {1.0f, 0.0f, 0.0f}, colour, {0.0f, 1.0f, 0.0f}},
{{-1.0f, 1.0f, -1.0f}, {0.0f, 1.0f, 0.0f}, colour, {0.0f, 0.0f, 0.0f}},
{{-1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, colour, {1.0f, 0.0f, 0.0f}},
{{1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, colour, {1.0f, 1.0f, 0.0f}},
{{1.0f, 1.0f, -1.0f}, {0.0f, 1.0f, 0.0f}, colour, {0.0f, 1.0f, 0.0f}}};
std::vector<std::uint32_t> indices{0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23};
return create_mesh(vertices, indices);
}
std::unique_ptr<Mesh> MeshManager::unique_mesh(
const std::vector<iris::VertexData> &vertices,
const std::vector<std::uint32_t> &indices) const
{
return create_mesh(vertices, indices);
}
const Mesh *MeshManager::plane(const Colour &colour, std::uint32_t divisions, float scale)
{
expect(divisions != 0, "divisions must be >= 0");
// create a unique for this mesh
std::stringstream strm{};
strm << "!plane" << colour << ":" << divisions;
const auto id = strm.str();
if (loaded_meshes_.count(id) == 0u)
{
std::vector<VertexData> vertices(static_cast<std::size_t>(std::pow(divisions + 1u, 2u)));
const Vector3 normal{0.0f, 1.0f, 0.0f};
const Vector3 tangent{1.0f, 0.0f, 0.0f};
const Vector3 bitangent{0.0f, 0.0f, 1.0f};
const auto width_unit = (1.0f / static_cast<float>(divisions));
const auto width = width_unit * scale;
const auto offset = 0.5f * scale;
for (auto y = 0u; y <= divisions; ++y)
{
for (auto x = 0u; x <= divisions; ++x)
{
vertices[(y * (divisions + 1u)) + x] = {
{(x * width) - offset, 0.0f, (y * width) - offset},
normal,
colour,
{(x * width_unit) * divisions, (scale - (y * width_unit)) * divisions, 0.0f},
tangent,
bitangent};
}
}
std::vector<std::uint32_t> indices{};
for (auto y = 0u; y < divisions; ++y)
{
for (auto x = 0u; x < divisions; ++x)
{
indices.emplace_back((y * (divisions + 1u) + x));
indices.emplace_back(((y + 1) * (divisions + 1u) + x));
indices.emplace_back((y * (divisions + 1u) + x + 1u));
indices.emplace_back((y * (divisions + 1u) + x + 1u));
indices.emplace_back(((y + 1) * (divisions + 1u) + x));
indices.emplace_back(((y + 1) * (divisions + 1u) + x + 1u));
}
}
loaded_meshes_[id].push_back({.mesh = create_mesh(vertices, indices)});
}
return loaded_meshes_[id].front().mesh.get();
}
const Mesh *MeshManager::heightmap(const Colour &colour, const Texture *height_image)
{
ensure(height_image->width() == height_image->height(), "height_image must be square");
// create a unique for this mesh
std::stringstream strm{};
strm << "!height_map" << colour << ":" << height_image;
const auto id = strm.str();
const auto divisions = height_image->width();
const auto &height_data = height_image->data();
if (loaded_meshes_.count(id) == 0u)
{
std::vector<VertexData> vertices(static_cast<std::size_t>(std::pow(divisions, 2u)));
const Vector3 tangent{1.0f, 0.0f, 0.0f};
const Vector3 bitangent{0.0f, 1.0f, 0.0f};
const auto width = 1.0f / static_cast<float>(divisions);
// lambda to get adjacent points (clamped to edges)
const auto get_adjacent =
[&height_data,
divisions,
width](std::uint32_t x, std::uint32_t z, std::int32_t offset_x, std::int32_t offset_z) -> Vector3 {
auto adj_x = x;
if (x != 0u && offset_x == -1)
{
--adj_x;
}
else if (x != (divisions - 1u) && offset_x == 1)
{
++adj_x;
}
auto adj_z = z;
if (z != 0u && offset_z == -1)
{
--adj_z;
}
else if (z != (divisions - 1u) && offset_z == 1)
{
++adj_z;
}
const auto raw_y = height_data[((adj_z * divisions) + adj_x) * 4u];
const auto y = static_cast<float>(raw_y) / 255.0f;
return {(adj_x * width) - 0.5f, y, (adj_z * width) - 0.5f};
};
for (auto z = 0u; z < divisions; ++z)
{
for (auto x = 0u; x < divisions; ++x)
{
const auto right = get_adjacent(x, z, 1, 0);
const auto left = get_adjacent(x, z, -1, 0);
const auto top = get_adjacent(x, z, 0, -1);
const auto bottom = get_adjacent(x, z, 0, 1);
const auto raw_y = height_data[((z * divisions) + x) * 4u];
const auto y = static_cast<float>(raw_y) / 255.0f;
vertices[(z * (divisions)) + x] = {
{(x * width) - 0.5f, y, (z * width) - 0.5f},
Vector3::normalise(Vector3::cross((right - left), (top - bottom))),
colour,
{(x * width) * 30.0f, (1.0f - (z * width)) * 30.0f, 0.0f},
tangent,
bitangent};
}
}
std::vector<std::uint32_t> indices{};
for (auto z = 0u; z < divisions - 1u; ++z)
{
for (auto x = 0u; x < divisions - 1u; ++x)
{
indices.emplace_back((z * (divisions) + x));
indices.emplace_back(((z + 1) * (divisions) + x));
indices.emplace_back((z * (divisions) + x + 1u));
indices.emplace_back((z * (divisions) + x + 1u));
indices.emplace_back(((z + 1) * (divisions) + x));
indices.emplace_back(((z + 1) * (divisions) + x + 1u));
}
}
loaded_meshes_[id].push_back({.mesh = create_mesh(vertices, indices)});
}
return loaded_meshes_[id].front().mesh.get();
}
const Mesh *MeshManager::quad(
const Colour &colour,
const Vector3 &lower_left,
const Vector3 &lower_right,
const Vector3 &upper_left,
const Vector3 &upper_right)
{
// create a unique for this mesh
std::stringstream strm{};
strm << "!quad" << colour << ":" << lower_left << ":" << lower_right << ":" << upper_left << ":" << upper_right;
const auto id = strm.str();
if (loaded_meshes_.count(id) == 0u)
{
std::vector<VertexData> vertices{
{upper_left, {}, colour, {0.0f, 1.0f, 0.0f}},
{upper_right, {}, colour, {1.0f, 1.0f, 0.0f}},
{lower_right, {}, colour, {1.0f, 0.0f, 0.0f}},
{lower_left, {}, colour, {0.0f, 0.0f, 0.0f}}};
std::vector<std::uint32_t> indices{0, 2, 1, 3, 2, 0};
loaded_meshes_[id].push_back({.mesh = create_mesh(vertices, indices)});
}
return loaded_meshes_[id].front().mesh.get();
}
MeshManager::Meshes MeshManager::load_mesh(const std::string &mesh_file)
{
if (!loaded_meshes_.contains(mesh_file))
{
expect(!loaded_animations_.contains(mesh_file), "unexpected animations");
expect(!loaded_skeletons_.contains(mesh_file), "unexpected skeleton");
mesh_loader::load(
resource_manager_,
mesh_file,
flip_uvs_on_load_,
[this, &mesh_file](auto vertices, auto indices, auto weights, const auto &texture_name) {
if (loaded_skeletons_.contains(mesh_file))
{
const auto &skeleton = loaded_skeletons_[mesh_file];
std::vector<std::uint32_t> bone_indices(vertices.size());
// stamp bone data into loaded vertices
for (const auto &[id, weight, bone_name] : weights)
{
if (weight == 0.0f)
{
continue;
}
// only support four bones per vertex
if (bone_indices[id] >= 4)
{
LOG_ENGINE_WARN("mf", "too many weights {} {}", id, weight);
continue;
}
const auto bone_index = skeleton.bone_index(bone_name);
// update vertex data with bone data
vertices[id].bone_ids[bone_indices[id]] = static_cast<std::uint32_t>(bone_index);
vertices[id].bone_weights[bone_indices[id]] = weight;
++bone_indices[id];
}
}
loaded_meshes_[mesh_file].push_back(
{.mesh = create_mesh(vertices, indices), .texture_name = texture_name});
},
[this, &mesh_file](auto animations, auto skeleton) {
loaded_animations_[mesh_file] = std::move(animations);
loaded_skeletons_[mesh_file] = std::move(skeleton);
});
}
Meshes meshes{};
for (const auto &[mesh, texture_name] : loaded_meshes_[mesh_file])
{
meshes.mesh_data.push_back({.mesh = mesh.get(), .texture_name = texture_name});
};
meshes.animations = loaded_animations_[mesh_file];
meshes.skeleton = std::addressof(skeleton_copies_.emplace_back(loaded_skeletons_[mesh_file]));
return meshes;
}
}