首页 > 解决方案 > 如何计算导入模型的顶点法线?

问题描述

我正在使用 Assimp 导入 3D 模型数据,但是网格提供的法线看起来不像在任何其他 3D 应用程序中那样。虽然我不知道其他 3d 应用程序是计算法线还是只是从文件中导入法线,但我无法确定问题出在我的渲染方式还是 normlas 上。

无论其他应用程序是使用网格提供的法线还是计算它们,我都必须实现一个 ComputeNormal 方法。

据我所知,我的渲染管道很好,因为在我使用硬编码顶点数据渲染立方体之前,我工作得很好。

CMesh ModelLoader::processMesh(aiMesh * mesh, const aiScene * scene)
{
    std::vector<Vertex> vertices;
    std::vector<short> indices;
    for (UINT i = 0; i < mesh->mNumVertices; i++)
    {
        Vertex vertex;
        vertex.x = mesh->mVertices[i].x;
        vertex.y = mesh->mVertices[i].y;
        vertex.z = mesh->mVertices[i].z;
        if (mesh->mNormals)
        {
            vertex.NormalX = mesh->mNormals->x;
            vertex.NormalY = mesh->mNormals->y;
            vertex.NormalZ = mesh->mNormals->z;
        }
        vertices.push_back(vertex);
    }

    for (unsigned int i = 0; i < mesh->mNumFaces; i++)
    {
        aiFace face = mesh->mFaces[i];

        for (UINT j = 0; j < face.mNumIndices; j++)
            indices.push_back(face.mIndices[j]);
    }

    CMesh ret = CMesh(vertices, indices);
    return ret;
}

虽然法线加载正确,但模型看起来不像它在任何其他 3D 应用程序中应该或应该的样子。

标签: c++3dshader

解决方案


推荐阅读