开发者

Vertex Buffers in opengl

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