Ray Tracing 1/38
Komputer Grafik 2 (AK045206)
Ray Tracing
Ray Tracing 2/38
Komputer Grafik 2 (AK045206)
Outline
• Review Illumination• Render dengan ray tracing• Pembentukan sinar• Interseksi sinar-obyek• Interseksi world space• Vektor normal pada titik interseksi• Transformasi vektor normal• Ray tracing rekursif• Bayangan• Permukaan tembus (transparan)
Ray Tracing 3/38
Komputer Grafik 2 (AK045206)
Review Illumination
• Global, physically-based illumination models describe energy transport and radiation
–subject to the properties of light and materials–subject to the geometry of light, objects and viewer
• For each surface, there is a distribution that characterizes its absorption and reflection at each wavelength
• All illumination models are, by definition, approximate
–based on sampling geometry, light distribution and material properties, and taking rendering shortcuts
Ray Tracing 4/38
Komputer Grafik 2 (AK045206)
Why Trace Rays?
• More elegant than polygon rasterization, especially for sophisticated physics
• Testbed for techniques and effects– modeling (reflectance, transport)– rendering (e.g. Monte Carlo)– texturing (e.g. hypertexture)
• Easiest photorealistic renderer to implement
Ray Tracing 5/38
Komputer Grafik 2 (AK045206)
Ray Tracing
• Ray tracing allows to add more realism to the rendered scene by allowing effects such as– shadows– transparency– reflections
Ray Tracing 6/38
Komputer Grafik 2 (AK045206)
• Raytracing, a simple idea: instead of forward mapping an infinite number of rays from light source to object to viewer, backmap finite number of rays from viewer through each pixel to object to light source (or other object)
• Raytracing: shoot rays from eye through sample point (e.g., a pixel center) of a virtual photo– compute what color/intensity you see at the
corresponding sample point on the object out there; paint this on “photo” at the ray-photo intersection point
• “What you see out there”– combination of the color of the material of the
surface element you hit first and the colors of light that hit it and reflect towards you
Ray Tracing
Ray Tracing 7/38
Komputer Grafik 2 (AK045206)
Rendering with Raytracing
Subproblems to solve• Generate primary (‘eye’) ray
• Find closest object along ray path– find the first intersection of a ray that goes out from
the eye through a pixel center (or any other sample point on image plane) into the scene
• Light sample– use illumination model to determine light at closest
surface element• generate ‘secondary’ rays from object
Ray Tracing 8/38
Komputer Grafik 2 (AK045206)
Ray origin• Let’s look at the geometry of the problem in
untransformed world-space• We start a ray from an “eye point”: P• We send it out in some direction d from your eye
toward a typical point on the film plane whose color we want to know
• Points along the ray have the form P + td where- P is the base point of the ray: the camera’s “eye point”- d is the unit vector direction of the ray- t is a nonnegative real number
• The “eye point” is the center of projection in the perspective view volume (we use perspective coords to avoid having to deal with the inverse of a perspective transformation later)
Generating Rays (1/3)
Ray Tracing 9/38
Komputer Grafik 2 (AK045206)
• We start raycasting with screen-space points (pixels). Need to find a point in 3-D that lies on the corresponding ray
– we will use ray to intersect with the original objects in the original, untransformed world coordinate system
• Transform 2D screen-space points into points on the camera’s 3D film plane
• Any planes orthogonal to the look vector is a convenient film plane: constant z in canonical view volume
• Choose a plane to be the film plane and then create a function that maps screen-space points onto it
– what’s a convenient plane? Try the far plane :-)– to convert, we just have to scale integer screen-space
coordinates into floating point values between -1 and 1
Generating Rays (2/3)Ray direction
Ray Tracing 10/38
Komputer Grafik 2 (AK045206)
• Transform film plane point into world-space point– - we can make the direction vector between
the eye and this world-space point– - we need direction to be in world space so
that we can intersect with the original object in the original, untransformed world coordinate system
• Normalizing transformation takes world-space points to points in the canonical view volume– - translate to the origin, orient with the axes,
scale x and y to adjust viewing angles, scale so that z: [-1, 0]; x,y: [-1, 1]
• Apply the inverse of the normalizing transformation: the Viewing Transformation
•
Ray direction (cont.)
Generating Rays (3/3)
Ray Tracing 11/38
Komputer Grafik 2 (AK045206)
• If an object is defined implicitly by a function f such that f(Q) = 0 if and only if Q is a point on the surface of the object, then ray-object intersection is comparatively easy– we can define many objects implicitly– implicit functions give you potentially infinite
resolution– tessellating implicit functions is more difficult than
using them directly • For example, a circle of radius R is an implicit
object in the plane, and its equation is
– points where f(x, y) = 0 are points on the circle• An infinite plane is defined by the function:
• A sphere of radius R in 3-space:•
Implicit objects
Ray Object Intersection (1/5)
(A,B,C)
(0,0,0)
-D
222) ,( Ryxyxf
DCzByAxzyxf ) , ,(
2222) , ,( Rzyxzyxf
Ray Tracing 12/38
Komputer Grafik 2 (AK045206)
Ray Object Intersection (2/5)
• At what points (if any) does the ray intersect the object?
• Points on the ray have the form P + td– t is any nonnegative real
• A point Q lying on the object has the property that f(Q) = 0
• Combining, we want to know “For which values of t is f(P + td) = 0 ?” (if any)
• We are solving a system of simultaneous equations in x, y (in 2D) or x, y, z (in 3D)
Implicit objects(cont.)
Ray Tracing 13/38
Komputer Grafik 2 (AK045206)
An Explicit Example (1/3)
• Consider the eye-point P = (-3, 1), the direction vector d = (.8, -.6) and the unit circle given by:
• A typical point of the ray is:
• Plugging this into the equation of the circle:
• Expanding, we get:
• Setting this to zero, we get:
1) ,( 22 yxyxf
)6.1,8.3()6.,8(.)1,3( ttttPQ d
1)6.1()8.3()6.1,8.3()( 22 ttttfQf
136.2.1164.8.49 22 tttt
0962 tt
2D ray-circle intersection example
Ray Tracing 14/38
Komputer Grafik 2 (AK045206)
An Explicit Example (2/3)2D ray-circle intersection example (cont.)
• Using the quadratic formula:
• We get:
• Because we have a root of multiplicity 2, our ray intersects the circle at one point (i.e., it’s tangent to the circle)
• We can use the discriminant D = b2 - 4ac to quickly determine if a ray intersects a curve or not
- if D < 0, imaginary roots; no intersection- if D = 0, double root; ray is tangent- if D > 0, two real roots; ray intersects circle at two
points
• Smallest non-negative real t represents intersection nearest to eye-point
a
acbbroots
2
42
3 ,3 ,2
36366
tt
Ray Tracing 15/38
Komputer Grafik 2 (AK045206)
An Explicit Example (3/3)2D ray-circle intersection example (cont.)
• Generalizing: our approach will be to take an arbitrary implicit surface with an equation f(Q) = 0 and a ray P + td, and plug the latter into the former:
• This results, after some algebra, in an equation with t as unknown
• We then solve for t, analytically or numerically
0)( dtPf
Ray Tracing 16/38
Komputer Grafik 2 (AK045206)
Ray Object Intersection (3/5)
Implicit objects-multiple conditions• For objects like cylinders, the equation
• in 3-space defines an infinite cylinder of unit radius, running along the y-axis Usually, it’s more useful to work with finite objects, e.g. such a unit cylinder truncated with the limits
• But how do we do the “caps?”• The cap is the inside of the cylinder at the y
extrema of the cylinder
0122 zx
1
1
y
y
1 ,0122 yzx
Ray Tracing 17/38
Komputer Grafik 2 (AK045206)
Ray Object Intersection (4/5)Multiple conditions (cont.)• Picture
• We want intersections satisfying the cylinder:
or top cap:
or bottom cap:
11
0122
y
zx
10122
yzx
10122
yzx
Ray Tracing 18/38
Komputer Grafik 2 (AK045206)
• Turn an implicit surface equation into an equation for t and solve
• The thing you see “first” from eye point is at the smallest non-negative t-value
• For complicated objects (not defined by a single equation), write out a set of equalities and inequalities and then code as cases…
• Latter approach can be generalized cleverly to handle all sorts of complex combinations of objects- Constructive Solid Geometry (CSG), where objects
are stored as a hierarchy of primitives and 3-D set operations (union, intersection, difference)
- “blobby objects”, which are implicit surfaces defined by sums of implicit equations
Ray Object Intersection (5/5)
Implicit surface strategy summary
Ray Tracing 19/38
Komputer Grafik 2 (AK045206)
World-Space IntersectionWorld space - a global view• One way to do intersection is to compute the
equation of your object in world-space• Example: a sphere translated to (3, 4, 5) after it
was scaled by 2 in the x-direction has equation•
• One can then take the ray P+td and plug it into the equation
• Solve the resulting equation for t• Always start with the untransformed object
definition in its own coordinate system• We then try to derive what the transformed
version should be, given the CTM (call it M)– this is not easy for general transformations, and
furthermore, the transformed version of the equation is always more complicated and thus more expensive to compute
– can we just work with the object in its own coordinate system? Yes, as follows:
1)5()4(2
)3() , ,( 22
2
2
zy
xzyxf
0)( dtPf
Ray Tracing 20/38
Komputer Grafik 2 (AK045206)
Object-Space Intersection
Transform ray into object-space
• Express world-space point of intersection as MQ, where Q is some point in object-space:
• If is the equation of the untransformed object, we just have to solve
– - note d is probably not a unit vector– - the parameter t along this vector and its
world space counterpart always have the same value. Normalizing d would alter this relationship. Do NOT normalize d.
dd
d
d
d
11
11
1
~,
~Let
)(
MPMP
QtMPM
QtPM
MQtP
) , ,(~
zyxf
0)~~
(~
dtPf
(0,0,0)
MQ
dtP dd 11~~ tMPMtP
0),,( zyxf
Q
0),,(~
zyxf
Ray Tracing 21/38
Komputer Grafik 2 (AK045206)
World-Space vs. Object-Space
Use object-space if objects defined there• To compute world-space intersections, you have to
transform the implicit equation of your canonical object defined in object space - often difficult
• But to compute intersections in object-space, you need only apply a matrix (M-1) to P and d, which is much simpler.
– does M-1 exist?– M was composed from two parts: the cumulative modeling
transformation that positions the object in world-space, and the camera’s normalizing transformation
– the modeling transformations are just translations, rotations, and scales (all invertible)
– the normalizing transformation also consists of translations, rotations and scales (also invertible); no perspective! (Now you see why we used the “hinged” perspective view volume)
• When you’re done, you get a t-value• This t can be used in two ways:
– P + td is the world-space location of the intersection of the ray with the transformed object
– P + td is the corresponding point on the untransformed object (in object space)
Ray Tracing 22/38
Komputer Grafik 2 (AK045206)
Normal Vectors at Intersection Points (1/4)
Normal vector to implicit surfaces
• For illumination we need to find a way, given a point on the object in object-space, to determine the normal vector to the object at that point so that we can calculate angles between the normal and other vectors
• If a surface bounds a solid whose interior is given by
then we can find the normal vector at a point(x, y, z) via the gradient at that point:
• Recall that the gradient is a vector with three components:
0) , ,( zyxf
),,( zyxf n
) , ,( ), , ,( ), , ,() , ,( zyxz
fzyx
y
fzyx
x
fzyxf
Ray Tracing 23/38
Komputer Grafik 2 (AK045206)
Normal Vectors at Intersection Points (2/4)
Sphere normal vector example• For the sphere, the equation is
• The partial derivatives are
• So the gradient is
• Normalize n before using in dot products!• In some degenerate cases, the gradient may be
zero, and this method fails…use nearby gradient as a cheap hack
1) , ,( 222 zyxzyxf
zzyxz
f
yzyxy
f
xzyxx
f
2) , ,(
2) , ,(
2) , ,(
)2,2,2(),,( zyxzyxf n
Ray Tracing 24/38
Komputer Grafik 2 (AK045206)
Normal Vectors at Intersection Points (3/4)
Transforming back to world-space
• We have an object-space normal vector• We want a world-space normal vector• To transform the object to world coordinates, we
just multiplied its vertices by M, the object’s CTM• Can we treat the normal vector the same way?
– Answer: NO
• Example: say M scales in x by .5 and y by 2
• See the normal scaling applets in AppletsLighting and Shading
objectworld Mnn
nobject
Mnobject
Wrong!Normal must be perpendicular
Ray Tracing 25/38
Komputer Grafik 2 (AK045206)
Normal Vectors at Intersection Points (4/4)
• Why doesn’t multiplying the normal work?• For translation and rotation, which are rigid body
transformations, it actually does work• Scale, however, distorts the normal in exactly the
opposite sense of the scale applied to the surface, not surprising given that the normal is perpendicular– scaling y by 2 causes normal to scale by .5:
• We’ll see this algebraically in the next slides
xy2
1:normal
xy 2
xy4
1:normal
xy 4
Ray Tracing 26/38
Komputer Grafik 2 (AK045206)
Transforming Normals (1/4)
Object-space to world-space
• Take the polygonal case, for example• Let’s compute the relationship between the
object-space normal nobj to a polygon H and the world-space normal nworld to a transformed version of H, say MH
• If v is a vector in world space that lies in the polygon (e.g., one of the edge vectors), then the normal is perpendicular to the vector v:
• But vworld is just a transformed version of some vector in object space, vobj. So we could write
• But we’d be wrong, because v is a tangent vector– it’s transformed by the derivative…– …which includes a 3D->4D->3D mapping…
0 worldworld vn
0 objworld Mvn
03
objworld M vn
M M of 3x3left upper3
Ray Tracing 27/38
Komputer Grafik 2 (AK045206)
Transforming Normals (2/4)
Object-space to world-space(cont.)• So we want a vector nworld such that
• We will show in a moment that this equation can be rewritten as
• We also already have
• Therefore
• Left-multiplying by (Mt)-1,
03 objworld M vn
objworldtM nn 3
03 objworldtM vn
0 objobj vn
objt
world M nn-1
3
Ray Tracing 28/38
Komputer Grafik 2 (AK045206)
Transforming Normals (3/4)Object-space to world-space(cont.)• So how did we rewrite this:
• As this:
• Recall that if we think of vector as nx1 matrices, then switching notation,
• Rewriting the formula above, we have
• Writing M = Mtt, we get
• Recalling that (AB)t = BtAt, we can write this:
• Switching back to dot product notation:
0)( 3 objt
worldtM vn
03 objworld M vn
baba t
03 objtworld M vn
03 objttt
world M vn
0)( 3 objworldtM vn
0)( 3 objworldtM vn
Ray Tracing 29/38
Komputer Grafik 2 (AK045206)
Transforming Normals (4/4)
Applying inverse-transpose of M to normals• So we ended up with
• Fortunately, “invert” and “transpose” can be swapped, to get our final form:
• Why do we do this? It’s easier!• A hand-waving interpretation of (M3-1)t
– M3 is a composition of rotations and scales, R and S (why no translates?). Therefore
– so we’re applying the transformed (inverted, transposed) versions of each individual matrix in their original order
– for a rotation matrix, this transformed version is equal to the original rotation, i.e., normal rotates with object
• (R-1)t=R; the inverse reverses the rotation, and the transpose reverses it back
– for a scale matrix, the inverse reverses the scale, while the transpose does nothing:
• (S(x,y,z)-1)t = S(x,y,z)-1=S(1/x,1/y,1/z)
objt
world M nn 13 )(
objt
world M nn )( 13
...))()(()(...)...)(( 11111 tttt SRRSRS
Ray Tracing 30/38
Komputer Grafik 2 (AK045206)
Summary : Non-Recursive Ray Tracer
P = eyePt
For each sample of image
Compute d
for each objectIntersect ray P+td with object
Select object with smallest non-negative t-value(this is the visible object)
For this object, find the object-space intersection point
Compute the normal at that pointTransform the normal to world space
Use the world-space normal for lighting computations
Ray Tracing 31/38
Komputer Grafik 2 (AK045206)
Recursive Raytracing
Simulating global lighting effects (Whitted, 1979)
• By recursively casting new rays into the scene, we can look for more information
• Start from the point of intersection• We’d like to send rays in all
directions• Send rays in directions we predict
will contribute the most:– toward lights (shadow)– bounce off objects (specular reflection)– through the object (transparency)
Ray Tracing 32/38
Komputer Grafik 2 (AK045206)
Shadows
• Each light in scene makes contribution to the color and intensity of a surface element…
• Iff it reaches the object!– - could be occluded by other objects in the
scene– - could be occluded by another part of the
same object
• Construct a ray from the surface to each light• Check to see if ray intersects other objects before
it gets to the light– - if ray does NOT intersect that same object
or another object on its way to the light, then the full contribution of the light can be counted
– - if ray does intersect an object on its way to the light, then no contribution of that light should be counted
– - what about transparent objects?
numlights
lightλ
λ
specular][diffusesitylightIntennattenuatio
ambientnsityobjectInte
1
Ray Tracing 33/38
Komputer Grafik 2 (AK045206)
Transparent Surfaces (1/2)Nonrefractive transparency
• For a partially transparent polygon
2polygon for calculatedintensity
1 polygon for calculatedintensity
1 polygon ofnce transmitta
2
1
1
2111)1(
I
I
k
IkIkI
t
tt
Iλ2
Iλ1
polygon 1
polygon 2
Ray Tracing 34/38
Komputer Grafik 2 (AK045206)
Transparent Surfaces (2/2)Refractive transparency• We model the way light bends at interfaces with
Snell’s Law
2medium of refraction ofindex
1 medium of refraction ofindex
t
i
t
iit
sinsin
Ray Tracing 35/38
Komputer Grafik 2 (AK045206)
Recursive Raytracing (1/2)
• Trace ‘secondary’ rays at intersections:– - light: trace ray to each light source. If light
source blocked by opaque object, it does not contribute to lighting, and object in shadow of blocked light at that point
– - specular reflection: trace ray in mirror direction by reflecting about N
– - refraction/transparency: trace ray in refraction direction by Snell’s law
– - recursively spawn new light, reflection, refraction rays at each intersection until contribution negligible
• Your new lighting equation…
– note: intensity from recursed rays calc’d with same eqn– light sources contribute specular and diffuse lighting
• Limitations– - recursive interobject reflection is strictly
specular: along ray reflected about N– - diffuse interobject reflection handled by
radiosity• Informative videos: silent movie on raytracing, A
Long Ray’s Journey into Light
recursive
refracted
ttt
reflected
ssm
specular
nss
diffuse
ddpatt
ambient
dkaa IOkOkVROkLNOkIfOII ])([
Ray Tracing 36/38
Komputer Grafik 2 (AK045206)
Recursive Raytracing (2/2)
Light-ray Trees
Ray Tracing 37/38
Komputer Grafik 2 (AK045206)
Raytracing Pipeline
• Raytracer produces visible samples from model– samples convolved with filter to form pixel image
• Additional pre-processing– pre-process database to speed up per-sample
calculations (done once, sometimes must be redone if objects transformed (resize, translate))
Ray Tracing 38/38
Komputer Grafik 2 (AK045206)
Referensi• F.S.Hill, Jr., COMPUTER GRAPHICS –
Using Open GL, Second Edition, Prentice Hall, 2001
• Andries van Dam, Introduction to Computer Graphics, Slide-Presentation, Brown University, 2003, (folder : brownUni)
• ______, Interactive Computer Graphic, Slide-Presentation, (folder : Lect_IC_AC_UK)