11/*
22* Vulkan glTF model and texture loading class based on tinyglTF (https://github.com/syoyo/tinygltf)
33*
4- * Copyright (C) 2018-2024 by Sascha Willems - www.saschawillems.de
4+ * Copyright (C) 2018-2025 by Sascha Willems - www.saschawillems.de
55*
66* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
77*/
@@ -108,6 +108,7 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
108108 buffer = &gltfimage.image [0 ];
109109 bufferSize = gltfimage.image .size ();
110110 }
111+ assert (buffer);
111112
112113 format = VK_FORMAT_R8G8B8A8_UNORM;
113114
@@ -140,7 +141,7 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
140141 VK_CHECK_RESULT (vkAllocateMemory (device->logicalDevice , &memAllocInfo, nullptr , &stagingMemory));
141142 VK_CHECK_RESULT (vkBindBufferMemory (device->logicalDevice , stagingBuffer, stagingMemory, 0 ));
142143
143- uint8_t * data;
144+ uint8_t * data{ nullptr } ;
144145 VK_CHECK_RESULT (vkMapMemory (device->logicalDevice , stagingMemory, 0 , memReqs.size , 0 , (void **)&data));
145146 memcpy (data, buffer, bufferSize);
146147 vkUnmapMemory (device->logicalDevice , stagingMemory);
@@ -337,7 +338,7 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
337338 VK_CHECK_RESULT (vkAllocateMemory (device->logicalDevice , &memAllocInfo, nullptr , &stagingMemory));
338339 VK_CHECK_RESULT (vkBindBufferMemory (device->logicalDevice , stagingBuffer, stagingMemory, 0 ));
339340
340- uint8_t * data;
341+ uint8_t * data{ nullptr } ;
341342 VK_CHECK_RESULT (vkMapMemory (device->logicalDevice , stagingMemory, 0 , memReqs.size , 0 , (void **)&data));
342343 memcpy (data, ktxTextureData, ktxTextureSize);
343344 vkUnmapMemory (device->logicalDevice , stagingMemory);
@@ -408,8 +409,6 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
408409 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
409410 samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
410411 samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
411- samplerInfo.maxAnisotropy = 1.0 ;
412- samplerInfo.anisotropyEnable = VK_FALSE;
413412 samplerInfo.maxLod = (float )mipLevels;
414413 samplerInfo.maxAnisotropy = 8 .0f ;
415414 samplerInfo.anisotropyEnable = VK_TRUE;
@@ -651,7 +650,7 @@ void vkglTF::Model::createEmptyTexture(VkQueue transferQueue)
651650 VK_CHECK_RESULT (vkBindBufferMemory (device->logicalDevice , stagingBuffer, stagingMemory, 0 ));
652651
653652 // Copy texture data into staging buffer
654- uint8_t * data;
653+ uint8_t * data{ nullptr } ;
655654 VK_CHECK_RESULT (vkMapMemory (device->logicalDevice , stagingMemory, 0 , memReqs.size , 0 , (void **)&data));
656655 memcpy (data, buffer, bufferSize);
657656 vkUnmapMemory (device->logicalDevice , stagingMemory);
@@ -733,13 +732,13 @@ vkglTF::Model::~Model()
733732 vkFreeMemory (device->logicalDevice , vertices.memory , nullptr );
734733 vkDestroyBuffer (device->logicalDevice , indices.buffer , nullptr );
735734 vkFreeMemory (device->logicalDevice , indices.memory , nullptr );
736- for (auto texture : textures) {
735+ for (auto & texture : textures) {
737736 texture.destroy ();
738737 }
739- for (auto node : nodes) {
738+ for (auto & node : nodes) {
740739 delete node;
741740 }
742- for (auto skin : skins) {
741+ for (auto & skin : skins) {
743742 delete skin;
744743 }
745744 if (descriptorSetLayoutUbo != VK_NULL_HANDLE) {
@@ -885,8 +884,10 @@ void vkglTF::Model::loadNode(vkglTF::Node *parent, const tinygltf::Node &node, u
885884 switch (numColorComponents) {
886885 case 3 :
887886 vert.color = glm::vec4 (glm::make_vec3 (&bufferColors[v * 3 ]), 1 .0f );
887+ break ;
888888 case 4 :
889889 vert.color = glm::make_vec4 (&bufferColors[v * 4 ]);
890+ break ;
890891 }
891892 }
892893 else {
@@ -1256,7 +1257,7 @@ void vkglTF::Model::loadFromFile(std::string filename, vks::VulkanDevice *device
12561257 }
12571258 }
12581259
1259- for (auto extension : gltfModel.extensionsUsed ) {
1260+ for (auto & extension : gltfModel.extensionsUsed ) {
12601261 if (extension == " KHR_materials_pbrSpecularGlossiness" ) {
12611262 std::cout << " Required extension: " << extension;
12621263 metallicRoughnessWorkflow = false ;
@@ -1273,7 +1274,7 @@ void vkglTF::Model::loadFromFile(std::string filename, vks::VulkanDevice *device
12731274 struct StagingBuffer {
12741275 VkBuffer buffer;
12751276 VkDeviceMemory memory;
1276- } vertexStaging, indexStaging;
1277+ } vertexStaging{} , indexStaging{} ;
12771278
12781279 // Create staging buffers
12791280 // Vertex data
@@ -1332,12 +1333,12 @@ void vkglTF::Model::loadFromFile(std::string filename, vks::VulkanDevice *device
13321333 // Setup descriptors
13331334 uint32_t uboCount{ 0 };
13341335 uint32_t imageCount{ 0 };
1335- for (auto node : linearNodes) {
1336+ for (auto & node : linearNodes) {
13361337 if (node->mesh ) {
13371338 uboCount++;
13381339 }
13391340 }
1340- for (auto material : materials) {
1341+ for (auto & material : materials) {
13411342 if (material.baseColorTexture != nullptr ) {
13421343 imageCount++;
13431344 }
0 commit comments