因此,我能够从.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
}

这应该是一个兔子:

任何的想法?我猜测顶点的顺序不正确,但是我检查了一下,发现它们似乎匹配了。

最佳答案

当我为glDrawElements函数使用GL_UNSIGNED_SHORT而不是GL_UNSIGNED_INT类型说明符时,我遇到了完全相同的问题。兔子模型(我假设是斯坦福兔子?)具有超过200000个顶点,因此无符号short不足以存储索引,因为其最大值为65535-因此应使用无符号int。也许这就是您的问题的答案?

07-27 13:22