1
\$\begingroup\$

I'm on Win10, OpenGL 4.5

I wrote some code that takes mesh data from an OBJ file and puts it into a vertex buffer. I used this code to import a cube OBJ and render it in OpenGL. For some reason, although the importer output shows that all tris (broken down into vert components) were put into the buffer, the actual rendered cube only has 4 tris showing. What could be happening here?

Here is my relevant code:

//Before the main loop
//Import mesh data
OBJ& cube = ImportOBJ("meshes/cube.obj");

//Bind mesh data
GLuint vertexArrID;
glGenVertexArrays(1, &vertexArrID);
glBindVertexArray(vertexArrID);

GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, cube.vertBufferSize, cube.vertBuffer, GL_STATIC_DRAW);

//Inside the main loop
//Draw cube
glDrawArrays(GL_TRIANGLES, 0, cube.vertCount);
glDisableVertexAttribArray(0);

Here is what it looks like:

enter image description here

and here is the importer output broken down like [face index][vert index] (x, y, z):

[0][0] (-0.5, 0.5, -0.5)
[0][1] (0.5, 0.5, 0.5)
[0][2] (0.5, 0.5, -0.5)

[1][3] (0.5, 0.5, 0.5)
[1][4] (-0.5, -0.5, 0.5)
[1][5] (0.5, -0.5, 0.5)

[2][6] (-0.5, 0.5, 0.5)
[2][7] (-0.5, -0.5, -0.5)
[2][8] (-0.5, -0.5, 0.5)

[3][9] (0.5, -0.5, -0.5)
[3][10] (-0.5, -0.5, 0.5)
[3][11] (-0.5, -0.5, -0.5)

[4][12] (0.5, 0.5, -0.5)
[4][13] (0.5, -0.5, 0.5)
[4][14] (0.5, -0.5, -0.5)

[5][15] (-0.5, 0.5, -0.5)
[5][16] (0.5, -0.5, -0.5)
[5][17] (-0.5, -0.5, -0.5)

[6][18] (-0.5, 0.5, -0.5)
[6][19] (-0.5, 0.5, 0.5)
[6][20] (0.5, 0.5, 0.5)

[7][21] (0.5, 0.5, 0.5)
[7][22] (-0.5, 0.5, 0.5)
[7][23] (-0.5, -0.5, 0.5)

[8][24] (-0.5, 0.5, 0.5)
[8][25] (-0.5, 0.5, -0.5)
[8][26] (-0.5, -0.5, -0.5)

[9][27] (0.5, -0.5, -0.5)
[9][28] (0.5, -0.5, 0.5)
[9][29] (-0.5, -0.5, 0.5)

[10][30] (0.5, 0.5, -0.5)
[10][31] (0.5, 0.5, 0.5)
[10][32] (0.5, -0.5, 0.5)

[11][33] (-0.5, 0.5, -0.5)
[11][34] (0.5, 0.5, -0.5)
[11][35] (0.5, -0.5, -0.5)
\$\endgroup\$
3
  • \$\begingroup\$ Try swapping 1st and 2nd vertex on some triangles and see if some dis/appear. \$\endgroup\$
    – wondra
    Oct 5 '20 at 7:11
  • 1
    \$\begingroup\$ Added onto Wondras comment, try glDisable(GL_CULL_FACE); to see if this is a culling issue due to incorrect vertex order \$\endgroup\$
    – Jason
    Oct 5 '20 at 7:49
  • \$\begingroup\$ @Jason I commented out my call to glEnable(GL_CULL_FACE) a while back, but it did nothing to fix this issue. Same goes for a call to disable. \$\endgroup\$
    – sunsigil
    Oct 5 '20 at 13:29
1
\$\begingroup\$

This issue looks to me like the array that you are feeding for the buffer binding process might be the issuse.

    glBufferData(GL_ARRAY_BUFFER, cube.vertBufferSize, cube.vertBuffer, GL_STATIC_DRAW);
--------------This*----------------------^^ 

this* parameter requires a stride , glBufferData(GL_ARRAY_BUFFER, vertexArray.size() * sizeof(float), vertexArray.data(), GL_STATIC_DRAW)

Try looking into this.

Also try keeping the array as a serialised byte array meaning [0]x , [1]y, [2]z ---> [3]x,[4]y,[5]z----->n and so on, for the code you have.

dosent have to be this way and can be any container structure you want but you will have to set the stride so that it knows what and how much it needs to move to get the info for one vertex that will be passes into the shader context.

you can also try this

void _Renderer::setModelDataInBuffers(std::vector<float> vertexArray)
{
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, vertexArray.size() * sizeof(float), vertexArray.data(), GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    //(nth attribute index, no of elements per sample, type,bool, stride for thewhole data set, location in stride)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

    // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}
\$\endgroup\$
2
  • \$\begingroup\$ You nailed it with the stride being the issue. vertBufferSize was being set to sizeof(GLfloat) * vertCount, but because of the way my buffer is set up, i needed it to be 3x that large. Thanks. \$\endgroup\$
    – sunsigil
    Oct 8 '20 at 20:39
  • \$\begingroup\$ yes 3x the sizeof() each data type \$\endgroup\$ Oct 11 '20 at 7:18
0
\$\begingroup\$

I believe before your draw call "glDrawArrays" you need to enable the VBO attribute stored in your VAO. Try putting glEnableVertexAttribArray(0 or 1) whatever attribute index you are storing your VBO in, and be sure to disable after rendering with glDisableVertexAttribArray(0 or 1)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.