c++ - 使用 opengl 从 .obj 文件加载立方体时无法绘制正确的立方体

标签 c++ opengl blender .obj

我从各个可能的角度审视了这个问题,但我没有看到我做错了什么,所以我问你。我有一个使用 opengl 的小 C++ 程序,可以绘制一个立方体。我尝试使用此函数从 .obj 文件加载它

    std::vector< unsigned int > vertexIndices, uvIndices;
std::vector< glm::vec3 > temp_vertices;
std::vector< glm::vec2 > temp_uvs;

FILE* file = fopen(objFile.c_str(), "r");
if (file == NULL) {
    printf("Impossible to open the file !\n");
    return;
}

while (true)
{
    char lineHeader[128];

    int res = fscanf(file, "%s", lineHeader);
    if (res == EOF)
        break;

    if (strcmp(lineHeader, "v") == 0)
    {
        glm::vec3 vertex;
        fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
        temp_vertices.push_back(vertex);
    }
    else if (strcmp(lineHeader, "vt") == 0)
    {
        glm::vec2 uv;
        fscanf(file, "%f %f\n", &uv.x, &uv.y);
        temp_uvs.push_back(uv);
    }
    else if (strcmp(lineHeader, "f") == 0)
    {
        unsigned int vertexIndex[3], uvIndex[3];
        int matches = fscanf(file, "%d/%d %d/%d %d/%d", &vertexIndex[0], &uvIndex[0],
        &vertexIndex[1], &uvIndex[1], &vertexIndex[2], &uvIndex[2]);

        if(matches != 6)
        {


            matches = fscanf(file, "%d %d",
                &vertexIndex[1], &vertexIndex[2]);

            if (matches != 2)
            {
                printf("File can't be read by our simple parser : ( Try exporting with other options\n");

                return;
            }
        }
        else
        {
            uvIndices    .push_back(vertexIndex[0]);
            uvIndices    .push_back(vertexIndex[1]);
            uvIndices    .push_back(vertexIndex[2]);
        }

        vertexIndices.push_back(vertexIndex[0]);
        vertexIndices.push_back(vertexIndex[1]);
        vertexIndices.push_back(vertexIndex[2]);

    }
    else if (strcmp(lineHeader, "mtllib") == 0)
    {

        int res = fscanf(file, "%s", lineHeader);

        FILE* fileTexture = fopen(lineHeader, "r");

        while (true)
        {
            char lineHeader2[128];

            int res = fscanf(fileTexture, "%s", lineHeader);
            if (res == EOF)
                break;

            if (strcmp(lineHeader2, "map_Kd") == 0)
            {
                char textureFile[128];
                fscanf(fileTexture, "%s", textureFile);
                m_texture = Texture(textureFile);
            }
        }

    }
}

m_vertices = new float[temp_vertices.size() * 3];
m_bytesSizeVertices = temp_vertices.size() * 3 * sizeof(float);

for (int i = 0; i < temp_vertices.size(); i++)
{
    m_vertices[i * 3] = temp_vertices[i].x;
    m_vertices[(i * 3) + 1] = temp_vertices[i].y;
    m_vertices[(i * 3) + 2] = temp_vertices[i].z;
}


m_indices = new unsigned int[vertexIndices.size()];
m_bytesSizeIndices = (vertexIndices.size() * sizeof(unsigned int));

for (int i = 0; i < vertexIndices.size(); i++)
{
    m_indices[i] = vertexIndices[i];
}

m_trianglesNum = vertexIndices.size();


if(temp_uvs.size() != 0)
{
    m_texCoords = new float[temp_uvs.size() * 2];
    m_bytesSizeTexCoords = temp_uvs.size() * 2 * sizeof(float);

    for (int i = 0; i < vertexIndices.size(); i++)
    {
        m_texCoords[vertexIndices[i] * 2] = temp_uvs[uvIndices[i]].x;
        m_texCoords[vertexIndices[i] * 2 + 1] = temp_uvs[uvIndices[i]].y;
    }

    m_texture.load();
}
else
    m_bytesSizeTexCoords = 0;

(抱歉发了这么长的帖子)。当我加载这个时:

# Blender v2.76 (sub 0) OBJ File: ''
# www.blender.org
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
s off
f 2 3 4
f 8 7 6
f 5 6 2
f 6 7 3
f 3 7 8
f 1 4 8
f 1 2 4
f 5 8 6
f 1 5 2
f 2 6 3
f 4 3 8
f 5 1 8

它画了这个:

img

没有灯光,但有两个很好的面和从底端到另一个顶角的 2 个三角形

我在加载 vbo 时检查了数组的内容,一切看起来都很好,所以我完全迷失在这里。

最佳答案

在 den obj-file 中,索引从 1 开始(范围 [1, 8],但 OpenGL 期望它们从零开始(范围 [0, 7])。要解决这个问题,只需从您的每个元素中减去 1索引列表。

关于c++ - 使用 opengl 从 .obj 文件加载立方体时无法绘制正确的立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38996191/

相关文章:

c++ - ‘operator>>’ 中 ‘std::cin >>' 的模糊重载

c++ - 使用 swig typemap 将 vector<pair<int,int>> & 从 c++ 方法返回到元组的 python 列表

c++ - glGetTexImage() 在 ATI 卡上不能正常工作?当 mip 级别 > 0 时失败

python - 如何在 blender 上使用Anaconda环境?

javascript - Blender 导出到 JSON 以查找缺少 Material 的 THREE.js

c++ - 对象 std::cout 是如何构造/实例化的

c++ - 如何将 ASCII 字符串转换为十六进制?

c++ - headless QT4 OpenGL 应用程序

c++ - OpenGL 映射纹理到球体

python - Blender Python 使用 invoke_default 将运算符添加到菜单