OpenGL crash in nvoglv32.dll during execution glDrawElements

1

During execution glDrawElements happen crash in nvoglv32.dll. Immediately called loadOpenGLObjects() and after it invoke draw(), in which happens сrash. Seems like something went wrong with glVertexAttribPointer, but i don't understand what.

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

Error:

Exception thrown at 0x041878C4 (nvoglv32.dll) in main.exe: 0xC0000005: Access violation reading location 0x00000000.

c++
opengl
asked on Stack Overflow May 9, 2020 by magicDM • edited May 9, 2020 by Rabbid76

1 Answer

0

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

Draw the 12 triangle primitives (36 indices):

glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
answered on Stack Overflow May 9, 2020 by Rabbid76 • edited May 9, 2020 by Rabbid76

User contributions licensed under CC BY-SA 3.0