Toda s class Geometric objects and transformations Wednesda, November 7, 27 Computer Graphics - Class 5
Vector operations Review of vector operations needed for working in computer graphics adding two vectors subtracting two vectors scaling a vector magnitude of a vector normalizing a vector Wednesda, November 7, 27 Computer Graphics - Class 5 2
Linear combination of vectors Conve combinations (coefficients of linear combination are all non-negative and add up to ) Important conve combination: p(t) = a(-t) + b(t) where a and b are vectors and t If a and b are points, then p(t) = a(-t) + b(t) is said to interpolate the points a and b ( t ) Wednesda, November 7, 27 Computer Graphics - Class 5 3
Conve sets and hulls A conve set of points is a collection of points in which the line connecting an pair in the set lies entirel in the set Eamples: circle rectangle A conve hull is the smallest conve set that contains a specified collection of points Wednesda, November 7, 27 Computer Graphics - Class 5 4
Dot products cos θ = u a u b Vectors are orthogonal if dot product is Vectors are less than 9 apart if dot product is positive Vectors are more than 9 apart if dot product is negative Perpendicular projection of a onto b is (a u b ) u b Reflected ra is a -2(a u n ) u n where u n is a unit normal vector to the surface Wednesda, November 7, 27 Computer Graphics - Class 5 5
Cross products u = (u, u, u z ), v = (v, v, v z ) u v is perpendicular to both u and v Orientation follows the right hand rule u v = u v sin u v = θ u v u v u v z z z Wednesda, November 7, 27 Computer Graphics - Class 5 6
Polgons A polgon is a defined b a set of vertices and edges An interior angle of a polgon is an angle formed b two adjacent edges that is inside the boundar A conve polgon has ever interior angle < 8 A concave polgon is a polgon that is not conve Wednesda, November 7, 27 Computer Graphics - Class 5 7
Identifing concave polgons Concave polgons will have at least one interior angle > 8 The sin of this angle will be negative Thus, the magnitude of the cross product of the two vectors (edges) forming this angle will be negative This will also manifest itself b some z components of the cross product being positive and others being negative Wednesda, November 7, 27 Computer Graphics - Class 5 8
Splitting concave polgons Concave polgons present problems for computer graphics Splitting them up will get us conve polgons Define edge vector E k as the vector V k+ -V k, where the V s are adjacent vertices Calculate the edge cross products in order counter-clockwise around the polgon If an z component of a cross product is negative the polgon is concave and can be split along the line from the first edge in the cross product Wednesda, November 7, 27 Computer Graphics - Class 5 9
Triangularization A conve polgon can easil be triangularized (sometimes also referred to as tessellated) Define three consecutive vertices of the polgon as a triangle Remove the middle verte from the polgon s verte list Repeat with the modified list until onl three vertices remain, forming the last triangle Wednesda, November 7, 27 Computer Graphics - Class 5
Is a point inside a polgon? Even-odd test: construct a line from point to a point known to be outside polgon count # of intersections of line with boundar if odd, point is inside if crossing occurs at a verte look at endpoints of the edges that meet if both endpoints are on same side of line segment then the verte counts as an even number of crossings, otherwise it counts as an odd number Wednesda, November 7, 27 Computer Graphics - Class 5
N-gons An n-gon is a regular polgon of n sides Wednesda, November 7, 27 Computer Graphics - Class 5 2
Rosettes Rosettes are n-gons where each verte has been joined to each other verte Wednesda, November 7, 27 Computer Graphics - Class 5 3
A cube Consider a cube Pair off and come up with: A set of vertices for the cube A sequence of OpenGL statements to draw the cube How man function calls are ou making? Wednesda, November 7, 27 Computer Graphics - Class 5 4
Verte arras Allow ou to specif geometr with much fewer function calls For eample, the cube can be drawn in just one function call Create an arra of verte data and use glvertepointer() to refer to it Need to enable this with glenableclientstate (GL_VERTEX_ARRAY); Wednesda, November 7, 27 Computer Graphics - Class 5 5
glvertepointer() Four parameters: Size: number of coordinates per verte; must be 2, 3, or 4 Tpe: the data tpe of each coordinate in the arra Stride: the bte offset between consecutive vertices (frequentl ) Pointer: a pointer to the first coordinate of the first verte in the arra Wednesda, November 7, 27 Computer Graphics - Class 5 6
Inde arra Need a second arra with indices into the verte arra to specif which vertices make up each face of our geometr gldrawelements() is then used to draw the geometr Wednesda, November 7, 27 Computer Graphics - Class 5 7
gldrawelements() Four parameters: Mode: specified what kind of geometric primitives to render Count: number of vertices in the inde arra to be rendered Tpe: the tpe of the values in the inde arra; must be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT Indices: a pointer to the arra of indices into the verte arra Wednesda, November 7, 27 Computer Graphics - Class 5 8
Eample program rotatingcube.c demonstrates verte arras and introduces the idle callback function Wednesda, November 7, 27 Computer Graphics - Class 5 9
Transformation matrices A transformation matri is a matri that transforms a point to another point In 2-dimensions, this has the following algebraic form: a a 2 a a 2 22 p p = a a 2 p p + + a a 2 22 p p Wednesda, November 7, 27 Computer Graphics - Class 5 2
Wednesda, November 7, 27 Computer Graphics - Class 5 2 Uniform scaling Given a scale factor s >, a matri which produces a uniform scaling is Eample: s s = sp sp p p s s
Wednesda, November 7, 27 Computer Graphics - Class 5 22 Non-uniform scaling Given scale factors s > and s >, a matri which produces non-uniform scaling is Eample: s s = p s p s p p s s
Comparison of scalings Uniform scaling preserves shape Non-uniform scaling introduces distortion Wednesda, November 7, 27 Computer Graphics - Class 5 23
Two scalings in a row Two scalings in a row have a multiplicative effect P = S P P = S 2 P = S 2 S P Note the order of the scales appears to be reversed (S 2 before S ), but it s not Wednesda, November 7, 27 Computer Graphics - Class 5 24
Wednesda, November 7, 27 Computer Graphics - Class 5 25 Composite transformation matri The product of two or more transformation matrices is known as a composite transformation matri For our two scalings we have = s s s s s s s s 2 2 2 2
A side effect Note that an object which does not have the origin inside it will be moved as well b a scaling We will see how to correct this effect shortl Wednesda, November 7, 27 Computer Graphics - Class 5 26
Reflections Reflection through the -ais negates the coordinate: Reflection through the -ais negates the coordinate: Wednesda, November 7, 27 Computer Graphics - Class 5 27
Rotation Rotation transforms a point in a circular manner Positive angles impl a counter-clockwise rotation Wednesda, November 7, 27 Computer Graphics - Class 5 28
The geometr of rotation (, ) r r (,) θ φ (, ) = (r cos φ, r sin φ) (, ) = (r cos (φ+θ), r sin (φ+θ)) Wednesda, November 7, 27 Computer Graphics - Class 5 29
Rotation formula (, ) = (r cos φ, r sin φ) (, ) = (r cos (φ+θ), r sin (φ+θ)) Recall sum of angle trig formulas: sin (φ+θ) = sin φ cos θ + cos φ sin θ cos (φ+θ) = cos φ cos θ -sin φ sin θ = r (cos φ cos θ - sin φ sin θ) = cos θ - sin θ = r (sin φ cos θ + cos φ sin θ) = cos θ + sin θ = sin θ + cos θ Wednesda, November 7, 27 Computer Graphics - Class 5 3
Rotation matri Results from the previous slide give the matri for rotation: ' ' cos sin = θ θ cos sin θ θ sin cos θ θ sin cos θ θ Wednesda, November 7, 27 Computer Graphics - Class 5 3
Two rotations in a row Two rotations in a row have an additive effect on the angles P = R P P = R 2 P = R 2 R P θ θ θ cos 2 sinθ2 sin cosθ cosθ 2 cosθ sinθ2 sinθ sinθ2 cosθ + cosθ 2 sinθ cos( θ2 + θ) sin( θ2 + θ) 2 2 cos sinθ sin( θ2 + θ) cos( θ + ) 2 θ sinθ = cosθ (cosθ sinθ + sinθ sinθ + sinθ2 cosθ) cosθ cos 2 θ Wednesda, November 7, 27 Computer Graphics - Class 5 32 2 2 =
Reflection through = = 45 Rotate point -45 so that reflection line is -ais Reflect through -ais Rotate 45 back Wednesda, November 7, 27 Computer Graphics - Class 5 33
. Rotate -45 Recall matri to perform rotation: Fill in θ = -45 : cos( 45 ) sin( 45 ) cosθ sinθ sinθ cosθ sin( 45 ) cos( 45 ) =.77.77.77.77 Wednesda, November 7, 27 Computer Graphics - Class 5 34
2. Reflect through -ais Recall matri for -ais reflection: Wednesda, November 7, 27 Computer Graphics - Class 5 35
3. Rotate back Recall matri to perform rotation: Fill in θ = 45 : cos(45 ) sin(45 ) cosθ sinθ sin(45 ) cos(45 ) sinθ cosθ =.77.77.77.77 Wednesda, November 7, 27 Computer Graphics - Class 5 36
Compute the total transformation Take each transformation matri in the order the transformations need to be applied and multipl them together:.77.77.77.77 =.77.77.77. 77 Wednesda, November 7, 27 Computer Graphics - Class 5 37
Wednesda, November 7, 27 Computer Graphics - Class 5 38 Result The result of reflecting a point through = is the switching of the and coordinates of the point = = ' '
Another transformation Find the transformation that reflects a point through the line = 2. =2 Wednesda, November 7, 27 Computer Graphics - Class 5 39
The steps Translate b -2 in the direction Reflect through the -ais Translate back Wednesda, November 7, 27 Computer Graphics - Class 5 4
Wednesda, November 7, 27 Computer Graphics - Class 5 4 Translation Translation is a transformation that results in a linear movement of a point + + = + = T T T T ' '
A problem with translation Ever other transformation we have looked at could be epressed using a 2 2 matri and multipling the point b the matri Translation cannot be epressed this wa as we need to add fied amounts to the and coordinates Wednesda, November 7, 27 Computer Graphics - Class 5 42
A solution So that translation behaves like our other transformations we want to epress it using a matri and multipling the point b the matri Will need to add an etra coordinate to our point This coordinate will have the value Wednesda, November 7, 27 Computer Graphics - Class 5 43
Wednesda, November 7, 27 Computer Graphics - Class 5 44 Translation matri The matri for doing a translation is Eample: T T + + = T T T T
Homogeneous coordinates This new representation for our points (with the in the etra coordinate) is known as homogeneous coordinates There are times when the homogeneous coordinate is not ; we ll see this in perspective projections Wednesda, November 7, 27 Computer Graphics - Class 5 45
New versions of transformation matrices Homogeneous coordinates requires a change in the transformation matrices to accommodate the etra coordinate Matrices will now be 3 3 Eample: uniform scaling s Wednesda, November 7, 27 Computer Graphics - Class 5 46 s
Wednesda, November 7, 27 Computer Graphics - Class 5 47 The transformations for our problem Translate b -2 in Reflect through -ais Translate b +2 in = 2 A = B = 2 C
The composite transformation The composite transformation for reflecting a point through the line =2 is found b multipling CBA: 4 Wednesda, November 7, 27 Computer Graphics - Class 5 48
Rotations about an arbitrar point The rotations we have considered so far all occur around the origin Suppose ou had the following figure, which ou would like to rotate about its center: Wednesda, November 7, 27 Computer Graphics - Class 5 49
Rotation analsis If we applied the rotation transformation as we currentl have it, the square would rotate about the origin This is not what we want We want the square to rotate in place Thus, we need to make the center of the square the point of rotation Wednesda, November 7, 27 Computer Graphics - Class 5 5
Fied points We call the point about which we want to rotate a fied point as it will not change its position during the transformation The same concept can be applied to scaling as well Wednesda, November 7, 27 Computer Graphics - Class 5 5
Wednesda, November 7, 27 Computer Graphics - Class 5 52 Steps for arbitrar rotation Translate the fied point to the origin Perform the rotation around the origin Translate the fied point back to its position = c c A = c c C = cos sin sin cos θ θ θ θ B
Reflection through an arbitrar line Consider the following picture: What steps are necessar to perform the indicated reflection? Wednesda, November 7, 27 Computer Graphics - Class 5 53
Arbitrar reflection transformation Sequence for arbitrar reflection: Translate line b its -intercept so that it goes through origin (T) Rotate around origin so line lies on -ais (R) Reflect through -ais (X) Rotate back (R - ) Translate back (T - ) Composite: T - R - XRT Wednesda, November 7, 27 Computer Graphics - Class 5 54