45
Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structure s 1.2 Vectors and computer graphics 1.3 Rays and computer graphics 1.4 Bi-linear Interpolation of polygon prope rties 1.5 A basic maths engine using SIMD instruct ions

Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Embed Size (px)

Citation preview

Page 1: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Mathematical fundamentals of 3D computer graphics1.1 Manipulating three-dimensional structures

1.2 Vectors and computer graphics

1.3 Rays and computer graphics

1.4 Bi-linear Interpolation of polygon properties

1.5 A basic maths engine using SIMD instructions

Page 2: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.1 Manipulating three-dimensional structures This chapter deals with basic three-dimensional transformations and

three-dimensional geometry . In computer graphics the most popular method for representing an

object is the polygon mesh model. A polygon mesh model consists of a structure of vertices, each

vertex being a three-dimensional point . 1.1.1 Three-dimensional geometry in computer graphics-affine

transformations 1.1.2 Transformations for changing coordinate systems

Page 3: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.1.1 Three-dimensional geometry in computer graphics-affine transformations

Cartesian coordinate system Right-handed

Left-handed

Page 4: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Transformation

Translation

Scaling

Rotation

RVV

DVV

SVV

Page 5: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Homogeneous coordinates

to for and scale factor

x = X/w y = Y/w z = Z/w

Default w =1 then the matrix representation of a point is

