admin管理员组

文章数量:1124550

i'm following vk-guide and use this library for loading gltf model. But I encounter this error, when I load the vertex.

Severity    Code    Description Project File    Line    Suppression State   Details
Error   GAF447572   no matching function for call to 'iterateAccessorWithIndex' F:\dev\vulkan\VulkanEngine\out\build\debug\VulkanEngine F:\dev\vulkan\VulkanEngine\renderer\src\huan\utils\gltf\gltf_loader.cpp 67      

Here is my code:

#include "huan/utils/gltf/gltf_loader.hpp"

#include <spdlog/spdlog.h>
#include <fastgltf/core.hpp>
#include <fastgltf/tools.hpp>
#include <glm/glm.hpp>

namespace huan
{

std::optional<std::vector<Ref<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath)
{

    spdlog::info("Loading GLTF meshes from {}", filePath.string());
    auto data = fastgltf::GltfDataBuffer::FromPath(filePath);
    if (data.error() != fastgltf::Error::None)
    {
        spdlog::error("Failed to load GLTF data from {}", filePath.string());
        return std::nullopt;
    }
    constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers | fastgltf::Options::LoadExternalBuffers;
    fastgltf::Parser parser{};
    fastgltf::Asset gltfAsset;
    auto load = parser.loadGltfBinary(data.get(), filePath.parent_path(), gltfOptions);
    if (load.error() == fastgltf::Error::None)
    {
        gltfAsset = std::move(load.get());
    }
    else
    {
        spdlog::error("Failed to parse GLTF binary from {}", filePath.string());
        return std::nullopt;
    }
    std::vector<Ref<MeshAsset>> meshes;

    // Process the GLTF asset to extract mesh data
    std::vector<uint32_t> indices;
    std::vector<Vertex> vertices;

    for (const auto& mesh : gltfAsset.meshes)
    {
        Ref<MeshAsset> newMesh = std::make_shared<MeshAsset>();
        // Populate meshAsset with data from mesh
        newMesh->name = mesh.name;
        indices.clear();
        vertices.clear();
        for (auto&& prim : mesh.primitives)
        {
            GeometrySuface newSurface;
            newSurface.startIndex = (uint32_t)indices.size();
            newSurface.count = (uint32_t)gltfAsset.accessors[prim.indicesAccessor.value()].count;
            size_t initialVerticesSize = vertices.size();
            // load indices
            {
                fastgltf::Accessor& indexAccessor = gltfAsset.accessors[prim.indicesAccessor.value()];
                indices.reserve(indices.size() + indexAccessor.count);
                fastgltf::iterateAccessor<std::uint32_t>(gltfAsset, indexAccessor, [&](std::uint32_t index) {
                    indices.push_back(index + initialVerticesSize);
                });
            }
            // load vertices
            {
                fastgltf::Accessor& positionAccessor =
                    gltfAsset.accessors[prim.findAttribute("POSITION")->accessorIndex];

                vertices.resize(vertices.size() + positionAccessor.count);
                fastgltf::iterateAccessorWithIndex<glm::vec3>(
                    gltfAsset, positionAccessor, [&](glm::vec3 v, size_t index) {
                        auto& curVertex = vertices[initialVerticesSize + index];
                        curVertex.position = v;
                        curVertex.normal = {1, 0, 0};
                        curVertex.color = glm::vec4(1.0f);
                        curVertex.uvX = 0;
                        curVertex.uvY = 0;
                    });
            }
            // load normal
            if (auto normalAccessor = prim.findAttribute("NORMAL"))
            {
                fastgltf::Accessor& normalAcc = gltfAsset.accessors[normalAccessor->accessorIndex];
                fastgltf::iterateAccessorWithIndex<glm::vec3>(
                    gltfAsset, normalAcc,
                    [&](glm::vec3 n, std::size_t index) { vertices[initialVerticesSize + index].normal = n; });
            }
            // load UVs
            if (auto uvAccessor = prim.findAttribute("TEXCOORD_0"))
            {
                fastgltf::Accessor& uvAcc = gltfAsset.accessors[uvAccessor->accessorIndex];
                fastgltf::iterateAccessorWithIndex<glm::vec2>(gltfAsset, uvAcc, [&](glm::vec2 uv, std::size_t index) {
                    vertices[initialVerticesSize + index].uvX = uv.x;
                    vertices[initialVerticesSize + index].uvY = uv.y;
                });
            }
            // load vertex color
            if (auto colorAccessor = prim.findAttribute("COLOR_0"))
            {
                fastgltf::Accessor& colorAcc = gltfAsset.accessors[colorAccessor->accessorIndex];
                fastgltf::iterateAccessorWithIndex<glm::vec4>(
                    gltfAsset, colorAcc,
                    [&](glm::vec4 color, std::size_t index) { vertices[initialVerticesSize + index].color = color; });
            }
            newMesh->surfaces.push_back(newSurface);
        }

        /**
         * @brief Display the vertex normal
         */
        constexpr bool overrideColors = true;
        if (overrideColors)
        {
            for (auto& vertex : vertices)
            {
                vertex.color = glm::vec4(vertex.normal * 0.5f + 0.5f, 1.0f);
            }
        }
        newMesh->meshBuffers = engine->uploadMesh(indices, vertices);
        meshes.push_back(newMesh);
    }

    return meshes;
}

} // namespace huan

I think it's weird, because the iterateAccessor is fine, but iterateAccessorWithIndex got an error.

I check the definition and didn't find any wrong with that. Is that a bug?

i'm following vk-guide and use this library for loading gltf model. But I encounter this error, when I load the vertex.

Severity    Code    Description Project File    Line    Suppression State   Details
Error   GAF447572   no matching function for call to 'iterateAccessorWithIndex' F:\dev\vulkan\VulkanEngine\out\build\debug\VulkanEngine F:\dev\vulkan\VulkanEngine\renderer\src\huan\utils\gltf\gltf_loader.cpp 67      

Here is my code:

#include "huan/utils/gltf/gltf_loader.hpp"

#include <spdlog/spdlog.h>
#include <fastgltf/core.hpp>
#include <fastgltf/tools.hpp>
#include <glm/glm.hpp>

namespace huan
{

std::optional<std::vector<Ref<MeshAsset>>> loadGltfMeshes(VulkanEngine* engine, std::filesystem::path filePath)
{

    spdlog::info("Loading GLTF meshes from {}", filePath.string());
    auto data = fastgltf::GltfDataBuffer::FromPath(filePath);
    if (data.error() != fastgltf::Error::None)
    {
        spdlog::error("Failed to load GLTF data from {}", filePath.string());
        return std::nullopt;
    }
    constexpr auto gltfOptions = fastgltf::Options::LoadGLBBuffers | fastgltf::Options::LoadExternalBuffers;
    fastgltf::Parser parser{};
    fastgltf::Asset gltfAsset;
    auto load = parser.loadGltfBinary(data.get(), filePath.parent_path(), gltfOptions);
    if (load.error() == fastgltf::Error::None)
    {
        gltfAsset = std::move(load.get());
    }
    else
    {
        spdlog::error("Failed to parse GLTF binary from {}", filePath.string());
        return std::nullopt;
    }
    std::vector<Ref<MeshAsset>> meshes;

    // Process the GLTF asset to extract mesh data
    std::vector<uint32_t> indices;
    std::vector<Vertex> vertices;

    for (const auto& mesh : gltfAsset.meshes)
    {
        Ref<MeshAsset> newMesh = std::make_shared<MeshAsset>();
        // Populate meshAsset with data from mesh
        newMesh->name = mesh.name;
        indices.clear();
        vertices.clear();
        for (auto&& prim : mesh.primitives)
        {
            GeometrySuface newSurface;
            newSurface.startIndex = (uint32_t)indices.size();
            newSurface.count = (uint32_t)gltfAsset.accessors[prim.indicesAccessor.value()].count;
            size_t initialVerticesSize = vertices.size();
            // load indices
            {
                fastgltf::Accessor& indexAccessor = gltfAsset.accessors[prim.indicesAccessor.value()];
                indices.reserve(indices.size() + indexAccessor.count);
                fastgltf::iterateAccessor<std::uint32_t>(gltfAsset, indexAccessor, [&](std::uint32_t index) {
                    indices.push_back(index + initialVerticesSize);
                });
            }
            // load vertices
            {
                fastgltf::Accessor& positionAccessor =
                    gltfAsset.accessors[prim.findAttribute("POSITION")->accessorIndex];

                vertices.resize(vertices.size() + positionAccessor.count);
                fastgltf::iterateAccessorWithIndex<glm::vec3>(
                    gltfAsset, positionAccessor, [&](glm::vec3 v, size_t index) {
                        auto& curVertex = vertices[initialVerticesSize + index];
                        curVertex.position = v;
                        curVertex.normal = {1, 0, 0};
                        curVertex.color = glm::vec4(1.0f);
                        curVertex.uvX = 0;
                        curVertex.uvY = 0;
                    });
            }
            // load normal
            if (auto normalAccessor = prim.findAttribute("NORMAL"))
            {
                fastgltf::Accessor& normalAcc = gltfAsset.accessors[normalAccessor->accessorIndex];
                fastgltf::iterateAccessorWithIndex<glm::vec3>(
                    gltfAsset, normalAcc,
                    [&](glm::vec3 n, std::size_t index) { vertices[initialVerticesSize + index].normal = n; });
            }
            // load UVs
            if (auto uvAccessor = prim.findAttribute("TEXCOORD_0"))
            {
                fastgltf::Accessor& uvAcc = gltfAsset.accessors[uvAccessor->accessorIndex];
                fastgltf::iterateAccessorWithIndex<glm::vec2>(gltfAsset, uvAcc, [&](glm::vec2 uv, std::size_t index) {
                    vertices[initialVerticesSize + index].uvX = uv.x;
                    vertices[initialVerticesSize + index].uvY = uv.y;
                });
            }
            // load vertex color
            if (auto colorAccessor = prim.findAttribute("COLOR_0"))
            {
                fastgltf::Accessor& colorAcc = gltfAsset.accessors[colorAccessor->accessorIndex];
                fastgltf::iterateAccessorWithIndex<glm::vec4>(
                    gltfAsset, colorAcc,
                    [&](glm::vec4 color, std::size_t index) { vertices[initialVerticesSize + index].color = color; });
            }
            newMesh->surfaces.push_back(newSurface);
        }

        /**
         * @brief Display the vertex normal
         */
        constexpr bool overrideColors = true;
        if (overrideColors)
        {
            for (auto& vertex : vertices)
            {
                vertex.color = glm::vec4(vertex.normal * 0.5f + 0.5f, 1.0f);
            }
        }
        newMesh->meshBuffers = engine->uploadMesh(indices, vertices);
        meshes.push_back(newMesh);
    }

    return meshes;
}

} // namespace huan

