执行glDrawElements期间nvoglv32.dll中的OpenGL崩溃

在执行过程中,glDrawElements在nvoglv32.dll中发生崩溃。立即调用loadOpenGLObjects(),并在调用draw()之后发生rash。 glVertexAttribPointer似乎出了点问题,但是我不明白是什么。

GLfloat vertices[24] =
{
     -1.f, -1.f, -1.f,
     -1.f, 1.f, -1.f,
     1.f, 1.f, -1.f,
     1.f, -1.f, -1.f,
     1.f, 1.f, 1.f,
     1.f, -1.f, 1.f,
     -1.f, 1.f, 1.f,
     -1.f, -1.f, 1.f,
};
//...

void loadOpenGLObjects()
{
    glGenVertexArrays(1, &m_VAO);
    glBindVertexArray(m_VAO);

    glGenBuffers(1, &m_VBO);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);
}
//...
void draw()
{
    glBindVertexArray(m_VAO);
    glDrawElements(GL_TRIANGLES, 8, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

错误: 在main.exe中的0x041878C4(nvoglv32.dll)处引发了异常:0xC0000005:访问冲突读取位置0x00000000。

评论
eet
eet

Either you've to specify an Index buffers or you've to use glDrawArrays.

When you use glDrawElements then you've to specify an GL_ELEMENT_ARRAY_BUFFER. Since you've 8 vertex coordinates, which are arranged to a cube, I suggest to specify and array of indices with 6 indices (2 triangles) for each side of the cube.

GLuint indices[36] = {
    0, 1, 2,  0, 2, 3,
    // [...]
};

Specify the GL_ELEMENT_ARRAY_BUFFER. Note the GL_ELEMENT_ARRAY_BUFFER is stated in the Vertex Array Object, do not glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);:

void loadOpenGLObjects()
{
    glGenVertexArrays(1, &m_VAO);
    glBindVertexArray(m_VAO);

    glGenBuffers(1, &m_VBO);

    glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &m_IBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * 36, indices, GL_STATIC_DRAW);

    glBindVertexArray(0);
}

绘制12个三角形图元(36个索引):

glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
点赞
评论