cse 472 more ray tracingcse472/secure/11.pdf · intersect function cse 472 s2019 bool...

38
CSE 472 More Ray Tracing CSE 472 S2019 Announcement (Today’s office hour) Yiying’s Zoom link https://msu.zoom.us/j/3952532206 CSE 472 S2019

Upload: others

Post on 31-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

CSE 472

More Ray Tracing

CSE 472 S2019

Announcement (Today’s office hour)

Yiying’s Zoom link

https://msu.zoom.us/j/3952532206

CSE 472 S2019

Page 2: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Fun with Ray Tracing

Shadows and shadow feelers

Antialiasing

Stochastic ray tracing

Reflection

Transmission

Penumbra

Depth of field

CSE 472 S2019

The Ray Tracing Process

Render model into intersection data structure

For each pixel

Compute pixel color

Set pixel color

CSE 472 S2019

Page 3: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Computing Ray Color

CSE 472 S2019

// Compute the color for a ray

color RayColor(ray)

Test ray for intersection

If(intersection)

Determine for intersection:

intersection point, normal, material, texture color

color = compute-ambient-color

foreach light

color += compute-diffuse-color-from-light

color += compuer-specular-color-from-light

return color

else

return background-color

What you get…

CSE 472 S2019

Page 4: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

This is nicer…

CSE 472 S2019

How do we get shadows?

What is different about the light computation to get shadows?

CSE 472 S2019

Page 5: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Shadow Feelers

Before we compute the color contribution for a light, shoot a ray in the light direction.

Does it hit anything?

Called a: “Shadow Feeler”

CSE 472 S2019

Computing Ray Color

CSE 472 S2019

// Compute the color for a ray

color RayColor(ray)

Test ray for intersection

If(intersection)

Determine for intersection:

intersection point, normal, material, texture color

color = compute-ambient-color

foreach light

if shadowfeeler does not hit anything

color += compute-diffuse-color-from-light

color += compuer-specular-color-from-light

return color

else

return background-color

Page 6: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

But, be careful or you get:

CSE 472 S2019 Stipple!

Problems w/ numerical precision

CSE 472 S2019

How far is this point

from the polygon?

Page 7: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Problems w/ numerical precision

CSE 472 S2019

A little above or a little below

Your intersection point will be rounded to the nearest float, double, whatever.

Unlikely it will be on the polygon!

If it’s above, no problem

If it’s below, what happens to our

shadow feeler?

Term: Self-shadowingCSE 472 S2019

Page 8: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Self-shadowing Solutions

Ignore any intersection less than some small amount

But, how small? What about

corners?

Ignore the polygon we are intersecting with.

Most common solution.

CSE 472 S2019

Intersect Function

CSE 472 S2019

bool Intersect(const CRay &p_ray, // The ray we are testing

double p_maxt, // The maximum allowable t

const Object *p_ignore, // A polygon to ignore

const Object *&p_object, // Object we hit (return)

double &p_t, // Distance to intersection (return)

CGrPoint &p_intersect); // Intersection point (return)

Page 9: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Intersect Function

CSE 472 S2019

bool Intersect(const CRay &p_ray, // The ray we are testing

double p_maxt, // The maximum allowable t

const Object *p_ignore, // A polygon to ignore

const Object *&p_object, // Object we hit (return)

double &p_t, // Distance to intersection (return)

CGrPoint &p_intersect); // Intersection point (return)

What is the use for p_maxt?

Summary

Construct a shadow feeler ray

Starts at intersection, direction is towards light

Shoot ray at light (intersection test)

Set ignore to ignore the polygon our intersection point is on.

Set maxt to the distance to the light.

If we get an intersection, we are

shadowed

CSE 472 S2019

Page 10: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Next amazing thing

CSE 472 S2019

Aliasing, aka “Jaggies”

CSE 472 S2019

Why does

this happen?

Page 11: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Point sampling

CSE 472 S2019

Point sampling

CSE 472 S2019

Page 12: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

But, we don’t see like this…

Why don’t we see like this?

Why don’t camera images look like this?

CSE 472 S2019

Characteristics of the Human Eye

Receptors have area

They are not points

What would this mean?

Receptors are randomly distributed

