太阳系的绘制
太陽系的繪制
分析:
1.以太陽為中心,繪制五個球體,太陽,地球,水星,火星,木星
2.查找5個球體的半徑以及與球體之間的距離,設置觀察點坐標,方便能看到五個球體
3.查找除了太陽之外的其他四個球體的運行速度,繪制出以太陽微中心的旋轉圖像。
4.光照的設置:太陽為光源,微微散發出紅色的光線,其他的四個(水星:灰色黃白色,地球上看略帶藍色,火星:黃色,木星:黃色)
查找資料分析如下:按一定比例設置球體半徑、運行速度以及運行軌跡
準備步驟:
1.下載 vc6.0,記住安裝位置,后面要用到
2.將.dll文件放入C:\Windows\System32中
3.將lib文件放入vc6.0安裝目錄中vc98 lib中
4.將.h文件放入vc98\include\GL中
準備工作做好就可以開始寫代碼了:
#include <gl/glut.h> #include <stdlib.h> #include <windows.h> static int year0 = 0 ,year1 = 0,year2=0, year3=0,year4=0,day = 0;void display(void) {GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };//鏡面光顏色白色GLfloat mat_shininess[] = { 200.0 };//粗糙程度GLfloat light_position[] = { 0.0, 0.0, 5.0, 0.0 };//光源位置,第四個參數為0,表示光源為無限遠處,eg:太陽光GLfloat mat_emission[] = { 0.2,0.0,0.0,0.2};//sun微微發紅光GLfloat white_light[] = { 1.0, 1.0, 1.0, 0.1 };GLfloat Light_Model_Ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 }; //環境光glClearColor (0.0, 0.0, 0.0, 0.0);//窗口背景色為白色glShadeModel (GL_SMOOTH);//漸變色glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);//鏡面glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);//粗糙glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);//使太陽微微發出紅光。會照射在其他球體上//glLight 函數主要作用是設置光源,該函數有四種形式glLightf、glLighti、glLightfv、glLightivglLightfv(GL_LIGHT0, GL_POSITION, light_position);//第一個參數表示設置哪個光源,第二個表示哪個光源的哪個屬性,第三個表示把屬性設置成多少glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);//漫glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);//鏡面glLightModelfv( GL_LIGHT_MODEL_AMBIENT , Light_Model_Ambient ); glLightModeli( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );glEnable( GL_COLOR_MATERIAL );glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_DEPTH_TEST);//光照測試glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//GL_DEPTH_BUFFER_BIT不會把原先繪制的圖形刪掉,近的的看見,遠的被遮住glColor3f (1.0, 1.0, 1.0);glPushMatrix(); glColor4f(1.0,1.0,1.0,1.0); glutSolidTorus(0.001,1.14,30,30);//木星軌道 glPopMatrix();glPushMatrix(); glColor4f(1.0,1.0,1.0,1.0); glutSolidTorus(0.001,1.8,30,30);//地球軌道 glPopMatrix();glPushMatrix(); glColor4f(1.0,1.0,1.0,1.0); glutSolidTorus(0.001,2.47,30,30);//火星軌道 glPopMatrix();glPushMatrix(); glColor4f(1.0,1.0,1.0,1.0); glutSolidTorus(0.001,3.78,30,30);//木星軌道 glPopMatrix();glPushMatrix();day = (day + 5) % 360; // year0 = (year0 + 0) % 360;//太陽速度year1 = (year1 + 13) % 360;//水星速度year2 = (year2 + 6)% 360;//地球速度year3 = (year3 + 3)% 360;//火星速度year4 = (year4 + 1)% 360;//木星速度// glRotatef ((GLfloat) year0, 0.0, 0.0, 1.0);glColor3f(1.0,0.0,0.0);glutSolidSphere(0.95, 30, 30); //太陽glPopMatrix();glPushMatrix();glRotatef ((GLfloat) year1, 0.0, 0.0, 1.0);glTranslatef (0.0, 1.14, 0.0);glRotatef ((GLfloat) day, 0.0, 0.0, 1.0);glColor3f(1.00,0.89,0.4);//土黃色很淡glutSolidSphere(0.1, 30, 30); // 水星glPopMatrix();glPushMatrix();glRotatef ((GLfloat) year2, 0.0, 0.0, 1.0);glTranslatef (0.0, 1.8, 0.0);glRotatef ((GLfloat) day, 0.0, 0.0, 1.0);glColor3f(0.0,0.0,1.0);//藍色glutSolidSphere(0.3, 30, 30); //地球glPopMatrix();glPushMatrix();glRotatef ((GLfloat) year3, 0.0, 0.0, 1.0);glTranslatef (0.0,-2.47, 0.0);glRotatef ((GLfloat) day, 0.0, 0.0, 1.0);glColor3f(0.95,0.4,0.0);//紅黃色glutSolidSphere(0.15, 30, 30); //火星glPopMatrix();glPushMatrix();glRotatef ((GLfloat) year4/2, 0.0, 0.0, 1.0);glTranslatef (0.0, 3.78, 0.0);glRotatef ((GLfloat) day, 0.0, 0.0, 1.0);glColor3f(0.9,0.9,0.75);//黃灰色glutSolidSphere(0.55, 30, 30); // 木星glutPostRedisplay();glPopMatrix();glFlush ();glutSwapBuffers();Sleep(80);//延時0.06秒后執行 }void reshape (int w, int h) {glViewport (0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode (GL_PROJECTION);glLoadIdentity ();//變為單位矩陣gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);//透視圖if (w <= h)glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, 10.0, -10.0);elseglOrtho(1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt (0.0, 6.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0,1.0);//這里有9個參數,三個三個為一組,第一組表示眼睛的位置,第二組表示被觀察物的位置,第三組代表觀察者認為上的方向 }int main(int argc, char** argv) {glutInit(&argc, argv);//初始化opengl類庫glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);//若為GLUT_RGBA,則其中的A表示透明度//1.0為不透明,0.0為透明。Alpha測試在測試級別中最低。glutInitWindowSize (900, 800);glutInitWindowPosition (250, 50); // glutCreateWindow (argv[0]);//這一句顯示的就是文件位置glutCreateWindow ("太陽系");//init ();glutDisplayFunc(display);glutIdleFunc(display);//全局回調函數,當沒有窗口事件到達時,GLUT程序功//能可以執行后臺處理任務或連續動畫,啟動,會被不斷調用,直到有窗口事件發生glutReshapeFunc(reshape);//guidao();glutMainLoop();//讓main函數里面的代碼循環實現return 0; }結果圖如下:
(更改reshape 下的gluLookAt中的參數的值,有如下三種視圖)
俯視圖
側視圖
斜視圖
總結
- 上一篇: 井通区块链数据上链介绍
- 下一篇: 小程序获取数组索引