代码如下:
struct Vertex {
float x, y, z;
float r, g, b;
};
static void render(GLFWwindow * window) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
Vertex rect[] =
{
{ 10, 10, 0, 1, 0, 0 },
{ 110, 10, 0, 0, 1, 0 },
{ 10, 110, 0, 0, 0, 1 },
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), rect);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &rect[0].r);
glDrawArrays(GL_TRIANGLES, 0, 3);
Vertex rect1[] =
{
{ 10, 10, 0, 1, 0, 0 },
{ 110, 10, 0, 0, 1, 0 },
{ 10, 110, 0, 0, 0, 1 },
};
for(int i = 0; i < 3; ++i) {
rect1[i].x += 300;
}
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), rect1);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &rect1[0].r);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
运行结果: