編輯器 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 }
效果