Total mess. The instructions significantly fall apart at this point and I'll have to go reverse engineer the working version from the course's git repo.
This commit is contained in:
parent
7ae6a63295
commit
b3cdf37045
8 changed files with 185 additions and 196 deletions
187
vk_loader.cpp
187
vk_loader.cpp
|
|
@ -11,125 +11,112 @@
|
|||
#include <fastgltf/parser.hpp>
|
||||
#include <fastgltf/tools.hpp>
|
||||
|
||||
constexpr bool OverrideColors = true;
|
||||
|
||||
std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath)
|
||||
{
|
||||
//> openmesh
|
||||
std::cout << "\nLoading GLTF: " << filePath << std::endl;
|
||||
std::print("Loading GLTF {}", filePath.string());
|
||||
|
||||
fastgltf::GltfDataBuffer data;
|
||||
data.loadFromFile(filePath);
|
||||
fastgltf::GltfDataBuffer data{};
|
||||
data.loadFromFile(filePath);
|
||||
|
||||
constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers
|
||||
| fastgltf::Options::LoadExternalBuffers;
|
||||
constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers | fastgltf::Options::LoadExternalBuffers;
|
||||
|
||||
fastgltf::Asset gltf;
|
||||
fastgltf::Parser parser {};
|
||||
fastgltf::Asset gltf;
|
||||
fastgltf::Parser parser{};
|
||||
|
||||
auto load = parser.loadBinaryGLTF(&data, filePath.parent_path(), gltfOptions);
|
||||
if (load) {
|
||||
gltf = std::move(load.get());
|
||||
} else {
|
||||
std::print("Failed to load glTF: {} \n", fastgltf::to_underlying(load.error()));
|
||||
return {};
|
||||
}
|
||||
//< openmesh
|
||||
//> loadmesh
|
||||
std::vector<std::shared_ptr<MeshAsset>> meshes;
|
||||
if(auto load = parser.loadBinaryGLTF(&data, filePath.parent_path(), gltfOptions)) {
|
||||
gltf = std::move(load.get());
|
||||
} else {
|
||||
std::print("Failed to load glTF: {}\n", fastgltf::to_underlying(load.error()));
|
||||
}
|
||||
|
||||
// use the same vectors for all meshes so that the memory doesnt reallocate as
|
||||
// often
|
||||
std::vector<uint32_t> indices;
|
||||
std::vector<Vertex> vertices;
|
||||
for (fastgltf::Mesh& mesh : gltf.meshes) {
|
||||
MeshAsset newmesh;
|
||||
std::vector<std::shared_ptr<MeshAsset>> meshes;
|
||||
|
||||
newmesh.name = mesh.name;
|
||||
std::vector<uint32_t> indices;
|
||||
std::vector<Vertex> vertices;
|
||||
|
||||
// clear the mesh arrays each mesh, we dont want to merge them by error
|
||||
indices.clear();
|
||||
vertices.clear();
|
||||
for(fastgltf::Mesh& mesh : gltf.meshes) {
|
||||
auto newmesh = std::make_shared<MeshAsset>();
|
||||
newmesh->name = mesh.name;
|
||||
|
||||
for (auto&& p : mesh.primitives) {
|
||||
GeoSurface newSurface;
|
||||
newSurface.startIndex = (uint32_t)indices.size();
|
||||
newSurface.count = (uint32_t)gltf.accessors[p.indicesAccessor.value()].count;
|
||||
indices.clear();
|
||||
vertices.clear();
|
||||
|
||||
size_t initial_vtx = vertices.size();
|
||||
for(auto&& p : mesh.primitives) {
|
||||
// have to do this here because p.indicesAccessor changes...for reasons
|
||||
newmesh->surfaces.emplace_back(
|
||||
(uint32_t)indices.size(),
|
||||
(uint32_t)gltf.accessors[p.indicesAccessor.value()].count);
|
||||
|
||||
// load indexes
|
||||
{
|
||||
fastgltf::Accessor& indexaccessor = gltf.accessors[p.indicesAccessor.value()];
|
||||
indices.reserve(indices.size() + indexaccessor.count);
|
||||
size_t initial_vtx = vertices.size();
|
||||
|
||||
fastgltf::iterateAccessor<std::uint32_t>(gltf, indexaccessor,
|
||||
[&](std::uint32_t idx) {
|
||||
indices.push_back(idx + initial_vtx);
|
||||
});
|
||||
}
|
||||
// load indexes
|
||||
{
|
||||
fastgltf::Accessor& indexaccessor = gltf.accessors[p.indicesAccessor.value()];
|
||||
indices.reserve(indices.size() + indexaccessor.count);
|
||||
fastgltf::iterateAccessor<std::uint32_t>(gltf, indexaccessor,
|
||||
[&](std::uint32_t idx) {
|
||||
indices.emplace_back(idx + initial_vtx);
|
||||
});
|
||||
}
|
||||
|
||||
// load vertex positions
|
||||
{
|
||||
fastgltf::Accessor& posAccessor = gltf.accessors[p.findAttribute("POSITION")->second];
|
||||
vertices.resize(vertices.size() + posAccessor.count);
|
||||
// load vertex positions
|
||||
{
|
||||
fastgltf::Accessor& posAccessor = gltf.accessors[p.findAttribute("POSITION")->second];
|
||||
vertices.resize(vertices.size() + posAccessor.count);
|
||||
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec3>(gltf, posAccessor,
|
||||
[&](glm::vec3 v, size_t index) {
|
||||
Vertex newvtx;
|
||||
newvtx.position = v;
|
||||
newvtx.normal = { 1, 0, 0 };
|
||||
newvtx.color = glm::vec4 { 1.f };
|
||||
newvtx.uv_x = 0;
|
||||
newvtx.uv_y = 0;
|
||||
vertices[initial_vtx + index] = newvtx;
|
||||
});
|
||||
}
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec3>(gltf, posAccessor,
|
||||
[&](glm::vec3 v, size_t index) {
|
||||
vertices[initial_vtx + index] = {
|
||||
.position = v,
|
||||
.uv_x = 0,
|
||||
.normal = { 1, 0, 0 },
|
||||
.uv_y = 0,
|
||||
.color = glm::vec4 { 1.0f},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// load vertex normals
|
||||
auto normals = p.findAttribute("NORMAL");
|
||||
if (normals != p.attributes.end()) {
|
||||
// load vertex normals
|
||||
auto normals = p.findAttribute("NORMAL");
|
||||
if(normals != p.attributes.end()) {
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec3>(gltf, gltf.accessors[(*normals).second],
|
||||
[&](glm::vec3 v, size_t index) {
|
||||
vertices[initial_vtx + index].normal = v;
|
||||
});
|
||||
}
|
||||
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec3>(gltf, gltf.accessors[(*normals).second],
|
||||
[&](glm::vec3 v, size_t index) {
|
||||
vertices[initial_vtx + index].normal = v;
|
||||
});
|
||||
}
|
||||
// load UVs
|
||||
auto uv = p.findAttribute("TEXCOORD_0");
|
||||
if(uv != p.attributes.end()) {
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec2>(gltf, gltf.accessors[(*uv).second],
|
||||
[&](glm::vec2 v, size_t index) {
|
||||
vertices[initial_vtx + index].uv_x = v.x;
|
||||
vertices[initial_vtx + index].uv_y = v.y;
|
||||
});
|
||||
}
|
||||
|
||||
// load UVs
|
||||
auto uv = p.findAttribute("TEXCOORD_0");
|
||||
if (uv != p.attributes.end()) {
|
||||
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec2>(gltf, gltf.accessors[(*uv).second],
|
||||
[&](glm::vec2 v, size_t index) {
|
||||
vertices[initial_vtx + index].uv_x = v.x;
|
||||
vertices[initial_vtx + index].uv_y = v.y;
|
||||
});
|
||||
}
|
||||
|
||||
// load vertex colors
|
||||
auto colors = p.findAttribute("COLOR_0");
|
||||
if (colors != p.attributes.end()) {
|
||||
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec4>(gltf, gltf.accessors[(*colors).second],
|
||||
[&](glm::vec4 v, size_t index) {
|
||||
vertices[initial_vtx + index].color = v;
|
||||
});
|
||||
}
|
||||
newmesh.surfaces.push_back(newSurface);
|
||||
}
|
||||
|
||||
// display the vertex normals
|
||||
constexpr bool OverrideColors = true;
|
||||
if (OverrideColors) {
|
||||
for (Vertex& vtx : vertices) {
|
||||
vtx.color = glm::vec4(vtx.normal, 1.f);
|
||||
}
|
||||
}
|
||||
newmesh.meshBuffers = engine->uploadMesh(indices, vertices);
|
||||
|
||||
meshes.emplace_back(std::make_shared<MeshAsset>(std::move(newmesh)));
|
||||
// load vertex colors
|
||||
auto colors = p.findAttribute("COLOR_0");
|
||||
if(colors != p.attributes.end()) {
|
||||
fastgltf::iterateAccessorWithIndex<glm::vec4>(gltf, gltf.accessors[(*colors).second],
|
||||
[&](glm::vec4 v, size_t index) {
|
||||
vertices[initial_vtx + index].color = v;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return meshes;
|
||||
if(OverrideColors) {
|
||||
for(Vertex& vtx : vertices) {
|
||||
vtx.color = glm::vec4(vtx.normal, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
//< loadmesh
|
||||
newmesh->meshBuffers = engine->uploadMesh(indices, vertices);
|
||||
|
||||
meshes.emplace_back(newmesh);
|
||||
}
|
||||
|
||||
return meshes;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue