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

时间:2016-08-17 12:06:46

标签: 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

没有光线,但是有两个面很好,两个三角形从底端到另一个顶角

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

1 个答案:

答案 0 :(得分:2)

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