27
computer graphics & visualization Image Synthesis Ray-Tracing – A Quick review

Computer graphics & visualization Ray-Tracing – A Quick review

Embed Size (px)

Citation preview

Page 1: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis

Ray-Tracing – A Quick review

Page 2: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Colored rays• Ray is a straight path followed by a photon of light

• Multiple photons can travel along the same ray at the same time

• Need to know the color of photons hitting the eye along some path

• Photons originate at some light source and bounce around the geometry in the scene until they are either completely absorbed or hit the eye

Page 3: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Bouncing rays

Page 4: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Forward or backwardTracing photons out from each light source would lead to an excellent picture of the sceneBut most photons will never hit the eye

We want to know what photons certainly contribute to the imageIf we consider a pixel on the image plane, we know where photons hitting that pixel must be coming fromTherefore, we trace rays backwards from the eye into the scene

Page 5: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

The ray tracing camera• Infinite viewing frustum

Page 6: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray tracing• The basic ray-tracing method– Recursive search for paths to the light source

• Interaction with matter• Point-wise evaluation of an illumination model• Shadows, reflection, transparency

ViewpointPoint light

source

R2

R1

R3

N2

N1

N3

T1

T2

L2 L1

L3

N: surface normals R: reflected rays L: shadow rays T: transmitted rays

Page 7: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray tracing• The basic ray-tracing method

Page 8: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray tracing• Recursive ray traversal results in a tree-like

computation order– Depth of recursion can be arbitrary– More work in branches but less contribution to the final

image due to absorption– I = Idirect + ks Ireflected + kt Irefracted

– Idirect = Iambiant + Idiffuse + Ispecular

Viewpoint

L1

L2

R1 T1

R2 R3 T2

Page 9: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray tracing• The basic ray-tracing program– Define the camera ( Cam(o,d,f,x,y,X,Y) )

• O: point of view• D: viewing direction• f: focal distance (distance to the view plane)• x,y: size of the image plane• Up vector U (orientation of the image plane)

– Define the viewport• X,Y: resolution of the viewport (the pixel raster)

– Sampling the image plane with at least one ray per pixel

Page 10: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray tracingSampling the image plane

For (each pixel ) {Define the ray from the viewpoint through that pixelTrace the ray through the scene [

compute ray-object intersectionsrecursively accumulate object colors along the ray

]Set accumulated color as the pixel color

}

Page 11: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray tracing• Sampling the image plane– A continuous sphere sampling at varying viewport

resolution

Page 12: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-object intersectionsImplicit surfaces

– Implicit equations implicitely define a set of points on the surface by the equation

F([x,y,z]T) = 0– For a possible intersection point it must hold:

f(o + td) = 0– Example: ray-plane(Ax+By+Cz+D = 0) intersection

A(ox+tdx)+B(oy+tdy)+C(oz+tdz)+D=0

Þ t = -(Pn o + D) / (Pn d)

Check the denominator for ray || plane

Page 13: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-object intersectionsFor implicit surfaces the normal at the intersection point is given by

n = f(p) = ( f(p)/x, f(p)/y, f(p)/z )

Example: sphere

(x-cx)2 +(y-cy)2 +(z-cz)2 - R2 = 0

f(p)/x = 2(x-cx)

f(p)/y = 2(y-cy)

f(p)/z = 2(z-cz)

n = 2(p-c), the vector from the center to the point

Page 14: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-object intersectionsRay-sphere(c,R) intersection

– Implicit equation of the sphere(x-cx)2 +(y-cy)2 +(z-cz)2 - R2 = 0, which is equal to

(p-c)(p-c) – R2 = 0

– With the parametric ray:(o+t d – c) (o+t d – c) – R2 = 0

Þ (dd)t2 + 2d(o-c)t + (o-c)(o-c) – R2 = 0Þ Quadratic equation in tÞ Discriminant determines the number of solutions

Page 15: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-object intersectionsParametric surfaces

– A mapping from R2(u,v) to R3(x,y,z) x = f(u,v), y = g(u,v), z = h(u,v)

A parametric unit sphere:

: [0:2pi]Azimuth: [0:pi] Zenith

x = cos()*sin()y = sin()*sin()z = cos()

Page 16: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-object intersectionsParametric surfaces

– Intersecting the ray with a parametric surfaceox + t dx = f(u,v)

oy + t dy = g(u,v)

oz + t dz = h(u,v)

-> Yields 3 equations and 3 unknowns -> solve it-> Might be complicated for non-simple f,g and h

– The normal is given byN(u,v) = (f/u, g/u, h/u) X (f/v, g/v, h/v)

Page 17: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-object intersectionsRay-triangle intersection– Given a triangle defined by the three points A, B and C– Any point within the triangle can be defined by means of

barycentric coordinates u,v,wP = uA+vB+wC, with u+v+w=1 and u,v,w > 0

P

B

A C

(u,v,w)

Aa

Ab

Ac

Page 18: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Given a Ray …• … R(t) by ist origin o and direction d

R(t)=o+td

• Intersection:

(1-u-v)V0+uV1+vV2=O+tD

-tD+ uV1-uV0+vV2-vV0=O-V0

[-D,V1-V0, V2-V0][t,u,v]T=O-V0

Page 19: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Geometric Interpretation[-D,V1-V0, V2-V0][t,u,v]T=O-V0

Page 20: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Solving the System of Equations

Page 21: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Implementationbool IntersectTriangle( const VECTOR3& orig, const VECTOR3& dir, VECTOR3& v0, VECTOR3& v1, VECTOR3& v2, float* t, float* u, float* v )

{

// Find vectors for two edges sharing vert0

VECTOR3 edge1 = v1 - v0;

VECTOR3 edge2 = v2 - v0;

// Begin calculating determinant - also used to calculate U parameter

VECTOR3 pvec;

Cross( &pvec, &dir, &edge2 ); 

// If determinant is near zero, ray lies in plane of triangle

float det = Dot( &edge1, &pvec );

 

VECTOR3 tvec;

if( det > 0 ) tvec = orig - v0; else

{

tvec = v0 - orig;

det = -det;

}

 

if( det < 0.0001f ) return false;

 

// Calculate U parameter and test bounds

*u = Dot( &tvec, &pvec );

if( *u < 0.0f || *u > det ) return false;

// Prepare to test V parameter

VECTOR3 qvec;

Cross( &qvec, &tvec, &edge1 ); 

// Calculate V parameter and test bounds

*v = Dot( &dir, &qvec );

if( *v < 0.0f || *u + *v > det ) return false;

// Calculate t, scale parameters, ray intersects triangle

*t = Dot( &edge2, &qvec );

float fInvDet = 1.0f / det;

*t *= fInvDet; *u *= fInvDet; *v *= fInvDet;

return true;

}

Page 22: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-tracing shadows • Shadow rays– For each ray-object intersection cast a ray toward

the light sources– Check for intersections in-between object and light

source• Take care of finite-precision arithmetic• Shadow ray might intersect object it originates from at t

= – Acceleration by means of pre-computed shadow

maps or shadow volumes (to be discussed later)

Page 23: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-tracing shadows• Soft-shadows (Penumbras)– Occur where an area light source is partially

occluded

Page 24: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Ray-tracing shadows• Soft-shadows (Penumbras)– Reflected intensity is proportional to the solid angle of the

visible portion of the light• Complex computation

– Distribute multiple shadow rays across the region

– Weight shadow contributions

Page 25: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Object representationPolygonal approximation of continuous objects

Page 26: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Object representationPolygonal approximation of continuous objects– Approximation of shape by polygonal patches– Modelling by hand, scanning, surface evaluation

Page 27: Computer graphics & visualization Ray-Tracing – A Quick review

computer graphics & visualization

Image Synthesis – WS 07/08Dr. Jens Krüger – Computer Graphics and Visualization Group

Object representation

• For display we need:

• The objects shape• i.e. geometric representation including normal• 3D Object coordinates (vertices)• 3D normal vectors• Topology (connectivity information)

• The objects appearance • i.e. material properties like color reflection, texture etc.