實(shí)驗(yàn)三 幾何圖形變換實(shí)驗(yàn)_第1頁(yè)
實(shí)驗(yàn)三 幾何圖形變換實(shí)驗(yàn)_第2頁(yè)
實(shí)驗(yàn)三 幾何圖形變換實(shí)驗(yàn)_第3頁(yè)
實(shí)驗(yàn)三 幾何圖形變換實(shí)驗(yàn)_第4頁(yè)
實(shí)驗(yàn)三 幾何圖形變換實(shí)驗(yàn)_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)三 幾何圖形變換實(shí)驗(yàn)實(shí)驗(yàn)項(xiàng)目性質(zhì):設(shè)計(jì)性實(shí)驗(yàn)所屬課程名稱:3D游戲圖形學(xué)實(shí)驗(yàn)計(jì)劃學(xué)時(shí):3學(xué)時(shí)一、 實(shí)驗(yàn)?zāi)康暮鸵?. 進(jìn)一步掌握二維、三維變換的數(shù)學(xué)知識(shí)、變換原理、變換種類、變換方法;2. 利用OpenGL實(shí)現(xiàn)二維、三維圖形變換,在屏幕上顯示變換過(guò)程或變換結(jié)果;3. 掌握OpenGL常用的變換函數(shù)。二、 實(shí)驗(yàn)原理OpenGL的三個(gè)基本幾何變換函數(shù)介紹如下:1. 平移變換平移變換函數(shù)如下:void glTranslate fd (TYPE x, TYPE y, TYPE z);三個(gè)函數(shù)參數(shù)就是目標(biāo)分別沿三個(gè)軸向平移的偏移量。這個(gè)函數(shù)表示用這三個(gè)偏移量生成的矩陣乘以當(dāng)前矩陣。2. 旋轉(zhuǎn)變換旋轉(zhuǎn)

2、變換函數(shù)如下: void glRotate fd (TYPE angle, TYPE x, TYPE y, TYPE z);函數(shù)中第一個(gè)參數(shù)是表示目標(biāo)沿從點(diǎn)(x, y, z)到原點(diǎn)的方向逆時(shí)針旋轉(zhuǎn)的角度,后三個(gè)參數(shù)是旋轉(zhuǎn)的方向點(diǎn)坐標(biāo)。這個(gè)函數(shù)表示用這四個(gè)參數(shù)生成的矩陣乘以當(dāng)前矩陣。3. 比例變換比例變換函數(shù)如下: void glScale fd (TYPE x, TYPE y, TYPE z);三個(gè)函數(shù)參數(shù)值就是目標(biāo)分別沿三個(gè)軸向縮放的比例因子。這個(gè)函數(shù)表示用這三個(gè)比例因子生成的矩形乘以當(dāng)前矩陣。這個(gè)函數(shù)能完成沿相應(yīng)的軸對(duì)目標(biāo)進(jìn)行拉伸、壓縮和反射三項(xiàng)功能。以參數(shù)x為例,若當(dāng)x大于1.0時(shí),表示

3、沿x軸方向拉伸目標(biāo);若x小于1.0時(shí),表示沿x軸方向收縮目標(biāo);若x=-1.0表示沿x軸反射目標(biāo)。4. 投影變換1)OpenGL平行投影函數(shù)共有兩個(gè):一個(gè)函數(shù)是:void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)它創(chuàng)建一個(gè)平行視景體。實(shí)際上這個(gè)函數(shù)的操作是創(chuàng)建一個(gè)平行投影矩陣,并且用這個(gè)矩陣乘以當(dāng)前矩陣。其中近裁剪平面是一個(gè)矩形,矩形左下角點(diǎn)三維空間坐標(biāo)是(left,bottom,-near),右上角點(diǎn)是(right,top,-near);遠(yuǎn)

4、裁剪平面也是一個(gè)矩形,左下角點(diǎn)空間坐標(biāo)是(left,bottom,-far),右上角點(diǎn)是(right,top,-far)。所有的near和far值同時(shí)為正或同時(shí)為負(fù)。如果沒(méi)有其他變換,正射投影的方向平行于Z軸,且視點(diǎn)朝向Z負(fù)軸。這意味著物體在視點(diǎn)前面時(shí)far和near都為負(fù)值,物體在視點(diǎn)后面時(shí)far和near都為正值。 另一個(gè)函數(shù)是:void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)它是一個(gè)特殊的正射投影函數(shù),主要用于二維圖像到二維屏幕上的投影。它的near和far缺省值分別為-1.0和1.

