[英]Can't draw a correct cube when loading one from .obj file with opengl
I looked at this problem from every conceivable angle and I don't see what I'm doing wrong so I'm asking you. 我从各种可能的角度看了这个问题,但我看不出我做错了什么,所以我问你。 I have a little C++ program that uses opengl and can draw a cube okay.
我有一个使用opengl的小C ++程序,可以画一个立方体。 I tried loading it from an .obj file with this function
我尝试使用此功能从.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;
(sorry for the long post). (很抱歉,帖子很长)。 and when i load this :
当我加载这个:
# 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
it draws this: 它得出这样的:
there's no lighting but there are two faces that are fine and 2 triangles going from the bottom end to the other top corner 没有照明,但是有两个很好的面孔,并且有两个三角形从底端到另一个顶角
I checked the content of the arrays when loading the vbo and every thing looks fine so I'm completely lost here. 我在加载vbo时检查了数组的内容,每件事看起来都很好,所以我在这里完全迷失了。
在den obj-file中,索引从1开始(范围[1,8],但是OpenGL期望它们从零开始(范围[0,7])。要解决这个问题,只需从索引列表中的每个元素中减去1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.