使用glDrawArrays绘制交错的VBO [英] drawing interleaved VBO with glDrawArrays

查看:187
本文介绍了使用glDrawArrays绘制交错的VBO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用glDrawElements来使用多个VBO(顶点,颜色,纹理和索引)进行渲染.我发现共享的顶点很少,所以我想切换到glDrawArrays和一个交错的VBO.

I'm currently using glDrawElements to render using multiple VBOs (vertex, color, texture, and index). I've found that very few vertices are shared, so I'd like to switch to glDrawArrays, and a single interleaved VBO.

我一直找不到一个清晰的例子:1)创建一个交错的VBO并向其添加一个四边形或三边形(垂直,颜色,纹理),以及2)执行使用glDrawArrays绘制它所需的一切.这两个步骤的代码是什么?

I've been unable to find a clear example of 1) creating an interleaved VBO and adding a quad or tri to it (vert, color, texture), and 2) doing whatever is needed to draw it using glDrawArrays. What is the code for these two steps?

推荐答案

离我远去:

//init
glBindBuffer(GL_ARRAY_BUFFER, new_array);
GLfloat data[] = { 
    0.f, 0.f, 0.f, 0.f, 0.f,
    0.f, 0.f, 100.f, 0.f, 1.f,
    0.f, 100.f, 100.f, 1.f, 1.f,
    0.f, 100.f, 100.f, 1.f, 1.f,
    0.f, 100.f, 0.f, 1.f, 0.f,
    0.f, 0.f, 0.f, 0.f, 0.f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

// draw
glBindBuffer(GL_ARRAY_BUFFER, new_array);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 5*sizeof(GLfloat), NULL);
glClientActiveTexture(GL_TEXTURE0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 5*sizeof(GLfloat), ((char*)NULL)+3*sizeof(GLfloat) );
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

此代码中没有什么特别神奇的东西.只是看看如何:

There is nothing particularly magic in this code. Just watch how:

  • data数组中的数据将被加载:按原样,所有连续数据
  • 各种属性的跨步设置为5 * sizeof(GLfloat),因为这就是数据中的内容:3个浮点表示位置,2个浮点表示texcoord.旁注,通常不希望这是2的幂.
  • 偏移量是从数组的开头算起的.因此,由于我们先存储顶点,所以顶点的偏移量为0.texcoord存储在位置数据的3个浮点之后,因此其偏移量为3 * sizeof(GLfloat).
  • The data from the data array gets loaded: as is, all contiguous
  • the stride for the various attributes is set to 5*sizeof(GLfloat), as this is what is in data: 3 floats for position and 2 for texcoord. Side note, you usually want this to be a power of 2, unlike here.
  • the offset is computed from the start of the array. so since we store vertex first, the offset for vertex is 0. texcoord is stored after 3 floats of position data, so its offset is 3*sizeof(GLfloat).

我之所以没有在其中包含颜色,是因为它们通常以UNORM的形式存储,这使得初始化代码更加混乱.您需要将GLfloat和GLubyte都存储在同一内存块中.到那时,如果您想在代码中使用结构,则结构可以提供帮助,但这在很大程度上取决于数据的最终来源.

I did not include color in there for a reason: they typically are stored as UNORMs, which makes for a messier initialization code. You need to store both GLfloat and GLubyte in the same memory chunk. At that point, structs can help if you want to do it in code, but it largely depends on where your data is ultimately coming from.

这篇关于使用glDrawArrays绘制交错的VBO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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