實驗三觀察三維物體_第1頁
實驗三觀察三維物體_第2頁
實驗三觀察三維物體_第3頁
實驗三觀察三維物體_第4頁
實驗三觀察三維物體_第5頁
已閱讀5頁,還剩34頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、實驗三觀察三維物體姓名 葉傳軍 學號E11414103得分閱讀arraycube.c,掌握彩色立方體的建模方法,為程序加注釋。修改arraycube.c,實現(xiàn)交互式地移動照相機來觀察已經(jīng)建模好的彩色立方體。即用鼠標 或鍵盤來改變 gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upziS數(shù)的 9 個參數(shù), 以此來觀察立方體。要求:交互時采用的鼠標和鍵盤按鍵自定。分別在正投影和透視投影下實現(xiàn)題目中的功能??稍黾硬藛喂δ埽瑢⒄队昂屯敢曂队跋碌挠^察功能融合到一個程序中。可參考交互 式教程Projection.c的功能。在arraycube.

2、c的基礎(chǔ)上編寫一個交互式程序,實現(xiàn)立方體的旋轉(zhuǎn)。具體要求如下:(1)立方體的旋轉(zhuǎn)方式由鼠標和鍵盤按鍵來控制:按下鼠標左鍵,立方體繞x軸連續(xù)旋轉(zhuǎn); 按下鼠標左鍵+ctrl鍵,立方體繞y軸連續(xù)旋轉(zhuǎn);按下鼠標右鍵,立方體繞z軸連續(xù)旋轉(zhuǎn)。(注意:旋轉(zhuǎn)的不動點在原點,正好是立方體的中心。)(2 )如果旋轉(zhuǎn)的不動點不在原點,而改為點P (1,1,1),如何實現(xiàn)立方體繞3個坐標軸軸 的旋轉(zhuǎn)?(3 )如果要求每按下一次鼠標按鍵或鍵盤按鍵,立方體旋轉(zhuǎn)的角度增加5度,應(yīng)如何修改 程序?自學教材4.2.7節(jié)。編寫一個交互式程序,使其可以通過鼠標(或鍵盤)和菜單實現(xiàn)一些glu和glut對象的旋轉(zhuǎn),平移和比例縮放。(加

3、自己的創(chuàng)意)二設(shè)計思想本次試驗主要是用戶交互與三維物體觀察的結(jié)合,主要用到的函數(shù)有g(shù)luPerspective(fovy, aspect, near, far);glOrtho(left, right, bottom, top, near, far);gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);通過這些函數(shù)的調(diào)用可以分別在正投影和透視投影下從不同的角度來觀察三維物體。三程序清單1. #includestdafx.h#include #include GLdouble vertices83= -1.0, -1.0, 1.0,

4、?-1.0,1.0,1.0,1.0,1.0,1.0,?1.0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0, 1.0,1.0,-1.0, 1.0,-1.0,-1.0;GLdouble colors83=0.0,0.0,0.0, 1.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0, 0.0,0.0,1.0,1.0,0.0,1.0, 1.0,1.0,1.0,0.0,1.0,1.0;/定義頂點和顏色的全局數(shù)組void polygon(int a, int b, int c , int d)/根據(jù)索引列表繪制多邊形/* draw a polygon v

5、ia list of vertices */glBegin(GL_QUADS);glColor3dv(colorsa);glVertex3dv(verticesa);glColor3dv(colorsb);glVertex3dv(verticesb);glColor3dv(colorsc);glVertex3dv(verticesc);glColor3dv(colorsd);glVertex3dv(verticesd);glEnd();void colorcube()/* map vertices to faces */利用表面繪制立方體polygon(0,3,2,1);polygon(2,3

6、,7,6);polygon(0,4,7,3);polygon(1,2,6,5);polygon(4,5,6,7);polygon(0,1,5,4);void display()/* display callback, clear frame buffer and z buffer,/and draw, swap buffers */顯示回調(diào)函數(shù)glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(1.0, 1.0, 1.0, 0.0, 0.

7、0, 0.0, 0.0, 1.0, 0.0);colorcube();glutSwapBuffers(); void myReshape(int w, int h)設(shè)置投影方式glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w = h)glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);elseglOrtho(-2.0 * (GLfloat) w / (GL

8、float) h,2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);glMatrixMode(GL_MODELVIEW);void main(int argc, char *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);/設(shè)置窗口的顯示模式glutInitWindowSize(500, 500);/指定窗口大小glutCreateWindow(E11414103 葉傳軍);/創(chuàng)建一個名為E11414103 葉傳

9、軍的 窗口glClearColor(0.0,0.0,0.0,0.0);/指 定窗口的背景色為黑色/glShadeModel(GL_SMOOTH);glShadeModel(GL_FLAT);glutReshapeFunc(myReshape);glutDisplayFunc(display);/設(shè)置當前窗口的顯示回調(diào)函數(shù)/glEnable(GL_DEPTH_TEST); /* Enable hidden-surface-removal */glutMainLoop();/啟動主GLUT事件處理循環(huán)2.(1)#includestdafx.h#include #include GLdouble v

10、ertices83 =(-1.0, -1.0, 1.0,-1.0,1.0,1.0,(1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0, 1.0,1.0,-1.0, 1.0,-1.0,-1.0;GLdouble colors83=0.0,0.0,0.0, 1.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0, 0.0,0.0,1.0,1.0,0.0,1.0, 1.0,1.0,1.0,0.0,1.0,1.0;/定義頂點和顏色的全局數(shù)組int x1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3 = 1

11、,z3=0;void polygon(int a, int b, int c , int d)/根據(jù)索引列表繪制多邊形/* draw a polygon via list of vertices */glBegin(GL_QUADS);glColor3dv(colorsa);glVertex3dv(verticesa);glColor3dv(colorsb);glVertex3dv(verticesb);glColor3dv(colorsc);glVertex3dv(verticesc);glColor3dv(colorsd);glVertex3dv(verticesd);glEnd();vo

12、id colorcube()/利用表面繪制立方體/* map vertices to faces */polygon(0,3,2,1);polygon(2,3,7,6);polygon(0,4,7,3);polygon(1,2,6,5);polygon(4,5,6,7);polygon(0,1,5,4);void display()/顯示回調(diào)函數(shù)/* display callback, clear frame buffer and z buffer,/and draw, swap buffers */glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_B

13、IT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x1, y1, z1, x2, y2, z2, x3, y3, z3);colorcube();glutSwapBuffers();void myReshape(int w, int h)設(shè)置投影方式正投影glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w = h)glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GL

14、float) h / (GLfloat) w, -10.0, 10.0);elseglOrtho(-2.0 * (GLfloat) w / (GLfloat) h,2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);glMatrixMode(GL_MODELVIEW); void keyboard(unsigned char key,int x,int y) if(key= = q)x1+;glutPostRedisplay();else if(key= = Q)x1-;glutPostRedisplay();else if(key

15、= = w)y1+;glutPostRedisplay();else if(key= = W)y1-;glutPostRedisplay();else if(key= = e)z1+;glutPostRedisplay();else if(key= = E)z1-;glutPostRedisplay();else if(key= = a)x2+;glutPostRedisplay();else if(key= = A)x2-;glutPostRedisplay();else if(key= = s)y2+;glutPostRedisplay();else if(key= = S)y2-;glu

16、tPostRedisplay();else if(key= = d)z2+;glutPostRedisplay();else if(key= = D)z2-;glutPostRedisplay();else if(key= = z)x3 + +;glutPostRedisplay();else if(key= = Z)z3-;glutPostRedisplay();else if(key= = x)y3+;glutPostRedisplay();else if(key= = X)y3-;glutPostRedisplay();else if(key= = c)z3 + +;glutPostRe

17、display();else if(key= = C)z3-; glutPostRedisplay();void main(int argc, char *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);/設(shè)置窗口的 顯示模式glutInitWindowSize(500, 500);/指定窗口大小glutCreateWindow(E11414103 葉傳軍);/創(chuàng)建一個名為E11414103 葉傳 軍的窗口glClearColor(0.0,0.0,0.0,0.0);/指 定窗口的背景

18、色為黑色/glShadeModel(GL_SMOOTH);glShadeModel(GL_FLAT);glutReshapeFunc(myReshape);glutDisplayFunc(display);/設(shè)置當前窗口的顯示回調(diào)函數(shù)glutKeyboardFunc(keyboard);/glEnable(GL_DEPTH_TEST); /* Enable hidden-surface-removal */g_uiMa5LOOP()7、mH+GLUTJHHiE11414103葉傳軍 g 回(2)#includestdafx.h#include #include GLdouble vertice

19、s83 =-1.0, -1.0, 1.0,-1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0, 1.0,1.0,-1.0, 1.0,-1.0,-1.0;GLdouble colors83=0.0,0.0,0.0, 1.0,0.0,0.0,1.0,1.0,0.0,0.0,1.0,0.0, 0.0,0.0,1.0,1.0,0.0,1.0, 1.0,1.0,1.0,0.0,1.0,1.0;/定義頂點和顏色的全局數(shù)組int x1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3 = 1,z3=0;vo

20、id polygon(int a, int b, int c , int d)/根據(jù)索引列表繪制多邊形/* draw a polygon via list of vertices */glBegin(GL_QUADS);glColor3dv(colorsa);glVertex3dv(verticesa);glColor3dv(colorsb);glVertex3dv(verticesb);glColor3dv(colorsc);glVertex3dv(verticesc);glColor3dv(colorsd);glVertex3dv(verticesd);glEnd();void color

21、cube()/利用表面繪制立方體/* map vertices to faces */polygon(0,3,2,1);polygon(2,3,7,6);polygon(0,4,7,3);polygon(1,2,6,5);polygon(4,5,6,7);polygon(0,1,5,4);void display()/顯示回調(diào)函數(shù)/* display callback, clear frame buffer and z buffer,/and draw, swap buffers */glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMa

22、trixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(x1, y1, z1, x2, y2, z2, x3, y3, z3);colorcube();glutSwapBuffers();void myReshape(int w, int h)設(shè)置投影方式透視投影glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w = h)glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat

23、) h / (GLfloat) w, -10.0, 10.0);elseglFrustum(-2.0 * (GLfloat) w / (GLfloat) h,2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);glMatrixMode(GL_MODELVIEW); void keyboard(unsigned char key,int x,int y)if(key= = q)x1+;glutPostRedisplay();else if(key= = Q)x1-;glutPostRedisplay();else if(key= =

24、w)y1+;glutPostRedisplay();else if(key= = W)y1-;glutPostRedisplay();else if(key= = e)z1+;glutPostRedisplay();else if(key= = E)z1-;glutPostRedisplay();else if(key= = a)x2+;glutPostRedisplay();else if(key= = A)x2-;glutPostRedisplay();else if(key= = s)y2+;glutPostRedisplay();else if(key= = S)y2-;glutPostRedisplay();else if(key= = d)z2+;glutPostRedisplay();else if(key= =

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論