这就是我的立方体目前的样子:
填充模式:
线路模式:
正如您所看到的,与指数相关的顶点位置几乎是分散的。我可能做错了什么?
我使用这些代码来设置缓冲区:
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);
}
};
事先,我通过遍历.obj文件中的每个关键字符来存储顶点和索引数据,如下所示:
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;
}
最后,我通过调用:
来渲染立方体 glBindVertexArray(_vao);
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
我不明白我在这里做错了什么。谁能看到错误?
P.S:我出于效率目的避免 glDrawArrays 。