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
0 commit comments