The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information...
-
Upload
myrtle-oconnor -
Category
Documents
-
view
212 -
download
0
Transcript of The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information...
The Fundamentals of The Fundamentals of Ray-TracingRay-Tracing
Szirmay-Kalos LászlóDept. of Control Engineering and Information Technology
Budapest University of Technologyemail: [email protected]
Web: http://www.iit.bme.hu/~szirmay
RenderingRendering
Tone mapping
Real worldcolor
pixel Virtual world
Light-object interactionLight-object interactionLin
kr Lin
kt Lin
Lin
Lin fr(’,) cos’
Coherent termsgeometric optics
Incoherent termsprobability theory
’ ’
BRDF: fr(’,) = Lout/Lin cos’ Bi-directional reflection distribution function
prob. density
BRDF modellingBRDF modelling
Depends on – the wavelength– light direction– viewing direction– surface normal
Physical measurements Tuning simple formulae
Diffuse reflectionDiffuse reflection
Light intensity is independent of the viewing direction
’
’ Lref = Lir kd () cos ’
Lambert’s law
Diffuse surfacesDiffuse surfaces
Phong’s specular reflectionPhong’s specular reflection
’
’r
’
’r
= diffuse +
Function: large at =0 and goes to zero ks cosn
Diffuse+Phong materialsDiffuse+Phong materials
5 10 20 50n=
diffuse
Phong
diffuse +Phong
own color
Plastics:
ks is white
Diffuse + Phong materialsDiffuse + Phong materials
General rendering problemGeneral rendering problem
L
Le
r
t
Local IlluminationLocal Illumination
pixel
L () Le()+lrlLl (’l) fr (’l ,) cos ’l
Point, directional light sources
Light sources
0/1: Light source visibilityShadows
’l
Role of the ambient termRole of the ambient term
L () Le()+lrlLl (’l) fr (’l ,) cos ’l + ka La
Local illumination + ambient term
Ray-tracingRay-tracing
Llshadow ray
Reflection ray
Refracted ray
L() = Le()+lrlLl (’l) fr (’l ,) cos ’l + ka La
+ + kr Lin (r) + kt Lin(t)
from the reflection direction
from the refraction direction
Localillumination
Localillumination+shadows
Ray-tracing
Globalillumination
Photorealistic image synthesis
Steps of Steps of ray-tracingray-tracing
Light intensity coming through a pixel– Visible point and the normal vector here– Visible light sources from here– mirror and refraction directions– Light intensity coming from the reflection direction– Light intensity coming form the refraction direction– Adding up the light components and
color the pixel Recursion
shadow ray
Reflection ray
Refracted ray
Tasks of ray-tracingTasks of ray-tracing Visibility computation
– Determination of that point which is visible from a point in a given direction + normal vector here
– point+direction = ray Computation of the mirroring and refraction
directions
Evaluation of the illumination formula L() = Le()+lrlLl (’l) fr (’l ,) cos ’l + ka La
+ + kr Lin (r) + kt Lin(t)
Visibility calculationVisibility calculation
ray(t) = eye + v ·t, t > 0
float FirstIntersect(ray iobject, x) // <=0 if no intersection t = -1 FOR each object
tnew = Intersect( ray, object ); // <= 0 if no intersectionIF (tnew > 0 && tnew < t) t = tnew, iobject = object
ENDFOR IF (t > 0) x = eye + v ·t; RETURN t;END
x
Intersection with a SphereIntersection with a Sphere
|ray(t) - center |2 = R2
Surface normal: (ray(t) - center)/R
centerR
|r - center |2 = R2
r
(v ·v) t 2 + 2 ((eye-center) ·v) t +((eye-center) ·(eye-center))- R2 = 0
Wanted: minimum from the positive solutions
no root 1 root 2 roots
+ + -1=0
Quadric surfacesQuadric surfacesxyz1
[x,y,z,1] A = 0Quadric surface: Second orderequation
Ellipsoid x2 y2 z2
a2 b2 c2 + - z2 =0
Infinite cone x2 y2
a2 b2
Infinite cylinder x2 y2
a2 b2 + - 1 =0
TriangleTriangle
1. Intersection with the plane: (ray(t) - r1) ·n = 0, t > 0Normal vector: n = (r2 - r1) x (r3 - r1)
2. Is the intersection point p inside the triangle? ((r2 - r1) x (p - r1)) · ((r2 - r1) x (r3 - r1)) > 0 ((r3 - r2) x (p - r2)) · ((r3 - r2) x (r1 - r2)) > 0 ((r1 - r3) x (p - r3)) · ((r1 - r3) x (r2 - r3)) > 0
r1 r1 r2
p
r3
n
Phase 1 Phase 2
Shading normalsShading normals
N1N2
N3
N
N = A X + B Y + C
(X1 , Y1 , Z1 )
N1 = A X1 + B Y1 + C
N2 = A X2 + B Y2 + C
N3 = A X3 + B Y3 + C
3 systems of equations of 3 unknowns
A, B, C
Parametric surfacesParametric surfaces
r(u,v) = Bi,j (u,v) ri, j
blending functions
control points
Intersecting parametric sufacesIntersecting parametric sufaces
r(u,v), u,v in [0,1]ray(t) = eye + v ·t, t > 0
r(u,v) = ray(t)
Solution of the equation:u,v,t
Test:0< u,v < 1, t > 0 Recursive tessellation
r(0.5,0.5)
r(0,0)
r(0,1)
r(1,1)
r(1,0)
r(0,0.5)
r(0.5,0)
Animated objectsAnimated objects
T1
T2
T1-1
T2 -1
Tasks of ray-tracingTasks of ray-tracing Visibility computation
– Determination of that point which is visible from a point in a given direction + normal vector here
– point+direction = ray Computation of the mirroring and refraction
directions
Evaluation of the illumination formula
Reflection DirectionReflection Direction
vr = v + 2 cos N
vrv
v + Ncos
v + NcosN
Ncos L = vr, V= v
ReflectDir( L, N, V ) { L = V - N * (N * V) * 2;}cos = - (v·N)
horizontal vector new ray = x + vr ·t
x
Refraction DirectionRefraction Direction
vt
v
v +NcosN
NNsin
-Ncos
vt = vn + N (cosn -(1-(1-cos2 )/n2))
n =sin sin
Snelliuslaw
vNcossinN =
vt = N sin Ncos
horizontal vector
vt = vn + N(cosn cos)
cos1-sin21-sin2/n2
Camera definitionCamera definition
eyelookat
fov,aspect
XRES, YRES
Ray-tracing algorithmRay-tracing algorithm
Render( ) for each pixel p
Ray r = ray( eye pixel p ) color = Trace(ray)
WritePixel(p, color) endforend
ray
color
p
p
Trace functionTrace functionColor Trace( ray ) IF (FirstIntersect(ray object, x) <= 0)
RETURN La
ENDIF color = Le (x, -ray.dir)
color += Direct Lightsource(x, -ray.dir) IF ( kr > 0 ) THEN
ReflectDir( ray, reflected ray)color += kr · Trace( reflected ray )
ENDIF IF ( kt > 0 && RefractDir( ray, refracted ray ) )
color += kt · Trace( refracted ray ) ENDIFRETURN color
Color Trace( ray, d) IF d > dmax THEN RETURN La
IF ( FirstIntersect(ray object, x) <= 0) RETURN La
ENDIF color = Le (x, -ray.dir)
color += Direct Lightsource(x, -ray.dir) IF ( kr > 0 ) THEN
ReflectDir( ray, reflected ray)color += kr · Trace( reflected ray, d+1 )
ENDIF IF ( kt > 0 && RefractDir( ray, refracted ray ) )
color += kt · Trace( refracted ray, d+1 ) ENDIFRETURN color
ray
x
DirectLightsource functionDirectLightsource functionDirectLightsource( x, vdir ) color = ka La
FOR each lightsource l DOshadowray = x to lightsource[l]t = FirstIntersect( shadowray );IF (t < 0 || t > |x - lightsource[l].pos|) color += Brdf(ldir, x, vdir) cos l' lightsource[l].IntensityENDIF
ENDFORRETURN color
shadow
pixel
xvdir
Ray-tracer class diagramRay-tracer class diagram
SceneObject
Sphere Mesh
Primitive( )
Intersect(Ray)Normal( )
Intersect(Ray)Normal( )
DefineRenderFirstIntersectDirectLightsourceTrace(Ray)
Light
Dir(Point)Intens(Point)
Latransform Le, pos
CameraXRES, YRESeye, lookat, vupfov, aspect
GetXRes( )GetYRes( )GetRay(int,int)
Ray
Dir( )Start( )
dir, start
MaterialLe, kd, ks, shine kr, kt, n
BRDFReflectDirRefractDir
Center, radius vertices[ ]
Primitive
Intersect(Ray)Normal( )
Ray-tracing: resultsRay-tracing: results
Computation time Pixel# · Object# ·(Light source# + 1)
PovRay
Speeding up the intersection Speeding up the intersection calculationcalculation
Speeding up for each objects:
– bounding volumes Computing for less number of objects:
– space partitioning
Bounding VolumeBounding Volume
float IntersectBV( ray, object ) // < 0 ha nincs IF ( Intersect( ray, bounding volume of object) <=0) RETURN -1; RETURN Intersect( ray, object );END
Space partitioning methodsSpace partitioning methods
Data structure:• Knowing the ray, it reduces the number of objects the
ray may intersect.• If we find an intersected object, the search can be
terminated.
pre-processing
objects
Space-partitioningdata structure
ray-tracing
Firstintersection
Ray-tracing: FOR each cell of the line // line drawing
Intersect objects registered in this cellIF intersection exists RETURN
ENDFOR
average case complexity: O(1 )
Uniform space partitioningUniform space partitioningPreprocessing: FOR each cell identify the objects that are partly here
complexity: O(n ·c ) = O(n 2)
Ray-shooting: FOR each cell intersecting the line Intersect objects registered in this cell IF intersection exists RETURN ENDFOR
OctreeOctree
2
1 3
Octree
Tree build IF a cell contains just a few objects
register objects in this cell ELSE
subdivide cells: c1, c2, …, c8Tree build(c1); … Tree build(c8);
ENDIF
12
3 3
Ray-shooting: FOR each cell intersecting the line
Intersect with the included objectsIF intersection exists RETURN
ENDFOR
Kd (BSP) Kd (BSP) treetree
kd-tree
2
1 3
Tree build: IF a cell contains just a few objects
register objects in this cell ELSE
find a subdivision planecell halving with the plane: c1, c2Tree build (c1); Tree build (c2);
ENDIF
12 3
Example 1Example 1
Example 2Example 2