C++atioglxx.pdb未加载错误glBufferData OpenGL

C++ atioglxx.pdb not loaded error glBufferData OpenGL

本文关键字:glBufferData OpenGL 错误 加载 pdb C++atioglxx      更新时间:2023-10-16

当我试图将OBJ文件加载到我的项目中时,我一直收到这个错误

未加载atioglxx.pdb

出现以下异常

Reality.exe中0x53A083FF(atioglxx.dll(处引发异常:0xC0000005:读取位置0x0894F000的访问冲突。

有时我会得到这个错误,有时我没有,并在屏幕上显示我的模型。因此,我尝试调试代码,发现glBufferData函数是导致此错误的原因,但无法找出它的问题所在。

此处为OBJ加载功能

bool Mesh::LoadOBJ(std::string objFile) 
{
std::vector<glm::vec3> position;
std::vector<glm::vec2> UVs;
std::vector<glm::vec3> normals;
std::vector< float > vertices;
std::vector<unsigned int> indices;
std::unordered_map< std::string, unsigned int> isProcessed;
std::ifstream myFile;
myFile.open(objFile);
if (!myFile.is_open())
{
std::cout << "Error Openening OBJ file : " << objFile;
return false; 
}
unsigned int cnt = 1;
while (!myFile.eof())
{
std::string type;
myFile >> type;
float x, y, z;
if (type == "v") {
myFile >> x >> y >> z;
glm::vec3 v(x, y, z); 
position.push_back(v);
}
else if (type == "vt") {
myFile >> x >> y;
glm::vec2 v(x, y);
UVs.push_back(v);
}
else if (type == "vn") {
myFile >> x >> y >> z;
glm::vec3 v(x, y, z);
normals.push_back(v);
}
else if (type == "f") {
std::string p1, p2, p3;
std::vector<std::string> vertex(3);
myFile >> p1;
if (!isProcessed[p1]) {
isProcessed[p1] = cnt;
indices.push_back(cnt - 1);
vertex[0] = "";
vertex[1] = "";
vertex[2] = "";
int c = 0;
for (int i = 0; i < p1.size(); ++i) {
if (p1[i] == '/') {
++c;
continue;
}
vertex[c] += p1[i]; 
}
if (vertex[0].size() > 0) {
int vertexIndex = std::stoi(vertex[0]);
--vertexIndex;
vertices.push_back(position[vertexIndex].x);
vertices.push_back(position[vertexIndex].y);
vertices.push_back(position[vertexIndex].z);
}
if (vertex[1].size() > 0) {
int UVsIndex = std::stoi(vertex[1]);
--UVsIndex;
vertices.push_back(UVs[UVsIndex].x);
vertices.push_back(UVs[UVsIndex].y);
}
if (vertex[2].size() > 0) {
int normalIndex = std::stoi(vertex[2]);
--normalIndex;
vertices.push_back(normals[normalIndex].x);
vertices.push_back(normals[normalIndex].y);
vertices.push_back(normals[normalIndex].z);
}
++cnt;
}
else {
indices.push_back(isProcessed[p1] - 1);
}
myFile >> p2;
if (!isProcessed[p2]) {
isProcessed[p2] = cnt;
indices.push_back(cnt - 1);
vertex[0] = "";
vertex[1] = "";
vertex[2] = "";
int c = 0;
for (int i = 0; i < p2.size(); ++i) {
if (p2[i] == '/') {
++c;
continue;
}
vertex[c] += p2[i];
}
if (vertex[0].size() > 0) {
int vertexIndex = std::stoi(vertex[0]);
--vertexIndex;
vertices.push_back(position[vertexIndex].x);
vertices.push_back(position[vertexIndex].y);
vertices.push_back(position[vertexIndex].z);
}
if (vertex[1].size() > 0) {
int UVsIndex = std::stoi(vertex[1]);
--UVsIndex;
vertices.push_back(UVs[UVsIndex].x);
vertices.push_back(UVs[UVsIndex].y);
}
if (vertex[2].size() > 0) {
int normalIndex = std::stoi(vertex[2]);
--normalIndex;
vertices.push_back(normals[normalIndex].x);
vertices.push_back(normals[normalIndex].y);
vertices.push_back(normals[normalIndex].z);
}
++cnt;
}
else {
indices.push_back(isProcessed[p2] - 1);
}
myFile >> p3;
if (!isProcessed[p3]) {
isProcessed[p3] = cnt;
indices.push_back(cnt - 1);
vertex[0] = "";
vertex[1] = "";
vertex[2] = "";
int c = 0;
for (int i = 0; i < p3.size(); ++i) {
if (p3[i] == '/') {
++c;
continue;
}
vertex[c] += p3[i];
}
if (vertex[0].size() > 0) {
int vertexIndex = std::stoi(vertex[0]);
--vertexIndex;
vertices.push_back(position[vertexIndex].x);
vertices.push_back(position[vertexIndex].y);
vertices.push_back(position[vertexIndex].z);
}
if (vertex[1].size() > 0) {
int UVsIndex = std::stoi(vertex[1]);
--UVsIndex;
vertices.push_back(UVs[UVsIndex].x);
vertices.push_back(UVs[UVsIndex].y);
}
if (vertex[2].size() > 0) {
int normalIndex = std::stoi(vertex[2]);
--normalIndex;
vertices.push_back(normals[normalIndex].x);
vertices.push_back(normals[normalIndex].y);
vertices.push_back(normals[normalIndex].z);
}
++cnt;
}
else {
indices.push_back(isProcessed[p3] - 1);
}
}
mVAO = new VertexArrayObject(vertices , vertices.size() , indices , static_cast<unsigned int>(indices.size())); 
myFile.close();
return true ; 

这是我VertexArray类的构造函数

VertexArrayObject::VertexArrayObject(std::vector<float>& vertices, int VBOsize, std::vector<unsigned int>& indecies, unsigned int EBOsize):
EBOsize(EBOsize)
{
glGenVertexArrays(1, &mVAOiD);
glBindVertexArray(mVAOiD);
glGenBuffers(1, &mVBOiD);
glBindBuffer(GL_ARRAY_BUFFER, mVBOiD);
glBufferData(GL_ARRAY_BUFFER, 8 * VBOsize * sizeof(float) , &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &mEBOiD);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBOiD);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, EBOsize * sizeof(unsigned int), &indecies[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), reinterpret_cast<void*>(sizeof(float) * 3));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), reinterpret_cast<void*>(sizeof(float) * 5));
}

