4

I'm making a small 3d graphics game/demo for personal learning. I know d3d9 and quite a bit about d3d11 but little about opengl at the moment so I'm intending to abstract out the actual rendering of the graphics so that my scene graph and everything "above" it needs to know little about how to actually draw the graphics. I intend to make it work with d3d9 then add d3d11 support and finally opengl support. Just as a learning exercise to learn about 3d graphics and abstraction.

I don't know much about opengl at this point though, and don't want my abstract interface to expose anything that isn't simple to implement in opengl. Specifically I'm looking at vertex buffers. In d3d they are essentially an array of structures, but looking at the opengl interface the equivalent seems to be vertex arrays. However these seem to be organised rather differently where you need a separate array for vertices, one for normals, one for texture coordinates etc and set the with glVertexPointer, glTexCoordPointer etc.

I was hoping to be able to implement a VertexBuffer interface much like the the directx one but it looks like in d3d you have an array of structures and in opengl you need a separate array for each element which makes finding a common abstraction quite hard to make efficient.

Is there any way to use opengl in a similar way to directx? Or any suggestions on how to come up with a higher level abstraction that will work efficiently with both systems?

1 Answer 1

3

Vertex Arrays have a stride and an offset attributes. This is specifically to allow for arrays of structure.

So, say you want to set up a VBO with a float3 vertex and a float2 texture coordinate, you'd do the following:

// on creation of the buffer
typedef struct { GLfloat vert[3]; GLfloat texcoord[2]; } PackedVertex;
glBindBuffer(GL_ARRAY_BUFFER, vboname);
glBufferData(...); // fill vboname with array of PackedVertex data

// on using the buffer
glBindBuffer(GL_ARRAY_BUFFER, vboname);
glVertexPointer(3, GL_FLOAT, sizeof(PackedVertex), BUFFER_OFFSET(0)));
glTexCoordPointer(2, GL_FLOAT, sizeof(PackedVertex), BUFFER_OFFSET(offsetof(PackedVertex, texcoord));

With BUFFER_OFFSET a macro to turn offsets into the corresponding pointers (vbos use the pointer parameter as an offset), and offsetof another macro to find the offset of texcoord inside the PackedVertex structure. Here, it's likely sizeof(float)*3, as there will unlikely be any padding inside the structure.

5
  • Ah ok that makes sense. You still have to call different functions to set up the different attribues then, but I should still be able to share a structure with d3d. Hmm, i really need to learn more about opengl :)
    – jcoder
    Apr 5, 2010 at 13:03
  • @JB, actually, if you go with GL3.0, It introduces vertex array objects, that encapsulate the whole thing. opengl.org/registry/specs/ARB/vertex_array_object.txt
    – Bahbar
    Apr 5, 2010 at 13:16
  • Avoid gl..Pointer as the plague. Use glVertexAttribPointer instead. Also, avoid interleaving data (strides) if you can help it. Rather save the individual vertex attributes sequentially in one buffer object. Remember to make the GPU happy by aligning the data buffers.
    – Mads Elvheim
    Apr 5, 2010 at 23:17
  • @Mads Elvheim: Why ? because they're deprecated in GL3 ? I don't know what GL JB uses. They're no slower, and are more expressive. But yeah, they are deprecated. Also, wrt interleaving, I can tell you for a fact it makes rendering faster on some platforms. Better caching behavior on vertex fetch, mostly. Plus, that was the original question.
    – Bahbar
    Apr 6, 2010 at 7:19
  • 1
    Sorry. I should have been more clear. In my opinion, forcing per-vertex data to be something specific, kind of restricts what you can express with a shader. I thought I'd share my view. Also, interleaving vertex data is fast on some embedded platforms (usually OpenGL ES), but not OpenGL on desktop-PCs.
    – Mads Elvheim
    Apr 7, 2010 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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