Question

I want to use VBO's and the fixed function pipeline (I know I should be using shaders but I need to use the FFP...)

I have a triangle being drawn, I just can't get the color attributes set-up correctly.

here is where I create the buffers

const GLfloat vertexPositions[] = {10, 10, 0,10, -10, 0, -10, -10, 0};

const GLfloat vertexColours[] = {1,0,0,0,0,1, 0,1,0};

GLuint positionsBufferObject;
glGenBuffers(1, &positionsBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionsBufferObject);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertexPositions, GL_STATIC_DRAW);
glFinish(); //wait until data transfered
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); //attributes are in currently bound buffer


GLuint colourBufferObject;
glGenBuffers(1, &colourBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, colourBufferObject);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(GLfloat), vertexColours, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW  
glFinish();
glColorPointer( 3, GL_FLOAT, 0, (char *) NULL );//attributes are in currently bound buffer

and this is where I draw;

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLES, 0, 3);   // draw first object

The triangle gets drawn, but the colors are wrong, the first vertex gets the correct color (red) but the remaining two are black;

enter image description here

I've tried messing around with glVertexAttribPointer with different values of index, but I suspect this won't have any impact in the FFP. Where am I going wrong?

Was it helpful?

Solution

glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(GLfloat), vertexColours, ...

Should presumably pass 9 * sizeof(GLfloat) for the size parameter.

Since you did just that for vertexPositions, this looks like a cut-and-paste bug:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top