人教小學(xué)信息第二冊《圖形變換》課件_第1頁
人教小學(xué)信息第二冊《圖形變換》課件_第2頁
人教小學(xué)信息第二冊《圖形變換》課件_第3頁
人教小學(xué)信息第二冊《圖形變換》課件_第4頁
人教小學(xué)信息第二冊《圖形變換》課件_第5頁
已閱讀5頁,還剩117頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第三講 圖形變換 圖形變換是計算機圖形學(xué)的基礎(chǔ)內(nèi)容。有二維(三維)圖形的平移、旋轉(zhuǎn)、變比、對稱等變換,三維圖形的投影透視變換等。第6講 圖形變換 二維幾何變換基本變換與復(fù)合變換三維幾何變換基本變換與復(fù)合變換本講主要內(nèi)容圖形變換的數(shù)學(xué)基礎(chǔ)二維圖形的基本變換、復(fù)合(組合)變換;三維圖形基本變換、復(fù)合(組合)變換;平行投影透視投影圖形變換的數(shù)學(xué)基礎(chǔ)矢量運算矩陣運算矩陣單位矩陣逆矩陣轉(zhuǎn)置矩陣行列式 上機編程,實現(xiàn)兩個矩陣相乘特別注意:矩陣相乘不適合交換律變換的數(shù)學(xué)基礎(chǔ)(1/4) 矢量矢量和 變換的數(shù)學(xué)基礎(chǔ)(2/4)矢量的數(shù)乘 矢量的點積性質(zhì)變換的數(shù)學(xué)基礎(chǔ)(3/4)矢量的長度 單位矢量 矢量的夾角矢量的

2、叉積 變換的數(shù)學(xué)基礎(chǔ)(4/4)矩陣 階矩陣n階方陣零矩陣行向量與列向量單位矩陣矩陣的加法 矩陣的數(shù)乘 矩陣的乘法 矩陣的轉(zhuǎn)置 矩陣的逆 齊次坐標(biāo)用n+1維向量表示n維向量優(yōu)越性提供了用矩陣運算把二維三維甚至高維空間的點集從一個坐標(biāo)系變換到另一個坐標(biāo)系的方法??梢员硎緹o窮遠(yuǎn)的點圖形幾何變換基本原理:按某種規(guī)律,改變圖形的形狀、大小、位置等方法:坐標(biāo)系不動,圖形變動后坐標(biāo)值變化;坐標(biāo)系變化后圖形在新坐標(biāo)系中的新值。二. 圖形變換的過程建立物體的 WC變換到 VC在VC空間 進行裁剪投影到 NDC變換到 DC在圖形設(shè)備上輸出三. 圖形變換的特點 圖形變換就是改變圖形的幾何關(guān)系,即改變圖形頂點的坐標(biāo),

3、但圖形的拓?fù)潢P(guān)系不變。 最基本的圖形變換可以分別用矩陣形式表示為:平移變換 PPTm TmMx My Mx、My分別為X方 向和Y方向的平移量。比例變換 PPTs Sx 0 0 Sy Sx、Sy分別表示比例因子。旋轉(zhuǎn)變換 PPTr cos sin -sin cos 0時為逆時針旋轉(zhuǎn) 0時為順時針旋轉(zhuǎn)Ts Tr四. 齊次坐標(biāo) 從形式上來說,用一個有n+1個分量的向量去表示一個有n個分量的向量的方法稱為齊次坐標(biāo)表示。 例如二維平面上的點(x,y)的齊次坐標(biāo)表示為(hx,hy, h),h是任一不為0的比例系數(shù)。 給定一個點的齊次坐標(biāo)表示 : (x,y,h), 該點的二維笛卡兒直角坐標(biāo): (x / h

4、,y / h)。 同樣,對于一個三維空間的向量(x,y,z), 它在四維空間中對應(yīng)的向量即齊次坐標(biāo)為(xh,yh,zh,h),其中h0。 齊次坐標(biāo)的概念可以推廣到n維空間的向量。齊次坐標(biāo)的表示不是唯一的,通常當(dāng)h=1時,稱為規(guī)格化齊次坐標(biāo)。為什么需要齊次坐標(biāo)?多個變換作用于多個目標(biāo)變換合成變換合成的問題引入齊次坐標(biāo) 變換的表示法統(tǒng)一齊次坐標(biāo)表示的優(yōu)點: 可方便地用變換矩陣實現(xiàn)對圖形的變換;齊次坐標(biāo)表示法可以表達(dá)無窮遠(yuǎn)點。齊次坐標(biāo)二維圖形變換矩陣 一般形式: a b p c d q l m s P = P T2D(二維仿射變換: p=q=0) 二維變換矩陣中: a b c d l m 是對圖形進

