ray tracing(week 4a)

Upload: djayusyus

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Ray Tracing(Week 4a)

    1/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 1/38

    Ray Tracing

  • 7/28/2019 Ray Tracing(Week 4a)

    2/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 2/38

    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)

  • 7/28/2019 Ray Tracing(Week 4a)

    3/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 3/38

    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 andviewer

    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

  • 7/28/2019 Ray Tracing(Week 4a)

    4/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 4/38

    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

  • 7/28/2019 Ray Tracing(Week 4a)

    5/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 5/38

    Ray Tracing

    Ray tracing allows to add more

    realism to the rendered scene

    by allowing effects such as shadows

    transparency

    reflections

  • 7/28/2019 Ray Tracing(Week 4a)

    6/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 6/38

    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 lightthat hit it and reflect towards you

    Ray Tracing

  • 7/28/2019 Ray Tracing(Week 4a)

    7/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 7/38

    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 fromthe 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

  • 7/28/2019 Ray Tracing(Week 4a)

    8/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 8/38

    Ray origin

    Lets look at the geometry of the problem inuntransformedworld-space

    We start a ray from an eye point: P

    We send it out in some direction dfrom your eye

    toward a typical point on the film plane whose colorwe want to know

    Points along the ray have the form P+ td where

    - Pis the base point of the ray: the cameras eyepoint

    - d is the unit vector direction of the ray

    - tis a nonnegative real number

    The eye point is the center of projection in theperspective view volume (we use perspectivecoords to avoid having to deal with the inverse of aperspective transformation later)

    Generating Rays (1/3)

  • 7/28/2019 Ray Tracing(Week 4a)

    9/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 9/38

    We start raycasting with screen-space points (pixels).Need to find a point in 3-D that lies on thecorresponding 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 thecameras 3D film plane

    Any planes orthogonal to the look vector is aconvenient film plane: constant z in canonical viewvolume

    Choose a plane to be the film plane and then create afunction that maps screen-space points onto it whats a convenient plane? Try the far plane :-)

    to convert, we just have to scale integer screen-spacecoordinates into floating point values between -1 and 1

    Generating Rays (2/3)Ray direction

  • 7/28/2019 Ray Tracing(Week 4a)

    10/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 10/38

    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 wecan intersect with the original object in the original,untransformed world coordinate system

    Normalizing transformation takes world-spacepoints to points in the canonical view volume - translate to the origin, orient with the axes, scalex

    and yto adjust viewing angles, scale so that z: [-1,0];x,y: [-1, 1]

    Apply the inverse of the normalizingtransformation: the Viewing Transformation

    Ray direction (cont.)

    Generating Rays (3/3)

  • 7/28/2019 Ray Tracing(Week 4a)

    11/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 11/38

    If an object is defined implicitly by a function fsuch that f(Q) = 0 if and only ifQ is a point on thesurface of the object, then ray-object intersectionis comparatively easy

    we can define many objects implicitly implicit functions give you potentially infiniteresolution

    tessellating implicit functions is more difficult thanusing them directly

    For example, a circle of radius Ris an implicitobject 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 Rin 3-space:

    Implicit objects

    Ray Object Intersection (1/5)

    (A,B,C

    (0,0,0)

    -D

    222),( Ryxyxf

    DCzByAxzyxf ),,(

    2222),,( Rzyxzyxf

  • 7/28/2019 Ray Tracing(Week 4a)

    12/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 12/38

    Ray Object Intersection (2/5)

    At what points (if any) does the ray intersect theobject?

    Points on the ray have the form P+ td tis any nonnegative real

    A point Q lying on the object has the property thatf(Q) = 0

    Combining, we want to know For which values oftis f(P + td)= 0 ? (if any)

    We are solving a system of simultaneousequations inx, y(in 2D) orx, y, z(in 3D)

    Implicit objects(cont.)

  • 7/28/2019 Ray Tracing(Week 4a)

    13/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 13/38

    An Explicit Example (1/3)

    Consider the eye-point P= (-3, 1), thedirection vectord = (.8, -.6) and the unitcircle given by:

    A typical point of the ray is:

    Plugging this into the equation of thecircle:

    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

  • 7/28/2019 Ray Tracing(Week 4a)

    14/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 14/38

    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, ourray intersects the circle at one point (i.e., its

    tangent to the circle) We can use the discriminant D = b2- 4acto

    quickly determine if a ray intersects a curve ornot

    - ifD < 0, imaginary roots; no intersection

    - ifD = 0, double root; ray is tangent

    - ifD > 0, two real roots; ray intersects circle at twopoints

    Smallest non-negative real trepresentsintersection nearest to eye-point

    a

    acbbroots

    2

    42

    3,3,2

    36366

    tt

  • 7/28/2019 Ray Tracing(Week 4a)

    15/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 15/38

    An Explicit Example (3/3)

    2D ray-circle intersection example (cont.)

    Generalizing: our approach will be to take anarbitrary implicit surface with an equation f(Q)= 0 and a ray P+ td, and plug the latter intothe former:

    This results, after some algebra, in anequation with tas unknown

    We then solve fort, analytically or numerically

    0)( dtPf

  • 7/28/2019 Ray Tracing(Week 4a)

    16/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 16/38

    Ray Object Intersection (3/5)Implicit objects-multiple conditions

    For objects like cylinders, the equation

    in 3-space defines an infinite cylinder of unitradius, running along the y-axis Usually, its moreuseful to work with finite objects, e.g. such a unitcylinder truncated with the limits

    But how do we do the caps?

    The cap is the inside of the cylinder at the yextrema of the cylinder

    0122 zx

    1

    1

    y

    y

    1,0122 yzx

  • 7/28/2019 Ray Tracing(Week 4a)

    17/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 17/38

    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

    1

    0122

    y

    zx

    1

    0122

    y

    zx

  • 7/28/2019 Ray Tracing(Week 4a)

    18/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 18/38

    Turn an implicit surface equation into an equationfortand solve

    The thing you see first from eye point is at thesmallest non-negative t-value

    For complicated objects (not defined by a singleequation), write out a set of equalities andinequalities and then code as cases

    Latter approach can be generalized cleverly to

    handle all sorts of complex combinations ofobjects- Constructive Solid Geometry (CSG), where objects

    are stored as a hierarchy of primitives and 3-D setoperations (union, intersection, difference)

    - blobby objects, which are implicit surfaces definedby sums of implicit equations

    Ray Object Intersection (5/5)Implicit surface strategy summary

  • 7/28/2019 Ray Tracing(Week 4a)

    19/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 19/38

    World-Space IntersectionWorld space - a global view

    One way to do intersection is to compute theequation of your object in world-space

    Example: a sphere translated to (3, 4, 5) after itwas scaled by 2 in the x-direction has equation

    One can then take the ray P+td and plug it intothe equation

    Solve the resulting equation fort Always start with the untransformed object

    definition in its own coordinate system

    We then try to derive what the transformedversion should be, given the CTM (call it M) this is not easy for general transformations, and

    furthermore, the transformed version of theequation is always more complicated and thusmore expensive to compute

    can we just work with the object in its owncoordinate system? Yes, as follows:

    1)5()4(2

    )3(),,( 22

    2

    2 zy

    xzyxf

    0)( dtPf

  • 7/28/2019 Ray Tracing(Week 4a)

    20/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 20/38

    Object-Space IntersectionTransform 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 untransformedobject, we just have to solve

    - note d is probably not a unit vector

    - the parametertalong this vector and its worldspace 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

  • 7/28/2019 Ray Tracing(Week 4a)

    21/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 21/38

    World-Space vs. Object-Space

    Use object-space if objects defined there

    To compute world-space intersections, you have totransform the implicit equation of your canonical objectdefined in object space - often difficult

    But to compute intersections in object-space, you needonly apply a matrix (M-1) to Pand d, which is much

    simpler. does M-1 exist?

    Mwas composed from two parts: the cumulative modelingtransformation that positions the object in world-space,and the cameras normalizing transformation

    the modeling transformations are just translations,rotations, and scales (all invertible)

    the normalizing transformation also consists oftranslations, rotations and scales (also invertible); noperspective! (Now you see why we used the hingedperspective view volume)

    When youre done, you get a t-value

    This tcan 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 theuntransformed object (in object space)

  • 7/28/2019 Ray Tracing(Week 4a)

    22/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 22/38

    Normal Vectors at

    Intersection Points (1/4)Normal vector to implicit surfaces

    For illumination we need to find a way, given apoint on the object in object-space, to determinethe normal vector to the object at that point so thatwe can calculate angles between the normal andother vectors

    If a surface bounds a solid whose interior is givenby

    then we can find the normal vector at a point(x, y,z) via the gradientat that point:

    Recall that the gradient is a vector with threecomponents:

    0),,( zyxf

    ),,( zyxf n

    ),,(),,,(),,,(),,( zyx

    z

    fzyx

    y

    fzyx

    x

    fzyxf

  • 7/28/2019 Ray Tracing(Week 4a)

    23/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 23/38

    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 failsuse nearby gradientas a cheap hack

    1),,( 222 zyxzyxf

    zzyxz

    f

    yzyxy

    f

    xzyxx

    f

    2),,(

    2),,(

    2),,(

    )2,2,2(),,( zyxzyxf n

  • 7/28/2019 Ray Tracing(Week 4a)

    24/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 24/38

    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, wejust multiplied its vertices by M, the objects CTM

    Can we treat the normal vector the same way? Answer: NO

    Example: say Mscales inxby .5 and yby 2

    See the normal scaling applets inAppletsLighting and Shading

    objectworld Mnn

    nobject

    Mnobject

    Wrong!Normal must be

    perpendicular

  • 7/28/2019 Ray Tracing(Week 4a)

    25/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 25/38

    Normal Vectors at

    Intersection Points (4/4) Why doesnt multiplying the normal work?

    For translation and rotation, which are rigid bodytransformations, 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 isperpendicular scaling y by 2 causes normal to scale by .5:

    Well see this algebraically in the next slides

    xy2

    1:normal

    xy 2

    xy4

    1:normal

    xy 4

  • 7/28/2019 Ray Tracing(Week 4a)

    26/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 26/38

    Transforming Normals (1/4)

    Object-space to world-space

    Take the polygonal case, for example

    Lets compute the relationship between theobject-space normal nobjto a polygon Hand the

    world-space normaln

    worldto a transformedversion ofH, say MH

    Ifv is a vector in world space that lies in thepolygon (e.g., one of the edge vectors), then thenormal is perpendicular to the vectorv:

    But vworldis just a transformed version of somevector in object space, vobj. So we could write

    But wed be wrong, because v is a tangent vector its transformed by the derivative

    which includes a 3D->4D->3D mapping

    0 worldworld vn

    0 objworld Mvn

    03

    objworld M vn

    M Mof3x3leftupper3

  • 7/28/2019 Ray Tracing(Week 4a)

    27/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 27/38

    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 canbe rewritten as

    We also already have

    Therefore

    Left-multiplying by (Mt)-1,

    03 objworld M vn

    objworld

    tM nn 3

    03 objworldt

    M vn

    0 objobj vn

    objtworld M nn-1

    3

  • 7/28/2019 Ray Tracing(Week 4a)

    28/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 28/38

    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

    obj

    t

    world

    t

    M vn

    03 objworld M vn

    baba t

    03 objt

    worldMvn

    03 objttt

    worldM vn

    0)( 3 objworldt

    M vn

    0)( 3 objworldt

    M vn

  • 7/28/2019 Ray Tracing(Week 4a)

    29/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 29/38

    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? Its easier!

    A hand-waving interpretation of(M3-1)t M3 is a composition of rotations and scales, Rand S (why

    no translates?). Therefore

    so were applying the transformed (inverted, transposed)versions of each individual matrix in their original order

    for a rotation matrix, this transformed version is equal tothe original rotation, i.e., normal rotates with object

    (R-1)t=R; the inverse reverses the rotation, and the transposereverses 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)

    obj

    t

    world M nn1

    3 )(

    obj

    t

    world M nn )(1

    3

    ...))()(()(...)...)(( 11111 tttt SRRSRS

  • 7/28/2019 Ray Tracing(Week 4a)

    30/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 30/38

    Summary :

    Non-Recursive Ray TracerP = eyePt

    For each sample of image

    Compute d

    for each object

    Intersect 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 point

    Transform the normal to world space

    Use the world-space normal for lighting computations

  • 7/28/2019 Ray Tracing(Week 4a)

    31/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 31/38

    Recursive RaytracingSimulating 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

    Wed 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)

  • 7/28/2019 Ray Tracing(Week 4a)

    32/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 32/38

    Shadows

    Each light in scene makes contribution to thecolor 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 beforeit gets to the light

    - if ray does NOT intersect that same object oranother 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 becounted

    - what about transparent objects?

    numlights

    light

    specular[diffusesitylightIntennattenuatio

    ambientnsityobjectInte

    1

  • 7/28/2019 Ray Tracing(Week 4a)

    33/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 33/38

    Transparent Surfaces (1/2)Nonrefractive transparency

    For a partially transparent polygon

    2polygonforcalculatedintensity

    1polygonforcalculatedintensity

    1polygonofncetransmitta

    2

    1

    1

    2111)1(

    I

    I

    k

    IkIkI

    t

    tt

    I2

    I1

    polygon 1

    polygon 2

  • 7/28/2019 Ray Tracing(Week 4a)

    34/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 34/38

    Transparent Surfaces (2/2)Refractive transparency

    We model the way light bends at interfaces with

    Snells Law

    2mediumofrefractionofindex

    1mediumofrefractionofindex

    t

    i

    t

    iit

    sinsin

  • 7/28/2019 Ray Tracing(Week 4a)

    35/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 35/38

    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 tolighting, and object in shadow of blocked light at thatpoint

    - specular reflection: trace ray in mirror direction byreflecting about N

    - refraction/transparency: trace ray in refractiondirection by Snells law

    - recursively spawn new light, reflection, refractionrays at each intersection until contribution negligible

    Your new lighting equation

    note: intensity from recursed rays calcd 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, ALong Rays Journey into Light

    recursive

    refracted

    ttt

    reflected

    ssm

    specular

    nss

    diffuse

    ddpatt

    ambient

    dkaa IOkOkVROkLNOkIfOII ])([

  • 7/28/2019 Ray Tracing(Week 4a)

    36/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 36/38

    Recursive Raytracing (2/2)Light-ray Trees

  • 7/28/2019 Ray Tracing(Week 4a)

    37/38

    Komputer Grafik 2 (AK045206)

    Ray Tracing 37/38

    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))

  • 7/28/2019 Ray Tracing(Week 4a)

    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 toComputer Graphics, Slide-Presentation, Brown University, 2003,(folder : brownUni)

    ______, Interactive Computer Graphic,Slide-Presentation, (folder :Lect_IC_AC_UK)