我目前正试图弄清楚 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---------------
绑定数据
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);