void drawSquareAnnulus() { // Bind vertex buffer and reserve space. glBindBuffer(GL_ARRAY_BUFFER, buffer[VERTICES]); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW); // Copy vertex coordinates data into first half of vertex buffer. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Copy vertex color data into second half of vertex buffer. glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors); // Bind and fill indices buffer. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer[INDICES]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(stripIndices), stripIndices, GL_STATIC_DRAW); glVertexPointer(3, GL_FLOAT, 0, 0); glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(vertices))); }将上述代码改成绘制圆形的
时间: 2024-01-22 07:17:17 浏览: 129
要绘制圆形,可以使用三角扇或三角形条带来逼近圆形。以下是使用三角扇绘制圆形的示例代码:
```
void drawCircle() {
const int numSegments = 32; // 圆形的线段数
const float radius = 1.0f; // 圆形半径
// 顶点坐标和颜色数组
GLfloat vertices[(numSegments + 2) * 3];
GLfloat colors[(numSegments + 2) * 3];
// 圆心点
vertices[0] = 0.0f;
vertices[1] = 0.0f;
vertices[2] = 0.0f;
colors[0] = 1.0f;
colors[1] = 1.0f;
colors[2] = 1.0f;
// 计算圆周上的点的坐标和颜色
for (int i = 0; i <= numSegments; i++) {
float angle = i * 2.0f * M_PI / numSegments;
int offset = (i + 1) * 3;
vertices[offset] = cos(angle) * radius;
vertices[offset + 1] = sin(angle) * radius;
vertices[offset + 2] = 0.0f;
colors[offset] = 1.0f;
colors[offset + 1] = 1.0f;
colors[offset + 2] = 1.0f;
}
// 绘制三角扇
glBindBuffer(GL_ARRAY_BUFFER, buffer[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
glVertexPointer(3, GL_FLOAT, 0, 0);
glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(vertices)));
glDrawArrays(GL_TRIANGLE_FAN, 0, numSegments + 2);
}
```
注意到圆形的顶点数是固定的,因此在画圆形时不需要使用索引缓冲区。在顶点坐标和颜色数组中,第一个顶点是圆心,其它顶点按逆时针顺序排列在圆周上。最后使用 `glDrawArrays` 函数绘制三角扇即可。
阅读全文