opengl绘画太极图
时间: 2023-11-17 09:05:16 浏览: 105
好的,下面是使用OpenGL绘制太极图的代码示例:
```cpp
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.7, 0.7);
glVertex2f(0.7, 0.7);
glVertex2f(0.7, -0.7);
glVertex2f(-0.7, -0.7);
glEnd();
glPushMatrix();
glTranslatef(-0.35, 0.35, 0.0);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-0.35, 0.35);
glVertex2f(0.35, 0.35);
glVertex2f(0.35, -0.35);
glVertex2f(-0.35, -0.35);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0.35, -0.35, 0.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.35, 0.35);
glVertex2f(0.35, 0.35);
glVertex2f(0.35, -0.35);
glVertex2f(-0.35, -0.35);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.35, -0.35, 0.0);
glColor3f(1.0,1.0, 1.0);
glBegin(GL_POLYGON);
for (float angle = 0.0; angle < 360.0; angle += 0.1) {
float x = 0.15 * cos(angle * 3.1415926 / 180.0);
float y = 0.15 * sin(angle * 3.1415926 / 180.0);
glVertex2f(x, y);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0.35, 0.35, 0.0);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
for (float angle = 0.0; angle < 360.0; angle += 0.1) {
float x = 0.15 * cos(angle * 3.1415926 / 180.0);
float y = 0.15 * sin(angle * 3.1415926 / 180.0);
glVertex2f(x, y);
}
glEnd();
glPopMatrix();
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Yin Yang");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}
```
请确保已安装OpenGL的开发环境,例如使用FreeGLUT库。
阅读全文