我尝试多次重写下面的代码,但是失败了,我知道我必须使用像glCreateBuffers,glVertexArrayElementBuffer,glVertexArrayVertexBuffer,glnamedBufferData,glBindVertexArray这样的函数,但是我有一个问题,有替换glBindBuffer,glVertexAttribPointer,glEnableVertexAttribArray和glGetAttribLocation的部分。
这是我尝试实现的以下代码:
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);
你不“替换”glBindBuffer
;你根本就不使用它。这就像DSA的重点:不必绑定对象只是为了修改它们。
glGetAttribLocation
已经是一个DSA函数(第一个参数是它作用的对象),所以你继续使用它(或者更好的是,停止询问着色器和assign position
a specific location within the shader)。
glVertexArray*
函数都操纵VAO,附加缓冲区和定义顶点格式。你遇到的“问题”是你已经习惯了可怕的glVertexAttribPointer
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);