Magdalena(Schwarzl 1 Magdalena(Schwarzl 1
Magdalena(Schwarzl 2 Magdalena(Schwarzl 2
Agenda
• Ray(tracing(algorithm(
• Code(structure(
• Ray(intersec;ons • Sphere • Triangle
• Hints • Transforma;ons • Debugging
• How(to(start
2
For each pixel: 1. Construct ray2. Test intersection with objects
→ Nearest intersection point visible on image3. Perform Shading
Basic algortihm
3
● Scene:Objects, lights, viewpoint, materials,...→ xml scene format, see course website
● Camera: For ray construction
● Image: 2D Array of Vec3's (for r,g,b), represents pixels
● Rays: For intersection tests
● Shading: Illumination models
Components
4
● Consists of origin + direction (both Vec3)
● Normalize direction!
● Every point p on ray = o+ λ*dir
Ray
o
dir
Object● Needs intersection routine, returning λ, normal, etc.
– Additional: Parameters for material properties,...
● For this lab: Spheres and triangle meshes
Scene
● Arrange objects for easy intersection (for example in a list)
5
List<SceneObjects> objectList
for (y=0; y<width; y++)
for (x=0; x<height; x++)
ray = camera.constructRayForPixel(x,y)
color = trace(ray,objectList)
image.setColor(x,y,color)
Implementation
iterate over all pixels
6
List<SceneObjects> objectList
for (y=0; y<width; y++)
for (x=0; x<height; x++)
ray = camera.constructRayForPixel(x,y)
color = trace(ray,objectList)
image.setColor(x,y,color)
trace(ray,objectList)
for(s=0; s < objectList.size(); s++)object = objectList.at(s)
result = object.intersect(ray)
//determine smallest result.t > 0!
if (no intersection) return backgroundColor
else return result.color
Implementation
List<SceneObjects> objectList
for (y=0; y<width; y++)
for (x=0; x<height; x++)
ray = camera.constructRayForPixel(x,y)
color = trace(ray,objectList)
image.setColor(x,y,color)
7
So far:
8
● Extend trace method to use local illumination models:
trace(ray,objectList)
for(s=0; s < objectList.size(); s++)
object = objectList.at(s)
result = object.intersect(ray)
//determine smallest result.t > 0!
if (no intersection)
return backgroundColor else
//determine intersection point
Vec3 iP = ray.origin + result.t*ray.direction Vec3 normal = result.normal
Vec3 light
Vec3 color = shade(ray, object, iP, normal, light) return color
Shading
9
● More than one light source:
trace(ray,objectList)
for(s=0; s < objectList.size(); s++)
object = objectList.at(s)
result = object.intersect(ray)
//determine smallest result.t > 0!
if (no intersection)
return backgroundColor else
//determine intersection point
Vec3 iP = ray.origin + result.t*ray.direction Vec3 normal = result.normal
Vec3 overallColor;
for (l = 0; l < lightsInScene; l++)
Vec3 light
overallColor += shade(ray, object, iP, normal, light) return overallcolor
Shading
10
● Test by creating shadow ray from intersection point to light source
● Intersect with all objects again
Shadows
→ Point is in shadow if not directly illuminated by light
if (intersection in front of light)
return darkColor
else
//perform shading as before
11
So far, only direct illumination:
12
So far, only direct illumination:
VS
Recursion needed!
13
For every intersection point:
→ Create new rays for reflection/refraction
→ Trace those rays!
→ Stop recursion after reaching max. bounces
Recursion
14
trace(ray,objectList, depth)
//perform intersection and shading as before...
if depth >= maxBounces
return overallColor
//depending on object's material properties,
cast new rays
if (object.transmittance > 0)
ray.origin = iP
ray.direction = calculateTransmittanceDirection() colorTrans = trace(ray,objectList, depth+1)
if (object.reflectance > 0)
ray.origin = iP
ray.direction = calculateReflectanceDirection() colorRefl = trace(ray,objectList, depth+1)
return finalColor = //combine colors
Recursion
stop after max bounces
15
● Coefficients for reflectance, transmittance are defined in scene file
● Reflectance + Transmittance + own Color = 1
Combine colors
finalColor =
r*reflectedColor +
t*transmittedColor +
(1-r-t)*ownColor
Magdalena(Schwarzl 3 Magdalena(Schwarzl 3
Agenda
• Ray(tracing(algorithm(
• Code(structure(
• Ray(intersec;ons • Sphere • Triangle
• Hints • Transforma;ons • Debugging
• How(to(start
Magdalena(Schwarzl 4 Magdalena(Schwarzl 4
Ray(intersec;on
A(ray(is(defined(as
P
!d
X = P +λ!d
Where(X(is(any(point(on(the(line(
Magdalena(Schwarzl 5
A(sphere(is(defined(as Where X is any point on the sphere
Sphere
r
C r2 = X −C( )2
Magdalena(Schwarzl 6
We(want(to(find(a(point(on(both Compute lambda
Intersect Ray-Sphere
r
C
r2 = X −C( )2
P
!d
S1
S2
X = P +λ!d
r2 = (P +λ!d )−C( )
2
r2 = (P +λ!d )−C( ) (P +λ
!d )−C( )
Magdalena(Schwarzl 7
Intersect Ray-Sphere
r2 = (P +λ!d )−C( ) (P +λ
!d )−C( )
0 = d 2λ 2 +λ 2P!d − 2C
!d( )+ P −C( )2 − r2
A = d 2 B = 2P!d − 2C
!d( ) C = P −C( )2 − r2
λ1,2 =−B± B2 − 4AC
2A
Magdalena(Schwarzl 8
Intersect Ray-Sphere
λ1,2 =−B± B2 − 4AC
2A
Magdalena(Schwarzl 9
Intersect Ray-Sphere
λ1,2 =−B± B2 − 4AC
2A
>(0(
two(intersec;on(points(
=(0(
tangent(<(0(
no(intersec;on(
r C
P
!d
S1
S2
r C
P
!d
r C
P
!d
S1
Magdalena(Schwarzl 10
A(triangle(is(defined(as Where X is any point inside the triangle
Barycentric coordinates
V1
V2
V3
e13
e12
e23
Magdalena(Schwarzl 11
A(triangle(is(defined(as Where X is any point inside the triangle
Barycentric coordinates
V1
V2
V3
e13
e12
e23
1−u− v = A1A1 + A2 + A3
u = A2A1 + A2 + A3
v = A3A1 + A2 + A3
X
A3
A1 A2
Magdalena(Schwarzl 12
A(triangle(is(defined(as Where X is any point inside the triangle
Barycentric coordinates
V1
V2
V3
e13
e12
e23
1−u− v = A1A1 + A2 + A3
u = A2A1 + A2 + A3
v = A3A1 + A2 + A3
X
A3
A1 A2
X u,v( ) = 1−u− v( )V1 +uV2 + vV3 u,v ≥ 0u+ v ≤1
Magdalena(Schwarzl 13
A triangle is defined by: 3 points define a plane:
Triangle
V1
V2
V3
e13
e12
e23 X u,v( ) = 1−u− v( )V1 +uV2 + vV3
u,v ≥ 0u+ v ≤1
Magdalena(Schwarzl 14
A triangle is defined by: 3 points define a plane:
Triangle
n = e13 × e12e13 × e12
nX = nV1
V1
V2
V3
e13
e12
e23 X u,v( ) = 1−u− v( )V1 +uV2 + vV3
u,v ≥ 0u+ v ≤1
Magdalena(Schwarzl 15
We(want(the(intersec;on Compute lambda
Intersect Ray-Triangle
V1
V2
V3
e13
e12
e23
P
!dX = P +λ
!dnX = nV1
n P +λ!d( ) = nV1
S1 X u,v( ) = 1−u− v( )V1 +uV2 + vV3
Magdalena(Schwarzl 16
Intersect Triangle-Sphere
λ =nV1 − nPn!d
1) ray-plane
Magdalena(Schwarzl 17
Intersect Triangle-Sphere
λ =nV1 − nPn!d
1) ray-plane != 0 One intersection
= 0 No intersection with plane
parallel Inside plane
S1 = P +λ!d
S1
Magdalena(Schwarzl 18
Intersect Ray-Triangle
2) Inside triangle?
P +λ!d = 1−u− v( )V1 +uV2 + vV3
Use x,y,z to get linear system of 3 equations Solve for v & u
Magdalena(Schwarzl 19
Intersect Ray-Triangle
2) Inside triangle?
P +λ!d = 1−u− v( )V1 +uV2 + vV3
Use x,y,z to get linear system of 3 equations Solve for v & u
intersection no intersection
u,v <=1 AND u,v >=0
u,v > 1 OR u,v < 0
Magdalena(Schwarzl 20 Magdalena(Schwarzl 20
Agenda
• Ray(tracing(algorithm(
• Code(structure(
• Ray(intersec;ons • Sphere • Triangle
• Hints • Transforma;ons • Debugging
• How(to(start
Magdalena(Schwarzl 21
Transformations
Think about transformations early!
Magdalena(Schwarzl 22
Transformations
Think about transformations early!
Two approaches: • transform object • inverse transform ray
Precompute inv.-transfomations No translation on vectors Inverse-transpose for normals
Magdalena(Schwarzl 23
Precision
Problems?
Magdalena(Schwarzl 24
Precision
Problems? Shadow ray hit its source (collision at distance ==0) • Offset the ray cast source
a small amount in the direction of the ray.
• Do never test for == 0 Test for between 0±ε
Magdalena(Schwarzl 25
Normals, Texures
Visual debugging Output textures, normals, reflection vectors as color to see problems
0±ε
Magdalena(Schwarzl 26 Magdalena(Schwarzl 26
Agenda
• Ray(tracing(algorithm(
• Code(structure(
• Ray(intersec;ons • Sphere • Triangle
• Hints • Transforma;ons • Debugging
• How(to(start
Magdalena(Schwarzl 27
How to start
• Plan code structure
• Classes, methods,… • Decide on libraries
• xml-parsing, images • Error messages!! • Create a black image • Read in xml scene • Compute intersections
• Use only object color • Compute shading
• ambient, diffuse, specular
• Test on PC labs • Include readme!!
• compile command • If it might take longer ;)
Magdalena(Schwarzl 28
Questions
Any questions?