【问题标题】:Assimp opengl load model vertices some values greater than 1Assimp opengl加载模型顶点一些大于1的值
【发布时间】:2021-07-17 01:44:45
【问题描述】:

我有以下来自 learnopengl.com 的代码

void Model::load_model(string path)
{
        //read file via ASSIMP
        Assimp::Importer Importer;
        const aiScene* scene = Importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
        //check for errors
        if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)// if not zero
        {
                cout << "error, assimp ," << Importer.GetErrorString() << endl;
                return;
        }
        //retrieve the directory path of the filepath
        directory = path.substr(0, path.find_first_of('/'));
        process_node(scene->mRootNode, scene);
}
/*
 * Process a node in a recursive fashion . Process each individual mesh located at the node and repeat this process on its children nodes (if any)
 */
void Model::process_node(aiNode* node, const aiScene* scene)
{
        for( GLuint i = 0; i < node->mNumMeshes; i++ )
        {
                //the node object only contains indices to index the actual objects of the scene.
                //The scene contains all the data , node is just to keep stuff organized( like relations between nodes )
                aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
                meshes.push_back(process_mesh(mesh, scene));
        }
        //after we've processed all the meshes ( if any ) we then recusrsively process each of the children nodes 
        for(GLuint i = 0; i < node->mNumChildren; i++)
        {
                process_node(node->mChildren[i], scene);
        }
}
Mesh Model::process_mesh(aiMesh* mesh, const aiScene* scene)
{
        //data to fill
        vector<Mesh::Vertex> vertices;
        vector<GLuint> indices;
        vector<Mesh::Texture> textures;

        //walk through each of the meshes vertices
        for(GLuint i = 0; i < mesh->mNumVertices; i++)
        {
                Mesh::Vertex vertex;
                // we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class
                //so we transfter the data to this placeholder glm::vec3 first 
                glm::vec3 vector;
                //positions
                vector.x = mesh->mVertices[i].x; 
                vector.y = mesh->mVertices[i].y;
                vector.z = mesh->mVertices[i].z;
                vertex.position = vector;
                //normals
                vector.x = mesh->mNormals[i].x;
                vector.y = mesh->mNormals[i].y;
                vector.z = mesh->mNormals[i].z; ...

当我打印出mesh-&gt;mVertices[i].x y 和 z 的前 20 个值时,我得到一些大于 1 的值,如下所示

x1: 1.58967 Y1: -0.618526 z1: -0.683333
x1: 1.58939 Y1: -0.626895 z1: -0.681676

我正在导入的 obj 文件没有任何大于 1 的值,这导致渲染失败。问题可能出在哪里?

【问题讨论】:

  • opengl也可以渲染值大于1的顶点吗?
  • 看起来一点也不像 C :P
  • 3D 模型不限于任何特定尺寸。 OpenGL 对此也没有任何限制。它的唯一限制是投影后的顶点必须在所有轴上的 [-1, 1] 范围内才能可见。
  • 我再次检查了 3D 模型,是的,有大于 1 的值,因此上面的代码按预期工作。使用 renderdoc 我已经能够看到 position 的缓冲区内容以像 1.1204E-44 这样的小数字结束。所有缓冲区内容都以某种方式设置为如此小的指数数字。

标签: c++ opengl assimp


【解决方案1】:

模型的位置与其模型坐标系有关。所以它们可以变得更大或小于 1、-1 或其他。对于任何值,OpenGL 不限于 [0 ... 1] 的范围。因此,当您的渲染器需要从 0 到 1 的范围时,您需要:

  1. 找到坐标的最小值和最大值

  2. 重新缩放所有坐标:

    浮动比例 = 1.0/(max - min); foreach(v:mesh.Vertices) v=v*比例;

或者重写你的渲染器,他可以渲染任意坐标。

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 2016-12-26
    • 2020-12-26
    • 2018-07-23
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多