CG Lecture2&3
CG Lecture2&3
1
Contents
• Definition & Motivation
• Basic 2D Geometric Transformation
– Translation
– Rotation
– Scaling
• Matrix Representation
• Homogeneous Coordinates
• Matrix Composition
• Composite Transformations
– Pivot-Point Rotation
– General Fixed-Point Scaling
– Reflection and Shearing
– Transformations Between Coordinate Systems
2
Geometric Transformation
• Definition
– Translation, Rotation, Scaling
3
Example: 2D Geometric Transformation
Modeling
Coordinates
World Coordinates
4
Example: 2D Scaling
Modeling
Coordinates
Scale(0.3, 0.3)
World Coordinates
5
Example: 2D Rotation
Modeling
Coordinates
Scale(0.3, 0.3)
Rotate(-90)
World Coordinates
6
Example: 2D Translation
Modeling
Coordinates
Scale(0.3, 0.3)
Rotate(-90)
Translate(5, 3)
World Coordinates
7
Example: 2D Geometric Transformation
Modeling
Coordinates
Again?
World Coordinates
8
Example: 2D Geometric Transformation
Modeling
Coordinates
Scale
Translate
Scale
Rotate
Translate
World Coordinates
9
Basic 2D Transformations
• Translation
–
x x tx
– y y ty
• Scale
–
x x sx
– y y sy
• Rotation
x x cosθ - y sinθ
–
y y sinθ y cosθ
–
• Shear
x x hx y
– y y hy x
–
10
Basic 2D Transformations
• Translation
–
x x tx
– y y ty
• Scale
–
x x sx
– y y sy
• Rotation
x x cosθ - y sinθ Transformations
– can be combined
y y sinθ y cosθ (with simple algebra)
–
• Shear
x x hx y
– y y hy x
–
11
Basic 2D Transformations
• Translation
–
x x tx
– y y ty
• Scale
–
x x sx
– y y sy
• Rotation
x x cosθ - y sinθ
–
y y sinθ y cosθ
–
• Shear x x sx
x x hx y
– y y hy x y y sy
–
12
Basic 2D Transformations
• Translation
–
x x tx
– y y ty
• Scale
–
x x sx
– y y sy
• Rotation
x x cosθ - y sinθ
–
y y sinθ y cosθ
–
• Shear x ((x sx) cos (y sy) sin )
x x hx y
– y y hy x y ((x sx) sin (y sy) cos )
–
13
Basic 2D Transformations
• Translation
–
x x tx
– y y ty
• Scale
–
x x sx
– y y sy
• Rotation
x x cosθ - y sinθ
–
y y sinθ y cosθ
–
• Shear x ((x sx) cos (y sy) sin ) tx
x x hx y
– y y hy x y ((x sx) sin (y sy) cos ) ty
–
14
Basic 2D Transformations
• Translation
–
x x tx
– y y ty
• Scale
–
x x sx
– y y sy
• Rotation
x x cosθ - y sinθ
–
y y sinθ y cosθ
–
• Shear x ((x sx) cos (y sy) sin ) tx
x x hx y
– y y hy x y ((x sx) sin (y sy) cos ) ty
–
15
Matrix Representation
• Represent a 2D Transformation by a Matrix
a b
c d
17
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Identity
x x x 1 0 x
y 0 1 y
y y
2D Scaling
x sx x x sx 0 x
y sy y y 0 sy y
18
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Rotation
x cos x sin y x cos sin x
y sin cos y
y sin x cos y
2D Shearing
x x shx y x 1 shx x
y shy x y y shy 1 y
19
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Mirror over Y axis
x x x 1 0 x
y y y 0 1 y
2D Mirror over (0,0)
x x x 1 0 x
y y y 0 1 y
20
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Translation
x x tx
NO!!
y y ty
21
2D Translation
• 2D translation can be represented by a 3×3
matrix
– Point represented with homogeneous coordinates
x 1 0 tx x
x x tx y 0 1 ty y
y y ty
1 0 0 1 1
22
Basic 2D Transformations
• Basic 2D transformations as 3x3 Matrices
x 1 0 tx x x sx 0 0 x
y 0 1 ty y y 0 sy 0 y
1 0 0 1 1 1 0 0 1 1
Translate Scale
Rotate Shear
23
Homogeneous Coordinates
• Add a 3rd coordinate to every 2D point
– (x, y, w) represents a point at location (x/w, y/w)
– (x, y, 0) represents a point at infinity
– (0, 0, 0) Is not allowed
y
2 (2, 1, 1) or (4, 2, 2) or (6, 3, 3)
1
x
1 2
24
Matrix Composition
• Transformations can be combined by matrix
multiplication
x 1 0 tx cos θ - sinθ 0 sx 0 0 x
y 0 1 ty sinθ cosθ 0 0 sy 0 y
w 0 0 1 0 0 1 0 0 1 w
p T(tx, ty) R( ) S(sx, sy) p
p (T (R (S p))) p (T R S) p
25
Matrix Composition
• Rotate by around arbitrary point (a,b)
– M T(a, b) R(θ ) T(-a,-b)
(a,b)
(a,b)
26
Pivot-Point Rotation
27
General Fixed-Point Scaling
T x f , y f S s x , s y T x f , y f S x f , y f , s x , s y
1 0 xf s x 0 0 1 0 xf s x 0 x f (1 s x )
0 1 yf 0 sy 0 0 1 yf 0 sy y f (1 s y )
0 0 1 0 0 1 0 0 1 0 0 1
28
Reflection
• Reflection with respect to the axis
• x-axis • y-axis • Origin
1 0 0 1 0 0 1 0 0
0 1 0 0 1 0 0 1 0
0 0 1 0 0 1 0 0 1
y 1 y y
1 1’ 1’
2 3 2 3 3’ 2 3’ 2
x x 3 x
2’ 3’ 1
1’ 2
29
Reflection
Reflection with respect to a Line
y
0 1 0
1 0 0
x
0 0 1
y=x
Clockwise rotation of 45 Reflection about the x axis
Counterclockwise rotation of 45
y y 1
y
2 3
x x
2’ 3’ x
1’
30
OpenGL Geometric-Transformation
Functions
glFlush();
}
Example3- Rotate the rectangle in
example2 by 200 CCW about (0,0)
Example4- Rotate the rectangle in example2
by 200 CCW about (50,50)
void display()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(50.0,50.0,0);
glRotatef(20, 0.0,0.0, 1.0);
glTranslatef(-50.0,-50.0,0);
Rect2();
glFlush();
}
Example4- Rotate the rectangle in example2
by 200 CCW about (50,50)
3 DIMENSIONAL
TRANSFORMATIONS
3D Transformations
• Analogous to 2D we have 4 dimensions in homogenous
coordinates.
• Basic transformations:
– Translation Y
– Rotation
– Scaling X
Z
Translation
• move the object to a relative position.
y
x 1 0 0 tx x P
y 0 1 0 t y y
x
P
z 0 0 1 tz z z
1 0 0 0 1 1 y
P T P
x
z
Rotation
• Rotation around the coordinate axes
y y y
x x x
z z z
cos 0 sin 0
• Around y 0 1 0 0
R y ( ) P R y ( ) P
sin 0 cos 0
0 0 0 1
cos sin 0 0
sin cos 0 0
• Around z R z ( ) P R z ( ) P
0 0 1 0
0 0 0 1
Rotation Around a Parallel Axis
• Rotating the object around a line parallel to one of the axes:
Translate to axis, rotate, translate back.
P T(0, y p , z p ) R x ( ) T(0, y p , z p ) P
y y y y
x
x
x x
z z z
z
P S P
x
z
Scaling with respect to a Fixed Point
• Translate to origin, scale, translate back
P T( x f , y f , z f ) S T( x f , y f , z f ) P
y y x
y y
x
x x
z z z
z
Window
• glMatrixMode (GL_PROJECTION)
• glLoadIdentity ();
• gluOrtho2D (xwmin, xwmax, ywmin, ywmax);
or
• glOrtho (xwmin, xwmax, ywmin, ywmax, zwmin,
zwmax);
Setting up a Viewport
• glViewport (xvmin, yvmin, vpWidth, vpHeight);
• All the parameters are given in integer screen
coordinates relative to the lower-left corner of the
display window.
• If we do not invoke this function, by default, a
viewport with the same size and position of the
display window is used (i.e., all of the GLUT window
is used for OpenGL display)
Creating a GLUT Display Window
• glutInitWindowPosition (xTopLeft, yTopLeft);
– the integer parameters are relative to the top-left corner
of the screen
• glutInitWindowSize (Width, Height);
• glutCreateWindow (“Title of Display Window”);
• glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB)
– Specification of the buffer that will be used
• glClearColor (red, green, blue, alpha)
– Specify the background color
OpenGL 2D Viewing Example
• 2 Viewports
• One triangle is displayed in
two colors and
orientations in 2 viewports
glutInitWindowSize (600, 300);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glViewport(0, 0, 300, 300);
drawCenteredTriangle();
Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10
wxmin wxmax
Line Clipping
• Examine the end-points of each line to see if
they are in the window or not
Situation Solution Example
Both end-points
Must clip
outside the window
Brute Force Line Clipping
• Brute force line clipping can be performed as
follows:
– Don’t clip lines with both
end-points within the
window
– For lines with one end-
point inside the window
and one end-point
outside, calculate the
intersection point (using the equation of the line)
and clip from this point out
Brute Force Line Clipping (cont…)
– For lines with both end-
points outside the window
test the line for
intersection with all of the
window boundaries, and
clip appropriately
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Lines In The
Window
Lines completely contained within the window
boundaries have region code [0000] for both
end-points so are not clipped
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Lines Outside The
Window
Any lines with a common set bit in the region
codes of both end-points can be clipped
– The AND operation can efficiently check this
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Other Lines
• Lines that cannot be identified as completely
inside or outside the window may or may not
cross the window interior
• These lines are processed as follows:
– Compare an end-point outside the window to a
boundary (choose any order in which to consider
boundaries e.g. left, right, bottom, top) and
determine how much can be discarded
– If the remainder of the line is entirely inside or
outside the window, retain it or clip it respectively
Cohen-Sutherland: Other Lines (cont…)
– Otherwise, compare the remainder of the line against
the other window boundaries
– Continue until the line is either discarded or a segment
inside the window is found
• We can use the region codes to determine which
window boundaries should be considered for
intersection
– To check if a line crosses a particular boundary we
compare the appropriate bits in the region codes of its
end-points
– If one of these is a 1 and the other is a 0 then the line
crosses the boundary
Cohen-Sutherland Examples
• Consider the line P9 to P10 below
Window
– Start at P10 wymax
– From the region codes
of the two end-points we
know the line doesn’t P [0000]
9
wymin
cross the left or right P ’ [0000]
10
boundary P [0100]
10
left boundary so
calculate the wxmin wxmax
intersection point to
generate P7’
Cohen-Sutherland Examples (cont…)
• Consider the line P7’ to P8
– Start at P8 Window
wymax
– Calculate the
intersection with the P7’ [0000]
P7 [0001] P8 [0010]
right boundary to P8’ [0000]
wymin
generate P8’
– P7’ to P8’ is inside
wxmin wxmax
the window so is
retained
Calculating Line Intersections
• Intersection points with the window boundaries are
calculated using the line-equation parameters
– Consider a line with the end-points (x1, y1) and (x2, y2)
– The y-coordinate of an intersection with a vertical window
boundary can be calculated using:
y = y1 + m (xboundary - x1)
Original Area Clip Left Clip Right Clip Top Clip Bottom
Sutherland-Hodgman Area Clipping
Algorithm (cont…)
• To clip an area against an individual boundary:
– Consider each vertex in turn against the boundary
– Vertices inside the boundary are saved for clipping
against the next boundary
– Vertices outside the boundary are clipped
– If we proceed from a point inside the boundary to
one outside, the intersection of the line with the
boundary is saved
– If we cross from the outside to the inside,
intersection point and the vertex are saved
Sutherland-Hodgman Example
• Each example S
shows the point
being processed (P) P
I
S
boundary in
question
S
No Points Saved Save Points I & P