15
1 CS 431/636 Advanced Rendering Techniques Dr. David Breen Korman 105D Wednesday 6PM 8:50PM Presentation 2 4/11/12 Start Up Any questions from last time? Go over sampling image plane? Or intersection algorithms? Slide Credits Leonard McMillan, Seth Teller, Fredo Durand, Barb Cutler - MIT G. Drew Kessler, Larry Hodges - Georgia Institute of Technology John Hart - University of Illinois Rick Parent - Ohio State University More Geometry & Intersections Ray/Plane Intersection Ray is defined by R(t) = R o + R d *t where t 0 R o = Origin of ray at (x o , y o , z o ) R d = Direction of ray [x d , y d , z d ] (unit vector) Plane is defined by [A, B, C, D] Ax + By + Cz + D = 0 for a point in the plane Normal Vector, N = [A, B, C] (unit vector) A 2 + B 2 + C 2 = 1 D = - N • P 0 (P 0 - point in plane) Ray/Plane (cont.) Substitute the ray equation into the plane equation: A(x o + x d t) + B(y o + y d t) + C(z o +z d t) + D = 0 Solve for t: t = -(Ax o + By o + Cz o + D) / (Ax d + By d + Cz d ) t = -(N • R o - N • P 0 ) / (N • R d )

CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

1

CS 431/636 Advanced Rendering Techniques"

Dr. David Breen"Korman 105D"Wednesday 6PM → 8:50PM"

Presentation 2"4/11/12"

Start Up"n Any questions from last time?"n Go over sampling image plane?"n Or intersection algorithms?"

Slide Credits"n  Leonard McMillan, Seth Teller,

Fredo Durand, Barb Cutler - MIT"n G. Drew Kessler, Larry Hodges - Georgia

Institute of Technology"n  John Hart - University of Illinois"n Rick Parent - Ohio State University"

More Geometry & Intersections

Ray/Plane Intersection"Ray is defined by R(t) = Ro + Rd*t where t ≥ 0""Ro = Origin of ray at (xo, yo, zo)""Rd = Direction of ray [xd, yd, zd] (unit vector)"

"Plane is defined by [A, B, C, D]""Ax + By + Cz + D = 0 for a point in the plane""Normal Vector, N = [A, B, C] (unit vector)""A2 + B2 + C2 = 1""D = - N • P0 (P0 - point in plane)"

Ray/Plane (cont.)"Substitute the ray equation into the plane equation:"

A(xo + xdt) + B(yo + ydt) + C(zo +zdt) + D = 0"

"Solve for t:"

t = -(Axo + Byo + Czo + D) / (Axd + Byd + Czd)"t = -(N • Ro - N • P0 ) / (N • Rd)""

Page 2: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

2

What Can Happen?" N • Rd = 0 N • Rd > 0

t < 0

Ro

t > 0

Ro

Ray/Plane Summary"Intersection point:"

(xi, yi, zi) = (xo + xdti, yo + ydti, zo + zdti)"

""1.  Calculate N • Rd and compare it to zero."2.  Calculate ti and compare it to zero."3.  Compute intersection point."4.  Flip normal if N • Rd is positive"

Ray-Parallelepiped Intersection"n Axis-aligned n From (X1, Y1, Z1) to (X2, Y2, Z2) n Ray P(t)=Ro+Rdt

y=Y2

y=Y1

x=X1 x=X2

Ro

Rd

Naïve ray-box Intersection"n Use 6 plane equations n Compute all 6 intersection n Check that points are inside box

Ax+By+Cz+D ≤ 0

y=Y2

y=Y1 x=X1 x=X2

Ro

Rd

Factoring out computation"n Pairs of planes have the same normal n Normals have only one non-0 component n Do computations one dimension at a time n Maintain tnear and tfar (closest and farthest

so far) y=Y2

y=Y1 x=X1 x=X2

Ro

Rd

Test if parallel"n  If Rdx = 0, then ray is parallel

n  If Rox < X1 or Rox > x2 return false

y=Y2

y=Y1

x=X1 x=X2

Ro

Rd

Page 3: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

3

If not parallel"n Calculate intersection distance t1 and t2"

n  t1 = (X1-Rox)/Rdx"n  t2 = (X2-Rox)/Rdx"

y=Y2

y=Y1

x=X1 x=X2

Ro

Rd

t1

t2

Test 1"n  Maintain tnear and tfar "

n  If t1 > t2, swap"n  if t1 > tnear, tnear = t1 "n  if t2 < tfar, tfar = t2"

n  If tnear > tfar, box is missed"

y=Y2

y=Y1

x=X1 x=X2

Ro Rd

t1x t2x tnear

