我多次尝试重写以下代码,但是失败了,我知道我必须使用glCreateBuffers,glVertexArrayElementBuffer,glVertexArrayVertexBuffer,glnamedBufferData,glBindVertexArray之类的函数,但是替换部分glBindBuffer,glVertexAttribPointer,glEnableVertexAttribArray和glGetAttribLocation时遇到问题。 / p>
这是我尝试实现的以下代码:
glCreateBuffers(1, &phong.vbo);
glNamedBufferData(phong.vbo,sizeof(modelVertices),modelVertices,GL_STATIC_DRAW);
glCreateBuffers(1, &phong.ebo); glNamedBufferData(phong.ebo,sizeof(modelIndices),modelIndices,GL_STATIC_DRAW);
glCreateVertexArrays(1, &phong.vao);
glBindVertexArray(phong.vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, phong.ebo);
glBindBuffer(GL_ARRAY_BUFFER, phong.vbo);
const GLint possitionAttribute = glGetAttribLocation(phong.program, "position");
glVertexAttribPointer((GLuint) possitionAttribute, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 6, (const GLvoid *) 0 );
glEnableVertexAttribArray((GLuint) possitionAttribute);
答案 0 :(得分:2)
您不会“替换” glBindBuffer
;您根本不使用它。就像DSA的全部要点一样:不必绑定对象只是为了修改。
glGetAttribLocation
已经是DSA函数(第一个参数是它所作用的对象),因此您可以继续使用它(或者更好的是,不再询问着色器和assign position
a specific location within the shader)。
glVertexArray*
函数都可操纵VAO,附加缓冲区并定义顶点格式。您遇到的“问题”是,您已经习惯了可怕的glVertexAttribPointer
API,该API已被with a much better API in 4.3取代。而且DSA没有提供糟糕的旧API的DSA版本。因此,您需要以DSA形式使用单独的属性格式API。
与您要执行的操作等效:
glCreateVertexArrays(1, &phong.vao);
//Setup vertex format.
auto positionAttribute = glGetAttribLocation(phong.program, "position");
glEnableVertexArrayAttrib(phong.vao, positionAttribute);
glVertexArrayAttribFormat(phong.vao, positionAttribute, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(phong.vao, positionAttribute, 0);
//Attach a buffer to be read from.
//0 is the *binding index*, not the attribute index.
glVertexArrayVertexBuffer(phong.vao, 0, phong.vbo, 0, sizeof(GLfloat) * 6);
//Index buffer
glVertexArrayElementBuffer(phong.vao, phong.ebo);