5、行平移變換。 S 是整體比例變換。 (非仿射變換)p,q 用于投影變換(三維點時使用) x y 1 = x y 1 是對圖形進行縮放、旋轉(zhuǎn)、對稱、錯切等變換。二維圖形變換 采用齊次坐標(biāo)可將二維圖形變換表示成如下形式: a b 0 c d 0 l m 1 P = P M 二維變換矩陣中: a b c d l m 是對圖形進行平移變換 x y 1 = x y 1 變換后的頂點坐標(biāo)變換前的頂點坐標(biāo)二維變換矩陣是對圖形進行縮放、旋轉(zhuǎn)、對稱、錯切等變換。二維基本幾何變換點的變換:恒等變換平移變換比例變換旋轉(zhuǎn)變換對稱變換錯切變換平移變換 只改變圖形的位置,不改變圖形的大小和形狀二維:將圖形對象從一個位置(

6、x, y)移到另一個位置(x,y)的變換。P = P T1 0 0 0 1 0 dx dy 1T(dx, dy ) =平移矩陣1 0 0 0 1 0 dx - dy 1T-1 (dx, dy ) =平移變換 (x, y)(x, y)(dx, dy)xyd yydxxy x+=+=比例變換比例矩陣P = PSsx 0 0 0 sy 0 0 0 1S (sx, sy ) =1/sx 0 0 0 1/sy 0 0 0 1S-1 (sx, sy ) =比例變換示例(x, y)(x, y)xy比例變換比例因子 if sx , sy 1, 物體被拉伸 if 0 sx , sy 1, 物體被壓縮 if sx

7、 , sy 0,物體被倒影均勻/非均勻比例變換if sx = sy ,均勻比例變換if sx sy , 非均勻比例變換旋轉(zhuǎn)變換xyfq(x, y)(x, y)旋轉(zhuǎn)變換Remember旋轉(zhuǎn)方向旋轉(zhuǎn)角度旋轉(zhuǎn)中心旋轉(zhuǎn)是剛體變換xqP(x, y)P (x, y)y旋轉(zhuǎn)變換點P(x,y)的極坐標(biāo)表示繞坐標(biāo)原點旋轉(zhuǎn)角度 (逆時針為正,順時針為負(fù)) P = P Rcosq sinq 0 -sinq cosq 0 0 0 1R(q ) =旋轉(zhuǎn)矩陣cosq -sinq 0 sinq cosq 0 0 0 1R-1 (q ) =對稱變換對稱變換關(guān)于x軸的對稱變換關(guān)于y軸的對稱變換 對稱變換對稱變換關(guān)于原點的對稱變

8、換關(guān)于y=x的對稱變換 關(guān)于y=-x的對稱變換錯切變換以y軸為依賴軸的錯切變換 y坐標(biāo)不變點的基本幾何變換的推廣點的變換:直線的變換多邊形的變換曲線的變換(由每一點的變換,重新畫線完成)二維仿射變換 x=ax+by+m y =cx+dy+n P = P T二維復(fù)合變換(組合變換)任何一復(fù)雜的幾何變換可以看成基本集合變換的組合: P = PT = P T1 T2 Tn復(fù)合平移復(fù)合比例復(fù)合旋轉(zhuǎn)其他常見復(fù)合變換相對于某個參考點的幾何變換(比例、旋轉(zhuǎn)等)相對于某直線的幾何變換(對稱等)相對于某個參考點的幾何變換(比例、旋轉(zhuǎn)等)求某點P相對于固定點F(xF,yY)旋轉(zhuǎn)一個角度 的變換矩陣T。步驟:將固定

