Komputer Grafik

Post on 17-Jan-2016

10 views 1 download

description

uhukh

Transcript of Komputer Grafik

Reading list

• Introduction to Computer Graphics– Foley, Van Dam and Feiner, Hughes, Phillips– Addison-Wesley

• Computer Graphics, Principles and Practice– Foley, Van Dam,Feiner, Hughes– Addison Wesley.

• Old Leaving Cert maths books !?!

What is computer graphics?

PicturesVideo Image processing / analysisGUIs3D ModellingVirtual Reality

Pictures

• Varoius formats: BMP, GIF, JPEG…

Video

• .mov, .avi, .wmv, DivX, Quicktime…

Image processing / analysis

• Filters, image enhancement, medical imaging, astronomy…

• Edge detection, face recognition…

Graphical User Interface (GUI)

• W.I.M.P - Windows, icons,menu, pointers

3D Modelling

• Cartesian Coordinate System• Transformations • Clipping• Hidden line removal• Lighting

3D Modelling

• Cartesian Coordinate System• Transformations • Clipping• Hidden line removal• Lighting

3D Modelling

• Cartesian Coordinate System• Transformations • Clipping• Hidden line removal• Lighting

VR

• Fully immersive, interactive 3D displays

Course content

• The emphasis of the course is on the algorithms and techniques used to develop realistic views of three dimensional objects modelled in the computer.

• Developing views will involve problems of scaling, rotation, translation, curves, and hidden surface removal.

• Aim to put the theory to practice through the use of the graphics language ‘processing’

Course content

What will be covered:• Raster displays • Cartesian Coordinate System• Introduction to ‘Processing’

language• 2D Transformation, Scaling &

Rotation• 2D Transformation using

matrices• 3D Transformation, Scaling &

Rotation using martices• Simple animation/interaction• Surface Modelling• Hidden-Surface Problem.

What will NOT be covered:1. Paint and Imaging packages

(Adobe Photoshop) 2. CAD packages (AutoCAD) 3. Rendering packages (Lightscape) 4. Modelling packages (3D Studio

MAX, Blender) 5. Animation packages (Digimation)

Computer graphics = Maths

• Can’t stress this enough!

• Primarily… matrices, trigonometry and calculus.

−=

=

=

1000cos*sin*0sin*cos*

1000cossin0sincos

1000000

)(),(1

θθθθ

θθθθ

θ

yy

xx

y

x

yx

ssss

ss

RssSM

−=

−=

=

1000cos*sin*0sin*cos*

1000000

1000cossin0sincos

),()(2

θθθθ

θθθθ

θ

yx

yx

y

x

yx

ssss

ss

ssSRM

Practical work

http://www.processing.org/

“ Processing is an open source programming language and environment for people who want to program images, animation, and sound. It is used by students, artists, designers, architects, researchers, and hobbyists for learning, prototyping, and production. It is created to teach fundamentals of computer programming within a visual context and to serve as a software sketchbook and professional production tool. ”

2D Transformations with Matrices

Matrices

=

3,32,31,3

3,22,21,2

3,12,11,1

aaaaaaaaa

A

• A matrix is a rectangular array of numbers.

• A general matrix will be represented by an upper-case italicised letter.

• The element on the ith row and jth column is denoted by ai,j. Note that we start indexing at 1, whereas C indexes arrays from 0.

• Given two matrices A and B if we want to add B to A(that is form A+B) then if A is (n×m), B must be (n×m), Otherwise, A+B is not defined.

• The addition produces a result, C = A+B, with elements:

Matrices – Addition

jijiji BAC ,,, +=

=

++++

=

+

121086

84736251

8765

4321

• Given two matrices A and B if we want to multiply B by A(that is form AB) then if A is (n×m), B must be (m×p), i.e., the number of columns in A must be equal to the number of rows in B. Otherwise, AB is not defined.

• The multiplication produces a result, C = AB, with elements:

(Basically we multiply the first row of A with the first column of B and put this in the c1,1 element of C. And so on…).

Matrices – Multiplication

∑=

=m

kkjikji baC

1,

=

×

966695557644

623386

329854762

Matrices – Multiplication (Examples)

2×6+ 6×3+ 7×2=44

×

623386

5462

Undefined!2x2 x 3x2 2!=3

2x2 x 2x4 x 4x4 is allowed. Result is 2x4 matrix

• Unlike scalar multiplication, AB ≠ BA

• Matrix multiplication distributes over addition:A(B+C) = AB + AC

• Identity matrix for multiplication is defined as I.

• The transpose of a matrix, A, is either denoted AT or A’ is obtained by swapping the rows and columns of A:

Matrices -- Basics

=⇒

=

3,23,1

2,22,1

1,21,1

3,22,21,2

3,12,11,1 'aaaaaa

Aaaaaaa

A

2D Geometrical Transformations

Translate

Rotate Scale

Shear

Translate Points

Recall.. We can translate points in the (x, y) plane to new positions by adding translation amounts to the coordinates of the points. For each point P(x, y) to be moved by dx units parallel to the x axis and by dyunits parallel to the y axis, to the new point P’(x’, y’ ). The translation has the following form:

y

x

dyydxx

+=+=

''

P(x,y)

P’(x’,y’)

dx

dy

In matrix format:

+

=

y

x

dd

yx

yx

''

If we define the translation matrix , then we have P’ =P + T.

=

y

x

dd

T

Scale Points

Points can be scaled (stretched) by sx along the x axis and by syalong the y axis into the new points by the multiplications:

We can specify how much bigger or smaller by means of a “scale factor”

To double the size of an object we use a scale factor of 2, to half the size of an obejct we use a scale factor of 0.5

ysyxsx

y

x

==''

P(x,y)

P’(x’,y’)

xsx• x

sy• y

y

=

yx

ss

yx

y

x

00

''

If we define , then we have P’ =SP

=

y

x

ss

S0

0

Rotate Points (cont.)

Points can be rotated through an angle θ about the origin:

θθθαθα

θαθα

θθθαθα

θαθα

cossincossinsincos

)sin()sin(|'|'

sincossinsincoscos

)cos()cos(|'|'

|||'|

yxll

lOPy

yxll

lOPx

lOPOP

+=+=

+=+=

−=−=

+=+=

==

P(x,y)

P’(x’,y’)

xx’

y’

y

θα

l

O

−=

yx

yx

θθθθ

cossinsincos

''

P’ =RP

Review…

• Translate: P’ = P+T• Scale: P’ = SP• Rotate: P’ = RP

• Spot the odd one out…– Multiplying versus adding matrix…– Ideally, all transformations would be the same..

• easier to code

• Solution: Homogeneous Coordinates

Homogeneous Coordinates

For a given 2D coordinates (x, y), we introduce a third dimension:

[x, y, 1]

In general, a homogeneous coordinates for a 2D point has the form:

[x, y, W]

Two homogeneous coordinates [x, y, W] and [x’, y’, W’] are said to be of the same (or equivalent) if

x = kx’ eg: [2, 3, 6] = [4, 6, 12]y = ky’ for some k ≠ 0 where k=2W = kW’

Therefore any [x, y, W] can be normalised by dividing each element by W:[x/W, y/W, 1]

Homogeneous Transformations

Now, redefine the translation by using homogeneous coordinates:

Similarly, we have:

+

=

y

x

dd

yx

yx

''

=

11001001

1''

yx

dd

yx

y

x

PTP ×='

=

11000000

1''

yx

ss

yx

y

x

−=

11000cossin0sincos

1''

yx

yx

θθθθ

Scaling Rotation

P’ = S × P P’ = R × P

Composition of 2D Transformations

1. Additivity of successive translations

We want to translate a point P to P’ by T(dx1, dy1) and then to P’’ by another T(dx2, dy2)

On the other hand, we can define T21= T(dx1, dy1) T(dx2, dy2) first, then apply T21 to P:

where

]),()[,('),('' 112222 PddTddTPddTP yxyxyx ==

PTP 21'' =

++

=

=

=

1001001

1001001

1001001

),(),(

21

21

1

1

2

2

112221

yy

xx

y

x

y

x

yxyx

dddd

dd

dd

ddTddTT

T(-1,2) T(1,-1)

(2,1)

(1,3)(2,2)

=

−=

100110001

100210101

100110

101

21T

Examples of Composite 2D Transformations

Composition of 2D Transformations (cont.)

2. Multiplicativity of successive scalings

where

PSPssSssS

PssSssSP

yxyx

yxyx

21

1122

1122

)],(),([

]),()[,(''

=

=

=

=

=

=

1000*000*

1000000

1000000

),(),(

12

12

1

1

2

2

112221

yy

xx

y

x

y

x

yxyx

ssss

ss

ss

ssSssSS

Composition of 2D Transformations (cont.)

3. Additivity of successive rotations

where

PRPRR

PRRP

21

12

12

)]()([])()[(''

===

θθθθ

+++−+

=

−=

=

1000)cos()sin(0)sin()cos(

1000cossin0sincos

1000cossin0sincos

)()(

1212

1212

11

11

22

22

1221

θθθθθθθθ

θθθθ

θθθθ

θθ RRR

Composition of 2D Transformations (cont.)

4. Different types of elementary transformations discussed above can be concatenated as well.

where

MPPddTR

PddTRP

yx

yx

=

=

=

)],()([

]),()[('

θ

θ

),()( yx ddTRM θ=

Consider the following two questions:

1) translate a line segment P1 P2, say, by -1 units in the x direction and -2 units in the y direction.

2). Rotate a line segment P1 P2, say by θ degrees counter clockwise, about P1.

