3

我目前正试图弄清楚 VBO 的东西是如何工作的,这会导致可怕的问题:/

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

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

//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

绑定数据

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

1 回答 1

2

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

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

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

但是绑定缓冲区对象时,这不应该导致段错误/访问冲突。

于 2013-09-14T16:55:01.747 回答