77
קורס גרפיקה ממוחשבת חלק מהשקפים מעובדים משקפים של פרדו דוראנד, טומס פנקהאוסר ודניאל כהן- אור1 Rendering, Ray Casting

Rendering, Ray Casting

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Rendering, Ray Casting

קורס גרפיקה ממוחשבת

1 אור-ודניאל כהן פנקהאוסר טומס, דוראנדחלק מהשקפים מעובדים משקפים של פרדו

Rendering, Ray Casting

Page 2: Rendering, Ray Casting

What is 3D rendering? Construct an image from a 3D model

2

תאורה מצלמה

3Dמודל

View Plane

Rendering

Page 3: Rendering, Ray Casting

Rendering Scenarios אינטראקטיבי 10לפחות (מייצרים תמונות בשבריר שנייה ◦

כאשר המשתמש שולט בפרמטרים של ) בשנייה הרינדור

יש צורך להשיג את האיכות הגבוהה ביותר בהתחשב )הקצב הנדרש(בזמן הנתון

וכומשחקים , שימושי לויזואליזציות'

3

Page 4: Rendering, Ray Casting

Rendering Scenarios אצווה)batch( כל תמונה מיוצרת ברמת פירוט גבוהה ככל ◦

האפשר עבור סט ספציפי של פרמטריםלוקח כמה זמן שצריך וכוסרטים , לפוטוריאליזםשימושי'

4 Jensen

Page 5: Rendering, Ray Casting

3D Rendering Issues What does a 3D rendering system have to do? ◦ Camera ◦ Visible surface determination ◦ Lights ◦ Reflectance ◦ Shadows ◦ Indirect illumination ◦ Sampling ◦ Etc.

5

Page 6: Rendering, Ray Casting

Camera Models The most common model is pin-hole camera ◦ All captured light rays arrive along paths toward focal

point without lens distortion (everything is in focus) ◦ Sensor response proportional to radiance

Other models consider ... Depth of field Motion blur Lens distortion

View plane

Eye position (focal point)

aristotle

Page 7: Rendering, Ray Casting

Camera Parameters Position ◦ Eye position (px, py, pz)

Orientation ◦ View direction (dx, dy, dz) ◦ Up direction (ux, uy, uz)

Aperture ◦ Field of view (xfov, yfov)

Film plane ◦ “Look at” point ◦ View plane normal

right

back

Up direction

Eye Position

View Plane

“Look at” Point

Page 8: Rendering, Ray Casting

3D Rendering Issues What does a 3D rendering system have to do? ◦ Camera ◦ Visible surface determination ◦ Lights ◦ Reflectance ◦ Shadows ◦ Indirect illumination ◦ Sampling ◦ Etc.

8

Page 9: Rendering, Ray Casting

Visible Surface Determination The color of each pixel on the view plane

depends on the radiance emanating from visible surfaces

View plane

Eye position

Simplest method is ray casting

Rays through

view plane

Page 10: Rendering, Ray Casting

Ray Casting For each sample … ◦ Construct ray from eye position through view plane ◦ Find first surface intersected by ray through pixel ◦ Compute color of sample based on surface radiance

Page 11: Rendering, Ray Casting

Ray Casting

For each sample … ◦ Construct ray from eye position through view plane ◦ Find first surface intersected by ray through pixel ◦ Compute color of sample based on surface radiance

Page 12: Rendering, Ray Casting

Ray Casting

For each sample … ◦ Construct ray from eye position through view plane ◦ Find first surface intersected by ray through pixel ◦ Compute color of sample based on surface radiance

Page 13: Rendering, Ray Casting

3D Rendering Issues What does a 3D rendering system have to do? ◦ Camera ◦ Visible surface determination ◦ Lights ◦ Reflectance ◦ Shadows ◦ Indirect illumination ◦ Sampling ◦ Etc.

13

Page 14: Rendering, Ray Casting

Lights and Surfaces Different Lights

14

Page 15: Rendering, Ray Casting

Lights and Surfaces Different Materials

15

Page 16: Rendering, Ray Casting

Other Lighting Parameters Atmospheric attenuation

Camera response

16

Page 17: Rendering, Ray Casting

Lighting Simulation Lighting parameters ◦ Light source emission ◦ Surface reflectance ◦ Atmospheric attenuation ◦ Camera response

N N

Camera

Surface

Light Source

Page 18: Rendering, Ray Casting

Lighting Simulation

18

Page 19: Rendering, Ray Casting

Phong Reflectance Model Simple analytic model ◦ Diffuse reflection+ ◦ Specular reflection+ ◦ Emission+ ◦ “ambient”

19

Page 20: Rendering, Ray Casting

3D Rendering Issues What does a 3D rendering system have to do? ◦ Camera ◦ Visible surface determination ◦ Lights ◦ Reflectance ◦ Shadows ◦ Indirect lllumination ◦ Sampling ◦ Etc.

20

Page 21: Rendering, Ray Casting

Shadows Occlusions from light sources

21

Page 22: Rendering, Ray Casting

Shadows Occlusions from light sources ◦ Soft shadows with area light source

22

Page 23: Rendering, Ray Casting

Shadows

23

Page 24: Rendering, Ray Casting

3D Rendering Issues What does a 3D rendering system have to do? ◦ Camera ◦ Visible surface determination ◦ Lights ◦ Reflectance ◦ Shadows ◦ Indirect illumination ◦ Sampling ◦ Etc.

24

Page 25: Rendering, Ray Casting

Path Types

25

Direct diffuse + indirect specular and transmission

Page 26: Rendering, Ray Casting

Path Types

26

+ Soft Shadows

Page 27: Rendering, Ray Casting

Path Types

27

+ caustics

Page 28: Rendering, Ray Casting

Path Types

28

+ indirect diffuse illumination

Page 29: Rendering, Ray Casting

3D Rendering Issues What does a 3D rendering system have to do? ◦ Camera ◦ Visible surface determination ◦ Lights ◦ Reflectance ◦ Shadows ◦ Indirect illumination ◦ Sampling ◦ Etc.

29

Page 30: Rendering, Ray Casting

Scene can be sampled with any ray ◦ Rendering is a problem in sampling and

reconstruction

30

Page 31: Rendering, Ray Casting

31

Ray Casting

Page 32: Rendering, Ray Casting

3D Rendering The color of each pixel on the view plane

depends on the radiance emanating from visible surfaces

View plane

Eye position

Rays through

view plane

Page 33: Rendering, Ray Casting

Ray Casting For each sample … ◦ Construct ray from eye position through view plane ◦ Find first surface intersected by ray through pixel ◦ Compute color sample based on surface radiance

Page 34: Rendering, Ray Casting

Ray Casting For each sample … ◦ Construct ray from eye position through view plane ◦ Find first surface intersected by ray through pixel ◦ Compute color sample based on surface radiance

Samples on view plane

Eye position

Rays through

view plane

Page 35: Rendering, Ray Casting

Ray Casting

Simple implementation: Image RayCast(Camera camera, Scene scene, int width, int height) { Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Ray ray = ConstructRayThroughPixel(camera, i, j); Intersection hit = FindIntersection(ray, scene); image[i][j] = GetColor(hit); } } return image; }

