mercredi 18 mars 2015

Why does my VBO not work with a vertex array? Works fine without


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.


My Vertex struct and how I fill it:



struct Position {
GLfloat x;
GLfloat y;
GLfloat z;
};

struct Color {
GLuint r; // 0 a 255
GLuint g;
GLuint b;
GLuint a;
};

struct Vertex {

Position position;

Color color;

};

// Store vertex data in structure Vertex3D
int i = 0, j = 0;
vertexData[0].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[0].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[1].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[1].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[2].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[2].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[3].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[3].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[4].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[4].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[5].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[5].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[6].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[6].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);
vertexData[7].setPosition(CubeVertexData[i++], CubeVertexData[i++], CubeVertexData[i++]);
vertexData[7].setColor(CubeColors[j++], CubeColors[j++], CubeColors[j++], CubeColors[j++]);

glGenBuffers(1, &myVboID);
glBindBuffer(GL_ARRAY_BUFFER, myVboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24 + sizeof(GLubyte) * 24, &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, // Mode
size / sizeof(GLushort), // Count
GL_UNSIGNED_SHORT, // Data type
0 // Offset
);

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, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

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, // Mode
size / sizeof(GLushort), // Count
GL_UNSIGNED_SHORT, // Data type
0 // Offset
);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);



Aucun commentaire:

Enregistrer un commentaire