I’ve been having a problem recently when getting to grips with Opengl VAOs.
Currently I have code which draws a triangle from a array of floats using a VBO.
Here is the code.
float vpp[] = { 0.75f, 0.75f, 0.0f,
0.75f, -0.75f, 0.0f,
-0.75f, -0.75f, 0.0f};
// Non Indexed
glGenBuffers(1, &m_mainVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_mainVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vpp), vpp, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_POINTS, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Now when I try to implement a VAO, the program only draws out 1 point. (I would post a screen cap but I can’t).
And the code for this one.
float vp[] = { 0.0f, 0.75f, -0.75f };
//VAO
unsigned short sInds[9] = { 1, 1, 0,
1, 2, 0,
2, 2, 0};
//Indexed
glGenBuffers(1, &m_mainVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_mainVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vp), vp, GL_STATIC_DRAW);
GLuint elBuf;
glGenBuffers(1, &elBuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elBuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 9 * sizeof(unsigned short), sInds, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawElements(GL_POINTS, 3, GL_UNSIGNED_SHORT, sInds);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Can anyone show me what I’m doing wrong?
Which OpenGL version do you use? So far your code uses only Vertex Buffer Objects, but there’s no Vertex Array Object support code there, yet. If you’re on OpenGL-4 core, you need to create a VAO first, otherwise nothing will draw.
If you don’t plan on using VAO abstraction, you can create a catch-all VAO right after creation of the context.
Update:
I didn’t it notice yesterday night, but Nicol Bolas did. Some part of your second code snippet doesn’t make sense.
vpdefines only 3 floats. It now completely depends on the semantics of the vertex shader (using generic vertex attributes mandates the use of shaders) how these are to be interpreted. Your call of glVertexAttribPointer with a element size of 3 tells OpenGL, that each element of that vertex attribute has 3 fields, which are addressed together.It would make more sense with your first definition
vppIf you enable several vertex attributes, they together form a long vector of several attributes.
The indices supplied to glDrawElements address such vertex attribute vectors, not their individual fields.