 | [TOC] Chapter 3: Rays, Geometry & Shapes |  |
Geometric objects are at the core of rendering scenes. Rays intersect with these shapes to compute color, light reflection, shadows, and more. Different geometric shapes require different methods for calculating ray intersections. We'll explore several common shapes used in ray-tracing, including spheres, cylinders, disks, quadrics, triangle meshes, curves, subdivision surfaces, and handling rounding errors.
 | Spheres |  |
Spheres are one of the simplest shapes to work with in ray-tracing. The equation for a sphere with center \( C \) and radius \( r \) is:
\[
(x - C_x)^2 + (y - C_y)^2 + (z - C_z)^2 = r^2
\]
To find whether a ray intersects a sphere, we need to solve for \( t \) in the ray equation:
\[
\mathbf{P}(t) = \mathbf{O} + t \mathbf{D}
\]
where:
\( \mathbf{O} \) is the ray's origin.
\( \mathbf{D} \) is the ray's direction.
\( \mathbf{P}(t) \) is a point along the ray.
Sphere-Ray Intersection Equation
The intersection equation for a ray with a sphere can be derived by plugging the ray equation into the sphere's equation:
\[
(\mathbf{O} + t \mathbf{D} - C) \cdot (\mathbf{O} + t \mathbf{D} - C) = r^2
\]
This simplifies to:
\[
(\mathbf{D} \cdot \mathbf{D})t^2 + 2\mathbf{D} \cdot (\mathbf{O} - C)t + (\mathbf{O} - C) \cdot (\mathbf{O} - C) - r^2 = 0
\]
This is a quadratic equation in \( t \). Solving for \( t \) gives the points where the ray intersects the sphere.
Example: Sphere-Ray Intersection in JavaScript
|