Demetriou/Loizidou – ACSC330 – Chapter 4 Geometric Objects and Transformations Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science.

Slides:



Advertisements
Similar presentations
Computer Graphics - Geometry & Representation -
Advertisements

Vector Calculus Mengxia Zhu Fall Objective Review vector arithmetic Distinguish points and vectors Relate geometric concepts to their algebraic.
UBI 516 Advanced Computer Graphics
Chapter 4.1 Mathematical Concepts. 2 Applied Trigonometry Trigonometric functions Defined using right triangle  x y h.
CS5500 Computer Graphics March 22, Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002 Coordinate-Free Geometry When we learned simple.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Geometry Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and.
Transformations. 2 Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002 Coordinate-Free Geometry When we learned simple geometry, most of us.
Vectors in the Plane and in Three-Dimensional Space.
Vectors.
Section 9.2 Vectors Goals Goals Introduce vectors. Introduce vectors. Begin to discuss operations with vectors and vector components. Begin to discuss.
2IV60 Computer Graphics Basic Math for CG
Computer Graphics Lecture 10 Fasih ur Rehman. Last Class Viewing – Perspectives – Projections.
Chapter 3 Vectors.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Graphics CSE 581 – Interactive Computer Graphics Mathematics for Computer Graphics CSE 581 – Roger Crawfis (slides developed from Korea University slides)
Math Primer for CG Ref: Interactive Computer Graphics, Chap. 4, E. Angel.
Kansas State University Department of Computing and Information Sciences CIS 736: Computer Graphics Monday, 26 January 2004 William H. Hsu Department of.
 A ABAB VECTORS Elements of a set V for which two operations are defined: internal  (addition) and external  (multiplication by a number), example.
