gpt4 book ai didi

c++ - glDrawElements 失败并出现错误 GL_INVALID_OPERATION

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:16 25 4
gpt4 key购买 nike

我的目标是让实例化渲染工作,但即使是单个 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;

最佳答案

对于 GL 核心上下文,您不能为 glDrawElementsglDrawElementsInstancedindices 参数传递客户端数组。在这两种情况下,您都需要创建一个索引缓冲区并将索引存储在该缓冲区中。然后,绘图调用的 indices 参数变为绑定(bind)索引缓冲区中的偏移量(以字节为单位),从中读取索引。

但是,鉴于您的索引数组只是 0 - 35,为什么不使用 glDrawArraysglDrawArraysInstanced 呢?

关于c++ - glDrawElements 失败并出现错误 GL_INVALID_OPERATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21619917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com