Skip to content
PostComputer Graphics / Lecture

06-CG-Raycasting

2025-06-01
Back to Blog

Computer Graphics-Raycasting ​

Rendering ​

Rendering refers to the entire process that produces color values for pixels, given a 3D representation of the scene

Pixels correspond to rays; need to figure out the visible scene point along each ray

  • Called "hidden surface problem" in older texts
  • "Visibility" is a more modern term
  • Also, we assume (for now) a single ray per pixel

Major algorithms: Ray casting and rasterization

Ray Casting ​

Ray Casting Basics ​

Ray Casting

  • For every pixel
    • Construct a ray from the eye
    • For every object in the scene
      • Find intersection with the ray
      • Keep if closest

LX4VIT

Shading

  • For every pixel
    • Construct a ray from the eye
    • For every object in the scene
      • Find intersection with the ray
      • Keep if closest shade

Shading = What Surfaces Look Like

  • Surface/Scene Properties
    • surface normal
    • direction to light
    • viewpoint
  • Material Properties
    • Diffuse (matte)
    • Specular (shiny)
  • Light properties
    • Position
    • Intensity, ...

Ray casting = eye rays only, tracing = also secondary

  • Secondary rays are used for testing shadows, doing reflections, refractions, etc.

D9GN4f

光线投射:通过数学方法模拟光线传播,解决三维场景到二维图像的投影问题,核心是可见性计算。 着色:在可见性基础上,结合物理光照模型和材质属性,为每个像素赋予真实感颜色,核心是光照模拟。

光线投射是光线追踪的子集:前者是后者的“第一步”(确定可见性),后者通过扩展次级光线实现更复杂的物理模拟。

Ray Representation ​

  • Origin - Point
    • Direction - Vector
  • normalized is better
  • Parametric line
    • P(t)=origin+t∗direction
    • 2YKHMH
    • P(t)=R0+td

Camera and Ray Generation ​

Construct a ray from the eye For every object in the scene

Cameras ​

Construct a ray from the eye

  • Box with a tiny hole
  • Inverted image
  • Similar triangles
  • Perfect image if hole infinitely small
  • Pure geometric optics
  • No depth of field issue (everything in focus)

kJJyAK

Simplified Pinhole Camera

  • Eye-image pyramid (view frustum)

  • Note that the distance/size of image are arbitrary 4zsyIM

  • Eye point e (center)

  • Orthobasis u,v,w (horizontal, up, direction)

  • Field of view angle

  • Image rectangle aspect ratio

m4Mmxf

Ray Generation in 2D ​

UQDV06

6R7kqK

tMrXi9

then we just normalize r to get the ray

P(t)=e+td (d=r||r||)

3D Cases ​

1. 三维正交基与向量分解

  • 视图坐标系的基向量:
    • u:水平向右(Right),
    • v:垂直向上(Up),
    • w:视图方向(View Direction,指向图像平面的正前方)。
  • 向量 r 的构造: 从视点 e 到图像平面点 p(x,y) 的向量可分解为:r=x⋅u+(y⋅aspect)⋅v+D⋅w
    • x⋅u:水平方向的偏移(x∈[−1,1] 为归一化坐标),
    • y⋅aspect⋅v:垂直方向的偏移,通过纵横比缩放 y 以匹配水平方向的单位长度,
    • D⋅w:视点到图像平面的距离(通常设为 D=1 简化计算)。

2. 归一化方向向量 d

  • 目的:提取光线方向,忽略向量长度。
  • 公式:d=r∥r∥=x⋅u+y⋅aspect⋅v+D⋅wx2+(y⋅aspect)2+D2由于 u,v,w 是单位正交向量,模长计算简化为各分量平方和的平方根。

3. 光线方程

三维光线的参数方程与二维一致:

P(t)=e+t⋅d(t≥0)

其中 d 是归一化后的方向向量,t 为参数。

Orthographic Camera ​

8qrn1v

  • Origin=e+x⋅size⋅u+y⋅size⋅v
  • Direction is constant: w

Ray-Plane Intersection ​

Find intersection with the ray

  • Ray representation P(t)=origin+t∗direction2YKHMH

  • 3D Plane Representation (Infinite) plane defined by

    • P0=(x0,y0,Z0) point in the plane
    • n=(A,B,C) normal vector of the plane
  • Implicit plane equation

    • H(P)=Ax+By+Cz+D=0=n•P+D=0
    • D=−Ax0−By0−Cz0
  • Point-Plane distance

    • If n is normalized, distance to plane is H(P)
    • It is a signed distance!
  • Ray equation is explicit P(t)=R0+t⋅Rd

    • Parametric
    • Generates points
    • Hard to verify that a point is on the ray
  • Plane equation is implicit H(P)=n⋅P+D

    • Solution of an equation
    • Does not generate points
    • Verifies that a point is on the plane

Intersection means both are satisfied So, insert explicit equation of ray into implicit equation of plane & solve for t

P(t)=Ro+t⋅RdH(P)=n⋅P+D=0n⋅(Ro+t⋅Rd)+D=0t=−(D+n⋅Ro)n⋅Rd

if n·Rd=0 then ray is parallel to plane

Additional Bookkeeping

  • Verify that intersection is closer than previous
    • t<tcurrent
    • 确保交点为最近可见点
    • 正确处理遮挡关系,实现真实渲染
  • Verify that it is not out of range (behind eye)
    • t>tmin
    • 排除视点后方的无效交点
    • 避免渲染不可见区域,提升正确性

jLuoHx

