I think there is an error in file GltfLoad.cpp
vrazumov opened this issue · 2 comments
I think there is an error in file GltfLoad.cpp
Available:
347 if (mat.pbrData.baseColorTexture && images[mat.pbrData.baseColorTexture->textureIndex])
348 matProgram.setTexture(Texture2D::create(*images[mat.pbrData.baseColorTexture->textureIndex]), MaterialTexture::BaseColor);
...
Right:
add const std::vectorfastgltf::Texture& textures as parameter in void loadMaterials
347 if (mat.pbrData.baseColorTexture && textures[mat.pbrData.baseColorTexture->textureIndex].imageIndex)
{
auto imageIndex = textures[mat.pbrData.baseColorTexture->textureIndex].imageIndex.value();
matProgram.setTexture(Texture2D::create(*images[imageIndex]), MaterialTexture::BaseColor);
}
if (mat.emissiveTexture && textures[mat.emissiveTexture->textureIndex].imageIndex)
{
auto imageIndex = textures[mat.emissiveTexture->textureIndex].imageIndex.value();
matProgram.setTexture(Texture2D::create(*images[imageIndex]), MaterialTexture::Emissive);
}
if (mat.occlusionTexture && textures[mat.occlusionTexture->textureIndex].imageIndex) { // Ambient occlusion
const Image ambientOcclusionImg = extractAmbientOcclusionImage(*images[mat.occlusionTexture->textureIndex]);
matProgram.setTexture(Texture2D::create(ambientOcclusionImg), MaterialTexture::Ambient);
}
if (mat.normalTexture && textures[mat.normalTexture->textureIndex].imageIndex)
{
auto imageIndex = textures[mat.normalTexture->textureIndex].imageIndex.value();
matProgram.setTexture(Texture2D::create(*images[imageIndex]), MaterialTexture::Normal);
}
if (mat.pbrData.metallicRoughnessTexture && textures[mat.pbrData.metallicRoughnessTexture->textureIndex].imageIndex) {
const auto [metalnessImg, roughnessImg] = extractMetalnessRoughnessImages(*images[mat.pbrData.metallicRoughnessTexture->textureIndex]);
matProgram.setTexture(Texture2D::create(metalnessImg), MaterialTexture::Metallic);
matProgram.setTexture(Texture2D::create(roughnessImg), MaterialTexture::Roughness);
}
At least it works correctly for me.
Very true, I misinterpreted how textures & images work within the format. This also fixes the Sponza test model (although not entirely as some triangles are much too stretched for yet unknown reasons), with which most textures were wrong; that explains a lot. I'll unit test this and push a commit soon, thanks a lot!