34
David Luebke 05/12/22 CS 551 / 645: Introductory Computer Graphics David Luebke [email protected] http://www.cs.virginia.edu/~cs551

David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke [email protected] cs551

Embed Size (px)

Citation preview

Page 1: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

CS 551 / 645: Introductory Computer Graphics

David Luebke

[email protected]

http://www.cs.virginia.edu/~cs551

Page 2: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Administrivia

Upcoming graphics events– Graphics lunch: Olsson 236D

Ongoing, informal event; bring your lunch

– “Graphics week”: 2 upcoming colloquia Dec 1: Dan Aliaga (Bell Labs), Image-Based Rendering Dec 6: Ben Watson (U. Alberta), Level of detail control

Page 3: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Ray Casting

An example:

ScreenEyepoint Scene

Page 4: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Recursive Ray Tracing

Obvious extension: – Spawn additional rays off reflective surfaces– Spawn transmitted rays through transparent

surfaces

Leads to recursive ray tracing

Primary Ray

Secondary Rays

Page 5: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Basic Algorithm

Object allObs[];

Color image[];

RayTraceScene()

allObs = initObjects();

for (Y all rows in image)for (X all pixels in row) Ray R = calcPrimaryRay(X,Y);

image[X,Y] = TraceRay(R);display(image);

Page 6: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Basic Algorithm

Color TraceRay(Ray R)

if rayHitsObjects(R) then

Color localC, reflectC, refractC;

Object O = findNearestObject(R);

localC = shade(O,R);

Ray reflectedRay = calcReflect(O,R)

Ray refractedRay = calcRefract(O,R)

reflectC = TraceRay(reflectedRay);

refractC = TraceRay(refractedRay);

return localC reflectC refractCelse return backgroundColor

Page 7: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Representing Rays

We represent a ray parametrically: – A starting point O– A direction vector D– A scalar t

O

D

t = 1t < 0

R = O + tDt > 1

Page 8: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Ray-Polygon Intersection

Find plane equation of polygon:ax + by + cz + d = 0

To find coefficients:N = [a, b, c]

d = N P1

x

yN

P1

P2

d

Page 9: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Ray-Polygon Intersection

Find intersection of ray and plane:t = -(aOx + bOy + cOz + d) / (aDx + bDy + cDz)

Does polygon contain intersection point Pi ?– One simple algorithm:

Draw line from Pi to each polygon vertex Measure angles between lines (how?) If sum of angles between lines is 360°,

polygon contains Pi

– Slow — better algorithms available

Page 10: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Shadow Rays

Simple idea: – Where a ray intersects a surface, send a shadow

ray to each light source– If the shadow ray hits any surface before the light

source, ignore light

Note: each ray-surface intersection now spawns n + 2 rays, n = # light sources– Remember: intersections 95% of work

Page 11: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Recap: Shadow Rays

Some problems with using shadow rays as described: – Lots of computation– Infinitely sharp shadows– No semitransparent object shadows

Page 12: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Ray Problems:Too Much Computation Light buffer (Haines/Greenberg, 86)

– Precompute lists of polygons surrounding light source in all directions

– Sort each list by distance to light source– Now shadow ray need only be intersected with appropriate

list!

Occluding Polys

ShadowRay

Current Intersection Point

Light Buffer

Page 13: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Rays

Some problems with using shadow rays as described: – Lots of computation– Infinitely sharp shadows– No semitransparent object shadows

Page 14: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Ray Problems:Sharp Shadows Why are the shadows sharp? A: Infinitely small point light sources What can we do about it? A: Implement area light sources How?

Page 15: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Ray Problems: Area Light Sources

30% blockage

Could trace a conical beam from point of intersection to light source:

Track portion of beam blocked by occluding polygons:

Page 16: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Ray Problems:Area Light Sources Too hard! Approximate instead: Sample the light source over its area and

take weighted average:

50% blockage

Page 17: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Ray Problems:Area Light Sources Disadvantages:

– Less accurate (50% vs. 30% blockage)– Oops! Just quadrupled (at least) number of

shadow rays

Moral of the story: – Soft shadows are very expensive in ray tracing

Page 18: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Rays

Some problems with using shadow rays as described: – Lots of computation– Infinitely sharp shadows– No semitransparent object shadows

Page 19: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Shadow Ray Problems:Semitransparent Objects In principle:

– Translucent colored objects should cast colored shadows

– Translucent curved objects should create refractive caustics

In practice:– Can fake colored shadows by attenuating color

with distance– Caustics need backward ray tracing

Page 20: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Speedup Techniques

Intersect rays faster Shoot fewer rays Shoot “smarter” rays

Page 21: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Speedup Techniques

Intersect rays faster Shoot fewer rays Shoot “smarter” rays

Page 22: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Intersect Rays Faster

Bounding volumes Spatial partitions Reordering ray intersection tests Optimizing intersection tests

Page 23: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Bounding Volumes

Bounding volumes– Idea: before intersecting a ray with a collection of

objects, test it against one simple object that bounds the collection

0

1

2

3

4

56

78

90

1

2

3

4

56

78

9

Page 24: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Bounding Volumes

Hierarchical bounding volumes– Group nearby volumes hierarchically– Test rays against hierarchy top-down

Page 25: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Bounding Volumes

Different bounding volumes – Spheres

Cheap intersection test Poor fit Tough to calculate optimal clustering

– Axis-aligned bounding boxes (AABBs) Relatively cheap intersection test Usually better fit Trivial to calculate clustering

Page 26: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Bounding Volumes

More bounding volumes– Oriented bounding boxes (OBBs)

Medium-expensive intersection test Very good fit (asymptotically better) Medium-difficult to calculate clustering

– Slabs (parallel planes) Comparatively expensive Very good fit Difficult to calculate good clustering

Page 27: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Spatial Partitioning

Hierarchical bounding volumes surround objects in the scene with (possibly overlapping) volumes

Spatial partitioning techniques classify all space into non-overlapping portions

Page 28: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Spatial Partitioning

Example spatial partitions:– Uniform grid (2-D or 3-D)– Octree– k-D tree– BSP-tree

Page 29: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Uniform Grid

Uniform grid pros:– Very simple and fast to generate– Very simple and fast to trace rays across (How?)

Uniform grid cons:– Not adaptive

Wastes storage on empty space Assumes uniform spread of data

Page 30: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Octree

Octree pros:– Simple to generate– Adaptive

Octree cons:– Nontrivial to trace rays across– Adaptive only in scale

Page 31: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

k-D Trees

k-D tree pros:– Moderately simple to generate– More adaptive than octrees

k-D tree cons:– Less efficient to trace rays across– Moderately complex data structure

Page 32: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

BSP Trees

BSP tree pros:– Extremely adaptive– Simple & elegant data structure

BSP tree cons:– Very hard to create optimum BSP– Splitting planes can explode storage– Simple but slow to trace rays across

Page 33: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Reordering RayIntersection Tests Caching ray hits (esp. shadow rays) Memory-coherent ray tracing

Page 34: David Luebke10/9/2015 CS 551 / 645: Introductory Computer Graphics David Luebke cs551@cs.virginia.edu cs551

David Luebke 04/21/23

Optimizing Ray Intersection Tests Fine-tune the math!

– Share subexpressions– Precompute everything possible

Code with care– Even use assembly, if necessary