Geometry CSC 2141 Introduction to Computer Graphics.
1 Geometry. 2 Objectives Introduce the elements of geometry ­Scalars ­Vectors ­Points Develop mathematical operations among them in a coordinate-free.
Kansas State University Department of Computing and Information Sciences CIS 736: Computer Graphics Wednesday, January 19, 2000 William H. Hsu Department.
3-2 Vectors and Scalars  Is a number with units. It can be positive or negative. Example: distance, mass, speed, Temperature… Chapter 3 Vectors  Scalar.
1.1 – 1.2 The Geometry and Algebra of Vectors.  Quantities that have magnitude but not direction are called scalars. Ex: Area, volume, temperature, time,
Mathematical Foundations Sections A-1 to A-5 Some of the material in these slides may have been adapted from university of Virginia, MIT and Åbo Akademi.
Intro to 3D Models Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Mathematics for Graphics. 1 Objectives Introduce the elements of geometry  Scalars  Vectors  Points Develop mathematical operations among them in a.
Section 10.2a VECTORS IN THE PLANE. Vectors in the Plane Some quantities only have magnitude, and are called scalars … Examples? Some quantities have.
Copyright © Cengage Learning. All rights reserved. 12 Vectors and the Geometry of Space.
2-D motion (no projectiles – yet) Physics Chapter 3 section 1 Pages
Geometric Objects and Transformation
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 52 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 52 Computer Graphics Three-Dimensional Graphics I.
Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.
VECTORS (Ch. 12) Vectors in the plane Definition: A vector v in the Cartesian plane is an ordered pair of real numbers:  a,b . We write v =  a,b  and.
Chapter 3 Vectors. Vector quantities  Physical quantities that have both numerical and directional properties Mathematical operations of vectors in this.
CS 551 / 645: Introductory Computer Graphics Mathematical Foundations.
Chun-Yuan Lin Mathematics for Computer Graphics 2015/12/15 1 CG.
Chapter 3 Vectors. Vectors – physical quantities having both magnitude and direction Vectors are labeled either a or Vector magnitude is labeled either.
Computer Graphics I, Fall 2010 Geometry.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Geometry.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
수학 모든 자연 법칙은 크기 사이의 관계 (Correlation) 를 나타냄 (Functional Relationship) y = f(x) x, y : quantity, Analytic Method Dependency of y on x Implicit Function F(x,
David Luebke6/12/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
Graphics Graphics Korea University kucg.korea.ac.kr Mathematics for Computer Graphics 고려대학교 컴퓨터 그래픽스 연구실.
Introduction; Mathematical Foundations CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
CA 302 Computer Graphics and Visual Programming
CS5500 Computer Graphics March 6, 2006.
Spaces.
Mathematics for Computer Graphics
Number Properties and Operations
Statics of Particles.
Chapter 3 Vectors.
Vectors.
Chapter 3 Vectors September 17, 2018 Chap 3.
CSC461: Lecture 12 Geometry Objectives
Lecture 03: Linear Algebra
Copyright © 2014 John Wiley & Sons, Inc. All rights reserved.
Chapter 3 Vectors.
Chapter 3 Vectors.
SCI340_L05 _vectors.ppt More math concepts
Vectors More math concepts.
Introduction to Computer Graphics with WebGL
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
CSE 411 Computer Graphics Lecture #2 Mathematical Foundations
2 Vectors in 2-space and 3-space
PPT&Programs
6.3 Vectors in the Plane Ref. p. 424.
Game Programming Algorithms and Techniques
Presentation transcript:

Demetriou/Loizidou – ACSC330 – Chapter 4 Geometric Objects and Transformations Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science Frederick Institute of Technology

Demetriou/Loizidou – ACSC330 – Chapter 4 2 Objectives  Introduce the elements of geometry  Scalars  Vectors  Points  Develop mathematical operations among them in a coordinate-free manner  Define basic primitives  Line segments  Polygons

Demetriou/Loizidou – ACSC330 – Chapter 4 3 Basic Elements  Geometry is the study of the relationships among objects in an n-dimensional space  In computer graphics, we are interested in objects that exist in three dimensions  Want a minimum set of primitives from which we can build more sophisticated objects  We will need three basic elements  Scalars  Vectors  Points

Demetriou/Loizidou – ACSC330 – Chapter 4 4 Coordinate-Free Geometry  When we learned simple geometry, most of us started with a Cartesian approach  Points were at locations in space p=(x,y,z)  We derived results by algebraic manipulations involving these coordinates  This approach was nonphysical  Physically, points exist regardless of the location of an arbitrary coordinate system  Most geometric results are independent of the coordinate system  Euclidean geometry: two triangles are identical if two corresponding sides and the angle between them are identical

Demetriou/Loizidou – ACSC330 – Chapter 4 5 Scalars  Need three basic elements in geometry  Scalars, Vectors, Points  Scalars can be defined as members of sets which can be combined by two operations (addition and multiplication) obeying some fundamental axioms (associativity, commutivity, inverses)  Examples include the real and complex number under the ordinary rules with which we are familiar  Scalars alone have no geometric properties

Demetriou/Loizidou – ACSC330 – Chapter 4 6 Vectors  Physical definition: a vector is a quantity with two attributes  Direction  Magnitude  Examples include  Force  Velocity  Directed line segments  Most important example for graphics  Can map to other types v

Demetriou/Loizidou – ACSC330 – Chapter 4 7 Vector Operations  Every vector has an inverse  Same magnitude but points in opposite direction  Every vector can be multiplied by a scalar  There is a zero vector  Zero magnitude, undefined orientation  The sum of any two vectors is a vector  Use head-to-tail axiom v -v vv v u w

Demetriou/Loizidou – ACSC330 – Chapter 4 8 Linear Vector Spaces  Mathematical system for manipulating vectors  Operations  Scalar-vector multiplication u =  v  Vector-vector addition w = u + v  Expressions such as v=u+2w-3r Make sense in a vector space

Demetriou/Loizidou – ACSC330 – Chapter 4 9 Vectors Lack Position  These vectors are identical  Same length and magnitude  Vectors spaces insufficient for geometry  Need points

Demetriou/Loizidou – ACSC330 – Chapter 4 10 Points  Location in space  Operations allowed between points and vectors  Point-point subtraction yields a vector  Equivalent to point-vector addition P = v+Q v = P-QS

Demetriou/Loizidou – ACSC330 – Chapter 4 11 Affine Spaces  Point + a vector space  Operations  Vector-vector addition  Scalar-vector multiplication  Point-vector addition  Scalar-scalar operations  For any point define  1 P = P  0 P = 0 (zero vector)

Demetriou/Loizidou – ACSC330 – Chapter 4 12 Lines  Consider all points of the form  P(  ) = P 0 +  d  Set of all points that pass through P 0 in the direction of the vector d

Demetriou/Loizidou – ACSC330 – Chapter 4 13 Parametric Form  This form is known as the parametric form of the line  More robust and general than other forms  Extends to curves and surfaces  Two-dimensional forms  Explicit: y = mx +h  Implicit: ax + by +c = 0  Parametric: x(  ) =  x 0 + (1-  )x 1 y(  ) =  y 0 + (1-  )y 1

Demetriou/Loizidou – ACSC330 – Chapter 4 14 Rays and Line Segments  If  >= 0, then P(  ) is the ray leaving P 0 in the direction d If we use two points to define v, then P(  ) = Q +  (R-Q)=Q+  v =  R + (1-  )Q For 0<=  <=1 we get all the points on the line segment joining R and Q

Demetriou/Loizidou – ACSC330 – Chapter 4 15 Convexity  An object is convex iff for any two points in the object all points on the line segment between these points are also in the object P Q Q P

Demetriou/Loizidou – ACSC330 – Chapter 4 16 Affine Sums  Consider the “sum” P =  1 P 1 +  2 P 2 +…..+  n P n Can show by induction that this sum makes sense iff  1 +  2 +…..  n = 1 in which case we have the affine sum of the points P 1  P 2,…..P n  If, in addition,  i >=0, we have the convex hull of P 1  P 2,…..P n

Demetriou/Loizidou – ACSC330 – Chapter 4 17 Convex Hull  Smallest convex object containing P 1  P 2,…..P n  Formed by “shrink wrapping” points

Demetriou/Loizidou – ACSC330 – Chapter 4 18 Curves and Surfaces  Curves are one parameter entities of the form P(  ) where the function is nonlinear  Surfaces are formed from two-parameter functions P( ,  )  Linear functions give planes and polygons P(  ) P( ,  )

Demetriou/Loizidou – ACSC330 – Chapter 4 19 Planes  A plane be determined by a point and two vectors or by three points P( ,  )=R+  u+  v P( ,  )=R+  (Q-R)+  (P-Q)

Demetriou/Loizidou – ACSC330 – Chapter 4 20 Triangles convex sum of P and Q convex sum of S(  ) and R for 0<= ,  <=1, we get all points in triangle

Demetriou/Loizidou – ACSC330 – Chapter 4 21 Normals  Every plane has a vector n normal (perpendicular, orthogonal) to it  From point-two vector form P( ,  )=R+  u+  v, we know we can use the cross product to find n = u  v and the equivalent form (P(  )-P)  n=0 u v P

Demetriou/Loizidou – ACSC330 – Chapter 4 22 Linear Independence  A set of vectors v 1, v 2, …, v n is linearly independent if v 1 + v v n =0 iff  1 =  2 =…=0  If a set of vectors is linearly independent, we cannot represent one in terms of the others  If a set of vectors is linearly dependent, as least one can be written in terms of the others

Demetriou/Loizidou – ACSC330 – Chapter 4 23 Dimension  In a vector space, the maximum number of linearly independent vectors is fixed and is called the dimension of the space  In an n-dimensional space, any set of n linearly independent vectors form a basis for the space  Given a basis v 1, v 2,…., v n, any vector v can be written as v=  1 v 1 +  2 v 2 +….+  n v n where the {  i } are unique

Demetriou/Loizidou – ACSC330 – Chapter 4 24 Representation  Until now we have been able to work with geometric entities without using any frame of reference, such a coordinate system  Need a frame of reference to relate points and objects to our physical world.  For example, where is a point? Can’t answer without a reference system  World coordinates  Camera coordinates

Demetriou/Loizidou – ACSC330 – Chapter 4 25 Coordinate Systems  Consider a basis v 1, v 2,…., v n  A vector is written v=  1 v 1 +  2 v 2 +….+  n v n  The list of scalars {  1,  2, ….  n } is the representation of v with respect to the given basis  We can write the representation as a row or column array of scalars a=[  1  2 ….  n ] T =

Demetriou/Loizidou – ACSC330 – Chapter 4 26 Example  V=2v1+3v2-4v3  A=[2 3 –4]  Note that this representation is with respect to a particular basis  For example, in OpenGL we start by representing vectors using the world basis but later the system needs a representation in terms of the camera or eye basis

Demetriou/Loizidou – ACSC330 – Chapter 4 27 Coordinate Systems  Which is correct?  Both are because vectors have no fixed location v v

Demetriou/Loizidou – ACSC330 – Chapter 4 28 Frames  Coordinate System is insufficient to present points  If we work in an affine space we can add a single point, the origin, to the basis vectors to form a frame P0P0 v1v1 v2v2 v3v3

Demetriou/Loizidou – ACSC330 – Chapter 4 29 Frames (cont.)  Frame determined by (P 0, v 1, v 2, v 3 )  Within this frame, every vector can be written as v=  1 v 1 +  2 v 2 +….+  n v n  Every point can be written as P = P 0 +  1 v 1 +  2 v 2 +….+  n v n

Demetriou/Loizidou – ACSC330 – Chapter 4 30 Confusing Points and Vectors Consider the point and the vector P = P 0 +  1 v 1 +  2 v 2 +….+  n v n v=  1 v 1 +  2 v 2 +….+  n v n They appear to have the similar representations p=[  1  2  3 ] v=[  1  2  3 ] which confuse the point with the vector A vector has no position v p v can place anywhere fixed

Demetriou/Loizidou – ACSC330 – Chapter 4 31 A Single Representation If we define 0P = 0 and 1P =P then we can write v=  1 v 1 +  2 v 2 +  3 v 3 = [  1  2  3 0 ] [v 1 v 2 v 3 P 0 ] T P = P 0 +  1 v 1 +  2 v 2 +  3 v 3 = [  1  2  3 1 ] [v 1 v 2 v 3 P 0 ] T Thus we obtain the four-dimensional homogeneous coordinate representation v = [  1  2  3 0 ] T p = [  1  2  3 1 ] T

Demetriou/Loizidou – ACSC330 – Chapter 4 32 Homogeneous Coordinates  The general form of four dimensional homogeneous coordinates is p=[x y x w] T  We return to a three dimensional point (for w  0 ) by x  x/w y  y/w z  z/w  If w=0, the representation is that of a vector  Note that homogeneous coordinates replaces points in three dimensions by lines through the origin in four dimensions

Demetriou/Loizidou – ACSC330 – Chapter 4 33 Homogeneous Coordinates and Computer Graphics  Homogeneous coordinates are key to all computer graphics systems  All standard transformations (rotation, translation, scaling) can be implemented by matrix multiplications with 4 x 4 matrices  Hardware pipeline works with 4 dimensional representations  For orthographic viewing, we can maintain w=0 for vectors and w=1 for points  For perspective we need a perspective division

Demetriou/Loizidou – ACSC330 – Chapter 4 34 Change of Coordinate Systems  Consider two representations of a the same vector with respect to two different bases. The representations are v =  1 v 1 +  2 v 2 +  3 v 3 = [  1  2  3 ] [v 1 v 2 v 3 ] T =  1 u 1 +  2 u 2 +  3 u 3 = [  1  2  3 ] [u 1 u 2 u 3 ] T a=[  1  2  3 ] b=[  1  2  3 ] where

Demetriou/Loizidou – ACSC330 – Chapter 4 35 Representing second basis in terms of first  Each of the basis vectors, u1,u2, u3, are vectors that can be represented in terms of the first basis u 1 =  11 v 1 +  12 v 2 +  13 v 3 u 2 =  21 v 1 +  22 v 2 +  23 v 3 u 3 =  31 v 1 +  32 v 2 +  33 v 3 v

Demetriou/Loizidou – ACSC330 – Chapter 4 36 Matrix Form  The coefficients define a 3 x 3 matrix  The basis can be related by  See text for numerical examples a = M T b M =

Demetriou/Loizidou – ACSC330 – Chapter 4 37 Change of Frames  We can apply a similar process in homogeneous coordinates to the representations of both points and vectors  Consider two frames  Any point or vector can be represented in each (P 0, v 1, v 2, v 3 ) (Q 0, u 1, u 2, u 3 ) P0P0 v1v1 v2v2 v3v3 Q0Q0 u1u1 u2u2 u3u3

Demetriou/Loizidou – ACSC330 – Chapter 4 38 Representing One Frame in Terms of the Other u 1 =  11 v 1 +  12 v 2 +  13 v 3 u 2 =  21 v 1 +  22 v 2 +  23 v 3 u 3 =  31 v 1 +  32 v 2 +  33 v 3 Q 0 =  41 v 1 +  42 v 2 +  43 v 3 +  44 P 0  Extending what we did with change of bases defining a 4 x 4 matrix M =

Demetriou/Loizidou – ACSC330 – Chapter 4 39 Working with Representations  Within the two frames any point or vector has a representation of the same form a = [  1  2  3  4 ] in the first frame b = [  1  2  3  4 ] in the second frame  where  4   4  for points and  4   4  for vectors and  The matrix M is 4 x 4 and specifies an affine transformation in homogeneous coordinates a=M T b

Demetriou/Loizidou – ACSC330 – Chapter 4 40 Affine Transformations  Every linear transformation is equivalent to a change in frames  Every affine transformation preserves lines  However, an affine transformation has only 12 degrees of freedom because 4 of the elements in the matrix are fixed and are a subset of all possible 4 x 4 linear transformations

Demetriou/Loizidou – ACSC330 – Chapter 4 41 The World and Camera Frames  When we work with representations, we work with n-tuples or arrays of scalars  Changes in frame are then defined by 4 x 4 matrices  InOpenGL, the base frame that we start with is the world frame  Eventually we represent entities in the camera frame by changing the world representation using the model-view matrix  Initially these frames are the same ( M=I )

Demetriou/Loizidou – ACSC330 – Chapter 4 42 Moving the Camera If objects are on both sides of z=0, we must move camera frame M =

Demetriou/Loizidou – ACSC330 – Chapter 4 43 General Transformations  A transformation maps points to other points and/or vectors to other vectors Q=T(P) v=T(u)

Demetriou/Loizidou – ACSC330 – Chapter 4 44 Affine Transformations  Line preserving  Characteristic of many physically important transformations  Rigid body transformations: rotation, translation  Scaling, shear  Importance in graphics is that we need only transform endpoints of line segments and let implementation draw line segment between the transformed endpoints

Demetriou/Loizidou – ACSC330 – Chapter 4 45 Pipeline Implementation transformationrasterizer u v u v T T(u) T(v) T(u) T(v) vertices pixels frame buffer (from application program)

Demetriou/Loizidou – ACSC330 – Chapter 4 46 Notation  We will be working with both coordinate-free representations of transformations and representations within a particular frame  P,Q, R: points in an affine space  u, v, w : vectors in an affine space  , ,  : scalars  p, q, r : representations of points -array of 4 scalars in homogeneous coordinates  u, v, w : representations of points -array of 4 scalars in homogeneous coordinates

Demetriou/Loizidou – ACSC330 – Chapter 4 47 Translation  Move (translate, displace) a point to a new location  Displacement determined by a vector d  Three degrees of freedom  P’ = P+d P P’ d

Demetriou/Loizidou – ACSC330 – Chapter 4 48 How many ways? Although we can move a point to a new location in infinite ways, when we move many points there is usually only one way objecttranslation: every point displaced by same vector

Demetriou/Loizidou – ACSC330 – Chapter 4 49 Translation Using Representations  Using the homogeneous coordinate representation in some frame p=[ x y z 1] T p’=[x’ y’ z’ 1] T d=[dx dy dz 0] T  Hence p’ = p + d or x’=x+d x y’=y+d y z’=z+d z note that this expression is in four dimensions and expresses that point = vector + point

Demetriou/Loizidou – ACSC330 – Chapter 4 50 Translation Matrix  We can also express translation using a 4 x 4 matrix T in homogeneous coordinates p ’= Tp where  This form is better for implementation because all affine transformations can be expressed this way and multiple transformations can be concatenated together

Demetriou/Loizidou – ACSC330 – Chapter 4 51 Rotation (2D)  Consider rotation about the origin by  degrees  radius stays the same, angle increases by  x’=x cos  –y sin  y’ = x sin  + y cos  x = r cos  y = r sin  x = r cos (  y = r sin ( 

Demetriou/Loizidou – ACSC330 – Chapter 4 52 Rotation about the z axis  Rotation about z axis in three dimensions leaves all points with the same z  Equivalent to rotation in two dimensions in planes of constant z  or in homogeneous coordinates p ’= R z (  )p x’ = x cos  – y sin  y’ = x sin  + y cos  z’ = z

Demetriou/Loizidou – ACSC330 – Chapter 4 53 Rotation Matrix

Demetriou/Loizidou – ACSC330 – Chapter 4 54 Rotation about x and y axes  Same argument as for rotation about z axis  For rotation about x axis, x is unchanged  For rotation about y axis, y is unchanged

Demetriou/Loizidou – ACSC330 – Chapter 4 55 Scaling x’=s x x y’=s y x z’=s z x p’=Sp  Expand or contract along each axis (fixed point of origin)

Demetriou/Loizidou – ACSC330 – Chapter 4 56 Reflection corresponds to negative scale factors original s x = -1 s y = 1 s x = -1 s y = -1s x = 1 s y = -1

Demetriou/Loizidou – ACSC330 – Chapter 4 57 Inverses  Although we could compute inverse matrices by general formulas, we can use simple geometric observations  Translation: T -1 (d x, d y, d z ) = T (-d x, -d y, -d z )  Rotation: R -1 (  ) = R(-  )  Holds for any rotation matrix  Note that since cos(-  ) = cos(  ) and sin(-  )=-sin(  ) R -1 (  ) = R T (  )  Scaling: S -1 (s x, s y, s z ) = S(1/s x, 1/s y, 1/s z )

Demetriou/Loizidou – ACSC330 – Chapter 4 58 Concatenation  We can form arbitrary affine transformation matrices by multiplying together rotation, translation, and scaling matrices  Because the same transformation is applied to many vertices, the cost of forming a matrix M=ABCD is not significant compared to the cost of computing Mp for many vertices p  The difficult part is how to form a desired transformation from the specifications in the application

Demetriou/Loizidou – ACSC330 – Chapter 4 59 Order of Transformations  Note that matrix on the right is the first applied  Mathematically, the following are equivalent p’ = ABCp = A(B(Cp))  Note many references use column matrices to present points. In terms of column matrices p T ’ = p T C T B T A T

Demetriou/Loizidou – ACSC330 – Chapter 4 60  A rotation by θ about an arbitrary axis can be decomposed into the concatenation of rotations about the x, y, and z axes  Note that rotations do not commute  We can use rotations in another order but with different angles General Rotation About the Origin  x z y v R(  ) = R z (  z ) R y (  y ) R x (  x )  x  y  z are called the Euler angles

Demetriou/Loizidou – ACSC330 – Chapter 4 61 Rotation About a Fixed Point other than the Origin  Steps 1.Move fixed point to origin 2.Rotate 3.Move fixed point back M = T(-p f ) R(  ) T(p f )

Demetriou/Loizidou – ACSC330 – Chapter 4 62 Instancing  In modeling, we often start with a simple object centered at the origin, oriented with the axis, and at a standard size  We apply an instance transformation to its vertices to Scale Orient Locate

Demetriou/Loizidou – ACSC330 – Chapter 4 63 Shear  Helpful to add one more basic transformation  Equivalent to pulling faces in opposite directions

Demetriou/Loizidou – ACSC330 – Chapter 4 64 Shear Matrix Consider simple shear along x axis x’ = x + y cot  y’ = y z’ = z H(  ) =

Demetriou/Loizidou – ACSC330 – Chapter 4 65 OpenGL Matrices  In OpenGL matrices are part of the state  Three types  Model-View ( GL_MODEL_VIEW )  Projection ( GL_PROJECTION )  Texture ( GL_TEXTURE ) (ignore for now)  Single set of functions for manipulation  Select which to manipulated by  glMatrixMode(GL_MODEL_VIEW);  glMatrixMode(GL_PROJECTION);

Demetriou/Loizidou – ACSC330 – Chapter 4 66 Current Transformation Matrix (CTM)  Conceptually there is a 4 x 4 homogeneous coordinate matrix, the current transformation matrix (CTM) that is part of the state and is applied to all vertices that pass down the pipeline  The CTM is defined in the user program and loaded into a transformation unit CTMvertices p p’=Cp C

Demetriou/Loizidou – ACSC330 – Chapter 4 67 CTM operations  The CTM can be altered either by loading a new CTM or by postmutiplication Load an identity matrix: C  I Load an arbitrary matrix: C  M Load a translation matrix: C  T Load a rotation matrix: C  R Load a scaling matrix: C  S Postmultiply by an arbitrary matrix: C  CM Postmultiply by a translation matrix: C  CT Postmultiply by a rotation matrix: C  C R Postmultiply by a scaling matrix: C  C S

Demetriou/Loizidou – ACSC330 – Chapter 4 68 Rotation about a Fixed Point  Steps 1.Start with identity matrix: C  I 2.Move fixed point to origin: C  CT -1 3.Rotate: C  CR 4.Move fixed point back: C  CT  Result : C = T -1 RT  Each operation corresponds to one function call in the program.  Note that the last operation specified is the first executed in the program

Demetriou/Loizidou – ACSC330 – Chapter 4 69 CTM in OpenGL  OpenGL has a model-view and a projection matrix in the pipeline which are concatenated together to form the CTM  Can manipulate each by first setting the matrix mode

Demetriou/Loizidou – ACSC330 – Chapter 4 70 Rotation, Translation, Scaling glRotatef(theta, vx, vy, vz) glTranslatef(dx, dy, dz) glScalef( sx, sy, sz) glLoadIdentity() Load an identity matrix: Multiply on right: theta in degrees, ( vx, vy, vz ) define axis of rotation Each has a float (f) and double (d) format ( glScaled )

Demetriou/Loizidou – ACSC330 – Chapter 4 71 Example  Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0)  Remember that last matrix specified in the program is the first applied glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(1.0, 2.0, 3.0); glRotatef(30.0, 0.0, 0.0, 1.0); glTranslatef(-1.0, -2.0, -3.0);

Demetriou/Loizidou – ACSC330 – Chapter 4 72 Arbitrary Matrices  Can load and multiply by matrices defined in the application program  The matrix m is a one dimension array of 16 elements which are the components of the desired 4 x 4 matrix stored by columns  In glMultMatrixf, m multiplies the existing matrix on the right glLoadMatrixf(m) glMultMatrixf(m)

Demetriou/Loizidou – ACSC330 – Chapter 4 73 Matrix Stacks  In many situations we want to save transformation matrices for use later  Traversing hierarchical data structures (Chapter 9)  Avoiding state changes when executing display lists  OpenGL maintains stacks for each type of matrix  Access present type (as set by glMatrixMode) by glPushMatrix() glPopMatrix()

Demetriou/Loizidou – ACSC330 – Chapter 4 74 Reading Back Matrices  Can also access matrices (and other parts of the state) by enquiry (query) functions  For matrices, we use as glGetIntegerv glGetFloatv glGetBooleanv glGetDoublev glIsEnabled double m[16]; glGetFloatv(GL_MODELVIEW, m);

Demetriou/Loizidou – ACSC330 – Chapter 4 75 Using Transformations  Example: use idle function to rotate a cube and mouse function to change direction of rotation  Start with a program that draws a cube ( colorcube.c ) in a standard way  Centered at origin  Sides aligned with axes  Will discuss modeling in next lecture

Demetriou/Loizidou – ACSC330 – Chapter 4 76 main.c void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); glutMainLoop(); }

