【问题标题】:Setting color attribute for a VBO in OpenGL using the fixed function pipeline使用固定函数管道在 OpenGL 中为 VBO 设置颜色属性
【发布时间】:2014-03-10 10:54:23
【问题描述】:

我想使用 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

三角形被绘制,但颜色错误,第一个顶点的颜色正确(红色),但其余两个为黑色;

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

【问题讨论】:

  • 您的 glFinish 调用完全没有必要。 OpenGL 向您保证,当 glBufferData 返回时,所有数据都已被复制。

标签: opengl vbo opengl-3


【解决方案1】:
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(GLfloat), vertexColours, ...

大概应该为size参数传递9 * sizeof(GLfloat)

由于您只是为 vertexPositions 做的,这看起来像是一个剪切和粘贴错误:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2011-11-25
    相关资源
    最近更新 更多