2

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!

Calco
  • 1,420
  • 1
  • 17
  • 31
  • I'm still looking over the code, but on a hunch, what happens if you remove `delete[] vertices;`? – Xirema Jan 19 '16 at 18:10
  • I removed it, but the same access violation still occurs. – Calco Jan 19 '16 at 19:03
  • Okay. OpenGL does funny things behind the scenes, so it's good to rule out stuff like that. – Xirema Jan 19 '16 at 19:09
  • You may wish to post the whole code, including the main method. The initGL() function looks a bit suspicious, but I can't know that for certain unless I can see how it gets called from the main function. – Xirema Jan 19 '16 at 19:11
  • I experimented with moving the glewInit around, but to no avail. The code is posted above, minus the methods that aren't anything to do with OpenGL. This simple box drawing part is the only OpenGL issue that seems to show up, as I can draw points and lines with no issues. For some insight, this is what works: EXAMPLE VIDEO – Calco Jan 19 '16 at 20:21
  • When you call glDrawArrays here have you bound a shader? This function looks like it shouldn't be doing any rendering, since it's called `createMesh`. Are you sure all other required state is set up correctly when `glDrawArrays` is called? – Sam Jan 20 '16 at 11:55

3 Answers3

4

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.

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • I am calling glewInit() in my 'int InitGL(GLvoid)' method that sets up things such as 'glShadeModel(GL_SMOOTH)' or 'glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)'. That's not too early is it? It gives GLEW_OK and doesn't seem to drop into the error check. Updated main question to show this. – Calco Jan 19 '16 at 18:56
2

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

float vertices[18];

and removing:

delete[] vertices;
Calco
  • 1,420
  • 1
  • 17
  • 31
0

I had the same error when i used glDraw. I fixed it by binding the VertexArray first then the IndexBuffer

Gaba Miau
  • 38
  • 4