Demetriou/Loizidou – ACSC330 – Chapter 4 77 Idle and Mouse callbacks void spinCube() { theta[axis] += 2.0; if( theta[axis] > ) theta[axis] -= 360.0; glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; }

Demetriou/Loizidou – ACSC330 – Chapter 4 78 Display callback void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glutSwapBuffers(); } Note that because of fixed from of callbacks, variables such as theta and axis must be defined as globals Camera information is in standard reshape callback

Demetriou/Loizidou – ACSC330 – Chapter 4 79 Using the Model-View Matrix  In OpenGL the model-view matrix is used to  Position the camera  Can be done by rotations and translations but is often easier to use gluLookAt (Chapter 5)  Build models of obejcts  The projection matrix is used to define the view volume and to select a camera lens  Although both are manipulated by the same functions, we have to be careful because incremental changes are always made by postmultiplication  For example, rotating model-view and projection matrices by the same matrix are not equivalent operations. Postmultiplication of the model-view matrix is equivalent to premultiplication of the projection matrix

Demetriou/Loizidou – ACSC330 – Chapter 4 80 Smooth Rotation  From a practical standpoint, we are often want to use transformations to move and reorient an object smoothly  Problem: find a sequence of model-view matrices M 0,M 1,…..,M n so that when they are applied successively to one or more objects we see a smooth transition  For orientating an object, we can use the fact that every rotation corresponds to part of a great circle on a sphere  Find the axis of rotation and angle  Virtual trackball (see text)

Demetriou/Loizidou – ACSC330 – Chapter 4 81 Incremental Rotation  Consider the two approaches  For a sequence of rotation matrices R 0,R 1,…..,R n, find the Euler angles for each and use R i = R iz R iy R ix  Not very efficient  Use the final positions to determine the axis and angle of rotation, then increment only the angle  Quaternions can be more efficient than either

Demetriou/Loizidou – ACSC330 – Chapter 4 82 Quaternions  Extension of imaginary numbers from two to three dimensions  Requires one real and three imaginary components i, j, k  Quaternions can express rotations on sphere smoothly and efficiently. Process:  Model-view matrix  quaternion  Carry out operations with quaternions  Quaternion  Model-view matrix q=q 0 +q 1 i+q 2 j+q 3 k

Demetriou/Loizidou – ACSC330 – Chapter 4 83 Interfaces  One of the major problems in interactive computer graphics is how to use two-dimensional devices such as a mouse to interface with three dimensional objects  Example: how to form an instance matrix?  Some alternatives  Virtual trackball  3D input devices such as the spaceball  Use areas of the screen  Distance from center controls angle, position, scale depending on mouse button depressed

Demetriou/Loizidou – ACSC330 – Chapter 4 84 Representing a Mesh  Consider a mesh  There are 8 nodes and 12 edges  5 interior polygons  6 interior (shared) edges  Each vertex has a location v i = (x i y i z i ) v1v1 v2v2 v7v7 v6v6 v8v8 v5v5 v4v4 v3v3 e1e1 e8e8 e3e3 e2e2 e 11 e6e6 e7e7 e 10 e5e5 e4e4 e9e9 e 12

Demetriou/Loizidou – ACSC330 – Chapter 4 85 Simple Representation  List all polygons by their geometric locations  Leads to OpenGL code such as  Inefficient and unstructured  Consider moving a vertex to a new locations glBegin(GL_POLYGON); glVertex3f(x1, x1, x1); glVertex3f(x6, x6, x6); glVertex3f(x7, x7, x7); glEnd();

Demetriou/Loizidou – ACSC330 – Chapter 4 86 Inward and Outward Facing Polygons  The order {v 1, v 6, v 7 } and {v 6, v 7, v 1 } are equivalent in that the same polygon will be rendered by OpenGL but the order {v 1, v 7, v 6 } is different  The first two describe outwardly facing polygons  Use the right-hand rule = counter-clockwise encirclement of outward-pointing normal  OpenGL treats inward and outward facing polygons differently

Demetriou/Loizidou – ACSC330 – Chapter 4 87 Geometry vs Topology  Generally it is a good idea to look for data structures that separate the geometry from the topology  Geometry: locations of the vertices  Topology: organization of the vertices and edges  Example: a polygon is an ordered list of vertices with an edge connecting successive pairs of vertices and the last to the first  Topology holds even if geometry changes

Demetriou/Loizidou – ACSC330 – Chapter 4 88 Vertex Lists  Put the geometry in an array  Use pointers from the vertices into this array  Introduce a polygon list x 1 y 1 z 1 x 2 y 2 z 2 x 3 y 3 z 3 x 4 y 4 z 4 x 5 y 5 z 5. x 6 y 6 z 6 x 7 y 7 z 7 x 8 y 8 z 8 P1 P2 P3 P4 P5 v1v7v6v1v7v6 v8v5v6v8v5v6 topology geometry

