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
GeometricPrimitives (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?
Overview
• 3D scene representation • 3D viewer representation • What do we see?
• How does it look?
How is the 3D scene described 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
3D Point
• Specifies a location
Represented by three coordinates
Infinitely small struct Point3D { float x , y , z; }; (𝑥𝑥,𝑦𝑦,𝑧𝑧) Origin
3D Vector
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 𝑤𝑤
3D Line Segment
• Linear path between two points
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) distance
of 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;
};
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 device described 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 field Motion blur Lens distortion View plane Eye position (focal point)
Camera Parameters
Camera Parameters
right back Up direction Eye Position View Plane “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
Other Models: Depth of Field
Close Focused Distance Focused
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.
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
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 Plane Pinhole
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 Plane Pinhole
Overview
• 3D scene representation • 3D viewer representation • Ray Casting
Where are we looking?
What do we see?
Ray Casting
• For each sample …
Where: Construct ray from eye through view plane
What: Find first surface intersected by ray through pixel
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<width ; i++ ) for( int j=0 ; j<height ; j++ ) {
Ray ray = ConstructRayThroughPixel( camera , i , j );
Intersection hit = FindIntersection( ray , scene ); image[i][j] = GetColor( hit );
}
return image; }
Ray Casting
Where?
Image RayCast( Camera camera , Scene scene , int width , int height) {
Image image = new Image( width , height );
for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) {
Ray ray = ConstructRayThroughPixel( camera , i , j );
Intersection hit = FindIntersection( ray , scene ); image[i][j] = GetColor( hit );
}
return image; }
Constructing a Ray Through a Pixel
right back Up direction 𝒑𝒑𝟎𝟎 View Plane 𝒗𝒗 𝒑𝒑 𝒊𝒊 [𝒋𝒋]Constructing a Ray Through a Pixel
right back
Up direction View
Plane
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 View
Plane
If the ray passes through the point
𝑝𝑝 𝑖𝑖 [𝑗𝑗], then the solution for ⃗𝑣𝑣 is:
⃗𝑣𝑣 = 𝑝𝑝 𝑖𝑖 [𝑗𝑗]−𝑝𝑝0
𝑝𝑝 𝑖𝑖 [𝑗𝑗]−𝑝𝑝0
𝒑𝒑𝟎𝟎
𝒗𝒗
Constructing a Ray Through a Pixel
right back
Up direction View
Plane
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 + 𝑖𝑖height+ 0.5 ⋅ 𝑝𝑝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 + 𝑖𝑖height+ 0.5 ⋅ 𝑝𝑝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 Pixel
Constructing Ray Through a Pixel
Figuring out how to do this in 3D is assignment 2 Note:
Given the vertical field of view angle, 𝜃𝜃𝑣𝑣
Constructing Ray Through a Pixel
Figuring 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 Pixel
Figuring 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 𝜃𝜃𝑣𝑣/2
tan 𝜃𝜃ℎ/2 = 𝑎𝑎𝑟𝑟
𝜽𝜽𝒉𝒉 𝜽𝜽𝒗𝒗
Ray Casting
Where?
Image RayCast( Camera camera , Scene scene , int width , int height) {
Image image = new Image( width , height );
for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) {
Ray ray = ConstructRayThroughPixel( camera , i , j );
Intersection hit = FindIntersection( ray , scene ); image[i][j] = GetColor( hit );
}
return image; }
Ray Casting
What?
Image RayCast( Camera camera , Scene scene , int width , int height) {
Image image = new Image( width , height );
for( int i=0 ; i<width ; i++ ) for( int j=0 ; j<height ; j++ ) {
Ray ray = ConstructRayThroughPixel( camera , i , j );
Intersection hit = FindIntersection( ray , scene ); image[i][j] = GetColor( hit );
}
return image; }
Ray-Scene Intersection
• Intersections with geometric primitives
Sphere
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 𝑝𝑝𝑝.
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
Ray-Triangle Intersection
1. 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
Ray-Triangle Intersection I
• Check for point-triangle intersection algebraically:
Generate planes through the ray
source and each edge
Check if the point of intersection is
above 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 given
three points 𝑣𝑣1, 𝑣𝑣2, 𝑣𝑣3 ⊂ ℝ3
(in general position) we can
solve 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 given
three points 𝑣𝑣1, 𝑣𝑣2, 𝑣𝑣3 ⊂ ℝ3
(in general position) we can
solve 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 given
three points 𝑣𝑣1, 𝑣𝑣2, 𝑣𝑣3 ⊂ ℝ3
(in general position) we can
solve 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 given
three points 𝑣𝑣1, 𝑣𝑣2, 𝑣𝑣3 ⊂ ℝ3
(in general position) we can
solve 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, 𝑣𝑣3 lie 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