5、0,所有二維物體的Z坐標(biāo)都為0.0。因此它的裁剪面是一個(gè)左下角點(diǎn)為(left,bottom)、右上角點(diǎn)為(right,top)的矩形。2)OpenGL透視投影函數(shù)共有兩個(gè):void glFrustum(GLdouble left, GLdouble Right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);它創(chuàng)建一個(gè)透視視景體。其操作是創(chuàng)建一個(gè)透視投影矩陣,并且用這個(gè)矩陣乘以當(dāng)前矩陣。這個(gè)函數(shù)的參數(shù)只定義近裁剪平面的左下角點(diǎn)和右上角點(diǎn)的三維空間坐標(biāo),即(left,bottom,-near)和(right,top,-ne

6、ar);最后一個(gè)參數(shù)far是遠(yuǎn)裁剪平面的Z負(fù)值,其左下角點(diǎn)和右上角點(diǎn)空間坐標(biāo)由函數(shù)根據(jù)透視投影原理自動(dòng)生成。near和far表示離視點(diǎn)的遠(yuǎn)近,它們總為正值。另一個(gè)函數(shù)是:void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);它也創(chuàng)建一個(gè)對(duì)稱透視視景體,但它的參數(shù)定義于前面的不同。其操作是創(chuàng)建一個(gè)對(duì)稱的透視投影矩陣,并且用這個(gè)矩陣乘以當(dāng)前矩陣。參數(shù)fovy定義視野在X-Z平面的角度,范圍是0.0,180.0;參數(shù)aspect是投影平面寬度與高度的比率;參數(shù)zNear和Far分別是遠(yuǎn)近裁

7、剪面沿Z負(fù)軸到視點(diǎn)的距離,它們總為正值。三、 實(shí)驗(yàn)內(nèi)容1、下面的代碼采用GLUT庫(kù),使用了雙緩存,在按下鼠標(biāo)左鍵后,程序在空閑時(shí)一直不停地調(diào)用spinDisplay函數(shù),實(shí)現(xiàn)了一個(gè)矩形在窗口中勻速轉(zhuǎn)動(dòng)(單擊鼠標(biāo)右鍵停止轉(zhuǎn)動(dòng))。請(qǐng)修改代碼,實(shí)現(xiàn)矩形在窗口內(nèi)沿著水平線移動(dòng)。/* * double.c * This is a simple double buffered program. * Pressing the left mouse button rotates the rectangle. * Pressing the right mouse button stops the rotatio

8、n. */#include #include static GLfloat spin = 0.0;void display(void) glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glRectf(-10.0, -10.0, 10.0, 10.0); glPopMatrix(); glutSwapBuffers(); /交換雙緩存void spinDisplay(void) spin = spin + 2.0; if (spin 36

9、0.0) spin = spin - 360.0; glutPostRedisplay();void init(void) glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);void reshape(int w, int h) glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL

10、_MODELVIEW); glLoadIdentity();void mouse(int button, int state, int x, int y) switch (button) case GLUT_LEFT_BUTTON: if (state = GLUT_DOWN) glutIdleFunc(spinDisplay); /設(shè)備空閑時(shí)調(diào)用的函數(shù) break; case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state = GLUT_DOWN) glutIdleFunc(NULL); break; default: break;

11、 /* * Request double buffer display mode. * Register mouse input callback functions */int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /使用雙緩存模式 glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv0); init (); glut

12、DisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); return 0; /* ANSI C requires main to return int. */2. 已知某三角形的三頂點(diǎn)坐標(biāo)50.0,25.0,150.0,25.0,100.0,100.0。要求:(1)創(chuàng)建一個(gè)長(zhǎng)寬分別為600、600的窗口,窗口的左上角位于屏幕坐標(biāo)(100,100)處。(提示:請(qǐng)?jiān)囍薷膅luOrtho2D函數(shù)參數(shù),使得繪制的三角形盡可能居中顯示)(2)繪制一個(gè)由上述頂點(diǎn)所描繪的三角形,實(shí)現(xiàn)該三角形

13、進(jìn)行下列的幾何變化:首先使三角形沿著其中心的x軸,y軸方向縮小50%;然后沿著初始中心旋轉(zhuǎn)90度;最后沿著y軸平移100個(gè)單位。參考代碼:#include #include void init(void) glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);void draw_triangle(void) glBegin (GL_LINE_LOOP); glVertex2f(0.0, 25.0); glVertex2f(25.0, -25.0); glVertex2f(-25.0, -25.0); glEnd();void dis

14、play(void) glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); glColor3f (1.0, 1.0, 1.0); draw_triangle (); glEnable (GL_LINE_STIPPLE); glLineStipple (1, 0xF0F0); glLoadIdentity (); glTranslatef (-20.0, 0.0, 0.0); draw_triangle (); glLineStipple (1, 0xF00F); glLoadIdentity (

15、); glScalef (1.5, 0.5, 1.0); draw_triangle (); glLineStipple (1, 0x8888); glLoadIdentity (); glRotatef (90.0, 0.0, 0.0, 1.0); draw_triangle (); glDisable (GL_LINE_STIPPLE); glFlush ();void reshape (int w, int h) glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentit

