3_ray_cast2

Embed Size (px)

Citation preview

  • 8/13/2019 3_ray_cast2

    1/51

    MIT EECS 6.837, Cutler and Durand 1

    Ray Casting II

    MIT EECS 6.837

    Frdo Durand and Barb Cutler

    Some slides courtesy of Leonard McMillan

    Courtesy of James Arvo and David Kirk.Used with permission.

  • 8/13/2019 3_ray_cast2

    2/51

    MIT EECS 6.837, Cutler and Durand 2

    Review of Ray Casting

  • 8/13/2019 3_ray_cast2

    3/51

    MIT EECS 6.837, Cutler and Durand 3

    Ray Casting

    For ever y pi xelConst r uct a r ay f r om t he eyeFor ever y obj ect i n t he scene

    Fi nd i nt er sect i on wi t h t he r ay

    Keep i f cl osest

  • 8/13/2019 3_ray_cast2

    4/51

    MIT EECS 6.837, Cutler and Durand 4

    Ray Tracing

    Secondary rays (shadows, reflection, refraction)

    In a couple of weeks

    reflection

    refraction

  • 8/13/2019 3_ray_cast2

    5/51

    MIT EECS 6.837, Cutler and Durand 5

    Ray representation

    Two vectors: Origin

    Direction (normalized is better) Parametric line

    P(t) = R + t * D

    RD

    P(t)

  • 8/13/2019 3_ray_cast2

    6/51

    MIT EECS 6.837, Cutler and Durand 6

    Explicit vs. implicit

    Implicit Solution of an equation

    Does not tell us how to generate a point on the plane Tells us how to check that a point is on the plane

    Explicit Parametric

    How to generate points

    Harder to verify that a point is on the ray

  • 8/13/2019 3_ray_cast2

    7/51

    MIT EECS 6.837, Cutler and Durand 7

    A note on shading

    Normal direction, direction to light

    Diffuse component: dot product

    Specular component for shiny materials Depends on viewpoint

    More in two weeks

    Diffuse sphere shiny spheres

  • 8/13/2019 3_ray_cast2

    8/51

    MIT EECS 6.837, Cutler and Durand 8

    Textbook

    Recommended, not required

    Peter Shirley

    Fundamentals of Computer GraphicsAK Peters

  • 8/13/2019 3_ray_cast2

    9/51

    MIT EECS 6.837, Cutler and Durand 9

    References for ray casting/tracing

    Shirley Chapter 9

    Specialized books: An Introduction to Ray Tracing, Edited by Andrew S. Glassner

    Realistic Ray Tracing, Peter Shirley

    Realistic Image Synthesis Using Photon Mapping, Henrik Wann Jensen

    Online resourceshttp://www.irtc.org/

    http://www.acm.org/tog/resources/RTNews/html/

    http://www.povray.org/http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtrace0.htm

    http://www.siggraph.org/education/materials/HyperGraph/raytrace/rt_java/raytrace.html

  • 8/13/2019 3_ray_cast2

    10/51

    MIT EECS 6.837, Cutler and Durand 10

    Assignment 1

    Write a basic ray caster Orthographic camera

    Spheres Display: constant color and distance

    We provide Ray

    Hit

    Parsing

    And linear algebra, image

  • 8/13/2019 3_ray_cast2

    11/51

    MIT EECS 6.837, Cutler and Durand 11

    Object-oriented design

    We want to be able to add primitives easily Inheritance and virtual methods

    Even the scene is derived from Object3D!

    Object3Dbool intersect(Ray, Hit, tmin)

    Planebool intersect(Ray, Hit, tmin)

    Spherebool intersect(Ray, Hit, tmin)

    Trianglebool intersect(Ray, Hit, tmin)

    Groupbool intersect(Ray, Hit, tmin)

  • 8/13/2019 3_ray_cast2

    12/51

    MIT EECS 6.837, Cutler and Durand 12

    Ray

    cl ass Ray {

    publ i c:

    // CONSTRUCTOR & DESTRUCTOR

    Ray ( ) {}

    Ray ( const Vec3f &di r , const Vec3f &or i g) {di r ect i on = di r ;or i gi n = or i g; }

    Ray ( const Ray& r ) {*t hi s=r ; }

    // ACCESSORS

    const Vec3f & get Or i gi n( ) const { r et ur n or i gi n; }const Vec3f & get Di r ect i on( ) const { r et ur n di r ect i on; }Vec3f poi nt At Par amet er ( f l oat t ) const {

    r et ur n or i gi n+di r ect i on*t ; }

    pr i vat e:

    // REPRESENTATION

    Vec3f di r ect i on;Vec3f or i gi n;

    };

  • 8/13/2019 3_ray_cast2

    13/51

    MIT EECS 6.837, Cutler and Durand 13

    Hit

    Store intersection point & various informationcl ass Hi t {

    publ i c:

    // CONSTRUCTOR & DESTRUCTOR

    Hi t ( f l oat _t , Vec3f c) { t = _t ; col or = c; }~Hi t ( ) {}

    / / ACCESSORS

    f l oat get T( ) const { r et ur n t ; }

    Vec3f get Col or ( ) const { r et ur n col or ; }

    // MODIFIER

    voi d set ( f l oat _t , Vec3f c) { t = _t ; col or = c; }

    pr i vat e:

    // REPRESENTATION

    f l oat t ;Vec3f col or ;

    //Material *material;

    //Vec3f normal;

    };

  • 8/13/2019 3_ray_cast2

    14/51

    MIT EECS 6.837, Cutler and Durand 14

    Tasks

    Abstract Object3D

    Sphere and intersection

    Group class Abstract camera and derive Orthographic

    Main function

  • 8/13/2019 3_ray_cast2

    15/51

    MIT EECS 6.837, Cutler and Durand 15

    Questions?

    Image removed due to copyright considerations.

  • 8/13/2019 3_ray_cast2

    16/51

    MIT EECS 6.837, Cutler and Durand 16

    Overview of today

    Ray-box intersection

    Ray-polygon intersection

    Ray-triangle intersection

    Ray Parallelepiped

  • 8/13/2019 3_ray_cast2

    17/51

    MIT EECS 6.837, Cutler and Durand 17

    Ray-ParallelepipedIntersection Axis-aligned

    From (X1, Y1, Z1) to (X2, Y2, Z2)

    Ray P(t)=R+Dt

    y=Y2

    y=Y1

    x=X1 x=X2

    R

    D

    N b I i

  • 8/13/2019 3_ray_cast2

    18/51

    MIT EECS 6.837, Cutler and Durand 18

    Nave ray-boxIntersection

    Use 6 plane equations

    Compute all 6 intersection

    Check that points are inside boxAx+by+Cz+D

  • 8/13/2019 3_ray_cast2

    19/51

    Factoringoutcomputation

    MIT EECS 6.837, Cutler and Durand 19

    Pairs of planes have the same normal

    Normals have only one non-0 component

    Do computations one dimension at a time

    Maintain tnear and tfar (closest and farthest

    so far)y=Y2

    y=Y1

    x=X1 x=X2

    R

    D

    T t if ll l

  • 8/13/2019 3_ray_cast2

    20/51

    Testifparallel

    MIT EECS 6.837, Cutler and Durand 20

    If Dx=0, then ray is parallel If Rxx2 return false

    y=Y2

    y=Y1

    x=X1 x=X2

    R

    D

    If t ll l

  • 8/13/2019 3_ray_cast2

    21/51

    MIT EECS 6.837, Cutler and Durand 21

    If not parallel

    Calculate intersection distance t1 and t2 t1=(X1-Rx)/Dx

    t2=(X2-Rx)/Dx

    y=Y2

    y=Y1

    x=X1 x=X2

    R

    D

    t1

    t2

    T t 1

  • 8/13/2019 3_ray_cast2

    22/51

    MIT EECS 6.837, Cutler and Durand 22

    Test 1

    Maintain tnear and tfar If t1>t2, swap

    if t1>tnear, tnear =t1 if t2tfar, box is missed

    y=Y2

    y=Y1R

    D

    t1x t2xtnear

    t1y

    t2ytfar

    tfar

    x=X1 x=X2

    T t 2

  • 8/13/2019 3_ray_cast2

    23/51

    MIT EECS 6.837, Cutler and Durand 23

    Test 2

    If tfar

  • 8/13/2019 3_ray_cast2

    24/51

    MIT EECS 6.837, Cutler and Durand 24

    Algorithm recap

    Do for all 3 axis Calculate intersection distance t1 and t2

    Maintain tnear and tfar If tnear>tfar, box is missed

    If tfar

  • 8/13/2019 3_ray_cast2

    25/51

    MIT EECS 6.837, Cutler and Durand 25

    Efficiency issues

    Do for all 3 axis Calculate intersection distance t1 and t2

    Maintain tnear and tfar

    If tnear>tfar, box is missed

    If tfar

  • 8/13/2019 3_ray_cast2

    26/51

    MIT EECS 6.837, Cutler and Durand 26

    Questions?

    Image removed due to copyright considerations.

    Overview of today

  • 8/13/2019 3_ray_cast2

    27/51

    MIT EECS 6.837, Cutler and Durand 27

    Overview of today

    Ray-box intersection

    Ray-polygon intersection

    Ray-triangle intersection

    Ray polygon intersection

  • 8/13/2019 3_ray_cast2

    28/51

    MIT EECS 6.837, Cutler and Durand 28

    Ray-polygon intersection

    Ray-plane intersection Test if intersection is in the polygon

    Solve in the 2D plane

    D

    R

    Point inside/outside polygon

  • 8/13/2019 3_ray_cast2

    29/51

    MIT EECS 6.837, Cutler and Durand 29

    Point inside/outside polygon

    Ray intersection definition: Cast a ray in any direction

    (axis-aligned is smarter) Count intersection

    If odd number, point is inside

    Works for concave and star-shaped

    Precision issue

  • 8/13/2019 3_ray_cast2

    30/51

    MIT EECS 6.837, Cutler and Durand 30

    Precision issue

    What if we intersect a vertex? We might wrongly count an intersection for each

    adjacent edge Decide that the vertex is always above the ray

    Winding number

  • 8/13/2019 3_ray_cast2

    31/51

    MIT EECS 6.837, Cutler and Durand 31

    Winding number

    To solve problem with star pentagon Oriented edges

    Signed number of intersection Outside if 0 intersection

    + -+

    -

    Alternative definitions

  • 8/13/2019 3_ray_cast2

    32/51

    MIT EECS 6.837, Cutler and Durand 32

    Alternative definitions

    Sum of the signed angles from point to vertices 360 if inside, 0 if outside

    Sum of the signed areas of point-edge triangles Area of polygon if inside, 0 if outside

    How do we project into 2D?

  • 8/13/2019 3_ray_cast2

    33/51

    MIT EECS 6.837, Cutler and Durand 33

    How do we project into 2D?

    Along normal Costly

    Along axis Smarter (just drop 1 coordinate)

    Beware of parallel planeD

    R

    D

    R

    Questions?

  • 8/13/2019 3_ray_cast2

    34/51

    MIT EECS 6.837, Cutler and Durand 34

    Questions?

    Image removed due to copyright considerations.

    Overview of today

  • 8/13/2019 3_ray_cast2

    35/51

    MIT EECS 6.837, Cutler and Durand 35

    Overview of today

    Ray-box intersection

    Ray-polygon intersection

    Ray-triangle intersection

    Ray triangle intersection

  • 8/13/2019 3_ray_cast2

    36/51

    MIT EECS 6.837, Cutler and Durand 36

    Ray triangle intersection

    Use ray-polygon Or try to be smarter

    Use barycentric coordinates

    c

    ab

    PRD

    Barycentric definition of a plane

  • 8/13/2019 3_ray_cast2

    37/51

    MIT EECS 6.837, Cutler and Durand 37

    Barycentric definition of a plane

    [Mbius, 1827] P( , , )=a+b+cwith + + =1

    Is it explicit or implicit?

    c

    P

    a b

    Barycentric definition of a triangle

  • 8/13/2019 3_ray_cast2

    38/51

    MIT EECS 6.837, Cutler and Durand 38

    Barycentric definition of a triangle

    P( , , )=a+b+cwith + + =1

    0<

  • 8/13/2019 3_ray_cast2

    39/51

    G ve , ow ca we co pute , , ?

    MIT EECS 6.837, Cutler and Durand 39

    Compute the areas of the opposite subtriangle Ratio with complete area

    =Aa/A, =Ab/A =Ac/AUse signed areas for points outside the triangle

    c

    a b

    P TaT

    Intuition behind area formula

  • 8/13/2019 3_ray_cast2

    40/51

    MIT EECS 6.837, Cutler and Durand 40

    u o be d a ea o u a

    P is barycenter of a and Q A is the interpolation coefficient on aQ

    All points on line parallel to bc have the same All such Ta triangles have same height/area

    c

    a b

    P Q

    Simplify

  • 8/13/2019 3_ray_cast2

    41/51

    MIT EECS 6.837, Cutler and Durand 41

    p y

    Since + + =1we can write =1

    P(, )=(1) a + b +c

    c

    a b

    P

    Simplify

  • 8/13/2019 3_ray_cast2

    42/51

    MIT EECS 6.837, Cutler and Durand 42

    p y

    P(, )=(1) a + b +c P(, )=a + (b-a) +(c-a)

    Non-orthogonal coordinate system of the plane

    c

    a b

    P

    How do we use it for intersection?

  • 8/13/2019 3_ray_cast2

    43/51

    MIT EECS 6.837, Cutler and Durand 43

    Insert ray equation into barycentric expression oftriangle

    P(t)= a+ (b-a)+ (c-a) Intersection if +

  • 8/13/2019 3_ray_cast2

    44/51

    MIT EECS 6.837, Cutler and Durand 44

    Rx+tDx= ax+ (bx-ax)+ (cx-ax) Ry+tDy= ay+ (by-ay)+ (cy-ay)

    Rz+tDz= az+ (bz-az)+ (cz-az)

    c

    ab

    PRD

  • 8/13/2019 3_ray_cast2

    45/51

    Cramers rule

  • 8/13/2019 3_ray_cast2

    46/51

    MIT EECS 6.837, Cutler and Durand 46

    | | denotes the determinant

    Can be copied mechanically in the code

    ab

    PRD

    A

    DcaRa

    DcaRa

    DcaRa

    zzzzz

    yyyyy

    xxxxx

    = A

    DRaba

    DRaba

    DRaba

    zzzzz

    yyyyy

    xxxxx

    = A

    Racaba

    Racaba

    Racaba

    t zzzzzz

    yyyyyy

    xxxxxx

    =

    c

    Advantage

  • 8/13/2019 3_ray_cast2

    47/51

    MIT EECS 6.837, Cutler and Durand 47

    Efficient Store no plane equation

    Get the barycentric coordinates for free Useful for interpolation, texture mapping

    ab

    PRD

    c

    Plucker computation

  • 8/13/2019 3_ray_cast2

    48/51

    MIT EECS 6.837, Cutler and Durand 48

    Plucker space:6 or 5 dimensional space describing 3D lines

    A line is a point in Plucker space

    Plucker computation

  • 8/13/2019 3_ray_cast2

    49/51

    MIT EECS 6.837, Cutler and Durand 49

    The rays intersecting a line are a hyperplane A triangle defines 3 hyperplanes

    The polytope defined by the hyperplanes is theset of rays that intersect the triangle

    Plucker computation

  • 8/13/2019 3_ray_cast2

    50/51

    MIT EECS 6.837, Cutler and Durand 50

    The rays intersecting a line are a hyperplane A triangle defines 3 hyperplanes

    The polytope defined by the hyperplanes is theset of rays that intersect the triangle

    Ray-triangleintersectionbecomes a polytope

    inclusion Couple of additional

    issues

    Next week: Transformations

  • 8/13/2019 3_ray_cast2

    51/51

    MIT EECS 6.837, Cutler and Durand 51

    Permits 3D IFS ;-)

    Image removed due to copyright considerations.