glDrawElements失败,错误GL_INVALID_OPERATION

时间:2014-02-07 05:18:33

标签: c++ macos opengl opengl-3

我的目标是让实例渲染工作,但即使是单个glDrawElements也会失败。注意:此代码已在Windows上运行。但是在OS X上,它失败了GL_INVALID_OPERATION

基本上我将所有静态数据加载到缓冲区中,然后最后一个缓冲区包含我在每次绘制之前重新加载的动态数据。然后我调用glDrawElementsInstanced(或用于调试glDrawElements),它会立即失败。我知道,因为之前和之后的打印有错误,并且它总是打印出一个OpenGL错误。 (即使使用glDrawElements)如果我使用glDrawArrays而不是

,也不会出现此错误

请参阅代码中的注释以获取更多信息。非常感谢任何帮助。

//Setup code, at this point vertices,textureCoordiantes,normals are all populated

//Allocate the space for the gpu buffers now
//and send the static data
//Rebind the array to bring them into the current context
glBindVertexArray ( vertexArray );

//Push voxel to gpu
glBindBuffer ( GL_ARRAY_BUFFER, vertexBuffer );
glBufferData ( GL_ARRAY_BUFFER, 36*sizeof(vec3), vertices, GL_STATIC_READ );
glEnableVertexAttribArray ( shader->AttributeVertex() );
glVertexAttribPointer ( shader->AttributeVertex(), 3, GL_FLOAT, GL_FALSE, 0, 0 );

glBindBuffer ( GL_ARRAY_BUFFER, textureBuffer );
glBufferData ( GL_ARRAY_BUFFER, 36*sizeof(vec2), textureCoordinates, GL_STATIC_READ );
glEnableVertexAttribArray ( shader->AttributeTexture() );
glVertexAttribPointer ( shader->AttributeTexture(), 2, GL_FLOAT, GL_FALSE, 0, 0 );

glBindBuffer ( GL_ARRAY_BUFFER, normalBuffer );
glBufferData ( GL_ARRAY_BUFFER, 36*sizeof(vec3), normals, GL_STATIC_READ );
glEnableVertexAttribArray ( shader->AttributeNormal() );
glVertexAttribPointer ( shader->AttributeNormal(), 3, GL_FLOAT, GL_FALSE, 0, 0 );
//Allocate space for positions
glBindBuffer ( GL_ARRAY_BUFFER, positionBuffer );
glBufferData ( GL_ARRAY_BUFFER, INSTANCE_RENDER_SWEEP*sizeof(vec4), positions, GL_DYNAMIC_READ );
glEnableVertexAttribArray ( shader->AttributePosition() );
glVertexAttribPointer ( shader->AttributePosition(), 4, GL_FLOAT, GL_FALSE, 0, 0 ); 


    //This code runs a bit later, but runs over and over:
    //indices is a vector<GLuint> of length 36 and is just 0-35

glBindVertexArray ( vertexArray );
glBindBuffer ( GL_ARRAY_BUFFER, positionBuffer );
glBufferSubData ( GL_ARRAY_BUFFER, 0,INSTANCE_RENDER_SWEEP*sizeof(vec4), positions );
glEnableVertexAttribArray ( shader->AttributePosition() );
glVertexAttribPointer ( shader->AttributePosition(), 4, GL_FLOAT, GL_FALSE, 0, 0 ); 

//The position is per-instance
//everything else is per-vertex
glVertexAttribDivisor(shader->AttributeNormal(),0);
glVertexAttribDivisor(shader->AttributePosition(),1);
glVertexAttribDivisor(shader->AttributeTexture(),0);
glVertexAttribDivisor(shader->AttributeVertex(),0);

cout << "1Err: " << glGetError() << "\n";
    glDrawDelements(GL_TRIANGLES,36,GL_UNSIGNED_BYTE,&indices[0]);
//glDrawElementsInstanced(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, &indices[0], bufferedVoxels);
    //This next error prints out 1282 which is GL_INVALID_OPERATION
    //However if i replace the above with glDrawArrays, it works for one instance (no error)
cout << "2Err: " << glGetError() << "\n";
//All buffered voxels now drawn
bufferedVoxels = 0;

2 个答案:

答案 0 :(得分:1)

使用GL核心上下文,您无法为indicesglDrawElements的{​​{1}}参数传递客户端数组。在这两种情况下,您都需要创建索引缓冲区并将索引存储在此缓冲区中。然后,绘制调用的glDrawElementsInstanced参数成为绑定索引缓冲区的偏移量(以字节为单位),从中读取索引。

但是,看到你的索引数组只是0 - 35,为什么不使用indicesglDrawArrays

答案 1 :(得分:0)

我对openGL没有太多经验,但你是否正在使用glew?如果是,您是否尝试在glewExperimental = GL_TRUE之前设置glewInit()