9、點移到坐標(biāo)原點上;對原點進行二維旋轉(zhuǎn)變換;反平移固定點到原位。例:求P(5,4)繞F(3,2)逆時針旋轉(zhuǎn)45度的變換矩陣,以及變換后P點的新坐標(biāo)P。相對于某直線的幾何變換(對稱等)步驟平移該對稱直線到原點;旋轉(zhuǎn)角度到與坐標(biāo)軸(X軸或Y軸)重合;對變換對象進行對稱變換;反向旋轉(zhuǎn)到原來方向;反平移到原來位置。關(guān)于任意軸的對稱變換 課堂練習(xí)(一)回答下列問題:什么是齊次坐標(biāo)?為什么要用規(guī)范化的齊次坐標(biāo)?什么是二維仿射變換?有哪些不變性?寫出它的變換形式(用矩陣表示)課堂練習(xí)(二)證明題證明兩個連續(xù)的旋轉(zhuǎn)變換(或變比例變換)的矩陣運算具有互換性。如,兩個二維旋轉(zhuǎn)變換R(1 ),R (2 ),具有下式

10、R(1 )R (2 ) = R(1 +2 ) 。證明二維點相對于X軸作對稱,緊跟著相對于y=-x直線作對稱變換,完全等價于該點相對于坐標(biāo)原點作旋轉(zhuǎn)變換(旋轉(zhuǎn)角度是多少?)課堂練習(xí)(三)推導(dǎo)題試推導(dǎo)將二維平面上任意直線段P1(x1,y1)與P1(x1,y1),轉(zhuǎn)換成與X軸重合的變換矩陣(線段P1P2與X軸的夾角小于45度)。求P點相對于L:ax+by+c=0,作對稱變換后得到的P。例如: 已知點P(8,12),直線L的方程為: x-2y+6=0, 請推導(dǎo)并計算該點相對于作對稱變換的矩陣T,使點P的對稱點 P=PT,并求出該點坐標(biāo)值。YXO已知點P(8,12),直線L的方程為:x-2y+6=0,

11、請推導(dǎo)并計算P相對于L作對稱變換的矩陣T,并求出該點坐標(biāo)值。三維圖形變換坐標(biāo)系 右手法則拇指指向坐標(biāo)軸Z的方向,其余四指指向旋轉(zhuǎn)方向 YZX0逆時針為正三維圖形變換三維空間點的齊次坐標(biāo)矩陣(x y z 1) x y z 1 = x y z 1 T三維圖形變換三維變換矩陣可表示為: a b c p d e f q g h i r l m n s 其中: a b c d e f 產(chǎn)生比例、錯切、鏡象和旋轉(zhuǎn)等基本變換。 g h i l m n 產(chǎn)生沿x、y、z三軸方向上的平移變換。 p q 產(chǎn)生透視變換。 r s 產(chǎn)生等比例縮放變換。T =三維圖形變換中要注意的幾個問題: 1. (三維)采用 s 來

12、實現(xiàn)整體的比例變換。 當(dāng) |s | 1 時,三維圖形整體等比例縮小。 2.(三維)對稱變換是相對于各個坐標(biāo)平面進行的。 3.(三維)旋轉(zhuǎn)變換是指繞坐標(biāo)軸的旋轉(zhuǎn)。 右手坐標(biāo)系下,繞坐標(biāo)軸逆時針為正 順時針旋轉(zhuǎn)為負(fù)。 4.三維圖形的級聯(lián)(組合)變換 對于復(fù)雜的三維圖形變換,也需要通過若干個變換矩陣的級聯(lián)才能實現(xiàn)。特別注意: 變換的方法和矩陣級聯(lián)的順序。三維幾何變換平移變換 (相對于原點)比例變換=1000000000000zyxsss(),zyxsssS三維幾何變換對稱變換 只考慮關(guān)于坐標(biāo)平面的對稱變換關(guān)于xy平面對稱關(guān)于yz平面對稱關(guān)于xz平面對稱=1000000000000-111=10000

