cgpage: 1 東吳資訊管理 江清水 computer game : basic 1.computer graphics and its application...

68
CG Page: 1 東東東東東東 東東東 Computer Game : Basic 1. Computer Graphics and Its application 2. 2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2 Object transformation and Coordinates System transformation 3.3 2D Transformation -- Scaling, Translation, Rotation. 3.4 Homogeneous Coordinates 3.5 Composite 2D Transformation 3.6 Other Transformation -- Reflection (Mirror) and Shearing (Skip) 4. 2D Viewing Operations 4.1 Window-to-Viewport Mapping 4.2 2D Clipping 5. 3D Coordinate System 5.1 Coordinates System -- Right-handed and Left-Hand Coordinate system (a) 5.2 3D gemoetry -- points and polyhedra (a) 5.3 3D Transformation -- Scaling, Translation and Rotation. 5.4 Composite 3D Transformation -- Rotation about arbitrary Axis 5.5 Other Transformation -- Reflection and Shearing 6. 3D Viewing Operations 6.1 Projections -- Orthgraphic Projections and Perspective Projections(a 6.2 View Transformation 6.3 View Volume and Z-clipping 7. Visual Realism (Skip) 7.1 Rendering techniques 7.2 Hidden line removal algorithm 7.3 z-buffer algorithm 7.4 illumination and shading 7.5 Ray-tracing algorithm 7.6 Color and light

Upload: cecilia-wilkinson

Post on 20-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 1 東吳資訊管理 江清水

Computer Game : Basic

1. Computer Graphics and Its application2. 2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2 Object transformation and Coordinates System transformation 3.3 2D Transformation -- Scaling, Translation, Rotation. 3.4 Homogeneous Coordinates 3.5 Composite 2D Transformation

3.6 Other Transformation -- Reflection (Mirror) and Shearing (Skip)

4. 2D Viewing Operations 4.1 Window-to-Viewport Mapping 4.2 2D Clipping

5. 3D Coordinate System 5.1 Coordinates System -- Right-handed and Left-Hand Coordinate system (a) 5.2 3D gemoetry -- points and polyhedra (a) 5.3 3D Transformation -- Scaling, Translation and Rotation. 5.4 Composite 3D Transformation -- Rotation about arbitrary Axis

5.5 Other Transformation -- Reflection and Shearing

6. 3D Viewing Operations 6.1 Projections -- Orthgraphic Projections and Perspective Projections(a 6.2 View Transformation 6.3 View Volume and Z-clipping

7. Visual Realism (Skip) 7.1 Rendering techniques 7.2 Hidden line removal algorithm 7.3 z-buffer algorithm 7.4 illumination and shading 7.5 Ray-tracing algorithm 7.6 Color and light

Page 2: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 2 東吳資訊管理 江清水

Computer Graphics vs. Pattern Recognition

3D arrows with 3D box

Computer

GraphicsPattern

Recognition

Page 3: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 3 東吳資訊管理 江清水

Some of today's most popular applications in computer graphics are :

• Computer Game

• Presentation Graphics

• Electronic design and circuit layout

• Mechanical design and drafting

• Process Control

• Data Analysis– 2D contour; 3D continuous mesh surface plot

– air flow and fluid flow

– 4D image; structural analysis of any properties

• Simulation

• Chemical and molecular analysis

• User Interface

• Animation

• Art

• Geometric and solid modeling

Computer Graphics Application

Page 4: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 4 東吳資訊管理 江清水

2D Geometry - points and polygons

X

Y

A point in 2D is represented by two real numbers (X,Y)

A line segment is represented by its two end points (X1,Y1) (X2,Y2), or a 2x2 matrix

[ ]X1 Y1

X2 Y2

X

Y

Page 5: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 5 東吳資訊管理 江清水

A polygon is represented by an list of points (X1,Y1), (X2,Y2), ..... (Xn,Yn) or a

n x 2 matrix [ ]

For example, a triangle is represent by

[ ]

X1 Y1

: :

Xn Yn

X1 Y1

X2 Y2

X3 Y3

X

Y

Page 6: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 6 東吳資訊管理 江清水

Object transformation and Coordinates System transformation

2

1

1 2 8

Y

X

(2,4)

(6,2)(2,2)

Object transformation is different from

the coordinates system transformation

Page 7: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 7 東吳資訊管理 江清水

Translation (2,1)

Rotation (/2)counter clock wiseabout the origin

X

Y

Y

X

Object Transformation

Scale (1/2)about the origin

Y

X

Page 8: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 8 東吳資訊管理 江清水

Y

X

Y

X

Coordinates system transformationY

X1 2 3

Translation (2,1)

Rotation (/2)counter clock wiseabout the origin

Scale (1/2)about the origin

If not described explicitly, transformation always means object transformation

Page 9: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 9 東吳資訊管理 江清水

There are 3 "basic" transformations:

(1) Translation

(2) Scaling

(3) Rotation

A series of transformations can be combined (concatenated) into one.

(1) Translation : T(Tx, Ty)

X

Y

(x,y)

(x',y')

2D Translations

Tx

Ty

Page 10: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 10 東吳資訊管理 江清水

(2) Scaling (about the origin) : S(Sx, Sy)

• What about "Mirror Images"?

• How do we avoid distortion?

• What happens when the scale factor equal to zero?

X

Y

(x,y)

Y

X

(x',y')

S(-2, -1)

Page 11: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 11 東吳資訊管理 江清水

(3) Rotation (about the origin

counterclockwise) :

x = R cos y = R sinx' = R cos( R(cos cos sin

sin y' = R sin(R(sin cos cos

sin

So, x ' = xcos - ysiny ' = xsin + ycos

X

Y

(x,y)

(x',y')

Page 12: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 12 東吳資訊管理 江清水

Concatenation -- A series of transformations can be combined (concatenated) into one.

Example: scaling about arbitrary point.

1. Translate so that point (a,b)

becomes the temporary origin:

x1 = x - a y1 = y - b

2. Scale to the correct size:

x2 = Sx*x1 y2 = Sy*y1

3. Translate again to restore the coordinates of (a,b):

x3 = x2 + a y3 = y2 + b

(a,b)X

Y(x,y)

Page 13: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 13 東吳資訊管理 江清水

Algebraic representations for transformations are very limited:

(1) A special procedure is need for

each basic transformation and

other known concatenated forms.

(2) It is difficult to provide general

transformation capabilities in

algebraic form in the computer.

(3) How can one find the inverse of

a concatenated equation (to

restore the original position, for

example)?

Page 14: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 14 東吳資訊管理 江清水

Let the point (x,y) be a row vector

[x y]:x' = ax + by and y' = cx + dy

can be expressed in the matrix equation:

[x' y'] = [x y] * [ ]2x2

Let P' = [x' y'] and P = [x y], this becomes the matrix equation:

P' = P * T where P' =[x' y'] ,

P = [x y] , T =

Consider three basic transformations, can we find a "T" for each?

• What about translations?

X'=X+Tx, Y'=Y+Ty

No, P' =[x' y']=[x y] [ ]+[Tx Ty]=P*T+Q

• What about scaling? X'=Sx*X, Y'=Sy*Y

Yes, P' =[x' y']=[x y] [ ] =P*T

• What about rotation?

x'=xcos-ysin, y'=xsin+ycos

Yes, P' =[x' y']=[x y] [ ]

a c

b d[ ]

1 0

0 1

Sx 0

0 Sy

cos sin-sin cos

Page 15: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 15 東吳資訊管理 江清水

Homogeneous Coordinates

Let the point (x,y) be a row vector [x y 1]:

x' = ax + by + e and y' = cx + dy + f can be expressed in the matrix equation:

[x' y' 1] = [x y 1] [ ]3x3

This becomes the matrix equation P' = P * T

where P'=[x' y' 1], P= [x y 1],

T= [ ]Now, can we find a "T" for each transformation?

• What about translations? Yes, T= [ ]• What about scaling? Yes, T= [ ]• What about rotation? Yes, T= [ ]

Page 16: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 16 東吳資訊管理 江清水

To check, what should the matrix be if we:

(1) translate by (0,0) ?

P'=P*[ ]=P*[ ]=P

(2) scale by (1,1) ?

P'=P*[ ]=P*[ ]=P

(3) rotate by 0 o ?

P'=P*[ ]=P*[ ]=P

(4) rotate by 90o ?

P'=[x' y' 1]=[x y 1] [ ] =[1 0 1]*[ ]=[0 1 1]

Draw the pictures and find the new vertices of the triangle for the following transformations (See the figure below) :

(x, y) = (1, 0)

(x', y') = (0, 1)

X

Y(2,2)

(2,1)

(4,1)

A

BC

Page 17: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 17 東吳資訊管理 江清水

(5) translate A by (2,1).

(6) translate A by (-2,1).

(7) scale A by (2,1).

(8) scale A by (-1,1).

X

Y

(2,2)

(2,1)(4,1)

A

BC

X

Y

(2,2)

(2,1)(4,1)

A

BC

X

Y

(2,2)

(2,1)(4,1)

A

BC

X

Y

(2,2)

(2,1)(4,1)

A

BC

Page 18: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 18 東吳資訊管理 江清水

(9) scale A by (1,0).

(10) scale A by (0,0).

(11) rotate A by 0o.

(12) rotate A by 90o .

X

Y

(2,2)

(2,1)(4,1)

A

BC

X

Y

(2,2)

(2,1)(4,1)

A

BC

X

Y

(2,2)

(2,1)(4,1)

A

BC

X

Y

(2,2)

(2,1)(4,1)

A

BC

Page 19: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 19 東吳資訊管理 江清水

Concatenation is easy now. Consider

our scaling example:Example: We want to reduce the size or the box,

without changing the position of point (a, b)

Functional form and matrix form for each transformations:

(1) translate: T(-a,-b)

(2) scale : S(Sx,Sy)

(3) translate: T(a,b)

(a,b)

Y(x, y)

X

Composite 2D Transformation

Page 20: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 20 東吳資訊管理 江清水

Combining the matrix equations:

P3 =P2* T (-a,-b)=(P1*S(Sx, Sy) )*T(a, b)

=P*T(-a, -b) *S(Sx, Sy)*T(a, b)

Multiply the matrices :

Now, the matrix equation is :

or, in algebraic form :

Thus, a single 3x3 matrix can represent any combination of basic transformations in a simple form.

Page 21: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 21 東吳資訊管理 江清水

We can define a window as a rectangular region of the world coordinate space, and the viewport as a rectangular region of the device coordinate system.

We can define a window as a rectangular region of the world coordinate space, and the viewport as a rectangular region of the device coordinate system.

Window-to-viewport Mapping

(0,1/2)

(-1,1)

(1,1)

(0,0)

(1,1)

WINDOW VIEWPORT (Normalize)

(3/4,1)

(1/4,0)

(Distortion)

(0,1/2)

(-1,1)

(1,1)

WINDOW

Page 22: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 22 東吳資訊管理 江清水

The problem is to "map" coordinates from the window units into the viewport units.

Derive the mapping from (xw,yw) , a coordinate in the window, to (xv, yv), the corresponding point in the viewport:

Remove window coordinate system

x1 =

y1 =

The problem is to "map" coordinates from the window units into the viewport units.

Derive the mapping from (xw,yw) , a coordinate in the window, to (xv, yv), the corresponding point in the viewport:

Remove window coordinate system

x1 =

y1 =

(cxw,cyw) (cxv,cyv)

sxw

sywsyv

window viewport

sxv

(xw,yw)

(xv,yv)

(cxw,cyw)

sxw

syw

window

(x1,y1)

Page 23: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 23 東吳資訊管理 江清水

(cxv,cyv)

syv

viewport

sxv

(x2,y2)

Convert units

x2 =

y2 =

Remove to viewport center

xv =

yv =

so xv =

yv =

(1) What happens if the ratios sxv/sxw

and syv/syw are not equal?

(2) If you want a set of data in the

window, how do you decide the

size of the window?

Page 24: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 24 東吳資訊管理 江清水

(3) Can the window-to-viewport mappintg represented in matrix form?

Page 25: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 25 東吳資訊管理 江清水

Homework:

Find the transformation matrix

that will map points contained in

a window whose lower left corner

is at (2,2) and upper right corner

is at (6,5) onto a viewport that

has a lower left corner at (1/2, 1/2)

and upper right corner at (1,1).

Page 26: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 26 東吳資訊管理 江清水

(4) Derive the window to viewport mapping for viewports defined on devices which have the origin at the upper left of the screen, with the horizontal coordinate increasing right, and the vertical coordinate increasing down.

h

(0,0)window

x

y

v

(100,100)

viewport

(80,80)

(100,100)

Page 27: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 27 東吳資訊管理 江清水

The process of clipping decides which

part, if any, of a primitive lies inside the window.

The algorithms used for line clipping can be divided into two parts: (1) Check all the line segments and separate those that intersect the window boundary. (The curve is represented by a sequence of line segment) (2) Clip the line segments obtained from step 1 by calculating their intersections with the window boundaries.

Clipping

2D Clipping

Page 28: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 28 東吳資訊管理 江清水

(1) How can we sure that a line segment is totally inside the window (such as A) ?

Both endpoints of the segment fall within the boundaries.

(2) How can we sure that a line segment is totally outside the window (such as E,F,G)?

Let two endpoints are (x1,y1),(x2,y2) If ( max{x1,x2} < xmin or min{x1,x2} > xmax

or max{y1,y2} < ymin or min{y1,y2} > ymax) then the line segment is totally outside the window.We introduce an efficient procedure, the

Cohen-Sutherland algorithm, to check the intersection property and further clip the line segment if necessary.

Xmin Xmax

Ymin

Ymax

A

BC

D

E F G

Page 29: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 29 東吳資訊管理 江清水

Cohen-Sutherland Algorithm

The 2D plane is divided into 9 regions. Each region is designated by a 4-bit (base 2) integer code. Each bit of the code is set to 1 (true) or 0 (false), starting with the left most one. The bits are assigned by the follow rule:

bit 1 = 1 if the region is above the window.

bit 2 = 1 if the region is below the window.

bit 3 = 1 if the region is on the right of the window.

bit 4 = 1 if the region is on the left of the window.

Xmin Xmax

Ymin

Ymax

(1001)

(0001) (0000) (0010)

(0100) (0110)

(1000) (1010)

(0001)

Page 30: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 30 東吳資訊管理 江清水

(1) Given a point (x,y), we would like to assign a "region code" to it by using Cohen-Suterlandalgorithm. In C this can be coded:

code = 0

if (x < xmin) code = 1

if (x > xmax) code = 2

if (y < ymin) code += 4

if (y > ymax) code += 8

(2) According to the C code in (1), what code will be assigned if the point is on the boundary of the window?

(0000) 0

(3) How about the point on the line Xmax and not on the boundary of the window?

(0100) or (1000) depending on y

Page 31: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 31 東吳資訊管理 江清水

(4) Draw precise picture for the partition of 2D plane according to the C code in (1). (Using solid line and dotted line)

Let's consider the line clipping now.

At first, the visibility of the line

segment is determined as follows:

(1) Visible -> Both endoint codes are

(0000).

Page 32: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 32 東吳資訊管理 江清水

(2) Invisible -> Bitwise

logical AND of the

endpoint codes is not

(0000).

(3) Indeterminate -> Bitwise logical

AND of the endpoint codes is

(0000), but at least one of the

endpoint does not have a

(0000) code.

On the third situation, the line endpoints are "pushed" towards the boundary line until the first or the second situation occurs.

(0001) (0010)

(0000)

Ymax

Ymin

Xmin Xmax

Page 33: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 33 東吳資訊管理 江清水

The algorithm steps:1. Calculate IC1 and IC2, the code of the two

endpoints.

2. If (IC1 + IC2 .eq. 0) then "Visible".

3. If ( (IC1 .AND. IC2) .NE. 0) then "Invisible".

4. If ( IC1 .EQ. 0) then

{ Swap(IC1, IC2);

Swap(X1,X2);

Swap(Y1,Y2); }

// At lease one point is outside the

// viewport. Insure that point #1 is

// one of the outside point.

5. Push endpoint #1 towards Xmin, Xmax, Ymin, or Ymax, depending on which region endpoint #1 occupies (via IC1).

bit 1 = 1 toward Ymax

bit 2 = 1 toward Ymin

bit 3 = 1 toward Xmax

bit 4 = 1 toward Xmin

If endpoint #1 towards Ymax , then

{ Ymax = Y = X1 + t(Y2 - Y1), find t and calculate

X = X1 + t(X2 - X1) }

6. Go To Step #1 above.

Page 34: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 34 東吳資訊管理 江清水

Example: Let Xmin = 2, Xmax = 8, Ymin = 2 and Ymax = 8, check the visibility of the following segments using the Cohen-Sutherland Algorithm and, if necessary, clip them against the appropriate window boundaries.

Line AB: A(3,10) B(6,12)

Line CD: C(4,1) D(10,6)

Xmin=2 Xmax=8

Ymin=2

Ymax =8A(3,10)

B(6,12)

C(4,1)

D(10,6)

Page 35: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 35 東吳資訊管理 江清水

Example: Let Xmin = 2, Xmax = 8, Ymin = 2 and Ymax = 8, check the visibility of the following segments using the Cohen-Sutherland Algorithm.

Line AB: A(3,10) B(6,12)

Line CD: C(4,1) D(10,6)

Page 36: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 36 東吳資訊管理 江清水

Page 37: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 37 東吳資訊管理 江清水

How will the algorithm handle these examples?

(A) (B)

(C) (D)

Page 38: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 38 東吳資訊管理 江清水

3D Coordinates System

Right-handed Coordinates System

X

Y

Y

X

X

Y

Left-handed Coordinates System

X

YX

Y

We will use vight-handed Coordinates

System in our class.

Y

X

Page 39: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 39 東吳資訊管理 江清水

5.2 3D Geometry

We can use the way to represent 3D pointsand polyhedras as the way to represent 2Dpoints and polygons. For example:

Y

X

Z

(0,2,0)

(0,0,0)

(3,0,0)(0,0,1)

[ ]0 0 03 0 00 2 00 0 1

represents a tetrahedron

In homogenlous coordinates system

represents the same tetrahedron

Page 40: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 40 東吳資訊管理 江清水

3D Transformations

Let's discuss 3D transformations, going directly to matrix and functional forms:

(1) Translation:

[x' y' z' 1] = [x y z 1] * [ ]

(2) Scaling:

[x' y' z' 1] = [x y z 1] * [ ]

(3) Rotation:• Rotating about X(Y or Z) does not change

X's.

• The value of Y' and Z' does not depend on X.

• Row 4 and column 4 are always the same.

• The problem is to locate sin, -sin and cos.

Page 41: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 41 東吳資訊管理 江清水

(a) Rotate about Z:

(b) Rotate about Y:

X

Y

Z

X

Y

Z

[x' y' z' 1] = [x y z 1] *[ ]cos sin 0 0

-sin cos 0 0

0 0 1 0

0 0 0 1

[y' z' 1] = [y z 1] *[ ]cos sin 0

-sin cos 0

0 0 1

Z

X

Eye

Page 42: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 42 東吳資訊管理 江清水

(c) Rotate about X:

[x' y' z' 1] = [x y z 1] *[ ]1 0 0 0

0 cos sin 0

0 -sin cos 0

0 0 0 1

X

Y

Z

[x' y' z' 1] = [x y z 1] *[ ]cos 0 -sin 0

0 1 0 0

sin 0 cos 0

0 0 0 1

[x' z' 1] = [x z 1] *[ ]cos -sin 0

sincos 0

0 0 1

[z' x' 1] = [z x 1] *[ ]cos sin 0

-sincos 0

0 0 1

[y' z' 1] = [y z 1] *[ ]cos sin 0

-sin cos 0

0 0 1

Page 43: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 43 東吳資訊管理 江清水

Homework: Rotate the block counterwisely degree about X, Y, Z axis respectively. Find new coordinate for each vertex.

Z

X

Y

Z

X

Y

Z

X

Y

Z

X

Y

RotateaboutY

RotateaboutX

RotateaboutZ

(0,3,0)

(0,0,2)

(1,0,0)

Page 44: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 44 東吳資訊管理 江清水

Different eye position has different view.

Projection

Page 45: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 45 東吳資訊管理 江清水

After the projection, the distance between your eyes and the object vertices is unknown.

With the help of hidden-line removalalgorithm, you can know which vertex is closer to you.

Page 46: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 46 東吳資訊管理 江清水

Orthgraphic Parallel Projections vs. Perspective Projections

Orthgraphic Parallel Projections: The center of projection is located at infinity and the projectors are prependicular to the projection plane .

Perspective projections: The center of projection is located at a finite distance from the projection plane.

Page 47: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 47 東吳資訊管理 江清水

Orthgraphic parallel Projection :

[x,y,z]

[x,y,0]

Y

X

Z

Projection Plane : XY-Plane (Z=0)

Projector : parallel to Z axis

The Problem for finding projection point

are simple:

project to [ x y 0 ] [ x y z ] will

Page 48: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 48 東吳資訊管理 江清水

Perspective Projection :

How to get a picture?

(1) Freeze the scene.

(2) Freeze you head (eyes).

Perspective:

Page 49: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 49 東吳資訊管理 江清水

What information we need to project

a picture into a projection plane via perspective projection? (1) eye position (2) object position (3) picture planeAnything else? (4) Up direction

Page 50: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 50 東吳資訊管理 江清水

Z

X

Y

Up1 = [ ]010 Up2 = [ ] Up3 = [ ]

100

110

Page 51: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 51 東吳資訊管理 江清水

Consider the eye coordinate system:

Projection plane: XY-plane (Z=0)

Eye position: [0 0 d]

Projector: the line from [x y z] to a

given eye position.

The problem for finding projection point becomes:

Find the intersection point for

projcetion plane and projector.

Xe

Ye

Ze

[x y z]

[xp yp 0]

[0 0 d] = Eye Position

Page 52: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 52 東吳資訊管理 江清水

(1) Look alone the -Xe direction:

(2) Look alone the -Ye direction:

(x,y,z)Ye

Ze

yp

Xe

Ye

Ze

[x y z][xp yp 0]

[0 0 d] = Eye Position

yp : y = d : d-z

yp= (d/(d-z)) * y

Xe

Ye

Ze

[x y z][xp yp 0]

[0 0 d] = Eye Position

xp : x = d : d-z

xp= (d/(d-z)) * x

y

d -z

(x,y,z)

Ze

Xe

xp-z

x

d

Page 53: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 53 東吳資訊管理 江清水

(3) Represented in matrix form :

In homogeneous coordinates system, the

points [ x y z 1] and [wx wy wz w] (w0)

represents the same points. So, the point

xp = (d/(d-z)) * x

yp= (d/(d-z)) * y

[xp yp 0 1]=[x y z 1][ ]d/(d-z) 0 0 0

0 d/(d-z) 0 0

0 0 0 0

0 0 0 1

[xp yp 0 1] = [dx/(d-z) dy/(d-z) 0 1]

= [x y 0 1-(z/d)]

=[x y z 1][ ] 1 0 0 0

0 1 0 0

0 0 0 -1/d

0 0 0 1

Page 54: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 54 東吳資訊管理 江清水

(a) How to draw a 3D line into the

picture plane ?

(b) What happens when Z

(c) What about d

(4) Questions :

X

Y

Z

[xt yt zt]

[xf yf zf]

[x'f y'f 0]

[x't y't 0]

Page 55: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 55 東吳資訊管理 江清水

Exercise:

(1) Draw The rectangle whose vertices are

P=(10,10,-10), Q=(-10,10,-10)

R=(-10,10,10), S=(10,10,10) and XY

plane is projection plane, (0,0,15) is the

eye position.

X

Y

Z

S

PQ

R

...(0,0,5)

(0,0,15)

1

2

3

4 X

Y

Page 56: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 56 東吳資訊管理 江清水

(2) Move your eye from (0,0,15) to ((0,0,15),

what you will see?

X

Y

X

Y

Z

S

PQ

R

...(0,0,5)

(0,0,15)

1

2

3

4

Page 57: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 57 東吳資訊管理 江清水

(d) How about z > d (i.e., point is

behind the eye) ?

(e) How about z d (i.e., point is

near the eye) ?

X

Y

Z

X

Y

Z

X

Y

Z

Page 58: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 58 東吳資訊管理 江清水

View Transformation

In general, we may want to observe object from some other position (in stead of (0,0,d)) and direction (in stead of - Ze), say from point (xf , yf

,zf ) to point (xt ,yt ,zt ).

Notice that the world data and view vector now are given in world coordinates, not eye coordinates.

Xw

Yw

Zw

world

viewvector

data

(Xf ,Yf ,Zf )

(Xt ,Yt ,Zt )

representingobject

Page 59: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 59 東吳資訊管理 江清水

We must transform the object data from world coordinates into eye coordinates before we can perform orthographic parallel or perspective projection . This transformation called viewing transformation.

Where is the eye coordinate system?

We make the arbitrary Ye (Up

direction) which perpedicular to Ze

Xw

Yw

Zw

worlddata

Ze

XeYe

Page 60: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 60 東吳資訊管理 江清水

We want a "neat" viewing transformation, V, such that[xe ye ze 1] = [xw yw zw 1] * V i.e.,multiply all world points by V and it will be just as if the eye was at (0,0,d) or (0,0,), depend onperspeative or orthographic parallelprojection, looking at (0,0,0).

Visualize the process as transforming the Ze and Ye axes to Zw and Yw axes. This is accomplished using four basic transformations: 1. Translate the vector so that (xt , yt , zt ) = (0, 0, 0). 2. Rotate the result so that the transformed (xf , yf , zf )' swings into the Yw-Zw plane. 3. Rotate the result so that (xf , yf

, zf )'' swings down onto the Zw

axis. 4. Rotate this result so that Ye swings down onto the Yw axis.

Page 61: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 61 東吳資訊管理 江清水

Step 1. The first step is just a simple translation.

Xw

Yw

Zw

XeYe

Ze

T1 =

= [ ]

Page 62: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 62 東吳資訊管理 江清水

Step 2. Rotate degrees about Yw to swing eye' into the Yw-Zw plane.

tan(2- =X'/Z'=tan(- =

Check the sign (check rotation direction)

Step 3. Swing eye'' down by degrees to (0,0,d) by rotating eye'' about Xw.

=

Xw

Yw

Zw

eye'eye''

Page 63: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 63 東吳資訊管理 江清水

Now, the eye coordinate system and world coordinate system are shown in the following picture.

Step 4. Rotate Xe about Zw degrees.

(asswne Xe = (X3, Y3, 0))

= -tan-1 Y'/X'

Xw

Yw

Zw

Xe

Ye

Ze

Page 64: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 64 東吳資訊管理 江清水

Our transformation equation becomes:

[xe ye ze 1] = [xw yw zw 1] T1 R2 R3 R4

To check your transformatios, try:

eye = (0,1,0)

aim = (0,0,0)

Ye = (1,0,0)

Xw

Yw

Zw

(0,1,0)w

(1,0,0)w

(0,0,1)w

Xe

Ye

Ze

Page 65: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 65 東吳資訊管理 江清水

Homework:

Try again with Ye = (0,0,1) and

other values are the same.

Xw

Zw

Yw

(0,1,0)w (0,0,1)e

(0,0,0)w (0,0,0)e

(1,0,0)w (-1,0,0)e

(0,0,1)w (0,1,0)e

Page 66: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 66 東吳資訊管理 江清水

View Volume

We saw that as z -> d, severe distortion occures, like a "fisheye" lens. Also, data far from the observer (z << 0) becomes dots!

To eliminate these effects, we'll introduce two clipping planes, parallel to the Xe-Ye plane:

(1) A front clipping plane at z=zfront

(2) A back clipping plane at z=zback

This creates a viewing volume, which is a pyramid, and is now a 6-sided frustum.

XXee

YYee

ZZee

Back

Clipping

Plane

Front Clipping Plane

Projection Plane

Page 67: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 67 東吳資訊管理 江清水

Z-Clipping

Now the full clipping procedure becomes:

(1) Clip on front and back planes. (2) Perspective projection to 2D window. (3) Clip on the 2D window.To determine the visibility of a point

P(x,y,z), just test its z value: If ((z > zfront) or (z<zback)) then "Invisible", else "Visible".(1) Under what conditions that a line segment will not be clipped? P1 = (X1, Y1, Z1) P2 = (X2, Y2, Z2) Zback Z1, Z2 Zfront

(2) How to clip a line segment using Z-clipping? (1) Z = Z1 + (Z2-Z1)t (2)replace Z = Zfront or Zend , find t (3)find (X Y Z) = (X1 Y1 Z1) + t(X2-X1, Y2-Y1, Z2-Z1)

Page 68: CGPage: 1 東吳資訊管理 江清水 Computer Game : Basic 1.Computer Graphics and Its application 2.2D Coordinate Geometry 3.1 2D geometry -- points and polygons 3.2

CG Page: 68 東吳資訊管理 江清水

Three types of perspective projections:

(1) one-point

(2) two-point

(3) three-point