glDrawArrays()给了我一个例外

时间:2018-04-06 17:36:46

标签: c++ exception opengl game-engine

我在 glDrawArrays()获得此信息:

  

“Test.exe中的0x69A3E360(nvoglv32.dll)抛出异常:   0xC0000005:访问冲突读取位置0x00000000。 “

我对现代 OpenGL 有点新鲜,当我尝试制作模型类时,我遇到了这个问题。我不认为它与opengl的版本

有关
OpenGL Version: 4.6.0 NVIDIA 388.13
GLEW Version: 2.1.0

raw_model.cpp

#include "Headers/raw_model.h"

RawModel::RawModel(unsigned int vertexCount, unsigned int vaoID)
{
    this->vaoID = vaoID;
    this->vertexCount = vertexCount;
}

int RawModel::getVaoID() const {

    return vaoID;
}

int RawModel::getVertexCount() const {

        return vertexCount;
}

RawModel::RawModel(){}

renderer.cpp

void Renderer::clear()
{
    glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
}

void Renderer::render(RawModel model)
{
     glBindVertexArray(model.getVaoID());
     glEnableVertexAttribArray(0);

     glDrawArrays(GL_TRIANGLES, 0, model.getVertexCount()); // Error
     glDisableVertexAttribArray(0);
     glBindVertexArray(0);
}

loader.cpp

#include "Headers/loader.h"

GLuint Loader::createVao()
{   
    GLuint vaoID;
    glGenVertexArrays(1, &vaoID);

    vaos.push_back(vaoID);
    glBindVertexArray(vaoID);

    return vaoID;
}

void Loader::storeInAttributeList(int attribNum, float data[])
{
    GLuint vboID;
    glGenBuffers(1, &vboID);

    vbos.push_back(vboID);

    glBindBuffer(GL_ARRAY_BUFFER, vboID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
    glVertexAttribPointer(attribNum, 3, GL_FLOAT, false, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

}

void Loader::unbindVao()
{
    glBindVertexArray(0);
}

RawModel Loader::loadToVao(float vertices[])
{
    GLuint vaoID = createVao();
    storeInAttributeList(0, vertices);
    unbindVao();

    return RawModel(vaoID, (sizeof(vertices[0])) / 3);
}

void Loader::cleanUp()
{
   for (auto vao : vaos) {

        glDeleteVertexArrays(sizeof(vao), &vao);

    }

    for (auto vbo : vbos) {

         glDeleteBuffers(sizeof(vbo), &vbo);

    }
}

0 个答案:

没有答案