Page 36: Rendering, Ray Casting

Ray Casting

Simple implementation: Image RayCast(Camera camera, Scene scene, int width, int height) { Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Ray ray = ConstructRayThroughPixel(camera, i, j); Intersection hit = FindIntersection(ray, scene); image[i][j] = GetColor(hit); } } return image; }

Page 37: Rendering, Ray Casting

Constructing Ray Through a Pixel

right

back

Up direction

P0

View Plane

P

V

Ray: P = P0 + tV

Page 38: Rendering, Ray Casting

Constructing Ray Through a Pixel

2D Example

d Θ towards P0

right

right = towards x up

Θ = frustum half-angle d = distance to view plane

P1 = P0 + d*towards - d*tan(Θ)*right P2 = P0 + d*towards + d*tan(Θ)*right

P1

P2

2*d*tan(Θ)

P

P = P1 + (i/width - 0.5) * 2*d*tan (Θ)*right V = (P - P0) / ||P - P0 ||

V

Ray: P = P0 + tV

,2 2

width widthi ∈ −

Page 39: Rendering, Ray Casting

Ray Casting

Simple implementation: Image RayCast(Camera camera, Scene scene, int width, int height) { Image image = new Image(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { Ray ray = ConstructRayThroughPixel(camera, i, j); Intersection hit = FindIntersection(ray, scene); image[i][j] = GetColor(hit); } } return image; }