Demetriou/Loizidou – ACSC330 – Chapter 4 89 Shared Edges  Vertex lists will draw filled polygons correctly but if we draw the polygon by its edges, shared edges are drawn twice  Can store mesh by edge list

Demetriou/Loizidou – ACSC330 – Chapter 4 90 Edge List v1v1 v2v2 v7v7 v6v6 v8v8 v5v5 v3v3 e1e1 e8e8 e3e3 e2e2 e 11 e6e6 e7e7 e 10 e5e5 e4e4 e9e9 e 12 e1 e2 e3 e4 e5 e6 e7 e8 e9 x 1 y 1 z 1 x 2 y 2 z 2 x 3 y 3 z 3 x 4 y 4 z 4 x 5 y 5 z 5. x 6 y 6 z 6 x 7 y 7 z 7 x 8 y 8 z 8 v1 v6 Note polygons are not represented

Demetriou/Loizidou – ACSC330 – Chapter 4 91 Modeling a Cube GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; Model a color cube for rotating cube program Define global arrays for vertices and colors

Demetriou/Loizidou – ACSC330 – Chapter 4 92 Drawing a polygon from a list of indices Draw a quadrilateral from a list of indices into the array vertices and use color corresponding to first index void polygon(int a, int b, int c, int d) { glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glVertex3fv(vertices[b]); glVertex3fv(vertices[c]); glVertex3fv(vertices[d]); glEnd(); }

