我正在尝试使用 vao 和 vbo 制作网格类。
首先,我创建向量来保存要缓冲的数据:
std::vector<GLfloat> vertices;
std::vector<GLfloat> normals;
std::vector<GLfloat> texCoords;
std::vector<GLushort> indices;
我从文件中存储数据,然后创建缓冲区:
// Vertex array
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Vertex buffer
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Index buffer
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
我尝试像这样填充缓冲区:
// Reserve space on the GPU (3 GLfloats per vertex, 3 per normal and 2 per texCoord)
glBufferData(GL_ARRAY_BUFFER, vertices.size() * 8 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
// Load data to the GPU
// Insert vertices at the beginning of the buffer
glBufferSubData(GL_ARRAY_BUFFER, NULL, vertices.size()*3*sizeof(GLfloat), &vertices);
// Insert normals behind vertices
glBufferSubData(GL_ARRAY_BUFFER, vertices.size()*3*sizeof(GLfloat), normals.size()*3*sizeof(GLfloat), &normals);
// Insert texCoords behind normals
glBufferSubData(GL_ARRAY_BUFFER, (vertices.size() + normals.size())*3*sizeof(GLfloat), texCoords.size()*2*sizeof(GLfloat), &texCoords);
// Load the index buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*3*sizeof(GLushort), &indices, GL_STATIC_DRAW);
当我尝试缓冲正常数据时它会中断:
glBufferSubData(GL_ARRAY_BUFFER, vertices.size()*3*sizeof(GLfloat), normals.size()*3*sizeof(GLfloat), &normals);
我收到此错误:
Unhandled exception at 0x699CEF38 (nvoglv32.dll) in Engine.exe: 0xC0000005: Access violation reading location 0x05C34000.
我一直在寻找一段时间,但我仍然没有找到解决方案。非常欢迎有关我所犯任何错误的任何信息。
提前谢谢我