Not true for cameras, but they do get more artifacts than we see

CSE 472 S2019

Page 13: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

That “area” issue…

CSE 472 S2019

What we want (to simulate nature)

We want the color of a pixel to be the area-weighted average of what’s under the box for the pixel

How can we do this with rays?

CSE 472 S2019

Page 14: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

General answer to all things ray tracing…

CSE 472 S2019

Instead of one ray though the

center…

CSE 472 S2019

Page 15: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

But, where?

Bad choice

Rectangular grid

• Causes animations problems

CSE 472 S2019

Common Alternatives

Fixed Jitter Tables

OpenGL book has one

Not really a very great choice

Random

The choice of champions

CSE 472 S2019

Page 16: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Antialiasing in Ray Tracing

For each pixel

For i=1 to antialiascnt

• Generate random location in pixel

• Shoot ray through random pixel

Ray color is average of all rays

CSE 472 S2019

uniform random numbers

First idea is to use uniformly distributed random number (rand())

This does not work as well because they tend to have clumps.

CSE 472 S2019

Page 17: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Clumping

CSE 472 S2019

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

10 100

1000

The Poisson Disk Distribution

Simple idea

We generate a set of random

numbers, no two of which are closer

than some minimum distance to

each other

How would you do this

• Think of generating one random number at a time

• How do you do this in 2D?CSE 472 S2019

Page 18: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Poisson Disk Sample Generator

CSE 472 S2019

For each set of random numbers needed:

history.clear

For each random number needed:

do

valid = true // Till we know otherwise

point p = (rand(min,max), rand(min, max))

foreach item in history

