【问题标题】:Rendering .ply files in OpenGL在 OpenGL 中渲染 .ply 文件
【发布时间】:2013-10-26 16:48:18
【问题描述】:

所以,我可以从 .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
}

这应该是兔子:

有什么想法吗?我猜顶点的顺序不正确,但我检查了一下,它们似乎匹配。

【问题讨论】:

  • 看起来顶点是正确的,因为它看起来像兔子的形状,我认为错误的是索引,因为它正在制作这些巨大的三角形。
  • 尝试将顶点渲染为点。这会告诉你顶点是否正确。
  • 每个面都包含 3 个顶点吗?
  • 是的,兔子就是这样

标签: c++ opengl rendering


【解决方案1】:

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

【讨论】:

  • 差不多就是这样。我使用的是 GL_UNSIGNED_BYTE 而不是 GL_UNSIGNED_INT。谢谢!
猜你喜欢
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 2012-10-30
  • 2015-07-08
  • 2011-05-25
相关资源
最近更新 更多