Wednesday, January 27, 2016

opengl - glDrawElements Crash


This is the "init" code of the Renderer class:


glGenBuffers(1,&_idVBO);
glGenBuffers(1,&_idEBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_idEBO);
glBindBuffer(GL_VERTEX_ARRAY,_idVBO);

GLint lUV = glGetAttribLocation(_SID,"inUV");

GLint lPosition = glGetAttribLocation(_SID,"inPosition");

glEnableVertexAttribArray(lPosition);
glEnableVertexAttribArray(lUV);
glVertexAttribPointer(lUV, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer(lPosition, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));

...and this is the "flush" function:


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,_idEBO);
glBindBuffer(GL_VERTEX_ARRAY,_idVBO);

glBufferData(GL_ARRAY_BUFFER,_lVBO*sizeof(GLfloat),_pOriginVBO,GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,_lEBO*sizeof(GLuint),_pOriginEBO,GL_STATIC_DRAW);

glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);

The glDrawElements call crashes with error code c0000005, which is caused by memory acces violation i.e. bad pointers. But I do not understand where the problem is. Any ideas? Thanks in advance...



Answer



One (or more) of your gl calls previous to glDrawElements is not being passed correct information. So when you finally get to glDrawElements it crashes on you because that information is not set properly. Here is what I do to debug that issue:


After each gl call place a ChecKGLError(); Here is that function:


void _CheckGLError(const char* file, int line);


#define CheckGLError() _CheckGLError(__FILE__, __LINE__)

void _CheckGLError(const char* file, int line)
{
GLenum err ( glGetError() );

while ( err != GL_NO_ERROR )
{
std::string error;

switch ( err )
{
case GL_INVALID_OPERATION: error="INVALID_OPERATION"; break;
case GL_INVALID_ENUM: error="INVALID_ENUM"; break;
case GL_INVALID_VALUE: error="INVALID_VALUE"; break;
case GL_OUT_OF_MEMORY: error="OUT_OF_MEMORY"; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: error="INVALID_FRAMEBUFFER_OPERATION"; break;
}
std::cout << "GL_" << error.c_str() <<" - " << file << ":" << line << std::endl;
err = glGetError();

}

return;
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...