P1(1,2)

P2(3,3)

P’2

P’1

P1(1,2)

P2(3,3)P’2

P’1

θ

Other Than Point Transformations…

Translate Lines: translate both endpoints, then join them.

Scale or Rotate Lines: More complex. For example, consider to rotate an arbitrary line about a point P1, three steps are needed:1). Translate such that P1 is at the origin;2). Rotate;3). Translate such that the point at the origin

returns to P1.

T(-1,-2) R(θ)

P1(1,2)

P2(3,3)

P2(2,1)T(1,2)P2

P2

P1

P1P1

θ

Another Example.

Scale

Translate

Rotate

Translate

Order Matters!

As we said, the order for composition of 2D geometrical transformations matters, because, in general, matrix multiplication is not commutative. However, it is easy to show that, in the following four cases, commutativity holds:

1). Translation + Translation2). Scaling + Scaling3). Rotation + Rotation4). Scaling (with sx = sy) + Rotation

just to verify case 4:

if sx = sy, M1 = M2.

−=

=

=

1000cos*sin*0sin*cos*

1000cossin0sincos

1000000

)(),(1

θθθθ

θθθθ

θ

yy

xx

y

x

yx

ssss

ss

RssSM

−=

−=

=

1000cos*sin*0sin*cos*

1000000

1000cossin0sincos

),()(2

θθθθ

θθθθ

θ

yx

yx

y

x

yx

ssss

ss

ssSRM

Rigid-Body vs. Affine Transformations

A transformation matrix of the form

where the upper 2×2 sub-matrix is orthogonal, preserves angles and lengths. Such transforms are called rigid-body transformations, because the body or object being transformed is not distorted in any way. An arbitrary sequence of rotation and translation matrices creates a matrix of this form.

The product of an arbitrary sequence of rotation, translations, and scale matrices will cause an affine transformation, which have the property of preserving parallelism of lines, but not of lengths and angles.

1002221

1211

y

x

trrtrr

Rigid-Body vs. Affine Transformations (cont.)

Shear transformation is also affine.

Unit cube 45º Scale in x, not in y

Rigid- bodyTransformation

AffineTransformation

Shear in the x direction Shear in the y direction

=

10001001 a

SH x

=

10001001

bSH y

3D Transformations

• 2D coordinates • 3D coordinates

x

y

x

y

z

xz

y

Right-handed coordinate system:

3D Transformations (cont.)

1. Translation in 3D is a simple extension from that in 2D:

2. Scaling is similarly extended:

=

1000100010001

),,(z

y

x

zyx ddd

dddT

=

1000000000000

),,(z

y

x

zyx ss

s

sssS

3D Transformations (cont.)

3. The 2D rotation introduced previously is just a 3D rotation about

the z axis.

similarly we have: X

Y

Z

=

1000010000cossin00sincos

)(θθθθ

θzR

=

10000cossin00sincos00001

)(θθθθ

θxR

−=

10000cos0sin00100sin0cos

)(θθ

θθ

θyR

Composition of 3D Rotations

In 3D transformations, the order of a sequence of rotations matters!

=

=

10000cos0sin0sinsincoscossin0sincossincoscos

10000cos0sin00100sin0cos

1000010000cossin00sincos

)()(βββααβαβααβα

ββ

ββαααα

βα yz RR

=

−=

10000cossinsincossin00cossin0sincossincoscos

1000010000cossin00sincos

10000cos0sin00100sin0cos

)()(βαβαβ

ααββαβα

αααα

ββ

ββ

αβ zy RR

)()()()( βααβ yzzy RRRR ≠

More Rotations

We have shown how to rotate about one of the principle axes, i.e. the

axes constituting the coordinate system. There are more we can do,

for example, to perform a rotation about an arbitrary axis:

X

Y

Z

P2(x2, y2 , z2)

P1(x1, y1 , z1)

We want to rotate an object about an

axis in space passing through (x1, y1, z1)

and (x2, y2, z2).

Rotating About An Arbitrary AxisY

Z

P2

P1

1). Translate the object by (-x1, -

y1, -z1): T(-x1, -y1, -z1)

X

Y

Z P2

P1

2). Rotate the axis about x so

that it lies on the xz plane: Rx(α)

X

X

Y

Z P2

P1

3). Rotate the axis about y so

that it lies on z: Ry (β)

X

Y

Z P2

P1

4). Rotate object about z by θ: Rz(θ)

β

α

Rotating About An Arbitrary Axis (cont.)

After all the efforts, don’t forget to undo the rotations and the translation!

Therefore, the mixed matrix that will perform the required task of rotating an

object about an arbitrary axis is given by:

M = T(x1,y1,z1) Rx(-α)Ry(-β) Rz(θ) Ry(β) Rx(α)T(-x1,-y1,-z1)

Finding β is trivial, but what about α?

The angle between the z axis and the

projection of P1 P2 on yz plane is α.

X

Y

Z

P2

αP1

2D Transformations

What is a transformation?

• A transformation is an operation that transforms or changes a shape (line, drawing etc.)

• There are several basic ways you can change a shape: – translation (moving it)

– rotation (turning it round)

– scaling (making it bigger or smaller).

• There are others, but we’ll worry about them later.

2D Geometrical Transformations

Translate

Rotate Scale

Shear

Translation

• Consider the arrow below. We may want tomove it from position A to position B.

A

B

dx

dy

x

y

Translate Points

We can translate points in the (x, y) plane to new positions by adding translation amounts to the coordinates of the points. For each point P(x, y) to be moved by dx units parallel to the x axis and by dyunits parallel to the y axis, to the new point P’(x’, y’ ). The translation has the following form:

y

x

dyydxx

+=+=

''

P(x,y)

P’(x’,y’)

dx

dy

Translating a line or shape

• To translate a line, simply translate both of its end points.

• Translating a shape therefore, simply translate all of thepoints.

• The same applies for all transformations.

Scale Points

Points can be scaled (stretched) by sx along the x axis and by syalong the y axis into the new points by the multiplications:

We can specify how much bigger or smaller by means of a “scale factor”

To double the size of an object we use a scale factor of 2, to half the size of an obejct we use a scale factor of 0.5

ysyxsx

y

x

==''P(x,y)

P’(x’,y’)

xsx• x

sy• y

y

We first review the idea of sin and cos for a given angle θ :

Useful formulas:

Rotate Points

θθ

θθ

sincos

/sin/cos

|| 22

lylx

lylx

yxOPl

==

==

+==

θθθθθαθαθαθαθαθα

