编辑器 vs2012 1 #include <windows.h> 2 #include <GL/glut.h>//本来OpenGL程序一般还要包含<GL/gl.h>和<GL/glu.h>, 3 //但GLUT的头文件中已经自动将这两个文件包含了,不必再次包含。 4 #include <math.h> 5 #include<time.h> 6 7 const GLfloat Pi = 3.1415926536; 8 const GLfloat R=0.8f; 9 const int n=200; 10 static GLfloat angle=2*Pi; 11 12 float Mysecond(struct tm *ptr) 13 { 14 return ((Pi/2)-(((float)ptr->tm_sec)/60)*2*Pi); 15 16 } 17 18 float Mymin(struct tm *ptr) 19 { 20 //return (Pi/2)-(((ptr->tm_min+(((float)ptr->tm_sec)/60))/60)*2*Pi); 21 return ((Pi/2)-((ptr->tm_min+(Mysecond(ptr)/60))/60)*2*Pi); 22 }; 23 24 float Myhour(struct tm *ptr) 25 { 26 if(0<ptr->tm_hour&&ptr->tm_hour<12) 27 { 28 return ((Pi/2)-((float)ptr->tm_hour+Mymin(ptr)/60.0)/12.0*2*Pi); 29 }else{ 30 return ((Pi/2)-((ptr->tm_hour-12.0+Mymin(ptr)/60.0)/12)*2*Pi); 31 } 32 }; 33 void myDisplay(void) 34 35 { 36 //glShadeModel(GL_SMOOTH); 37 //获取系统时间 38 struct tm *ptr; 39 time_t it; 40 it=time(NULL); 41 ptr=localtime(&it); 42 43 glClear(GL_COLOR_BUFFER_BIT);//清除,GL_COLOR_BUFFER_BIT表示清除颜色 44 glEnable(GL_POINT_SMOOTH); 45 glEnable(GL_LINE_SMOOTH); 46 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points 47 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines 48 glEnable(GL_BLEND); 49 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 50 //钟盘 51 glColor3f(0.5,0.5,0.5); 52 glBegin(GL_POLYGON); 53 for(int i=0;i<n;i++){ 54 glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i)); 55 } 56 glEnd(); 57 58 //刻度 59 glColor3f(1.0,1.0,1.0); 60 glBegin(GL_POINTS); 61 glPointSize(5.0f); 62 for(int j=0;j<12;j++) 63 { 64 glVertex2f(0.75*cos(2*Pi/12*j),0.75*sin(2*Pi/12*j)); 65 //for(int k= 66 } 67 68 glEnd(); 69 //中心点 70 glPointSize(5.0f); 71 glColor3f(0.0,0.0,0.0); 72 glBegin(GL_POINTS); 73 glVertex2f(0.0,0.0); 74 glEnd(); 75 76 //时针 77 glLineWidth(5.0f); 78 glColor3f(0.0,0.0,0.0);//红色 79 //glRotatef((angle/3600.0),0.0,0.0,1.0); 80 81 glBegin(GL_LINES); 82 glRotatef((angle/3600.0),0.0,0.0,1.0); 83 glVertex2f(0.0,0.0); 84 glVertex2f(cos(Myhour(ptr))*R*0.55,sin(Myhour(ptr))*R*0.55); 85 glEnd(); 86 87 //分针 88 glLineWidth(5.0f); 89 glColor3f(0.0,0.0,0.0);//绿色 90 //glRotatef((angle/60.0),0.0,0.0,1.0); 91 92 glBegin(GL_LINES); 93 glRotatef((angle/60.0),0.0,0.0,1.0); 94 glVertex2f(0.0,0.0); 95 glVertex2f(cos(Mymin(ptr))*R*0.65,sin(Mymin(ptr))*R*0.65); 96 glEnd(); 97 98 //秒针 99 glLineWidth(3.0f); 100 glColor3f(0.0,0.0,0.0);//蓝色 101 //glRotatef(angle,0.0,0.0,1.0); 102 103 glBegin(GL_LINES); 104 glRotatef(angle,0.0,0.0,1.0); 105 glVertex2f(0.0,0.0); 106 glVertex2f(cos(Mysecond(ptr))*R*0.85,sin(Mysecond(ptr))*R*0.85); 107 glEnd(); 108 glFlush();//glFlush,保证前面的OpenGL命令立即执行(而不是让它们在缓冲区中等待)。 109 110 } 111 112 void myIdle(void) 113 { 114 angle-=((2*Pi)/60); 115 Sleep(1000); 116 if(angle<0.0f){ 117 angle=2*Pi; 118 } 119 myDisplay(); 120 } 121 122 int main(int argc, char *argv[]) 123 124 { 125 126 glutInit(&argc, argv);//glutInit,对GLUT进行初始化,这个函数必须在其它的GLUT使用之前调用一次。其格式比较死板,一般照抄这句glutInit(&argc, argv)就可以了。 127 128 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);//设置显示方式,其中GLUT_RGB表示使用RGB颜色,与之对应的GLUT_INDEX表示使用索引颜色。GLUT_SINGLE表示使用单缓冲,与之对应的还有GLUT_DOUBLE(使用双缓冲) 129 130 glutInitWindowPosition(300,100);//设置窗口在屏幕中的位置. 131 132 glutInitWindowSize(400, 400);//设置窗口的大小 133 134 glutCreateWindow("时钟");//根据前面设置的信息创建窗口。参数将被作为窗口的标题。注意:窗口被创建后,并不立即显示到屏幕上。需要调用glutMainLoop才能看到窗口。 135 136 glutDisplayFunc(&myDisplay);//调用画图函数 137 glutIdleFunc(&myIdle); 138 139 glutMainLoop();//进行一个消息循环。这个函数可以显示窗口,并且等待窗口关闭后才会返回 140 141 return 0; 142 143 } 效果
时间: 2024-11-06 19:27:18