Upload
truonganh
View
235
Download
0
Embed Size (px)
Citation preview
TURNER WHITTED’S RAY-TRACING ALGORITHM: PRACTICE
PROGRAMAÇÃO 3D
MEIC/IST
Ray Tracing History
Ray Tracing History
Whitted ray tracing
It combines in a single model:
Hidden surface removal
Shading due to direct illumination
Shading due to indirect illumination (reflection and refraction effects due to reflective/translucid objects)
Shadow computation (hard shadows)
Image Rendering
xo
zo
Objects
Pixel(RGB)
Light
Illumination
yo
Camera
xe
ye
ze eye
Ray Casting
For each pixel in the viewport;
shoot a ray;
for each object in the scene
compute intersection ray-object;
store the closest intersection;
if there is an intersection
shade the pixel using color, lights, materials;
else /* ray misses all objects */
shade the pixel with background color
Shadows Only shades the intersection if not in shadow
Ray Casting
How does RT differ fromRasterization (slide from CS348b: image synthesis by Matt Pharr)
Global Illumination
Global Illumination or Light Transport
Light Transport and Shading The appearance of objects, only depends on
the way light interacts with matter and travels trough space.
Shading: Interaction light-matter
Light transport: determine and follow pathlight rays due to inter-reflections
Forward tracing aka Light Tracing
Backward Tracing
Backward Tracing
Ray-Tracing de Turner Whitted
Define the viewpoint, the view window and the viewport resolutionfor each pixel in the viewport{
compute a ray in World space from the eye towards the pixel;pixel_color = trace ( scene, eye, primary ray direction, 1);
}
• Backward Ray Tracer
• We trace light rays from the eye through a pixel in the viewport - primary rays
• ie we follow light beams in the reverse direction of the light propagation
• Find the intersection with the first object during the backwards trace of the ray
• The color of the ray (hence at the required pixel) is made up of 3 contributions:
-local color due to direct illumination (it can be in shadow) and ambient light ;
-color from a ray coming from the reflection direction – reflected ray;
- color from a ray coming from the refraction direction – transmitted or refratced ray;
• Shadow feelers, reflected and refracted rays are called secondary rays
Recursive nature
Algorithm’s recursive nature
R1
T1
R2
T2
R3
L1
L3
L2
R1
R2
L1
L3L2
T1
T2R3
Result
Mirror Maze
Mirror Maze
Color trace (Scene scene, Vector3d origin, Vector3d ray direction, int depth){
intersect ray with all objects and find a hit point (if any) closest to the start of the rayif (!intersection point) return BACKGROUND;else {
color = object material’s ambient color;compute normal at the hit point;for (each source light) {
L = unit light vector from hit point to light source;if (L • normal>0)
if (!point in shadow); //trace shadow raycolor += diffuse color + specular color;
}if (depth >= maxDepth) return color;
if (reflective object) {rRay = calculate ray in the reflected direction;rColor = trace(scene, point, rRay direction, depth+1);reduce rColor by the specular reflection coefficient and add to color; }
if (translucid object) {tRay = calculate ray in the refracted direction;tColor = trace(scene, point, tRay direction, depth+1);reduce tColor by the transmittance coefficient and add to color; }
return color;}
Rays
Parametric formulation: p = o + td
p: a point on the ray
o: origin of the ray
t: scalar parameter
d: unit vector giving the direction of the ray
The axes in the Camera Frame
Viewing direction
v
u
eyexe
ye
ze
ResX (pixels)width
Res
Y(p
ixel
s)h
eig
ht
viewing window-viewporttransformation
o xe
ye
t (top)
b (bottom)
l (le
ft)
r (r
igh
t)
Camera Position and Orientation
eye = viewerat = target point in the center of viewing window (near plane in OpenGL), up = up direction
World Coordinates
eyeat
fovy
upeye
at
up
x0
z0
y0
near
far
view = at - eye
Camera Frame - xe ye ze (uvn)
at
zeeye
up
z0
y0
x0
view
up
at
eye
z0
y0
x0
data:eye, at, up
view = at - eye
ze 1
eye ateye at
Camera Frame - xe ye ze (u v -n)
at
eye
up
z0
y0
x0
view
zexe
at
eye
up
z0
y0
x0
view
zexe
ye
e
e
e zupzup
x
1
eee xzy eye
up ze
xeye
at
Primary Rays
zw
xw eyeo
d pxy eye
yw
dop ttraio )(:
d
Computing Primary Rays
vuop ˆ)(ˆ)(1 yvxuxy
wsX
xxu
Re)(
hsY
yyv
Re)(
v
h
eexysY
yh
sX
xw yxop ˆ
Reˆ
Re1
(w/2, h/2)
uResX-1o1
pxy
10 2 x3
ResY-1
1
2
y
w
(-w/2, -h/2)
Ray at the left-bottom corner of the unit square pixel
Ray at the center of the square pixel
eexysY
yh
sX
xw yxop ˆ
Re
5.0ˆ
Re
5.01
for unit square pixels, ie. s=1;
Computing Primary Rays
zo
xo
eyeo yo
dop ttray )(:
d
eeefsX
xw
sY
yhd xyzd ˆ
2
1
Reˆ
2
1
Reˆ
d f eye at
Ray at the left-bottom corner of the square pixel
Camera Data in C
struct _Camera {
/* Camera definition*/
Vector eye, at, up;
float fovy;
float near,far; //hither and yon planes
int ResX,ResY;
float w,h;
Vector xe,ye,ze; //uvn frame
};
typedef struct _Camera Camera;
Camera* camCreate( Vector eye, Vector at, Vector up,
double fovy, double near, double far, int ResX, int ResY );
Ray camGetPrimaryRay( Camera camera, double x, double y );
The Camera object
ze 1
eye ateye at e
e
e zupzup
x
1
eee xzy
Initialization:
Ray in parametic form : o + td (normalize d; why?)
eyeo
d f eye at
h 2d f tanfov
2
w ResX
ResYh
Data input: fov, ResX, ResY, near, far, eye, at, up
Given: x, y
eeefsX
xw
sY
yhd xyzd ˆ
2
1
Reˆ
2
1
Reˆ
Ray at the left-bottom corner of the square pixel
Shadow Feeleers
No light at that point
ip
sr̂
sitt rpp ˆ)(:ray Shadow
Intersections computation
• Read the slides “Geometry Intersections”• Self-intersections problem due to floating-point precision (secondary rays and shadow-feelers) •Solution: Slightly offset intersections • Read the article : Andrew Woo et al., “It’s Really Not a Rendering Bug, You See…”, IEEE CG&A, September 1996, vol. 21
Reflection Model for Lambert Surfaces
luz incidente luz
incidenteluz incidente
1. Reflete igualmente em todas as direções2. Diffuse Intensity: linear variation with angle cos
Diffuse Reflection Component
cos
cos
cos
dbb
dgg
drr
b
g
r
kl
kl
kl
I
I
I
- /2 0 /2
cos
1
n̂
L̂
ip
Diffuse Reflection Component
I, l, k [0 ,1 ]
Eq. 1
LnLn
Ln
Ln
Ln
ˆˆˆˆ
ˆˆ
ˆˆ
ˆˆ
db
dg
dr
b
g
r
dbb
dgg
drr
dbb
dgg
drr
b
g
r
k
k
k
l
l
l
kl
kl
kl
kl
kl
kl
I
I
I
cos
cos
cos
dbb
dgg
drr
b
g
r
kl
kl
kl
I
I
I
Ambient plus Diffuse:
Ln ˆˆ
db
dg
dr
b
g
r
db
dg
dr
ab
ag
ar
b
g
r
k
k
k
l
l
l
k
k
k
I
I
I
I
I
I
Local Specular Reflection Component
I, l, k [0 ,1 ]
Eq.2
n̂
L̂
r̂
v̂
-1 -0,5 0 0,5 1
n=4
n=1
n=8
nvr ˆˆ
n
sbb
n
sgg
n
srr
especularb
g
r
kl
kl
kl
I
I
I
cos
cos
cos
Specular area
Local Specular Reflection Component
n
sb
sg
sr
b
g
r
db
dg
dr
b
g
r
db
dg
dr
ab
ag
ar
b
g
r
k
k
k
l
l
l
k
k
k
l
l
l
k
k
k
I
I
I
I
I
I
vrLn ˆˆˆˆ
Local Specular Reflection Component
n̂
L̂
r̂ v̂
ip
luzrefletida
Mirror Reflection Vector
LnnLr ˆˆˆˆ2ˆ
LLh n
h h
Ln
nnLL ˆ)ˆˆ( n
hLr nˆ
n̂
L̂ r̂
Phong Model: Local Reflection Distribution
nsluzdifluzamb kCkCCC vrLn ˆˆˆˆ
n̂
L̂
r̂v̂
ip
Ambient Diffuse Specular
Multiple Light Sources
luzes
n
sb
sg
sr
b
g
r
db
dg
dr
b
g
r
db
dg
dr
ab
ag
ar
b
g
r
k
k
k
l
l
l
k
k
k
l
l
l
k
k
k
I
I
I
I
I
I
vrLn ˆˆˆˆ
vnnvr ˆˆ)ˆˆ(2ˆ r
luzes
n
r
sb
sg
sr
b
g
r
db
dg
dr
b
g
r
db
dg
dr
ab
ag
ar
b
g
r
k
k
k
l
l
l
k
k
k
l
l
l
k
k
k
I
I
I
I
I
I
LrLn ˆˆˆˆ
LnnLr ˆˆ)ˆˆ(2ˆ
First Lab Exercise
Model with multiple lights and shadow
otherwise1
shadowin if0s
f
luzes
n
r
sb
sg
sr
b
g
r
db
dg
dr
b
g
r
s
db
dg
dr
ab
ag
ar
b
g
r
k
k
k
l
l
l
k
k
k
l
l
l
f
k
k
k
I
I
I
I
I
I
LrLn ˆˆˆˆ
Indirect Illumination: Mirror reflectionsfrom other objects
Reflective surface
n̂
v̂
iprr̂
ritt rpp ˆ)(:ray reflected
Indirect illumination: Mirror Reflection Vector
VVr nnr ˆˆ2ˆ
VVn h
h h
Vn
nn ˆ)ˆ( VVn
hr nVrˆ
n̂
Vrr̂
Translucid objects
1
2
h1
h2
n̂
v̂
ip
tr̂
ti ttrefratadoRaio rpp ˆ)(:
1
2
2
1
sin
sin
h
h
Snell law
n̂
t̂
t
t
vv
t1ˆ
Indirect Illumination: Refracted Ray
i
n̂ vnnvv ˆˆ)ˆˆ( t
v̂
ip
tr̂
nv
tvti vsin
i
t
it
h
h sinsin
tt 2sin1cos
)ˆ(cosˆsin ntr ttt
titt rpp ˆ)(:ray refracted
t
Advertência: Refração não é simples!
Objectos refletores e transparentes
redução da
transparência
redução da
reflexão
)(
)(
)(
)1(
)(
)(
)(
ˆˆˆˆ
tb
tg
tr
rb
rg
rr
luzes
n
r
sb
sg
sr
b
g
r
db
dg
dr
b
g
r
s
db
dg
dr
ab
ag
ar
b
g
r
I
I
I
o
I
I
I
k
k
k
k
l
l
l
k
k
k
l
l
l
f
k
k
k
I
I
I
I
I
I
r
r
r
r
r
r
LrLn
Texturas
u
v
1.0
1.0
0 u1.0
1.0
0
v
u1.0
1.0
0
v
Texturas 2D = Imagens onde o domínio é u, v [0,1]×[0,1] R2
Sistemas de coordenada de textura na caixa
u
v
u
v
x
y
z
u
v
(1,1)(0,0)
(0,0)
(0,0)
face x=xmax
minmax
min
yy
yyu
minmax
min
zz
zzv
Sistema de coordenadas de textura na esfera
x
y
z
sinsin
cossin
cos
y
x
z
z
yx
xy
22
tan
/tan
1
1
v
u
)1(
)1(
wuj
hvi
i
j
Sistema de coordenadas de textura no triângulo
u
v
(u1,v1)
(u2,v2)
(u3,v3)
u1= u3 u2
v1= v2
v3
1
2
3
Textura no triângulo e coordenadas baricêntricas
p1
v12
v23
v31
2/)(ˆ
2/)(ˆ
2/)(ˆ
1123
3312
2231
ppvn
ppvn
ppvn
i
i
i
A
A
A
A3
A1A2
321 AAAAT
T
T
T
AAL
AAL
AAL
/
/
/
33
22
11
n̂
p2
p3
int
ip
3
3
3
2
2
2
1
1
1v
uL
v
uL
v
uL
v
u
i
i
)(1 213 LLL
Textura
Texturas de rugosidade (bump textures) eTexturas de deslocamentos (displacement mapping)
Pertubar aleatoriamente
as normais dos objetos
n̂
),(ˆ'ˆ nn
Pertubar aleatoriamente
as posições dos pontos
Textura ambiente(environment maps)
xy
z
u
v
(0,0)
(1,1)
face dos raios x>y e x>z
x
xyu
2
x
xzv
2
y=-x
y=x
z=-x
z=x
Processamento anti-aliasing
•Lance um raio para cada sub-pixel•Faça uma média dos valores obtidos
dx, dy = variáveis aleatórias
dxdya sub-pixels
pixel
Anti-aliasing
(a) original (b) uniforme (c) “jittered”
pixel pixel pixel
Refinamento Progresivo
subdivisão finalsegunda subdivisão
primeira subdivisãoamostragem inicial
pixels sendo visitados pixels já visitados
Radiosidade e Ray Tracing
Klaus Mueller, Stony Brook University, Computer Science (CSE 564)
global illuminationstandard raytracing
Todays State of the Art- Some Snapshots
RT Acceleration
More efficient computations
Hierarchical data structures: Kd-Tree, BVH o Grids