Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 76

2M050: Computer Graphics

Jack van Wijk


vanwijk@win.tue.nl
HG6.71
1
Aims

• Introduce basic graphics concepts and


terminology

• Base for development of 2D interactive


computer graphics programmes (OGO 2.3)

2
Literature

Computer Graphics - Principles and Practice


Foley - van Dam - Feiner - Hughes
2nd edition in C - Addison and Wesley

Computer Graphics - C Version


Donald Hearn - M. Pauline Baker
2nd edition - international edition
Prentice Hall

3
Overview

Introduction
Geometry
Interaction
Raster graphics

4
Introduction

What is Computer Graphics?


Applications
Computer Graphics in Eindhoven
Raster/vector graphics
Hardware

5
Computer Graphics

Computer Graphics is ubiquitous:


• Visual system is most important sense:
– High bandwidth
– Natural communication
• Fast developments in
– Hardware
– Software

6
Computer Graphics

Image Analysis
(pattern recognition)

Mathematical
Image
Model

Image Synthesis
(Rendering)

Modeling Image processing7


Supporting Disciplines

• Computer science (algorithms, data


structures, software engineering, …)
• Mathematics (geometry, numerical, …)
• Physics (Optics, mechanics, …)
• Psychology (Colour, perception)
• Art and design

8
Applications

• Computer Aided Design (CAD)


• Computer Aided Geometric Design (CAGD)
• Entertainment (animation, games, …)
• Geographic Information Systems (GIS)
• Visualization (Scientific Vis., Inform. Vis.)
• Medical Visualization
• …
9
Computer Graphics Eindhoven

Current: Jack van Wijk


Kees Huizing
•Information visualisation
Arjan Kok
•Interactive 3D design Robert van Liere
Wim Nuij
•Virtual reality
Alex Telea
Huub van de Wetering
Past: Rasterization, Animation

10
Interactive Computer Graphics

User input

Application

Screen image

11
Graphics pipeline

User Edit

Model

Map

Geometry, colour

Screen Display

12
Representations in graphics

Vector Graphics
• Image is represented by continuous
geometric objects: lines, curves, etc.

Raster Graphics
• Image is represented as an rectangular grid
of coloured squares
13
Vector graphics

• Graphics objects: geometry + colour


• Complexity ~ O(number of objects)
• Geometric transformation possible without
loss of information (zoom, rotate, …)
• Diagrams, schemes, ...
• Examples: PowerPoint, CorelDraw, ...

14
Raster graphics

• Generic
• Image processing techniques
• Geometric Transformation: loss of information
• Complexity ~ O(number of pixels)
• Jagged edges, anti-aliasing
• Realistic images, textures, ...
• Examples: Paint, PhotoShop, ...
15
Conversion

Vector graphics

Rasterization, Pattern recognition


Scan conversion

Raster graphics
16
Hardware

• Vector graphics
• Raster graphics
• Colour lookup table
• 3D rendering hardware

17
Vector Graphics Hardware
Display list
continuous & smooth move 10 20
lines
no filled objects line 20 40
random scan
...
refresh speed depends
on complexity of the
char O
scene
VECTOR
char R

Display Controller 18
Raster Graphics Hardware

Frame buffer

0 0 0 0 0 0
0 7 7 7 6
0 7 7 7 Video Controller
0 0 0 RASTER
0 0
0
jaggies (stair casing) refresh speed independent of scan conversion
scene complexity
filled objects resolution
pixel
(anti)aliasing bit planes 19
Colour Lookup Table

colour R G B
Frame buffer index
0 0 0 0
0 0 0 0 0 0
0 7 7 7 6 1 CLUT:
102 255 53
0 7 7 7 pixel = code
2 255 255 204
0 0 0
0 0 4 255 102 153 True colour:
0 ...
pixel = R,G,B
7 102 0 51
20
3D rendering hardware

Geometric representation: Triangles


Viewing: Transformation
Hidden surface removal: z-buffer
Lighting and illumination: Gouraud shading
Realism: texture mapping
Special effects: transparency, antialiasing
21
2D geometric modelling

• Coordinates
• Transformations
• Parametric and implicit representations
• Algorithms

22
Coordinates

• Point: position on plane y


p = (px , py) p

x = (x, y) v
x
x = (x1 , x2)
x = x1 e1 + x2 e2, e1 = (1, 0), e2 = (0, 1)
• Vector: direction and magnitude
v = (vx , vy), etc.
23
Vector arithmetic