sin)sin(cos)cos(sinsincoscos)cos(sincoscossin)sin(

−=−=−−=++=+

P

x

y

θ

l

O

Rotate Points (cont.)

Points can be rotated through an angle θ about the origin:

θθθαθα

θαθα

θθθαθα

θαθα

cossincossinsincos

)sin()sin(|'|'

sincossinsincoscos

)cos()cos(|'|'

|||'|

yxll

lOPy

yxll

lOPx

lOPOP

+=+=

+=+=

−=−=

+=+=

==

P(x,y)

P’(x’,y’)

xx’

y’

y

θα

l

O

Identity transformations

• Some transformations lead to no change in the shape/drawing

• Translate(0,0)• Rotate(0)• Scale(1,1)

Order of transformations

• The order in which transformations are applied to ashape is important.

• Performing a translation followed by a rotation, will givean entirely different drawing to a performing the rotationfollowed by the same translation.

Order of transformations

x

y

rotation by 45° followed by translation (100,0)

Order of transformations

x

translation (100,0) followed by rotation by 45°

y

Rotation around local origin

• At the moment, our rotation is centred around theorigin. What is often required is to spin the shape inits original location (in-situ)

x

y

local origin

Rotation around local origin

• This can be achieved by combining two of our existingtransformations: Translate the shape to the origin, rotateit the required amount about the origin and translate itback to where it was.

Local rotation

x

y

dy

dx

Local rotation

x

y

Local rotation

y

x

y

Local rotation

x

+dy

+dx

Summary

• So we have looked at simple transformation ofpoints/lines etc.

• Next we will look at transformations using matrices.

Composite 3D Transformations

Example of Composite 3D Transformations

Try to transform the line segments P1P2 and P1P3 from their start position in (a) to their ending position in (b).

The first solution is to compose the primitive transformations T, Rx, Ry, and Rz. This approach is easier to illustrate and does offer help on building an understanding. The 2nd, more abstract approach is to use the properties of special orthogonal matrices.

y

xz

y

xz

P1 P2

P3

P1

P2

P3

(a) (b)

Composition of 3D Transformations

Breaking a difficult problem into simpler sub-problems:1.Translate P1 to the origin.2. Rotate about the y axis such that P1P2 lies in the (y, z) plane.3. Rotate about the x axis such that P1P2 lies on the z axis.4. Rotate about the z axis such that P1P3 lies in the (y, z) plane.

y

xz

y

xz

y

xz

P1 P2

P3

P1P2

P3

y

xz

P1 P2

P3

y

xz

P1P2

P3P3

P2 P1

1

2

34

Composition of 3D Transformations

1.

2.

−−−

=

−−−

1000100010001

),,(

1

1

1

111

zyx

zyxT

=

−−

10000sin0cos00100cos0sin

))90((

θθ

θθ

θ

yR

T

T

T

zzyyxxPzyxTPzzyyxxPzyxTP

PzyxTP

]1[),,(

]1[),,(

]1000[),,(

1313133111'

3

1212122111'

2

1111'

1

−−−=•−−−=

−−−=•−−−=

=•−−−=

y

xz

P′1 P′2

P′3

θD1

Composition of 3D Transformations

3

4.

=

10000cossin00sincos00001

)(

φφφφ

φxRy

xz

P′′1P′′2 φD2

y

xz

P′′′1

P′′′2

αD3

P′′′3

)(αzR

TRzyxTRRR yxz ⋅=−−−⋅−⋅⋅ ),,()90()()( 111θφα

Finally, we have the composite matrix:

Vector Rotation

y

x

y

Rotate the vector

01

u

=

−=

θθ

θθθθ

sincos

01

cossinsincos

u

The unit vector along the x axis is [1, 0]T. After rotating about the origin by θ, the resulting vector is

x

Vector Rotation (cont.)

y

Rotate the vector

10

−=

−=

θθ

θθθθ

cossin

10

cossinsincos

v

x

y

θv

The above results states that if we try to rotate a vector, originally pointing the direction of the x (or y) axis, toward a new direction, u (or v), the rotation matrix, R, could be simply written as [u | v] without the need of any explicit knowledge of θ, the actual rotation angle.

Similarly, the unit vector along the y axis is [0, 1]T. After rotating about the origin by θ, the resulting vector is

Vector Rotation (cont.)

The reversed operation of the above rotation is to rotate a vector that is not originally pointing the x (or y) direction into the direction of the positive x or y axis. The rotation matrix in this case is R(-θ ), expressed by R-1(θ )

where T denotes the transpose.

)(cossinsincos

)cos()sin()sin()cos(

)(1 θθθθθ

θθθθ

θ TT

T

Rvu

R =

=

=

−−−−−

=−

x xθ

yy

Rotate the vectoruu

Example

what is the rotation matrix if one wants the vector T in the left figure to be rotated to the direction of u.

T

(2, 3)

[ ] TT

uu

=+

=133

132

3232

|| 22

[ ]

−==

132

133

133

132

| vuR

If, on the other hand, one wants the vector u to be rotated to the direction of the positive x axis, the rotation matrix should be

−=

=

132

133

133

132

T

T

vu

R

Rotation Matrices

Rotation matrix is orthonormal:• Each row is a unit vector

• Each row is perpendicular to the other, i.e. their dot product is zero.

• Each vector will be rotated by R(θ) to lie on the positive x and y axes, respectively. The two column vectors are those into which vectors along the positive x and y axes are rotated.

• For orthonormal matrices, we have

1sincos1)sin(cos

cossinsincos

22

22

=+=−+

−=

θθθθ

θθθθ

R

0)sin(cossincos =−×+× θθθθ

)()(1 θθ TRR =−

Cross Product

• The cross product or vector product of two vectors, v1 and v2, is another vector:

• The cross product of two vectors is orthogonal to both• Right-hand rule dictates direction of cross product.

−−−−

1221

1221

1221

21 )(y x y xz x z x

z y z yvv

v1

v2v1× v2

u2

Extension to 3D Cases

The above examples can be extended to 3D cases….

In 2D, we need to know u, which will be

rotated to the direction of the positive x axis.uv

x

y

z u1

v=u1×u2

In 3D, however, we need to know more than

one vector. See in the left figure, for example,

two vectors, u1 and u2 are given. If after

rotation, u1 is aligned to the positive z axis, this

will only give us the third column in the rotation

matrix. What about the other two columns?

3D Rotation

In many cases in 3D, only one vector will be aligned to one of the coordinate axes, and the others are often not explicitly given. Let’s see the example:

y

xz

y

xz

P1 P2

P3

P1

P2

P3

Note, in this example, vector P1P2 will be

rotated to the positive z direction. Hence the

fist column vector in the rotation matrix is the

normalised P1P2. But what about the other

two columns? After all, P1P3 is not perpendi-

cular to P1P2. Well, we can find it by taking

the cross product of P1P2 and P1P3. Since

P1P2 × P1P3 is perpendicular to both P1P2

and P1P3, it will be aligned into the direction

of the positive x axis.

And the third direction is decide by the cross

product of the other two directions, which is

P1P2 ×(P1P2 ×P1P2 ).

Therefore, the rotation matrix should be

3D Rotation (cont.)

u

y

xz

P1

P2

P3

vw

××××

××

=

21

21

312121

312121

3121

3121

)()(

PPPP

PPPPPPPPPPPP

PPPPPPPP

R

y

xz

P1P2

P3

u

v

Yaw, Pitch, and Roll

Imagine three lines running through an airplane and intersecting at right angles at the airplane’s centre of gravity.

Roll: rotation around the

front-to-back axis.

Roll: rotation around the

side-to-side axis.

Roll: rotation around the

vertical axis.

An Example of the Airplane

Consider the following example. An airplane is oriented such that its nose is pointing in the positive z direction, its right wing is pointing in the positive xdirection, its cockpit is pointing in the positive y direction. We want to transform the airplane so that it heads in the direction given by the vector DOF (direction of flight), is centre at P, and is not banked.

Solution to the Airplane Example

First we are to rotate the positive zp direction into the direction of DOF, which gives us the third column of the rotation matrix: DOF / |DOF|. The xpaxis must be transformed into a horizontal vector perpendicular to DOF –that is in the direction of y×DOF. The yp direction is then given by xp × zp = DOF ×(y × DOF).

××××

××

=DOFDOF

DOFyDOFDOFyDOF

DOFyDOFyR

)()(

Inverses of (2D and) 3D Transformations

1. Translation:

2. Scaling:

3. Rotation:

4. Shear:

),,(),,(1zyxzyx dddTdddT −−−=−

)1,1,1(),,(1

zyxzyx sss

SsssS =−

)()()(1 θθθ TRRR =−=−

),(),(1yxyx shshSHshshSH −−=−

Transformation as a change in coordinate system

Local versus world coordinate system

• Until now, we have defined all points, lines, etc, within the world coordinate system.

• Not ideal to have every object defined inside the world-coordinate system.

• Better (read easier, more re-usable) to have ‘objects’, each with their own ‘local’ coordinate system… which can then be placed into the world coordinate system.

Local versus world coordinate system

• The idea is analogous to having several pieces of paper…– Each page with an object on it which is being

translated, scaled, rotated.. Before being placed on the world coordinate system

Local versus world coordinate system

Local versus world coordinate system

• This requires a change in mentality when drawing our objects.

• Before, we drew an object in the world-coordinate system, scaled it, rotated it, and then transformed it to the required location.

• With a ‘local’ coordinate system, the reverse is the case… • We translate our local coordinate system, then rotate,

then scale, and finally draw our object…• The advantage of this, is that each object (or page) can

in turn be made of several simliar ‘local’ coordinate systems, which can be draw with out regard to the ‘world’ coordinate system…

Local versus world coordinate system

• Let define the representation of a point in the coordinate system i, and define the representation of a point in the coordinate system j.

• Then let define the transformation the converts the representation of a point in the coordinate system j into its representation in the coordinate system i, such that:

)(iP

)()( jji

i PMP •= ←

)( jP

jiM ←

Local versus world coordinate system

• If for coordinate system k,then:

• so

)()( kkj

j PMP •= ←

)()()()( kki

kkjii

jji

i PMPMMPMP •=••=•= ←←←←

kjjiki MMM ←←← •=

)2,4(21 TM =← )5.0,5.0()3,2(32 STM •=←

)5.0,5.0()3,2()2,4(31 STTM ••=←

P

)6,6()2,0()5.0,5.0()3,2()2,4(3311 =•••=•= ← STTPMP

)6,6(1 =P

)2,0(3 =P

Local versus world coordinate system

• Computer graphics languages / APIs typically use this concept of local coordinate systems…

• Cue processing example…

Viewing in 3D

Projections

• Display device (a screen) is 2D…– How do we map 3D objects to 2D space?

• 2D to 2D is straight forward…– 2D window to world.. and a viewport on the 2D

surface.– Clip what won't be shown in the 2D window, and map

the remainder to the viewport.

• 3D to 2D is more complicated…– Solution : Transform 3D objects on to a 2D plane

using projections

Projections

• In 3D…– View volume in the world– Projection onto the 2D projection plane– A viewport to the view surface

• Process…– 1… clip against the view volume, – 2… project to 2D plane, or window,– 3… map to viewport.

Projections

• Conceptual Model of the 3D viewing process

Projections

• Projections: key terms…– Projection from 3D to 2D is defined by straight projection

rays (projectors) emanating from the 'center of projection', passing through each point of the object, and intersecting the 'projection plane' to form a projection.

Types of projections

• 2 types of projections – perspective and parallel.

• Key factor is the center of projection. – if distance to center of projection is finite : perspective– if infinite : parallel

Perspective v Parallel

• Perspective: – visual effect is similar to human visual system... – has 'perspective foreshortening'

• size of object varies inversely with distance from the center of projection.

– angles only remain intact for faces parallel to projection plane.

• Parallel:– less realistic view because of no foreshortening– however, parallel lines remain parallel. – angles only remain intact for faces parallel to

projection plane.

Perspective Projections

• Any parallel lines not parallel to the projection plane, converge at a vanishing point. – There are an infinite number of these, 1 for each of

the infinite amount of directions line can be oriented.

• If a set of lines are parallel to one of the three principle axes, the vanishing point is called an axis vanishing point. – There are at most 3 such points, corresponding to the

number of axes cut by the projection plane.

Perspective Projections

• Example: – if z projection plane cuts the z axis: normal to it, so

only z has a principle vanishing point, as x and y are parallel and have none.

• Can categorise perspective projections by the number of principle vanishing points, and the number of axes the projection plane cuts.

Perspective Projections

• 2 different examples of a one-point perspective projection of a cube.(note: x and y parallel lines do not converge)

Perspective Projections

• Two-point perspective projection:– This is often used in architectural, engineering

and industrial design drawings. – Three-point is used less frequently as it adds

little extra realism to that offered by two-point perspective projection.

Perspective Projections

• Two-point perspective projection:

Perspective Projections

p (x,y,z)

z

y

Projection plane

ps(xs,ys)C

d

By similar triangles:

p(x,y,z)

z

x

Projection plane

ps(xs,ys)C

d

dx

dzx s=+

dz

xxs

+=

1

dz

yys

+=

1

Perspective Projections

=

+ 1

.

1/100000000100001

10 z

yx

ddz

yx

s

s

+

+

=

+

10

1

1

10

dz

ydz

x

dz

yx

s

s

s

s

Parallel Projections

• 2 principle types: – orthographic and oblique.

• Orthographic : – direction of projection = normal to the projection

plane.

• Oblique : – direction of projection != normal to the projection

plane.

Parallel Projections

• Orthographic (or orthogonal) projections: – front elevation, top-elevation and side-elevation. – all have projection plane perpendicular to a principle axes.

• Useful because angle and distance measurements can be made...

• However, As only one face of an object is shown, it can be hard to create a mental image of the object, even when several view are available.

Parallel Projections

• Orthogonal projections:

Parallel Projections

• Oblique parallel projections– Objects can be visualised better then with

orthographic projections– Can measure distances, but not angles*

* Can only measure angles for faces of objects parallel to the plane

• 2 common oblique parallel projections: – Cavalier and Cabinet

Parallel Projections

• Cavalier:– The direction of the projection makes a 45 degree angle

with the projection plane. – Because there is no foreshortening, this causes an

exaggeration of the z axes.

Parallel Projections

• Cabinet:– The direction of the projection makes a 63.4 degree angle with

the projection plane. This results in foreshortening of the z axis, and provides a more “realistic” view.

Oblique Parallel Projections

• Cavalier, cabinet and orthogonal projections can all be specified in terms of (α, β) or (α, λ) since – tan(β) = 1/λ

α

β

P=(0, 0, 1)

P’

λ cos(α)

λ sin(α)

λ

Oblique Parallel Projections

λ=1 β = 45 Cavalier projection α = 0 - 360

λ=0.5 β = 63.4 Cabinet projection α = 0 – 360

λ=0 β = 90 Orthogonal projection α = 0 – 360

Oblique Parallel Projections

x

y

λα

(xs,ys)

(0,0,1)P

Consider the point P:

P can be represented in 3D space - (0,0,1)

P can be represented in 2D (screen coords) - (xs,ys)

Oblique Parallel Projections

• At (0,0,1)xs = λ cos αys = λ sin α

• Generally – multiply by z and allow for (non-zero) x and y

xs= x + z.λ.cos αys = y + z.λ.sin α

Oblique Parallel Projections

=

1

.

100000000sin100cos01

10 z

yx

yx

s

s

αλαλ

Basic Raster Graphics Algorithms for 2D Drawing

• Raster Display:Primitive element: pixel Raster: A rectangular array of points or dotsPixel: One dot or picture element of the rasterScan line: A row of pixels

Great asset: total control of the image Biggest problems: you work at a particular RESOLUTION

Plotting pixels

• In it’s most basic form, each pixel can be set to black or white (i.e. turned on or off), allowing patterns of dots to be created on the screen.

• Classic problem… “staircasing”.

Memory Mapping• Drawing on the computer screen is achieved by

setting the right pixels either on or off. Each pixel on the screen corresponds to an address in the computers memory - this is known as memory mapping and the display is said to be a “memory mapped display.”

Memory Mapping

Cartesian Coordinate System

• In practice, the Cartesian coordinate system is used to define the location of pixels, leaving the computer to convert the coordinates to specific memory locations.

• The modern Cartesian coordinate system in two dimensions (also called a rectangular coordinate system) is defined by two axes, at right angles to each other, forming a plane (an xy-plane). The horizontal axis is labeled x, and the vertical axis is labeled y.

• The coordinates in a three dimensional system are of the form (x,y,z).

Cartesian Coordinate System

O x

y

p(x, y)

Line Drawing

• A (straight) line can be mathematically defined by its end points. Todescribe a line we need simply to state the coordinates of the two ends.

e.g. - (3, 8),(12, 20)

p(12, 20)

p(3, 8)

Line Drawing

Drawing lines on a raster grid implicitly involves approximation. The general process is called rasterization or scan-conversion.

Optimal Line Drawing

– straight – pass through endpoints – smooth – independent of endpoint

order – uniform brightness – brightness independent of

slope – efficient

What is the best way to draw a line from the pixel (x1,y1) to (x2,y2)? Such a line should ideally have the following properties:

O x

y Line Characterisations

Explicit: y = mx + B Implicit: F(x,y) = ax + by + c = 0

Constant slope:

(xi , yi)

mxy=

∆∆

(xi , yi)

The simplest strategy is:1) Compute m;2) Increment x by 1 starting

with the leftmost point;3) Calculate yi = mxi + B;4) Intensify the pixel at (xi ,

Round(yi)), where Round(yi) = Floor(0.5+yi)

(xi , Round(yi))

The above brute-force strategy is inefficient, however, becauseeach iteration requires a floating-point (or binary fraction) multiply,Addition, and invocation of Floor. We can eliminate the multiplicationby noting that:

And if ∆x =1, then yi+1 = yi + m. Thus, a unit of change in x changesy by m, which is the slope of the line.

xmyBxxmBmxy iiii ∆+=+∆+=+= ++ )(11

(xi , yi)

(xi , Round(yi))(xi+1 , yi+1)

(xi +1, yi + m)

Simple Incremental Algorithm

An incremental algorithm can be defined: At each step, we make incremental calculations based on the preceding step, starting from one of the endpoints.

Slope Problem

• Note that in order to implement the above algorithm, the slope mhas to be between 0 and 1; then we are able to step along x axis; otherwise, we must reverse the roles of x and y and assigning a unit step to y and incrementing x by ∆x = ∆y/m = 1/m.

m >1, cannot step along x. To handle this, swap x and y.

m <1, can step along x.

Midpoint Line (Bresenham) Algorithm

• What is wrong with the incremental algorithm?– It requires floating-point operations (Round)– The time-consuming floating-point operations are

unnecessary because both endpoints are integers.– Therefore, instead of incrementing y and then rounding it at each

step, we just go to the right, or to the right and up using only integer quantities.

Increment Decision

The problem becomes to decide on which side of the line the midpoint lies?

Q

P = (xp , yp)

M

E

NE

Assume the slope of the line, m, is between 0 and 1.Consider the line in the right figure, where the previously selected pixel is P (xp , yp). Now, we must choose between the pixel one increment to the right (called the east pixel, E) or the pixel one increment to the right and one increment up (called the northeast pixel, NE). Let Qbe the intersection point of the line being scan-converted with the grid line x = xp+1. Let M be the midpoint between E and NE. It is easy to see that , if M lies above the line, pixel Eis closer to the line; if M is below the line, pixel NE is closer to the line.

Solution

Let’s consider the explicit form of the line: y = mx + B.We have

F(x,y) = dy• x - dx• y + B• dx = 0.Compare with the implicit form

F(x,y) = ax + by + c = 0We have a=dy, b = -dx, and c =B• dx.

P (x0,y0)

P (x1,y1)

dx=x1 – x0

dy=y1 -y

0

01

01

xxyy

dxdym

−−

=

Given a point (x , y), investigate the sign of F:

+= 0),( yxF

Point below the line

Point on the line

Point above the line

Decision Variable d

Since we’re trying to decide the relationship between M and the line, we need only to compute

and to test its sign.

Define a decision variable d:

)21,1()( ++= pp yxFMF

+=

++++=++=

0

)21()1()

21,1( cybxayxFd pppp

Choose pixel NEChoose pixel E

Choose pixel E

Q

(xp , yp)

M

E

NE

(xp +1, yp)

)21,1( ++ pp yx

(xp+1, yp+1)

How will d be used?

Assume E is chosen, M is incremented by one step in the x direction. Then

dydad

ayxFcybaxa

cybxayxFd

oldold

pppp

ppppnew

+=+=

+++=+++++=

++++=++=

)21,1()

21()1(

)21()2()

21,2(

Q

(xp , yp)M

E

NE

(xp +1, yp) (xp +2, yp)

)21,2( ++= ppnew yxFd

(xp+1, yp+1)(xp+2, yp+1)

)21,1( ++= ppold yxFd

Incremental updateWe call the increment to add after E is chosen ∆E : ∆E = a = dy. In other words, we can derive the value of the decision variable at the next step incrementally from the value at the current step without having to computing F(M) directly, by merely adding ∆E .On the other hand, if NE is chosen:

Therefore, ∆NE = a + b= dy – dx.Now the initial condition:

Hence, dstart = a +b/2 = dy – dx/2.

dxdydbad

bayxFcbybaxa

cybxayxFd

oldold

pppp

ppppnew

−+=++=

++++=++++++=

++++=++=

)21,1()

21()1(

)23()2()

23,2(

2/),(

)21()1()

21,1(

00

0000

bayxF

cybxayxF

++=

++++=++

Summary to the midpoint technique

Let’s summarise the incremental midpoint algorithm. At each step, the algorithm chooses between two pixels based on the sign of the decision variable calculated in the previous iteration; then it updates the decision variable by adding either ∆E or ∆NE to the old value, depending on the choice of the pixel. The algorithm starts from the first endpoint, and the first decision variable is given by a+b/2. Using dstart, we choose the second pixel, and so on.

To eliminate the fraction in dstart, we redefine our original F by multiplying it by 2; F(x,y) = 2(ax+by+c). This multiplies each constant the decision variable (and the increments ∆E and ∆NE) by 2 but does not affect the sign of the decision variable, which is all that matters for the midpoint test.

Summary to the midpoint technique (cont.)

Initialisation: dstart = 2 × a +b = 2×dy – dx where dy = y1 – y0 and dx = x1 – x0 .

Incremental update: 1) if E was chosen, ∆E = 2× dy dnew = dold + ∆E

2) if NE was chosen, ∆NE = 2× (dy – dx)dnew = dold + ∆NE

Advantage: The arithmetic needed to evaluate dnew for any step is a simple integer addition. No time-consuming multiplication is involved. Further, the incremental update is quite simple, therefore, it is an efficient algorithm. Note: the given algorithm only works for those line with slope (0, 1).

Lines: Arbitrary Directions

Step through x, increment y

Step through x, decrement y

Step through y, decrement x

Step through y, increment x

1

8

2

7

3

6

4

5

Example• Line end points:

(x0 , y0) = (5, 8) (x1 , y1) = (9, 11)

Solution:dx = 4dy = 3dstart=2*dy-dx=2 > 0NE is chosen∆NE = 2*(dy-dx) = -2 <0d = d + ∆NE = 0E is chosen

…4 5 6 7 8 9 10 116

7

8

9

10

11

12

13

Lines: Some Remarks• Weaker Intensity of Diagonal Lines.

Consider the two scan-converted lines in the left figure. The diagonal line, B, has a slope of 1 and hence is times as long as A, the horizontal line. Yet the same number of pixels (9) is drawn to represent each line. If the intensity of each pixel is I, then the intensity per unit length of line A is I, whereas for line B it is only I/ .

2

2

Filling Polygons• Three types of polygons

1. Simple convex 2. simple concave 3. non-simple(self-intersection)

Convex polygons have the property that intersecting lines crossing it either one (crossing a corner), two (crossing an edge, going through the polygon and going out the other edge), or an infinite number of times (if the intersecting line lies on an edge).

Some Problems

1. Which pixels should be filled in? 2. Which happened to the top pixels? To the rightmost pixels?

Some Remarks to the 2nd problem

Why is the 2nd problem such a big deal? What would happen if we fill the top and right most pixels? Because this will cause “double-fill” when two rectangles are adjacent. “Double-filling” brings the following two disadvantages:

1). Inefficient2). If polygons have different colour, then final

colour depends on the order in which the polygons are drawn, and may lead to “flicker”.

General Ideas about Polygon Filling

• Rules for shared edges:– A shared vertical edge belongs to the rightmost of the two

sharing shapes. (Therefore, the right edge of rectangles will not be filled).

– A shared non-vertical edge belongs to the upper shape. (Therefore, the top edge of rectangles will not be filled).

• Fill in polygons by computing intersections of boundaries with scan lines, and filling between pairs of intersections– This is the actual algorithm!

A span is the collection of adjacent pixels on a single scan line which lie inside the primitive.

Coherence literally means to be logically consistent or connected. Spatial coherencemeans that primitives don't change an awful lot if at all from pixel to pixel within a scan line or from scan line to scan line. This allows us to optimise our algorithms.

Edge coherence means that most of the edges that intersect scan line i also intersect scan line i+1.

Illustration of the Ideas

scan lines

spanspan

span

span

Filling the Spans

Span-filling is an important step in the whole polygon-filling algorithm, and i is implemented by a three-step process:

1. Find the intersections of the scan line with all edges of the polygon.

2. Sort the intersections by increasing x coordinates.3. Fill in all pixels between pairs of intersections that lie interior to

the polygon.

Now more questions arise:– How do we find and sort the intersections efficiently?– How do we judge whether a pixel lying inside or outside the

polygon?

Parity (Odd-Even) Rule

Begin from a point outside the polygon, increasing the x value, counting the number of edges crossed so far, a pixel is inside the polygon if the number of edges crossed so far (parity) is odd, and outside if the number of edges crossed so far (parity) is even. This is known as the parity, or the odd-even, rule. It works for any kind of polygons.

Parity starting from even

oddodd

odd

odd

even

even

even

Implementation of the Ideas

: intersection points between scan lines and edges (span extrema).

: interior pixels

Implementation of the Ideas (cont.)

Filled pixels using the strictly inside principle.

Q1: Given an intersection with an arbitrary, fractional xvalue, how do we determine which pixel on either side of that intersection is interior?

Four Elaborations to the 2nd Question

A: The strictly

inside rule:

Q2: How do we deal with the special case of intersections at integer pixel coordinates?

A: Use the criterion we used for avoid conflicting between shared edges: if the leftmost pixel in a span has integer x coordinate, we define it to be interior; if the rightmost pixel has integer x coordinate, we define it to be exterior.

Four Elaborations (cont.)

Q3: How do we deal with the special case for shared vertices?

A: We count the ymin vertex of an edge in the parity calculation but not the ymax vertex.

Four Elaborations (cont.)

A

ymin

ymax

B

C

A

ymin ymin

B

C

Q4: How do we deal with the special case in which the vertices define a horizontal edge?

A: Bottom edges are drawn but top edges are not.

Four Elaborations (cont.)

Bottom edges are drawn Top edges are not

Example

Let’s apply the rules to scan line 8 below. We fill in the pixels from point a, pixel (2, 8), to the first pixel to the left of point b, pixel (4, 8), and from the first pixel to the right of point c, pixel (9, 8), to one pixel to the left of point d, pixel (12, 8). For scan line 3, vertex A counts once because it is the ymin vertex of edge FA, but the ymax vertex of edge AB; this causes odd parity, so we draw the span from there to one pixel to the left of the intersection with edge CB.

oddoddeven evena b c d

A

B

C

D

E

F

Four Elaborations (cont.)

A B

C D

E

FG

HI

J

E

A B

C D

FG

HI

J

Four Elaborations (cont.)

We deal properly with the horizontal edges by not counting their vertices. For the figure in the last slide, consider bottom edge AB. Vertex A is a yminvertex for edge JA, and AB does not contribute. Therefore, the parity is odd and the span AB is drawn. Vertical edge BC has its ymin at B, but again ABdoes not contribute. The parity becomes even, and the span is terminated. At vertex J, edge IJ has a ymin vertex but edge JA does not, so the parity becomes odd and the span is drawn to edge BC. The span that starts at edge IJ and hits C sees no change at C because C is a ymax vertex for BC, so the span continues along bottom edge CD; at D, however, edge DE has a ymin vertex, so the parity is reset to even and the span ends. At I, edge IJhas its ymax vertex and edge HI also does not contribute, so parity stays even and the top edge IH is not drawn. At H, however, edge GH has a yminvertex, the parity becomes odd, and the span is drawn from H to the pixel to the left of the intersection with edge EF. Finally, there is no ymin vertex at G, nor is there one at F, so top edge FG is not drawn.

In order to calculate intersections between scan lines and edges, we must avoid the brute-force technique of testing each polygon edge for intersection with each new scan line – it is inefficient and slow.

Clever Solution: if an edge intersects with a scan line, and the slope of the edge is m, then successive scan line intersections can be found from:

xi+1 = xi + 1/m

where i is the scan line count.

Given that 1/m = (x1 – x0)/(y1 – y0)

the floating-point arithmetic can be avoided by storing the numerator, comparing to the denominator, and incrementing x when it overflows.

Edge Coherence

1/m = 2/5

xmin = 3,

the sequence is:

numerator:

denominator:

Edge Coherence (cont.)

25

3516=

−−

=m

,...514

563,

543,

523 =

523

543

514

2

5

4 1

• Two sub-problems for polygon filling

• Find and sort intersections

• Fill the spans.

AET = active-edge table

store all edges intersected by a scan-line y sorted by xintersection. Each entry in AET contains the ymax coordinate of the edge, the x coordinate of the intersection point, and 1/m.

ET = Global Edge Table

Scan-Line Algorithm

• Catch the intersection information in a table

Edge Table (ET) with all edges sorted by ymin

Active Edge Table (AET) containing

The edges that intersect the current scan line

Their points of intersection

Sorted by x-coordinate, left to right

Catching Edge/Scan line intersections

An Example of the Active Edge Table

AB

C

F

E

D

25−

46

11 10

11 13 0

09 2

9 2

AET pointer

FA

EF

DE

CDAs each new scan-line y+1 is encounter, update AET:1). Remove edges not intersected by y+1 (ymax=y)2). Add edges intersected by y+1 (ymin=y+1)3). Calculate new x intersections.Ymax x m

1

Global edge table (ET): make the addition of edges to the AET efficient. It contains all edges sorted by their smaller y coordinate. The ET is typically built by using a bucket sort with as many buckets as there are scan-lines. Within each bucket, edges are kept in order of increasing x coordinate of the lower endpoint. Each entry in the ET contains the ymaxcoordinate of the edge, the x coordinate of the bottom endpoint (xmin), and 1/m.

An Example for the Global Edge Table

λ

λ

λ

λ

λ

λ

λ

λ

11 13 0 λ CD

9 2 0 λ FA

3 2 -5/2 AB

5 7 6/4 λ BC

9 2 -5/2 EF

11 7 6/4 λ DE

11

10

9

8

7

6

5

4

3

2

1

0

y co

ordi

nate

y max

x min

1/m

1. Set y to the smallest y coordinate that has an entry in the ET, that is, y for the first nonempty bucket.

2. Initialise the AET to be empty.

3. Repeat until the AET and ET are empty.

3.1 Move from ET bucket y to the AET those edges whose ymin= y (entering edges).

3.2 Remove from the AET those entries for which y=ymax (edges not involved in the next scan line), then sort the AET on x (made easier because ET is pre-sorted).

3.3 Fill in desired pixel values on scan line y by using pairs of x coordinates from the AET.

3.4 Increment y by 1 (to the coordinate of the next scan line).

3.5 For each non-vertical edge remaining in the AET, update x for the new y.

Scan-Line Algorithm (cont.)

Examples:

Solutions:

Line Clipping

• Line clipping against rectangles

The problem: Given a set of 2D lines or polygons and a window, clip the lines or polygons to their regions that are inside the window.

(x1, y1)

xmin xmax

ym

axy

min

(x0, y0)

Motivations

• Efficiency

• Display in portion of a screen

• Occlusions

Clip rectangle

In: 3 verticesOut: 6 verticesClip

Clip In: 1 polygonOut: 2 polygons

Clipping is tricky!

1. If x0 < xmin and x1 < xmin

or x0 > xmax and x1 > xmax

or y0 < ymin and y1 < ymin

or y0 > ymax and y1 > ymax

trivial rejection.

2. If xmin ≤ x ≤ xmax

and ymin ≤ y ≤ ymax

trivially accepted.

Simple Cases

xmin xmax

ym

ax ym

in

xmin xmax

ym

ax ym

in

• Region and outcodes

The Cohen-Sutherland Line-Clipping Algorithm

First bit: above top edge y > ymaxSecond bit: below bottom edge y < yminThird bit: to right of right edge x > xmaxFourth bit: to left of left edge x < xmin

• Checking for trivial acceptance or rejection using outcodes1). Each endpoint of a line segment is assigned an outcode;

2). If both 4-bit codes are zero, the line can be trivially accepted;

3). A logical and is performed on both outcodes;

4). If the result is nonzero, the line can be trivially rejected.

The C-S Line-Clipping Algorithm (cont.)

Steps for Cohen-Sutherland Algorithm

1. End-points pairs are checked for trivial acceptance or rejection using outcode;

2. If not trivially accepted or rejected, divide the line segment into two at a clip edge;

3. Iteratively clipped by test trivial-acceptance or trivial-rejection, and divided into two segments until completely inside or trivial-rejection.

AB

CD

E

F

G

H

I1001

0001

0101 0100

0000

1000 1010

0010

0110

Polygon Clipping

• Sutherland-Hodgeman algorithm (A divide-and-conquer strategy)

• Polygons can be clipped against each edge of the window one at a time. Edge intersections, if any, are easy to find since the X or Y coordinates are already known.

• Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges.

• Note that the number of vertices usually changes and will often increases.

Top Clip Boundary

Clipping A Polygon Step by Step

Right Clip Boundary

Bottom Clip Boundary

Left Clip Boundary

Sutherland-Hodgeman Algorithm

Note the difference between this strategy and the Cohen-Sutherland algorithm for clipping a line: the polygon clipper clips against each window edge in succession, whereas the line clipper is a recursive algorithm.

Given a polygon with n vertices, v1, v2,…, vn, the algorithm clips the polygon against a single, infinite clip edge and outputs another series of vertices defining the clipped polygon. In the next pass, the partially clipped polygon is then clipped against the second clip edge, and so on. Let’s considering the polygon edge from vertex vi to vertex vi+1.Assume that start point vi has been dealt with in the previous iteration, four cases will appear.

Sutherland-Hodgeman Algorithm(cont.)

Inside Outside

Clip Boundary

Polygon been clipped

vi

vi+1: output

Case 1

Inside Outside

Polygon been clipped vi

vi+1

Case 2

i: output

Inside Outside

Polygon been clipped

vi

vi+1

Case 3

(no output)

Inside Outside

vi

Case 4

vi+1: second output

i: first output

An Example for the Polygon Clipping

v1

v5

v2 v3

v4

Solution:

v1

v5

v2 v3

v4

