nvolgv32.dll - glDrawElements麻烦

时间:2014-06-10 00:29:42

标签: c++ opengl glew

此时我使用OpenGL编写图形应用程序,并使用glDrawElements绘制obj文件。我是 Nvidia GeForce GT 740m

问题是:在demo.exe中0x68FCF460(nvoglv32.dll)的第一次机会异常:0xC0000005:访问冲突读取位置0x00000000。我不知道如何解决这个问题。< / p>

这些是我的缓冲区初始化

    // <-- VBOs IBO VAO creation --> //

    glGenBuffers(3, obj_vbo);
    glGenBuffers(1, &obj_ibo);
    glGenVertexArrays(1, &obj_vao);

    // <-- IBO --> //

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj_ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, obj_triangles.size() * sizeof(GLushort), obj_triangles.data(), GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);

    // <-- VAO --> //

    glBindVertexArray(obj_vao);

        glEnableVertexAttribArray(VERTEX_LAYOUT);
        glBindBuffer(GL_ARRAY_BUFFER, obj_vbo[VERTEX_LAYOUT]);
        glBufferData(GL_ARRAY_BUFFER, obj_vertices.size() * sizeof(GLfloat), obj_vertices.data(), GL_STATIC_DRAW);
        glVertexAttribPointer(VERTEX_LAYOUT, 3, GL_FLOAT, GL_FALSE, GL_FALSE, (GLubyte *) NULL);
        glBindBuffer(GL_ARRAY_BUFFER, NULL);
        glDisableVertexAttribArray(VERTEX_LAYOUT);

        glEnableVertexAttribArray(NORMAL_LAYOUT);
        glBindBuffer(GL_ARRAY_BUFFER, obj_vbo[NORMAL_LAYOUT]);
        glBufferData(GL_ARRAY_BUFFER, obj_normals.size() * sizeof(GLfloat), obj_normals.data(), GL_STATIC_DRAW);
        glVertexAttribPointer(NORMAL_LAYOUT, 3, GL_FLOAT, GL_FALSE, GL_FALSE, (GLubyte *) NULL);
        glBindBuffer(GL_ARRAY_BUFFER, NULL);
        glDisableVertexAttribArray(NORMAL_LAYOUT);

        glEnableVertexAttribArray(TEXTURE_LAYOUT);
        glBindBuffer(GL_ARRAY_BUFFER, obj_vbo[TEXTURE_LAYOUT]);
        glBufferData(GL_ARRAY_BUFFER, obj_textures.size() * sizeof(GLfloat), obj_textures.data(), GL_STATIC_DRAW);
        glVertexAttribPointer(TEXTURE_LAYOUT, 2, GL_FLOAT, GL_FALSE, GL_FALSE, (GLubyte *) NULL);
        glBindBuffer(GL_ARRAY_BUFFER, NULL);
        glDisableVertexAttribArray(TEXTURE_LAYOUT);

    glBindVertexArray(NULL);

    // <-- Save and calculate stuffs . . . --> //

    obj_triangles_size = (GLuint) obj_triangles.size();

这是draw方法,其中VERTEX_LAYOUT = 0,NORMAL_LAYOUT = 1,TEXTURE_LAYOUT = 2

    glBindVertexArray(obj_vao);

        glEnableVertexAttribArray(VERTEX_LAYOUT);
        glEnableVertexAttribArray(NORMAL_LAYOUT);
        glEnableVertexAttribArray(TEXTURE_LAYOUT);

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj_ibo);
            glDrawElements(GL_TRIANGLES, obj_triangles_size, GL_UNSIGNED_SHORT, NULL);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);

        glDisableVertexAttribArray(TEXTURE_LAYOUT);
        glDisableVertexAttribArray(NORMAL_LAYOUT);
        glDisableVertexAttribArray(VERTEX_LAYOUT);

    glBindVertexArray(NULL);

