The shaders are very simple and just multiply the positions with the positions and pass the colors.
Now, everything works fine when I use 4 vertices or less. (3 draw a triangle and 2 draw nothing, as expected) But if I try to put 5 vertex/color pairs in vertex_data I get a crash with EXC_BAD_ACCESS
Here's the code which I think contains the problem:
const GLfloat vertex_data[] = {
-3.0f, 3.0f, 3.0f,
0.0f, 1.0f, 0.0f,
3.0f, 3.0f, 3.0f,
1.0f, 0.0f, 0.0f,
3.0f, 3.0f, -3.0f,
1.0f, 0.0f, 1.0f,
-3.0f, 3.0f, -3.0f,
1.0f, 1.0f, 0.0f,
//3.0f, 3.0f, 0.0f,
//1.0f, 0.0f, 0.0f, it crashes if I uncomment these lines
};
void render() {
glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
glEnableVertexAttribArray(glGetAttribLocation(program, "position"));
glVertexAttribPointer(glGetAttribLocation(program, "position"), sizeof(vertex_data)/(sizeof(GLfloat)*6), GL_FLOAT, GL_FALSE, sizeof(GLfloat)*6, (void*)0);
glEnableVertexAttribArray(glGetAttribLocation(program, "color"));
glVertexAttribPointer(glGetAttribLocation(program, "color"), sizeof(vertex_data)/(sizeof(GLfloat)*6), GL_FLOAT, GL_FALSE, sizeof(GLfloat)*6, (void*)(sizeof(GLfloat)*3));
glUniformMatrix4fv(glGetUniformLocation(program, "perspective"), 1, GL_TRUE, perspective);
glUniformMatrix4fv(glGetUniformLocation(program, "transform"), 1, GL_TRUE, transform);
//Here is where it crashes with EXC_BAD_ACCESS if I have more than 4 vertices
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertex_data)/(sizeof(GLfloat)*6));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glUseProgram(0);
glutSwapBuffers();
}
I tried using GL_QUADS, GL_TRIANGLES and GL_TRIANGLE_STRIP, nothing works with more than 4 vertices.
Here is all the initialization:
glutInit(&argc, argv);
position_buffer = 0;
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(800, 600);
glutCreateWindow("GLUT Window");
glutIdleFunc(&update);
glutDisplayFunc(&render);
glutReshapeFunc(&reshape);
glutSpecialFunc(&keyboard);
glutSpecialUpFunc(&keyboard_up);
glGenBuffers(1, &position_buffer);
glBindBuffer(GL_ARRAY_BUFFER, position_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
I can't get the debugger to give me any meaningful information and what's really bothering me is that it does work with 4 vertices or less. I think I provided all the necessary information, but if I'm missing something let me know.
I'm absolutely stumped, all help is appreciated.