t1y

t2y tfar

tfar

Test 2"n  If tfar < 0, box is behind"

y=Y2

y=Y1

x=X1 x=X2

Ro

Rd

t1x t2x tfar

Algorithm recap"n  Do for all 3 axes"

n  Calculate intersection distance t1 and t2"n  Maintain tnear and tfar"n  If tnear > tfar, box is missed; Done"n  If tfar < 0, box is behind; Done"

n  If box survived tests, return intersection at tnear"n  If tnear is negative, return tfar "

y=Y2

y=Y1

x=X1 x=X2 Ro Rd

tnear

t1y

tfar

Ray/Ellipsoid Intersection Ray/Cylinder Intersection "

Ellipsoid's surface is defined by the set of points {(xs, ys, zs)} satisfying the equation:"

"(xs/ a)2 + (ys/ b)2 + (zs/ c)2 - 1 = 0"

"Cylinder's surface is defined by the set of points

{(xs, ys, zs)} satisfying the equation:""

(xs)2 + (ys)2 - r2 = 0 " "-z0 ≤ zs ≤ z0""

Centers at origin""

§  Substitute ray equation into surface equations"

§  This is a quadratic equation in t:"§  At2 + Bt + C = 0"

§  Analyze as before"§  Solve for t with quadratic formula"§  Plug t back into ray equation - Done"§  Well,… not exactly"

Ray/Ellipsoid Intersection Ray/Cylinder Intersection "

Page 4: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

4

Ray/Cylinder Intersection "§  Is intersection point Pi between -Z0 and Z0?"§  If not, Pi is not valid"§  Also need to do intersection test with

z = -Z0 , Z0 plane"§  If (Pix)2 + (Piy)2 ≤ r2, you’ve intersected a “cap”"

§  Which valid intersection is closer?"

Superquadrics"

!

xa1

"

# $

%

& '