如果我在 nvidia面板上使用选项(AUTO-SELECT)/管理3D 设置应用程序渲染效果不错,但当我将该选项切换为高性能NVIDIA处理器时它给出了这个问题。

1 个答案:

答案 0 :(得分:3)

声明:

这不是答案,但在评论中解释这一点是不可能的。


以下是VAO的使用方法,它们实际上跟踪了每次绘制时冗余变化的大多数状态。

初始化:

// <-- VBOs IBO VAO creation --> //

glGenBuffers(3, obj_vbo);
glGenBuffers(1, &obj_ibo);
glGenVertexArrays(1, &obj_vao);

// <-- VAO --> //

glBindVertexArray(obj_vao);

    // <-- IBO (moved) --> //

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj_ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, obj_triangles.size() * sizeof(GLushort), obj_triangles.data(), GL_STATIC_DRAW);
    // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL); -- REMOVE

    glEnableVertexAttribArray(VERTEX_LAYOUT);
    glBindBuffer(GL_ARRAY_BUFFER, obj_vbo[VERTEX_LAYOUT]);
    glBufferData(GL_ARRAY_BUFFER, obj_vertices.size() * sizeof(GLfloat), obj_vertices.data(), GL_STATIC_DRAW);
    glVertexAttribPointer(VERTEX_LAYOUT, 3, GL_FLOAT, GL_FALSE, GL_FALSE, (GLubyte *) NULL);
    //glBindBuffer(GL_ARRAY_BUFFER, NULL);        -- REMOVE
    //glDisableVertexAttribArray(VERTEX_LAYOUT);  -- REMOVE

    glEnableVertexAttribArray(NORMAL_LAYOUT);
    glBindBuffer(GL_ARRAY_BUFFER, obj_vbo[NORMAL_LAYOUT]);
    glBufferData(GL_ARRAY_BUFFER, obj_normals.size() * sizeof(GLfloat), obj_normals.data(), GL_STATIC_DRAW);
    glVertexAttribPointer(NORMAL_LAYOUT, 3, GL_FLOAT, GL_FALSE, GL_FALSE, (GLubyte *) NULL);
    //glBindBuffer(GL_ARRAY_BUFFER, NULL);        -- REMOVE
    ///glDisableVertexAttribArray(NORMAL_LAYOUT); -- REMOVE

    glEnableVertexAttribArray(TEXTURE_LAYOUT);
    glBindBuffer(GL_ARRAY_BUFFER, obj_vbo[TEXTURE_LAYOUT]);
    glBufferData(GL_ARRAY_BUFFER, obj_textures.size() * sizeof(GLfloat), obj_textures.data(), GL_STATIC_DRAW);
    glVertexAttribPointer(TEXTURE_LAYOUT, 2, GL_FLOAT, GL_FALSE, GL_FALSE, (GLubyte *) NULL);
    //glBindBuffer(GL_ARRAY_BUFFER, NULL);        -- REMOVE
    //glDisableVertexAttribArray(TEXTURE_LAYOUT); -- REMOVE

glBindVertexArray(NULL);

// <-- Save and calculate stuffs . . . --> //

obj_triangles_size = (GLuint) obj_triangles.size();

绘制:

glBindVertexArray(obj_vao);

    //glEnableVertexAttribArray(VERTEX_LAYOUT);  -- REMOVE
    //glEnableVertexAttribArray(NORMAL_LAYOUT);  -- REMOVE
    //glEnableVertexAttribArray(TEXTURE_LAYOUT); -- REMOVE

        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, obj_ibo); -- REMOVE
        glDrawElements(GL_TRIANGLES, obj_triangles_size, GL_UNSIGNED_SHORT, NULL);
        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);    -- REMOVE

    //glDisableVertexAttribArray(TEXTURE_LAYOUT); -- REMOVE
    //glDisableVertexAttribArray(NORMAL_LAYOUT);  -- REMOVE
    //glDisableVertexAttribArray(VERTEX_LAYOUT);  -- REMOVE

glBindVertexArray(NULL);