I think it's weird, because the iterateAccessor is fine, but iterateAccessorWithIndex got an error.

I check the definition and didn't find any wrong with that. Is that a bug?

Share Improve this question asked 2 days ago qiyuewuyi2333qiyuewuyi2333 675 bronze badges 4
  • What version of fastgltf do you have? iterateAccessorWithIndex was added in 0.6.0: https://fastgltf.readthedocs.io/v0.7.x/changelog.html#id10 – drescherjm Commented 2 days ago
  • 1 When you get a "no matching function" error, the compiler will usually tell you what you tried to call along with a list of possible functions. It should not be too difficult to see how your attempt to call the function differs from the available possibilities. That's one of the superpowers of a statically typed language. – Jesper Juhl Commented 2 days ago
  • The Output Tab of Visual Studio often has a better / more verbose error message than the Errors List. – drescherjm Commented 2 days ago
  • 1 Take a look at the compiler output and post the complete error from there instead of the error title from the error list. – 3CxEZiVlQ Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Ok i get that. The ElementType in the function template( iterateAccessorWithIndex ) need to have a ElementTraits specialization. So just use #include <fastgltf/glm_element_traits.hpp> and done. For details, you can check this link

本文标签: cno matching function for call to iterateAccessorWithIndex when using fastgltfStack Overflow