Hello,
I'am learning OpenGL and I am experimenting with glDrawElements, I use SFML for window system.
I've generated a 3d cube VAO using VBO and index buffer object, it renders well and correctly, but when I close the window I have an error message:
"Unhandled exception at 0x69761CB7 (nvoglv32.dll) in Opengl.exe: 0xC0000005: Access violation reading location 0x00000000."
And it shows me this line :
"window.display();"
VAO generation part:
GLuint createCube()
{
const float vertices[] = {
0.25f, 0.25f, -1.25f, 1.0f,
0.25f, -0.25f, -1.25f, 1.0f,
-0.25f, 0.25f, -1.25f, 1.0f,
-0.25f, -0.25f, -1.25f, 1.0f,
0.25f, 0.25f, -2.75f, 1.0f,
-0.25f, 0.25f, -2.75f, 1.0f,
0.25f, -0.25f, -2.75f, 1.0f,
-0.25f, -0.25f, -2.75f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.8f, 0.8f, 0.8f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 1.0f,
};
const GLshort indexData[]
{
0, 1, 2,
1, 3, 2,
4, 5, 6,
6, 5, 7,
2, 3, 7,
2, 7, 5,
0, 6, 1,
0, 4, 6,
4, 0, 2,
4, 2, 5,
6, 3, 1,
6, 7, 3,
};
GLuint vao;
glGenVertexArrays(1, &vao);
GLuint vbo;
glGenBuffers(1, &vbo);
GLuint indexObjectBuffer;
glGenBuffers(1, &indexObjectBuffer);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexObjectBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36*4, indexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); // vertices
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)128); // color
glBindVertexArray(0);
return vao;
}
Drawing:
glUseProgram(program);
glBindVertexArray(vao);
// (...)
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
// (...)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUniform3f(offsetUniform, offsetX, offsetY, offsetZ);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
window.display();
}
The entire code:
http://pastebin.com/uhRRbKNrThanks,