OpenGL VBO中的颜色?

3

我正在尝试为OpenGL中使用单个结构体和单个VBO的对象添加颜色。为此,我创建了一个名为Vertex的结构体,它看起来像这样:

typedef struct {
    float x;
    float y;
    float z;
    float r;
    float g;
    float b;
    float a;
} Vertex;

I already know how to set the coordinates and colors correctly to achieve my desired outcome. I conducted a test by iterating over each object in my list, drawing out the points, and setting the colors using glVertex3f and glColor4f. However, this method was considerably slower than what I intend to use. When attempting to draw using VBOs, it results in a chaotic display of colored triangles.
Here's the code snippet responsible for drawing the VBO in my rendering loop:
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, offsetof(Vertex, r), NULL);
glDrawArrays(GL_TRIANGLES, 0, vertex_amount);
glBindBuffer(GL_ARRAY_BUFFER, 0);

我做错了什么?

1个回答

4
glVertexPointer(3, GL_FLOAT, 0, NULL);
                             ^

Vertex数组中的位置值不是紧密打包的,因此您不能使用0来表示stride。请使用sizeof(Vertex)

glColorPointer(4, GL_FLOAT, offsetof(Vertex, r), NULL);
                            ^^^^^^^^^^^^^^^^^^^

我不确定您想要表达什么。也许您认为glVertexPointer()/glColorPointer()的第三个参数应该是pointer而不是stride?请将offsetof()移动到最后一个参数pointer

全部内容如下:

glVertexPointer(3, GL_FLOAT, sizeof( Vertex ), offsetof(Vertex, x) );
glColorPointer(4, GL_FLOAT, sizeof( Vertex ), offsetof(Vertex, r) );

根据你实现offsetof()的方式,你可能需要将pointer参数的值转换为void*


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接