简体   繁体   中英

calling glDrawElements causes access violation

Im currently trying to figure out how VBO stuff is working which results in horrible problems sadly : /

im loading the data from an .obj file (see: http://pastebin.com/B8uibDvV for the .obj file) and (trying to) put it into a VBO

my code to bind the data looks like this:

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

at the end im rendering everything with this:

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

and at the end i get the access violation message in the output of VisualStudio 2010

0xC0000005: Access violation reading location 0x0147fbd8.

So my question is how to fix this? (or how to do it right if im that wrong)

---------------EDIT 1---------------

http://pastebin.com/DEbZH9iy

Bind data

 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;

Render part:

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

You probably didn't initialize the entry points for functionality introduced after OpenGL-1.1. In Windows only the OpenGL-1.1 entry points are specified, everything beyond must be initialized at runtime. Otherwise you get an access violation. Have a look at GLEW for a convenient way to do that tedious task with just 3 lines of code.

That being said, your code still got mistakes:

You need to tell OpenGL where where from the buffer exactly it shall take it's elements. This done by calling the gl…Pointer functions with the data elements being a byte offset into the buffer. Your code misses that. Also glDrawElements no longer expects are pointer put an offset as well.

But with buffer objects being bound this shouldn't cause a segfault/access violation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM