資源描述:
《基于c++ opengl與系統(tǒng)同步的時(shí)鐘》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、基于opengl和C++與系統(tǒng)同步的簡(jiǎn)單時(shí)鐘初學(xué)者可以看看,挺簡(jiǎn)單的顯示效果如下://main.cpp#includeusingnamespacestd;#include#include"clock.h"intmain(intargc,char*argv[]){Clockclock;//新?建?§一°?個(gè)?時(shí)o?à鐘¨?clock.start();//啟?動(dòng)?¥并?é顯?示o?時(shí)o?à鐘¨?return0;}//clock.h#ifndefOPENGL_CLOCK_D
2、ISPLAY_H#defineOPENGL_CLOCK_DISPLAY_H#include#includeusingnamespacestd;#include#defineTIME_GAP1000//ms#definepi3.141592653fclassClock{public:staticfloatsecondAngle;staticfloatminuteAngle;staticfloathourAngle;staticintcurSecon
3、d;staticintcurMinute;staticintcurHour;public:Clock(inth=0,intm=0,ints=0){curSecond=s;curHour=h;curMinute=m;}staticvoidclockDisplay();//繪?制?時(shí)o?à鐘¨?staticvoidsetAngle(intval);//調(diào)ì??整?時(shí)o?à間?點(diǎn)ì?voidstart()//啟?動(dòng)?¥并?é顯?示o?時(shí)o?à鐘¨?{SYSTEMTIMEcurtime;GetLocalTime(&c
4、urtime);//獲?取¨?當(dāng)ì?à前??的ì?時(shí)o?à間?curSecond=curtime.wSecond;curMinute=curtime.wMinute;curHour=curtime.wHour;secondAngle=curSecond*360/60.0f;minuteAngle=curMinute*360/60.0f+curSecond*360.0f/60/60;hourAngle=(curHour%12)*360/12.0f+curMinute*360/12/60;inti=1;char*
5、c="";glutInit(&i,&c);glutInitDisplayMode(GLUT_RGB
6、GLUT_DOUBLE);glutInitWindowPosition(100,100);glutInitWindowSize(400,400);glutCreateWindow("opengl時(shí)o?à鐘¨?");glutDisplayFunc(&clockDisplay);//curtime.wYear//curtime.wMonth//curtime.wDayglutTimerFunc(TIME_GAP,s
7、etAngle,1);//設(shè)|¨¨置?定?§時(shí)o?à器??回?調(diào)ì??函?¥數(shù)oyglutMainLoop();}};voidClock::clockDisplay(){glClear(GL_COLOR_BUFFER_BIT);floatclockMinAngle=2*pi/60;intcnt=0;glLineWidth(3);glColor3f(1.0f,0.0f,0.0f);//setcolorglBegin(GL_LINE_STRIP);//畫-表à¨a盤¨?for(cnt=0;cnt<=100;cnt
8、++){glVertex2f(cos(cnt/100.f*2*pi),sin(cnt/100.f*2*pi));}glEnd();glBegin(GL_LINES);//畫-刻¨?度¨¨for(cnt=0;cnt<60;cnt++){if(cnt%5==0){glVertex2f(sin(cnt*clockMinAngle),cos(cnt*clockMinAngle));glVertex2f(0.85*sin(cnt*clockMinAngle),0.85*cos(cnt*clockMinAngle));}
9、else{glVertex2f(sin(cnt*clockMinAngle),cos(cnt*clockMinAngle));glVertex2f(0.95*sin(cnt*clockMinAngle),0.95*cos(cnt*clockMinAngle));}}glEnd();glMatrixMode(GL_MODELVIEW);glLoadIdentity();//繪?制?秒?針?glColo