opengl 中的顶点缓冲区 [英] Vertex Buffers in opengl

查看:53
本文介绍了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.

有没有什么方法可以像directx一样使用opengl?或者有什么关于如何提出更高级别的抽象以在两个系统中有效工作的建议?

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 具有 stride 和 offset 属性.这是专门允许结构数组.

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

因此,假设您想设置一个带有 float3 顶点和一个 float2 纹理坐标的 VBO,您可以执行以下操作:

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一个宏把offsets转成对应的指针(vbos用pointer参数作为偏移量),offsetof另一个宏在PackedVertex结构里面找到texcoord的偏移量.在这里,它可能是 sizeof(float)*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天全站免登陆