As we said, the Sutherland-Hodgeman algorithm clip the polygon against one at a time. We start with the right edge of the clip rectangle. In order to clip the polygon against the line, each edge of the polygon have to be considered. Starting with the edge, represented by a pair of vertices, v5v1:

v1

v5

v1

Clipping edge Clipping edge Clipping edge

Solution (cont.):

v1

v5

v2 v3

v4

Now v1v2:

Clipping edge Clipping edge Clipping edge

v1

v2

v1

v2

Solution (cont.):

v1

v5

v2 v3

v4

Now v2v3:

Clipping edge Clipping edge Clipping edge

v1

v2v2 v3v2 v3

Solution (cont.):

v1

v5

v2 v3

v4

Now v3v4:

Clipping edge Clipping edge Clipping edge

v1

v2v2 v3v3

v4

i1

Solution (cont.):

v1

v5

v2 v3

v4

Now v4v5:

Clipping edge Clipping edge Clipping edge

v1

v2v2 v3

i1

v5

v4 i2

v5

After these, we have to clip the polygon against the other three edges of the window in a similar way.

Visible surface determination

Problem outline

• Given a set of 3D objects and a viewing specification, we wish to determine which lines or surfaces are visible, so that we do not needlessly calculate and draw surfces, which will not ultimately be seen by the viewer, or which might confuse the viewer.

Simple example…

With all lines drawn, it is not easy to determine the front and back of the box.

Is this the front face of the cube or the rear?

Simple example…

? ?

Approaches

• There are 2 fundamental approaches to the problem.

– Object space – Image space

Object space

• Object space algorithms do their work on the objects themselves before they are converted to pixels in the frame buffer. The resolution of the display device is irrelevant here as this calculation is done at the mathematical level of the objects

• Pseudo code…– for each object a in the scene

• determine which parts of object a are visible• draw these parts in the appropriate colour

• Involves comparing the polygons in object a to other polygons in a and to polygons in every other object in the scene.

Image space

• Image space algorithms do their work as the objects are being converted to pixels in the frame buffer. The resolution of the display device is important here as this is done on a pixel by pixel basis.

• Pseudo code…– for each pixel in the frame buffer

• determine which polygon is closest to the viewer at that pixel location

• colour the pixel with the colour of that polygon at that location

Algorithms

• Object space – Back-face culling

• Image space– z-buffer algorithm

• Hybrid– Depth-sort

Back-face culling

• Back-face culling (an object space algorithm) works on 'solid' objects which you are looking at from the outside. That is, the polygons of the surface of the object completely enclose the object.

Back-face culling

• Which way does a surface point?

• Vector mathematics defines the concept of a surface’s normal vector.

• A surface’s normal vector is simply an arrow that is perpendicular tothat surface.

• Every planar polygon has a surface normal, that is, a vector that is normal to the surface of the polygon.

– Actually every planar polygon has two normals.

• Given that this polygon is part of a 'solid' object we are interested in the normal that points OUT, rather than the normal that points in.

Back-face culling

• Consider the 2 faces of a cube and their normal vectors.

• Vectors N1 and N2 are the normals to surfaces 1 and 2 respectively.

• Vector L points from surface 1 to the viewpoint.

• Depending on the angle between L, and N1 & N2, they may or may not be visible to the viewer.

N2

N1L

surface1surface2

Back-face culling

• If we define θ to be the angle between L and N.

• Then a surface is visible from the position given by Lif:

• Can calculate Cos θ using the formula:

°≤≤ 900 θ 1cos0 ≤≤ θor

θcos... NLNL =

Back-face culling

• Determining N:• Can use cross product to calculate N

• N = A × B– NB. Vertices are given a counterclockwise order when

looking at visible side.

A

B

E

DN

N

Back-face culling

• Limitations:– It can only be used on solid

objects…• Remove the top or front face of the

cube, and suddenly we can see inside, and our algorithm falls over.

– It works fine for convex polyhedra but not necessarily for concave polyhedra.

• example of a partially hidden face, that will not be eliminated by Back-face removal.

Z-buffer algorithm

• The z-buffer or depth-buffer is one of the simplest visible-surface algorithms.

• Z-buffering is an image-space algorithm, an involves the use of a (surprisingly) z-buffer, to store the depth, or z-value of each pixel.

• All of the elements of the z-buffer are initially set to be 'very far away.' Whenever a pixel colour is to be changed the depth of this new colour is compared to the current depth in the z-buffer. If this colour is 'closer' than the previous colour the pixel is given the new colour, and the z-buffer entry for that pixel is updated as well. Otherwise the pixel retains the old colour, and the z-buffer retains its old value.

Z-buffer algorithm

• Pseudo code…for each polygon for each pixel p in the polygon's projection

{

pz = polygon's normalized z-value at (x, y);

//z ranges from -1 to 0

if (pz > zBuffer[x, y]) // closer to the camera { zBuffer[x, y] = pz;

framebuffer[x, y] = colour of pixel p;

}

}

Depth-sort algorithm

• a.k.a. The Painter's Algorithm• The idea here is to go back to front drawing all the

objects into the frame buffer with nearer objects being drawn over top of objects that are further away.

• Simple algorithm:– Sort all polygons based on their farthest z coordinate – Resolve ambiguities – draw the polygons in order from back to front

• This algorithm would be very simple if the z coordinates of the polygons were guaranteed never to overlap. Unfortunately that is usually not the case, which means that step 2 can be somewhat complex.

Depth-sort algorithm

• First must determine z-extent for each polygon

z-extent

zmax

z

x

Depth-sort algorithm

• Ambiguities arise when the z-extents of twosurfaces overlap.

z

x

surface 1

surface 2

Depth-sort algorithm

x

y

Depth-sort algorithm

• Any polygons whose z extents overlap must be tested against each other.

• We start with the furthest polygon and call it P. Polygon P must be compared with every polygon Q whose z extent overlaps P's z extent. 5 comparisons are made. If any comparison is true then P can be written before Q. If at least one comparison is true for each of the Qs then P is drawn and the next polygon from the back is chosen as the new P.

Depth-sort algorithm

1. do P and Q's x-extents not overlap. 2. do P and Q's y-extents not overlap. 3. is P entirely on the opposite side of Q's plane from the viewport. 4. is Q entirely on the same side of P's plane as the viewport. 5. do the projections of P and Q onto the (x,y) plane not overlap.

• If all 5 tests fail we quickly check to see if switching P and Q will work. Tests 1, 2, and 5 do not differentiate between P and Q but 3 and 4 do. So we rewrite 3 and 4

3’. is Q entirely on the opposite side of P's plane from the viewport.4’. is P entirely on the same side of Q's plane as the viewport.

Depth-sort algorithm

z

x

P

Qif they do, test fails

x - extents not overlap?

Depth-sort algorithm

z

y

P

Qif they do, test fails

y - extents not overlap?

Depth-sort algorithm

is P entirely on the opposite side of Q's plane from the viewport.

z

x

P

Q

Test is true…

Depth-sort algorithm

is Q entirely on the same side of P's plane asthe viewport.

z

xP

QTest is true…

Depth-sort algorithm

do the projections of P and Q onto the (x,y) plane not overlap.

z

x

P

Q

y

x

PQ

hole in P

Test is true…

Depth-sort algorithm

• If all tests fail…

– … then reverse P and Q in the list of surfacessorted by Zmax

– set a flag to say that the test has beenperformed once.

– If the tests fail a second time, then it isnecessary to split the surfaces and repeat thealgorithm on the 4 surfaces

Depth-sort algorithm

z

x

P1Q2

P2Q1

• End up drawing Q2,P1,P2,Q1

Surface Modelling

Many real-world objects are inherently smooth, therefore need infinitely many points to model it. This is not feasible for a computer with finite storage. More often we merely approximate the object with pieces of planes, spheres, or other shapes that are easy to describe mathematically. We will introduce two most common representations for 3D surfaces: polygon mesh surfaces and parametric surfaces. We will also discuss parametric curves since parametric surfaces are a generalisation of the curves.

Modelling the Elegant Teapot

Polygon Meshes

VERTEX TABLEV1: x1, y1, z1

V2: x2, y2, z2

V3: x3, y3, z3

V4: x4, y4, z4

V5: x5, y5, z5

EDGE TABLEE1: V1, V2

E2: V2, V3

E3: V3, V1

E4: V3, V4

E5: V4, V5

E6: V5, V1

POLYGON TABLEP1: V1, V2 , V3

P2: V1 , V3 , V4 , V5

POLYGON TABLEP1: E1, E2 , E3

P2: E3 , E4 , E5 , E6

or

E1

E2

E3

E5E4

E6

V2

V3

V4

V5

V1

