第 1 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Phép biến đổi - Transformations Bài 4 Các phép biến đổi Đồ hoạ Transformations z Trong kỹ thuật đồ hoạ 3 bước: Mô hình, Tô trát và Hiên thị (modeling, rendering, displaying) z Với Modeling ( Mô hình hóa) : modeling world viewing coordinate Modeling coordinate Viewing coordinate (eye transformation transformation coordinate) Le Tan Hung Email: hunglt@it-hut.edu.vn z Transformation: là phép ánh xạ tọa độ điểm hay vector thành tọa độ hay vector khác 1 2 Phép biến đổi Transformations Transformations - Modeling Biến đổi mô hình hoá - Modeling transformations world z – build complex models by positioning simple components z Biến đổi tạo góc nhìn - Viewing transformations – placing virtual camera in the world – transformation from world coordinates to camera coordinates z Biến Phép chiếu – Projection Transform 3 4 Phép biến đổi Affine Affine Transformations? Modeling Transformations z Phép biến đổi Affine là phép biến đổi tọa độ Transform Transform coordinate system điểm đặc trưng của đối tượng thành tập tương objects/points ứng các điểm mới để tạo ra các hiệu ứng cho toàn đối tượng. – Ví dụ: phép biến đổi tọa độ với chỉ 2 điểm đầu cuối của đoạn thẳng tạo thành 2 điểm mới mà khi nối chúng với nhau tạo thành đoạn thẳng mới. z Các điểm nằm trên đoạn thẳng sẽ có kết quả là điểm nằm trên đoạn thẳng mới với cùng phép biến đổi thông qua phép nội suy. 5 6 goupee.com fb.com/groups/goupee 1
第 2 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Biểu diễn Ma trận Matrix Representation Việc biến đối các đối tượng làm thay đổi các z Phép biến đổi đồ họa - affine transformation T ánh xạ z tập P sang tập Q: điểm P thành các điểm Q theo thuật toán Q = aP + bP + t – x x x x z Việc biến đổi P sử dụng tọa độ của P (Px,Py) ánh – Q = cP + dP + t xạ thành các tọa độ mới Q (Qx,Qy) y y y y Việc biến đổi có thể biểu diễn thông qua hàm T, – where a, b, c, d, tx and ty là các hệ số z Biểu diễn ma trận: hàm ánh xạ của điểm: z T(P ,P ) = (Q ,Q ) ⎛ Q ⎞ ⎛ a b ⎞⎛ P ⎞ ⎛ t ⎞ – x y x y ⎜ x ⎟ = ⎜ ⎟⎜ x ⎟ + ⎜ x ⎟ or: ⎜ Q ⎟ ⎜ c d ⎟⎜ P ⎟ ⎜ t ⎟ – i.e. ⎝ y ⎠ ⎝ ⎠⎝ y ⎠ ⎝ y ⎠ z – T(P) = Q Q = MP + Tr 7 8 Các phép biến đổi hình học hai chiều Phép biến đổi y z Phương pháp biểu diễn đối tượng P = [ x y ] ⎡1 0⎤ z Phép bất biến T = ⎢ ⎥ z Phép biến đổi vị trí điểm ⎡a b⎤ ⎣0 1⎦ T = ⎢ ⎥ z x ⎣c d⎦ Thực thi phép biến đổi đúng trên 1 điểm ảnh sẽ đúng z Phép biến đổi tỉ lệ - Scaling z trên toàn bộ đối tượng z A scaling changes the size of an object with two scale ⎡a b ⎤ [ ] factors, Sx and Sy [X ] [ ]* T = [x y]* ⎢ ⎥ = [(ax + cy ) (bx + dy )]= x' y' [ ] [ ] [ ] ⎡a 0⎤ [ ] [ ] ⎣c d ⎦ X * T = x y *⎢ ⎥ = (ax) y = x' y' y ⎣0 1⎦ [ ] [ ] [ ] ⎡1 b⎤ [ ] [ ] p z Phép biến dạng X * T = x y *⎢ ⎥ = bx+ dy = x' y' p W ⎣0 1⎦ M A shearing shears an object in a particular direction, (in z x z 2D, it’s either in the x or in the y direction 9 10 Thuộc tính cơ bản của phép biến Phép quay- Rotation đổi Affine Transformations x = ρ cos α, y = ρ sin α ; Preservation of lines: z x’ = ρ cos (θ +α ), y’ = ρ sin (θ +α ) ; y x’ = ρ ( cosθ cosα - sinθ sinα ) – They preserve lines, so the image of a straight line is ( x’, y’ ) another straight line. = x cosθ - y sinθ y’ = ρ ( sinθ cosα + cosθ sinα ) – This vastly simplifies drawing transformed line = x sinθ + y cosθ segments. ρ [x' y']= [xcosθ - ysinθ xsinθ + ycosθ] ( x, y ) – We need only compute the image of the two endpoints θ ρ of the original line and then draw a straight line between α x them ⎡ cosθ sinθ ⎤ – Preservation of collinearity guarantees that polygons will [T] = ⎢ ⎥ ⎣− sinθ cosθ⎦ transform into polygons – Affine transformations map lines to lines; 11 12 goupee.com fb.com/groups/goupee 2
第 3 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Thuộc tính Kết hợp các phép biến đổi Composition of Affine Transforms Preservation of parallelism z Any affine transformation can be z decomposed into elementary – Preservation of parallelism guarantees that transformations. parallelograms will transform into parallelograms Mọi phép biến đổi phức tạp đều z z Preservation of proportional distances có thể tạo thành từ các phép biến – Preservation of proportional distances means that mid- đổi cơ sở như: points of lines remain mid-points – Dịch chuyển - Translation z Affine transformations change volume by | – Tỉ lệ - Scaling Det(M) |; – Quay- Rotation – Biến dạng - Shearing 13 14 Affine transformations preserve affine combinations Thuộc tính It is rare that we want to perform just one elementary T z transformation. z Usually an application requires that we build a complex transformation out of several elementary Tác động lên tập các điểm đặc trưng của đối ones z tượng tạo thành phép biến đổi cho đối tượng – e.g. translate an object, rotate it, and scale it, all in one move These individual transformations combine into one z We have defined each transformation by their effects on z overall transformation single points This is called the composition of transformations. z In practice these will be applied to multiple points to z transfer entire scenes or objects made up of many z The composition of two or more affine transformations defining points is also an affine transformation 15 16 Điểm gốc - Pivotal points Pivotal points Cho phép quay và tỉ lệ Rotation and Scaling z The simple versions of rotation and scaling have been based around the z Often we wish to rotate or scale with respect to some origin. pivotal point, not the origin z This means that when we rotate or scale, the object will also move, with z Most significantly, we often wish to rotate or scale an respect to the origin object about its centre, or midpoint z Translate all points through (-c1,-c2) z In this way, the object’s location does not change z Rotate all points about the origin by To do this, we relate the rotation or scaling about the z z Translate all points back through (c1,c2) pivotal point V, to an elementary rotation or scaling about the origin – We first translate all points so that V coincides with the origin (c ,c ) – We then rotate or about the origin 1 2 – then all points are translated back, so that V is restored to its original location (0,0) 17 18 goupee.com fb.com/groups/goupee 3
第 4 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Hệ toạ độ đồng nhất Tọa độ đồng nhất Homogeneous Transform z Vấn đề gặp phải: – x' = ax + by + n z An affine transformation is composed of a linear – y' = cx + dy + m transformation followed by a translation z Phương pháp biểu diễn mở rộng thông qua z Unfortunately, the translation portion is not a tọa độ đồng nhất của các vector vị trí matrix multiplication but must instead be added as an extra term, or vector What we need is a “trick”, so that translations can z Với ứng dụng của phép chiếu hình học mà z be represented in matrix multiplication form ở đó tọa độ điểm được mô tả dưới ma trận [ z This then means that they can be easily x* y* h] composed with other transformations, by simply – với x = x*/h, y = y*/h, z = z*/h và h là một số multiplying the matrices together thực tuỳ ý 19 20 Ưu điểm của Hệ tọa độ đồng nhất Homogeneous Transform Phép biến đổi với tọa độ đồng nhất z Ðưa ra cái nhìn hợp nhất của các phép biến đổi z Ma trận biến đổi đồng nhất ⎡a b 0⎤ dưới phép nhân ma trận, hỗ trợ cho việc xử lý [T] = ⎢c d 0⎥ bằng cả phần cứng và phần mềm ⎢ ⎥ Phép tịnh tiến ⎢⎣m n 1⎥⎦ Kết hợp các các phép biến đổi tạo thành ma trận z z tích đơn giản duy nhất. Tránh nhầm lẫn về thứ tự ⎡1 0 0⎤ [x' y' 1]=[x y 1]⎢0 1 0⎥ =[x + m y + n 1] của các phép nhân khi sử dụng. ⎢ ⎥ Order matters: AB is generally not the same as BA ⎢⎣m n 1⎥⎦ – z Cho phép kết hợp với cả các phép biến đổi đặc biệt không tuyến tính khác(non-affine) như: (tx, ty, tz) – Phép chiếu phối cảnh - Perspective projections! – Uốn - Bends, Vuốt tapers v.v.v 21 22 Phép tỉ lệ Phép quay S1 0 0 ⎡ cosφ sin φ 0⎤ ⎡ ⎤ ⎢ ⎥ [x' y' 1] = [x y 1]⎢ 0 S2 0⎥ = [x.S1 y.S2 1] [x' y' 1] = [x y 1] ⎢− sin φ cosφ 0⎥ y ⎢ ⎥ ⎢ 0 0 1⎥ ⎢⎣ 0 0 1⎥⎦ ⎣ ⎦ ( x’, y’ ) =[x.cosφ − y.sinφ x.sinφ + y.cosφ 1] ρ ( x, y ) θ ρ α x 23 24 goupee.com fb.com/groups/goupee 4
第 5 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Ma trận biến đổi 3 chiều Phép biến đổi tổng hợp 3D Matrix Transformations z Các phép biến đổi chuyển vị - translation, tỉ lệ- scaling và quay-rotation sử dụng trong không gian 2D đều co thể mở rộng trong không gian 3D z Again, using homogeneous coordinates it is possible to represent each type of transformation in a matrix form z In 3D, each transformation is represented by a 4x4 matrix 25 26 Các phép biến đổi hình học 3 chiều Phép tịnh tiến z Biểu diễn điểm trong không gian 3 chiều z [ x* y* z* h ] = [ x y z 1 ]. [ T ] ⎡ 1 0 0 0⎤ z [x' y' z' 1 ]= [ x*/h y*/h z*/h 1 ][ T ] ⎢ 0 1 0 0⎥ [T(dx,dy,dz)]= ⎢ ⎥ z Ma trận biến đổi ⎡a b c p⎤ ⎢ 0 0 1 0⎥ ⎢ ⎥ ⎢dx dy dz 1⎥ ⎢d e f q⎥ ⎣ ⎦ [T] = ⎢g i j r⎥ ⎢ ⎥ – [X'] = [ X ] . [ T(dx,dy,dz) ] ⎣l m n s⎦ [ x' y' z' 1 ] = – – [ x y z 1 ].[ T(dx,dy,dz) ] z = [ x+dx y+dy z+dz 1 ] 27 28 Phép tỉ lệ Rotation ⎡s1 0 0 0⎤ z In 2D, the only rotation possible was about the ⎢0 s2 0 0⎥ origin. [x' y' z' 1] =[x y z 1] ⎢ ⎥ ⎢0 0 s3 0⎥ z In 3D, there are 3 possible rotations, one about ⎢ ⎥ each of the x, y and z axes ⎣0 0 0 1⎦ z Positive rotations are anti-clockwise, negative =[x.s1 y.s2 z.s3 1] rotations are clockwise, when looking down a positive axis towards the origin y y y z s1, s2, s3 là các hệ số tỉ lệ tương ứng trên các trục toạ độ x x x z z z 29 30 goupee.com fb.com/groups/goupee 5
第 6 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Phép quay 3 chiều Quay quanh trục y z Quay quanh các trục toạ độ ⎡cosθ 0 − sinθ 0⎤ Quay quanh trục x ⎢ 0 1 0 0⎥ z ⎢ ⎥ ⎡1 0 0 0⎤ [Ty] = ⎢ ⎥ ⎢sinθ 0 cosθ 0⎥ ⎢0 cosφ sinφ 0⎥ ⎢ ⎥ [Tx] = ⎣ 0 0 0 1⎦ ⎢0 −sinφ cosφ 0⎥ ⎢ ⎥ ⎣0 0 0 1⎦ z Quay quanh trục z ⎡ cosϕ sinϕ 0 0⎤ ⎢− sinϕ cosϕ 0 0⎥ [Tz] = ⎢ ⎥ ⎢ 0 0 1 0⎥ ⎢ 0 0 0 1⎥ ⎣ ⎦ 31 32 Phép biến dạng Phép lấy đối xứng (secondary translation) (reflections-secondary translation) ⎡1 b c 0⎤ ⎢d 1 f 0⎥ [x' y' z' 1] =[x y z 1] ⎢ ⎥ ⎢g i 1 0⎥ ⎢ ⎥ ⎣0 0 0 1⎦ =[x + yd + gz bx + y + iz cx + fy + z 1] 33 34 Quay quanh một trục bất kỳ song song với Quay quanh một trục bất kỳ các trục tọa độ ⎡1 0 0 0 ⎤ ⎡1 0 0 0⎤ ⎢ ⎥ ⎢0 cosφ sinφ 0⎥ ⎢0 1 0 0 ⎥ [T(φ)] = ⎢ ⎥ [Tr ] = ⎢0 −sinφ cosφ 0⎥ ⎢0 0 1 0 ⎥ ⎢ ⎥ ⎢ ⎥ , ⎣0 y z 1 ⎦ ⎣0 0 0 1⎦ , ⎡1 0 0 0⎤ ⎡1 0 0 0⎤ ⎢0 cosφ sinφ 0⎥ ⎢ ⎥ [Tth]=⎢ ⎥ ⎢0 1 0 0⎥ [Tr]−1 = ⎢0 −sinφ cosφ 0⎥ ⎢0 0 1 0⎥ ⎢ ⎥ ⎢ ⎥ ⎢⎣0 y(1−cosφ)+zsinφ z(1−cosφ)−ysinφ 1⎥⎦ 0 − y − z 1 ⎣ ⎦ 35 36 goupee.com fb.com/groups/goupee 6
第 7 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Solution Biến đổi góc nhìn z Chuyển P1 về gốc tọa độ. Viewing Transformation z Quay quanh trục y sao cho P1P2 nằm trên mặt phẳng (y, z) z Quay quanh trục x sao cho P1P2 trùng với trục z. z Quay quanh trục z sao cho P1P3 nằm trên mặt phẳng (y, z) z Euler’s Theorem: Every rotation around the origin Lê Tấn Hùng can be decomposed into a rotation around the x-axis followed by a rotation around the y-axis followed by a rotation around the z-axis. 37 38 Hệ tọa độ thực Nội dung (WCS-World Coordinate System) z Phép biến đổi nhìn cơ bản z Là hệ tọa độ của đối tượng z Biến đổi hệ thống tọa độ được các chương trình ứng dụng sử dụng để mô z Camera tả tọa độ của các đối Biến đổi 3D viewing tượng trong thế giới thực. z Đơn vị trong hệ thống tọa z z Ví dụ độ phụ thuộc vào không gian và kích thước của đối tượng được mô tả, có thể từ A0, nm, mm ... đến m, km ... z Modeling Coordinate 39 40 Hệ tọa độ thiết bị Hệ tọa độ chuẩn (DCS-Device Coordinate System) (NCS - Normalized Coordinate System) z Là hệ thống tọa độ của thiết bị nơi hiển thị z Chuyển đổi hệ tọa độ hình ảnh và không gian của đối tượng mà Giải quyết vấn đề khi ứng dụng chạy trên các ứng dụng mô tả. z thiết bị khác nhau z Có kích thước 1x1 Wcs NCS Dcs chuyÓn ®æi 2 chuyÓn ®æi 1 subselect.me Vïng täa ®é thiÕt bÞ VGA=640x480 41 ThiÕt bÞ hiÓn thÞ 42 goupee.com fb.com/groups/goupee 7
第 8 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Phép biến đổi nhìn cơ bản Basic Viewing Transform 2-Dimensional Views z Phép biến đổi góc nhìn cơ bản là chuyển đổi từ z A Window : Cửa sổ, Phần hình ảnh của thế giới hệ tọa độ mô hình sang hệ tọa độ mà hình thực sẽ được hiển thị (Modelling co-ordinates to Screen Coordinates) z A Viewport : Cổng nhìn, vùng màn hình nơi hình z Vấn đề: ảnh thế giới thực sẽ hiển thị z Thay đổi tỷ lệ hình ảnh (wx ,wy ) (vx ,vy ) max max Hiệu ứng về tỉ lệ khi Zoom hình ảnh max max z z Phần nào của hình vẽ sẽ hiển thị ? z Hiển thị tại đâu trên màn hình ? (wx,wy) (vx,vy) z Chuyển đổi sẽ được thực hiện ra sao? (vx ,vy ) min min 43 44 (wx ,wy ) min min Phép chuyển đổi Phép biến đổi theo ma trận z Ma trận chuyển vị theo Window ⎡ 1 0 0⎤ [T1] = ⎢ 0 1 0⎥ ⎢ ⎥ ⎢⎣− Xw −Yw 1⎥⎦ ⎡ Xvmax-Xvmin ⎤ ⎢ 0 0⎥ Xwmax-Xwmin ⎢ Yvmax−Yvmin ⎥ z Ma trận biến đổi tỉ lệ [S1] = ⎢ 0 0⎥ ⎢ Ywmax−Ywmin ⎥ ⎢ 0 0 1⎥ ⎢⎣ ⎥⎦ ⎡ 1 0 0⎤ [T2] = ⎢ 0 1 0⎥ ⎢ ⎥ ⎢⎣Xv Yy 1⎥⎦ z Ma trận chuyển vị theo tọa độ viewport 45 46 Ma trận biến đổi tổng hợp của phép chuyển đổi Ví dụ OpenGL - Windows and Viewports tọa độ z Plotting a function revisted.. [T]=[T1]x[S1]x[T2] //set the viewing coordinates //--------------- setWindow --------------------- void setWindow(GLdouble left, Gldouble ⎡ Xvmax-Xvmin ⎤ setWindow(xmin, xmax, ymin, right, GLdouble bottom, GLdouble top) ⎢ 0 0⎥ ymax); { Xwmax-Xwmin glMatrixMode(GL_PROJECTION); ⎢ ⎥ setViewport(0,640,0,480); glLoadIdentity(); Yvmax−Yvmin gluOrtho2D(left, right, bottom, top); [T]= ⎢ 0 0⎥ glBegin(GL_POINTS); } ⎢ Ywmax−Ywmin ⎥ for(GLdouble x = xmin; x Xvmax-Xvmin Yvmax−Yvmin < xmax; x+=0.005 ) ⎢ − − ⎥ //---------------- setViewport ------------------ Xvmin Xwmin Yvmin Ywmin 1 { void setViewport(GLdouble left, Gldouble ⎢⎣ Xwmax-Xwmin Ywmax−Ywmin ⎥⎦ glVertex2d(x, pow(2.7183,-x) right, GLdouble bottom, GLdouble top) { *cos(2*3.14*x)); glViewport(left, bottom, right – left, top - } bottom);} glEnd(); 47 48 goupee.com fb.com/groups/goupee 8
第 9 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Biến đổi hệ tọa độ Coordinate Transforms Các phép biến đổi z Là phép biến đổi trên hệ tọa độ của đối tượng. Việc thay x x đổi hệ tọa độ của đối tượng với hệ thống tọa độ chung u u’ tao nên sự thay đổi toàn cảnhx u (1,1) (1,1) u’ (1,1) v y v’ y (1,1) ⎡1 0 0⎤ Q = ⎢0 1 0⎥P v v’ ⎢ ⎥ ⎢⎣0 0 1⎥⎦ Hệ tọa độ cục bộ y Đối tượng sau biến đổi trên tđ toàn 49 cục 50 Translation Rotation ⎡0⎤ O = ⎢0⎥ x x ⎢ ⎥ u ⎡1 0 tx⎤ x u’ u x ⎢⎣1⎥⎦ Q = ⎢0 1 ty⎥P u’ ⎢ ⎥ (1+tx,1+ty) (1,1) ⎢0 0 1⎥ (1,1) ⎣ ⎦ v’ v y v’ y v y y ⎡1 0 tx⎤⎡0⎤ ⎡tx⎤ ⎡1 0 tx⎤⎡1⎤ ⎡ +1 tx⎤ ⎡1 0 tx⎤⎡0⎤ ⎡ tx ⎤ ⎢0 1 ty⎥⎢0⎥ = ⎢ty⎥ ⎢0 1 ty⎥⎢0⎥ = ⎢ ty ⎥ ⎢0 1 ty⎥⎢1⎥ = ⎢1+ty⎥ ⎡cosθ −sinθ 0⎤ ⎡cosθ⎤ ⎡−sinθ⎤ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢⎣0 0 1⎥⎦⎢⎣1⎥⎦ ⎢⎣1⎥⎦ ⎢⎣0 0 1⎥⎦⎢⎣1⎥⎦ ⎢⎣ 1 ⎥⎦ ⎢⎣0 0 1⎥⎦⎢⎣1⎥⎦ ⎢⎣ 1 ⎥⎦ Q = ⎢sinθ cosθ 0⎥P v = sinθ u = cosθ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎡tx⎤ ⎢⎣ 0 0 1⎥⎦ ⎢⎣ 1 ⎥⎦ ⎢⎣ 1 ⎥⎦ origin O = ⎢ty⎥ v (1, 0, 0) u (0, 1, 0) ⎢ ⎥ 51 ⎢⎣1⎥⎦ 52 Scaling Composite Transformations u x x x ⎡x (1−cosθ)+ y sinθ⎤ u 1 1 u ⎢y (1−cosθ)− y sinθ⎥ x O =⎢ 1 1 ⎥ (1,1) u’ ⎢⎣ 1 ⎥⎦ (1,1) (sx*1,sy*1) v ⎡cosθ +x (1−cosθ)+ y sinθ⎤ v ’ ⎢ 1 1 ⎥ y y v = sinθ + y (1−cosθ)− y sinθ ⎢ 1 1 ⎥ ⎢⎣ 1 ⎥⎦ v y y v ⎡−sinθ +x (1−cosθ)+ y sinθ⎤ 1 1 sx 0 0 0 ⎛cosθ −sinθ x (1−cosθ)+ y sinθ ⎞ u =⎢ cosθ + y (1−cosθ)− y sinθ ⎥ ⎡ ⎤ ⎡0⎤ ⎡sx⎤ ⎡ ⎤ ⎜ 1 1 ⎟ ⎢ 1 1 ⎥ Q = ⎢ 0 sy 0⎥P ⎢ ⎥ ⎢ ⎥ u = ⎢sy⎥ M = ⎜sinθ cosθ y (1−cosθ)− x sinθ ⎟ ⎢⎣ 1 ⎥⎦ ⎢ ⎥ O = ⎢0⎥ v = ⎢ 0 ⎥ ⎢ ⎥ ⎜ 1 1 ⎟ ⎝ 0 0 1 ⎠ ⎢⎣ 0 0 1⎥⎦ ⎢⎣1⎥⎦ ⎢⎣ 1 ⎥⎦ ⎢⎣ 1 ⎥⎦ 53 54 goupee.com fb.com/groups/goupee 9
第 10 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Quan sát 3D - 3D Viewing + z Hiển thị hình ảnh 3D trên các thiết bị 2D z Mỗi 1 quan sát 3D trên cùng 1 đối + + + tượng chỉ ra trạng thái chiếu của transform transform transform điểm quan sát đó. – HÌnh ảnh chiếu 2D của đối tượng 3D phụ thuộc vào người quan sát z Các quan sát khác nhau được phân biệt bởi thông số: vị trí position, hướng Obviously we want orientation, và vùng nhìn field of view something more versatile = 55 56 Viewing The Viewing Process modeling world viewing coordinate Modeling coordinate Viewing coordinate (eye coordinate) z Để tạo 1 cảnh nhìn gồm các bước transformation transformation – Mô tả biểu diễn hình học – Định nghĩa điểm nhìn – Camera y V z Chuyển đổi từ đối tượng 3D sang 2D y x Xác định vùng hiển thị của đố tượng W V -z p z V V yM z Xác định camera để có thể tạo thành phép pW chiếu từ 3D thành 2D. p M xM x z W z M W 57 58 Khái quát về - Camera Analogy Viewing Coordinates z Có thể sử dụng mô hình hệ z Khái niệm thống tọa độ quan sát trên các z camera location: Vị trí trong thế thông số gồm: giới thực – Điểm nhìn (VRP View Reference Point ): điểm gốc quan sát. Vị trí của z viewing direction: Hướng chiếu Camera) của Camera. – Vector mặt phẳng quan sát (VPN Three mutually orthogonal View Plane Normal vector ): viewing basis vectors: z up vector: Hướng phía trên của direction) N Camera Vector hướng quan sát n = = (n ,n ,n ) – (VUP View N 1 2 3 UP vector ): establishes orientation of z aperture size: Vùng nhìn thấy của “camera” u = V×N = (u ,u ,u ) Camera V×N 1 2 3 v = n×u = (v ,v ,v ) 1 2 3 59 60 goupee.com fb.com/groups/goupee 10
第 11 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Ví dụ: Viewing Transformation (i) simple cases ( always view along negative z-axis ) (xw, yw, zw) and (u, v, n) 2 hệ tọa độ với cùng 1 gốc. Vector đơn vị (u, v, n) được định nghĩa theo WCS Điểm nhìn trên trục Z Nhìn ở vị trí (a, b, c) theo trục Z và zW -axis ngược hướng z -axis ⎛u ⎞ ⎛v ⎞ ⎛n ⎞ W ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ 1 1 1 y u = ⎜u ⎟, v = ⎜v ⎟, and n = ⎜n ⎟. W y W ⎜ 2 ⎟ ⎜ 2 ⎟ ⎜ 2 ⎟ ⎜u ⎟ ⎜v ⎟ ⎜n ⎟ ⎝ 3 ⎠ ⎝ 3 ⎠ ⎝ 3 ⎠ (a, b, c) x Ma trận biến đổi tọa độ thế giới sang tọa độ quan sát : n W x W (0, 0, z) MWC,VC = R T z W z M = T (0, 0, z,) M = T (-a, -b, -c ) W WC,VC WC,VC 61 61 62 62 3D Viewing Transformation Viewing Transformation z Với điểm quan sát VRPoint (x0, y0, z0) the Viewing z We might also achieve this in terms of the following Transformation can be defined by composition of the transformation sequence: following two transformations: – Translate the view reference point to the origin of the world co- ordinate system ⎛1 0 0 − x ⎞ ⎛u u u 0⎞ – Apply Rotations to align x , y and z , axes, respectively ⎜ 1 2 3 ⎟ v v v ⎜ 0 ⎟ ⎜0 1 0 − y ⎟ ⎜v v v 0⎟ T = 0 R = 1 2 3 ⎜0 0 1 − z ⎟ ⎜n n n 0⎟ ⎜ 1 2 3 ⎟ ⎛1 0 0 − x ⎞ ⎜⎜ 0 ⎟⎟ ⎜ ⎟ ⎜ 0 ⎟ 0 0 0 1 ⎝ 0 0 0 1⎠ ⎜0 1 0 − y ⎟ ⎝ ⎠ 0 ⎜0 0 1 − z ⎟ ⎜⎜ 0 ⎟⎟ ⎝0 0 0 1 ⎠ M = R•T (x0, y0, z0) WC,VC This is used to determine the location of each vertex in the camera co-ordinate system r′ r x = M x wc,vc 63 64 ⎛u u u 0⎞ • Recall we’ve re-expressed our world geometry in eye’s frame ⎜ 1 2 3 ⎟ ⎜ ⎟ reference. y v v v 0 V where R = ⎜ 1 2 3 ⎟, ⎜n n n 0⎟ yW xV 1 2 3 -z p ⎜⎜ 0 0 0 1⎟⎟ y V V M ⎝ ⎠ pW ⎡u ⎤ p M 1 ⎢u ⎥ z xM xW ⎢ ⎥ z M Verification: R u = R 2 = ? , etc W ⎢u ⎥ • To do projection, we use a synthetic camera by introducing a ⎢ 3 ⎥ ⎣ 1 ⎦ viewing frustum after the viewing transformation. y V y x W y V -z pV W V p = M p V WC,VC W pW p p p W M M x x W W z z W W 65 65 66 66 goupee.com fb.com/groups/goupee 11
第 12 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Camera trong OpenGL z Các hàm cơ bản – glOrtho() and gluLookAt() – these created parallel projections We now want to modify a scene to give us Demo : Viewing z perspective projections. from Nate Robins (Utah) – lines converge as they get further away 67 68 68 The Camera The Camera z Khởi tạo Camera với phép z The Perspective Camera chiếu phối cảnh glMatrixMode(GL_PROJECTION); – Thay cho glOrtho() bằng: glLoadIdentity(); – gluPerspective() gluPerspective(viewAngle, aspectRatio, N, F); z gluPerspective – viewAngle – aspectRatio (W/H) – near plane – far plane 69 70 The Camera The Camera z Positioning the Camera – The camera is positioned using a combination of translations and rotations. – Think if the camera being in the same location up(x,y,z) as the viewers eye. eye(x,y,z) lookat(x,y,z) 71 72 goupee.com fb.com/groups/goupee 12
第 13 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 The Camera The Camera glMatrixMode(GL_MODELVIEW); z The camera can have an arbitrary orientation and glLoadIdentity(); position. gluLookAt(eye.x, eye.y, eye.z, look.x, look.y, look.z, z Therefore we can think of the camera having its own up.x, up.y, up.z); axes. 73 74 Camera Movement The Camera z A camera has six degrees of freedom: z Camera Movement 1. slid in 3 directions; and – To move the camera along the u axis you would simply perform rotated in 3 directions z eye = eye + Du v 2. – To move slide the camera in any direction: z Camera Movement eye.x = dU*u.x + dV*v.x + dN*n.x – The camera can move along its axes. eye.y = dU*u.y + dV*v.y + dN*n.y This is called sliding the camera. eye.z = dU*u.z + dV*v.z + dN*n.z – n z Camera Movement u – Besides physically moving the camera to another location… – the camera can be tilted in different directions to look at different parts of the scene. 75 76 The Camera The Camera - Camera Movement Camera Movement We use a plane analogy to describe the cameras z movement. – Besides physically moving the camera to another a rotation from the horizontal along the length is called PITCH z location… a rotation from the horizontal along the width is called ROLL z – the camera can be tilted in different directions to look z a rotation around the vertical is called YAW at different parts of the scene. 77 78 goupee.com fb.com/groups/goupee 13
第 14 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 The Camera The Camera v v z Pitch n z Roll u v’ = cos(θ)v – sin(θ)n u’ = cos(θ)u + sin(θ)v n’ = sin(θ)n + cos(θ)v v’ = cos(θ)v - sin(θ)u 79 80 The Camera A 3D Scene n z Yaw u z Notice the presence of n’ = cos(θ)n - sin(θ)u the camera, the u’ = sin(θ)n + cos(θ)u projection plane, and the world coordinate axes z Viewing transformations define how to acquire the image on the projection plane 81 82 Viewing Transformations 2 Basic Steps z Create a camera-centered view z Align the two coordinate frames by rotation z Camera is at origin z Camera is looking along negative z-axis z Camera’s ‘up’ is aligned with y-axis 83 84 goupee.com fb.com/groups/goupee 14
第 15 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Creating Camera Coordinate 2 Basic Steps Space z Translate to align origins z Specify a point where the camera is located in world space, the eye point z Specify a point in world space that we wish to become the center of view, the lookat point z Specify a vector in world space that we wish to point up in camera image, the up vector z Intuitive camera movement 85 86 Constructing Viewing Constructing Viewing Transformation, V Transformation, V z Create a vector from eye-point to lookat-point z Construct another important vector from the cross product of the lookat-vector and the vup- vector z Normalize the vector z This vector, when normalized, should align with [1, 0, 0]T Why? z Desired rotation matrix should map this vector to [0, 0, -1]T Why? 87 88 Constructing Viewing Transformation, V Compositing Vectors to Form V z One more vector to define… z We know the three world axis vectors (x, y, z) This vector, when normalized, should align with [0, 1, 0]T z We know the three camera axis vectors (r, u, l) z z Viewing transformation, V, must convert from world to camera coordinate systems z Now let’s compose the results 89 90 goupee.com fb.com/groups/goupee 15
第 16 页
Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Compositing Vectors to Form V Compositing Vectors to Form V z Remember Each camera axis vector is unit length. z Therefore, rotation component of viewing – – Each camera axis vector is perpendicular to others transformation is just transpose of computed vectors z Camera matrix is orthogonal and normalized – Orthonormal z Therefore, M-1 = MT 91 92 Compositing Vectors to Form V Final Viewing Transformation, V z Translation component too z To transform vertices, use this matrix: z Multiply it through z And you get this: 93 94 goupee.com fb.com/groups/goupee 16
评论 (0)
暂无评论。快来抢沙发吧!