The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information...

41
The Fundamentals of The Fundamentals of Ray-Tracing Ray-Tracing Szirmay-Kalos László of Control Engineering and Information Techn Budapest University of Technology email: [email protected] Web: http://www.iit.bme.hu/~szirmay

Transcript of The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information...

Page 1: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 2: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

RenderingRendering

Tone mapping

Real worldcolor

pixel Virtual world

Page 3: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 4: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

BRDF modellingBRDF modelling

Depends on – the wavelength– light direction– viewing direction– surface normal

Physical measurements Tuning simple formulae

Page 5: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Diffuse reflectionDiffuse reflection

Light intensity is independent of the viewing direction

’ Lref = Lir kd () cos ’

Lambert’s law

Page 6: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Diffuse surfacesDiffuse surfaces

Page 7: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Phong’s specular reflectionPhong’s specular reflection

’r

’r

= diffuse +

Function: large at =0 and goes to zero ks cosn

Page 8: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Diffuse+Phong materialsDiffuse+Phong materials

5 10 20 50n=

diffuse

Phong

diffuse +Phong

own color

Plastics:

ks is white

Page 9: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Diffuse + Phong materialsDiffuse + Phong materials

Page 10: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

General rendering problemGeneral rendering problem

L

Le

r

t

Page 11: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Local IlluminationLocal Illumination

pixel

L () Le()+lrlLl (’l) fr (’l ,) cos ’l

Point, directional light sources

Light sources

0/1: Light source visibilityShadows

’l

Page 12: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Role of the ambient termRole of the ambient term

L () Le()+lrlLl (’l) fr (’l ,) cos ’l + ka La

Local illumination + ambient term

Page 13: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 14: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Localillumination

Localillumination+shadows

Ray-tracing

Globalillumination

Photorealistic image synthesis

Page 15: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 16: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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)

Page 17: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 18: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 19: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

+ + -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

Page 20: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 21: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 22: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Parametric surfacesParametric surfaces

r(u,v) = Bi,j (u,v) ri, j

blending functions

control points

Page 23: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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)

Page 24: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Animated objectsAnimated objects

T1

T2

T1-1

T2 -1

Page 25: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 26: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 27: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 28: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Camera definitionCamera definition

eyelookat

fov,aspect

XRES, YRES

Page 29: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 30: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 31: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 32: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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( )

Page 33: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Ray-tracing: resultsRay-tracing: results

Computation time Pixel# · Object# ·(Light source# + 1)

PovRay

Page 34: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Speeding up the intersection Speeding up the intersection calculationcalculation

Speeding up for each objects:

– bounding volumes Computing for less number of objects:

– space partitioning

Page 35: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 36: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 37: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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)

Page 38: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 39: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

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

Page 40: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Example 1Example 1

Page 41: The Fundamentals of Ray-Tracing Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: szirmay@iit.bme.hu.

Example 2Example 2