magdalena(schwarzlvda.univie.ac.at/teaching/graphics/15w/lecture... · magdalena(schwarzl 2 agenda...

42
Magdalena Schwarzl 1 Magdalena Schwarzl 1

Upload: others

Post on 23-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 1 Magdalena(Schwarzl 1

Page 2: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 2 Magdalena(Schwarzl 2

Agenda

•  Ray(tracing(algorithm(

•  Code(structure(

•  Ray(intersec;ons •  Sphere •  Triangle

•  Hints •  Transforma;ons •  Debugging

•  How(to(start

Page 3: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

2

For each pixel: 1. Construct ray2. Test intersection with objects

→ Nearest intersection point visible on image3. Perform Shading

Basic algortihm

Page 4: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 5: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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)

Page 6: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 7: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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)

Page 8: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

7

So far:

Page 9: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 10: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 11: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 12: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

11

So far, only direct illumination:

Page 13: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

12

So far, only direct illumination:

VS

Recursion needed!

Page 14: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

13

For every intersection point:

→ Create new rays for reflection/refraction

→ Trace those rays!

→ Stop recursion after reaching max. bounces

Recursion

Page 15: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 16: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 17: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 3 Magdalena(Schwarzl 3

Agenda

•  Ray(tracing(algorithm(

•  Code(structure(

•  Ray(intersec;ons •  Sphere •  Triangle

•  Hints •  Transforma;ons •  Debugging

•  How(to(start

Page 18: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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(

Page 19: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 5

A(sphere(is(defined(as Where X is any point on the sphere

Sphere

r

C r2 = X −C( )2

Page 20: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 21: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 22: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 8

Intersect Ray-Sphere

λ1,2 =−B± B2 − 4AC

2A

Page 23: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 24: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 10

A(triangle(is(defined(as Where X is any point inside the triangle

Barycentric coordinates

V1

V2

V3

e13

e12

e23

Page 25: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 26: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 27: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 28: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 29: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 30: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 16

Intersect Triangle-Sphere

λ =nV1 − nPn!d

1) ray-plane

Page 31: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 32: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 33: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

AND0<=u+v <=1
Page 34: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 20 Magdalena(Schwarzl 20

Agenda

•  Ray(tracing(algorithm(

•  Code(structure(

•  Ray(intersec;ons •  Sphere •  Triangle

•  Hints •  Transforma;ons •  Debugging

•  How(to(start

Page 35: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 21

Transformations

Think about transformations early!

Page 36: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 37: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 23

Precision

Problems?

Page 38: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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±ε

Page 39: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 25

Normals, Texures

Visual debugging Output textures, normals, reflection vectors as color to see problems

0±ε

Page 40: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 26 Magdalena(Schwarzl 26

Agenda

•  Ray(tracing(algorithm(

•  Code(structure(

•  Ray(intersec;ons •  Sphere •  Triangle

•  Hints •  Transforma;ons •  Debugging

•  How(to(start

Page 41: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

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

Page 42: Magdalena(Schwarzlvda.univie.ac.at/Teaching/Graphics/15w/Lecture... · Magdalena(Schwarzl 2 Agenda • Ray(tracing(algorithm(• Code(structure(• Ray(intersec;ons • Sphere •

Magdalena(Schwarzl 28

Questions

Any questions?