Page 40: Rendering, Ray Casting

Ray-Scene Intersection Intersections with geometric primitives ◦ Sphere ◦ Triangle ◦ Groups of primitives (scene)

Acceleration techniques ◦ Bounding volume hierarchies ◦ Spatial partitions Uniform grids Octrees BSP trees

Page 41: Rendering, Ray Casting

Ray-Sphere Intersection Ray: P = P0 + tV Sphere: |P - O|2 - r 2 = 0

P0

V

O

P

r

P’

Page 42: Rendering, Ray Casting

Ray-Sphere Intersection I Ray: P = P0 + tV Sphere: |P - O|2 - r 2 = 0 Substituting for P, we get: |P0 + tV - O|2 - r 2 = 0 Solve quadratic equation: at2 + bt + c = 0 where: a = 1 b = 2 V • (P0 - O) c = |P0 - O|2 - r 2 = 0

P0

V

O

P

r

P’

Algebraic Method

P = P0 + tV

Page 43: Rendering, Ray Casting

Ray-Sphere Intersection II Ray: P = P0 + tV Sphere: |P - O|2 - r 2 = 0 L = O - P0

tca = L • V if (tca < 0) return 0

d2 = L • L - tca2

if (d2 > r2) return 0

thc = sqrt(r2 - d2) t = tca - thc and tca + thc P0

V O

P

r

P’

r d thc

tca

L

Geometric Method

P = P0 + tV

Page 44: Rendering, Ray Casting

Ray-Sphere Intersection

P0

V

O

P r

N = (P - O) / ||P - O||

N

Need normal vector at intersection for lighting calculations

Page 45: Rendering, Ray Casting

Ray-Scene Intersection Intersections with geometric primitives ◦ Sphere » Triangle ◦ Groups of primitives (scene)

Acceleration techniques ◦ Bounding volume hierarchies ◦ Spatial partitions Uniform grids Octrees BSP trees

Page 46: Rendering, Ray Casting

Ray-Triangle Intersection First, intersect ray with plane Then, check if point is inside triangle

P

P0

V

Page 47: Rendering, Ray Casting

Ray-Plane Intersection Ray: P = P0 + tV Plane: N(P-P0)=0 P • N + c = 0 Substituting for P, we get: (P0 + tV) • N + c = 0 Solution: t = -(P0 • N + c) / (V • N)

N

P

P0

V

Algebraic Method

And the intersection at: P = P0 + tV

P0

Page 48: Rendering, Ray Casting

Ray-Triangle Intersection I Check if point is inside triangle algebraically

P

P0

N1

T1

T2

T3

V2

V1

For each side of triangle V1 = T1 – P0 V2 = T2 – P0 N1 = V2 x V1 Normalize N1 if (P -P0) • N1 < 0 return FALSE; end

Page 49: Rendering, Ray Casting

Ray-Triangle Intersection II Check if point is inside triangle parametrically

P

P0

Compute α, β: P = α (T2-T1) + β (T3-T1) Check if point inside triangle. 0 ≤ α ≤ 1 and 0 ≤ β ≤ 1 α + β ≤ 1

V α

β T1

T2

T3

Page 50: Rendering, Ray Casting

Other Ray-Primitive Intersections

Cone, cylinder, ellipsoid: ◦ Similar to sphere

Box ◦ Intersect 3 front-facing planes, return

closest

Convex polygon ◦ Same as triangle (check point-in-polygon

algebraically)

Concave polygon ◦ Same plane intersection ◦ More complex point-in-polygon test

Algorithms for 3D object intersection: http://www.realtimerendering.com/int/

Page 51: Rendering, Ray Casting

Intersection FindIntersection(Ray ray, Scene scene) { min_t = infinity min_primitive = NULL For each primitive in scene { t = Intersect(ray, primitive); if (t < min_t) then min_primitive = primitive min_t = t } } return Intersection(min_t, min_primitive) }

Ray-Scene Intersection Find intersection with front-most primitive in group

A

B

C

D

E

F

Brute Force!

Page 52: Rendering, Ray Casting

Ray-Scene Intersection Intersections with geometric primitives ◦ Sphere ◦ Triangle ◦ Groups of primitives (scene)

» Acceleration techniques ◦ Bounding volume hierarchies ◦ Spatial partitions Uniform grids Octrees BSP trees

Page 53: Rendering, Ray Casting

Bounding Volumes Check for intersection with simple shape first ◦ If ray doesn’t intersect bounding volume,

then it doesn’t intersect its contents

Page 54: Rendering, Ray Casting

Bounding Volume Hierarchies I Build hierarchy of bounding volumes ◦ Bounding volume of interior node contains all

children

1

2 3

A

B

C

D

E

F

3

2

1

A B E F D

C

Page 55: Rendering, Ray Casting

Bounding Volume Hierarchies Use hierarchy to accelerate ray intersections ◦ Intersect node contents only if hit bounding volume

1

2 3 C

A B E F D A

B

C

D

E

F

3

2

1

1

2

A B

C 3

Page 56: Rendering, Ray Casting

Bounding Volume Hierarchies III

