Ray Tracing

Local illumination
Ray Tracing
only direct
illumination
Ll
pixel
Balázs Csébfalvi
L (V) ≈ Le(V)+Σl rl Ll (Ll) fr (Ll ,V) cos θ’l
Department of Control Engineering and
Information Technology
email: [email protected]
Web: http://www.iit.bme.hu/~cseb
0/1: visibility
of the light source
Ambient term
Local illumination
illumination from spot or
area light sources
2 /25
Abstract light source models
+ ambient term
n
n
Directional light source : illumination into one direction, the
light rays are parallel, the intensity does not depend on the
position
Positional light source: illumination from one single point,
the intensity decreases by the squared distance
d
L (V) ≈ Le(V)+Σl rl Ll (Ll) fr (Ll ,V) cos θ’l + ka La
directional
positional
3 /25
Local illumination algorithms
4 /25
Visibility from the eye
pixel
pixel
ray(t) = eye + v ·t, t > 0
n
n
n
Visibility calculation from the eye position
Visibility of the light sources is calculated from the
visible points
Light coming from the light source is reflected to the
viewing direction: surface normal is needed
5 /25
FirstIntersect(ray ð t, iobject, x)
t = FLT_MAX;
FOR each object
tnew = Intersect( ray, object ); // < 0 if no intersection
IF (tnew > 0 && tnew < t ) t = tnew, iobject = object
ENDFOR
IF (t < FLT_MAX) x = eye + v ·t;
RETURN (t, iobject, x);
RETURN „no intersection”
END
6 /25
1
Quadratic surfaces
Sphere/ray intersection calculation
|r - center|2 = R2
x
Quadratic surfaces: [x,y,z,1] A y = 0
z
1
r
R
center
no root
|ray(t) - center |2 = R2
1 root
Second-degree
equation
2 roots
ray(t) = eye + v ·t, t > 0
2
(v ·v) t + 2 ((eye -center) ·v) t +((eye -center) ·(eye -center))- R2 = 0
Wanted: minimal positive solution
Surface normal: (ray(t) - center)/R
7 /25
Ellipsoid
x2 + 
y2 + 
z2 -1=0

a2 b2 c2
Triangle/ray intersection
Infinite cone
x2 y2 2
2 + 
- z =0
a b2
Infinite cylinder
x2 + 
y2 - 1 =0

