Skip to content

Commit 9de716f

Browse files
committed
Improve usage of fastgltf & use fmt::print
1 parent a4bf185 commit 9de716f

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

chapter-3/vk_loader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngi
2020
data.loadFromFile(filePath);
2121

2222
constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers
23-
| fastgltf::Options::LoadExternalBuffers;
23+
| fastgltf::Options::LoadExternalBuffers
24+
| fastgltf::Options::GenerateMeshIndices;
2425

2526
fastgltf::Asset gltf;
2627
fastgltf::Parser parser {};
@@ -29,7 +30,7 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngi
2930
if (load) {
3031
gltf = std::move(load.get());
3132
} else {
32-
fmt::print("Failed to load glTF: {} \n", fastgltf::to_underlying(load.error()));
33+
fmt::print("Failed to load glTF: {} \n", fastgltf::getErrorMessage(load.error()));
3334
return {};
3435
}
3536
//< openmesh
@@ -40,7 +41,7 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngi
4041
// often
4142
std::vector<uint32_t> indices;
4243
std::vector<Vertex> vertices;
43-
for (fastgltf::Mesh& mesh : gltf.meshes) {
44+
for (const fastgltf::Mesh& mesh : gltf.meshes) {
4445
MeshAsset newmesh;
4546

4647
newmesh.name = mesh.name;

chapter-4/vk_loader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngi
2020
data.loadFromFile(filePath);
2121

2222
constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers
23-
| fastgltf::Options::LoadExternalBuffers;
23+
| fastgltf::Options::LoadExternalBuffers
24+
| fastgltf::Options::GenerateMeshIndices;
2425

2526
fastgltf::Asset gltf;
2627
fastgltf::Parser parser {};
@@ -29,7 +30,7 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngi
2930
if (load) {
3031
gltf = std::move(load.get());
3132
} else {
32-
fmt::print("Failed to load glTF: {} \n", fastgltf::to_underlying(load.error()));
33+
fmt::print("Failed to load glTF: {} \n", fastgltf::getErrorMessage(load.error()));
3334
return {};
3435
}
3536
//< openmesh
@@ -40,7 +41,7 @@ std::optional<std::vector<std::shared_ptr<MeshAsset>>> loadGltfMeshes(VulkanEngi
4041
// often
4142
std::vector<uint32_t> indices;
4243
std::vector<Vertex> vertices;
43-
for (fastgltf::Mesh& mesh : gltf.meshes) {
44+
for (const fastgltf::Mesh& mesh : gltf.meshes) {
4445
MeshAsset newmesh;
4546

4647
newmesh.name = mesh.name;

chapter-5/vk_loader.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "vk_initializers.h"
77
#include "vk_types.h"
88
#include <glm/gtx/quaternion.hpp>
9+
#include <glm/gtc/type_ptr.hpp>
910

1011
#include <fastgltf/glm_element_traits.hpp>
1112
#include <fastgltf/parser.hpp>
@@ -25,9 +26,7 @@ std::optional<AllocatedImage> load_image(VulkanEngine* engine, fastgltf::Asset&
2526
assert(filePath.fileByteOffset == 0); // We don't support offsets with stbi.
2627
assert(filePath.uri.isLocalPath()); // We're only capable of loading
2728
// local files.
28-
29-
const std::string path(filePath.uri.path().begin(),
30-
filePath.uri.path().end()); // Thanks C++.
29+
const std::string path(filePath.uri.path());
3130
unsigned char* data = stbi_load(path.c_str(), &width, &height, &nrChannels, 4);
3231
if (data) {
3332
VkExtent3D imagesize;
@@ -137,7 +136,11 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
137136

138137
fastgltf::Parser parser {};
139138

140-
constexpr auto gltfOptions = fastgltf::Options::DontRequireValidAssetMember | fastgltf::Options::AllowDouble | fastgltf::Options::LoadGLBBuffers | fastgltf::Options::LoadExternalBuffers;
139+
constexpr auto gltfOptions = fastgltf::Options::DontRequireValidAssetMember
140+
| fastgltf::Options::AllowDouble
141+
| fastgltf::Options::LoadGLBBuffers
142+
| fastgltf::Options::LoadExternalBuffers
143+
| fastgltf::Options::GenerateMeshIndices;
141144
// fastgltf::Options::LoadExternalImages;
142145

143146
fastgltf::GltfDataBuffer data;
@@ -153,15 +156,15 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
153156
if (load) {
154157
gltf = std::move(load.get());
155158
} else {
156-
std::cerr << "Failed to load glTF: " << fastgltf::to_underlying(load.error()) << std::endl;
159+
fmt::print("Failed to load glTF: {}", fastgltf::getErrorMessage(load.error()));
157160
return {};
158161
}
159162
} else if (type == fastgltf::GltfType::GLB) {
160163
auto load = parser.loadBinaryGLTF(&data, path.parent_path(), gltfOptions);
161164
if (load) {
162165
gltf = std::move(load.get());
163166
} else {
164-
std::cerr << "Failed to load glTF: " << fastgltf::to_underlying(load.error()) << std::endl;
167+
fmt::print("Failed to load glTF: {}", fastgltf::getErrorMessage(load.error()));
165168
return {};
166169
}
167170
} else {
@@ -180,7 +183,7 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
180183
//> load_samplers
181184

182185
// load samplers
183-
for (fastgltf::Sampler& sampler : gltf.samplers) {
186+
for (const fastgltf::Sampler& sampler : gltf.samplers) {
184187

185188
VkSamplerCreateInfo sampl = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .pNext = nullptr};
186189
sampl.maxLod = VK_LOD_CLAMP_NONE;
@@ -229,16 +232,13 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
229232
//< load_buffer
230233
//
231234
//> load_material
232-
for (fastgltf::Material& mat : gltf.materials) {
235+
for (const fastgltf::Material& mat : gltf.materials) {
233236
std::shared_ptr<GLTFMaterial> newMat = std::make_shared<GLTFMaterial>();
234237
materials.push_back(newMat);
235238
file.materials[mat.name.c_str()] = newMat;
236239

237240
GLTFMetallic_Roughness::MaterialConstants constants;
238-
constants.colorFactors.x = mat.pbrData.baseColorFactor[0];
239-
constants.colorFactors.y = mat.pbrData.baseColorFactor[1];
240-
constants.colorFactors.z = mat.pbrData.baseColorFactor[2];
241-
constants.colorFactors.w = mat.pbrData.baseColorFactor[3];
241+
constants.colorFactors = glm::make_vec4(mat.pbrData.baseColorFactor.data());
242242

243243
constants.metal_rough_factors.x = mat.pbrData.metallicFactor;
244244
constants.metal_rough_factors.y = mat.pbrData.roughnessFactor;
@@ -280,7 +280,7 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
280280
std::vector<uint32_t> indices;
281281
std::vector<Vertex> vertices;
282282

283-
for (fastgltf::Mesh& mesh : gltf.meshes) {
283+
for (const fastgltf::Mesh& mesh : gltf.meshes) {
284284
std::shared_ptr<MeshAsset> newmesh = std::make_shared<MeshAsset>();
285285
meshes.push_back(newmesh);
286286
file.meshes[mesh.name.c_str()] = newmesh;
@@ -290,7 +290,7 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
290290
indices.clear();
291291
vertices.clear();
292292

293-
for (auto&& p : mesh.primitives) {
293+
for (const fastgltf::Primitive& p : mesh.primitives) {
294294
GeoSurface newSurface;
295295
newSurface.startIndex = (uint32_t)indices.size();
296296
newSurface.count = (uint32_t)gltf.accessors[p.indicesAccessor.value()].count;
@@ -364,7 +364,7 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
364364

365365
glm::vec3 minpos = vertices[initial_vtx].position;
366366
glm::vec3 maxpos = vertices[initial_vtx].position;
367-
for (int i = initial_vtx; i < vertices.size(); i++) {
367+
for (size_t i = initial_vtx; i < vertices.size(); i++) {
368368
minpos = glm::min(minpos, vertices[i].position);
369369
maxpos = glm::max(maxpos, vertices[i].position);
370370
}
@@ -393,23 +393,23 @@ std::optional<std::shared_ptr<LoadedGLTF>> loadGltf(VulkanEngine* engine, std::s
393393
nodes.push_back(newNode);
394394
file.nodes[node.name.c_str()];
395395

396-
std::visit(fastgltf::visitor { [&](fastgltf::Node::TransformMatrix matrix) {
397-
memcpy(&newNode->localTransform, matrix.data(), sizeof(matrix));
398-
},
399-
[&](fastgltf::Node::TRS transform) {
400-
glm::vec3 tl(transform.translation[0], transform.translation[1],
401-
transform.translation[2]);
402-
glm::quat rot(transform.rotation[3], transform.rotation[0], transform.rotation[1],
403-
transform.rotation[2]);
404-
glm::vec3 sc(transform.scale[0], transform.scale[1], transform.scale[2]);
405-
406-
glm::mat4 tm = glm::translate(glm::mat4(1.f), tl);
407-
glm::mat4 rm = glm::toMat4(rot);
408-
glm::mat4 sm = glm::scale(glm::mat4(1.f), sc);
409-
410-
newNode->localTransform = tm * rm * sm;
411-
} },
412-
node.transform);
396+
std::visit(fastgltf::visitor {
397+
[&](fastgltf::Node::TransformMatrix matrix) {
398+
std::memcpy(&newNode->localTransform, matrix.data(), sizeof(matrix));
399+
},
400+
[&](fastgltf::Node::TRS transform) {
401+
glm::vec3 tl = glm::make_vec3(transform.translation.data());
402+
glm::quat rot(transform.rotation[3], transform.rotation[0], transform.rotation[1],
403+
transform.rotation[2]);
404+
glm::vec3 sc = glm::make_vec3(transform.scale.data());
405+
406+
glm::mat4 tm = glm::translate(glm::mat4(1.f), tl);
407+
glm::mat4 rm = glm::toMat4(rot);
408+
glm::mat4 sm = glm::scale(glm::mat4(1.f), sc);
409+
410+
newNode->localTransform = tm * rm * sm;
411+
}
412+
}, node.transform);
413413
}
414414
//< load_nodes
415415
//> load_graph

meshbaker/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void output_meshes(std::filesystem::path target_file, std::filesystem::path sour
6767
gltf = std::move(load.get());
6868
}
6969
else {
70-
std::cerr << "Failed to load glTF: " << fastgltf::to_underlying(load.error()) << std::endl;
70+
std::cerr << "Failed to load glTF: " << fastgltf::getErrorMessage(load.error()) << std::endl;
7171
}
7272
}
7373
else if (type == fastgltf::GltfType::GLB) {
@@ -76,7 +76,7 @@ void output_meshes(std::filesystem::path target_file, std::filesystem::path sour
7676
gltf = std::move(load.get());
7777
}
7878
else {
79-
std::cerr << "Failed to load glTF: " << fastgltf::to_underlying(load.error()) << std::endl;
79+
std::cerr << "Failed to load glTF: " << fastgltf::getErrorMessage(load.error()) << std::endl;
8080
}
8181
}
8282
else {

0 commit comments

Comments
 (0)