OpenGl 330版内核
调用glDrawElements
时出现此错误,但仅在主循环中的第一次调用时出现!
实际上,下面的代码可以正常工作!。
我知道此错误的含义:
如果将非零缓冲区对象名称绑定到已启用的数组或元素数组,并且当前已映射缓冲区对象的数据存储,则会生成。
我想了解为什么只有在第一次调用glDrawElement
时才会“抛出”该错误!
在代码Mesh::Init()
中,初始化缓冲区并Mesh::Draw()
调用glDrawElement
void Mesh::init() {
size_t vtx = vertices.size() * sizeof(vec4);
size_t vtx_col = verticesColor.size() * sizeof(vec4);
size_t nrm = normals.size() * sizeof(vec3);
size_t tng = tangent.size() * sizeof(vec3);
size_t btng = bitangent.size() * sizeof(vec3);
size_t txc = text_coord.size() * sizeof(vec2);
size_t total = vtx + vtx_col + nrm + tng + btng + txc;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (indices.size() * sizeof(unsigned int)), &indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, total, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0 , vtx, &vertices[0]);
glBufferSubData(GL_ARRAY_BUFFER, vtx , nrm, &normals[0]);
glBufferSubData(GL_ARRAY_BUFFER, (vtx + nrm) , txc, &text_coord[0]);
glBufferSubData(GL_ARRAY_BUFFER, (vtx + nrm + txc) , tng, &tangent[0]);
glBufferSubData(GL_ARRAY_BUFFER, (vtx + nrm + txc + tng) , btng, &bitangent[0]);
glBufferSubData(GL_ARRAY_BUFFER, (vtx + nrm + txc + tng + btng), vtx_col, &verticesColor[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(loc.vertex, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (0));
glVertexAttribPointer(loc.normals, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (vtx));
glVertexAttribPointer(loc.texCoord, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (vtx + nrm));
glEnableVertexAttribArray(loc.vertex);
glEnableVertexAttribArray(loc.normals);
glEnableVertexAttribArray(loc.texCoord);
if(flag_TngBtg) {
glVertexAttribPointer(loc.tangent, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (vtx + nrm + txc));
glVertexAttribPointer(loc.bitangent, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (vtx + nrm + txc + tng));
glEnableVertexAttribArray(loc.tangent);
glEnableVertexAttribArray(loc.bitangent);
}
if(flag_VtxColor) {
glVertexAttribPointer(loc.vertex_color, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) (vtx + nrm + txc + tng + btng));
glEnableVertexAttribArray(loc.vertex_color);
}
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Mesh::draw() {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, (const GLvoid*)0);
glBindVertexArray(0);
}
答案 0 :(得分:0)
我现在以这种方式对这个问题感到困惑。
当我的程序使用Assimp加载模型3D文件时,在初始化Mesh类Mesh :: init()之前,首先要加载纹理
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
.......
在加载和绑定纹理结束时放置:
glBindTexture(GL_TEXTURE_2D, 0);
这样,glDrawElements不会“抛出”任何错误。