Search code examples
openglgltf

OpenGL: using a single blob of data for ARRAY_BUFFER and ELEMENT_ARRAY_BUFFER


I'm trying to draw a glTF model where all geometry is indexed, e.g.:

"meshes": [
{
  "name": "Plane.010",
  "primitives": [
    {
      "attributes": {
        "POSITION": 0,
        "NORMAL": 1,
        "COLOR_0": 2
      },
      "indices": 3, <-- here
      "material": 0
    }
  ]
},

But there is only a single buffer defined, and no buffer views specify targets:

"buffers": [
    {
        "uri": "data.bin",
        "byteLength": 4518576
    }
]

So I send the binary data to the GPU using a single call to glBufferData with a VertexArray bound. Now I need to draw the primitives somehow using glDrawElements. Is there a way to create an ELEMENT_ARRAY_BUFFER and point it to a part of an already loaded buffer?


Solution

  • Is there a way to create an ELEMENT_ARRAY_BUFFER and point it to a part of an already loaded buffer?

    There is no need to, because OpenGL buffer objects aren't typed at all. You can bind the same buffer to different binding targets at the same time without any issue, so you can simply bind the same buffer as ARRAY_BUFFER and ELEMENT_ARRAY_BUFFER. You just need to use the particular byte offsets in your glDrawElements calls.