gpt4 book ai didi

c++ - 尝试从 OpenGL 中的 .object 绘制网格时不显示某些三角形

转载 作者:行者123 更新时间:2023-11-30 01:16:08 25 4
gpt4 key购买 nike

我正在尝试显示从 .obj 文件加载的对象。我使用此函数读取 .obj:

bool loadOBJ(
const char * path,
std::vector<glm::vec3> & vertices,
std::vector<glm::vec3> & vertexIndices
){
printf("Loading OBJ file %s...\n", path);

FILE * file = fopen(path, "r");
if( file == NULL ){
printf("Impossible to open the file ! Are you in the right path ? \n");
getchar();
return false;
}

while( 1 ){

char lineHeader[128];
// read the first word of the line
int res = fscanf(file, "%s", lineHeader);
if (res == EOF)
break; // EOF = End Of File. Quit the loop.

// else : parse lineHeader

if ( strcmp( lineHeader, "v" ) == 0 ){
glm::vec3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
vertices.push_back(vertex);
}else if ( strcmp( lineHeader, "f" ) == 0 ){

glm::vec3 vertexIndex;
fscanf(file, "%f %f %f\n", &vertexIndex.x, &vertexIndex.y, &vertexIndex.z );
vertexIndices.push_back(vertexIndex);
}else{
// Probably a comment, eat up the rest of the line
char stupidBuffer[1000];
fgets(stupidBuffer, 1000, file);
}

}

return true;
}

然后我在我的初始化函数中调用这个函数。加载网格后,我通过在此处循环遍历顶点来显示它:

for (unsigned int i = 0; i < meshVertices.size(); i++)
{
glBegin(GL_TRIANGLES);
glVertex3f(meshVertices[faceIndices[i].x-1].x, meshVertices[faceIndices[i].x-1].y, meshVertices[faceIndices[i].x-1].z);
glVertex3f(meshVertices[faceIndices[i].y-1].x, meshVertices[faceIndices[i].y-1].y, meshVertices[faceIndices[i].y-1].z);
glVertex3f(meshVertices[faceIndices[i].z-1].x, meshVertices[faceIndices[i].z-1].y, meshVertices[faceIndices[i].z-1].z);
glEnd();

}

但是,当程序运行时,对象的远侧根本不会加载,并且随机三角形会丢失。像这样:

Screenshot

最佳答案

您不想循环顶点的大小,而是循环面的大小。

你的循环应该是for (unsigned int i = 0; i < faceIndices.size(); i++)

您正在遍历变量 faceIndices[i]我想你想画所有的三角形,而不是所有的点(顶点)!


旁注:我在教你这门类(class)吗? :D该代码看起来很熟悉......

关于c++ - 尝试从 OpenGL 中的 .object 绘制网格时不显示某些三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27175725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com