Bài giảng Đồ họa và hiện thực ảo - Bài 4: Các phép biến đổi đồ hoạ - Lê Tấn Hùng

pdf 16 trang cucquyet12 7040
Bạn đang xem tài liệu "Bài giảng Đồ họa và hiện thực ảo - Bài 4: Các phép biến đổi đồ hoạ - Lê Tấn Hùng", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_do_hoa_va_hien_thuc_ao_bai_4_cac_phep_bien_doi_do.pdf

Nội dung text: Bài giảng Đồ họa và hiện thực ảo - Bài 4: Các phép biến đổi đồ hoạ - Lê Tấn Hùng

  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ạ z Trong kỹ thuật đồ hoạ 3 bước: Mụ hỡnh, Tụ trỏt Transformations 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 world z Biến đổi mụ hỡnh hoỏ - Modeling transformations – 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 đổitọa độ Transform Transform coordinate system điểm đặctrưng của đốitượng thành tậptương objects/points ứng cỏc điểmmới để tạoracỏchiệu ứng cho toàn đốitượng. – Vớdụ: phộp biến đổitọa độ vớichỉ 2 điểm đầucuốicủa đoạnthẳng tạo thành 2 điểmmớimàkhinối chỳng với nhau tạo thành đoạnthẳng mới. z Cỏc điểmnằmtrờnđoạnthẳng sẽ cú kếtquả là điểmnằmtrờnđoạnthẳng mớivới cựng phộp biến đổi thụng qua phộp nộisuy. 5 6 1
  2. Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Biểu diễn Ma trận Matrix Representation z Phộp biến đổi đồ họa - affine transformation T ỏnh xạ z Việc biến đối cỏc đối tượng làm thay đổi cỏc tập P sang tập Q: điểm P thành cỏc điểm Q theo thuật toỏn – Qx = aPx + bPx + tx z Việc biến đổi P sử dụng tọa độ của P (Px,Py) ỏnh – Qy = cPy + dPy + t y xạ thành cỏc tọa độ mới Q (Qx,Qy) – where a, b, c, d, tx and ty là cỏc hệ số z Việc biến đổi cú thể biểu diễn thụng qua hàm T, z Biểu diễn ma trận: hàm ỏnh xạ của điểm: ⎛ Q x ⎞ ⎛ a b ⎞⎛ Px ⎞ ⎛ t x ⎞ – T(Px,Py) = (Qx,Qy) ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ = ⎜ ⎟⎜ ⎟ + ⎜ ⎟ – or: ⎝ Q y ⎠ ⎝ c d ⎠⎝ P y ⎠ ⎝ t y ⎠ z i.e. – T(P) = Q Q = MP + Tr 7 8 Cỏc phộp biến đổihỡnhhọchai chiều Phộp biến đổi y z Phương phỏp biểudiễn đốitượng P = [ x y ] ⎡1 0⎤ z Phộp bấtbiến T = ⎢ ⎥ z Phộp biến đổivị trớ điểm ⎡a b⎤ ⎣0 1⎦ T = ⎢ ⎥ z x ⎣c d⎦ z Phộp biến đổitỉ lệ - Scaling z Thực thi phộp biến đổi đỳngtrờn1 điểm ảnh sẽđỳng trờn toàn bộđốitượng z A scaling changes the size of an object with two scale factors, S and S ⎡a b ⎤ ' ' x y [][][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 [][][X * T = x y ]* = [bx + dy ][= x' y' ] W z Phộp biếndạng ⎢0 1⎥ pM ⎣ ⎦ z A shearing shears an object in a particular direction, (in 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 α ; z Preservation of lines: x’ = ρ cos (θ +α ), y’ = ρ sin (θ +α ) ; y x’ = ρ ( cosθ cosα - sinθ sinα ) – They preserve lines, so the image of a straight line is ( x’, y’ ) = x cosθ - y sinθ another straight line. 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 2
  3. Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Thuộc tớnh Kếthợp cỏc phộp biến đổi Composition of Affine Transforms z Any affine transformation can be z Preservation of parallelism decomposed into elementary – Preservation of parallelism guarantees that transformations. parallelograms will transform into parallelograms z Mọi phộp biến đổiphứctạp đều 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- đổicơ 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ếndạng - Shearing 13 14 Affine transformations preserve affine combinations Thuộc tớnh z It is rare that we want to perform just one elementary T transformation. z Usually an application requires that we build a complex transformation out of several elementary z Tỏc động lờn tập cỏc điểm đặc trưng của đối ones 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 z These individual transformations combine into one z We have defined each transformation by their effects on overall transformation single points z z This is called the composition of transformations. In practice these will be applied to multiple points to 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 Cho phộp quay và tỉ lệ Rotation and Scaling Pivotal points 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 z To do this, we relate the rotation or scaling about the 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 (c1,c2) – We then rotate or about the origin – then all points are translated back, so that V is restored to its original location (0,0) 17 18 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ểudiễnmở rộng thụng qua z Unfortunately, the translation portion is not a tọa độ đồng nhấtcủa cỏc vector vị trớ matrix multiplication but must instead be added as an extra term, or vector z z What we need is a “trick”, so that translations can Với ứng dụng của phộp chiếuhỡnhhọcmà be represented in matrix multiplication form ở đútọa độ điểm đượcmụtả dướima 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ộtsố multiplying the matrices together 19 20 thựctuỳ ý Ưu điểmcủaHệ tọa độ đồng nhất Homogeneous Transform Phộp biến đổivớitọa độ đồng nhất z éưaracỏinhỡnhợpnhấtcủa cỏc phộp biến đổi z Ma trậnbiến đổi đồng nhất ⎡a b 0⎤ dưới phộp nhõn ma trận, hỗ trợ cho việcxử lý ⎢ ⎥ [T] = ⎢ c d 0⎥ bằng cả phầncứng và phầnmềm ⎢m n 1⎥ z Phộp tịnh tiến ⎣ ⎦ z Kếthợp cỏc cỏc phộp biến đổitạo thành ma trận ⎡1 0 0⎤ tớch đơngiản duy nhất. Trỏnh nhầmlẫnvề thứ tự ⎢ ⎥ của cỏc phộp nhõn khi sử dụng. [x' y' 1] = [x y 1]⎢ 0 1 0⎥ = [x + m y + n 1] – Order matters: AB is generally not the same as BA ⎣⎢m n 1⎦⎥ z Cho phộp kếthợpvớicả 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ếuphốicảnh - Perspective projections! – Uốn - Bends, Vuốt tapers v.v.v 21 22 Phộp tỉ lệ Phộp quay ⎡ cos φ sin φ 0⎤ ⎡S1 0 0⎤ [x' y' 1] = [x y 1] ⎢− sin φ cos φ 0⎥ y [x' y' 1] = [x y 1]⎢ 0 S2 0⎥ = [x.S1 y.S2 1] ⎢ ⎥ ⎢ ⎥ ⎢ 0 0 1⎥ ⎣⎢ 0 0 1⎦⎥ ⎣ ⎦ ( x’, y’ ) = [x.cosφ − y.sinφ x.sinφ + y.cosφ 1] ρ ( x, y ) ρ θ α x 23 24 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 đổitổ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 đổihỡnhhọc3 chiều Phộp tịnh tiến z Biểudiễ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ậnbiến đổi a b c p ⎢ 0 0 1 0⎥ ⎡ ⎤ ⎢ ⎥ ⎢d e f q⎥ ⎣dx dy dz 1⎦ [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ụctoạ độ x x x z z z 29 30 5
  6. Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Phộp quay 3 chiều Quay quanh trụcy z Quay quanh cỏc trụctoạđộ ⎡cosθ 0 − sinθ 0⎤ ⎢ ⎥ z Quay quanh trụcx 0 1 0 0 ⎡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ụcz ⎡ cosϕ sinϕ 0 0⎤ ⎢− sinϕ cosϕ 0 0⎥ [Tz] = ⎢ ⎥ ⎢ 0 0 1 0⎥ ⎢ ⎥ ⎣ 0 0 0 1⎦ 31 32 Phộp biếndạng Phộp lấy đốixứ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ộttrụcbấtkỳ song song với Quay quanh mộttrụcbấtkỳ cỏc trụctọa độ ⎡ 1 0 0 0 ⎤ ⎡1 0 0 0⎤ ⎢ ⎥ ⎢ 0 1 0 0 ⎥ 0 cosφ sin φ 0 [Tr ] = ⎢ ⎥ [T (φ)] = ⎢ ⎥ ⎢ 0 0 1 0 ⎥ ⎢0 − sin φ cosφ 0⎥ ⎢ ⎥ , ⎢ ⎥ 0 0 0 1 , ⎣ 0 y z 1 ⎦ ⎣ ⎦ ⎡1 0 0 0⎤ ⎡1 0 0 0⎤ ⎢ ⎥ ⎢ ⎥ ⎢0 cosφ sinφ 0⎥ 0 1 0 0 [Tth]= [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 6
  7. Khoa CNTT - DDHBK Hà nội hunglt@it-hut.edu.vn 8682595 Solution Biến đổi gúc nhỡn z ChuyểnP1 về gốctọa độ. Viewing Transformation z Quay quanh trục y sao cho P1P2 nằmtrờnmặtphẳng (y, z) z Quay quanh trục x sao cho P1P2 trựng vớitrụcz. z Quay quanh trục z sao cho P1P3 nằmtrờnmặtphẳ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 đốitượ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 tượng trong thế giớithực. z Biến đổi 3D viewing z Đơnvị trong hệ thống tọa z Vớ dụ độ phụ thuộc vào khụng gian và kớch thướccủa đối tượng đượcmụtả, cú thể từ A0, nm, mm đếnm, km z Modeling Coordinate 39 40 Hệ tọa độ thiếtbị Hệ tọa độ chuẩn (DCS-Device Coordinate System) (NCS - Normalized Coordinate System) z Là hệ thống tọa độ củathiếtbị nơihiểnthị z Chuyển đổi hệ tọa độ hỡnh ảnh và khụng gian của đốitượng mà z Giải quyếtvấn đề khi ứng dụng chạytrờncỏc ứng dụng mụ tả. thiếtbị khỏc nhau z Cú kớch thước1x1 NCS Dcs Wcs 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 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 (wxmax,wymax) (vxmax,vymax) z Hiệu ứng về tỉ lệ khi Zoom hỡnh ảnh 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? (vxmin,vymin) 43 44 (wxmin,wymin) Phộp chuyển đổi Phộp biến đổitheoma trận z Ma trậnchuyểnvị theo Window ⎡ 1 0 0⎤ 1 ⎢ 0 1 0⎥ [T ] = ⎢ ⎥ ⎣⎢− Xw −Yw 1⎦⎥ ⎡ Xvmax- Xvmin ⎤ 0 0 ⎢Xwmax- Xwmin ⎥ ⎢ Yvmax−Yvmin ⎥ z Ma trậnbiến đổitỉ lệ [S1] = ⎢ 0 0⎥ ⎢ Ywmax−Ywmin ⎥ ⎢ 0 0 1⎥ ⎣⎢ ⎦⎥ ⎡ 1 0 0⎤ ⎢ ⎥ [T 2] = ⎢ 0 1 0⎥ ⎢Xv Yy 1⎥ z Ma trậnchuyểnvị theo tọa độ viewport ⎣ ⎦ 45 46 Ma trậnbiến đổitổng hợpcủaphộp chuyển đổi tọa độ Vớ dụ OpenGL - Windows and Viewports z Plotting a function revisted [T] =[T1]x[S1]x[T2] // setWindow //set the viewing coordinates 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 < xmax; x+=0.005 ) ⎢ Xvmax- Xvmin Yvmax−Yvmin ⎥ // setViewport Xvmin− Xwmin Yvmin−Ywmin 1 { void setViewport(GLdouble left, Gldouble ⎣⎢ Xwmax- Xwmin Ywmax−Ywmin ⎦⎥ right, GLdouble bottom, GLdouble top) glVertex2d(x, pow(2.7183,-x) { *cos(2*3.14*x)); glViewport(left, bottom, right – left, top - } bottom);} glEnd(); 47 48 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) (1,1) ⎣⎢0 0 1 ⎦⎥ v’ v’ v y v y y y ⎡1 0 tx⎤⎡0⎤ ⎡tx⎤ ⎡1 0 tx⎤⎡1⎤ ⎡1+ tx⎤ ⎡1 0 tx⎤⎡0⎤ ⎡ tx ⎤ ⎢0 1 ty⎥⎢0⎥ = ⎢ty⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥ cosθ − sinθ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢0 1 ty⎥⎢0⎥ = ⎢ ty ⎥ 0 1 ty 1 = 1+ ty ⎡cosθ − sinθ 0⎤ ⎡ ⎤ ⎡ ⎤ ⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣⎢0 0 1 ⎦⎥⎣⎢1⎦⎥ ⎣⎢1 ⎦⎥ ⎣⎢0 0 1 ⎦⎥⎣⎢1⎦⎥ ⎣⎢ 1 ⎦⎥ ⎣⎢0 0 1 ⎦⎥⎣⎢1⎦⎥ ⎣⎢ 1 ⎦⎥ ⎢ ⎥ v = sinθ u = cosθ Q = ⎢sinθ cosθ 0⎥P ⎢ ⎥ ⎢ ⎥ ⎡tx⎤ ⎢ 0 0 1⎥ ⎣⎢ 1 ⎦⎥ ⎣⎢ 1 ⎦⎥ O = ⎢ty⎥ ⎣ ⎦ origin⎢ ⎥ v (1, 0, 0) u (0, 1, 0) 51 ⎣⎢ 1 ⎦⎥ 52 Scaling Composite Transformations u x x ⎡x1(1− cosθ ) + y1 sinθ ⎤ x u u ⎢y (1− cosθ ) − y sinθ ⎥ x O =⎢ 1 1 ⎥ (1,1) ’ ⎢ 1 ⎥ (sx*1,sy*1) u ⎣ ⎦ (1,1) v ⎡cosθ + x (1−cosθ)+ y sinθ⎤ ’ 1 1 v y ⎢sinθ + y (1−cosθ)− y sinθ ⎥ y v = ⎢ 1 1 ⎥ ⎢ 1 ⎥ v y ⎣ ⎦ ⎡−sinθ + x1(1−cosθ)+ y1 sinθ⎤ y v ⎢ ⎥ ⎛cosθ − sinθ x (1− cosθ ) + y sinθ ⎞ u = cosθ + y1(1−cosθ)− y1 sinθ ⎡sx 0 0⎤ ⎡0⎤ ⎡sx⎤ ⎡ 0 ⎤ ⎜ 1 1 ⎟ ⎢ ⎥ ⎢ 1 ⎥ ⎢ ⎥ ⎢ ⎥ M = ⎜ sinθ cosθ y1(1− cosθ ) − x1 sinθ ⎟ ⎣ ⎦ Q = 0 sy 0 P O = ⎢0⎥ v = ⎢ 0 ⎥ u = sy ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎜ 0 0 1 ⎟ ⎢ 0 0 1⎥ ⎢ 1 ⎥ ⎝ ⎠ ⎣ ⎦ ⎣⎢1⎦⎥ ⎣⎢ 1 ⎦⎥ ⎣ ⎦ 53 54 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 yV z Chuyển đổi từ đối tượng 3D sang 2D x yW V -z V pV z Xỏc định vựng hiển thị của đố tượng yM z Xỏc định camera để cú thể tạo thành phộp pW pM chiếu từ 3D thành 2D. x M xW zM zW 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 n = = (n ,n ,n ) Camera – Vector hướng quan sỏt (VUP View N 1 2 3 UP vector ): establishes orientation of V ì N z aperture size: Vựng nhỡn thấy của “camera” u = = (u ,u ,u ) Camera V ì N 1 2 3 v = n ì u = (v1 ,v2 ,v3 ) 59 60 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à z -axis W ngược hướng zW-axis ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎜ u1 ⎟ ⎜ v1 ⎟ ⎜ n1 ⎟ yW y u = ⎜u ⎟, v = ⎜v ⎟, and n = ⎜n ⎟ . W ⎜ 2 ⎟ ⎜ 2 ⎟ ⎜ 2 ⎟ ⎜u3 ⎟ ⎜v3 ⎟ ⎜ n3 ⎟ (a, b, c) ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ Ma trận biến đổi tọa độ thế giới sang tọa độ quan sỏt : xW x n W (0, 0, z) MWC,VC = R T zW zW MWC,VC = T (0, 0, z,) MWC,VC = T (-a, -b, -c ) 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 – Apply Rotations to align x , y and z , axes, respectively ⎛1 0 0 − x ⎞ ⎛u1 u2 u3 0⎞ v v v ⎜ 0 ⎟ ⎜ ⎟ 0 1 0 − y ⎜v1 v2 v3 0⎟ ⎜ 0 ⎟ R = T = ⎜ ⎟ ⎜ ⎟ 0 0 1 − z n n n 0 ⎛1 0 0 − x0 ⎞ ⎜ 0 ⎟ ⎜ 1 2 3 ⎟ ⎜ ⎟ ⎜ ⎟ ⎜0 1 0 − y0 ⎟ ⎜0 0 0 1 ⎟ 0 0 0 1 ⎝ ⎠ ⎝ ⎠ ⎜0 0 1 − z ⎟ ⎜ 0 ⎟ ⎜ ⎟ ⎝0 0 0 1 ⎠ (x , y , z ) MWC,VC = R • T 0 0 0 This is used to determine the location of each vertex in the camera co-ordinate system r r x′ = M wc,vc x 63 64 ⎛ ⎞ • Recall we’ve re-expressed our world geometry in eye’s frame ⎜u1 u2 u3 0⎟ y ⎜v v v 0⎟ reference. V where R = ⎜ 1 2 3 ⎟, y xV n n n 0 W ⎜ 1 2 3 ⎟ -z V pV y ⎜ 0 0 0 1⎟ M ⎝ ⎠ pW pM ⎡u1 ⎤ xM x ⎢ ⎥ z W u z M Verification: R u = R ⎢ 2 ⎥ = ? , etc W ⎢ ⎥ u3 • To do projection, we use a synthetic camera by introducing a ⎢ ⎥ ⎣ 1 ⎦ viewing frustum after the viewing transformation. yV yW xV pV yW -zV pV = MWC,VC pW p p W pW M pM x W xW zW zW 65 65 66 66 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 Demo : Viewing z We now want to modify a scene to give us from Nate Robins perspective projections. (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 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, z look.x, look.y, look.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 z eye = eye + Du v 2. rotated in 3 directions – 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 eye.z = dU*u.z + dV*v.z + dN*n.z – This is called sliding the camera. 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 We use a plane analogy to describe the cameras z Camera Movement movement. – Besides physically moving the camera to another z a rotation from the horizontal along the length is called PITCH location z a rotation from the horizontal along the width is called ROLL – 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 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 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) z We know the three camera axis vectors (r, u, l) z This vector, when normalized, should align with [0, 1, 0]T z Viewing transformation, V, must convert from world to camera coordinate systems z Now let’s compose the results 89 90 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 16