Anda di halaman 1dari 54

Raster Graphics Hardware

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

Video Controller RASTE R

Colour Lookup Table


Frame buffer 0 0 0 0 0 0 0 7 7 7 6 0 7 7 7 0 0 0 0 0 0 colour index 0 1 2 4 ... 7 102 0 51
2

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

Multiplication vector-scalar: Ev = (Evx , Evy)

2v v x
4

Transformation types
Translate according to vector v:
t=p+v

Scale with factor s:


s = sp

y s r v E p x
5

Rotate over angle E


rx = cos(E)px - sin(E)py ry = sin(E)px + cos(E)py

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

Matrices for transformation


x' ! Mx , or x' m11 m12 m13 x y ' ! m21 m22 m23 y , or w' m m m w 31 32 33 x' ! m11 x  m12 y  m13w y ' ! m21 x  m22 y  m23w w' ! m31 x  m32 y  m33w
8

Direct interpretation
y (x,y) b a t x

x' ! M x, or x' ! a b t x, or x ' a x bx t x x y ' ! a y by t y y 1 0 0 1 1

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

x' ' ! T(2,3)R(30 ) x

x' ' ! R(30)T(2,3 ) 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 and viewport


Viewport: Area on screen to be used for drawing. Unit: pixels (screen coordinates) Note: y-axis often points down
(200,200) (0,0) (2,1) (800,600) (600,400)

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

Forward and backward


Viewport
(vx1, vy1) (vx2, vy2)

Drawing: (meters to pixels) Use x = Mx

x:screen coordinates

Drawing

Picking

Window: (wx2, wy2) x: user coordinates


(wx1, wy1)

Picking:(pixels to meters) Use x = M-1x


18

Implementation example
Suppose, basic library supports two functions:
MoveTo(x, y: integer); LineTo(x, y: integer); x and y in pixels.

How to make life easier?


19

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

SetWindow(x1, x2, y1, y2):


Update Window 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

Good and bad


Good: symmetric in x and y Good: matrices, vectors Bad: y = f(x) Good: dot product, determinant Bad: arcsin, arccos
28

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

Dot product properties


vw ! w v ( v  w) u ! v u  w u (P v ) w ! P v w v v ! | v |2 v w ! 0 iff v and w are perpendicu lar

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

Parametric line representation


Given point p and vector v: x(t) = p + vt
y t q p v x

Given two points p and q: x(t) = p + (q-p)t , or = pt + q(1-t)

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));

Define segment: tmin et etmax

34

Implicit line representation


(x-p).n = 0 with n.v = 0 n is normal vector: n = [-vy , vx] Also: ax+by+c=0

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=1 f=0 f =-1 f =-2 f <0

f >0

36

Circle

Parametric : ( x, y ) ! (r cos E , r sin E ) Implicit : x  y r !0


2 2 2

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

Parametric : x(E ) ! c  a cos E  b sin E Implicit : | Mx | ! 1, with M ! a b c

y x

1

39

Some standard puzzles


Conversion of line representation Projection of point on line Line/Line intersection Position points/line Line/Circle intersection

40

Conversion line representations


Given line : p ( s ) ! a  us; Find implicit representa tion : n x  c ! 0. First, determine normal n. n must be B on u, hence we set : n ! (u y , u x ) a must be on the line, hence : c ! n a
41

s a u

Projection point on line


Project point q on line p( s ) ! a  us : u q' ! a  cos U | q  a | |u| Use (q  a) u !| q  a || u | cos U : (q  a) u u, or q' ! a  | u || u | (q  a) u q' ! a  u u u
q u q-a U a w q s

cos U | q  a |: length w u : unit vecto r along u |u| 42

Intersection of line segments


Find intersecti on of line segments : p( s ) ! a  us, 0 e s e 1 and q(t ) ! b  vt , 0 e t e 1. At intersecti on : p ( s ) ! q (t ) Solve for s and t (next sheet); Check if 0 e s e 1 and 0 e t e 1; If so, intersecti on is p( s ).
43

s a u v

t b

Solving for s and t


p( s ) ! q(t ), or a  us ! b  vt , or s u v ! b  a, or t s ! u v 1 (b  a), or t s 1 ! t u v u v x y y x vy u y  v x bx  a x b  a ux y y
44

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

(a  ut ) (a  ut ) ! r 2 , or u ut 2  a ut  a a  r 2 ! 0. Solve quadratic equation for t : 0, 1, or 2 solutions.

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

Convex and concave


Convex:
each line between two arbitrary points inside the polygon does not cross its boundary

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 Convex Concave

pi-1
50

Polygon area and orientation


a ! Det (p i  c, p i 1  c) / 2, c is arbitrary point
i N

area ! | a | a " 0 : counterclo ckwise orientatio n a 0 : clockwise orientatio n

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

Point/polygon test (cntd.)


Beware of special cases:
Point at boundary v parallel to edge c + vt through vertex

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

Anda mungkin juga menyukai