P1 P2

• The geometry can be stored as three tables: a vertex table, an edge table, and a polygon table. Each entry in the vertex table is a list of coordinates defining that point. Each entry in the edge table consists of a pointer to each endpoint of that edge. And the entries in the polygon table define a polygon by providing pointers to the edges that make up the polygon.

• We can eliminate the edge table by letting the polygon table reference the vertices directly, but we can run into problems, such as drawing some edges twice, because we don't realise that we have visited the same set of points before, in a different polygon. We could go even further and eliminate the vertex table by listing all the coordinates explicitly in the polygon table, but this wastes space because the same points appear in the polygon table several times.

Polygon Meshes

Representing Polygon Meshes

1). The explicit way: just list 3D vertices of each polygon in a certain order. The problems are, firstly it represents same vertex many times and secondly, no explicit representation of shared edges and vertices

2). Pointer to a vertex list: store all vertices once into a numbered list, and represent each polygon by its vertices. It saves space (vertex only listed once) but still has no explicit representation of shared edges and vertices

3). Explicit edges: list all edges that belong to a polygon, and for each edge list the vertices that define it along with the polygons of which it is a member.

)),,(),...,,,(),,,(( 222111 nnn zyxzyxzyxP =

)5,4,3,1(=P

),,( 121 PVVE =

Types of Curves

1). Explicit: In the Cartesian plane, an explicit equation of a planar curve is given by y = f(x). The difficulties with this approach are that (1) it is impossible to get multiple values of y for a single x, so curves such as circles and ellipses must be represented by multiple curve segments; and (2) describing curves with vertical tangents is difficult and numerically unstable.

2). Implicit:f(x, y) = 0 Ax+By+C =0This method has difficulties on determining tangent continuity of two given curves which is crucial in many applications.(Circle can be defined as: x2+y2=1, but what about a half circle?)

Types of Curves

3). Parametric Curves:The cubic polynomials that defines a curve segment Q(t)= [x(t) y(t)]T are of the form

Written in matrix form, it becomes

Q(t)= [x(t) y(t)] = T⋅ C

where

yyyy

xxxx

dtctbtatydtctbtatx

+++=

+++=23

23

)(

)(

=

x

y

y

y

y

x

x

x

dcba

dcba

C [ ]123 tttT =

Parametric Curves

The equations that describe a parametric curve depend on a variable t that is not explicitly part of the geometry.

By sweeping through t, in our case 0 ≤ t ≤ 1, it is possible to evaluate the equations and determine the x and y values for points on the curve.

t = 0.0

t = 1.0

t

Parameter space Object space

Continuity

The derivative of Q(t) is the parametric tangent vector of the curve. Applying the definition to the curve’s equation, we have

Often we will want to represent a curve as a series of curves pieced together. But if we will want these curves to fit together reasonably ...continuity!

[ ]yyyxxx ctbtactbta

CttCTdtd

dtdy

dtdxtQtQ

dtd

++++=

⋅=⋅=

==

2323

]0123[

)(')(

22

2

Continuity

Two curve segments join together: G0

geometric continuity.

The directions of the tangent vectors (not necessarily the magnitudes) are equal: G1

geometric continuity.

Both the directions and magnitudes are equal: C1

parametric continuity.

Second-order parametric continuity: C2 parametric continuity.

Note: For two curves to join smoothly, we require only that their tangent-vector directions match; we do not require that their magnitudes match.

Parametric Cubic Curves

To join multiple curves into one curve smoothly, we need to specify the positions and their tangent-vectors of both ends, i.e. the continuity requirements. This gives us four constraints, and so the four knowns can be used to solve the four unknowns, i.e. coefficients, in a cubic polynomial.

p1

p2∇p1

∇p2

Specifying Curves

• Interpolating splinecurve passes through the control points knots (Cardinal spline, Catmull-Rom spline)

• Control Pointscontrol points that lie on the curve

• Knotsa set of points that influence the curve's shape

• Approximating splinecontrol points merely influence shape (Hermite Curves, B-spline)

Examples of Cubic Curves

• Hermite• Defined by two end points + two endpoint tangent vectors.

• Bézier• Defined by two endpoints + two other points that control the

endpoints tangent vectors.

Hermite Curves

Depending on which four knowns we have, parametric curves can be divided into different groups. The simplest of the cubic curves, the Hermite form, is defined by two endpoints and the tangent vectors at these endpoints.

Hermite Curves

dtdy

dtdx

dtdy

dtdx

yxyx

22

11

22

11

(x1,y1)

(x2,y2)∇p1

∇p2t=1

t=0

Hermite Curves (cont.)

(x1,y1)

(x2,y2)∇p1

∇p2t=1

t=0

ytyyyyt

xtxxxxt

ddtctbtatyddtctbtatx

=+++=

=+++=

==

==

023

01

023

01

|)(|)(

|)(|)(

yyyyt

xxxxt

dcbatydcbatx

+++=+++=

=

=

12

12

|)(|)(

ytyyyt

xtxxxt

cctbtadtdy

cctbtadtdx

=++=

=++=

==

==

02

01

02

01

|)23(|)(

|)23(|)(

yyyt

xxxt

cbadt

dy

cbadt

dx

++=

++=

=

=

23|

23|

12

12

Hermite Curves (cont.)

=

yx

yx

yx

yx

ddccbbaa

dtdy

dtdx

dtdy

dtdx

yxyx

0123010011111000

22

11

22

11

=

−−−

yx

yx

yx

yx

GM

ddccbbaa

dtdy

dtdx

dtdy

dtdx

yxyx

Hermite

Hermite

22

11

22

11

000101001233

1122

Geometry VectorBasis Matrix

Bézier Curves

P1(x1,y1,z1)P4(x4,y4,z4)

∇p1

∇p4

t=1

t=0

P2(x2,y2,z2)

P3(x3,y3,z3)

A cubic Bézier curve indirectly specifies the endpoint tangent-vector by specifying two intermediate points that are not on the curve.

BezierHB

Hermite

GMG

Hermite

yxyxyxyx

dtdy

dtdx

dtdy

dtdx

yxyx

G

−−

=

=

44

33

22

11

44

11

44

11

3300003310000001

)3()()( 344121 =−=∇−=∇ kPPkPPPkP

Bézier Curves (cont.)

−−

−−−

=⋅=

−−−

=

3300003310000001

000101001233

1122

000101001233

1122

22

11

22

11

HBHB

GM

yx

yx

yx

yx

MMM

dtdy

dtdx

dtdy

dtdx

yxyx

ddccbbaa

Hermite

Hermite

Bézier Curves (cont.)

−−

−−

=

44

33

22

11

0001003303631331

yxyxyxyx

ddccbbaa

BezierM

yx

yx

yx

yx

A Bézier Curve

For example, if we are given four control points (0,0), (0.2,0.8), (0.9, 0.1), (1,1). Then we have

And the parametric equation will be

At any point on this curve, the tangent vector to the curve is

=

−−

−−

=

004.26.05.45.1

1.31.1

111.09.08.02.0

00

0001003303631331

yx

yx

yx

yx

ddccbbaa

ttttyttttx

4.25.41.3)(6.05.11.1)(

23

23

+−+=

++−=

00|])(,)([|)(' tttt dt

tdydt

tdxtQ == =

Parametric Bicubic Surfaces

The equations that describe a parametric curve depend on a variable t that is not explicitly part of the geometry.

By sweeping through t, in our case 0 ≤ t ≤ 1, it is possible to evaluate the equations and determine the x and y values for points on the curve.

)()(

tgytfx

==

t = 0.0

t = 1.0

t

Parameter space Object space

Parametric Bicubic Surfaces

Parametric Bicubic Surfaces are a generalisation of parametric cubic curves

1010)]()()([)( ≤≤≤≤= vuu, v, zu, v, yu, vx u, vP• If one parameter, say v, is held at a constant value then the above willrepresent a curve.

• The surface is then generated by sweeping all points on the boundary curveP(u,0) (say) through cubic trajectories, defined using the parameter v, to theboundary curve P(u,1).

v=0u=0

u=1

v=1

The representation of the bicubic surface patch can be illustrated by considering the Bézier Surface Patch. The edge P(0,v) of a Bezier patch is defined by giving four control points P00, P01, P02 and P03. Similarly the opposite edge P(1,v) can be represented by a Bezier curve with four control points. The surface patch is generated by sweeping the curve P(0,v) through a cubic trajectory in the parameter u to P(1,v). To define this trajectory we need four control points, hence the Bezier surface patch requires a mesh of 4×4 control points as illustrated below.

Bézier Surface Patch

Further reading…

• Chapter 11 of Foley and VanDam– Representing Curves and Surfaces