2

I tried to rewrite following code many times, but failed, I know I have to use functions like glCreateBuffers, glVertexArrayElementBuffer, glVertexArrayVertexBuffer, glnamedBufferData, glBindVertexArray but I have a problem with section with replacing glBindBuffer, glVertexAttribPointer, glEnableVertexAttribArray and glGetAttribLocation.

This is the following code, which I tried to implement:

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);
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Afendas
  • 31
  • 2

1 Answers1

3

You don't "replace" glBindBuffer; you simply don't use it. That's like the whole point of DSA: to not have to bind objects just to modify them.

glGetAttribLocation is already a DSA function (the first parameter is the object it acts on), so you continue to use it (or better yet, stop asking the shader and assign position a specific location within the shader).

The glVertexArray* functions all manipulate the VAO, attaching buffers and defining vertex formats. The "problem" you're having is that you're used to the terrible glVertexAttribPointer API, which was replaced with a much better API in 4.3. And DSA doesn't provide a DSA version of the terrible old API. So you need to use the separate attribute format API, in DSA form.

The equivalent to what you're trying to do would be:

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);
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982