我正在尝试为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);
我做错了什么?