glVertexAttribPointer(Offset?)给出了GL_INVALID_OPERATION

时间:2014-02-11 20:54:47

标签: c++ opengl void-pointers

我的代码有问题:( OpenGL 4.1核心上下文,我正在使用VertexArrayObjects)

//This is a member method, vertexArrayObject and vertexBufferObject are of course generated using glGenVertexArrays & glGenBuffers
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 8, 0);
Draw::debugLogGLError(); // OpenGL Error #0
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (void*)(sizeof(float) * 3));
Draw::debugLogGLError(); // OpenGL Error #1282
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (void*)(sizeof(float) * 6));
Draw::debugLogGLError(); // OpenGL Error #1282
glBindVertexArray(0);

Draw :: debugLogGLError()只是将最新的错误代码输出到控制台

void Draw::debugLogGLError(void){
     std::cout << "OpenGL Error #" << glGetError() << std::endl;
}

如果我删除偏移量(void*)(sizeof(float) * 3),它就会运行得很好。 所以我假设(void*)有问题。然而,在我开始在课堂上实现它之前,它工作得很好。 我可能错过了一些代码

OS:Mavericks,HD5000,Xcode中的Clang编译器

1 个答案:

答案 0 :(得分:1)

根据http://www.opengl.org/wiki/GLAPI/glVertexAttribPointer

GL_INVALID_OPERATION​ is generated if size​ is GL_BGRA​ and type​ is not GL_UNSIGNED_BYTE​, GL_INT_2_10_10_10_REV​ or GL_UNSIGNED_INT_2_10_10_10_REV​.
GL_INVALID_OPERATION​ is generated if type​ is GL_INT_2_10_10_10_REV​ or GL_UNSIGNED_INT_2_10_10_10_REV​ and size​ is not 4 or GL_BGRA​.
GL_INVALID_OPERATION​ is generated if type​ is GL_UNSIGNED_INT_10F_11F_11F_REV​ and size​ is not 3.
GL_INVALID_OPERATION​ is generated by glVertexAttribPointer if size​ is GL_BGRA​ and noramlized​ is GL_FALSE​.
GL_INVALID_OPERATION​ is generated if zero is bound to the GL_ARRAY_BUFFER​ buffer object binding point and the pointer​ argument is not NULL​.
GL_INVALID_OPERATION​ is generated if no vertex array object is bound.

前4个显然不是这样,所以它必须是最后的2个之一。最后一个不可能是这种情况,因为它并不总是抛出错误,最后一个会。所以根据维基的问题是:

GL_INVALID_OPERATION​ is generated if zero is bound to the GL_ARRAY_BUFFER​ buffer object binding point and the pointer​ argument is not NULL​.

这意味着代码中的vertexBufferObject为0,因此不是有效的缓冲区对象。如果不是这种情况,我认为你偶然发现了一个主要的驱动程序错误。