gpt4 book ai didi

c++ - 调用 glDrawElements 导致访问冲突

转载 作者:太空狗 更新时间:2023-10-29 21:44:57 25 4
gpt4 key购买 nike

我目前正试图弄清楚 VBO 的工作原理,这很遗憾地导致了可怕的问题:/

我正在从 .obj 文件加载数据(有关 .obj 文件,请参阅:http://pastebin.com/B8uibDvV)并(试图)将其放入 VBO

我绑定(bind)数据的代码如下所示:

//Generate Buffers
glGenBuffers(1, &this->glVerticesBufferID);
glGenBuffers(1, &this->glIndexBufferID);

//bind the buffers to set the data
glBindBuffer(GL_ARRAY_BUFFER, this->glVerticesBufferID);
glBufferData(GL_ARRAY_BUFFER, this->verticesList.size() * sizeof(GLfloat), 0, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->glIndexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indexList.size() * sizeof(GLshort), 0, GL_STATIC_DRAW);

glBufferSubData(GL_ARRAY_BUFFER, 0, this->verticesList.size(), this->verticesList.data());
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, this->indexList.size(), this->indexList.data());

//Clear binds
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

最后我用这个渲染了一切:

glBindBuffer(GL_ARRAY_BUFFER, this->glVerticesBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->glIndexBufferID);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawElements(GL_TRIANGLES, this->indexList.size(), GL_UNSIGNED_SHORT, this->indexList.data());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

最后,我在 VisualStudio 2010 的输出中收到了访问冲突消息

0xC0000005: 读取位置 0x0147fbd8 访问冲突。

所以我的问题是如何解决这个问题? (或者如果我错了怎么办)

----------------编辑 1----------------

http://pastebin.com/DEbZH9iy

绑定(bind)数据

 glGenBuffers(1, &this->glVerticesBufferID);
glBindBuffer(GL_ARRAY_BUFFER, this->glVerticesBufferID);
glBufferData(GL_ARRAY_BUFFER, (this->verticesList.size() + this->textureCoordList.size() + this->normalsList.size()) * sizeof(GLfloat), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,
0,
this->verticesList.size() * sizeof(GLfloat),
this->verticesList.data()
);
glBufferSubData(
GL_ARRAY_BUFFER,
this->verticesList.size() * sizeof(GLfloat),
this->normalsList.size() * sizeof(GLfloat),
this->normalsList.data()
);
glBufferSubData(
GL_ARRAY_BUFFER,
(this->verticesList.size() + this->normalsList.size()) * sizeof(GLfloat),
this->textureCoordList.size() * sizeof(GLfloat),
this->textureCoordList.data()
);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &this->glIndexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->glIndexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indexList.size() * sizeof(GLushort), this->indexList.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);



pub_glVerticesBufferID = glVerticesBufferID;
pub_glIndexBufferID = glIndexBufferID;

渲染部分:

 glBindBuffer(GL_ARRAY_BUFFER, this->glVerticesBufferID);
glVertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (GLvoid*)0);
glNormalPointer(GL_FLOAT, 3 * sizeof(GLfloat), (GLvoid*)(this->verticesList.size() * sizeof(GLfloat)));
glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), (GLvoid*)((this->normalsList.size() + this->verticesList.size()) * sizeof(GLfloat)));
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->glIndexBufferID);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

最佳答案

您可能没有为 OpenGL-1.1 之后引入的功能初始化入口点。在 Windows 中,只指定了 OpenGL-1.1 入口点,超出的一切都必须在运行时初始化。否则你会遇到访问冲突。查看 GLEW,了解仅需 3 行代码即可轻松完成这项繁琐任务的方法。

话虽如此,您的代码仍然有错误:

您需要告诉 OpenGL 它应该从缓冲区的确切位置获取它的元素。这是通过调用 gl…Pointer 函数完成的,数据元素是缓冲区中的字节偏移量。您的代码遗漏了这一点。此外,glDrawElements 不再期望指针也放置一个偏移量。

但是随着缓冲区对象被绑定(bind),这不应该导致段错误/访问冲突。

关于c++ - 调用 glDrawElements 导致访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18803921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com