FindIntersection(Ray ray, Node node) { // Find intersections with child node bounding volumes ... // Sort intersections front to back ... // Process intersections (checking for early termination) min_t = infinity; for each intersected child i { if (min_t < bv_t[i]) break; shape_t = FindIntersection(ray, child); if (shape_t < min_t) { min_t = shape_t;} } return min_t; }

Sort hits & detect early termination

Page 57: Rendering, Ray Casting

Ray-Scene Intersection Intersections with geometric primitives ◦ Sphere ◦ Triangle ◦ Groups of primitives (scene)

» Acceleration techniques ◦ Bounding volume hierarchies ◦ Spatial partitions Uniform grids Octrees BSP trees

Page 58: Rendering, Ray Casting

Uniform Grid

Construct uniform grid over scene ◦ Index primitives according to overlaps with

grid cells

A

B

C

D

E

F

Page 59: Rendering, Ray Casting

Uniform Grid

Trace rays through grid cells ◦ Fast ◦ Incremental

A

B

C

D

E

F

Only check primitives in intersected grid cells

Given an entry point into a cell and a vector, its easy to calculate exit point

Page 60: Rendering, Ray Casting

Uniform Grid

Potential problem: ◦ How to choose a suitable grid resolution?

A

B

C

D

E

F

No/Little benefit if grid is too coarse

Too costly if grid is too fine

Page 61: Rendering, Ray Casting

Octree A tree data structure used to partition

three dimensional space A 2D version is called a Quadtree

61

Page 62: Rendering, Ray Casting

Octree Construct adaptive grid over scene ◦ Recursively subdivide box-shaped cells into 8 octants ◦ Index primitives by overlaps with cells

A

B

C

D

E

F Generally fewer cells

Quadtree

Page 63: Rendering, Ray Casting

Octree Trace rays through neighbor cells ◦ Fewer cells ◦ More complex neighbor finding

A

B

C

D

E

F Trade-off fewer cells for

more expensive traversal

Page 64: Rendering, Ray Casting

Octree Very useful in computer graphics, used for ◦ Intersections ◦ Collisions ◦ Color quantization ◦ Surface reconstruction (meshing)

◦ …

64

Page 65: Rendering, Ray Casting

Binary Space Partition (BSP) Tree Recursively partition space by planes ◦ Every cell is a convex polyhedron

A

B

C

D

E

F

1

2

3

1

2

4

4

3

5

5

Page 66: Rendering, Ray Casting

Binary Space Partition (BSP) Tree Simple recursive algorithms ◦ Example: point finding

A

B

C

D

E

F

1

2

3

1

2

4

4

3

5

5

P 1

3

Page 67: Rendering, Ray Casting

Binary Space Partition (BSP) Tree Trace rays by recursion on tree ◦ BSP construction enables simple front-to-back

traversal

A

B

C

D

E

F

1

2

3

1

2

4

4

3

5

5

P 1

2

4

3

Page 68: Rendering, Ray Casting

Other Accelerations Screen space coherence ◦ Check last hit first ◦ Beam tracing ◦ Pencil tracing ◦ Cone tracing

Memory coherence ◦ Large scenes

Parallelism ◦ Ray casting is “embarrassingly parallelizable”

etc.

Page 69: Rendering, Ray Casting

We need to determine Vx and Vy

right

back

Up direction

P0

View Plane

P

V

Ray: P = P0 + tV

Vy

Vx

P0

Page 70: Rendering, Ray Casting

Camera Coordinate System

Find the transformation matrix M that rotate the world coordinate system to the camera coordinate system (Vx,Vy,Vz) (normalized)

Vy

Vx

Vz

y

x

z

M (0,0,1) = Vz

Page 71: Rendering, Ray Casting

Camera Coordinate System

The vector X and Y are rotated by M

Vy

Vx

Vz

y

x

z

M (0,0,1) = Vz

Page 72: Rendering, Ray Casting

The definition of the Matrix M

1 0 00 C x S x0 -Sx C x

C y 0 S y0 1 0

-Sy 0 C y•

CxCySx-CxSy-SxCyCxSxSy-

Sy0Cy

= M =

Cz Sz 0Sz Cz 00 0 1

− (0,0,1) • = (0,0,1)

Rotate around x

Rotate around z

Rotate around y

Let Cx and Sx denote sin(x), cos(x), respectively

Page 73: Rendering, Ray Casting

The definition of the Matrix M Since: (0,0,1) • M = (-CxSy, -Sx, CxCy) = Vz Vz = (Vz.x,Vz.y,Vz.z) = (a,b,c). We get: a = -CxSy; b = -Sx; c = CxCy, or Sx = -b; Cx = sqrt(1 - (Sx)2 );

Sy = -a/Cx; Cy = c/Cx;

CxCySx-CxSy-SxCyCxSxSy-

Sy0Cy

Page 74: Rendering, Ray Casting

Compute the Camera Coordinate System

Now, use M to rotate the world coordinate vectors:

Vx = (1,0,0) • M Vy = (0,1,0) • M Vz = (0,0,1) • M Note that the vector V is normalized.

Page 75: Rendering, Ray Casting

We need to determine Vx and Vy

E

View Plane

P

Vy

Vx

P0

f

Vz

P = E + Vz * f

P0 = P - w∗Vx - h∗Vy

Let f be the distance between the eye E and the plane along Vz, and w and h the lengths of half the screen size.

Page 76: Rendering, Ray Casting

The main loop Image RayCast(Camera camera, Scene scene, int width, int height) { Image image = new Image(width, height); Set P0 (as in the previous slide);

for (int i = 0; i < height; i++) { p = P0; for (int j = 0; j < width; j++) { Ray ray = E + t * (p – E); Intersection hit = FindIntersection(ray, scene); image[i][j] = GetColor(hit); p += Vx; // move one pixel along the vector Vx } P0 += Vy; // move one pixel along the vector Vy } return image; }

Page 77: Rendering, Ray Casting

Summary Writing a simple ray casting renderer is easy ◦ Generate rays ◦ Intersection tests ◦ Lighting calculations

What’s next?

◦ Illumination Illumination