Managed to get the monkey to load by patching simdjson 3.3.0 to fix a dubious 3 lines of code as mentioned here: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=280590 then have fastgltf _NOT_ download simdjson again.
This commit is contained in:
parent
dbda70e3a0
commit
11ed1540ab
6 changed files with 122 additions and 125 deletions
BIN
basicmesh.glb.gz
BIN
basicmesh.glb.gz
Binary file not shown.
|
|
@ -94,6 +94,7 @@ glm_opts.add_cmake_defines({
|
||||||
glm_proj = cmake.subproject('glm', options: glm_opts)
|
glm_proj = cmake.subproject('glm', options: glm_opts)
|
||||||
glm = glm_proj.get_variable('glm_dep')
|
glm = glm_proj.get_variable('glm_dep')
|
||||||
|
|
||||||
|
simdjson = dependency('simdjson')
|
||||||
|
|
||||||
vk_opts = cmake.subproject_options()
|
vk_opts = cmake.subproject_options()
|
||||||
|
|
||||||
|
|
@ -120,6 +121,7 @@ dependencies += [
|
||||||
imgui,
|
imgui,
|
||||||
sdl2,
|
sdl2,
|
||||||
fastgltf,
|
fastgltf,
|
||||||
|
simdjson,
|
||||||
]
|
]
|
||||||
|
|
||||||
sources = [
|
sources = [
|
||||||
|
|
|
||||||
|
|
@ -820,7 +820,7 @@ void VulkanEngine::init_default_data() {
|
||||||
|
|
||||||
rectangle = uploadMesh(rect_indices, rect_vertices);
|
rectangle = uploadMesh(rect_indices, rect_vertices);
|
||||||
|
|
||||||
auto basicmesh = loadGltfMeshes(this, "basicmesh.glb");
|
auto basicmesh = loadGltfMeshes(this, "./basicmesh.glb");
|
||||||
assert(basicmesh != std::nullopt && "Failed to load basicmesh.glb");
|
assert(basicmesh != std::nullopt && "Failed to load basicmesh.glb");
|
||||||
|
|
||||||
testMeshes = *basicmesh;
|
testMeshes = *basicmesh;
|
||||||
|
|
|
||||||
152
vk_loader.cpp
152
vk_loader.cpp
|
|
@ -1,6 +1,6 @@
|
||||||
#include "vk_loader.h"
|
|
||||||
#include "vendor/stb_image.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "vendor/stb_image.h"
|
||||||
|
#include <vk_loader.h>
|
||||||
|
|
||||||
#include "vk_engine.h"
|
#include "vk_engine.h"
|
||||||
#include "vk_initializers.h"
|
#include "vk_initializers.h"
|
||||||
|
|
@ -8,134 +8,128 @@
|
||||||
#include <glm/gtx/quaternion.hpp>
|
#include <glm/gtx/quaternion.hpp>
|
||||||
|
|
||||||
#include <fastgltf/glm_element_traits.hpp>
|
#include <fastgltf/glm_element_traits.hpp>
|
||||||
#include <fastgltf/core.hpp>
|
#include <fastgltf/parser.hpp>
|
||||||
#include <fastgltf/tools.hpp>
|
#include <fastgltf/tools.hpp>
|
||||||
|
|
||||||
constexpr bool OverrideColors = true;
|
|
||||||
|
|
||||||
std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath)
|
std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath)
|
||||||
{
|
{
|
||||||
std::println("\nLoading GLTF: {}", filePath.string());
|
//> openmesh
|
||||||
auto data = fastgltf::GltfDataBuffer::FromPath(filePath);
|
std::cout << "\nLoading GLTF: " << filePath << std::endl;
|
||||||
|
|
||||||
if(data.error() != fastgltf::Error::None) {
|
fastgltf::GltfDataBuffer data;
|
||||||
std::println("Failed to load glTF: {}={}\n",
|
data.loadFromFile(filePath);
|
||||||
typeid(data.error()).name(),
|
|
||||||
fastgltf::to_underlying(data.error()));
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr auto gltfOptions = fastgltf::Options::LoadExternalBuffers;
|
constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers
|
||||||
|
| fastgltf::Options::LoadExternalBuffers;
|
||||||
|
|
||||||
fastgltf::Asset gltf{};
|
fastgltf::Asset gltf;
|
||||||
fastgltf::Parser parser{};
|
fastgltf::Parser parser {};
|
||||||
|
|
||||||
auto load = parser.loadGltfBinary(data.get(), filePath.parent_path(), gltfOptions);
|
auto load = parser.loadBinaryGLTF(&data, filePath.parent_path(), gltfOptions);
|
||||||
|
if (load) {
|
||||||
switch(load.error()) {
|
|
||||||
case fastgltf::Error::None:
|
|
||||||
gltf = std::move(load.get());
|
gltf = std::move(load.get());
|
||||||
break;
|
} else {
|
||||||
case fastgltf::Error::InvalidGLB:
|
std::print("Failed to load glTF: {} \n", fastgltf::to_underlying(load.error()));
|
||||||
std::println("fastgltf says Error::InvalidGLB {}", filePath.string());
|
return {};
|
||||||
return std::nullopt;
|
|
||||||
break;
|
|
||||||
case fastgltf::Error::UnsupportedVersion:
|
|
||||||
std::println("fastgltf says Error::UnsupportedVersion {}", filePath.string());
|
|
||||||
return std::nullopt;
|
|
||||||
break;
|
|
||||||
case fastgltf::Error::InvalidPath:
|
|
||||||
std::println("fastgltf says Error::UnsupportedVersion {}",
|
|
||||||
filePath.string());
|
|
||||||
return std::nullopt;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::println("Unknown fastgltf error loading {}", filePath.string());
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
//< openmesh
|
||||||
|
//> loadmesh
|
||||||
std::vector<std::shared_ptr<MeshAsset>> meshes;
|
std::vector<std::shared_ptr<MeshAsset>> meshes;
|
||||||
// use the same vectors for all meshes
|
|
||||||
|
// use the same vectors for all meshes so that the memory doesnt reallocate as
|
||||||
|
// often
|
||||||
std::vector<uint32_t> indices;
|
std::vector<uint32_t> indices;
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
|
for (fastgltf::Mesh& mesh : gltf.meshes) {
|
||||||
for(auto& mesh : gltf.meshes) {
|
|
||||||
MeshAsset newmesh;
|
MeshAsset newmesh;
|
||||||
|
|
||||||
newmesh.name = mesh.name;
|
newmesh.name = mesh.name;
|
||||||
|
|
||||||
|
// clear the mesh arrays each mesh, we dont want to merge them by error
|
||||||
indices.clear();
|
indices.clear();
|
||||||
indices.clear();
|
vertices.clear();
|
||||||
|
|
||||||
for(auto&& p : mesh.primitives) {
|
for (auto&& p : mesh.primitives) {
|
||||||
auto& indexAccessor = gltf.accessors[p.indicesAccessor.value()];
|
GeoSurface newSurface;
|
||||||
|
newSurface.startIndex = (uint32_t)indices.size();
|
||||||
GeoSurface newSurface {
|
newSurface.count = (uint32_t)gltf.accessors[p.indicesAccessor.value()].count;
|
||||||
.startIndex = (uint32_t)indices.size(),
|
|
||||||
.count = (uint32_t)indexAccessor.count,
|
|
||||||
};
|
|
||||||
|
|
||||||
size_t initial_vtx = vertices.size();
|
size_t initial_vtx = vertices.size();
|
||||||
|
|
||||||
// load indices
|
// load indexes
|
||||||
{
|
{
|
||||||
indices.reserve(indices.size() + indexAccessor.count);
|
fastgltf::Accessor& indexaccessor = gltf.accessors[p.indicesAccessor.value()];
|
||||||
fastgltf::iterateAccessor<std::uint32_t>(
|
indices.reserve(indices.size() + indexaccessor.count);
|
||||||
gltf, indexAccessor, [&](std::uint32_t idx) {
|
|
||||||
|
fastgltf::iterateAccessor<std::uint32_t>(gltf, indexaccessor,
|
||||||
|
[&](std::uint32_t idx) {
|
||||||
indices.push_back(idx + initial_vtx);
|
indices.push_back(idx + initial_vtx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// load vertex positions
|
// load vertex positions
|
||||||
{
|
{
|
||||||
fastgltf::Accessor& posAccessor = gltf.accessors[
|
fastgltf::Accessor& posAccessor = gltf.accessors[p.findAttribute("POSITION")->second];
|
||||||
p.findAttribute("POSITIONS")->accessorIndex];
|
|
||||||
vertices.resize(vertices.size() + posAccessor.count);
|
vertices.resize(vertices.size() + posAccessor.count);
|
||||||
|
|
||||||
fastgltf::iterateAccessorWithIndex<glm::vec3>(
|
fastgltf::iterateAccessorWithIndex<glm::vec3>(gltf, posAccessor,
|
||||||
gltf, posAccessor, [&](glm::vec3 v, size_t index)
|
[&](glm::vec3 v, size_t index) {
|
||||||
{
|
Vertex newvtx;
|
||||||
vertices[initial_vtx + index] = {
|
newvtx.position = v;
|
||||||
.position = v,
|
newvtx.normal = { 1, 0, 0 };
|
||||||
.uv_x = 0,
|
newvtx.color = glm::vec4 { 1.f };
|
||||||
.normal = {1, 0, 0},
|
newvtx.uv_x = 0;
|
||||||
.uv_y = 0,
|
newvtx.uv_y = 0;
|
||||||
.color = glm::vec4{1.0f},
|
vertices[initial_vtx + index] = newvtx;
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// load vertex normals
|
// load vertex normals
|
||||||
auto normals = p.findAttribute("NORMA:");
|
auto normals = p.findAttribute("NORMAL");
|
||||||
if(normals != p.attributes.end()) {
|
if (normals != p.attributes.end()) {
|
||||||
fastgltf::iterateAccessorWithIndex<glm::vec3>(
|
|
||||||
gltf, gltf.accessors[(*normals).accessorIndex],
|
fastgltf::iterateAccessorWithIndex<glm::vec3>(gltf, gltf.accessors[(*normals).second],
|
||||||
[&](glm::vec3 v, size_t index)
|
[&](glm::vec3 v, size_t index) {
|
||||||
{
|
|
||||||
vertices[initial_vtx + index].normal = v;
|
vertices[initial_vtx + index].normal = v;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load UVs
|
||||||
auto uv = p.findAttribute("TEXCOORD_0");
|
auto uv = p.findAttribute("TEXCOORD_0");
|
||||||
if(uv != p.attributes.end()) {
|
if (uv != p.attributes.end()) {
|
||||||
fastgltf::iterateAccessorWithIndex<glm::vec2>(
|
|
||||||
gltf, gltf.accessors[(*uv).accessorIndex],
|
fastgltf::iterateAccessorWithIndex<glm::vec2>(gltf, gltf.accessors[(*uv).second],
|
||||||
[&](glm::vec2 v, size_t index) {
|
[&](glm::vec2 v, size_t index) {
|
||||||
vertices[initial_vtx + index].uv_x = v.x;
|
vertices[initial_vtx + index].uv_x = v.x;
|
||||||
vertices[initial_vtx + index].uv_y = v.y;
|
vertices[initial_vtx + index].uv_y = v.y;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if(OverrideColors) {
|
// load vertex colors
|
||||||
for(Vertex& vtx : vertices) {
|
auto colors = p.findAttribute("COLOR_0");
|
||||||
vtx.color = glm::vec4(vtx.normal, 1.0f);
|
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);
|
newmesh.meshBuffers = engine->uploadMesh(indices, vertices);
|
||||||
meshes.emplace_back(std::make_shared<MeshAsset>(std::move(newmesh)));
|
|
||||||
|
|
||||||
}
|
meshes.emplace_back(std::make_shared<MeshAsset>(std::move(newmesh)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return meshes;
|
return meshes;
|
||||||
|
|
||||||
|
//< loadmesh
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,4 @@ class VulkanEngine;
|
||||||
|
|
||||||
std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath);
|
std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
[wrap-git]
|
[wrap-git]
|
||||||
directory=fastgltf
|
directory=fastgltf
|
||||||
url=https://github.com/spnda/fastgltf.git
|
url=https://github.com/spnda/fastgltf.git
|
||||||
revision=v0.9.0
|
revision=v0.6.1
|
||||||
depth=1
|
depth=1
|
||||||
method=cmake
|
method=cmake
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue