Docsity

Programming - Introduction to Computer Graphics - Lecture Slides, Slides of Computer Graphics

Toggle sidebar
bg1
1
Programming 4 Tips
and Advanced Ray
Tracing
Programming
Assignment 4
•Build a ray tracer
•Components:
•Handle file output
•You will be storing your images to disk
•Generate ray casted images
•Generate ray traced images
Ray Caster Overview
For Each
Pixel
Camera Ray
Generates
Linked List of Objects
Sphere
Plane
Etc.
App Camera
Matrix
Test For
Closest
Sphere
Closest Object
Linked List of Materials
Material #1
Material #2
Linked List of Lights
Ambient #1
Point #1
Point #2
Shade()
Material #2
Shade()
Surface Material
Illuminated
By
Pixel Color
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Programming - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!

Programming 4 Tips

and Advanced Ray

Tracing

Programming

Assignment 4

• Build a ray tracer

• Components:

• Handle file output

• You will be storing your images to disk

• Generate ray casted images

• Generate ray traced images

Ray Caster Overview

For Each Pixel

Camera Ray

Generates

Linked List of Objects

Sphere

Plane

Etc.

App CameraMatrix

Test For Closest

Sphere Closest Object

Linked List of Materials

Material #

Material #

Linked List of Lights

Ambient #

Point #

Point #

Shade()

Material #

Shade()

Surface Material

Illuminated By

Pixel Color

Ray Tracer Overview

For Each Pixel

Camera Ray

Generates

Linked List of Objects

Sphere

Plane

Etc.

App Camera Matrix

Test For Closest

Sphere Closest Object

Linked List of Materials

Material #

Material #

Linked List of Lights

Ambient #

Point #

Point #

Shade()

Material #

Shade()

Surface Material

Illuminated By

Pixel Color

Cast More Rays

Transform Ray

What I will give you

• FSF image format specification

• FSF Viewer

• Matrix / Vector / Ray classes

• .ray file format specification

• Some sample .ray files

• All available on the website

Expected Raytracing

Results

reflect.ray

I’d like to put your

mind at ease...

• I know some (alright, probably all) of

you are worried about this assignment

• I am now going to try to convince you not to

worry as much

Why this assignment

shouldn’t scare you

• Unlike OpenGL,

• Don’t have to handle user input

• Don’t have to have a moving camera or

moving objects

• Don’t have to manage graphics state

• All computation is just on the CPU

Why this assignment

shouldn’t scare you

• Unlike previous assignments, this one

(for better or worse) is pretty

straightforward

• Just getting the ray tracer working is already

good for full credit

• Don’t need to do a bunch of different

programs

• Test cases are readily available

Inverting Composed

Transforms

• Answer: Yes!

• Lucky for us,

• Note that the order of transforms gets

reversed

• Now the operator that gets applied first is the

leftmost

Transforming Rays

• The point of origin is a point, so it gets

transformed as a homogeneous point

• The direction is a vector, so it gets

transformed as a vector

Untransformed ray:

Ray equivalent to the transform

M being applied to the world:

r (t) = S + t V r’ (t) = M -1 S + t M -1 V

Object Intersections

with Transformed

Rays

• Once the ray is transformed, just

intersect it with your canonical objects

as normal

• The resulting t value can be plugged

into the original untransformed ray to

find the point of intersection in world

space

Caution: Do not normalize the vector in the ray

after transformation r’ , or else values of t will not

be comparable to each other

You didn’t really think

it would be that

• That gets us the new ray to the eyeeasy...

• But that isn’t the only thing we need for

shading

• What about the normal vector?

• Normals do not remain “normal” after

transformation

Finding the New

Normal Vector

• So, in the end, the new normal vector is

given by

• N ’ = ( M -1)T N

• Since we already know how to compute

the inverse of the transform matrix

• All that is left to do is transpose it!

Putting it All Together:

Applying Ray

• For each ray-object intersectionTransforms

• Apply the inverse of any object transforms to

the ray

• Intersect the resulting ray with the canonical

object

• If there is a valid intersection

• Plug t into the original ray equation to get the

location of the intersection in world space

• Get the correct normal as shown on the last

slide

Re-Intersection

Illustration

A ray tracer without any re-intersection handling

Why does this

happen?

• Short answer: numerical precision issues

• Sequences of floating point multiplies

(accumulated in our transforms) result in small inaccuracies

• It is essentially random whether a ray from

any given point will work correctly (because the point is at t=0 or just behind it) or fail (because the point is at t>0)

• Note that this is a problem for shadow

rays too

Re-Intersection

Solutions

• Solution

• Simply do not allow intersections for values of t

< ε

• Where ε is a very small number, like.

• Solution

• When a new ray is generated, offset it’s origin

point by ε in the direction of the surface normal

Today

• Talking about advanced ray tracing

techniques

• Acceleration data structures

• To improve performance

• Distributed ray tracing techniques

• To achieve some neat visual effects

Must go faster...

• So what we’ve done so far works

• We can render any scene just fine

• At least, any scene that doesn’t use

additional effects

• But it’s really, really slow:

• Loop

• For each pixel

• For each object

• For each light

• Reflection/refraction/shadows make it even

worse

Must go faster...

• Can’t do anything about looping over

each pixel

• That we’re stuck with

• But looping over every object and every

light?

• There we have some options

Regular Grids

• Simplest structure

• Just divide space into a regular grid

• Say, 1-unit axis aligned cubes

• Only need to test against any objects inside

a region if the ray hits it

• Only need to test farther regions if no

intersection in nearer regions

• Can use 3D line drawing algorithms for fast

cell traversal

Regular Grids

Regular Grids Adaptive Grids

• Several ways to do it

• We’ll talk about quadtrees / octrees

• Quadtrees are 2D, octrees are 3D

Octrees

• Start out with a very coarse regular grid

• Subdivide only in areas where there is

geometry

• If there is a primitive inside a grid cell, split

that grid cell into 8 equal cells

• Repeat until you’ve reached some

maximum split depth, or a cell only contains a single primitive

Octree (well, really

Quadtree) example

Remember this

example?

• Let’s see an example (in 2D, not 3D)

• Here, a line divides the plane into two half-

planes

Space BSP Tree

A A

X

B C C

X

Y B

D

E E

Y

D

BSP Trees

• Can create a BSP tree for your scene

• Then only have to test against objects

that are on the “right” side of a split

plane

Accelerating Lighting

• The previous structures reduce ray

intersections

• Can we improve lighting, too?

• Sure!

• One way: Do a pre-pass that determines if a

light is visible from an object (or region of space...), and cache that information

• Then only need to test against potentially

visible lights

Acceleration Review

• We’re stuck looping over each pixel

• But we can:

• Test against fewer and/or simpler objects

• Bounding volumes

• Spatial data structures

• Test against fewer lights

• Do a potential visibility pre-pass

Stochastic Ray

Tracing

• Cook argues that classical ray tracing

( i.e. everything we’ve done so far) only

represents sharp phenomena

• Unrealistic sharp shadows, infinite depth of

focus, etc.

• How can we do better?

Distributed Ray

Tracing

• So what are some of the effects we can

expect this way?

• Antialiasing

• Distribute rays across each pixel

• Glossy reflections

• Distribute multiple reflection rays instead

of just one

Cook et al., 1984

Soft Shadows

• In most graphics applications (and in

our ray tracer so far), we’ve assumed

point light sources

• In the real world, lights

have area

• This leads to soft

shadows in the real world, which we can’t yet simulate in our ray

tracer Hard Shadows Soft Shadows

Soft Shadows

• To get soft shadows, don’t just shoot one

ray

• Shoot multiple rays distributed across the

surface of the light

• Sum their contributions to find the amount of

shadow

Soft Shadow

Examples

Depth of Field

• Our ray tracer up to this point simulates

a pinhole camera

• Real world cameras have lenses,

differing aperture sizes, differing exposure times, etc.

• We’re going to focus (no pun

intended) on depth of field

Depth of Field

• To get depth of field, generate multiple

rays for each pixel

• Distribute them across the surface of the lens

Perfect Focus Depth of Focus

Depth of Field

Examples

Motion Blur

• Motion blur in the real world happens

when objects are moving while the

camera shutter is open

• Effectively, the same point on the object is

seen along multiple rays from the camera

Motion Blur

• To get motion blur, you need to distribute

your rays over time

• As an object moves, it will get hit by different

camera rays

• Moving objects get averaged with the

environment

• What happens to stationary objects?

• Additional rays can still be used for

antialiasing, depth of field effects, etc.

Motion Blur

Examples

Next Time

• Leaving the world of standard ray

tracing

• Introducing radiosity