这是我要渲染的模型的OBJ文件Rock.obj

注意这是我在stackoverflow上的第一个问题,所以请放心。

以字节为单位的缓冲区大小计算错误。verizes.size()不是顶点属性的数目,而是std::vectorfloat元素的数目。

vertices.size()传递给VertexArrayObject的构造函数的参数VBOsize

mVAO = new VertexArrayObject(vertices , vertices.size(), indices ,static_cast<unsigned int>(indices.size()));

在构造函数中,VBOsize乘以8:

VertexArrayObject::VertexArrayObject(std::vector<float>& vertices, int VBOsize, std::vector<unsigned int>& indecies, unsigned int EBOsize)
:EBOsize(EBOsize)
{
// [...]
glBufferData(GL_ARRAY_BUFFER, 8 * VBOsize * sizeof(float) , &vertices[0], GL_STATIC_DRAW);
// [...]

如果VBOsize是顶点的数量,则必须将vertices.size()除以8:

mVAO = new VertexArrayObject(vertices, vertices.size() , indices , static_cast<unsigned int>(indices.size()));

mVAO = new VertexArrayObject(vertices, vertices.size() / 8, indices, static_cast<unsigned int>(indices.size())); 

无论如何,我建议更改缓冲区大小的计算:

glBufferData(GL_ARRAY_BUFFER, 8 * VBOsize * sizeof(float) , &vertices[0], GL_STATIC_DRAW);

glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);