• Addition of two vectors:


y
v + w = (vx + wx , vy + wy) w v+w

v x

• Multiplication vector-scalar: y
2v
v = (vx , vy)
v
x
24
Coordinate systems

world

train

wheel
image
25
Why transformations?

• Model of objects
world coordinates: km, mm, etc.
hierarchical models:
human = torso + arm + arm + head + leg + leg
arm = upperarm + lowerarm + hand …
• Viewing
zoom in, move drawing, etc.

26
Transformation types

• Translate according to vector v:


t=p+v
• Scale with factor s:
y
s = sp s
• Rotate over angle  r t
rx = cos()px - sin()py v
 p
ry = sin()px + cos()py x

27
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

28
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

29
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  m13 w
y '  m21 x  m22 y  m23 w
w'  m31 x  m32 y  m33 w
30
Direct interpretation

y’
x'  M x, or (x, y )
y
x'   a b t  x, or (x’,y’)
x
 x'   a x bx t x   x  b
     a
 y '    a y by t y   y 
1   0 0 1  1  t
     x’

31
Translation matrix

Translation :
x'  T (t x , t y )x , with
 1 0 tx 
 
T (t x , t y )   0 1 t y 
0 0 1 
 

32
Scaling matrix

Scaling :
x'  S( s x , s y )x , with
 sx 0 0
 
S( s x , s y )   0 sy 0
0 0 1 

33
Rotation matrix

Rotation :
x'  R( ) x , with
 cos   sin  0 
 
R( )   sin  cos  0 
 0 0 1 
 

34
Sequences of transformations

x’’’
y’’’ x'  R ( / 2)x
y x' '  S(1/2)x'
x’
x' ' '  T(5,4) x' '
y’ or
x’’
x' ' '  Mx, with
y’’
M  T(5,4)S(1/2)R( / 2)
x
Sequences of transformations can be described with a single
transformation matrix, which is the result of concatenation of
all transformations. 35
Order of transformations

y’
y’’ y’’
x’’ x’’
’ y y
y
x’
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. 36
Order of transformations

• Pre-multiplication:
x’ = M n M n-1…M 2 M 1 x
Transformation M n in global coordinates
• Post-multiplication:
x’ = M 1 M 2…M n-1 M n x
Transformation M n in local coordinates, i.e., the
coordinate system that results from application of
M 1 M 2…M n-1
37
Window and viewport

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

Window:
Virtual area to be used by application
Unit: km, mm,… (world coordinates)
(-2,-1) 38
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
39
Forward and backward

(vx2, vy2)
Viewport
Drawing: (meters to pixels)
x’:screen coordinates
(vx1, vy1) Use x’ = Mx

Drawing Picking
Picking:(pixels to meters)
Window: (wx2, wy2)
Use x = M-1x’
x: user coordinates
(wx1, wy1)
40
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?

41
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

42
Procedures

• Define coordinate system:


SetViewPort(x1, x2, y1, y2):
Update Viewport and Mwv
SetWindow(x1, x2, y1, y2):
Update Window and Mwv

43
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

44
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);
45
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’)

46
Application

DrawUnitSquare: Main program:


MyMoveTo(0, 0); Initialize;
MyLineTo(1, 0); Translate(-0.5, -0.5);
MyLineTo(1, 1); for i := 1 to 10 do
MyLineTo(0, 1); begin
MyLineTo(0, 0); Rotate(pi/20);
Scale(0.9, 0.9);
Initialize: DrawUnitSquare;
SetViewPort(0, 100, 0, 100); end;
SetWindow(0, 1, 0, 1);

47
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.

48
Geometry

• Dot product, determinant


• Representations
• Line
• Ellipse
• Polygon

49
Good and bad

• Good: symmetric in x and y


• Good: matrices, vectors
• Bad: y = f(x)

• Good: dot product, determinant


• Bad: arcsin, arccos

50
Dot product

Notation : v  w (sometimes (v, w))


Definition :
v  w  v x wx  v y wy
w
Also :
v  w  | v | | w | cos  (0      
v
with   angle between v and w, |w| cos 
and | v | is the length of vector v

51
Dot product properties

vw  w v
( v  w)  u  v  u  w  u
( v )  w   v  w
v  v  | v |2
v  w  0 iff v and w are perpendicular

52
Determinant

Det ( v, w )  v x wy  v y wx
 | v | | w | sin  w

 is angle from v to w v
0     : Det ( v, w )  0  v
    2 : Det ( v, w )  0
w
Det(v, w): signed area of parallellogram
Det(v, w) = 0 iff v and w are parallel
53
Curve representations

• Parametric: x(t) = (x(t), y(t))

• Implicit: f(x) = 0

54
Parametric line representation

Given point p and vector v:


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

55
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 t tmax

56
Implicit line representation

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

57
Implicit representation

f (x)  0 : curve
f=1
f (x)  C : contours f >0
f=0
f  0 divides plane in
f =-1
two areas : f  0 and f  0
f =-2
| f (x) | : measure of distance
f <0
of x to curve

58
Circle

Parametric :
( x, y )  (r cos  , r sin  ) y

Implicit : r x

x  y r 0
2 2 2

59
Ellipse

Parametric :
( x, y )  (a cos  , b sin  )
y

Implicit : b a x
2 2
x  y
     1  0
a b

60
Generic ellipse

Parametric : a
y b
x( )  c  a cos   b sin  c
x

Implicit :
| Mx |  1, with M   a b c 
1

61
Some standard puzzles

• Conversion of line representation


• Projection of point on line
• Line/Line intersection
• Position points/line
• Line/Circle intersection

62
Conversion line representations

Given line :
p ( s )  a  us;
Find implicit representation :
n  x  c  0.
First, determine normal n. s

n must be  on u, hence we set : a u


n  (u y , u x )
a must be on the line, hence :
c  n  a
63
Projection point on line

Project point q on line p( s )  a  us : q


u u
q'  a  cos | q  a |
|u| q’
q-a s
Use (q  a)  u | q  a | | u | cos :

(q  a)  u w
q'  a  u, or a
| u || u |
(q  a)  u cos | q  a |: length w
q'  a  u
u u u
: unit vector along u
|u|
64
Intersection of line segments

Find intersection of line segments :


p( s )  a  us, 0  s  1 and
q(t )  b  vt , 0  t  1.
At intersection : s
p( s )  q(t ) t
a u v
Solve for s and t (next sheet); b
Check if 0  s  1 and 0  t  1;
If so, intersection is p( s ).

65
Solving for s and t

p( s )  q(t ), or
a  us  b  vt , or
s
 u v     b  a, or
t  s
s t
    u v  (b  a), or
1
a u v
t  b

s 1  vy  v x   bx  a x 
     
u u x   by  a y 
 t  u x v y  u y vx  y
66
Position points/line

Check if points a and b are on the


u
same side of line p( s )  c  us
a s

Use Det (u, v ) | u | | v | sin  :


Points are on the same side if c
b
Det (u, a  c) and Det (u, b  c) have
the same sign.

67
Line/circle intersection
Find intersections of :
line : p(t )  a  ut , 0  t  1 and
circle : x  x  r 2 .
y
At intersection :
u
p(t )  p(t )  r , or
2
r x
(a  ut )  (a  ut )  r 2 , or
t
u  u t 2  a  u t  a  a  r 2  0.
a
Solve quadratic equation for t :
0, 1, or 2 solutions.
68
Polygons

• Sequence of points pi, i = 1,…, N, connected by straight lines


• Index arithmetic: modulo N
p0 = pN , pN+1 = p1 , etc.

pN pi
p2
p1
69
Regular N-gon

p i  (r cos  i , r sin  i )
 i  2 (i  1 / 2) / N   / 2

triangle square pentagon hexagon octagon

70
Convex and concave

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

• Concave:
– not convex
71
Convexity test

Assume polygon is oriented counterclockwise.


Polygon is concave, if
Det(p i  p i-1 , p i 1  p i )  0 for all i
pi+1 pi+1

pi pi

pi-1 pi-1
Convex Concave
72
Polygon area and orientation

N
a   Det (p i  c, p i 1  c) / 2, c is arbitrary point
i

area  | a |
a  0 : counterclockwise orientation
a  0 : clockwise orientation pi+1
c

pi
73
Point/polygon test

Given a polygon. Test if a point c is inside or outside.

2
Solution :
1
Define a line L  c  vt , t  0. 3
v can be chosen arbitrarily, 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.
74
Point/polygon test (cntd.)

• Beware of special cases:


– Point at boundary
– v parallel to edge
– c + vt through vertex

75
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.

76

You might also like