OpenGL expects 4 bytes per index - I have 2 bytes per index

I've read a gltf2 (.glb) file into memory on the heap. And I've determined where the index data for my mesh is and have got a pointer to it, now I want to pass that data into OpenGL. The problem is, the gltf file stores the indices as uint16/2 bytes, but the call to OpenGL (glBufferData) expects uint32/4 bytes.

So as a workaround I'm having to do this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
uint16* ptr = meshes[ 0 ].index_data; // this is my pointer to the data I already have
// then I have to allocate more memory on the heap, cycles through
// the existing data and convert it to uint32
GLuint* new_indices = new GLuint[ index_count ];
for( uint32 j = 0; j < index_count; j++ ) {
  uint16 thisIndex = *ptr;
  new_indices[ j ] = ( GLuint )thisIndex;
  ptr++;
}

GLuint ibo;
glGenBuffers( 1, &ibo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
// I then have to pass this new data on the heap to OpenGL
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( new_indices[ 0 ] ) * index_count, new_indices, GL_STATIC_DRAW );


Is there an easier way to do this without having to allocate more memory on the heap and cycle through my indices? The call to glBufferData is where the problem lies, it wants the data from meshes[0].index_data, but in a format different to how it is stored in the .glb file.

Thanks in advance.
Last edited on
[Disclaimer: I haven't programmed with OpenGL in a while, so someone else might be able to help more]

You say the glBufferData call expects 32-bit elements? I don't see where it says this. Does the glBufferData call itself generate an error? I think you just need to make sure you are passing GL_UNSIGNED_SHORT when drawing or otherwise using the elements.
Last edited on
so, this does not work?

glBufferData( meshes, 2 * index_count, new_indices, GL_STATIC_DRAW );

what does this do, if it does not work?
Last edited on
That would only be sending over half the data :)
How so? Meshs is 16 bit / 2 bytes * index_count ... ?
or did I get the order wrong, should it be
glBufferData(GL_ELEMENT_ARRAY_BUFFER , 2 * index_count, meshes, GL_STATIC_DRAW );
!! in which case, should line 11 change to be a 16 bit thing?!!

I havent used GL in a while either!
Last edited on
My mistake, your previous code would actually just not compile.

I think OP would need to pass ptr, not meshes.

Yes, your current line of code would be better, I think (of course, I would use sizeof(uint16) instead of 2 to express intent better).
Last edited on
Lol, sorry about the mess.
Yes, ptr not meshes. My mind wandered badly there. And yes, sizeof instead of 2 is the way to go, that was actually intentional to highlight what needed doing.
Again, sorry for my mental state.
Topic archived. No new replies allowed.