I am trying to build my mesh by passing in the informations with a vector. I thought it would must be the same. But it is not working. The actual information in this vector must be correct so I am actual pretty sure the problem is the building the actual bufferData.
Does somebody has a hint what is going wrong there? Or how I maybe do it in a better way?
void MeshBuilder::buildMesh(std::vector<Vertex> vert, std::vector<int> index){
Vertex vertex;
//Vertex Array Object: Binds Buffer Obj
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
std::vector<GLfloat> verticies;
for (int i = 0; i < vert.size(); i++) {
verticies.push_back(vert[i].getPosition().x);
verticies.push_back(vert[i].getPosition().y);
verticies.push_back(vert[i].getPosition().z);
verticies.push_back(vert[i].getNormals().x);
verticies.push_back(vert[i].getNormals().y);
verticies.push_back(vert[i].getNormals().z);
verticies.push_back(vert[i].getTexture().x);
verticies.push_back(vert[i].getTexture().y);
}
std::vector<int> indicies;
for (int i = 0 ; i < index.size(); i++) {
indicies.push_back(i);
}
//Vertex Buffer Object
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verticies.size()*sizeof(GLfloat), &verticies, GL_STATIC_DRAW);
//Attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertex.SIZE*4, (GLvoid*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertex.SIZE*4, (GLvoid*)12);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertex.SIZE*4, (GLvoid*)24);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
//Element Buffer Object
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicies.size()*sizeof(int), &indicies, GL_STATIC_DRAW);
std::cout << "Mesh buildet..." << std::endl;
}
In your method declaration & definition try changing it from void MeshBuilder::buildMesh(std::vector<Vertex> vert, std::vector<int> index);
to void MeshBuilder::buildMesh(std::vector<Vertex>& vert, std::vector<int>& index)
to pass by reference instead of making a copy; this will help speed things up.
The only other thing I can see that may cause a problem is that you are stopping your vertex array before you define your indices. Try moving your glBindVertexArray(0);
that is before your //Element Buffer Object
code to be called after these three lines of code. See if this makes a difference.
Also make sure that you do the following before the end of this function or after this function is called:
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ARRAY_BUFFER, 0 );