if(Distance(item, p) < minimumD

valid = false

while !valid

history.add(p)

return p

Poisson Disk Distributions

CSE 472 S2019

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

10 100

1000

Page 19: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Compare

CSE 472 S2019

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

0 0.2 0.4 0.6 0.8 1

Random Poisson

What’s the minimum distance?

I usually use: range / (2 * sqrt(N))

N points in 2D spaced evenly would

be range / sqrt(N) apart

We make the minimum distance half

that

When sampling an area that is not

square, use: sqrt(area) / (2 * sqrt(N))

CSE 472 S2019

Page 20: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Where does the name come from?

CSE 472 S2019

Poisson is French for “Fish”

CSE 472 S2019

Page 21: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

BTW…

The rods and cones in the eye approximate a Poisson distribution

Well, what do you know about that!!

CSE 472 S2019

Antialiasing Ray Tracing

CSE 472 S2019

CPoisson2D poisson

Poisson.SetMinDistance( 1 / (2 * sqrt(antialiascnt) )

for r = 0 to height-1

for c = 0 to width-1

poisson.reset()

color = (0, 0, 0)

for a=1 to antialiascnt

p = poisson.generate()

ray.origin = (0, 0, 0)

ray.direction.x = xmin + (c + p.x) / width * xwid

ray.direction.y = ymin + (r + p.y) / height * ywid

color += RayColor(ray)

color /= antialiascnt

pixel(r, c) = RangeBound(color)

Page 22: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

No AntialiasingCSE 472 S2019

4x AntialiasingCSE 472 S2019

Page 23: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

16x AntialiasingCSE 472 S2019

64x Antialiasing/20 minutesCSE 472 S2019

Page 24: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Stochastic Ray Tracing

The term for the use of random rays to increase quality in ray tracing is:

Stochastic Ray Tracing

Sometimes also called:

Distributed Ray Tracing

• A most unfortunate choice of words

CSE 472 S2019

Next?

CSE 472 S2019

Page 25: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Reflections

Hall indicated what to do with the reflection, so where does it come from?

Shoot more rays

Shoot a ray in the reflection direction and see what color you see.

• The Self-Shadowing issue still applies!

CSE 472 S2019

RayColor function

You should have a RayColor function

Here’s what mine looks like:

This will call itself

We call this a recursive ray tracer

What is your stopping condition?

CSE 472 S2019

void CRaytraceRenderer::RayColor(const CRay &p_ray, (input)

CRayColor &p_color, (output)

int p_recurse, (to control recursion)

const CUseIntersection::Object *p_ignore) (ignore)

Page 26: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Transmission

I’ll let you figure this one out…

CSE 472 S2019

Recursion issues

Don’t recurse if you don’t need to

What if the specular color is black?

Recursion is expensive

Decide how much you want to do

CSE 472 S2019

Page 27: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

What’s different, now?

CSE 472 S2019

CSE 472 S2019

Page 28: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Penumbra

Penumbra is a soft-edged shadow

You get it when the light has area

Often called “soft shadows”

CSE 472 S2019

How do we solve this problem?

All together, now!

CSE 472 S2019

Page 29: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

How do we solve this problem?

All together, now!

CSE 472 S2019

Implementing Penumbra

CSE 472 S2019

Sample point on the surface of the light with a Poisson

Distribution. Minimum distance is:

d = sqrt(lightArea) / sqrt(numRays) * 0.5

Page 30: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

How to find a spot on the surface of

a rectangular light

CSE 472 S2019

ab

cd

len=length(b – a)

wid=length(d – a)

area = len * wid

x = xp / len y = yp / wid

point = a (1 – x) (1 – y) + b (x) (1 – y) + c (x) (y) + d (1 – x) (y)

(xp, yp) is from Poisson distribution from (0, 0) to (len, wid)

RayColor with Penumbra

CSE 472 S2019

CPoisson2D poisson

Poisson.SetMinDistance( sqrt(lightarea) / (2 * sqrt(antialiascnt) )

color = ambient light

foreach light

for p=1 to penumbraCnt

PP = random point on surface of light (Poisson distribution)

L = PP – intersect

if shadowfeeler doesn’t hit anything

color += contribution from this light / penumbraCnt

color += any reflections

color += any transmission

return color

Page 31: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

16x Antialiasing, 8x penumbra, 1 level of recursionCSE 472 S2019

Stats for that image

CSE 472 S2019

Kd Tree Nodes: 31399

Tree Depth: 39

Intersection Tests: 112,276,007

Polygon Tests: 476,466,446

Average polygon tests per IT: 4.24371

Page 32: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Depth of Field

What causes depth of field in a camera or your eye?

How would you implement it?

CSE 472 S2019

Putting it all together

We have three stochastic things:

Antialiasing

Penumbra

Depth of Field

What’s an efficient way to do all of these together?

CSE 472 S2019

Page 33: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Motion blur

What causes motion blur?

How would you implement it?

CSE 472 S2019

Fog

Any ideas?

CSE 472 S2019

Page 34: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Stars

Why are they hard?

CSE 472 S2019

Stochastic Ray Tracing

Problem with ray tracing: Point sampling

We can introduce randomness all over the place Jitter for antialiasing

Light area for penumbra

Depth of field and motion blur

CSE 472 S2019

[Jensen07]

Page 35: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Path Tracing

CSE 472 S2019

1) Sample each pixel with N rays

2) At each intersection, trace only one rayproportion of rays in each category

should match the actual (surface) distribution

Ray Tracing Path Tracing

Reduces the amount of work deeper in the ray tree

Adaptive sampling

Determine if number of samples is enough

If not, increase the number

Works for uniform and stochastic sampling

CSE 472 S2019

Page 36: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Ray tracing from the light sources

In regular ray tracing we don’t see

Lighting reflected through mirrors

Surfaces lit by reflection from other surfaces

Large set of rays from each light

Computes light that hits surfaces

General solution for diffuse reflection?

Still requires tracing from the eye as pass 2

CSE 472 S2019

Photon map

• Photon by light-based raytracing

• Separating GI from rendering

• Stored in kd-trees

• Good for

Caustics, subsurface scattering,

CSE 472 S2019

Page 37: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Photon path

Start from light sources

Intensity -> photon

CSE 472 S2019[Jensen]

Final Gathering

• Radiance Computation

• Distributed ray tracing

CSE 472 S2019[Jensen]

Page 38: CSE 472 More Ray Tracingcse472/secure/11.pdf · Intersect Function CSE 472 S2019 bool Intersect(const CRay &p_ray, // The ray we are testing double p_maxt, // The maximum allowable

Another example

CSE 472 S2019

[Jensen00]

RTX (check http://intro-to-dxr.cwyman.org/)

CSE 472 S2019