1 Ray Tracing Polyhedra ©Anthony Steed 1999-2005.

21
Ray Tracing Polyhedra ©Anthony Steed 1999- 2005

Transcript of 1 Ray Tracing Polyhedra ©Anthony Steed 1999-2005.

1

Ray Tracing Polyhedra

©Anthony Steed 1999-2005

2

Overview

Barycentric coordinates Ray-Polygon Intersection Test Affine Transformations

3

Line Equation

Recall that given p1 and p2 in 3D space, the straight line that passes between is:

for any real number t This is a simple example of a

barycentric combination

21)1()( tppttp

4

A Barycentric Combination is

a weighted sum of points, where weight sum to 1. • Let p1,p2,…,pn be points

• Let a1,a2,…,an be weights

11

n

ii

n

iiii

a

pap

5

Implications

If p1,p2,…,pn are co-planar points then p as defined will be inside the polygon defined by the points

Proof of this is out of scope, but a few diagrams should convince you of the outline of a proof …

6

Ray Tracing a Polygon

Three steps• Does the ray intersect the plane of the

polygon?– i.e. is the ray not orthogonal to the plane

normal

• Intersect ray with plane• Test whether intersection point lies within

polygon on the plane

7

Does the ray intersect the plane?

Ray eq. is p0 + t.d Plane eq. is n.(x,y,z) = k

Then test is n.d !=0

8

Where does it intersect?

Substitute line equation into plane equation

Solve for t

Find pi

kdztzdytydxtxn .... 000

dn

pnkt

)( 0

9

Is this point inside the polygon?

If it is then it can be represented in barycentric co-ordinates

There are potentially several barycentric combinations

Many tests are possible• Winding number (can be done in 3D)• Infinite ray test (done in 2D)• Half-space test (done in 2D for convex poylgons)

10

Winding number test

Sum the angles subtended by the vertices. If sum is zero, then outside. If sum is +/-2, inside.

With non convex shapes, can get +/-4, +-6, etc…

1

n-1

p1

p2

pn-1

11

Infinite Ray Test

Draw a line from the test point to the outside• Count +1 if you cross an edge in

an anti-clockwise sense• Count -1 if you cross and edge in

a clockwise sense For convex polygons you can

just count the number of crossings, ignoring the sign

If total is even then point is outside, otherwise inside

+1

-1

12

Half-Space Test (Convex Polygons)

A point p is inside a polygon if it is in the negative half-space of all the line segments

13

Note

That the winding angle and half-space tests only tell you if the point is inside the polygon, they do not get you a barycentric combination

With some minor extensions, its easy to show that the infinite ray test finds a barycentric combination.

14

Transforming Polygons

Although its sort of “obvious” that transformations of object preserve flatness and shape, we need to convince ourselves of something specific: that barycentric coordinates are preserved under affine transformations

If they weren’t it would be extremely hard to shade and texture polygons later on in the course

If a rotation, scale, translation are affine then barycentricity is preserved

If barycentricity is preserved then polygons are still “flat” after transformation

15

Transformations Revisited

Homogenous transform as described is affine

Preserves barycentric co-ordinates f is affine iff

11

1

1

n

ii

n

ii

n

iii

pp

pfpf

16

To Show Transformation is Affine

Unit vectors e1=(1 0 0), e2=(0 1 0), e3=(0 0 1) and e4=(0 0 0)

p=(x1 x2 x3)

Let x4=1-x1-x2-x3

Thus

4

1

44332211

iiiexp

exexexexp

17

...

But for unit vectors, mapping is easy to derive

3

1

321

jjij

iiii

e

ef

18

So combine those two

4

1

3

1

4

1

3

1

iiij

jj

jjij

ii

xe

expf

19

...

But we know x4=1-x1-x2-x3

3

14

3

1

4

4

1

3

1

ijiij

jj

jijij

iiij

jj

xepf

xepf

20

...

Expand that, remembering what ei is

But this is the matrix transformation

3

1433

3

1422

3

1411

iii

iii

iii xxxpf

...

' 41312111 azayaxax

21

Recap

Lines and polygons (and volumes) can be determined in terms of barycentric coordinates

We have shown that homogenous transformations are, by definition, affine

This polygons remain “flat” Thus we now can use arbitrary

transformations on polyhedra