#include <windows.h>
#include <gl/gl.h>
#include <gl/glaux.h>
#include <math.h>
#include <time.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
float starData[8];//存储目标五角星数据的全局变量
//按序为xc, yc, r1, r2, angle, R, G, B
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);//黑色背景
}
/**
版本号:2.0
功能:计算五角星的10个顶点坐标
参数:
points 存放10个顶点坐标的数组
xc, yc 中心坐标
r1 五角星外面5个顶点所在圆的半径
r2 里面5个顶点所在圆的半径,r2<r1
angle 弧度表示的倾斜度,0为标准,数值增大表示向逆时针方向倾斜
**/
void getStarPoints(float points[10][2], float xc, float yc, float r1, float r2, float angle)
{
const float PI = 3.14;
int i, r;
angle = angle + 0.1 * PI;
for (i=0; i<=9; i++)
{
r = (i%2==0)?r1:r2;
points[i][0] = xc + (float)r*cos(angle+i*0.2*PI);
points[i][1] = yc + (float)r*sin(angle+i*0.2*PI);
}
}
void render()
{
int con;
float points[10][2];
getStarPoints(points, starData[0], starData[1], starData[2], starData[3], starData[4]);
glColor3f(starData[5], starData[6], starData[7]);
for (con=0; con<=9; con++)
{
glBegin(GL_LINES);
glVertex2f(points[con][0], points[con][1]);
glVertex2f(points[(con+1)%10][0], points[(con+1)%10][1]);
glEnd();
}
}
void CALLBACK draw()
{
glClear(GL_COLOR_BUFFER_BIT);
render();
glFinish();
}
void CALLBACK change()
{
int i;
srand(time(NULL));
i = rand()%300 + 100; //xc从100到400
starData[0] = (float)i;
i = rand()%300 + 100; //yc从100到400
starData[1] = (float)i;
i = rand()%40 + 60; //r1从60到100
starData[2] = (float)i;
i = rand()%30 + 20; //r2从20到50
starData[3] = (float)i;
i = rand()%6; //偏转角度从0到6
starData[4] = i/1.0f;
i = rand()%200; //RGB中红色的比例从0到1
starData[5] = i/200.0f;
i = rand()%200; //RGB中绿色的比例从0到1
starData[6] = i/200.0f;
i = rand()%200; //RGB中蓝色的比例从0到1
starData[7] = i/200.0f;
draw();
}
void main()
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(200, 100, 500, 500);
auxInitWindow("CGOpenGL");
init();
auxIdleFunc(change);
auxMainLoop(draw);
}