PDA

View Full Version : Can't display triangles in vectors



j_sb22
09-18-2015, 02:45 PM
Hello,

EDIT : I mean I can't display triangles that are stored in a vector

I'm learning OpenGL and i want load an .obj then display it. I load it well and store it in vectors but I can't display it. I'm on this problem for 4 days now I'm very bored of it.

For testing I put the coordinates of 2 triangles in 2 vectors



std::vector<GLfloat> vertices_vector;
std::vector<GLuint> faces_vector;


vertices_vector.push_back(0.5f);
vertices_vector.push_back(0.5f);
vertices_vector.push_back(0.0f);

vertices_vector.push_back(0.5f);
vertices_vector.push_back(-0.5f);
vertices_vector.push_back(0.0f);

vertices_vector.push_back(-0.5f);
vertices_vector.push_back(-0.5f);
vertices_vector.push_back(0.0f);

vertices_vector.push_back(-0.5f);
vertices_vector.push_back(0.5f);
vertices_vector.push_back(0.0f);

faces_vector.push_back(0);
faces_vector.push_back(1);
faces_vector.push_back(3);

faces_vector.push_back(1);
faces_vector.push_back(2);
faces_vector.push_back(3);


Where faces are the indices.

Then I set VBO, VAO and EBO



GLuint VBO, VAO, EBO;

glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices_vector.size(), &vertices_vector[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint) * faces_vector.size(), &faces_vector[0], GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindVertexArray(0);

In the loop of GLFW, the glDrawElements is like that



glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, faces_vector.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);


Of course i have a fragment and vertex shader. They work well because when i use an array instead of vectors and replace the right sizes the functions, they are displayed well.

It only fails when i use vectors.

So..can you help me please ?

Thanks a lot

GClements
09-18-2015, 03:03 PM
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices_vector.size(), &vertices_vector[0], GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint) * faces_vector.size(), &faces_vector[0], GL_STATIC_DRAW);


You're uploading to VBO twice and to EBO never.

j_sb22
09-18-2015, 03:51 PM
You saved my life man... love you thank you !