I tried rendering a cube using a vertex array on a VBO, with position and color, and an index to especify, but when I try to draw it shows nothing on the screen.
This is my data:
Vertex VertexData[8];
GLfloat CubeVertexData[] =
{
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0 };
GLubyte CubeColors[] = {
// Front
0, 255, 0, 255,
255, 255, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
// Back
255, 0, 0, 255,
255, 255, 0, 255,
0, 255, 0, 255,
0, 255, 0, 255
};
GLushort CubeElementData[] = {
// front
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1
};
So I'm using my own Vertex struct to store the positions and colors together.
glGenBuffers(1, &myVboID);
glBindBuffer(GL_ARRAY_BUFFER, myVboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData), &VertexData[0], GL_STATIC_DRAW);
glGenBuffers(1, &vertElemID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeElementData), &CubeElementData[0], GL_STATIC_DRAW);
`
And when I try to draw, it just shows nothing on screem. I'm really confused, because I have another similar program that does exactly the same, but uses glut, and it works fine.
//Bind vbo and indices
glBindBuffer(GL_ARRAY_BUFFER, myVboID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_INT, GL_TRUE, sizeof(Vertex), (void*) offsetof(Vertex, color));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
This is how I created the VBO for each array when it worked
// Vertex data
glGenBuffers(1, &vertBuffID);
glBindBuffer(GL_ARRAY_BUFFER, vertBuffID);
glBufferData(GL_ARRAY_BUFFER, sizeof(CubeVertexData), CubeVertexData, GL_STATIC_DRAW);
// Color data
glGenBuffers(1, &vertColorID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertColorID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeColors), CubeColors, GL_STATIC_DRAW);
// Index array of vertex / color data
glGenBuffers(1, &vertElemID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeElementData), &CubeElementData[0], GL_STATIC_DRAW);
And this is how I draw, this works perfectly fine, but I just can't get it to work with the vertex array for some reason.
glBindBuffer(GL_ARRAY_BUFFER, vertBuffID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertColorID);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);