a2 b2
Shading normals
r3
N2
N
N1
p
N3
N = A X + B Y+C
(ray(t) - r1) ·n = 0, t > 0
n = (r2 - r1) x (r3 - r1)
N1 = A X1 + B Y1 + C
N2 = A X2 + B Y2 + C
N3 = A X3 + B Y3 + C
2. Is the intersection point inside the triangle ?
((r2 - r1) x (p - r1)) ·n > 0
surface normal: n
((r3 - r2) x (p - r2)) ·n > 0
or „shading normals”
9 /25
((r1 - r3) x (p - r3)) ·n > 0
Parametric surfaces
Solving the linear equation:
u,v,t
Test:
0< u,v < 1,
t>0
A, B, C
3 unknowns
3 linear
equations
10 /25
Images obtained by ray casting
r(u,v), u,v in [0,1]
ray(t) = eye + v ·t, t > 0
r(u,v) = ray(t)
(X1 , Y1 , Z1 )
r2
r1
r1
1. Plane:
normal:
8 /25
r(0,0)
Local
illumination
r(0,0.5)
r(0.5,0)
r(1,0)
r(0,1)
r(0.5,0.5)
Local
illumination
+
shadows
r(1,1)
Recursive tesselation
11 /25
12 /25
2
Recursive ray tracing
Main steps of recursive ray tracing
Refracted rays
Reflected rays
Ll
Shadow rays
L (V) ≈ Le(V)+Σl rl Ll (Ll) fr (Ll ,V) cos θ’l + ka La
+ kr Lin (R) + kt Lin(T)
light coming from the
reflected direction
light coming from the
refracted direction
Light coming from a pixel to the eye
• Surface point visible in the given direction and
its normal vector
• Light sources visible from the given point
• Reflection/refraction directions in the given point
• Light coming from the reflection direction
• Light coming from the refraction direction
Recursion
• Summation of the secondary
ray contributions
13 /25
Ray tracing
14 /25
Ray tracing: function Trace
Color Trace ( ray )
ray
ray
p
Render( )
for each pixel p
Ray r = ray( eye ð pixel p )
color = Trace(ray)
WritePixel(p, color)
endfor
end
p color
15 /25
x
16 /25
DirectLightsource
Ray tracing: function Trace
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 += DirectLightsource(x, -ray.dir)
IF ( kr > 0 ) THEN
ReflectDir( ray, reflected ray)
color += k r · Trace( reflected ray, d + 1)
ENDIF
IF ( kt > 0 && RefractDir( ray, refracted ray ) )
color += kt · Trace( refracted ray, d + 1)
ENDIF
RETURN color
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 += k r · Trace( reflected ray )
ENDIF
IF ( kt > 0 && RefractDir( ray, refracted ray ) )
color += kt · Trace( refracted ray )
ENDIF
RETURN color
ray
x
DirectLightsource ( x, vdir )
color = ka La
shadow
FOR each lightsource l DO
shadowray = x to lightsource[l]
(t,y) = FirstIntersect( shadowray );
IF (t < 0 || |x-y| > |x-lightsource[l].pos|)
color += Brdf(ldir, x, vdir) cos θ l' lightsource[l].Intensity
ENDIF
ENDFOR
RETURN color
pixel
x
17 /25
y
18 /25
3
Ray tracing program
Images obtained by ray tracing
typedef struct{double x,y,z}vec;vec U,black,amb ={.02,.02,.02}; struct sphere{ vec cen,color;double rad,kd,ks,kt,kl,ir}*s,
*best,sph[]={0.,6.,.5,1.,1.,1.,.9, .05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,
.8,1.,7.,0.,0.,0.,.6,1.5,-3.,-3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5,};yx; double u,b,tmin,sqrt(),tan();doublevdot(A,B)vecA ,B;
{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B; {B.x+=a* A.x;B.y +=a*A.y;B.z+=a*A.z;return B;}
vec vunit(A)vec A;{return vcomb(1./sqrt( vdot(A,A)),A,black);}struct sphere *intersect(P,D)vecP,D;{best=0;tmin=1e30;
s= sph+5;while(s-->sph)b=vdot(D,U=vcomb(-1.,P,s ->cen)),u=b*b-vdot(U,U)+s ->rad*s ->rad,u =u>0?sqrt(u):1e31,u=b-u>
1e-7?b-u:b+u,tmin=u>=1e-7&&u<tmin?best=s,u: tmin;return best;}vec trace(level,P,D)vecP,D;{double d,eta,e;vec N,color;
struct sphere*s,*l;if(!level--)return black;if(s=intersect(P,D));else return amb;color=amb;eta=s->ir;d= -vdot(D,N=vunit(vcomb
(-1.,P=vcomb(tmin,D,P),s ->cen )));if(d<0)N=vcomb(-1.,N,black),eta=1/eta,d= -d;l=sph+5;while(l-->sph)if((e=l ->kl*vdot(N,
U=vunit(vcomb(-1.,P,l ->cen))))>0&&intersect(P,U)==l)color=vcomb(e ,l->color,color);U=s->color;color.x*=U.x;color.y*=
U.y;color.z*=U.z;e=1-eta* eta*(1-d*d);return vcomb(s->kt,e>0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt (e),N,black))):
black,vcomb(s->ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s->kd, color,vcomb(s->kl,U,black))));}
main(){printf("%d %d\n",32,32);while(yx<32*32) U.x=yx%32-32/2,U.z=32/2-yx++/32,U.y=32/2/tan(25/114.5915590261),
U=vcomb(255., trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}/* minray!*/
Computational cost ˜ #pixels x #objects x #light sources
19 /25
Bounding volumes
20 /25
Space-partitioning methods
objects
preprocessing
spacepartitioned data
double IntersectBV( ray, object )
// < 0 if no intersection
IF ( Intersect( ray, bounding volume of object) < 0) RETURN -1;
RETURN Intersect( ray, object );
END
21 /25
Regular space partitioning
Space-partitioned data structure:
• For a given ray decreases the number of the
potentially intersected objects
• If a ray hits a potentially intersected object then
most of the other objects do not have to be checked
ray tracing
first
hit point
22 /25
Octree
Preprocessing:
For each cell find the intersected objects
complexity: O(n ·c) = O(n 2)
Ray tracing:
FOR each cell of the line // line drawing
Intersection with the objects of the cell
IF intersection RETURN
ENDFOR
average complexity: O(1 )
23 /25
BuildTree( cell ):
IF a cell contains few objects THEN
register the objects
ELSE
cell subdivision: c1, c2, …, c8
BuildTree(c1); … BuildTree(c8);
ENDIF
2
1
1
3
2
Octree
3
3
Ray Tracing:
FOR each cell intersected by the ray
Intersected objects in the cell
IF intersection RETURN
ENDFOR
24 /25
4
Binary space-partitioning tree (kd-tree)
2
1
3
1
2
kd-tree
3
BuildTree( cell ):
IF a cell contains few objects THEN
register the objects
ELSE
find a subdivision plane
cell subdivision by the plane : c1, c2
BuildTree(c1); BuildTree(c2);
ENDIF
Ray Tracing:
FOR each cell intersected by the ray
Intersected objects in the cell
IF intersection RETURN
ENDFOR
25 /25
5