#include<gl/glut.h>
#include<math.h>
#include <stdlib.h>
const double PI = 3.14159265357f;
const double R = 0.57f;
double red[] = { 1,0,0 };
double blue[] = { 0,0,1 };
double green[] = { 0,1,0 };
void DrawCircle(double radius,double* color) {
//画圆,凸多边形化曲为直
int n = 150;
glBegin(GL_LINE_LOOP);
glColor3f(color[0], color[1], color[2]);
for (int i = 1; i <= n; i++) {
glVertex2f((radius * cos(2 * PI / n * i)), radius * sin(2 * PI / n * i));
}
glEnd();
}
void DrawDiamond(double length, double* color) {
glBegin(GL_LINE_LOOP);
glColor3f(color[0], color[1], color[2]);
glVertex2f(0, length); glVertex2f(length, 0);
glVertex2f(0, -length); glVertex2f(-length, 0);
glEnd();
}
void DrawRectangle(double length, double* color) {
glBegin(GL_LINE_LOOP);
glColor3f(color[0], color[1], color[2]);
glVertex2f(length, length); glVertex2f(length, -length);
glVertex2f(-length, -length); glVertex2f(-length, length);
glEnd();
}
void myDisplay(){
glClearColor(1.0, 1.0, 1.0, 1.0);//白色背景,前3个是RGB,最后是Alpha值,用来控制透明,1.0表示完全不透明
glClear(GL_COLOR_BUFFER_BIT);//使用glClearColorz中指定的值设定颜色缓存区的值,即将窗口中的每一个像素设置为背景色
double l = 0.5;
for (int i = 0; i < 10; i++) {
DrawDiamond(l*2, red);
DrawCircle(l*sqrt(2), blue);
DrawRectangle(l, green);
l /= 2;
}
glFlush();//清空OpenGL命令缓冲区,强制执行命令缓冲区中所有OpenGL函数
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(200, 200);//设定窗口的初始位置,屏幕左上角为原点,单位为像素
glutInitWindowSize(500, 500);//设定窗口的大小
glutCreateWindow("Hellow World!");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
有圆有方
最新推荐文章于 2019-11-17 19:19:42 发布