glDrawArrays Access Violation

2019-08-15 17:35发布

I am trying to follow [this][1] simple tutorial, but I am getting the following error upon reaching 'glDrawArrays':

Unhandled exception at 0x03D7598E (nvoglv32.dll) in openGLTest.exe: 0xC0000005: Access violation reading location 0x00000000.

void createMesh(void) {
    float* vertices = new float[18];// Amount of vertices

    vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
    vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
    vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner

    vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
    vertices[12] = -0.5; vertices[13] = -0.5; vertices[14] = 0.0; // Bottom left corner
    vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner

    glGenVertexArrays(1, &vaoID[0]); // Create our Vertex Array Object
    glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object so we can use it

    glGenBuffers(1, vboID); // Generate our Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind our Vertex Buffer Object
    glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW

    glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer

    glEnableVertexAttribArray(0); // Disable our Vertex Array Object
    glBindVertexArray(0); // Disable our Vertex Buffer Object

    delete[] vertices; // Delete our vertices from memory

    glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object

    glDrawArrays(GL_TRIANGLES, 0, 6); // Draw our square

    glBindVertexArray(0); // Unbind our Vertex Array Object 
}

I am at a loss as to what is causing it as the tutorial is the same!

2条回答
Root(大扎)
2楼-- · 2019-08-15 17:54

When dealing with OpenGL, an Access Violation reading location 0 typically means that your hooks into the OpenGL API are not properly established. Is GLEW properly set up in your code? Did you call glewInit() and verify that the result is GL_TRUE? Is glewInit() being called before or after you make the context current? Verify all of these things, and it should resolve the issue.

查看更多
▲ chillily
3楼-- · 2019-08-15 18:07

After much experimentation I fixed this issue by changing the first line to:

float vertices[18];

and removing:

delete[] vertices;
查看更多
登录 后发表回答