繁体   English   中英

使用opengl从.obj文件加载一个多维数据集时无法绘制正确的多维数据集

[英]Can't draw a correct cube when loading one from .obj file with opengl

我从各种可能的角度看了这个问题,但我看不出我做错了什么,所以我问你。 我有一个使用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

没有照明,但是有两个很好的面孔,并且有两个三角形从底端到另一个顶角

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM