#include<stdlib.h>
#include<glut.h>
#include<gl\GLU.h>
#include<gl\GL.h>
#include<math.h>
#include<stdio.h>
#include<windows.h>
float tite = 0.0f;
const float PI = 3.14159f;
const int W_width = 500;
const int W_height = 500;
float xc, yc, R, n;
float ss, mm, hh;
const int S_width = glutGet(GLUT_SCREEN_WIDTH);
const int S_height = glutGet(GLUT_SCREEN_HEIGHT);
//system
void init();
void draw();
void reShape(int ww, int hh);
//colck
void drawCircle();
void myIdle();
void drawline();
void TimerFunc(int value);
void arith_foot();
void drawshortLine();
void TimerFunc(int value);
void init(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0f, 0.5f, 0.8f, 1.0);
//colck
SYSTEMTIME Timetite;
GetLocalTime(&Timetite);
xc = 0.0f, yc = 0.0f, R = 100.0f, n = 20.0f, ss = Timetite.wSecond, mm = Timetite.wMinute, hh = Timetite.wHour;
}
int main(int agrs, char **args) {
glutInit(&agrs, args);
init();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(W_width, W_height);
glutInitWindowPosition((S_width - W_width) / 2, (S_height - W_width) / 2);
glutCreateWindow("openGL");
//设置视景体窗口
glClearColor(0.6f, 0.4f, 0.3f, 1.0f); //清除窗口颜色的代码
glutDisplayFunc(draw);
glutReshapeFunc(reShape);
glutMainLoop();
return 0;
}
void draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
drawCircle();
arith_foot();
drawshortLine();
//TimerFunc(10);
glutTimerFunc(1000, TimerFunc, 10);
//drawline();
glutSwapBuffers();
//shapes
}
void reShape(int ww, int hh) {
glViewport(0, 0, ww, hh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (hh == 0) hh = 1;
if (ww >= hh) {
gluOrtho2D(-500, 500 * (float)ww / hh, -500, 500);
}
else
{
gluOrtho2D(-500, 500, -500, 500 * (float)hh / ww);
}
glMatrixMode(GL_MODELVIEW);
}
//利用基本图元的绘制来实现简单动画的功能(时钟)
//draw circle
void drawCircle() {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < n; i++) {
glVertex2d(xc + R * cos(i * 2 * PI / n), yc + R * sin(i * 2 * PI / n));
}
glEnd();
}//画出时钟圆
//直线测试
void drawline() {
glBegin(GL_LINES);
glVertex2d(xc, yc);
glVertex2d(R*cos(tite), R*sin(tite));
glEnd();
}
//闲置函数
void myIdle(void) {
tite += 0.002f;
if (tite >= 2 * PI) {
tite = 0.0f;
}
glutPostRedisplay();
}
void TimerFunc(int value) {//更新时分秒的值
int carry1 = 0;
ss += 1;
if (ss >= 60) {
ss = 0;
carry1 = 1;
}
mm += carry1;
int carry2 = 0;
if (mm >= 60) {
carry2 = 1;
mm = 0;
}
hh += carry2;
if (hh == 24) hh = 0;
glutPostRedisplay();//(回调函数)返回draw方法根据ss,mm,hh更新xs,xm,xh的刻度值
}
//draw hh,ss,mm lines(绘制时针与分针的刻度线)
void drawshortLine(){
int lines = 60;
for (int i = 0; i < lines;i++){
if (i % 5 == 0){//时针
glLineWidth(5);
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex2f(xc + (R-10)*sin(2*PI*i/lines),yc+(R-10)*cos(2*PI*i/lines));
glVertex2f(xc + R*sin(2 * PI*i / lines), yc + R*cos(2 * PI*i / lines));
glEnd();
}
else{
glLineWidth(2);
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2f(xc+(R-5)*sin(2*PI*i/lines),yc+(R-5)*cos(2*PI*i/lines));
glVertex2f(xc + R*sin(2 * PI*i / lines), yc + R*cos(2 * PI*i / lines));
glEnd();
}
}
}
//arith foot colck(每一次调用draw方法的时候都会根据此时不同的ss,mm,hh重新计算时分秒指针的值)
void arith_foot(){
float xs = 0.0f, ys=0.0f, xm=0.0f, ym=0.0f, xh=0.0f, yh=0.0f;
glLineWidth(1);
xs = xc + (R-10)*sin(ss*2*PI/60);//根据比值法计算出秒针与y轴之间的角度来计算x终点位置与y终点位置
ys = yc + (R - 10)*cos(ss * 2 * PI / 60);
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINES);
glVertex2f(xc,yc);
glVertex2f(xs,ys);
glEnd();
glLineWidth(3);
xm = xm + (R - 25)*sin((mm + ss / 60) * 2 * PI / 60);//秒针与分针都是60份
ym = ym + (R - 25)*cos((mm + ss / 60) * 2 * PI / 60);
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(xc,yc);
glVertex2f(xm,ym);
glEnd();
glLineWidth(5);
xh = xh + (R - 45)*sin((hh + (mm + ss / 60) / 60) * 2 * PI / 12);//每一次走的角度分为12份
yh = yh + (R - 45)*cos((hh + (mm + ss / 60) / 60) * 2 * PI / 12);
glColor3f(0.0f,0.0f,1.0f);
glBegin(GL_LINES);
glVertex2f(xc,yc);
glVertex2f(xh,yh);
glEnd();
}
//draw a lots of shapes
void Shapes(){
//draw points
glPointSize(10);
glBegin(GL_POINT);
//glVertex2d();
//glVertex2d();
//glVertex2d();
glEnd();
}
来源:CSDN
作者:一剑孤寂
链接:https://blog.csdn.net/Icecoldless/article/details/80023346