0w),,( zyxV ),,,( wZwYwXwV

1

z

y

x

Page 6: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Translation

translation

11000

100

010

001

1

'

'

'

z

y

x

T

T

T

z

y

x

z

y

x

TVV

z

y

x

Tzz

Tyy

Txx

Page 7: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Scaling

Scaling

SVV

11000

000

000

000

1

'

'

'

z

y

x

S

S

S

z

y

x

z

y

x

z

y

x

Szz

Syy

Sxx

Page 8: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Rotation

Rotation

Z axis

RVV

1000

0cossin0

0sincos0

0001

xR

1000

0cos0sin

010

0sin0cos

yR

1000

010

0cossin

00sincos

zR

zz

yxy

yxx

cossin

sincos

Page 9: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Inverse of transformation

T-1, negate Tx, Ty, Tz

S-1, replace Sx, Sy, Sz by 1/Sx,1/Sy,1/Sz

R-1, negate the angle of rotation

Page 10: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Inverse of transformation

These transformation can be multiplied or concatenated together to give a net transformation matrix. For example:

in the product M2M1, the first matrix to be multiplied is M1

translations are commutative, rotations are not and

11

1z

y

x

Mz

y

x

11

2z

y

x

Mz

y

x

123 MMM

11

3z

y

x

Mz

y

x

1221 RRRR

Page 11: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Linear transformations examples

Identity

1000

0100

0010

0001

Page 12: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Linear transformations examples

Z-axis rotation

1000

0100

00866.05.0

005.0866.0

Page 13: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Linear transformations examples

X-scale

1000

0100

0010

0002

Page 14: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Linear transformations examples

Translation

1000

0100

2010

2001

Page 15: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Linear transformations examples

Rotation followed by translation

1000

0100

20866.05.0

205.0866.0

1000

0100

00866.05.0

005.0866.0

1000

0100

2010

2001

Page 16: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Linear transformations examples

Translation followed by rotation

1000

0100

732.00866.05.0

732.205.0866.0

1000

0100

2010

2001

1000

0100

00866.05.0

005.0866.0

Page 17: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Rotating about the object’s reference point(1) Translate the object to origin

(2) Apply the desired rotation, and

(3) Translate the object back to its original position

The net transformation matrix is:

1000

0100

)cossin(0cossin

)sincos(0sincos

1000

0100

010

001

1000

0100

00cossin

00sincos

1000

0100

010

001

12

yyx

zyx

y

x

y

x

TTT

TTT

T

T

T

T

RTT

Page 18: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

Rotating about the object’s reference point

Page 19: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.1.2 Transformations for changing coordinate systems Coordinate systems

Object coordinate World coordinate View coordinate

Transformations Modeling transformation

Object to world

Viewing transformation World to view

Page 20: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2 Vectors and computer graphics

Addition of vectors Length of vectors Normal vectors and cross-products Normal vectors and dot products Vectors associated with normal vector

Page 21: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.1 Addition of vectors

X = V + W

= (x1,x2,x3)

= (v1+w1, v2+w2, v3+w3)

Page 22: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.2 Length of vectors

Length of V

Normalise vector V to produce a unit vector U

212

32

22

1 )( vvvV

V

VU

Page 23: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.3 Normal vectors and cross-products Cross product

i, j and k are the standard unit vectors

WVX kwvwvjwvwviwvwv )()()( 122131132332

)1,0,0(

)0,1,0(

)0,0,1(

k

j

i

Page 24: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.3 Normal vectors and cross-products The normal to the polygon is found by taking the cross product

of these:

21 VVNp

Page 25: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.3 Normal vectors and cross-products If the surface is a bi-cubic parametric surface, then the

orientation of the normal vector varies continuously over the surface. For a surface defined as Q (u, v) we find:

We then define:

),(),( vuQv

andvuQu

v

Q

u

QNs

Page 26: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.4 Normal vectors and products

Dot product of vectors

Using the cosine rule ( θis the angle between the vectors )332211 wvwvwv

WVX

WV

WV

WVWV

thus

WWVVWV

WVWVWV

cos

cos

2

cos2222

222

Page 27: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.4 Normal vectors and products

We can use the dot product to project a vector onto another vector. If we project any vector W onto V(V is a unit vector), then we have:

cosWX

WV

WV

WVW

Page 28: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.5 Vectors associated with the normal vector The light vector L is a vector whose direction is given by the line

from the tail of the surface normal to the light source

Page 29: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.5 Vectors associated with the normal vector The reflection vector R is given by the direction of the light reflected from the s

urface due to light incoming along direction L.

Page 30: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.5 Vectors associated with the normal vector View vector V that this vector has any arbitrary orientation and

we are normally interested in that component of light incoming in direction L that is reflected along V.

Page 31: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.2.5 Vectors associated with the normal vector Consider the construction shown in figure.

LRR

Thus

RLR

RRR

2

2

21

2

1

LNLNR

and

NLNR

)(2

)(2

Page 32: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3 Rays and computer graphics

Ray geometry – intersections Intersections – ray – sphere Intersections – ray – convex polygon Intersections – ray – box

Page 33: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.1 Ray geometry – intersections

The most common calculation associated with rays is intersection testing.

The most common technique used to make this more efficient is to enclose objects in the scene in bounding volume – the most convenient being a sphere.

Page 34: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.2 Intersections – ray – sphere

The intersection between a ray and a sphere is easily calculated. The end points of the ray are (x1,y1,z1), (x2,y2,z2) then parametrise the ray:

A sphere at center (l, m ,n) of radius r is given by:

Substituting for x, y, z gives a quadratic equation in t of the form:

ktztzzzz

jtytyyyy

itxtxxxx

1121

1121

1121

)(

)(

)(

10 t

where

2222 )()()( rnzmylx

02 cbtat2

1112

12

12

1222

111

222

)(2

)(2)(2)(2

rnzmylxzyxnmlc

nzkmyjlxib

kjia

Page 35: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.2 Intersections – ray – sphere

Solution:

Determinant: D>0 :二根為前後二交點 D=0 :相切於一點 D<0 :沒有交點

If the intersection point is (xi, yi, zi ) and the centre of the sphere is (l, m, n) then the normal at the intersection point is :

a

acbbt

2

42

acbD 42

),,(r

nz

r

my

r

lxN

iii

Page 36: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.3 Intersections – ray – convex polygon If an object is represented by a set of polygons and is convex

then the straightforward approach is to test the ray individually against each polygon. We do this as follow:

Obtain an equation for the plane containing the polygon Check for an intersection between this plane and the ray Check that this intersection is contained by the polygon

Page 37: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.3 Intersections – ray – convex polygon Example: 包含 polygon的平面 :Ax + By + Cz + D = 0 Ray 的參數式:

The intersection (將參數式代入平面求解 )

ktztzzzz

jtytyyyy

itxtxxxx

1121

1121

1121

)(

)(

)(

)()( 111

CkBjAiDCzByAxt

Page 38: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.3 Intersections – ray – convex polygon If t<0. This means that the ray is in the half-space defined by the plane that

does not contain the polygon. If the denominator is equal to zero which means that the line and plane are

parallel. In this case the ray origin is either inside or outside the polyhedron.

Page 39: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.3 Intersections – ray – convex polygon Test a point for containment by a polygon

Simple but expensive method: 由此點畫直線連接多邊形各頂點 , 若夾角的合為 360, 則交點在此 polygon內部 , 否則此交點在 polygon外部

Haines method: 依光線行進方向將每一平面分成 front-facing, back-facing:

分母>0: back-facing 分母<0: front-facing

Page 40: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.3 Intersections – ray – convex polygon Haines algorithm

{initialize tnear to large negative value tfar to large positive value}

if {plane is back-facing} and (t<tfar)

then tfar = t

if {plane is front-facing} and (t>tnear)

then tnear = t

if (tnear>tfar) then {exit – ray misses}

Page 41: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.3 Intersections – ray – convex polygon

Page 42: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.1 Intersections – ray – box

Treat each pair of parallel planes in turn. Calculating the distance along the ray to the first plane (tnear) a

nd the distance to the second plane (tfar). The larger value of tnear and the smaller value of tfar is retained

between comparisons. If the larger value of tnear is greater than smaller value of tfar, th

e ray cannot intersect the box.

Page 43: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.3.1 Intersections – ray – box

Page 44: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.4 Bi-linear interpolation of polygon properties Bi-linear interpolation equations

Final equation (constant value calculated once per scan line)

ppp ss p

Page 45: Mathematical fundamentals of 3D computer graphics 1.1 Manipulating three-dimensional structures 1.2 Vectors and computer graphics 1.3 Rays and computer

1.4 Bi-linear interpolation of polygon properties