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 float Mymin(struct tm *ptr) 18 { 19 //return (Pi/2)-(((ptr->tm_min+(((float)ptr->tm_sec)/60))/60)*2*Pi); 20 return ((Pi/2)-((ptr->tm_min+ptr->tm_sec/60.0)/60)*2*Pi); 21 }; 22 23 float Myhour(struct tm *ptr) 24 { 25 if(0<ptr->tm_hour&&ptr->tm_hour<12) 26 { 27 return ((Pi/2)-((float)ptr->tm_hour+ptr->tm_min/60.0)/12*2*Pi); 28 }else{ 29 return ((Pi/2)-((ptr->tm_hour-12.0+ptr->tm_min/60.0)/12)*2*Pi); 30 } 31 }; 32 void myDisplay(void) 33 34 { 35 //glShadeModel(GL_SMOOTH); 36 //獲取系統時間 37 struct tm *ptr; 38 time_t it; 39 it=time(NULL); 40 ptr=localtime(&it); 41 42 glClear(GL_COLOR_BUFFER_BIT);//清除,GL_COLOR_BUFFER_BIT表示清除顏色 43 glEnable(GL_POINT_SMOOTH); 44 glEnable(GL_LINE_SMOOTH); 45 glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points 46 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines 47 glEnable(GL_BLEND); 48 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 49 //鍾盤 50 glColor3f(0.5,0.5,0.5); 51 glBegin(GL_POLYGON); 52 for(int i=0;i<n;i++){ 53 glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i)); 54 } 55 glEnd(); 56 57 //刻度 58 glColor3f(1.0,1.0,1.0); 59 glPointSize(5.0f); 60 glBegin(GL_POINTS); 61 for(int j=0;j<12;j++) 62 { 63 glVertex2f(0.75*cos(2*Pi/12*j),0.75*sin(2*Pi/12*j)); 64 } 65 glEnd(); 66 67 //時針 68 glLineWidth(5.0f); 69 glColor3f(0.0,0.0,0.0); 70 71 glBegin(GL_LINES); 72 glRotatef((angle/3600.0),0.0,0.0,1.0); 73 glVertex2f(0.0,0.0); 74 glVertex2f(cos(Myhour(ptr))*R*0.55,sin(Myhour(ptr))*R*0.55); 75 glEnd(); 76 77 //分針 78 glLineWidth(4.0f); 79 glColor3f(0.0,0.0,0.0); 80 81 glBegin(GL_LINES); 82 glRotatef((angle/60.0),0.0,0.0,1.0); 83 glVertex2f(0.0,0.0); 84 glVertex2f(cos(Mymin(ptr))*R*0.65,sin(Mymin(ptr))*R*0.65); 85 glEnd(); 86 87 //秒針 88 glLineWidth(3.0f); 89 glColor3f(0.0,0.0,0.0); 90 91 glBegin(GL_LINES); 92 glRotatef(angle,0.0,0.0,1.0); 93 glVertex2f(0.0,0.0); 94 glVertex2f(cos(Mysecond(ptr))*R*0.85,sin(Mysecond(ptr))*R*0.85); 95 glEnd(); 96 glFlush();//glFlush,保證前面的OpenGL命令立即執行(而不是讓它們在緩沖區中等待)。 97 98 } 99 100 void myIdle(void) 101 { 102 angle-=((2*Pi)/60); 103 Sleep(1000); 104 if(angle<0.0f){ 105 angle=2*Pi; 106 } 107 myDisplay(); 108 } 109 110 int main(int argc, char *argv[]) 111 112 { 113 114 glutInit(&argc, argv);//glutInit,對GLUT進行初始化,這個函數必須在其它的GLUT使用之前調用一次。其格式比較死板,一般照抄這句glutInit(&argc, argv)就可以了。 115 116 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);//設置顯示方式,其中GLUT_RGB表示使用RGB顏色,與之對應的GLUT_INDEX表示使用索引顏色。GLUT_SINGLE表示使用單緩沖,與之對應的還有GLUT_DOUBLE(使用雙緩沖) 117 118 glutInitWindowPosition(300,100);//設置窗口在屏幕中的位置. 119 120 glutInitWindowSize(400, 400);//設置窗口的大小 121 122 glutCreateWindow("時鍾");//根據前面設置的信息創建窗口。參數將被作為窗口的標題。注意:窗口被創建后,並不立即顯示到屏幕上。需要調用glutMainLoop才能看到窗口。 123 124 glutDisplayFunc(&myDisplay);//調用畫圖函數 125 glutIdleFunc(&myIdle); 126 127 glutMainLoop();//進行一個消息循環。這個函數可以顯示窗口,並且等待窗口關閉后才會返回 128 129 return 0; 130 131 }
說明:其實可以在繪制時針(分針,秒針)時就指定固定形狀,由glRotatef(假定旋轉參數為angle0x)來為其旋轉到系統時間,之后"angle0x+=t"(其中t為各針對應轉速)