thisistherk / fast_obj

Fast C OBJ parser
MIT License
616 stars 46 forks source link

Confused about OpenGL usage #44

Closed somerandompiggo closed 9 months ago

somerandompiggo commented 9 months ago

I'm testing the library with a cube OBJ exported from Blender. It seems to load fine, but loading it into a VBO and EBO using this code doesn't work properly:

glGenVertexArrays(1, &mesh->VAO);
glBindVertexArray(mesh->VAO);

glGenBuffers(1, &mesh->VBO);
glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
glBufferData(GL_ARRAY_BUFFER, objmesh->position_count * sizeof(float) * 3, objmesh->positions, GL_STATIC_DRAW);

glGenBuffers(1, &mesh->EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, objmesh->index_count * sizeof(unsigned int), objmesh->indices, GL_STATIC_DRAW);

When drawing with GL_TRIANGLES, nothing is displayed but if I use GL_LINES they go out from the origin to the 8 vertices of the cube. I think it's an issue with the indices, but I've been trying for days and I can't seem to figure out why

image (view from inside renderdoc)

Thanks!

cube.obj:

# Blender 4.0.1
# www.blender.org
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
s 0
f 5 3 1
f 3 8 4
f 7 6 8
f 2 8 6
f 1 4 2
f 5 2 6
f 5 7 3
f 3 7 8
f 7 5 6
f 2 4 8
f 1 3 4
f 5 1 2
thisistherk commented 9 months ago

Not really sure, but you might get something like that if you passed GL_UNSIGNED_SHORT to glDrawElements rather than GL_UNSIGNED_INT. fastObjMesh::positions[0] is a vertex at (0, 0, 0) - all valid indices are 1 based so it's never actually referenced, but if you got the size wrong to the glDrawElements call GL would see lots of 0 indices and might give you something like you describe. Hopefully give you something to look for anyway...

somerandompiggo commented 9 months ago

Had another crack at it, no luck so far. Here is a more complete snippet, I hope it helps since my last one was really minimal

Initialization:

fastObjMesh* objmesh = fast_obj_read(path);
if (objmesh == NULL) {
    printf("Error loading OBJ file with path: %s\n", path);
    return;
}

mesh->count = objmesh->index_count;

glGenVertexArrays(1, &mesh->VAO);
glBindVertexArray(mesh->VAO);

glGenBuffers(1, &mesh->VBO);
glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
glBufferData(GL_ARRAY_BUFFER, objmesh->position_count * sizeof(float) * 3, objmesh->positions, GL_STATIC_DRAW);

glGenBuffers(1, &mesh->EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, objmesh->index_count * sizeof(unsigned int), objmesh->indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

Draw loop:

glBindVertexArray(mesh->VAO);
glDrawElements(GL_TRIANGLES, mesh->count, GL_UNSIGNED_INT, 0);
somerandompiggo commented 9 months ago

Sorry, wrote my own loader to test. Exact same result, not an issue with the library. Will edit this comment when I find a fix