CG OpenGL vectors geometric & transformations-course 5

1,502 views
1,313 views

Published on

OpenGL - draw 2D & 3D graphics

control vector geometic & make a tranformation

Published in: Design, Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,502
On SlideShare
0
From Embeds
0
Number of Embeds
103
Actions
Shares
0
Downloads
52
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CG OpenGL vectors geometric & transformations-course 5

  1. 1. Vectors & Geometric Transformations Chen Jing-Fung (2006/11/17) Assistant Research Fellow, Digital Media Center, National Taiwan Normal University Video Processing Lab 臺灣師範大學數位媒體中心視訊處理研究室 National Taiwan Normal UniversityCh5: Computer Graphics with OpenGL 3th, Hearn Baker
  2. 2. 3D Graphics• Goal – Using mathematical method produce 2D images which is described 3D• Problems – Modeling (environment) • Construct a scene • Hierarchical description of a complex object (several parts composed) -> simpler parts – Rendering • Described how objects move around in an animation sequence? • Simply to view objects from another angle Video Processing Lab 2 臺灣師範大學數位媒體中心視訊處理研究室
  3. 3. Coordinate Systems• Coordinate systems are fundamental in computer graphics – Describe the locations of points in space• Project from one coordinate system to another – Easier to understand and implement Video Processing Lab 3 臺灣師範大學數位媒體中心視訊處理研究室
  4. 4. One coordinate system• Scalars (α), Point (P) & Vector (V) y P2: (x2, y2) V2=[x2, y2] vx vy y V=P2-P1=[x2- x1, y2-y1]=[vxy] P1: (x1, y1) b a+b V1=[x1, y1] x a x Scalars (α) α V=[αvij] Where is a-b & ab ?? Video Processing Lab 4 臺灣師範大學數位媒體中心視訊處理研究室
  5. 5. Two vectors in one coordinate system• Scalar product or dot product V2 – V1.V2=|V1| |V2| cos Θ V1 • Commutative V1.V2= V2.V1 Θ • Associative V1.(V2+V3)= V1.V2+V1.V3• Vector product or cross product V1xV2 V2 – V1xV2=u|V1| |V2| sin Θ Θ • Anti-commutative V1xV2= -V2xV1 u: unit vector V 1 • No associative V1x(V2xV3) ≠ (V1xV2)xV3 • Associative V1x(V2+V3)= (V1xV2)+(V1xV3) u x u y uz V1  V2  v1x v1y v1z v 2 x v 2y v 2z Video Processing Lab 5 臺灣師範大學數位媒體中心視訊處理研究室
  6. 6. Two coordinate systems• The point is represented by two different coordinate systems y v (u, v) u (4, 6) x – Maybe only one coordinate system can represent the point by the easier way Video Processing Lab 6 臺灣師範大學數位媒體中心視訊處理研究室
  7. 7. Transformations (1)• Transformation functions between two Coordinate systems (rendering) y v (u, v) u=x-3 v=y-4 u (4, 6) x=u+3 y=v+4 x Video Processing Lab 7 臺灣師範大學數位媒體中心視訊處理研究室
  8. 8. Transformations (2)• In the same coordinate system to modify an object’s shape y y x’=x-2 y’=y-3 (4, 6) (2, 3) x=x’+2 y=y’+3 x x Video Processing Lab 8 臺灣師範大學數位媒體中心視訊處理研究室
  9. 9. 2D affine transformations• Affine transformation – Linear projected function x  x x  axx x  axy y  t x linear xy x  x  y y  ayx x  ayy y  t y y   x  axx axy   x  t x  or  y   a     t  ayy   y   y     yx Video Processing Lab 9 臺灣師範大學數位媒體中心視訊處理研究室
  10. 10. Composition of affine transforms• Simple transformations – Translation – Scaling – Rotation – Shear – Reflection Video Processing Lab 10 臺灣師範大學數位媒體中心視訊處理研究室
  11. 11. 2D Translation• Translation equation P  P  T y P’3  x   1 0   x  t x  y  y   0 1  y   t  ty       y x x tx – Translation is a rigid-body transformation that moves objects without deformation. – Delete the original polygon • To cover the background color (& save it in different array) Video Processing Lab 11 臺灣師範大學數位媒體中心視訊處理研究室
  12. 12. 2D Scaling• Scaling equation P  S  P y  x  s x 0   x  0  y  y    0      sy   y  0     syy x x sxx – Uniform scaling: sx=sy sx>1, sy< 1 – Differential scaling: sx≠sy Video Processing Lab 12 臺灣師範大學數位媒體中心視訊處理研究室
  13. 13. 2D Rotation (1)• Single point rotation – Pivot-point = origin x  r cos(   ), y  r sin(   ) (x’,y’) x  r cos( ), y  r sin( ) r (x,y)  x  cos * x  sin * y y  sin * x  cos * y Video Processing Lab 13 臺灣師範大學數位媒體中心視訊處理研究室
  14. 14. 2D Rotation (2)• Rotation equation P  R( )  P  x  cos  sin   x  0  y    sin    y   0  cos          – Now, graphics packages all follow the standard column-vector convention • OpenGL, Java, Matlab … – Trans. and rotations are rigid-body transformations that move object without deformation • Each point on object is rotation through the same angle • Must be defined the direction of the rotated angle Rotation point is also called pivot point Video Processing Lab 14 臺灣師範大學數位媒體中心視訊處理研究室
  15. 15. Y-axis Shear• Shear along y axis (what is x-axis shear?)  x   1 0   x  0   y   sh    y y    y 1  y  0  x x Video Processing Lab 15 臺灣師範大學數位媒體中心視訊處理研究室
  16. 16. Y axis Reflection  x   1 0   x  0   y    0 1  y   0 y y        x x• What is the reflection of x-axis? Video Processing Lab 16 臺灣師範大學數位媒體中心視訊處理研究室
  17. 17. Basic transformations• General form: P  M2  P  M1 – Translation (shift): M1=identity matrix – Rotation or scaling: M2=translation term (R(Θ)) or scaling fixed pixel (S(s*))• The efficient approach – To produce a sequence of transformations with these equation • Scale -> rotate(Θ) -> translate (linear) • The final coordinate positions are obtained directly from initial coordinates Video Processing Lab 17 臺灣師範大學數位媒體中心視訊處理研究室
  18. 18. Homogeneous coordinate• General transformation equations: – x’ = axx + axy + tx  x  axx axy tx   x   y   a ty   y  y’ = ayx + ayy + ty 2D    yx ayy    1  0    0 1   1   • A standard technique is used to expand the matrix 2D (x,y) -> 3D(x,y,z) h*x – Homogenous coordinates: (xh, yh, h) – Homogenous parameter: h h*y • ‘h’ means the number of points in z-axis – Simply to set h=1 Video Processing Lab 18 臺灣師範大學數位媒體中心視訊處理研究室
  19. 19. Basic Matrix3x3• Translation matrix 1 0 tx  P  P  T(t* )  P  T(t* )  P 0 1 t   y 0 0 1   • Scaling matrix s x 0 0 P  S(s* )  P 0 sy 0   0  0 1 • Rotation matrix cos  sin 0 P  R( )  P  sin cos  0    0  0 1  Video Processing Lab 19 臺灣師範大學數位媒體中心視訊處理研究室
  20. 20. Arbitrary point’s rotation (1)• Single point rotation – Pivot-point (xr,yr) ! = original point 1. translate• How to find solution u  x  xr – !! Coordinate transformation v  y  yr 2. rotate u  r cos(   ),v  r sin(   ) u  x  x r (u’,v’) u  r cos( ),v  r sin( ) v  y  y r r (u,v)  x  xr  ( x  xr )cos  ( y  y r )sin (xr,yr) y  y r  ( x  xr )sin  ( y  y r )cos Video Processing Lab 20 臺灣師範大學數位媒體中心視訊處理研究室
  21. 21. Arbitrary point’s rotation (2)• General 2D point rotation (or scaling) – (xr, yr) & (xr, yr) ≠Origin • Translate origin coordinate -> the point position T( xr , y r ) • Rotate (or scaling) the object about the coordinate origin R( ) or S(sx , sy ) • Translate the point returned to its original position T( xr , y r ) Video Processing Lab 21 臺灣師範大學數位媒體中心視訊處理研究室
  22. 22. Pivot-point rotation composite matrix T( xr , y r ) R( ) T( xr , y r ) T( xc , yc )  R( )  T( xc , yc )  R( xc , y c , ) 1 0 X c  cos  sin 0   1 0 X c 0 1 Y    sin cos 0   0 1 Y  x  xr  ( x  xr )cos  ( y  y r )sin c     c  y  y  ( x  x )sin  ( y  y )cos  r r r0 0 1   0   0 1 0 0 0     cos  sin xc (1  cos )  y c sin    sin cos y c (1  cos )  xc sin     0  0 1   Video Processing Lab 22 臺灣師範大學數位媒體中心視訊處理研究室
  23. 23. Scaling an Object not at the Origin• What case happens? – Apply the scaling transformation to an object not at the origin?• Based on the rotating about a point composition, what should you do to resize an object about its own center? T( xc , yc )  S(sx , sy )  T(xc , y c )  S( xc , y c , sx , sy ) Video Processing Lab 23 臺灣師範大學數位媒體中心視訊處理研究室
  24. 24. Back to Rotation About a Pt • R (rotation matrix) and p (Pivot-point) describe how to rotate – Translation Origin to the position: x  x  p – Rotation: x  Rx  R(x  p)  Rx  Rp – Translate back: x  x  p  Rx  Rp  p • The composite transformation involves the rotation matrix. T( xnc , y nc )  R( )  T( xnc , y nc ) Video Processing Lab 24 臺灣師範大學數位媒體中心視訊處理研究室
  25. 25. Matrix concatenation properties• What is matrix concatenation? M3  M2  M1  (M3  M2 )  M1  M3  (M2  M1 ) – Multiplication of matrices is associative • Premultiplying (left-to-right) = ?? Postmultiplying (right-to-left) – Transformation products not be commutative M2  M1  M1  M2 Video Processing Lab 25 臺灣師範大學數位媒體中心視訊處理研究室
  26. 26. 3D transformations• Homogeneous coordinates  x  a d g tx   x   y  b e h ty  y  – 4x4 matrices      z  c f u tz  z         1  0 0 0 1   1• Specification of translation, rotation, scaling and other matrices in OpenGL – glTranslate(), glRotate(), glScale(), glMultMatrix() Video Processing Lab 26 臺灣師範大學數位媒體中心視訊處理研究室
  27. 27. 3D translation & scaling• 3D Translation  x  1 0 0 tx   x   y  0 1 0 ty  y       z  0 0 1 tz  z         1  0 0 0 1   1• 3D Scaling  x  s x 0 0 0  x   y   0 sy 0 0  y      z   0 0 sz 0 z        1 0 0 0 1  1  Video Processing Lab 27 臺灣師範大學數位媒體中心視訊處理研究室
  28. 28. 3D z-Axis Rotation• 2D extend along z-axis y axis – (2D->3D)  X  cos     sin 0 X   Y    sin cos  0  Y      Z   0 1  Z     0   P  R z ( )  P x axis z axis  X  cos   sin 0 0 X  counterclockwise     Y    sin cos  0 0 Y    Z   0 0 1 0 Z        1   0   0 0 1 1  y-axis & x-axis? Video Processing Lab 28 臺灣師範大學數位媒體中心視訊處理研究室
  29. 29. 3D Rotation of arbitraryy axis y y Step 2 Step 1 P’2 P2 P’1 P’1 P”2 P1 x x z x z P’2 rotate onto z-axis z Initial position y P1 translate to the Origin y Step 3 Step 4 y Step 5 P2 P’1 P’2 P”2 P1 x P’1 z x z Rotation the Object x Translate the rotation z around z-axis Rotate the axis to its axis to its Original Original Orientation position R( )  T  R  T 1 R  R 1( )  R 1(  )  R z ( )  R y (  )  R x ( ) x y Video Processing Lab 29 臺灣師範大學數位媒體中心視訊處理研究室
  30. 30. Problems with Rotation Matrices• Specifying a rotation really only requires 3 numbers in three Cartesian coordinates – 2 numbers to show a unit vector – Third number to show the rotation angle• Rotation matrix has a large amount of redundancy – Orthonormal constraints reduce degrees of freedom back down to 3 – Keeping a matrix orthonormal is difficult when transformations are combined Video Processing Lab 30 臺灣師範大學數位媒體中心視訊處理研究室
  31. 31. Alternative Representations• Specify the axis and the angle (OpenGL method) – Hard to compose multiple rotations• Specify the axis, scaled by the angle – Only 3 numbers, but hard to compose• Euler angles: – First, how much to rotate about X – Second, how much to rotate about Y – Final, how much to rotate about Z • Hard to think about, and hard to compose• Quaternions Video Processing Lab 31 臺灣師範大學數位媒體中心視訊處理研究室
  32. 32. Quaternions• 4-vector related to axis and angle, unit magnitude – Rotation about axis (x,y,z) by angles θ:  x  cos   sin 0 0  x   y   sin cos  0 0  y      z   0 0 1 0 z        1   0 0 0 1 1  • Easy to compose • Easy to find rotation matrix Video Processing Lab 32 臺灣師範大學數位媒體中心視訊處理研究室
  33. 33. Transformation in OpenGL• Transformation pipeline & matrices – Current Transformation Matrix (CTM) – CTM operations – CTM in OpenGL – OpenGL matrices Video Processing Lab 33 臺灣師範大學數位媒體中心視訊處理研究室
  34. 34. Transformation pipeline & matrices object eye Projection Modelview Matrix vertex matrix modelview projection modelview• OpenGL matrices have three types – Model-View (GL_MODEL_VIEW) – Projection (GL_PROJECTION) – Texture (GL_TEXTURE) (ignore for now) Video Processing Lab 34 臺灣師範大學數位媒體中心視訊處理研究室
  35. 35. Current Transformation Matrix (CTM)• CTM is a 4x4 homogeneous coordinate matrix – It can be altered by a set of function – It is defined in the user program – and loaded into a transformation unit C Current matrix P P’=CP vertices CTM vertices Video Processing Lab 35 臺灣師範大學數位媒體中心視訊處理研究室
  36. 36. CTM operations P Current matrix C P’=CP CTM vertices vertices• CTM can be altered by loading new matrix or by postmultiply matrix – Load form glLoadIdentity(); • identity matrix: C←I T: glTranslatef(dx, dy, dz); • an arbitrary matrix: C←M R: glRotatef(angle, vx, vy, vz); • translation matrix: C←T … S: glScalef(sx, sy, sz); – Postmultiply form glMultMatrixf( ); • an arbitrary matrix: C←CM User input matrix • a translation matrix: C←CT • a rotation matrix: C←CR … Video Processing Lab 36 臺灣師範大學數位媒體中心視訊處理研究室
  37. 37. Example by point rotation • Rotation with an arbitrary point – Order of transformations in OpenGL (one step = one function call)Initial • Loading an identity matrix: C←I • Translation Origin to the position: C← CT • Rotation: C← CR • Translate back: C← CT-1 – Result: C= TRT-1 Video Processing Lab 37 臺灣師範大學數位媒體中心視訊處理研究室
  38. 38. CTM in OpenGL• In OpenGL, CTM has the model-view matrix and the projection matrix CTM Modelview Projection vertices vertices matrix matrix Geometric transformations glMatrixMode routine – Manipulate those matrices by concatenation and start from first setting matrix Video Processing Lab 38 臺灣師範大學數位媒體中心視訊處理研究室
  39. 39. OpenGL matrices (1)• Current matrix glMatrixMode (GL_MODELVIEW|GL_PROJECTION)• Arbitrary matrix – Load 16-elements array glLoadMatrix* (elems); • A suffix code: f or d • The elements must be specified in column order – First list 4-elements in first-column – … – Finally the fourth column – Stack & store the Matrix glPushMatrix (); glPopMatrix (); Video Processing Lab 39 臺灣師範大學數位媒體中心視訊處理研究室
  40. 40. OpenGL matrices (2)• Multiple by two arbitrary matrices C<-M2M1 glLoadIdentity(); glMultMatrixf(elemsM2); glMultMatrixf(elemsM1);• Access matrices by query functions glGetIntegerv glGetFloatv glGetBooleanv glGetdoublev glIsEnabled… Video Processing Lab 40 臺灣師範大學數位媒體中心視訊處理研究室
  41. 41. Summery• Rotation related with axis and the origin – Use the same trick as in 2D: • Translate origin to the position • Rotate • translate back again• Rotation is not commutative – Rotation order matters – Experiment to convince by yourself Video Processing Lab 41 臺灣師範大學數位媒體中心視訊處理研究室
  42. 42. Transformation trick• Rotation and Translation are the rigid-body transformations – Do not change lengths, sizes or angles, so a body does not deform• Scale, shear… extend naturally transformation from 2D to 3D Video Processing Lab 42 臺灣師範大學數位媒體中心視訊處理研究室
  43. 43. Triangle’s rotation at arbitrary point• vertices tri = {{50.0, 25.0}, {150.0, 40.0}, {100.0, 100.0}}; //set object’s vertices• Centpt; //find center point to describe the triangle• glLoadIdentity();• glTranslatef(); //translate the center point• glRotatef(angle, vx,vy,vz); //rotate the center point, axis=(vx,vy,vz), angle: user define• glTranslatef(); //translate return Video Processing Lab 43 臺灣師範大學數位媒體中心視訊處理研究室
  44. 44. Middle project• Make some visual components by yourself – more than three object’s from HW1 & HW2 • Practice each one composition (rotation, scaling, translate, shear and reflection) – Note: original & new • Practice combining two or three compositions• Team work (2~3) Video Processing Lab 44 臺灣師範大學數位媒體中心視訊處理研究室
  45. 45. Reference• http://www.cs.wisc.edu/~schenney/• http://graphics.csie.ntu.edu.tw/~robi n/courses/3dcg06/• http://www.cse.psu.edu/~cg418/• http://groups.csail.mit.edu/graphics/ classes/6.837 Video Processing Lab 45 臺灣師範大學數位媒體中心視訊處理研究室
  46. 46. Inverse matrix• Identify matrix (Inxn) 1 0 0 I I3 x 3  0 1 0  – MM-1=I,   1 M  M  0 0 1   – Inverse matrix (M-1 ) 1 0 tx   1 0 t x  T  0 1 t y    T 1  0 1 t y    0 0 1    0 0 1    cos  sin 0  cos sin 0 R   sin  cos 0  R 1    sin  cos 0   RT   0  0 1   0  0 1 Video Processing Lab 46 臺灣師範大學數位媒體中心視訊處理研究室
  47. 47. 2D Reflection Reflection line y=0 Reflection line x=0 Reflection line y=-x y y y x x x• Transformation matrix 1 0 0  1 0 0   0 1 0  0 1 0   0 1 0  1 0 0         0 0 1    0 0 1  0 0 1     Video Processing Lab 47 臺灣師範大學數位媒體中心視訊處理研究室

×