glDrawArrays访问冲突

时间:2016-01-19 18:08:19

标签: c++ opengl visual-studio-2013 gldrawarrays

我正在尝试关注[this] [1]简单教程,但是在达到' glDrawArrays'时出现以下错误:

openGLTest.exe中0x03D7598E(nvoglv32.dll)的未处理异常:0xC0000005:访问冲突读取位置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 
}

由于教程是一样的,我对导致它的原因感到茫然!

3 个答案:

答案 0 :(得分:4)

在处理OpenGL时,访问冲突读取位置0通常意味着您未正确建立到OpenGL API的挂钩。 GLEW是否在您的代码中正确设置?您是否致电glewInit()并确认结果为GL_TRUE?在使上下文变为当前之前或之后调用glewInit()?验证所有这些事情,它应该解决问题。

答案 1 :(得分:2)

经过多次实验,我通过将第一行更改为:

来解决此问题
float vertices[18];

并删除:

delete[] vertices;

答案 2 :(得分:0)

我在使用 glDraw 时遇到了同样的错误。我通过先绑定 VertexArray 然后绑定 IndexBuffer 来修复它