0
\$\begingroup\$

I successfully can render two triangles (with a texture) that looks like the side of a crate and also "walk" around it. Also, a small .obj file parser is available which gives me a float array of the vertices, texture coords and an unsigned int array of the indices (of a simple cube).

The vertices and texture coords look like the following:

float vertices[] =
{
     // positions         // texture coords
     0.5f,  0.5f, 0.0f,   1.0f, 1.0f,
     0.5f, -0.5f, 0.0f,   1.0f, 0.0f,
    -0.5f, -0.5f, 0.0f,   0.0f, 0.0f,
    -0.5f,  0.5f, 0.0f,   0.0f, 1.0f
};

And the indices:

unsigned int indices[] =
{
    0, 1, 3, // first triangle
    1, 2, 3  // second triangle
};

To assign the vertices and tex coords I use the following:

unsigned int VBO;
glGenBuffers( 1, &VBO );
glBindBuffer( GL_ARRAY_BUFFER, VBO );
glBufferData( GL_ARRAY_BUFFER, 5 * 4 * sizeof( float ), vertices, GL_STATIC_DRAW );

Creating an IBO

call glEnableVertexAttribArray()
glVertexAttribPointer()

Creating a shader, a texture etc.. and draw it with glDrawElements().

This works all very well, I just can't wrap my head around how to use just a single VBO with the "split" data that my .obj parser gives back.
I now (when using the .obj parser) have the vertices and texture coords separated; how do I put that two arrays into one VBO?

The picture I have in mind is to have fewer draw calls and also prevent redundant code if I, for example, use the same model for 100 times.

\$\endgroup\$
  • \$\begingroup\$ For vertex arrays, simply concatenate them as is, but for indices you will need to add offset for new consequitive stream of index data. That is, for each index of the new index array you add current size of the concatenated array. index = index + concatenated_ib_size; \$\endgroup\$ – Ocelot Jun 27 at 18:10
  • \$\begingroup\$ @Ocelot That sounds promising, can you please put it in an answer so I can accept it? Also, can you please explain how to use adjust glEnableVertexAttribArray() and glVertexAttribPointer() in this particular case? \$\endgroup\$ – Schrödinger's cat Jun 28 at 11:35
  • \$\begingroup\$ Wait... Do you want to pack multiple vertex attributes into one VBO or you want to concatenate multiple vertex arrays into one? \$\endgroup\$ – Ocelot Jun 28 at 19:11
  • \$\begingroup\$ I want to pack multiple vertex attributes into one VBO \$\endgroup\$ – Schrödinger's cat Jun 28 at 20:20
  • \$\begingroup\$ Well then you can't pack more than 4 floats per vertex attribute in one VBO. Specification says that glVertexAttribPointer() parameter "size" must be in range [1; 4] which corresponds to the types from float to vec4 in your shader program. You can, at best, pack 2D position and UV coordinates which will be two 2D vectors packed in one 4D vector. Specification page for glVertexAttribPointer() : khronos.org/registry/OpenGL-Refpages/gl4/html/… \$\endgroup\$ – Ocelot Jun 28 at 20:30

Your Answer

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

Browse other questions tagged or ask your own question.