Download Ray Tracing

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Free and open-source graphics device driver wikipedia , lookup

Computer vision wikipedia , lookup

BSAVE (bitmap format) wikipedia , lookup

Waveform graphics wikipedia , lookup

Apple II graphics wikipedia , lookup

Framebuffer wikipedia , lookup

Video card wikipedia , lookup

Tektronix 4010 wikipedia , lookup

Graphics processing unit wikipedia , lookup

2.5D wikipedia , lookup

Molecular graphics wikipedia , lookup

Rendering (computer graphics) wikipedia , lookup

Ray tracing (graphics) wikipedia , lookup

Transcript
Last Time?
Ray Tracing
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Quiz!
Today
• Tuesday Oct 18th
• In class
• 1 page of notes
• Ray Casting
Cutler
– Ray-Plane Intersection
– Ray-Sphere Intersection
– Point in Polygon
• Ray Tracing
– Shadows
– Reflection
– Refraction
• Recursive Ray Tracing
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Durer’s Ray Casting Machine
Ray Casting
• Albrecht Durer, 16th century
For every pixel
Construct a ray from the eye
For every object in the scene
Cutler
Finding the intersection
and normal is the central
part of ray casting
Find intersection with the ray
Keep if closest
Shade depending on light and normal vector
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Cutler
1
A Note on Shading
Ray Representation?
• Surface/Scene Characteristics:
• Two vectors:
N
– surface normal
– direction to light
– viewpoint
L
– Origin
– Direction (normalized is better)
V
• Parametric line (explicit representation)
• Material Properties
– P(t) = origin + t * direction
– Diffuse (matte)
– Specular (shiny)
– …
P(t)
• More later!
direction
origin
Diffuse sphere
CSCI-6962 Advanced Computer Graphics
Specular spheres
Cutler
CSCI-6962 Advanced Computer Graphics
3D Plane Representation?
H(p) = d > 0
P
• Plane defined by
– Po = (x,y,z)
– n = (A,B,C)
Explicit vs. Implicit?
• Ray equation is explicit
normal
Po
• Implicit plane equation
– H(P) = Ax+By+Cz+D = 0
= n·P + D = 0
H
P'
H(p) = d < 0
• Point-Plane distance?
– If n is normalized,
distance to plane, d = H(P)
– d is the signed distance!
CSCI-6962 Advanced Computer Graphics
Cutler
P(t) = Ro + t * Rd
– Parametric
– Generates points
– Hard to verify that a point is on the ray
• Plane equation is implicit
H(P) = n·P + D = 0
– Solution of an equation
– Does not generate points
– Verifies that a point is on the plane
• Exercise: Explicit plane and implicit ray
Cutler
CSCI-6962 Advanced Computer Graphics
Cutler
Ray-Plane Intersection
Additional Housekeeping
• Intersection means both are satisfied
• So, insert explicit equation of ray into
implicit equation of plane & solve for t
• Verify that intersection is closer than previous
P(t)
P(t) = Ro + t * Rd
H(P) = n·P + D = 0
n·(Ro + t * Rd) + D = 0
t = -(D + n·Ro) / n·Rd
CSCI-6962 Advanced Computer Graphics
Cutler
P(t) < tcurrent
• Verify that it is not out of range (behind eye)
P(t) > tmin
P(t)
CSCI-6962 Advanced Computer Graphics
Cutler
2
Normal
Ray-Polygon Intersection
• For shading
• Ray-plane intersection
• Test if intersection is in the polygon
– diffuse: dot product between light and normal
• Normal is constant
– Solve in the 2D plane
normal
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Point Inside/Outside Polygon
Sphere Representation?
• Ray intersection definition:
• Implicit sphere equation
– Cast a ray in any direction
Cutler
– Assume centered at origin (easy to translate)
– H(P) = P·P - r2 = 0
• (axis-aligned is smarter)
– Count intersections
– If odd number, point is inside
• Works for concave and star-shaped
• Special case for triangle…
Rd
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Ray-Sphere Intersection
Ray-Sphere Intersection
• Insert explicit equation of ray into
implicit equation of sphere & solve for t
• Quadratic: at2 + bt + c = 0
r2
P(t) = Ro + t*Rd
H(P) = P·P - = 0
(Ro + tRd) · (Ro + tRd) - r2 = 0
Rd·Rdt2 + 2Rd·Rot + Ro·Ro - r2 = 0
Ro
Cutler
– a = 1 (remember, ||Rd|| = 1)
– b = 2Rd·Ro
– c = Ro·Ro – r2
• with discriminant
• and solutions
Rd
CSCI-6962 Advanced Computer Graphics
Ro
Cutler
CSCI-6962 Advanced Computer Graphics
Cutler
3
Questions?
Today
• Ray Casting
– Ray-Plane Intersection
– Ray-Sphere Intersection
– Point in Polygon
• Ray Tracing
– Shadows
– Reflection
– Refraction
• Recursive Ray Tracing
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
How Can We Add Shadows?
Mirror Reflection
Find the point to be shaded
For every light,
Construct ray from point to light
For every object
find intersection of ray with object
If no objects between point and light
Add contribution from light
• Cast ray symmetric with
respect to the normal
• Multiply by reflection
coefficient (color)
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Reflection
Amount of Reflection
• Reflection angle = view angle
• R = V – 2 (V · N) N
• Traditional ray tracing (hack)
Cutler
Cutler
– Constant reflectionColor
• More realistic:
– Fresnel reflection term (more reflection at grazing angle)
– Schlick’s approximation: R(θ)=R0+(1-R0)(1-cos θ)5
N
V
θV θR
R
N
V N N
V
θV θR
R
V N N
V
CSCI-6962 Advanced Computer Graphics
Cutler
metal
CSCI-6962 Advanced Computer Graphics
Dielectric (glass)
Cutler
4
Transparency
Qualitative Refraction
• Cast ray in refracted direction
• Multiply by transparency coefficient (color)
From “Color and Light in Nature” by Lynch and Livingston
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Refraction
Refraction & the Sidedness of Objects
I
– M sin Өi
ηi
ηT
I
N
Өi N cos Өi
N
= [ ηr cos Өi – √1 – sin2 ӨT ] N – ηr I
= [ ηr cos Өi – √1 –
ηr2 sin2
ηi = material
index
ηi=1
ηT = material
index
= [ ηr cos Өi – cos ӨT ] N – ηr I
T
I
I
= – N cos ӨT + (N cos Өi – I) sin ӨT / sin Өi
= – N cos ӨT + (N cos Өi – I) ηr
Snell-Descartes Law:
ηi sin Өi = ηT sin ӨT
sin ӨT
η
= i = ηr
sin Өi
ηT
• Make sure you know whether you’re entering or leaving
the transmissive material:
T = – N cos ӨT + M sin ӨT
M
-N
= N cos Өi – M sin Өi
M = (N cos Өi – I) / sin Өi
ӨT
Cutler
ηT= 1
Өi ] N – ηr I
T
T
= [ ηr cos Өi – √1 – ηr2 (1 – cos2 Өi ) ] N – ηr I
N
= [ ηr (N · I) – √1 – ηr2 (1 – (N · I)2 ) ] N – ηr I
• Total internal reflection when
the square root is imaginary
• Don’t forget to normalize!
CSCI-6962 Advanced Computer Graphics
• What about intersecting transparent objects?
Cutler
Total Internal Reflection
CSCI-6962 Advanced Computer Graphics
Cutler
Refraction and the Lifeguard Problem
• Running is faster than swimming
Water
Lifeguard
Beach
Run
Person
in trouble
From “Color and Light in Nature” by Lynch and Livingston
CSCI-6962 Advanced Computer Graphics
Cutler
Swim
CSCI-6962 Advanced Computer Graphics
Cutler
5
Questions?
Today
• Ray Casting
– Ray-Plane Intersection
– Ray-Sphere Intersection
– Point in Polygon
• Ray Tracing
– Shadows
– Reflection
– Refraction
• Recursive Ray Tracing
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Recap: Ray Tracing
Recursion For Reflection
trace ray
Intersect all objects
color = ambient term
For every light
cast shadow ray
Stopping criteria:
color += local shading term
• Recursion depth
If mirror
– Stop after a number
color += colorrefl *
of bounces
trace reflected ray
If transparent
• Ray contribution
color += colortrans *
– Stop if reflected /
trace transmitted ray
transmitted
contribution
• Does it ever end?
becomes too small
CSCI-6962 Advanced Computer Graphics
CSCI-6962 Advanced Computer Graphics
Eye
T1
N3
N1
L1
R3
Cutler
L1
R1
L3
T1
L2
Ni surface normal
2 recursions
• Visualize the ray tree for single image pixel
T3
L2
1 recursion
Ray Debugging
R2
R1
0 recursion
Cutler
The Ray Tree
N2
Cutler
Eye
R2
L3
R3
T3
incoming
Ri reflected ray
reflected ray
Li shadow ray
Ti transmitted (refracted) ray
CSCI-6962 Advanced Computer Graphics
Complexity?
shadow ray
transmitted (refracted) ray
Cutler
CSCI-6962 Advanced Computer Graphics
Cutler
6
Does Ray Tracing Simulate Physics?
Forward Ray Tracing
• Photons go from the light to the eye,
not the other way
• What we do is backward ray tracing
• Start from the light source
– But low probability to reach the eye
• What can we do about it?
– Always send a ray to the eye…. still not efficient
CSCI-6962 Advanced Computer Graphics
Cutler
Transparent Shadows?
CSCI-6962 Advanced Computer Graphics
Cutler
Traditional Ray Tracing?
• What to do if ray to light source intersects a transparent
object?
– Pretend it’s opaque?
– multiply by transparency color?
(ignores refraction & does not produce caustics)
• Ray Tracing is full of dirty tricks
Images by Henrik Wann Jensen
• No, Refraction and complex reflection for illumination are not
handled properly in traditional (backward) ray tracing
CSCI-6962 Advanced Computer Graphics
Cutler
CSCI-6962 Advanced Computer Graphics
Cutler
What makes a Rainbow?
The Rendering Equation
• Refraction is wavelength-dependent
• Clean mathematical framework for lighttransport simulation
• At each point, outgoing light in one direction
is the integral of incoming light in all directions
multiplied by reflectance property
– Refraction increases as the wavelength of light decreases
– violet and blue experience more bending than orange and red
• Usually ignored in graphics
• Rainbow is caused by
refraction + internal reflection + refraction
Pink Floyd, The Dark Side of the Moon
From “Color and Light in Nature”
CSCI-6962 Advanced
Graphics
Cutler
by Computer
Lynch and
Livingstone
CSCI-6962 Advanced Computer Graphics
Cutler
7
Questions?
CSCI-6962 Advanced Computer Graphics
Cutler
8