This is what my cube looks like at the moment:
Fill mode:
Line mode:
As you can see the vertex positions in relation to the indices are almost scattered. What could I possibly be doing wrong?
I use these this code for setting up the buffers:
struct Mesh
{
CBuint v_vbo, vn_vbo, vt_vbo;
CBuint ebo;
Mesh() {}
Mesh(std::vector <CBuint>& i, std::vector <vec3>& positions, std::vector <vec3>& normals, std::vector <vec2>& tex_coords)
{
// Generate
glGenBuffers(1, &v_vbo);
glGenBuffers(1, &vn_vbo);
glGenBuffers(1, &vt_vbo);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, v_vbo);
glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(vec3), &positions[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vn_vbo);
glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(vec3), &normals[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(vec2), &tex_coords[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, i.size() * sizeof(CBuint), &i[0], GL_STATIC_DRAW);
}
~Mesh()
{
glDeleteBuffers(1, &v_vbo);
glDeleteBuffers(1, &vn_vbo);
glDeleteBuffers(1, &vt_vbo);
glDeleteBuffers(1, &ebo);
}
};
Before-hand, I store the vertex and index data by iterating through each key character in the .obj file like so:
case 'f':
CBuint v_i[3];
CBuint vn_i[3];
CBuint vt_i[3];
sscanf_s((&line[i])[0].c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v_i[0], &vt_i[0], &vn_i[0], &v_i[1], &vt_i[1], &vn_i[1], &v_i[2], &vt_i[2], &vn_i[2]);
// Faces
_f.push_back(Face(v_i[0], vt_i[0], vn_i[0], v_i[1], vt_i[1], vn_i[1], v_i[2], vt_i[2], vn_i[2]));
// Indices
_indices.push_back(v_i[0] - 1);
_indices.push_back(vt_i[0] - 1);
_indices.push_back(vn_i[0] - 1);
_indices.push_back(v_i[1] - 1);
_indices.push_back(vt_i[1] - 1);
_indices.push_back(vn_i[1] - 1);
_indices.push_back(v_i[2] - 1);
_indices.push_back(vt_i[2] - 1);
_indices.push_back(vn_i[2] - 1);
break;
}
Then finally, I render the cube by calling:
glBindVertexArray(_vao);
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
I don't understand what I'm doing wrong here. Can anyone see an error?
P.S: I'm avoiding glDrawArrays for efficiency purposes.