16、y (); if (w = h) gluOrtho2D (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w, 50.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D (-50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0); glMatrixMode(GL_MODELVIEW);int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGL

17、E | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv0); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0;3. 繪制一個(gè)三棱椎的透視投影圖,要求:實(shí)現(xiàn)勻速旋轉(zhuǎn)(提示:參考題目1,使用雙緩存模式);改變變換方式,達(dá)到不同的效果。參考代碼:#include#includevoid init(void)glClearColo

18、r(0.0f,0.0f,0.0f,0.0f);glShadeModel(GL_SMOOTH); /設(shè)為smooth處理方式glEnable(GL_DEPTH_TEST); /激活深度測(cè)試(可以去掉,看看效果)void display(void)glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /清除顏色緩存和深度緩存glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); /試嘗試修改此函數(shù),掌握其用法與參數(shù)含義 glRotatef(0.0f,0.0f,1.0f,0.0f); /試嘗試修改此函數(shù),掌握其

19、用法與參數(shù)含義glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);gl

20、Color3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,0.0f,1.0f);glVe

21、rtex3f(-1.0f,-1.0f,-1.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);glEnd();glFlush();void reshape(int width, int height)glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW)

22、;glLoadIdentity();void keyboard(unsigned char key, int x, int y)switch(key)case x:exit(0);break;default:break;int main(int argc, char * *argv)glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(640,480);glutInitWindowPosition(100,100);glutCreateWindow(Transf

23、orm2);init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMainLoop();return 0;4、 實(shí)驗(yàn)代碼1. 實(shí)驗(yàn)代碼:#include #include static GLfloat spin = 0.0;void display(void) glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glTranslatef(spin, 0.0, 0.0); glColor3f(1.0, 1.0, 1.0); glRect

24、f(-10.0, -10.0, 10.0, 10.0); glPopMatrix(); glutSwapBuffers(); /交換雙緩存void spinDisplay(void) spin = spin + 0.02; if (spin 50.0) spin = spin - 100.0; glutPostRedisplay();void init(void) glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);void reshape(int w, int h) glViewport (0, 0, (GLsizei) w,

25、(GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();void mouse(int button, int state, int x, int y) switch (button) case GLUT_LEFT_BUTTON: if (state = GLUT_DOWN) glutIdleFunc(spinDisplay); /設(shè)備空閑時(shí)調(diào)用的函數(shù)

26、 break; case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state = GLUT_DOWN) glutIdleFunc(NULL); break; default: break; int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /使用雙緩存模式 glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); gl

27、utCreateWindow (argv0); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); return 0; 運(yùn)行結(jié)果:2. 實(shí)驗(yàn)代碼:#include #include void init(void) glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);void draw_triangle(void) glBegin (GL_LINE_LOOP); glVertex2f(25

28、.0, 25.0); glVertex2f(100.0, 100.0); glVertex2f(150.0, 25.0); glEnd();void display(void) glClear (GL_COLOR_BUFFER_BIT); glLoadIdentity(); glColor3f(0.0,1.0,0.0); draw_triangle(); glEnable(GL_LINE_STIPPLE); glLineStipple(1,0xF0F0); glLoadIdentity(); glTranslatef(100.0,63.0,0.0); glTranslatef(0.0,100.

29、0,0.0); glRotatef(90.0,0.0,0.0,1.0); glScalef(0.5,0.5,1.0); glTranslatef(-100.0,-63.0,0.0); glColor3f(0.0,255.0,1.0); draw_triangle(); glDisable(GL_LINE_STIPPLE); glFlush();void reshape (int w, int h) glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w

30、 = h) gluOrtho2D (-300.0, 500.0, -300.0*(GLfloat)h/(GLfloat)w, 500.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D (-50.0*(GLfloat)w/(GLfloat)h, 50.0*(GLfloat)w/(GLfloat)h,-50.0, 50.0); glMatrixMode(GL_MODELVIEW);int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT

31、_RGB|GLUT_DEPTH); glutInitWindowSize (600, 600); glutInitWindowPosition (100, 100); glutCreateWindow (argv0); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); return 0;3. 實(shí)驗(yàn)代碼:#include#includestatic GLfloat spin=0.0;void init(void)glClearColor(0.0f,0.0f,0.0f,0.0f);glShade

32、Model(GL_SMOOTH); /設(shè)為smooth處理方式glEnable(GL_DEPTH_TEST); /激活深度測(cè)試(可以去掉,看看效果)void display(void)glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /清除顏色緩存和深度緩存glPushMatrix();glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); /試嘗試修改此函數(shù),掌握其用法與參數(shù)含義 glRotatef(spin,0.0f,5.0f,0.0f); /試嘗試修改此函數(shù),掌握其用法與參數(shù)含義glBegin(

33、GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);glColor3f(1.0f,0.0f,0

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論