Demetriou/Loizidou – ACSC330 – Chapter 4 93 Draw cube from faces void colorcube( ) { polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); } Note that vertices are ordered so that we obtain correct outward facing normals

Demetriou/Loizidou – ACSC330 – Chapter 4 94 Efficiency  The weakness of our approach is that we are building the model in the application and must do many function calls to draw the cube  Drawing a cube by its faces in the most straight forward way requires  6 glBegin, 6 glEnd  6 glColor  24 glVertex  More if we use texture and lighting

Demetriou/Loizidou – ACSC330 – Chapter 4 95 Vertex Arrays  OpenGL provides a facility called vertex arrays that allow us to store array data in the implementation  Six types of arrays supported  Vertices  Colors  Color indices  Normals  Texture coordinates  Edge flags  We will need only colors and vertices

Demetriou/Loizidou – ACSC330 – Chapter 4 96 Initialization  Using the same color and vertex data, first we enable glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);  Identify location of arrays glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); 3d arrays stored as floats data contiguous data array

Demetriou/Loizidou – ACSC330 – Chapter 4 97 Mapping indices to faces  Form an array of face indices  Each successive four indices describe a face of the cube  Draw through glDrawElements which replaces all glVertex and glColor calls in the display callback GLubyte cubeIndices[24] = {0,3,2,1,2,3,7,6 0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

Demetriou/Loizidou – ACSC330 – Chapter 4 98 Drawing the cube  Method 1:  Method 2: for(i=0; i<6; i++) glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cubeIndices[4*i]); format of index data start of index data what to draw number of indices glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); Draws cube with 1 function call!!

Demetriou/Loizidou – ACSC330 – Chapter 4 99 Physical Trackball  The trackball is an “upside down” mouse  If there is little friction between the ball and the rollers, we can give the ball a push and it will keep rolling yielding continuous changes  Two possible modes of operation  Continuous pushing or tracking hand motion  Spinning

Demetriou/Loizidou – ACSC330 – Chapter A Trackball from a Mouse  Problem: we want to get the two behavior modes from a mouse  We would also like the mouse to emulate a frictionless (ideal) trackball  Solve in two steps  Map trackball position to mouse position  Use GLUT to obtain the proper modes

Demetriou/Loizidou – ACSC330 – Chapter Trackball Frame origin at center of ball

