32
CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1— 12.3,7.4,3.2.2]

CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

Embed Size (px)

Citation preview

Page 1: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

CS 3388Rendering Pipelines: Ray

Casting vs Rasterization

[Hill §12.1—12.3,7.4,3.2.2]

Page 2: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

2

“Rendering”

• Generate visual representation of computer’s internal model

• Inverse problem: . Computer Vision

output: bitmap of rendered scene

input: computer model of 3D scene

computer model of camera

rendering

?input: bitmap

output: model

Page 3: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

3

“Rendering”

• Problem: determine colour of every pixel

• Two major approaches:– ray tracing (“trace pixel’s path to the objects”)

– rasterization (“project object onto image plane”)

? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?

Page 4: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

4

• Important facts about light:– travels along straight lines (geodesics, in general)– can reflect off surface, but usually scatters

• Ray Casting: “what surface point would send light into eye through this pixel?”

• Ray Tracing: “what is total amount of light that would really arrive at that surface point?”

Ray Casting vs Ray Tracing

forward propagation of photons (only tiny fraction enter eye)

ray casting

Page 5: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

5

Light Isn’t Obvious

• Pythagoras (582—500BC) thought eyes sent out ‘feeler rays’ to sense objects

a

b

c

“light consists of rays that, acting like feelers, travel in straight lines from the eye to the object, and the sensation of sight is obtained when these rays touch the object, much like the sense of touch.”

Page 6: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

6

Aside: “Helmholtz Reciprocity”

• Can interchange light and camera, yet camera sees identical shading at fixed 3D point

http://www.cs.virginia.edu/~mjh7v/vision/final/index.php

Page 7: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

Ray Casting

• Photon reaching eye through pixel (i,j) traveled on straight path from final surface

• Find closest intersection on eye-pixel ray!

• Called primary rays

pixel grid² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²² ² ² ² ² ²

path photon would take

wd£ht pixels

Assuming canonical view frustum:

Page 8: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

8

• Ray casting makes very local assumptions:– that photons arrive directly from a light source– that photons are never ‘blocked’ (no hard shadows)

• Hack: angle of light-ray, primary-ray, and normal determine colour

Ray Casting

path photon would take

We cover ray tracing for assignment 4!

Page 9: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

9

Ray Casting Loop

• Really simple to implement

• Trivially parallelizable

// pseudocode for ray casting (wd,ht = framebuffer dimensions)raycast(scene,eye) { for i=0..wd-1 for j=0..ht-1 { define ray r_ij relative to eye for each surface in scene { hit = intersection of r_ij with surface (if any) if hit.z is closest to eye so far, keep it } compute colour at hit point store colour at pixel (i,j) }}

Page 10: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

10

We’ll Work in Flatland (x-z Plane)

you live here

A2 lives here

Page 11: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

11

We’ll Work in Flatland (x-z Plane)

Page 12: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

12

We’ll Work in Flatland (x-z Plane)

Page 13: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

13

3D from 2D Ray Casting (“2.5D”)

• Define world as 2D lines (walls of height h)• Hit at means 3D points

and perspective projection

x

z

horizon

n

assumes eye at height

Page 14: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

14

2D Ray Casting EnginesWolfenstein 3D DOOM

Duke Nukem 3D

a mirror!!

John Carmack

Ken Silverman (when he was 18!!!)

Page 15: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

15

Ray Casting vs Rasterization

• Rendering needs final colour for every pixel

• Ray casting “samples” a colour at every pixel independently (perfect!)

• Rasterization does not “sample”…– apply perspective projection only at

sparse 3D points (not surfaces!)– draw lines/surfaces by

interpolating in screen-space

? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ?

Page 16: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

16

Rasterization Pipeline

• The “graphics pipeline” of a 3D triangle…

(modelview & perspective transformation)

(clipping)

(perspective divide)

(viewport transformation)

model coordinates

clip coordinates

normalized devicecoordinates (NDC)

window coordinates

(Z-culling, rasterization)

(homogeneous)

(cartesian)

Page 17: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

17

Model View & Perspective (3D)(modelview & perspective transformation)

clip coordinates(homogeneous)

x

z

x

z

model coordinates

x0

pseudodepth

world coordinates

eye coordinates

clip coordinates

homogenous!!

Page 18: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

18

The Projection Matrix

• Derive perspective transformation that distorts view frustum before projection

• Matrix P maps canonical view frustum to canonical view volume

xz

y

¡zx

y

P

(NDC before clipping)

Page 19: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

19

• Assume point p in x-z plane

• Orthographic needs

The Projection Matrix

what do we want here?

P

Page 20: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

20

• Assume point p in x-z plane

• Perspective is tricky

The Projection Matrix

what do we want here?

P

whaaaa?how does this work?

Page 21: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

Deriving the Projection Matrix

• Assuming l = ¡1, r = 1, want

• #1 means must have

#1. need perspective divide #2. must scale x

#3. need pseudodepth(n) = ¡1 pseudodepth(f) = 1

x not relevant to depth so set b1=0

Page 22: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

General Projection Matrices

• General perspective matrix in x-z plane

• Full 3D projection matricesorthographic perspective

reminder: we assume near plane z=n for n < 0; textbook assumes z=-n for n>0

orthographic!

what we wanted!

Page 23: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

23

Clipping

• Must clip in homogeneous coordinates, before perspective divide!

• Clip to canonical view volume– axis-aligned faces ) easy to clip!

clip coordinates(homogeneous) (clipping)

Excellent research paper explains how it all works: http://research.microsoft.com/pubs/73937/p245-blinn.pdf

Jim Blinn

SIGGRAPH 1978

Page 24: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

24

Clipping in x-z Plane

• Canonical view ‘area’ = four boundaries

• For boundary i, three cases:1. both a and b outside i (trivial reject)2. both a and b inside i (don’t clip to i)3. one inside and one outside i (may need to clip)

¡1 · x · 1

¡1 · z · 1

(¡1,1) (1,1)

(¡1,¡1) (1,¡1)

HAVE WANT

three lines two clipped lines

ab

Page 25: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

25

Clipping in x-z Plane

• Apply “Cohen-Sutherland” clipping to homogeneous coordinates

// return false if line was clipped away entirely, true otherwisebool clip(a,b) { test a against every boundary test b against every boundary

if a and b both outside same boundary return false // trivial reject

if a and b inside all boundaries return true // trivial accept

for each boundary clip a or b against this boundary (if applicable)

return true if line not empty}

[Hill p.106]

Page 26: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

26

Clipping in x-z Plane

• How to test if a=(x,z,w) inside boundary?

• What if w · 0? Means original point was behind near plane! (its z > n)

• Such points automatically fail above tests

x ¸ ¡1 x · 1

z ¸ ¡1

z · 1 Assuming w > 0 we have...

if all are true, then a inside all boundaries!

[Hill p.357]

Page 27: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

27

Clipping in x-z Plane

• How to clip line (a,b) to x = ¡1?• Write line parametrically

as• Solve for t such that

• Where have we seen these “boundary coordinate” quantities before?

ab

x ¸ ¡1

a(t)

“boundary coordinate of a on x = ¡1”

“boundary coordinate of b on x = ¡1”

Page 28: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

28

Clipping in x-z Planebool clip(a,b) { abc[0..3] = boundary coordinates of point a bbc[0..3] = boundary coordinates of point b

if any abc[i] and bbc[i] both negative return false // trivial reject (outside same boundary)

if all abc and bbc non-negative return true // trivial accept (inside all boundaries)

for i = 0..3 if abc[i] < 0 tmin[i] = abc[i] / (abc[i] – bbc[i]) else if bbc[i] < 0 tmax[i] = abc[i] / (abc[i] – bbc[i])

if max(tmin) > min(tmax) return false // line was clipped away entirely

set a based on max(tmin) // careful not to invalidate tmin/tmax set b based on min(tmax) // by changing a and b before finished! return true}

[Hill. p359]

Page 29: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

29

Device Coordinates

• After clipping, all w > 0 so safe to divide

• Must map NDC to viewport coordinates

• V is viewport transformation matrix

(perspective divide)

(viewport transformation)

normalized devicecoordinates (NDC)

window coordinates

(cartesian)

Page 30: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

3D Viewport Transformation

30

¡z x

y

NDC window coordinates

viewport

¡z x

y

xy

V

A2 warning: bitmap coordinates have flipped y axis!!

maps pseudodepth to [0,1]

glViewport(...)

Page 31: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

31

Raster Interpolation

• Final stage in 3D rasterization pipeline• Take (x,y) window coordinates and draw

2D primitives (points, lines, polygons)– next topic!!

• Also use pseudodepth to figure out which 2D pixels are ‘in front’ of others

convex non-convex always convex

Page 32: CS 3388 Rendering Pipelines: Ray Casting vs Rasterization [Hill §12.1—12.3,7.4,3.2.2] TexPoint fonts used in EMF. Read the TexPoint manual before you delete

32

The z-Buffer Test

• For every pixel in framebuffer, remember its final pseudodepth (the z-buffer)

• Overwrite colour at (x,y ) if corresponding pseudodepth < zbuffer[x,y] (‘in front’)

• Can draw opaque objects in any order!!

framebuffer (BGR 2[0..255])

z-buffer (pseudodepth 2[0,1])

pseudodepth ¼ 0

pseudodepth z = 1