使用固定功能管道在OpenGL中设置VBO的颜色属性

时间:2014-03-10 10:54:23

标签: opengl vbo opengl-3

我想使用VBO和固定功能管道(我知道我应该使用着色器,但我需要使用FFP ......)

我画了一个三角形,我无法正确设置颜色属性。

这是我创建缓冲区的地方

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

这是我画的地方;

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

绘制三角形,但颜色错误,第一个顶点得到正确的颜色(红色),但剩下的两个是黑色;

enter image description here

我已尝试使用不同的索引值来处理glVertexAttribPointer,但我怀疑这对FFP没有任何影响。我哪里错了?

1 个答案:

答案 0 :(得分:2)

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

可能会为9 * sizeof(GLfloat)参数传递size

因为你为vertexPositions做了这个,所以这看起来像是一个剪切和粘贴的bug:)