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

C++ atioglxx.pdb not loaded error glBufferData OpenGL

我在尝试将 OBJ 文件加载到我的项目中时不断收到此错误

atioglxx.pdb 未加载

有以下例外情况

在 Reality.exe 中的 0x53A083FF (atioglxx.dll) 抛出异常:0xC0000005:读取位置 0x0894F000 的访问冲突。

有时我会收到此错误,有时我不会收到并在屏幕上显示我的模型。因此,我尝试调试代码,发现 glBufferData 函数是导致此错误的原因,但无法弄清楚它的问题所在。

这里是OBJ Loaded函数

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 class

的构造函数
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

备注 这是我关于 Whosebug 的第一个问题,所以请放轻松。

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

中的float个元素的个数

您将 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);