3D Rendering and Ray Castingmisha/Fall20/06.pdf · 2020. 9. 16. · 3D Rendering and Ray Casting...

69
3D Rendering and Ray Casting Michael Kazhdan (601.457/657) HB Ch. 13.7, 14.6 FvDFH 15.5, 15.10

Transcript of 3D Rendering and Ray Castingmisha/Fall20/06.pdf · 2020. 9. 16. · 3D Rendering and Ray Casting...

  • 3D Rendering and Ray Casting

    Michael Kazhdan

    (601.457/657)

    HB Ch. 13.7, 14.6

    FvDFH 15.5, 15.10

  • Rendering• Generate an image from geometric primitives

    Geometric Primitives (3D)

  • Rendering• Generate an image from geometric primitives

    Rendering

    Geometric Primitives (3D)

    Raster Image (2D)

  • 3D Rendering Example

    What issues must be addressed by a 3D rendering system?

  • Overview• 3D scene representation

    • 3D viewer representation

    • What do we see?

    • How does it look?

  • Overview• 3D scene representation

    • 3D viewer representation

    • What do we see?

    • How does it look?

    How is the 3D scenedescribed in a computer?

  • 3D Scene Representation• Scene is usually approximated by 3D primitives

    Point Line segment Triangles Polygon Polyhedron Curved surface Solid object etc.

  • 3D Point• Specifies a location

    Origin

  • 3D Point• Specifies a location

    Represented by three coordinates Infinitely small

    struct Point3D{

    float x , y , z;};

    (𝑥𝑥,𝑦𝑦, 𝑧𝑧)

    Origin

  • 3D Vector

    • Specifies a direction and a magnitude

  • 3D Vector

    • Specifies a direction and a magnitude Represented by three coordinates Magnitude �⃗�𝑣 = 𝑑𝑑𝑥𝑥2 + 𝑑𝑑𝑦𝑦2 + 𝑑𝑑𝑧𝑧2 Has no location

    �⃗�𝑣 = (𝑑𝑑𝑥𝑥,𝑑𝑑𝑦𝑦,𝑑𝑑𝑧𝑧)

    struct Vector3D{

    float dx , dy , dz;};

  • • Specifies a direction and a magnitude Represented by three coordinates Magnitude �⃗�𝑣 = 𝑑𝑑𝑥𝑥2 + 𝑑𝑑𝑦𝑦2 + 𝑑𝑑𝑧𝑧2 Has no location

    • Dot product of two 3D vectors �⃗�𝑣1, �⃗�𝑣2 = 𝑑𝑑𝑥𝑥1 ⋅ 𝑑𝑑𝑥𝑥2 + 𝑑𝑑𝑦𝑦1 ⋅ 𝑑𝑑𝑦𝑦2 + 𝑑𝑑𝑧𝑧1 ⋅ 𝑑𝑑𝑧𝑧2 �⃗�𝑣1, �⃗�𝑣2 = �⃗�𝑣1 ⋅ �⃗�𝑣2 ⋅ cos 𝜃𝜃

    • Cross product of two 3D vectors �⃗�𝑣1 × �⃗�𝑣2 = Vector normal to 𝑣𝑣1 and 𝑣𝑣2 �⃗�𝑣1 × �⃗�𝑣2 = �⃗�𝑣1 ⋅ �⃗�𝑣2 ⋅ sin𝜃𝜃

    3D Vector

    �⃗�𝑣1 = (𝑑𝑑𝑥𝑥1,𝑑𝑑𝑦𝑦1,𝑑𝑑𝑧𝑧1)

    �⃗�𝑣2 = (𝑑𝑑𝑥𝑥2,𝑑𝑑𝑦𝑦2 ,𝑑𝑑𝑧𝑧2)

    𝜃𝜃

  • Cross Product: Review• Let �⃗�𝑣1 = �⃗�𝑣2 × �⃗�𝑣3:

    𝑑𝑑𝑥𝑥1 = 𝑑𝑑𝑦𝑦2 ⋅ 𝑑𝑑𝑧𝑧3 − 𝑑𝑑𝑧𝑧2 ⋅ 𝑑𝑑𝑦𝑦3 𝑑𝑑𝑦𝑦1 = 𝑑𝑑𝑧𝑧2 ⋅ 𝑑𝑑𝑥𝑥3 − 𝑑𝑑𝑥𝑥2 ⋅ 𝑑𝑑𝑧𝑧3 𝑑𝑑𝑧𝑧1 = 𝑑𝑑𝑥𝑥2 ⋅ 𝑑𝑑𝑦𝑦3 − 𝑑𝑑𝑦𝑦2 ⋅ 𝑑𝑑𝑥𝑥3

    • �⃗�𝑣 × 𝑤𝑤 = −𝑤𝑤 × �⃗�𝑣 (remember “right-hand” rule)

    • We can show: �⃗�𝑣 × 𝑤𝑤 = �⃗�𝑣 ⋅ 𝑤𝑤 ⋅ sin𝜃𝜃 ⋅ 𝑛𝑛,

    where 𝑛𝑛 is the unit vector normal to �⃗�𝑣 and 𝑤𝑤 �⃗�𝑣 × �⃗�𝑣 = 0

  • 3D Line Segment• Linear path between two points

    Origin

  • 3D Line Segment• Use a linear combination of two points

    Parametric representation:» 𝑝𝑝 𝑡𝑡 = 𝑝𝑝1 + 𝑡𝑡 ⋅ (𝑝𝑝2 − 𝑝𝑝1), (0 ≤ 𝑡𝑡 ≤ 1)

    struct Segment3D{

    Point3D p1 , p2;};

    𝑝𝑝1

    𝑝𝑝2

    Origin

  • 3D Ray• Line segment with one endpoint at infinity

    Parametric representation: » 𝑝𝑝 𝑡𝑡 = 𝑝𝑝1 + 𝑡𝑡 ⋅ �⃗�𝑣, (0 ≤ 𝑡𝑡 < ∞)

    𝑝𝑝1

    �⃗�𝑣

    Origin

    struct Ray3D{

    Point3D p1;Vector3D v;

    };

  • 3D Line• Line segment with both endpoints at infinity

    Parametric representation: » 𝑝𝑝 𝑡𝑡 = 𝑝𝑝1 + 𝑡𝑡 ⋅ �⃗�𝑣, (−∞ < 𝑡𝑡 < ∞)

    𝑝𝑝1

    struct Line3D{

    Point3D p1;Vector3D v;

    };

    �⃗�𝑣

    Origin

  • Origin

    3D Plane• A linear combination of three points

    𝑝𝑝1

    𝑝𝑝3𝑝𝑝2

  • Origin

    3D Plane• A linear combination of three points

    Implicit representation: » Φ 𝑝𝑝 = 𝑎𝑎𝑥𝑥 + 𝑏𝑏𝑦𝑦 + 𝑐𝑐𝑧𝑧 − 𝑑𝑑 = 0» Φ 𝑝𝑝 = 𝑝𝑝,𝑛𝑛 − 𝑑𝑑 = 0

    𝑛𝑛 is the plane normal» (May be) unit-length vector» Perpendicular to plane

    𝑑𝑑 is the signed (weighted) distanceof the plane from the origin.

    struct Plane3D{

    Vector3D n;float d;

    };

    𝑛𝑛 = (𝑎𝑎, 𝑏𝑏, 𝑐𝑐)

    𝑑𝑑

    𝑝𝑝1

    𝑝𝑝3𝑝𝑝2

  • 3D Polygon• Area “inside” a sequence of coplanar points

    Triangle Quadrilateral Convex Star-shaped Concave Self-intersecting

    Holes (use > 1 polygon struct)

    struct Polygon3D{

    Point3D *points;int npoints;

    };

    Points are in counter-clockwise order

  • 3D Sphere• All points at distance 𝑟𝑟 from center point 𝑐𝑐 = (𝑐𝑐𝑥𝑥 , 𝑐𝑐𝑦𝑦, 𝑐𝑐𝑧𝑧)

    Implicit representation:» Φ 𝑝𝑝 = 𝑝𝑝 − 𝑐𝑐 2 − 𝑟𝑟2 = 0

    Parametric representation:» 𝑥𝑥 𝜙𝜙,𝜃𝜃 = 𝑟𝑟 ⋅ cos𝜙𝜙 ⋅ sin𝜃𝜃 + 𝑐𝑐𝑥𝑥» 𝑦𝑦 𝜙𝜙,𝜃𝜃 = 𝑟𝑟 ⋅ cos𝜙𝜙 ⋅ sin𝜃𝜃 + 𝑐𝑐𝑦𝑦» 𝑧𝑧 𝜃𝜃,𝜙𝜙 = 𝑟𝑟 ⋅ sin𝜙𝜙 + 𝑐𝑐𝑧𝑧

    struct Sphere3D{

    Point3D center;float radius;

    };

    𝑟𝑟

    Origin

    𝑐𝑐

  • Other 3D primitives• Cone

    • Cylinder

    • Ellipsoid

    • Box

    • Etc.

  • 3D Geometric Primitives• More detail on 3D modeling later in course

    Point Line segment Triangle Polygon Polyhedron Curved surface Solid object etc.

  • Overview• 3D scene representation

    • 3D viewer representation

    • What do we see?

    • How does it look?

    How is the viewing devicedescribed in a computer?

  • Camera Models

    • The most common model is pin-hole camera All captured light rays arrive along paths toward focal point

    without lens distortion (everything is in focus)

    Other models consider ...Depth of fieldMotion blurLens distortion

    View plane

    Eye position(focal point)

  • Camera Parameters• What are the parameters of a camera?

  • Camera Parameters

    right

    back

    Up direction

    Eye Position

    ViewPlane

    “Look at”Point

    • Position Eye position: Point3D eye

    • Orientation View direction: Vector3D view Up direction: Vector3D up

    • Aperture Field of view angle: float xFov , yFov Resolution of film plane: int width , height Distance of film plane (Orientation of film plane)

  • Other Models: Depth of Field

    Close Focused Distance Focused

    P. Haeberli

  • Other Models: Motion Blur

    Brostow & Essa

    • Mimics effect of open camera shutter

    • Gives perceptual effect of high-speed motion

    • Generally involves temporal super-sampling

  • Traditional Pinhole Camera• The film sits behind the pinhole of the camera.

    • Rays come in from the outside, pass through the pinhole, and hit the film plane.

    Film Plane Pinhole

  • Traditional Pinhole Camera• The film sits behind the pinhole of the camera.

    • Rays come in from the outside, pass through the pinhole, and hit the film plane.

    Film Plane Pinhole

    Photograph is upside down

  • Virtual Camera• The film sits in front of the pinhole of the camera.

    • Rays come in from the outside, pass through the film plane, and hit the pinhole.

    Film PlanePinhole

  • Virtual Camera• The film sits in front of the pinhole of the camera.

    • Rays come in from the outside, pass through the film plane, and hit the pinhole.

    Film PlanePinhole

    Photograph is right side up

  • Overview• 3D scene representation

    • 3D viewer representation

    • Ray Casting Where are we looking? What do we see? How does it look?

  • Ray Casting• For each sample …

    Where: Construct ray from eye through view plane What: Find first surface intersected by ray through pixel How: Compute color sample based on surface radiance

  • Ray Casting• Simple implementation:

    Image RayCast( Camera camera , Scene scene , int width , int height){

    Image image = new Image( width , height );for( int i=0 ; i

  • Ray CastingWhere?

    Image RayCast( Camera camera , Scene scene , int width , int height){

    Image image = new Image( width , height );for( int i=0 ; i

  • Constructing a Ray Through a Pixel

    right

    back

    Up direction

    𝒑𝒑𝟎𝟎

    ViewPlane

    𝒗𝒗

    𝒑𝒑 𝒊𝒊 [𝒋𝒋]

  • Constructing a Ray Through a Pixel

    right

    back

    Up direction ViewPlane

    The ray originates at 𝑝𝑝0 (the position of the camera).So the equation for the ray is:

    Ray 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣

    𝒑𝒑𝟎𝟎

    𝒗𝒗

    𝒑𝒑 𝒊𝒊 [𝒋𝒋]

  • Constructing a Ray Through a Pixel

    right

    back

    Up direction ViewPlane

    If the ray passes through the point 𝑝𝑝 𝑖𝑖 [𝑗𝑗], then the solution for �⃗�𝑣 is:

    �⃗�𝑣 = 𝑝𝑝 𝑖𝑖 [𝑗𝑗]−𝑝𝑝0𝑝𝑝 𝑖𝑖 [𝑗𝑗]−𝑝𝑝0

    𝒑𝒑𝟎𝟎

    𝒗𝒗

    𝒑𝒑 𝒊𝒊 [𝒋𝒋]

  • Constructing a Ray Through a Pixel

    right

    back

    Up direction ViewPlane

    If 𝑝𝑝 𝑖𝑖 [𝑗𝑗] represents the (𝑖𝑖, 𝑗𝑗)-th pixel of the image, what is its position?

    𝒑𝒑𝟎𝟎

    𝒗𝒗

    𝒑𝒑 𝒊𝒊 [𝒋𝒋]

  • Constructing Ray Through a Pixel• 2D Example: Side view of camera at 𝑝𝑝0

    Where is the 𝑖𝑖-th pixel, 𝑝𝑝[𝑖𝑖]? (𝑖𝑖 ∈ [0, height))

    𝑑𝑑towards𝑝𝑝0

    up

    𝜃𝜃 = field of view angle (given)𝑑𝑑 = distance to view plane (arbitrary = you pick)

    𝜃𝜃

  • Constructing Ray Through a Pixel• 2D Example: Side view of camera at 𝑝𝑝0

    Where is the 𝑖𝑖-th pixel, 𝑝𝑝[𝑖𝑖]? (𝑖𝑖 ∈ [0, height))

    𝑝𝑝2

    𝑝𝑝1

    2⋅𝑑𝑑

    ⋅tan𝜃𝜃/2

    𝜃𝜃 = field of view angle (given)𝑑𝑑 = distance to view plane (arbitrary = you pick)

    𝑝𝑝1 = 𝑝𝑝0 + 𝑑𝑑 ⋅ towards − 𝑑𝑑 ⋅ tan𝜃𝜃2⋅ up

    𝑝𝑝2 = 𝑝𝑝0 + 𝑑𝑑 ⋅ towards + 𝑑𝑑 ⋅ tan𝜃𝜃2⋅ up

    𝑑𝑑towards𝑝𝑝0

    up

    𝜃𝜃

  • Constructing Ray Through a Pixel• 2D Example: Side view of camera at 𝑝𝑝0

    Where is the 𝑖𝑖-th pixel, 𝑝𝑝[𝑖𝑖]? (𝑖𝑖 ∈ [0, height))

    𝑝𝑝 𝑖𝑖 = 𝑝𝑝1 +𝑖𝑖 + 0.5height

    ⋅ 𝑝𝑝2 − 𝑝𝑝1

    𝜃𝜃 = field of view angle (given)𝑑𝑑 = distance to view plane (arbitrary = you pick)

    𝑝𝑝1 = 𝑝𝑝0 + 𝑑𝑑 ⋅ towards − 𝑑𝑑 ⋅ tan𝜃𝜃2⋅ up

    𝑝𝑝2 = 𝑝𝑝0 + 𝑑𝑑 ⋅ towards + 𝑑𝑑 ⋅ tan𝜃𝜃2⋅ up

    𝑝𝑝2

    𝑝𝑝1

    𝑑𝑑towards𝑝𝑝0

    up

    𝑝𝑝[𝑖𝑖]

    𝜃𝜃

    2⋅𝑑𝑑

    ⋅tan𝜃𝜃/2

  • Constructing Ray Through a Pixel• 2D Example:

    The ray passing through the 𝑖𝑖-th pixel is defined by:Ray 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣

    𝑝𝑝0: camera position �⃗�𝑣: direction to the 𝑖𝑖-th pixel:

    �⃗�𝑣 =𝑝𝑝 𝑖𝑖 − 𝑝𝑝0𝑝𝑝 𝑖𝑖 − 𝑝𝑝0

    𝑝𝑝[𝑖𝑖]: 𝑖𝑖-th pixel location:

    𝑝𝑝 𝑖𝑖 = 𝑝𝑝1 +𝑖𝑖 + 0.5height

    ⋅ 𝑝𝑝2 − 𝑝𝑝1

    𝑝𝑝1 and 𝑝𝑝2 are the endpoints of the view plane:𝑝𝑝1 = 𝑝𝑝0 + 𝑑𝑑 ⋅ towards − 𝑑𝑑 ⋅ tan𝜃𝜃/2 ⋅ up𝑝𝑝2 = 𝑝𝑝0 + 𝑑𝑑 ⋅ towards + 𝑑𝑑 ⋅ tan𝜃𝜃/2 ⋅ up

    𝑝𝑝2

    𝑝𝑝1

    𝑑𝑑towards𝑝𝑝0

    up

    𝑝𝑝[𝑖𝑖]

    𝜃𝜃

    2⋅𝑑𝑑

    ⋅tan𝜃𝜃/2

  • Constructing Ray Through a PixelFiguring out how to do this in 3D is assignment 2

  • Constructing Ray Through a PixelFiguring out how to do this in 3D is assignment 2

    Note: Given the vertical field of view angle, 𝜃𝜃𝑣𝑣

    𝜽𝜽𝒗𝒗

  • Constructing Ray Through a PixelFiguring out how to do this in 3D is assignment 2

    Note: Given the vertical field of view angle, 𝜃𝜃𝑣𝑣 And the aspect ratio, 𝑎𝑎𝑟𝑟 = ℎ𝑒𝑒𝑖𝑖𝑒𝑒ℎ𝑒𝑒

    𝑤𝑤𝑖𝑖𝑤𝑤𝑒𝑒ℎ

    𝜽𝜽𝒗𝒗

  • Constructing Ray Through a PixelFiguring out how to do this in 3D is assignment 2

    Note: Given the vertical field of view angle, 𝜃𝜃𝑣𝑣 And the aspect ratio, 𝑎𝑎𝑟𝑟 = ℎ𝑒𝑒𝑖𝑖𝑒𝑒ℎ𝑒𝑒

    𝑤𝑤𝑖𝑖𝑤𝑤𝑒𝑒ℎThe horizontal field of view angle, 𝜃𝜃ℎ, satisfies:

    tan 𝜃𝜃𝑣𝑣/2tan 𝜃𝜃ℎ/2

    = 𝑎𝑎𝑟𝑟

    𝜽𝜽𝒉𝒉

    𝜽𝜽𝒗𝒗

  • Ray CastingWhere?

    Image RayCast( Camera camera , Scene scene , int width , int height){

    Image image = new Image( width , height );for( int i=0 ; i

  • Ray CastingWhat?

    Image RayCast( Camera camera , Scene scene , int width , int height){

    Image image = new Image( width , height );for( int i=0 ; i

  • Ray-Scene Intersection• Intersections with geometric primitives

    Sphere Triangle

  • Ray-Sphere Intersection

    Ray: 𝑝𝑝 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣, (0 ≤ 𝑡𝑡 < ∞)Sphere: Φ 𝑝𝑝 = 𝑝𝑝 − 𝑐𝑐 2 − 𝑟𝑟2 = 0

    𝑝𝑝0

    𝒗𝒗

    𝑐𝑐

    𝑝𝑝

    𝑟𝑟

    𝑝𝑝′

  • Ray-Sphere Intersection

    Ray: 𝑝𝑝 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣, (0 ≤ 𝑡𝑡 < ∞)Sphere: Φ 𝑝𝑝 = 𝑝𝑝 − 𝑐𝑐 2 − 𝑟𝑟2 = 0

    Substituting for 𝑝𝑝, we get:Φ 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣 − 𝑐𝑐 2 − 𝑟𝑟2 = 0

    Solve quadratic equation:𝑎𝑎 ⋅ 𝑡𝑡2 + 𝑏𝑏 ⋅ 𝑡𝑡 + 𝑐𝑐 = 0

    where:𝑎𝑎 = 1𝑏𝑏 = 2 �⃗�𝑣,𝑝𝑝0 − 𝑐𝑐𝑐𝑐 = 𝑝𝑝0 − 𝑐𝑐 2 − 𝑟𝑟2

    𝑝𝑝0

    𝒗𝒗

    𝑐𝑐

    𝑝𝑝𝑟𝑟

    𝑝𝑝𝑝

  • Ray-Sphere Intersection

    Ray: 𝑝𝑝 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣, (0 ≤ 𝑡𝑡 < ∞)Sphere: Φ 𝑝𝑝 = 𝑝𝑝 − 𝑐𝑐 2 − 𝑟𝑟2 = 0

    Substituting for 𝑝𝑝, we get:Φ 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣 − 𝑐𝑐 2 − 𝑟𝑟2 = 0

    Solve quadratic equation:𝑎𝑎 ⋅ 𝑡𝑡2 + 𝑏𝑏 ⋅ 𝑡𝑡 + 𝑐𝑐 = 0

    where:𝑎𝑎 = 1𝑏𝑏 = 2 �⃗�𝑣,𝑝𝑝0 − 𝑐𝑐𝑐𝑐 = 𝑝𝑝0 − 𝑐𝑐 2 − 𝑟𝑟2

    𝑝𝑝0

    𝒗𝒗

    𝑐𝑐

    𝑝𝑝𝑟𝑟

    𝑝𝑝𝑝

    Generally, there are two solutions to the quadratic equation, giving two points of intersection, 𝑝𝑝 and 𝑝𝑝𝑝.Want to return the first positive hit.

  • Ray-Sphere Intersection

    𝑝𝑝0

    �⃗�𝑣

    𝑐𝑐

    𝑝𝑝 𝑟𝑟

    𝑛𝑛

    • Need normal vector at intersection for lighting calculations:

    𝑛𝑛 =𝑝𝑝 − 𝑐𝑐𝑝𝑝 − 𝑐𝑐

  • Ray-Sphere Intersection• More generally, if the shape is given as the set of

    points 𝑝𝑝 satisfying:Φ 𝑝𝑝 = 0

    for some function Φ:ℝ3 → ℝ, then the normal of the surface will be parallel to the gradient.

  • Ray-Scene Intersection• Intersections with geometric primitives

    Sphere» Triangle

  • Ray-Triangle Intersection1. Intersect ray with plane

    2. Check if the point is inside the triangle

    𝑝𝑝

    𝑝𝑝0

    �⃗�𝑣

  • Ray-Plane Intersection

    Ray: 𝑝𝑝 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣, (0 ≤ 𝑡𝑡 < ∞)Plane: Φ 𝑝𝑝 = 𝑝𝑝,𝑛𝑛 − 𝑑𝑑 = 0

    Substituting for 𝑝𝑝, we get:Φ 𝑡𝑡 = 𝑝𝑝0 + 𝑡𝑡 ⋅ �⃗�𝑣,𝑛𝑛 − 𝑑𝑑 = 0

    Solution:

    𝑡𝑡 = −𝑝𝑝0,𝑛𝑛 − 𝑑𝑑�⃗�𝑣,𝑛𝑛 𝑛𝑛

    𝑝𝑝

    𝑝𝑝0

    �⃗�𝑣

    Algebraic Method

    What are the implications of �⃗�𝑣,𝑛𝑛 = 0?

  • Ray-Triangle Intersection I• Check for point-triangle intersection algebraically:

    Generate planes through the raysource and each edge

    Check if the point of intersection isabove each of these planes

    𝑝𝑝0

    𝑣𝑣2

    𝑣𝑣3

    For each edge:𝑛𝑛𝑖𝑖 = (𝑣𝑣𝑖𝑖+1−𝑝𝑝0) × (𝑣𝑣𝑖𝑖 − 𝑝𝑝0)if ( 𝑝𝑝 − 𝑝𝑝0,𝑛𝑛𝑖𝑖 < 0 )

    return FALSE;𝑛𝑛1

    𝑣𝑣1

    𝑝𝑝

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    In general, given 𝑝𝑝 ∈ ℝ3 and giventhree points 𝑣𝑣1,𝑣𝑣2,𝑣𝑣3 ⊂ ℝ3(in general position) we cansolve for 𝛼𝛼,𝛽𝛽, 𝛾𝛾 ∈ ℝ such that:

    𝑝𝑝 = 𝛼𝛼𝑣𝑣1 + 𝛽𝛽𝑣𝑣2 + 𝛾𝛾𝑣𝑣3

    𝑝𝑝 is in the plane spanned by 𝑣𝑣1,𝑣𝑣2,𝑣𝑣3 iff.:𝛼𝛼 + 𝛽𝛽 + 𝛾𝛾 = 1

    𝑝𝑝 is inside the triangle with vertices {𝑣𝑣1,𝑣𝑣2,𝑣𝑣3} iff.:𝛼𝛼,𝛽𝛽, 𝛾𝛾 ≥ 0

    𝑝𝑝

    𝑣𝑣1𝑣𝑣2

    𝑣𝑣3

    𝛼𝛼 𝛽𝛽

    𝛾𝛾

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    In general, given 𝑝𝑝 ∈ ℝ3 and giventhree points 𝑣𝑣1,𝑣𝑣2,𝑣𝑣3 ⊂ ℝ3(in general position) we cansolve for 𝛼𝛼,𝛽𝛽, 𝛾𝛾 ∈ ℝ such that:

    𝑝𝑝 = 𝛼𝛼𝑣𝑣1 + 𝛽𝛽𝑣𝑣2 + 𝛾𝛾𝑣𝑣3

    To get 𝛼𝛼,𝛽𝛽, 𝛾𝛾, solve the system:𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    ⇔𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣11

    −1𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    𝑝𝑝𝛼𝛼 𝛽𝛽

    𝛾𝛾

    𝑣𝑣1𝑣𝑣2

    𝑣𝑣3

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    In general, given 𝑝𝑝 ∈ ℝ3 and giventhree points 𝑣𝑣1,𝑣𝑣2,𝑣𝑣3 ⊂ ℝ3(in general position) we cansolve for 𝛼𝛼,𝛽𝛽, 𝛾𝛾 ∈ ℝ such that:

    𝑝𝑝 = 𝛼𝛼𝑣𝑣1 + 𝛽𝛽𝑣𝑣2 + 𝛾𝛾𝑣𝑣3

    To get 𝛼𝛼,𝛽𝛽, 𝛾𝛾, solve the system:𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    ⇔𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣31

    −1𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    𝑝𝑝𝛼𝛼 𝛽𝛽

    𝛾𝛾

    𝑣𝑣1𝑣𝑣2

    𝑣𝑣3

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    In general, given 𝑝𝑝 ∈ ℝ3 and giventhree points 𝑣𝑣1,𝑣𝑣2,𝑣𝑣3 ⊂ ℝ3(in general position) we cansolve for 𝛼𝛼,𝛽𝛽, 𝛾𝛾 ∈ ℝ such that:

    𝑝𝑝 = 𝛼𝛼𝑣𝑣1 + 𝛽𝛽𝑣𝑣2 + 𝛾𝛾𝑣𝑣3

    To get 𝛼𝛼,𝛽𝛽, 𝛾𝛾, solve the system:𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    ⇔𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    −1 𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    𝑝𝑝𝛼𝛼 𝛽𝛽

    𝛾𝛾

    𝑣𝑣1𝑣𝑣2

    𝑣𝑣3

    This will fail if the vertices 𝑣𝑣1,𝑣𝑣2,𝑣𝑣3lie in a plane through the origin.

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    Embrace the problem case by translating the whole system to the origin:

    𝑝𝑝

    𝑣𝑣1 𝑣𝑣2

    𝑣𝑣3

    𝛼𝛼 𝛽𝛽𝛾𝛾

    𝑝𝑝 − 𝑣𝑣1

    𝑣𝑣2 − 𝑣𝑣1

    𝑣𝑣3 − 𝑣𝑣1

    𝛼𝛼 𝛽𝛽𝛾𝛾

    𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    0 𝑣𝑣2𝑥𝑥 − 𝑣𝑣1𝑥𝑥 𝑣𝑣3𝑥𝑥 − 𝑣𝑣1𝑥𝑥

    0 𝑣𝑣2𝑦𝑦 − 𝑣𝑣1

    𝑦𝑦 𝑣𝑣3𝑦𝑦 − 𝑣𝑣1

    𝑦𝑦

    0 𝑣𝑣2𝑧𝑧 − 𝑣𝑣1𝑧𝑧 𝑣𝑣3𝑧𝑧 − 𝑣𝑣1𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥 − 𝑣𝑣1𝑥𝑥

    𝑝𝑝𝑦𝑦 − 𝑣𝑣1𝑦𝑦

    𝑝𝑝𝑧𝑧 − 𝑣𝑣1𝑧𝑧

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    Embrace the problem case by translating the whole system to the origin:

    𝑝𝑝

    𝑣𝑣1 𝑣𝑣2

    𝑣𝑣3

    𝛼𝛼 𝛽𝛽𝛾𝛾

    𝑝𝑝 − 𝑣𝑣1

    𝑣𝑣2 − 𝑣𝑣1

    𝑣𝑣3 − 𝑣𝑣1

    𝛼𝛼 𝛽𝛽𝛾𝛾

    𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    𝑣𝑣2𝑥𝑥 − 𝑣𝑣1𝑥𝑥 𝑣𝑣3𝑥𝑥 − 𝑣𝑣1𝑥𝑥

    𝑣𝑣2𝑦𝑦 − 𝑣𝑣1

    𝑦𝑦 𝑣𝑣3𝑦𝑦 − 𝑣𝑣1

    𝑦𝑦

    𝑣𝑣2𝑧𝑧 − 𝑣𝑣1𝑧𝑧 𝑣𝑣3𝑧𝑧 − 𝑣𝑣1𝑧𝑧𝛽𝛽𝛾𝛾 =

    𝑝𝑝𝑥𝑥 − 𝑣𝑣1𝑥𝑥

    𝑝𝑝𝑦𝑦 − 𝑣𝑣1𝑦𝑦

    𝑝𝑝𝑧𝑧 − 𝑣𝑣1𝑧𝑧

  • Ray-Triangle Intersection II• Check for point-triangle intersection parametrically

    Embrace the problem case by translating the whole system to the origin:

    𝑝𝑝

    𝑣𝑣1 𝑣𝑣2

    𝑣𝑣3

    𝛼𝛼 𝛽𝛽𝛾𝛾

    𝑝𝑝 − 𝑣𝑣1

    𝑣𝑣2 − 𝑣𝑣1

    𝑣𝑣3 − 𝑣𝑣1

    𝛼𝛼 𝛽𝛽𝛾𝛾

    𝑣𝑣1𝑥𝑥 𝑣𝑣2𝑥𝑥 𝑣𝑣3𝑥𝑥

    𝑣𝑣1𝑦𝑦 𝑣𝑣2

    𝑦𝑦 𝑣𝑣3𝑦𝑦

    𝑣𝑣1𝑧𝑧 𝑣𝑣2𝑧𝑧 𝑣𝑣3𝑧𝑧

    𝛼𝛼𝛽𝛽𝛾𝛾

    =𝑝𝑝𝑥𝑥𝑝𝑝𝑦𝑦𝑝𝑝𝑧𝑧

    This is an over-constrained system!

    This is an over-constrained system!In general, we can’t express a 3D point as the

    linear combination of two 3D points.This is not the general case!

    A solution exists since 𝑝𝑝 is in the plane spanned by {𝑣𝑣1,𝑣𝑣2,𝑣𝑣3}After solving for 𝛽𝛽 and 𝛾𝛾, we can set:

    𝛼𝛼 = 1 − 𝛽𝛽 − 𝛾𝛾

    𝑣𝑣2𝑥𝑥 − 𝑣𝑣1𝑥𝑥 𝑣𝑣3𝑥𝑥 − 𝑣𝑣1𝑥𝑥

    𝑣𝑣2𝑦𝑦 − 𝑣𝑣1

    𝑦𝑦 𝑣𝑣3𝑦𝑦 − 𝑣𝑣1

    𝑦𝑦

    𝑣𝑣2𝑧𝑧 − 𝑣𝑣1𝑧𝑧 𝑣𝑣3𝑧𝑧 − 𝑣𝑣1𝑧𝑧𝛽𝛽𝛾𝛾 =

    𝑝𝑝𝑥𝑥 − 𝑣𝑣1𝑥𝑥

    𝑝𝑝𝑦𝑦 − 𝑣𝑣1𝑦𝑦

    𝑝𝑝𝑧𝑧 − 𝑣𝑣1𝑧𝑧

  • Other Ray-Primitive Intersections• Cone, cylinder, ellipsoid:

    Similar to sphere

    • Box Intersect 3 front-facing planes, return closest

    • Convex polygon» Find the intersection of the ray with the plane» Check that the intersection is above every triangle

    generated by the ray source and polygon edge.

    • Concave polygon Same plane intersection More complex point-in-polygon test

    3D Rendering and Ray CastingRenderingRendering3D Rendering ExampleOverviewOverview3D Scene Representation3D Point3D Point3D Vector3D Vector3D VectorCross Product: Review3D Line Segment3D Line Segment3D Ray3D Line3D Plane3D Plane3D Polygon3D SphereOther 3D primitives3D Geometric PrimitivesOverviewCamera ModelsCamera ParametersCamera ParametersOther Models: Depth of FieldOther Models: Motion BlurTraditional Pinhole CameraTraditional Pinhole CameraVirtual CameraVirtual CameraOverviewRay CastingRay CastingRay CastingConstructing a Ray Through a PixelConstructing a Ray Through a PixelConstructing a Ray Through a PixelConstructing a Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelConstructing Ray Through a PixelRay CastingRay CastingRay-Scene IntersectionRay-Sphere IntersectionRay-Sphere IntersectionRay-Sphere IntersectionRay-Sphere IntersectionRay-Sphere IntersectionRay-Scene IntersectionRay-Triangle IntersectionRay-Plane IntersectionRay-Triangle Intersection IRay-Triangle Intersection IIRay-Triangle Intersection IIRay-Triangle Intersection IIRay-Triangle Intersection IIRay-Triangle Intersection IIRay-Triangle Intersection IIRay-Triangle Intersection IIOther Ray-Primitive Intersections