glDrawArrays crashing when more than 4 vertices are provided

Started by
1 comment, last by Massena 13 years, 2 months ago
Hey, just starting to work with openGL so I might be making a very stupid mistake. I'm using glDrawArrays and glVertexAttribPointer to draw primitives from an array that has 4 vertex position/vertex color pairs.

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.
Advertisement
Check your second argument to glVertexAttribPointer, I think you have misinterpreted it.


size
Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, or 4. The initial value is 4.
[/quote]

You appear to be passing in the number of vertices, when in your case this should just be a fixed '3'.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

Check your second argument to glVertexAttribPointer, I think you have misinterpreted it.


size
Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, or 4. The initial value is 4.


You appear to be passing in the number of vertices, when in your case this should just be a fixed '3'.
[/quote]

[url="http://thinkexist.com/quotation/may_flowers_always_line_your_path_and_sunshine/151623.html"][/url]May flowers always line your path and sunshine light your day, that was exactly it. Thanks a lot, man!

This topic is closed to new replies.

Advertisement