Demetriou/Loizidou – ACSC330 – Chapter Projection of Trackball Position  We can relate position on trackball to position on a normalized mouse pad by projecting orthogonally onto pad

Demetriou/Loizidou – ACSC330 – Chapter Reversing Projection  Because both the pad and the upper hemisphere of the ball are two-dimensional surfaces, we can reverse the projection  A point (x,z) on the mouse pad corresponds to the point (x,y,z) on the upper hemisphere where y = if r  |x|  0, r  |z|  0

Demetriou/Loizidou – ACSC330 – Chapter Computing Rotations  Suppose that we have two points that were obtained from the mouse.  We can project them up to the hemisphere to points p 1 and p 2  These points determine a great circle on the sphere  We can rotate from p 1 to p  by finding the proper axis of rotation and the angle between the points

Demetriou/Loizidou – ACSC330 – Chapter Using the cross product  The axis of rotation is given by the normal to the plane determined by the origin, p 1, and p 2 n = p 1  p 1

Demetriou/Loizidou – ACSC330 – Chapter Obtaining the angle  The angle between p 1 and p 2 is given by  If we move the mouse slowly or sample its position frequently, then  will be small and we can use the approximation | sin  | = sin 

Demetriou/Loizidou – ACSC330 – Chapter Implementing with GLUT  We will use the idle, motion, and mouse callbacks to implement the virtual trackball  Define actions in terms of three booleans  trackingMouse : if true update trackball position  redrawContinue : if true, idle function posts a redisplay  trackballMove : if true, update rotation matrix

Demetriou/Loizidou – ACSC330 – Chapter Example  In this example, we use the virtual trackball to rotate the color cube we modeled earlier  The code for the colorcube function is omitted because it is unchanged from the earlier examples

Demetriou/Loizidou – ACSC330 – Chapter Initialization #define bool int /* if system does not support bool type */ #define false 0 #define true 1 #define M_PI /* if not in math.h */ int winWidth, winHeight; float angle = 0.0, axis[3], trans[3]; bool trackingMouse = false; bool redrawContinue = false; bool trackballMove = false; float lastPos[3] = {0.0, 0.0, 0.0}; int curx, cury; int startX, startY;

Demetriou/Loizidou – ACSC330 – Chapter The Projection Step voidtrackball_ptov(int x, int y, int width, int height, float v[3]) { float d, a; /* project x,y onto a hemisphere centered within width, height, note z is up here*/ v[0] = (2.0*x - width) / width; v[1] = (height - 2.0F*y) / height; d = sqrt(v[0]*v[0] + v[1]*v[1]); v[2] = cos((M_PI/2.0) * ((d < 1.0) ? d : 1.0)); a = 1.0 / sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); v[0] *= a; v[1] *= a; v[2] *= a; }

Demetriou/Loizidou – ACSC330 – Chapter glutMotionFunc (1) voidmouseMotion(int x, int y) { float curPos[3], dx, dy, dz; /* compute position on hemisphere */ trackball_ptov(x, y, winWidth, winHeight, curPos); if(trackingMouse) { /* compute the change in position on the hemisphere */ dx = curPos[0] - lastPos[0]; dy = curPos[1] - lastPos[1]; dz = curPos[2] - lastPos[2];

Demetriou/Loizidou – ACSC330 – Chapter glutMotionFunc (2) if (dx || dy || dz) { /* compute theta and cross product */ angle = 90.0 * sqrt(dx*dx + dy*dy + dz*dz); axis[0] = lastPos[1]*curPos[2] – lastPos[2]*curPos[1]; axis[1] = lastPos[2]*curPos[0] – lastPos[0]*curPos[2]; axis[2] = lastPos[0]*curPos[1] – lastPos[1]*curPos[0]; /* update position */ lastPos[0] = curPos[0]; lastPos[1] = curPos[1]; lastPos[2] = curPos[2]; } glutPostRedisplay(); }

Demetriou/Loizidou – ACSC330 – Chapter Idle and Display Callbacks void spinCube() { if (redrawContinue) glutPostRedisplay(); } void display() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if (trackballMove) { glRotatef(angle, axis[0], axis[1], axis[2]); } colorcube(); glutSwapBuffers(); }

Demetriou/Loizidou – ACSC330 – Chapter Mouse Callback void mouseButton(int button, int state, int x, int y) { if(button==GLUT_RIGHT_BUTTON) exit(0); /* holding down left button allows user to rotate cube */ if(button==GLUT_LEFT_BUTTON) switch(state) { case GLUT_DOWN: y=winHeight-y; startMotion( x,y); break; case GLUT_UP: stopMotion( x,y); break; }

Demetriou/Loizidou – ACSC330 – Chapter Start Function void startMotion(int x, int y) { trackingMouse = true; redrawContinue = false; startX = x; startY = y; curx = x; cury = y; trackball_ptov(x, y, winWidth, winHeight, lastPos); trackballMove=true; }

Demetriou/Loizidou – ACSC330 – Chapter Stop Function void stopMotion(int x, int y) { trackingMouse = false; /* check if position has changed */ if (startX != x || startY != y) redrawContinue = true; else { angle = 0.0; redrawContinue = false; trackballMove = false; }

Demetriou/Loizidou – ACSC330 – Chapter Quaternions  Because the rotations are on the surface of a sphere, quaternions provide an interesting and more efficient way to implement the trackball  See code in some of the standard demos included with Mesa