Frame buffer 0 0 0 0 0 0 0 7 7 7 6 0 7 7 7 0 0 0 0 0 0
jaggies (stair casing) filled objects (anti)aliasing refresh speed independent of scene complexity pixel scan conversion resolution bit planes 1
R G B 0 0 0 102 255 53 255 255 204 255 102 153 True colour: pixel = R,G,B CLUT: pixel = code
Coordinates
Point: position on plane y p p = (px , py) v x = (x, y) x x = (x1 , x2) x = x1 e1 + x2 e2, e1 = (1, 0), e2 = (0, 1) Vector: direction and magnitude v = (vx , vy), etc.
3
Vector arithmetic
Addition of two vectors: v + w = (vx + wx , vy + wy)
y w v x v+w
2v v x
4
Transformation types
Translate according to vector v:
t=p+v
y s r v E p x
5
Homogeneous coordinates
Unified representation of rotation, scaling, translation Unified representation of points and vectors Compact representation for sequences of transformations Here: convenient notation, much more to it
Homogeneous coordinates
Extra coordinate added: p = (px , py , pw) or x = (x, y, w) Cartesian coordinates: divide by w x = (x/w, y/w) Here: for a point w = 1, for a vector w = 0
7
Direct interpretation
y (x,y) b a t x
Translation matrix
Translatio n : x' ! T(t x , t y )x , with 1 0 tx T(t x , t y ) ! 0 1 t y 0 0 1
10
Scaling matrix
Scaling : x' ! S( s x , s y )x , with sx S( s x , s y ) ! 0 0 0 sy 0 0 0 1
11
Rotation matrix
Rotation : x' ! R(E ) x , with cos E sin E 0 R(E ) ! sin E cos E 0 0 0 1
12
Sequences of transformations
y y y x y x x x x' ! R (T / 2)x x' ' ! S(1/2) x' x' ' ' ! T(5,4) x' ' or x' ' ' ! Mx, with M ! T(5,4)S(1/ 2)R( T / 2)
Sequences of transformations can be described with a single transformation matrix, which is the result of concatenation of all transformations. 13
Order of transformations
y y y x x x
Matrix multiplication is not commutative. Different orders of multiplication give different results. 14
Order of transformations
Pre-multiplication:
x = M n M n-1M 2 M 1 x Transformation M n in global coordinates
Post-multiplication:
x = M 1 M 2M n-1 M n x Transformation M n in local coordinates, i.e., the coordinate system that results from application of M 1 M 2M n-1
15
Window: Virtual area to be used by application Unit: km, mm, (world coordinates)
(-2,-1) 16
Window/viewport transform
Determine a matrix M, such that the window (wx1, wx2, wy1, wy2) is mapped on the viewport (vx1, vx2, vy1, vy2): A = T(-wx1, -wy1) B = S(1/(wx2-wx1), 1/(wy2-wy1)) A C = S(vx2-vx1 ,vy2-vy1)B M = T(vx1, vy1) C
17
x:screen coordinates
Drawing
Picking
Implementation example
Suppose, basic library supports two functions:
MoveTo(x, y: integer); LineTo(x, y: integer); x and y in pixels.
State variables
Define state variables:
Viewport: array[1..2, 1..2] of integer; Window: array:[1..2, 1..2] of real; Mwv, Mobject: array[1..3, 1..3] of real; Mwv: transformation from world to view Mobject: extra object transformation
20
Procedures
Define coordinate system:
SetViewPort(x1, x2, y1, y2):
Update Viewport and Mwv
21
Procedures (continued)
Define object transformation:
ResetTrans:
Mobject := IdentityMatrix
Translate(tx, ty):
Mobject := T(tx,ty)* Mobject
Rotate(alpha):
Mobject := R(tx,ty)* Mobject
Scale(sx, sy):
Mobject := S(sx, sy)* Mobject
22
Procedures (continued)
Handling hierarchical models:
PushMatrix();
Push an object transformation on a stack;
PopMatrix()
Pop an object transformation from the stack.
Or:
GetMatrix(M); SetMatrix(M);
23
Procedures (continued)
Drawing procedures:
MyMoveTo(x, y):
(x, y) = Mwv*Mobject*(x,y); MoveTo(x, y)
MyLineTo(x,y):
(x, y) = Mwv*Mobject*(x,y); LineTo(x, y)
24
Application
DrawUnitSquare: MyMoveTo(0, 0); MyLineTo(1, 0); MyLineTo(1, 1); MyLineTo(0, 1); MyLineTo(0, 0); Initialize: SetViewPort(0, 100, 0, 100); SetWindow(0, 1, 0, 1); Main program: Initialize; Translate(-0.5, -0.5); for i := 1 to 10 do begin Rotate(pi/20); Scale(0.9, 0.9); DrawUnitSquare; end;
25
Puzzles
Modify the window/viewport transform for a display y-axis pointing downwards. How to maintain aspect-ratio world->view? Which state variables? Define a transformation that transforms a unit square into a wybertje, centred around the origin with width w and height h.
26
Geometry
Dot product, determinant Representations Line Ellipse Polygon
27
Dot product
Notation : v w (sometimes (v, w)) Definition : v w ! v x wx v y wy Also : v w ! | v | | w | cosU (0 e U e T
with U angle between v and w, and | v | is the length of vector v
w U |w| cos U v
29
30
Determinant
Det ( v, w ) ! v x wy v y wx ! | v | | w | sin U U is angle from v to w 0 U T U T : Det ( v, w ) " 0 2T : Det ( v, w ) 0
w U w U v v
Det(v, w): signed area of parallellogram Det(v, w) = 0 iff v and w are parallel
31
Curve representations
Parametric: x(t) = (x(t), y(t)) Implicit: f(x) = 0
32
33
Parametric representation
x(t) = (x(t), y(t)) Trace out curve:
MoveTo(x(0)); for i := 1 to N do LineTo(x(i*(t));
34
n v
p x
35
Implicit representation
f (x) ! 0 : curve f (x) ! C : contours f ! 0 divides plane in two areas : f " 0 and f 0 | f (x) | : measure of distance of x to curve
f >0
36
Circle
y r x
37
Ellipse
Parametric : ( x, y ) ! (a cos E , b sin E )
y
Implicit : x y 1 ! 0 a b
2 2
38
Generic ellipse
y x
1
39
40
s a u
s a u v
t b
s a u v
t b
Position points/line
Check if points a and b are on the same side of line p( s ) ! c us
a
u s
Use Det (u, v) !| u || v | sin U : Points are on the same side if Det (u, a c) and Det (u, b c) have the same sign.
45
Line/circle intersection
Find intersecti ons of : line : p(t ) ! a ut , 0 e t e 1 and circle : x x ! r 2 . At intersecti on : p(t ) p(t ) ! r , or
2
y u r t a x
46
Polygons
Sequence of points pi, i = 1,, N, connected by straight lines Index arithmetic: modulo N
p0 = pN , pN+1 = p1 , etc.
pi p2 p1
47
pN
Regular N-gon
p i ! ( r cos E i , r sin E i ) E i ! 2T (i 1 / 2) / N T / 2
triangle
square
pentagon
hexagon
octagon
48
Concave:
not convex
49
Convexity test
Assume polygon is oriented counterclo ckwise. Polygon is concave, if Det( p i p i-1 , p i 1 p i ) " 0 for all i
pi+1 pi pi pi+1
pi-1
50
pi+1
pi
51
Point/polygon test
Given a polygon. Test if a point c is inside or outside. Solution : Define a line L ! c vt , t u 0. v can be chosen arbitraril y, f.i. (1, 0). Let n be the number of crossings of L with the polygon. If n is odd : point is inside, else it is outside.
52
2 1 3
53
Puzzles
Define a procedure to clip a line segment against a rectangle. Define a procedure to calculate the intersection of two polygons. Define a procedure to draw a star. Same, with the constraint that the edges pi-1 pi and pi+2 pi+3 are parallel.
54