因此,我能够从 .ply 文件中读出我需要的顶点、法线和索引,并将它们写入 VBO。但是,我没有得到正确的形状。看来我的指数是关闭的。
这是我的结构:
typedef struct vtx {
float x, y, z;
float nx, ny, nz;
} vtx;
typedef struct Face {
unsigned int count;
unsigned int *vertices;
float nx, ny, nz;
} Face;
typedef struct PLY_vals {
Face **f;
vtx **v;
unsigned int vcount;
unsigned int fcount;
int norms;
float center[3];
} PLY_vals;
设置顶点和三角形:
nindices = ply.fcount * 3;
nvertices = ply.vcount;
pindices = new GLuint[nindices];
plyverts = new Vertex[nvertices];
for (int i = 0; i < nvertices; i++)
{
// Vertices
plyverts[i].location[0] = ply.v[i]->x;
plyverts[i].location[1] = ply.v[i]->y;
plyverts[i].location[2] = ply.v[i]->z;
plyverts[i].location[3] = 1;
// Normals
plyverts[i].normal[0] = ply.v[i]->nx;
plyverts[i].normal[1] = ply.v[i]->ny;
plyverts[i].normal[2] = ply.v[i]->nz;
plyverts[i].normal[3] = 0;
}
// set indices (assumes all faces have 3 vertices
int pos=0;
for (int i = 0; i < nindices/3; i++)
{
pindices[pos++] = ply.f[i]->vertices[0]; // first vertex
pindices[pos++] = ply.f[i]->vertices[1]; // second vertex
pindices[pos++] = ply.f[i]->vertices[2]; // third vertex
}
这应该是兔子:
任何想法?我猜顶点的顺序不正确,但我检查了一下,它们似乎匹配。