13、0000000011-1=10000000000001-11錯切變換變換矩陣為:三維錯切變換矩陣: 1 b c 0 d 1 f 0 g h 1 0 0 0 0 1 其中:b=c=f=h=0,沿X方向產(chǎn)生錯切T =三維幾何變換旋轉(zhuǎn)變換需要指定旋轉(zhuǎn)角度和旋轉(zhuǎn)軸.yxzrotation axis(x, y, z)(x, y, z)三維幾何變換旋轉(zhuǎn)變換繞x軸(x, y, z)(x, y, z)xyz三維幾何變換旋轉(zhuǎn)變換繞y軸(x, y, z)(x, y, z)xyz三維幾何變換繞z軸xz(x, y, z)(x, y, z)幾點說明1)平移變換只改變圖形的位置,不改變圖形的大小和形狀;2)旋轉(zhuǎn)變換保持圖

14、形各部分間的線性關(guān)系和角度關(guān)系,變換后直線的長度不變;3)比例變換可改變圖形的大小和形狀;4)錯切變換引起圖形角度關(guān)系的改變,甚至導(dǎo)致圖形發(fā)生崎變;5) 拓?fù)洳蛔兊膸缀巫儞Q不改變圖形的連續(xù)關(guān)系和平行關(guān)系; 三維復(fù)合變換 實際的圖形對象的變換往往是由多個簡單變換復(fù)合而來。將相關(guān)的簡單變換的變換矩陣乘起來就可得到復(fù)合變換的變換矩陣。例:求基于參考點(xf, yf, zf)的比例變換,變換方法步驟: 1)通過平移變換將參考點移到原點,使原點與參考點重合; 2)相對于原點進行比例變換; 3)通過平移變換將參考點移原來位置;繞空間任意軸的三維旋轉(zhuǎn)變換。例:有空間任意軸AB(用點A: xA,yA,zA,方

15、向數(shù)a,b,c表示),現(xiàn)有空間點P(x,y,z)繞AB軸旋轉(zhuǎn)角后為P(x ,y ,z ),求該變換矩陣。方法步驟:1)平移AB軸與原點重合; 2)AB繞X軸旋轉(zhuǎn)角,使之落到ZX平面上; 3)將AB繞Y軸旋轉(zhuǎn)角,使之與Z軸重合; 4)此時AB與Z軸重合,繞Z軸旋轉(zhuǎn) 角; 5)繞X軸反旋轉(zhuǎn)-角; 6)繞X軸反旋轉(zhuǎn)-角;7)反平移。預(yù)備知識:方向數(shù)與各坐標(biāo)軸、坐標(biāo)平面的關(guān)系(夾角、投影等)B點的方向數(shù)為(2,3,4)YXZ繞X軸旋轉(zhuǎn)角;繞Y軸旋轉(zhuǎn)角。習(xí) 題7、在坐標(biāo)系oxyz中,求一個變換將P(1,1,1)Q(2,2,2)變換到z軸上:P在坐標(biāo)原點,Q在z軸正半軸。(方向數(shù)為:?)xyzPQoxyz

16、Qo(P)M隨堂練習(xí)寫出三維基本幾何變換的逆變換;(教材P213)將圖7-40中的物體ABCDEFGH進行如下變換:平移使C點與P(1,-1,0)重合,然后繞Z軸旋轉(zhuǎn)60度。將圖7-41中的空間四面體進行關(guān)于P點的整體放大,試寫出變換矩陣。假定空間直線AB兩端點坐標(biāo)為A(0,0,0)B(2,2,2),試寫出繞AB軸旋轉(zhuǎn)30度的三維復(fù)合變換矩陣。*在右手坐標(biāo)系中,從原點到P(x,y,z)給出一直線,用三種方法將此直線旋轉(zhuǎn)到正的軸線上,并用代數(shù)證明所得的結(jié)果等價。 a. 先繞X軸旋轉(zhuǎn)到XZ平面上,再繞Y軸到Z軸上; b. 先繞Y軸旋轉(zhuǎn)到Y(jié)Z平面上,再繞X軸到Z軸上; c. 先繞Z軸旋轉(zhuǎn)到XZ平面上

