顶点缓冲区在OpenGL [英] Vertex Buffers in opengl

查看:244
本文介绍了顶点缓冲区在OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个小的3D图形游戏/演示个人学习。我知道D3D9和相当多关于D3D11但很少关于OpenGL的时刻,所以我打算抽象出图形的实际的渲染,使我的场景图和一切上面,它需要知之甚少如何实际绘制图形。我打算用D3D9工作,然后添加D3D11的支持,最终OpenGL的支持。就像一个学习的过程,了解3D图形和抽象。

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.

我不知道很多关于OpenGL在这一点上,虽然,不希望我的抽象接口暴露任何不是简单的在OpenGL实现。具体来说,我期待在顶点缓冲区。在D3D它们基本上是结构的阵列,但在看的OpenGL接口的等效似乎是顶点数组。然而,这些似乎是有组织的,而不同的,你需要的顶点一个单独的数组,一个正常人,一个纹理坐标等,并设置与的glVertexPointer,glTexCoordPointer等。

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.

我希望能够实现一个VertexBuffer界面很像了DirectX之一,但它看起来像在D3D你有结构的阵列,并在OpenGL你需要的,这使得找到一个共同的抽象相当的每个元素一个单独的数组很难作出有效的。

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.

有没有办法使用OpenGL类似的方式到DirectX?或者对如何拿出一个更高层次的抽象任何建议,将工作有效地与这两个系统?

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.

所以,说你要建立一个VBO与FLOAT3顶点和FLOAT2纹理坐标,你会做以下内容:

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));

通过BUFFER_OFFSET宏把偏移到相应的指针(维也纳组织使用指针参数作为偏移),和对offsetof另一个宏查找TEXCOORD所述PackedVertex结构内部的偏移量。在这里,很可能的sizeof(浮动)* 3,因为将不可能是结构内部的填充。

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.

这篇关于顶点缓冲区在OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