如何为导入的模型计算顶点法线?

时间:2019-06-09 12:34:50

标签: c++ 3d shader

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

无论其他应用程序是使用网格提供的法线还是计算它们,我都必须实现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应用程序中那样。

0 个答案:

没有答案