glDrawArrays Access Violation

吃可爱长大的小学妹 提交于 2019-12-13 02:25:21

问题


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!


回答1:


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.




回答2:


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

float vertices[18];

and removing:

delete[] vertices;


来源:https://stackoverflow.com/questions/34883985/gldrawarrays-access-violation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!