Ray Casting and Surface Intersections in Computer Graphics
Rendering Foundations and Ray Concepts
Rendering Process Overview:
- Solves the visibility / hidden surface problem by determining what scene elements are visible from a given perspective.
- Projects 3D scene objects onto a 2D sensor plane.
- Evaluates light transport and shading models, such as the Phong illumination model and the global Rendering Equation, to calculate color and luminance.
- Employs ray casting to explicitly compute ray-object intersections.
Ray Definition:
- A ray is mathematically modeled as a half-line defined by an origin point and a normalized direction vector .
- Expressed in parametric vector form as: where is the ray parameter representing distance along the direction vector.
- In orthographic projection models, rays are cast parallel to one another from the camera sensor plane.
Ray-Scene Intersection Goal:
- Computes the intersection point on a surface corresponding to a sensor position .
- Requires evaluating intersections across all scene objects to isolate the nearest surface intersection, defined by the minimal positive distance parameter .
Ray Casting vs. Ray Tracing
Ray Casting:
- Primary Objective: Solves the fundamental visibility problem ("What object is visible at each sensor pixel?").
- Primary Rays: Rays originating at or ending at the camera sensor.
- Determines the exact surface intersection position corresponding to pixel .
Ray Tracing:
- Primary Objective: Computes radiative light transport along primary rays toward the camera ("What exact color and intensity does the pixel receive?").
- Secondary Rays: Rays generated at surface interaction points that do not directly originate or terminate at the sensor plane.
- Shadow Rays: Specific secondary rays cast directly toward light sources to evaluate occlusion and direct illumination.
- Illumination Pathways:
- Ray 1 & Ray 2: Model direct illumination, traveling from light sources to surface interaction points.
- Ray 3: Models indirect illumination, capturing light emitted or reflected from one surface point toward another surface point .
- Ray 4: Models primary incoming light transported from surface point directly into the camera sensor.
- Computational Challenge: Calculating outgoing light from point toward point depends recursively on incoming light at from point and all other surrounding surfaces. The final outgoing radiance along a primary ray is the integral sum of incoming radiance from all incident directions (secondary rays), weighted by local surface material properties (BRDF).
Intersection with Implicit Surfaces
Implicit Surface Formulation:
- Defined implicitly as the zero-level set of a scalar function :
- An intersection with a ray occurs at parameter values that satisfy:
Plane Intersection Example:
- A plane is defined implicitly by a reference point , a surface point , and a unit surface normal vector :
- Substituting the ray equation yields:
- Solving explicitly for the parametric intersection distance :
- Valid solution condition: A unique intersection exists provided the ray direction is not orthogonal to the surface normal ().
Surface Normal Derivation:
- The normalized vector perpendicular to an implicit surface at point is given by the spatial gradient of the implicit function:
- For a planar implicit function , the gradient directly simplifies to the plane normal vector .
Intersection with Quadric Surfaces
Quadric Surface Properties:
- Surfaces represented by general second-degree polynomial quadratic equations.
- Include geometric primitives such as spheres, ellipsoids, paraboloids, hyperboloids, cones, and cylinders.
- Intersecting a straight ray with a quadric yields a quadratic equation in parameter , resulting in zero, one, or two real intersection solutions.
Unit Sphere at Origin:
- Implicit equation for a unit sphere centered at origin with radius :
- Substituting ray equation :
- Standard quadratic form coefficients:
- Surface normal at intersection point :
Discriminant Analysis ():
- Zero Intersections (): Ray misses the sphere entirely; no real solutions exist.
- One Intersection (): Ray is perfectly tangent to the sphere surface at point with a single root:
- Two Intersections (): Ray pierces the sphere, entering at and exiting at :
Intersection with Parametric Surfaces
Parametric Surface Formulation:
- Defined using continuous 2D parameter pairs over domain boundaries:
- Ray intersection requires solving a non-linear system of 3 simultaneous equations with 3 unknown variables :
Tangent and Normal Vectors:
- Surface tangent vectors along parameter directions:
- Surface normal vector defined by the cross product of parameter tangents:
Specific Parametric Primitives:
- Cylinder (aligned with z-axis, radius , height , parameters , ):
- Sphere (centered at origin, radius , parameters , ):
- Disk (at height along z-axis, outer radius , inner radius , parameters , ):
- Cone (base radius , height , parameters , ):
- Bounds on parameter domains enable precise rendering of partial, bounded geometric sub-sections.
Compound Objects and Constructive Solid Geometry (CSG)
Constructive Solid Geometry Concepts:
- Combines simple solid primitives (cubes, spheres, cylinders) to build complex volumetric geometries via Boolean operators.
- Requires strictly closed manifold surfaces and well-defined volumetric interiors.
Boolean CSG Operations:
- Union (): Combines volumes of both and .
- Intersection (): Retains only the overlapping volume common to both and .
- Difference ( or ): Subtracts volume from volume . For example, subtracting a sphere from a cube creates a hollow spherical boundary inside the cube where sphere intersections are valid only inside the cube bounds, while cube surfaces inside the sphere are discarded.
Implementation via Ray-Interval Analysis:
- Computes all entry and exit intersection parameter intervals along a ray for each primitive object.
- Union: Selects the overall closest entering intersection point across primitives.
- Intersection: Selects the closest intersection of located inside volume , or the closest intersection of inside volume .
- Difference (): Evaluates ray intervals to keep intersections on outside , or surface intersections on inside with reversed normal directions.
Ray-Triangle Intersections and Barycentric Coordinates
Triangle Representation:
- Triangle meshes form the primary approximate surface representation for complex models (e.g., Stanford bunny).
- Defined by 3 vertices forming a planar face.
Barycentric Coordinates:
- Expresses any point lying within the plane of triangle as a weighted linear combination:
- Constraint equation:
- Simplified 2-parameter expression:
Spatial Domain Properties of Barycentric Coordinates:
- Vertices: , , .
- Triangle Edges: One coordinate equals , while the remaining two parameters lie within .
- Interior Point: , , and (ensuring ).
- Exterior Point: , , or .
System of Linear Equations for Ray Intersection:
- Equating the parametric ray equation and triangle surface equation:
- Rearranging terms into matrix-vector form:
Direct Solution via Cramer's Rule:
- Let edge vectors , , and origin vector \mathbf{s} = \mathbf{o} - \mathbf{p}_0$.\n - Solved numerically using scalar triple products (evaluating parallelepiped volumes):\n \begin{pmatrix} t \ \beta \ \gamma \end{pmatrix} = \frac{1}{(-\mathbf{d}) \cdot (\mathbf{e}1 \times \mathbf{e}_2)} \begin{pmatrix} \mathbf{s} \cdot (\mathbf{e}_1 \times \mathbf{e}_2) \ (-\mathbf{d}) \cdot (\mathbf{s} \times \mathbf{e}_2) \ (-\mathbf{d}) \cdot (\mathbf{e}_1 \times \mathbf{s}) \end{pmatrix}\n\n- **Intersection Criteria**:\n 1. Non-degenerated triangle with ray not parallel to plane: Determinant D = (-\mathbf{d}) \cdot (\mathbf{e}_1 \times \mathbf{e}_2) \neq 0\n 2. Point inside triangle boundary: \beta \ge 0\gamma \ge 0\beta + \gamma \le 1\n 3. Intersection in front of sensor: t \ge 0\n\n\n# Axis-Aligned Bounding Boxes (AABB) and Acceleration Structures\n\n- **Acceleration Structure Motivation**:\n - Encloses complex, high-polygon geometry inside simpler proxy geometries with highly efficient intersection tests.\n - Rays that miss the simplified outer proxy bounding box are immediately pruned, avoiding expensive individual triangle intersection checks.\n\n- **Axis-Aligned Bounding Box Characteristics**:\n - Alignment: Bounding surfaces are strictly aligned with principal coordinate axes (x, y, z).\n - Representation: Represented compactly by one 1D range interval per spatial dimension ([x_{\min}, x_{\max}], [y_{\min}, y_{\max}], [z_{\min}, z_{\max}]).\n - Transformation behavior: Easily translated alongside objects; however, non-translational transformations (such as rotations) require recomputing bounding intervals.\n - Bounding proxy alternatives include Object-Oriented Boxes (OBB), kk-DOPs), and Bounding Spheres.\n\n- **Slab Intersection Method**:\n - A 3D box is defined by three intersecting orthogonal pairs of parallel planes called "slabs".\n - Ray-plane parameter math per coordinate axis (e.g., x-axis slab):\n t{x1} = \frac{x_{\min} - o_x}{d_x}\n t_{x2} = \frac{x_{\max} - o_x}{d_x}\n - Spatial entry and exit parameters for x-slab:\n t_{x,\min} = \min(t_{x1}, t_{x2})\n t_{x,\max} = \max(t_{x1}, t_{x2})\n - Overlap condition: A valid ray-box intersection occurs if and only if the maximum entry parameter across all slabs is less than or equal to the minimum exit parameter across all slabs:\n t_{\text{enter}} = \max(t_{x,\min}, t_{y,\min}, t_{z,\min}) \le t_{\text{exit}} = \min(t_{x,\max}, t_{y,\max}, t_{z,\max})\n - Non-overlapping interval ranges indicate the ray completely misses the bounding box.\n\n- **Bounding Volume Hierarchies (BVH)**:\n - Hierarchical tree structure combining nested AABBs.\n - Structure: Root AABB \rightarrow\rightarrow Leaf node object primitives.\n - Traversal algorithm: Test ray against parent bounding box; if intersected, recursively traverse child nodes. If missed, prune entire sub-tree.\n - Performance impact: Reduces computational complexity from linear search O(n)O(\log n)n polygon faces.\n - Overhead: Introduces additional memory footprint and preprocessing build times.\n\n\n# Iso-Surfaces in Grids and Volumetric Data\n\n- **Volumetric Surface Motivation**:\n - Enables ray casting of fluid surfaces directly from particle simulations without constructing explicit polygon surface meshes.\n\n- **Density Mapping onto 3D Grid**:\n - Fluid simulation particles carry discrete scalar mass density quantities.\n - Particle density values are mapped and interpolated onto surrounding regular 3D grid cell nodes.\n\n- **Trilinear Grid Cell Interpolation**:\n - Scalar density \rho\mathbf{p} = (x, y, z) using continuous trilinear interpolation across the 8 surrounding grid corner nodes.\n\n- **Ray-Isosurface Intersection**:\n - Defines a target threshold scalar iso-value \rho_{\text{iso}}.\n - Evaluates ray equation \mathbf{r}(t) = \mathbf{o} + t\,\mathbf{d}.\n - Solves for distance parameter t where interpolated grid cell density matches the iso-value threshold:\n \rho(\mathbf{r}(t)) = \rho_{\text{iso}}\n subject to t \ge 0\mathbf{r}(t) being contained within cell bounds.\n\n- **Isosurface Intersection Normal**:\n - Surface normal at an iso-surface point is defined by the normalized spatial gradient vector of the scalar density field:\n \mathbf{n} = \nabla \rho(\mathbf{r}(t))\n - Numerically approximated during ray casting using finite difference schemes across neighboring grid cell values.\n\n\n# Summary and Structural Overview\n\n- **Versatility of Ray Casting**:\n - Acts as a core rendering mechanism to evaluate sensor visibility across diverse geometric representations, including implicit surfaces, quadric volumes, parametric patches, polygon meshes, CSG solids, and grid-based volume iso-surfaces.\n- **Computational Complexity Management**:\n - Unaccelerated intersection evaluation is computationally expensive for high-complexity models.\n - Spatial acceleration structures (such as Bounding Volume Hierarchies utilizing Axis-Aligned Bounding Boxes) reduce scene interaction complexity to O(\log n)$$.
Mathematical Spectrum:
- Standard primitives utilize closed-form linear or quadratic algebraic formulas (planes, triangles, spheres).
- Volumetric isosurfaces require dynamic spatial evaluations, such as grid interpolation combined with numerical gradient extraction.