Ошибка C ++ atioglxx.pdb не загружен glBufferData OpenGL - PullRequest
0 голосов
/ 12 июля 2020

Я продолжаю получать эту ошибку при попытке загрузить файл OBJ в мой проект

atioglxx.pdb не загружен

со следующими исключение

Исключение по адресу 0x53A083FF (atioglxx.dll) в Reality.exe: 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

Примечание это мой первый вопрос по stackoverflow, поэтому, пожалуйста, успокойтесь.

1 Ответ

1 голос
/ 12 июля 2020

Вычисление размера буфера в байтах неверно. verizes.size() - это не номер атрибута вершины, это количество float элементов в std::vector.

Вы передаете vertices.size() аргументу VBOsize конструктора VertexArrayObject s:

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);
...