1
1
/*
2
2
* Vulkan glTF model and texture loading class based on tinyglTF (https://github.com/syoyo/tinygltf)
3
3
*
4
- * Copyright (C) 2018-2024 by Sascha Willems - www.saschawillems.de
4
+ * Copyright (C) 2018-2025 by Sascha Willems - www.saschawillems.de
5
5
*
6
6
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
7
7
*/
@@ -108,6 +108,7 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
108
108
buffer = &gltfimage.image [0 ];
109
109
bufferSize = gltfimage.image .size ();
110
110
}
111
+ assert (buffer);
111
112
112
113
format = VK_FORMAT_R8G8B8A8_UNORM;
113
114
@@ -140,7 +141,7 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
140
141
VK_CHECK_RESULT (vkAllocateMemory (device->logicalDevice , &memAllocInfo, nullptr , &stagingMemory));
141
142
VK_CHECK_RESULT (vkBindBufferMemory (device->logicalDevice , stagingBuffer, stagingMemory, 0 ));
142
143
143
- uint8_t * data;
144
+ uint8_t * data{ nullptr } ;
144
145
VK_CHECK_RESULT (vkMapMemory (device->logicalDevice , stagingMemory, 0 , memReqs.size , 0 , (void **)&data));
145
146
memcpy (data, buffer, bufferSize);
146
147
vkUnmapMemory (device->logicalDevice , stagingMemory);
@@ -337,7 +338,7 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
337
338
VK_CHECK_RESULT (vkAllocateMemory (device->logicalDevice , &memAllocInfo, nullptr , &stagingMemory));
338
339
VK_CHECK_RESULT (vkBindBufferMemory (device->logicalDevice , stagingBuffer, stagingMemory, 0 ));
339
340
340
- uint8_t * data;
341
+ uint8_t * data{ nullptr } ;
341
342
VK_CHECK_RESULT (vkMapMemory (device->logicalDevice , stagingMemory, 0 , memReqs.size , 0 , (void **)&data));
342
343
memcpy (data, ktxTextureData, ktxTextureSize);
343
344
vkUnmapMemory (device->logicalDevice , stagingMemory);
@@ -408,8 +409,6 @@ void vkglTF::Texture::fromglTfImage(tinygltf::Image &gltfimage, std::string path
408
409
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
409
410
samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
410
411
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
411
- samplerInfo.maxAnisotropy = 1.0 ;
412
- samplerInfo.anisotropyEnable = VK_FALSE;
413
412
samplerInfo.maxLod = (float )mipLevels;
414
413
samplerInfo.maxAnisotropy = 8 .0f ;
415
414
samplerInfo.anisotropyEnable = VK_TRUE;
@@ -651,7 +650,7 @@ void vkglTF::Model::createEmptyTexture(VkQueue transferQueue)
651
650
VK_CHECK_RESULT (vkBindBufferMemory (device->logicalDevice , stagingBuffer, stagingMemory, 0 ));
652
651
653
652
// Copy texture data into staging buffer
654
- uint8_t * data;
653
+ uint8_t * data{ nullptr } ;
655
654
VK_CHECK_RESULT (vkMapMemory (device->logicalDevice , stagingMemory, 0 , memReqs.size , 0 , (void **)&data));
656
655
memcpy (data, buffer, bufferSize);
657
656
vkUnmapMemory (device->logicalDevice , stagingMemory);
@@ -733,13 +732,13 @@ vkglTF::Model::~Model()
733
732
vkFreeMemory (device->logicalDevice , vertices.memory , nullptr );
734
733
vkDestroyBuffer (device->logicalDevice , indices.buffer , nullptr );
735
734
vkFreeMemory (device->logicalDevice , indices.memory , nullptr );
736
- for (auto texture : textures) {
735
+ for (auto & texture : textures) {
737
736
texture.destroy ();
738
737
}
739
- for (auto node : nodes) {
738
+ for (auto & node : nodes) {
740
739
delete node;
741
740
}
742
- for (auto skin : skins) {
741
+ for (auto & skin : skins) {
743
742
delete skin;
744
743
}
745
744
if (descriptorSetLayoutUbo != VK_NULL_HANDLE) {
@@ -885,8 +884,10 @@ void vkglTF::Model::loadNode(vkglTF::Node *parent, const tinygltf::Node &node, u
885
884
switch (numColorComponents) {
886
885
case 3 :
887
886
vert.color = glm::vec4 (glm::make_vec3 (&bufferColors[v * 3 ]), 1 .0f );
887
+ break ;
888
888
case 4 :
889
889
vert.color = glm::make_vec4 (&bufferColors[v * 4 ]);
890
+ break ;
890
891
}
891
892
}
892
893
else {
@@ -1256,7 +1257,7 @@ void vkglTF::Model::loadFromFile(std::string filename, vks::VulkanDevice *device
1256
1257
}
1257
1258
}
1258
1259
1259
- for (auto extension : gltfModel.extensionsUsed ) {
1260
+ for (auto & extension : gltfModel.extensionsUsed ) {
1260
1261
if (extension == " KHR_materials_pbrSpecularGlossiness" ) {
1261
1262
std::cout << " Required extension: " << extension;
1262
1263
metallicRoughnessWorkflow = false ;
@@ -1273,7 +1274,7 @@ void vkglTF::Model::loadFromFile(std::string filename, vks::VulkanDevice *device
1273
1274
struct StagingBuffer {
1274
1275
VkBuffer buffer;
1275
1276
VkDeviceMemory memory;
1276
- } vertexStaging, indexStaging;
1277
+ } vertexStaging{} , indexStaging{} ;
1277
1278
1278
1279
// Create staging buffers
1279
1280
// Vertex data
@@ -1332,12 +1333,12 @@ void vkglTF::Model::loadFromFile(std::string filename, vks::VulkanDevice *device
1332
1333
// Setup descriptors
1333
1334
uint32_t uboCount{ 0 };
1334
1335
uint32_t imageCount{ 0 };
1335
- for (auto node : linearNodes) {
1336
+ for (auto & node : linearNodes) {
1336
1337
if (node->mesh ) {
1337
1338
uboCount++;
1338
1339
}
1339
1340
}
1340
- for (auto material : materials) {
1341
+ for (auto & material : materials) {
1341
1342
if (material.baseColorTexture != nullptr ) {
1342
1343
imageCount++;
1343
1344
}
0 commit comments