17、,再繞Y軸到Z軸上.二維圖形的顯示流程圖繞繞任意點的旋轉(zhuǎn)xqP(x, y)P (x, y)V = (Vx, Vy)y平移P點經(jīng)過矢量 -V.繞原點旋轉(zhuǎn) q.將P點平沿矢量V移回;Coordinate TransformationsWe can think about, alternatively, a transformation is as a change of coordinate systems.(x,y)(x,y)(dx, dy)(x, y)(dx, dy)Coordinate TransformationsObjects are usually defined in theirs

18、own local coordinate system.We wish to express these objects coordinates in a single, global coordinate.Object CoordinatesWorld CoordinatesScreen CoordinatesObject CoordinatesObjects are usually defined in their own local coordinate system.World CoordinatesRepresent these objects coordinates in a si

19、ngle, global coordinate.Screen CoordinatesFinally, we want to project these objects onto the screen.Coordinate HierarchyObject #1Object CoordinatesTransformationObject #1 -WorldObject #2Object CoordinatesTransformationObject #2 -WorldObject #3Object CoordinatesTransformationObject #3 -WorldWorld Coo

20、rdinatesTransformationWorld-ScreenScreen CoordinatesTransformations in OpenGLTransformations in OpenGLTransformations are specified by matrix operations. Desired transformation can be obtained by a sequence of simple transformations that can be concatenated together.Transformation matrix is usually

21、represented by 4x4 matrix (homogeneous coordinates).Provides matrix stacks for each type of supported matrix to store matrices.Transformation MatricesModel-viewing matrixProjection matrixTexture matrixOpenGL Transformation PipelineProgramming TransformationsIn OpenGL, the transformation matrices are

22、 part of the state, they must be defined prior to any vertices to which they are to apply.In modeling, we often have objects specified in their own coordinate systems and must use transformations to bring the objects into the scene.OpenGL provides matrix stacks for each type of supported matrix (mod

23、el-view, projection, texture) to store matrices.Steps in ProgrammingPrior to rendering, view, locate, and orient:Eye/camera position3D geometryManage the matricesIncluding matrix stackComposite transformationsCurrent Transformation MatrixCurrent Transformation Matrix (CTM) The matrix that is applied

24、 to any vertex that is defined subsequent to its setting. If change the CTM, we change the state of the system.CTM is a 4 x 4 matrix that can be altered by a set of functions.Current Transformation MatrixThe CTM can be set/reset/modify (by post-multiplication) by a matrixEx:C = M / set to matrix M C

25、 = CT / post-multiply by TC = CS / post-multiply by SC = CR / post-multiply by RCurrent Transformation MatrixEach transformation actually creates a new matrix that multiplies the CTM; the result, which becomes the new CTM.CTM contains the cumulative product of multiplying transformation matrices.Ex:

26、 If C = M; C = CT; C = CR; C = CSThen C = M T R S Ways to Specify TransformationsIn OpenGL, we usually have two styles of specifying transformations:Specify matrices ( glLoadMatrix, glMultMatrix )Specify operations ( glRotate, glTranslate )Specifying MatrixSpecify current matrix modeModify current m

27、atrixLoad current matrixMultiple current matrixSpecifying Matrix (1)Specify current matrix modeglMatrixMode (mode) Specified what transformation matrix is modified. mode: GL_MODELVIEW GL_PROJECTION GL_TEXTURESpecifying Matrix (2)Modify current matrix glLoadMatrixfd ( Type *m )Set the 16 values of cu

28、rrent matrix to those specified by m.Note: m is the 1D array of 16 elements arranged by the columns of the desired matrixSpecifying Matrix (3)Modify current matrix glLoadIdentity ( void )Set the currently modifiable matrix to the 4x4 identity matrix.Specifying Matrix (4)Modify current matrix glMultM

