为什么将GL_ELEMENT_ARRAY_BUFFER绑定到0会产生记忆错误? [英] why does binding a GL_ELEMENT_ARRAY_BUFFER to 0 produce a memmove error?

查看:620
本文介绍了为什么将GL_ELEMENT_ARRAY_BUFFER绑定到0会产生记忆错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误使我花了很多时间来修复. 我一直得到EXC_BAD_ACCESS和对memmove错误的引用,没有任何进一步的描述,直到我评论以下行:

I had a bug that took me quite some time to fix. I kept getting EXC_BAD_ACCESS and a reference to a memmove error without any further description until I commented the following line:

[self loadShaders];

[self loadShaders];

glGenVertexArraysOES(1, &_vao);
glBindVertexArrayOES(_vao);

// Vertex Buffer
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);

glEnableVertexAttribArray(ATTRIB_TEXTURE);
glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));

// Index Buffer
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER,0);  

////////// COMMENTED THIS ONE //////////////
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);//
////////////////////////////////////////////

glBindVertexArrayOES(0);

我认为将缓冲区绑定到0意味着要取消绑定它,所以我真的不明白如何使我的应用程序崩溃.

I thought binding a buffer to 0 meant unbinding it, so I really cant understand how would that made my app crash.

感谢您提供的信息!我只是不留这个顾虑...

Thanks for the information! I just do not stay with this concern...

我的结构:

const Vertex Vertices[4] = {
    {{0.75, -1, 0}, {1, 0, 0, 1},     {0.125, 0.833496}},
    {{0.75, 1, 0}, {0, 1, 0, 1},      {0.125, 1}},
    {{-0.75, 1, 0}, {0, 0, 1, 1},     {0,     1}},
    {{-0.75, -1, 0}, {0, 0, 0, 1},    {0,     0.833496}},
};

const GLushort Indices[6] =
{
    0, 1, 2,
    2, 3, 0
};

推荐答案

从代码中看,此函数似乎在某个初始化例程中,在该例程中您初始化属性数据,该数据存储在绑定的VAO中,以便绘制时,您只需绑定VAO.

From your code, it looks as if this function is in some initialization routine, where you initialize your attribute data, which gets stored in the bound VAO, so that when drawing you only need to bind the VAO.

VAO依次封装绘制VBO所需的所有状态,这是属性(由gl(En/Dis)ableVertexAttribArray设置),属性源和属性(由glVertexAttribPointer设置),当前绑定的索引缓冲区(使用glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...)设置).请注意,它不存储当前绑定的顶点缓冲区,因为此信息以属性状态存储.

A VAO in turn encapsulates all the state needed for drawing VBOs, which is the enabled flags of your attributes (set by gl(En/Dis)ableVertexAttribArray), the attribute sources and properties (set with glVertexAttribPointer), and the currently bound index buffer (set with glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...)). Note that it doesn't store the currently bound vertex buffer, as this information is stored in the attribute state.

所以发生的事情是,您创建并绑定了索引缓冲区,设置了它的数据,然后取消绑定它,而VAO仍然处于活动状态.因此,VAO状态将存储0GL_ELEMENT_ARRAY_BUFFER绑定.当您现在用

So what happens is, that you create and bind the index buffer, set its data, then unbind it, with the VAO still active. So the VAO state will store a GL_ELEMENT_ARRAY_BUFFER binding of 0. When you now draw something with

glBindVertexArrayOES(_vao);
glDrawElements(...);

没有缓冲区绑定,并且glDrawElements失败,因为VAO不能与客户端阵列一起使用.您要么必须使用VBO来存储索引数据,要么必须绘制未索引的基元(使用glDrawArrays).

there is no buffer bound and the glDrawElements fails, as VAOs don't work with client side arrays. You either have to use a VBO for the index data or draw non-indexed primitives (with glDrawArrays).

也不会

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBindVertexArrayOES(_vao);
glDrawElements(...);

工作,因为绑定索引缓冲区在绑定时被VAO覆盖(其索引缓冲区为0).如果先绑定VAO,然后绑定索引缓冲区,则该方法将起作用,但这只会避免该问题.

work, because the bound index buffer is overwritten by the VAO when it gets bound (whose index buffer is 0). It would work if you first bind the VAO and then the index buffer, but that would only circumvent the problem.

从概念上讲,绑定的GL_ELEMENT_ARRAY_BUFFER是VAO状态的一部分,因此,不应在VAO初始化例程中将其绑定到0(仅在不需要任何索引数据的情况下).使用VAO时,也不允许将客户端数组用于索引或顶点数据.如果您不想绘制索引的几何图形,只需使用glDrawArrays而不是glDrawElements,但是索引缓冲区还是过时的.

Conceptually the bound GL_ELEMENT_ARRAY_BUFFER is part of the VAO state, so you should not bind it to 0 in your VAO initialization routine (only if you don't need any index data). And neither are you allowed to use client side arrays for index or vertex data when using VAOs. If you don't want to draw indexed geometry though, just use glDrawArrays instead of glDrawElements, but then the index buffer is obsolete, anyway.

这篇关于为什么将GL_ELEMENT_ARRAY_BUFFER绑定到0会产生记忆错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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