Constants:
// attribute index in GLSL shader const GLint a_pos = 0; const GLint a_texcoords = 1; const GLint a_normals = 2;
// vertex buffer index used in setup & draw calls const GLuint vbuf_index_pos = 0; const GLuint vbuf_index_texcoords = 1; const GLuint vbuf_index_normals = 2;
Format setup:
GLuint vao; glCreateVertexArrays(1, &vao);
glVertexArrayAttribFormat(vao, a_pos, 3, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(vao, a_pos, vbuf_index_pos); glEnableVertexArrayAttrib(vao, a_pos);
glVertexArrayAttribFormat(vao, a_texcoords, 2, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(vao, a_texcoords, vbuf_index_texcoords); glEnableVertexArrayAttrib(vao, a_texcoords);
glVertexArrayAttribFormat(vao, a_normals, 3, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribBinding(vao, a_normals, vbuf_index_normals); glEnableVertexArrayAttrib(vao, a_normals);
mesh setup:
GLuint vbuf_pos; GLuint vbuf_texcoord; GLuint vbuf_normals; GLuint ibuf;
// can create all of them with one call, just pass array of 4 glCreateBuffers(1, &vbuf_pos); glCreateBuffers(1, &vbuf_texcoord); glCreateBuffers(1, &vbuf_normals); glCreateBuffers(1, &ibuf);
glNamedBufferStorage(vbuf_pos, points.size()*sizeof(points[0]), points.data()); glNamedBufferStorage(vbuf_texcoord, texcoords.size()*sizeof(texcoords[0]), texcoords.data()); glNamedBufferStorage(vbuf_normals, normals.size()*sizeof(normals[0]), normals.data()); glNamedBufferStorage(ibuf, indexes.size()*sizeof(indexes[0]), indexes.data());
draw call:
// do it once glBindVertexArray(format.vao);
// for each mesh do: glVertexArrayVertexBuffer(format.vao, vbuf_index_pos, mesh.vbuf_pos, 0, sizeof(PointStruct)); glVertexArrayVertexBuffer(format.vao, vbuf_index_texcoords, mesh.vbuf_texcoord, 0, sizeof(TexcoordStruct)); glVertexArrayVertexBuffer(format.vao, vbuf_index_normals, mesh.vbuf_normals, 0, sizeof(NormalStruct)); glVertexArrayElementBuffer(format.vao, mesh.ibuf); glDrawElements(GL_TRIANGLES, mesh.vertex_count, GL_UNSIGNED_BYTE, NULL);
This code will require at least GL 4.5 version. Or ARB_direct_state_access extension presence).
Be aware that interleaved attributes in one buffer will be better for performance.
Ok so now you edited the previous answer and gave code, thanks. :)
I'll see if I can use OpenGL 4.5, but I think mine is 4.1.
Is there any real performance penalty or otherwise in using OpenGL 4.1 and older API?
Thanks.