29、atrixfd ( Type *m )Multiple the matrix specified by the 16 values pointed by m by the current matrix, and stores the result as current matrix.Note: m is the 1D array of 16 elements arranged by the columns of the desired matrixSpecifying OperationsThree OpenGL operation routines for modeling transfor

30、mations:TranslationScaleRotationProblem Modify cube program to translate, scale, and rotate using glMultMatrix (changes commented out)Specifying Operations (1)TranslationglTranslate fd (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that translates an object by the given x, y, z.S

31、pecifying Operations (2)ScaleglScale fd (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that scales an object by the given x, y, z.Specifying Operations (3)RotateglRotate fd (TPE angle, TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that rotates an object in a c

32、ounterclockwise direction about the ray from origin through the point by the given x, y, z. The angle parameter specifies the angle of rotation in degree. ExampleLets examine an example:Rotation about an arbitrary pointQuestion:Rotate a object for a 45.0-degree about the line through the origin and

33、the point (1.0, 2.0, 3.0) with a fixed point of (4.0, 5.0, 6.0).Rotation About an Arbitrary PointTranslate object through vector V.T(-4.0, -5.0, -6.0)Rotate about the origin through angle q.R(45.0)Translate back through vector VT(4.0, 5.0, 6.0)M = T(V ) R(q ) T(-V )OpenGL ImplementationglMatrixMode

34、(GL_MODEVIEW);glLoadIdentity ();glTranslatef (4.0, 5.0, 6.0);glRotatef (45.0, 1.0, 2.0, 3.0);glTranslatef (-4.0, -5.0, -6.0);Order of TransformationsThe transformation matrices appear in reverse order to that in which the transformations are applied.In OpenGL, the transformation specified most recen

35、tly is the one applied first.Order of TransformationsIn each step:C = IC = CT(4.0, 5.0, 6.0)C = CR(45, 1.0, 2.0, 3.0)C First translate, then rotate =Composition of TransformationsWe can compose an overall transformation by applying several transformations in succession.Any composition of affine tran

36、sformations is still affine.When homogeneous coordinates are used, affine transformations are composed by simple matrix multiplication. ProblemDraw the car body.Translate to right front and draw wheel.Return back to car body.Translate to left front and draw wheel.Return back to car body.Always remem

37、ber where you are!Matrix StacksOpenGL uses matrix stacks mechanism to manage transformation hierarchy.OpenGL provides matrix stacks for each type of supported matrix to store matrices.Model-view matrix stackProjection matrix stackTexture matrix stackMatrix StacksTop Bottom PoppingPushingCurrent matr

38、ix is always the topmost matrix of the stackWe manipulate the current matrix is that we actually manipulate the topmost matrix.We can control the current matrix by using push and pop operations.Manipulating Matrix Stacks (1)Remember where you are glPushMatrix ( void )Pushes all matrices in the curre

39、nt stack down one level. The topmost matrix is copied, so its contents are duplicated in both the top and second-from-the top matrix.Note: current stack is determined by glMatrixModel()Manipulating Matrix Stacks (2)Go back to where you were glPopMatrix ( void )Pops the top matrix off the stack, dest

40、roying the contents of the popped matrix. What was the second-from-the top matrix becomes the top matrix.Note: current stack is determined by glMatrixModel()Manipulating Matrix Stacks (3)The depth of matrix stacks are implementation-dependent.The Modelview matrix stack is guaranteed to be at least 3

41、2 matrices deep.The Projection matrix stack is guaranteed to be at least 2 matrices deep. glGetIntegerv ( Glenum pname, Glint *parms )Pname:GL_MAX_MODELVIEW_STACT_DEPTHGL_MAX_PROJECTION_STACT_DEPTHDemonstration (look at code)Demonstration (look at code)Demonstration (look at code)Lets Examine Some E

42、xamplesQuestion 1:Draw a simple solar system with a planet and a sun.Sun rotates around its own axis (Y axis)The planet rotates around its own axis (Y axis)The planet also rotates on its orbit around the sun.Example-1Sun:Locates at the origin and rotates around its own axis (Y axis) M = Ry (q)Planet

43、:1. Rotates around its own axis. M1 = Ry (q) 2. Translate to its orbit. M2 = T (x, y, z) 3. Rotates around Sun. M3 = Ry (q) M = M3 M2 M1OpenGL Implementationvoid main (int argc, char* argv) glutInit ( &argc, argv ); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (500, 500); glutCre

44、ateWindow (Composite Modeling Transformation); init (); glutDisplayFunc (display); glutReshapeFunc (reshape); glutKeyboardFunc (keyboard); glutMainLoop ();OpenGL Implementationvoid init (void) glViewport(0, 0, 500, 500); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1, 1.0, 20.

45、0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef (0.0, 0.0, -5.0); / viewing transform glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);OpenGL Implementationvoid display(void) glClear(GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glPushMatrix(); / draw sun glPushMatrix(); gl

46、Rotatef (GLfloat) ang2, 0.0, 1.0, 0.0); glRotatef (90.0, 1.0, 0.0, 0.0); / rotate it upright glutWireSphere(1.0, 20, 16); / glut routine glPopMatrix();OpenGL Implementation (con.)/ draw smaller planet glRotatef (GLfloat) ang1, 0.0, 1.0, 0.0); glTranslatef (2.0, 0.0, 0.0); glRotatef (GLfloat) ang3, 0

47、.0, 1.0, 0.0); glRotatef (90.0, 1.0, 0.0, 0.0); / rotate it upright glutWireSphere(0.2, 10, 8); / glut routine glPopMatrix(); glutSwapBuffers();ResultLets Examine Some ExamplesQuestion 2: Draw a simple articulated robot arm with three segments. The arms should be connected with pivot points as the s