(r0+t⋅d−a)⋅n=0t=(a−r0)⋅nd⋅n

Ray-triangle intersection ​

m23LqE 光线与三角形相交的计算是计算机图形学的核心问题之一(如光线追踪、碰撞检测),其核心在于判断光线是否与三角形所在平面相交,且交点是否位于三角形内部。

((P2−r0)×(P1−ro))⋅d>0((P3−r0)×(P2−ro))⋅d>0((P1−r0)×(P3−ro))⋅d>0
重心坐标(Barycentric Coordinates) ​

核心思想

  • 任意三角形内的点可表示为三个顶点的加权和,权重称为重心坐标(α,β,γ),满足 α+β+γ=1 且 α,β,γ≥0。
  • 图示中,点 pθ 可表示为:pθ=p1+αu+βv(其中 u=p0−p1, v=p2−p1)当 α>0,β>0,α+β≤1 时,点位于三角形内部。

作用

  • 判断光线与平面的交点是否在三角形内部:通过求解重心坐标的权重,验证其是否满足非负且和为1的条件。
克拉默法则(Cramer's Rule)** ​

数学原理

  • 用于求解线性方程组的变量,核心利用行列式比值计算唯一解。
  • 对于方程组 Ax=b,解为:xi=det(Ai)det(A),其中 Ai 是将 A 的第 i 列替换为 b 的矩阵。
在光线追踪中的应用 ​
  • 求解交点参数: 在光线与三角形相交的Möller-Trumbore算法中,通过构建线性方程组:{t⋅d=αu+βvα+β+γ=1利用克拉默法则计算重心坐标 β,γ 和参数 t,避免直接求解矩阵逆,提升效率。

光线与三角形相交的两步法 ​

步骤1:光线与平面相交 ​
  • 与三角形所在平面相交,求得参数 t 和交点 P(同光线-平面相交算法)。
步骤2:判断交点是否在三角形内部 ​

方法1:投影到2D平面判断

  • 核心思路: 将三维三角形顶点和交点投影到二维坐标系(如选择平面内的x-y平面),转化为二维点与三角形的包含性检测。
  • 投影方法: 选择平面法线方向作为投影轴(如法线的最大分量方向),忽略该轴坐标,保留二维坐标。
  • 2D检测: 使用重心坐标或叉乘法判断二维点是否在三角形内(如之前讨论的三边内侧条件)。

方法2:三维空间直接判断(避免投影)

  • 核心思路: 利用三角形边的正交法向量判断交点是否在边的内侧,无需显式投影。
  • 关键公式: 对边 e1=p2−p1,计算正交法向量:n=e1−e1⋅e2∥e2∥2e2若 (P−p1)⋅n≥0,则交点 P 在边 e1 的内侧(朝向对顶点 p3)。
  • 优势: 避免投影带来的精度损失,直接在三维空间中通过向量点积完成判断,与Möller-Trumbore算法逻辑一致。

1. 重心坐标与克拉默法则的结合

  • 通过克拉默法则求解重心坐标 β,γ,若满足 0≤β,γ≤1 且 β+γ≤1,则点在三角形内。

2. 算法优化逻辑

  • 投影法:直观但需处理坐标系转换,
  • 三维直接法:高效,适合硬件加速(如SIMD指令),是实时渲染(如游戏引擎)的首选。

3. 与先前内容的关联

  • 前序幻灯片中通过叉乘点积判断三边内侧,本质上是重心坐标条件的几何等效(α,β>0,α+β<1)。
概念作用算法关联
重心坐标用权重表示三角形内点,判断包含性交点内侧条件
克拉默法则高效求解线性方程组,得到交点参数Möller-Trumbore算法
投影到2D检测将三维问题降维,简化判断逻辑传统几何方法
三维直接法(叉乘/点积)避免投影,直接利用向量方向判断内侧现代光线追踪算法

Ray-Sphere Intersection ​

LDKPyL

  • Explicit Ray: P(t)=Ro+t⋅Rd

  • Implicit Sphere H(P)=P⋅P−r2=0

  • Insert explicit equation of ray into implicit equation of sphere & solve for t

    (Ro+tRd)⋅(Ro+tRd)−r2=0
Rd⋅Rd⏟at2+2Rd⋅Ro⏟bt+(Ro⋅Ro−r2)⏟c=0
  • Quadratic: at2+bt+c=0

    • a=1 (remember, ∥Rd∥=1)
    • b=2Rd⋅Ro
    • c=Ro⋅Ro−r2
  • With discriminant:

    Δ=b2−4ac
  • And solutions:

    t±=−b±d2a

QLBIsQ

  • Δ>0:光线与球体相交于两点,
    • t− 和 t+(假设 t−<t+),取最小正根作为有效交点(光线从外向内入射),
  • Δ=0:光线与球体相切,仅有一个交点(t=−b/2),
  • Δ<0:光线与球体无交点。

交点的有效性判断筛选有效参数 t

  • 若 t+ 和 t− 均为正:取较小的 t 值(最近交点),
  • 若 t− 为负、t+ 为正:取 t+(光线从外部入射,交点在球体表面),
  • 若两者均为负:光线起点在球体外部且远离球体,无有效交点。

QScoFC

tc=(c−r0)⋅db=∥rc−c∥w=a2−b2trh=tc−w

sdFevc

Intersection Point

P=A+tintD

Intersection Normal:

N=P−C|P−C|

For spheres centered at origin

BIUUUL

Sphere Normal Q||Q||

Q=P(t), intersection point