I am trying to get a grip on how to use VAOs in a good way.
I understand the basics and have some simple test code that works alright, but I don’t understand how one is supposed to use VAOs with shaders.
Some working pseudo-code for what I am doing, but want to improve on:
// Setup
glGenVertexArrays(1, &gVAO);
glBindVertexArray(gVAO);
sizeInBytes = sizeof(float)*3*NUM_VERTICES;
glGenBuffers(1, &gVBO);
glBindBuffer(GL_ARRAY_BUFFER, gVBO);
glBufferData(GL_ARRAY_BUFFER, sizeInBytes, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeInBytes, vertices);
sizeInBytes = sizeof(unsigned int)*3*NUM_INDICES;
glGenBuffers(1, &gEBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeInBytes,NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeInBytes, indices);
// Draw
GLuint positionLoc = glGetAttribLocation(gShader, "inPosition");
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(positionLoc);
GLuint projMatrixLoc = glGetUniformLocation(gShader, "inProjectionMatrix");
glUniformMatrix4fv(projMatrixLoc, 1, GL_FALSE, gProjectionMatrixf);
GLuint mvMatrixLoc = glGetUniformLocation(gShader, "inModelViewMatrix");
glUniformMatrix4fv(mvMatrixLoc, 1, GL_FALSE, gModelViewMatrixf);
glUseProgram(gShader);
glBindVertexArray(gVAO);
glDrawElements(GL_TRIANGLES, NUM_INDICES*3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
I want to put the attrib bindings within the VAO. (which I thought was one of the main reasons for using it in the first place)
If I use deprecated glVertexPointer code this works fine but obviously, in this example, I want to try out how to work with “pure” opengl 3.
Since I have no idea on which shader to use at the setup point (it might change dynamically after all) I cannot use
glGetAttribLocation(gShader, "inPosition");
The only thing I can think of is me inventing a standard for myself always using location 0 for position, 1 for normals etc. but that seems to only recreate what opengl had as a standard before it got deprecated.
Am I approaching this the wrong way and can anyone with some insight clarify the situation?