48、houlder, elbow, or other joints. (the three segments have same length, saying 2 units)Pivot pointsExample-2Red segment:1. Translates unit 1 to its pivot point. M1 = T (1, 0, 0) 2. Rotates around its pivot point. M2 = Ro (q)3. Translates 1 unit back to origin. M3 = T (-1, 0, 0) M = M3 M2 M1OpenGL Imp

49、lementationvoid display(void) glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); / draw shoulder (red) glTranslatef (-1.0, 0.0, 0.0); glRotatef (shoulder, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 0.1); glColor3f (1.0, 0.0, 0.0); glutSolidCube (1); / glut routine g

50、lPopMatrix();Example-2Green segment:1. Translates unit 1 to its pivot point. M1 = T (1, 0, 0) 2. Rotates around its pivot point. M2 = Ro (q)3. Translates 1 unit to the edge of the Red segment. M3 = T (1, 0, 0) M = M3 M2 M1OpenGL Implementationvoid display(void) / draw elbow glTranslatef (1.0, 0.0, 0

51、.0); glRotatef (elbow, 0.0, 0.0, 1.0); glTranslatef (1.0, 0.0, 0.0); glPushMatrix(); glScalef (2.0, 0.4, 0.1); glColor3f (0.0, 1.0, 0.0); glutSolidCube(1); / glut routine glPopMatrix();Example-2Yellow segment:1. Translates unit 1 to its pivot point. M1 = T (1, 0, 0) 2. Rotates around its pivot point

52、. M2 = Ro (q)3. Translates 1 unit to the edge of the Green segment. M3 = T (1, 0, 0) M = M3 M2 M1ResultProblemsModify the Sailboat program, utilizing matrix multiplication, to do the following:Translate it to the middle of the windowScale it to half sizeMake it spin about the :X axis left mouse butt

53、onY axis middle mouse buttonZ axis - right mouse buttonProblemsModify the Teapot program, utilizing rotate-scale-translate, to do the following:Translate it to the bottom of the windowScale it to 1/3 sizeRotate it 120 degrees about the x axisRememberAlways keep tracking your current position. Remember where you are, and go back to where you were.Matrix multiplication is not commutative, the transformation order is very important.In OpenGL, the transformation specified most recently is the one applied first.glu quadric PrimitivesQuadric primiti

溫馨提示

  • 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)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論