Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering .ply files in OpenGL

So, I'm able to read out the vertices, normals, and indices I need from the .ply file and write them to the VBO. However, I'm not getting the correct shapes. It seems my indices are off.

Here's my structs:

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;

Setting up vertices and triangles:

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
}

This is supposed to be a bunny:

enter image description here

Any idea? I'm guessing that the vertices are not in the correct order, but I checked and they seem to match up.

like image 410
Riftus Avatar asked Dec 04 '25 14:12

Riftus


1 Answers

I had the exact same problem when I used GL_UNSIGNED_SHORT instead of GL_UNSIGNED_INT type specifier for the glDrawElements function. The bunny model (I assume it's the Stanford bunny?) has got over 200000 vertices so the unsigned short is not enough to store the indices since its max value is 65535 - therefore an unsigned int should be used. Maybe that's the answer to your problem?

like image 70
tybur Avatar answered Dec 06 '25 10:12

tybur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!