2( 2

+ya2

"

# $

%

& '

2( 2

)

*

+ +

,

-

.

.

( 2(1

+za3

"

# $

%

& '

2(1

=1

Bezier Patch"n  Patch of order (n, m) can be defined in terms of a set

of (n + 1)(m + 1) control points Pi+1,j+1 for integer indices i = 0 to n, j = 0 to m."

!

(u,v)" [0,1]2

p(u,v) =i= 0

n

# Bin (u)B j

m (v)j= 0

m

# Pi+1, j+1

Bin (u) =

ni$

% & '

( ) ui(1* u)n* i

Tesselate Patches and Superquadrics"

Tesselate Patches and Superquadrics"

Page 5: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

5

Utah Teapot"

n  Modeled by 32 Bézier Patches"n  Control points available at http://

www.holmes3d.net/graphics/teapot"

SMF Triangle Meshes"v -1 -1 -1 v 1 -1 -1 v -1 1 -1 v 1 1 -1 v -1 -1 1 v 1 -1 1 v -1 1 1 v 1 1 1 f 1 3 4 f 1 4 2 f 5 6 8 f 5 8 7 f 1 2 6 f 1 6 5 f 3 7 8 f 3 8 4 f 1 5 7 f 1 7 3 f 2 4 8 f 2 8 6

vertices

triangles

Draw data structure

Triangle Meshes (.iv)" Transformations & Hierarchical Models

n Add an extra dimension"•  in 2D, we use 3 x 3 matrices"•  In 3D, we use 4 x 4 matrices"

n Each point has an extra value, w"

Homogeneous Coordinates"

x' y' z' w'

=

x y z w

a e i m

b f j n

c g k o

d h l p

p' = M p

n Most of the time w = 1, and we can ignore it"

Homogeneous Coordinates"

x' y' z' 1

=

x y z 1

a e i 0

b f j 0

c g k 0

d h l 1

Page 6: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

6

Translate (tx, ty, tz)"n Why bother with the

extra dimension? Because now translations can be encoded in the matrix!"

Translate(c,0,0)

x

y

p p'

c

x' y' z' 1

=

x y z 1

1 0 0 0

0 1 0 0

0 0 1 0

tx

ty

tz

1

x' y' z'

Scale (sx, sy, sz)"n  Isotropic (uniform)

scaling: sx = sy = sz!

n  You only have to implement uniform scaling!

x' y' z' 1

=

x y z 1

sx

0 0 0

0 sy

0 0

0 0 sz

0

0 0 0 1

Scale(s,s,s)

x

p

p'

qq'

y

Rotation"n About z axis"

x' y' z' 1

=

x y z 1

cos φ sin φ

0 0

-sin φ cos φ

0 0

0 0 1 0

0 0 0 1

ZRotate(φ)

x

y

z

p

p'

θ

Rotation"

n About x axis:"

n About y axis:"

x' y' z' 1

=

x y z 1

0 cos α sin α

0

0 -sin α cos α

0

1 0 0 0

0 0 0 1

x' y' z' 1

=

x y z 1

cos θ 0

-sin θ 0

sin θ 0

cos θ 0

0 1 0 0

0 0 0 1

Rotation"n About (kx, ky, kz), an " arbitrary unit vector " (Rodrigues Formula)"

x' y' z' 1

=

x y z 1

kxkx(1-c)+c kykx(1-c)+kzs kzkx(1-c)-kys

0

0 0 0 1

kykx(1-c)-kzs kyky(1-c)+c

kzky(1-c)+kxs 0

kxkz(1-c)+kys kykz(1-c)-kxs kzkz(1-c)+c

0

where c = cos θ & s = sin θ

Rotate(k, θ)

x

y

z

θ

k

How are transforms combined?"

(0,0) (1,1)

(2,2)

(0,0)

(5,3)

(3,1) Scale(2,2) Translate(3,1)

TS = 2

0

0 2

0 0

1

0

0 1

3 1

2

0

0 2

3 1

=

Scale then Translate

Use matrix multiplication: p' = T ( S p ) = ((TS) p)

Caution: matrix multiplication is NOT commutative!

0

0

1

0

0

1

0

0

1

Page 7: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

7

Non-commutative Composition"Scale then Translate: p' = T ( S p ) = TS p

Translate then Scale: p' = S ( T p ) = ST p

(0,0)

(1,1) (4,2)

(3,1)

(8,4)

(6,2)

(0,0) (1,1)

(2,2)

(0,0)

(5,3)

(3,1) Scale(2,2) Translate(3,1)

Translate(3,1) Scale(2,2)

TS = 2

0 0

0 2

0

0 0 1

1

0 0

0 1

0

3 1 1

ST = 2

0

0 2

0 0

1

0

0 1

3 1

Non-commutative Composition"Scale then Translate: p' = T ( S p ) = TS p

2

0 0

0 2

0

3 1 1

2

0

0 2

6 2

=

=

Translate then Scale: p' = S ( T p ) = ST p 0

0

1

0

0

1

0

0

1

Transformations in Ray Tracing

Transformations in Modeling"n Position objects in a scene"n Change the shape of objects"n Create multiple copies of objects"n Projection for virtual cameras"n Animations"

Scene Description"

Scene

Lights Camera

Objects Materials Background

Simple Scene Description File"

Camera { center 0 0 10 direction 0 0 -1 up 0 1 0 } Lights { numLights 1 DirectionalLight { direction -0.5 -0.5 -1 color 1 1 1 } } Background { color 0.2 0 0.6 } Materials { numMaterials <n>

<MATERIALS> } Group { numObjects <n> <OBJECTS> }

Page 8: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

8

Hierarchical Models"

n Logical organization of scene"Group { numObjects 3 Group { numObjects 3 Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { numObjects 2 Group { Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { Box { <BOX PARAMS> } Sphere { <SPHERE PARAMS> } Sphere { <SPHERE PARAMS> } } } Plane { <PLANE PARAMS> } }

Simple Example with Groups"

Group { numObjects 3 Group { numObjects 3 Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { numObjects 2 Group { Box { <BOX PARAMS> } Box { <BOX PARAMS> } Box { <BOX PARAMS> } } Group { Box { <BOX PARAMS> } Sphere { <SPHERE PARAMS> } Sphere { <SPHERE PARAMS> } } } Plane { <PLANE PARAMS> } }

Adding Materials" Adding Transformations"

Using Transformations"

n  Position the logical groupings of objects within the scene"

n  Transformation in group"

Directed Acyclic Graph is more efficient and useful"

Page 9: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

9

Processing Model Transformattions"

n Goal"n  Get everything into world coordinates"

n Traverse graph/tree in depth-first order"n Concatenate transformations"n Can store intermediate transformations"n Apply/associate final transformation to

primitive at leaf node"n What about cylinders, superquadrics, etc.?"

n  Transform ray!"

Transform the Ray"

n Map the ray from World Space to Object Space!

World Space

r major r minor

(x,y)

(0,0) Object Space

r = 1

pWS = M pOS

pOS = M-1 pWS

Transform Ray"n  New origin:"

n  New direction:"originOS = M-1 originWS

directionOS = M-1 (originWS + 1 * directionWS) - M-1 originWS

originOS

originWS

directionOS

directionWS

Object Space World Space

qWS = originWS + tWS * directionWS

qOS = originOS + tOS * directionOS

directionOS = M-1 directionWS

Transforming Points & Directions"

n  Transform point"

n  Transform direction"

n  Map intersection point and normal back to world coordinates"

Homogeneous Coordinates: (x,y,z,w) W = 0 is a point at infinity (direction)

Transform Normals"n Why? They’re used for shading"""

object color only Diffuse Shading

Transforming Normals"n  A surface normal is a property, not a

geometric entity!

n  Correct normal transformation matrix:"!

See http://www.cgafaq.info/wiki/Transforming_Normals"

Page 10: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

10

Given overlapping shapes A and B:"

Union Intersection Subtraction"

Constructive Solid Geometry (CSG)" How can we implement CSG?"

Points on A, Outside of B

Points on B, Outside of A

Points on B, Inside of A

Points on A, Inside of B

Union Intersection Subtraction"

Collect all the intersections"

" Union Intersection Subtraction"

Implementing CSG"

1.  Test "inside" intersections:"•  Find intersections with A,

test if they are inside/outside B"•  Find intersections with B,

test if they are inside/outside A"

2.  Overlapping intervals:"•  Find the intervals of "inside"

along the ray for A and B"•  Compute union/intersection/

subtraction of the intervals"

Constructive Solid Geometry

Ray Tracing CSG

Models

Rick ParentOhio State U."

Seminal paper "Ray tracing CSG models"

n S.D. Roth, "Ray Casting for Modeling Solids", Computer Graphics and Image Processing 18 (2): 109–144, February 1982"

Page 11: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

11

CSG"n Form object as booleans of primitive

objects"n  Primitives: sphere, cube, cylinder, cone"n  Boolean operators: union, intersection,

difference"n Tree structure used to manage operations"

n  Leaf nodes are primitive objects"n  Intermediate nodes specify combination

operator"

Ray tracing CSG models"n  Intersect ray with primitives"n Produces “spans” along ray"n Perform Boolean operations on

spans"n Determines intersection of evaluated

model"n Calculate normal at intersection"

Union"

Min (tCmin, tB

min )

B

C +

B C

Ray intersects union: at first intersection

Possible ways for 2 spans to overlap"

Intersection"++

If ((tCmin< tB

min ) and (tCmax> tB

min ) ): tBmin

Else If ((tBmin< tC

min ) and (tBmax> tC

min ) ): tCmin

Else: none

B C

B

C

First time in B and in C

Difference"

-

B C

If ((tBmin< tC

min ): tBmin

Else if (tCmax< tB

max ): tCmax

Else: none

B

C

First time in B not in C

Page 12: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

12

Difference"-

B C

If ((tCmin< tB

min): tCmin

Else if (tBmax< tC

max ): tBmax

Else: none

B

C

First time in C not in B

Primitives"Anything that can be intersected (easily) with a ray

Conics: solve analytically using R(t) Convex polyhedra A plane (a cutting plane is useful)

can be used as a modeling tool (boolean operations) surface model (e.g., polyhedron) computed from CGS

or Can be used as a model representation

keep tree structure and ray trace directly

Controlling the Combinations"

- +

+

?

Tree Structure"

circle rectangle rectangle

+ +

+ -

T1 T2 T3

T4

T5 - +

+ T1

T2

T3

Tree Structure #1" Tree Structure"

circle rectangle rectangle

+ +

- +

T1 T2 T3

T4

T5 - +

+ T1

T2

T3

Page 13: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

13

Tree Structure #2" Tree Structure"•  Intersect ray with leaf nodes (primitive objects) •  Combine intersection spans according to intermediate nodes

•  union •  intersection •  difference

•  Might create multiple spans

Union of Spans" Intersection of Spans"

Difference of Spans" Normals of CSG intersections"

Normal of some surface (or its negation)

Union or intersection: positive normal of intersected surface

Page 14: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

14

Difference normals"•  Intersection is one of:

•  tmin of positive object – normal of surface •  tmax of negative object – negated normal

Add transformations to tree"

http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/model/csg.html

Bounding Volumes"

circle rectangle rectangle

+ +

+ -

T1 T2 T3

T4

T5

Construction • Use bounding volumes at leaf nodes •  Union bounding volumes at interior nodes

Traversal • Top-down • Test bounding volume at interior

Example"

Example" Example (Simon Chorley)"

Page 15: CS 431/636 Start Up Advanced Rendering Techniquesdavid/Classes/CS431/Lectures/CGII_Pres… · 1! CS 431/636 Advanced Rendering Techniques" Dr. David Breen" Korman 105D" Wednesday

15

Example (Simon Chorley)" For example:"

Wrap Up"n Discuss status/problems/issues with

this week’s programming assignment"