如何在OpenGL中使用向量和结构上传顶点数据?

时间:2014-10-12 01:50:07

标签: c++ opengl

我一直在尝试将顶点数据从矢量上传到VBO,以渲染纹理的彩色方块。当我运行该程序时,它会在" glDrawArrays(...)"并且我在Tutorial.exe中的... nvoglv64.dll获得了第一次机会异常:访问违规读取位置..."我不确定我做错了什么。虽然我的主要目标是在sprite batcher中使用它,但这里是简化的例子:

Game::Game()
{
    Oreg::Core::init();

    //INIT VAO
    tex = Oreg::FileSys::getTex("Feels.png");

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_VERTEX_ARRAY, vbo);

    GLuint posAtt = Oreg::DefShader::texture2D.getAtt("vert");
    GLuint texAtt = Oreg::DefShader::texture2D.getAtt("texcoords");
    GLuint colAtt = Oreg::DefShader::texture2D.getAtt("color");
    glEnableVertexAttribArray(posAtt);
    glEnableVertexAttribArray(texAtt);
    glEnableVertexAttribArray(colAtt);
    glVertexAttribPointer(posAtt, 2, GL_FLOAT, GL_FALSE, sizeof(Det::Vert), (GLvoid*)offsetof(Det::Vert, pos));
    glVertexAttribPointer(texAtt, 2, GL_FLOAT, GL_FALSE, sizeof(Det::Vert), (GLvoid*)offsetof(Det::Vert, uv));
    glVertexAttribPointer(colAtt, 4, GL_FLOAT, GL_FALSE, sizeof(Det::Vert), (GLvoid*)offsetof(Det::Vert, col));

    glBindVertexArray(0);

    std::cout<<"Size of GLfloat: "<<sizeof(GLfloat)<<"\n"; //4
    std::cout<<"Size of Vert: "<<sizeof(Det::Vert)<<"\n"; //32
    std::cout<<"Offset of Vert's pos: "<<offsetof(Det::Vert, pos)<<"\n"; //0
    std::cout<<"Offset of Vert's uv: "<<offsetof(Det::Vert, uv)<<"\n"; //8
    std::cout<<"Offset of Vert's col: "<<offsetof(Det::Vert, col)<<"\n"; //16

    //UPLOAD VERTS
    std::vector<Det::Vert> verts;

    verts.resize(6);
    verts.at(0).setPos(0, 0);
    verts.at(0).setUV(0, 0);
    verts.at(0).setCol(1, 0, 1, 1);
    verts.at(1).setPos(50, 0);
    verts.at(1).setUV(1, 0);
    verts.at(1).setCol(1, 0, 1, 1);
    verts.at(2).setPos(50, 50);
    verts.at(2).setUV(1, 1);
    verts.at(2).setCol(1, 0, 1, 1);
    verts.at(3).setPos(50, 50);
    verts.at(3).setUV(1, 1);
    verts.at(3).setCol(1, 0, 1, 1);
    verts.at(4).setPos(0, 50);
    verts.at(4).setUV(0, 1);
    verts.at(4).setCol(1, 0, 1, 1);
    verts.at(5).setPos(0, 0);
    verts.at(5).setUV(0, 0);
    verts.at(5).setCol(1, 0, 1, 1);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, verts.size()*sizeof(Det::Vert), verts.data(), GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void Game::update()
{
    //BEGIN RENDER
    Oreg::Window::clear(0.0f, 0.06f, 0.09f, 1.0f);
    Oreg::Window::renderSettings();
    Oreg::DefShader::texture2D.use();
    glm::mat4 mat = glm::ortho(0.0f, (float)Oreg::Window::getRes().x, (float)Oreg::Window::getRes().y, 0.0f, -1.0f, 1.0f);
    glUniform1i(Oreg::DefShader::texture2D.getUni("tex"), 0);
    glUniformMatrix4fv(Oreg::DefShader::texture2D.getUni("matrix"), 1, GL_FALSE, glm::value_ptr(mat));
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    //RENDER
    glBindVertexArray(vao);
    glBindTexture(GL_TEXTURE_2D, tex->id);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);

    //END RENDER
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glUseProgram(0);
    Oreg::Window::showBuffer();
}

Game::~Game(){ Oreg::Core::dest(); }

1 个答案:

答案 0 :(得分:1)

在接近开头的glBindBuffer()电话中有一个错误的参数:

glGenBuffers(1, &vbo);
glBindBuffer(GL_VERTEX_ARRAY, vbo);

GL_VERTEX_ARRAY作为第一个参数无效。它应该是这样的:

glBindBuffer(GL_ARRAY_BUFFER, vbo);

如果您的OpenGL代码无法按预期工作,则应始终致电glGetError()。在这种情况下,您会收到GL_INVALID_ENUM错误。

没有直接的迹象表明你有这个问题,但只需要注意:由于你在这个类的构造函数中进行OpenGL调用,你必须确保只实例化对象拥有当前的OpenGL上下文后,这个类。