Skip to content
All
10-CG-Rasterization
NOTE
Computer-Graphics
Rasterization
June 1, 2025
11-CG-Raycasting

11-CG-Ray casting

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+tdirection
    • 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=xu+(yaspect)v+Dw
    • xu:水平方向的偏移(x[1,1] 为归一化坐标),
    • yaspectv:垂直方向的偏移,通过纵横比缩放 y 以匹配水平方向的单位长度,
    • Dw:视点到图像平面的距离(通常设为 D=1 简化计算)。

2. 归一化方向向量 d

  • 目的:提取光线方向,忽略向量长度。
  • 公式d=rr=xu+yaspectv+Dwx2+(yaspect)2+D2由于 u,v,w 是单位正交向量,模长计算简化为各分量平方和的平方根。

3. 光线方程

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

P(t)=e+td(t0)

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

Orthographic Camera

8qrn1v

  • Origin=e+xsizeu+ysizev
  • Direction is constant: w

Ray-Plane Intersection

Find intersection with the ray

  • Ray representation P(t)=origin+tdirection 2YKHMH

  • 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=nP+D=0
    • D=Ax0By0Cz0
  • 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+tRd

    • Parametric
    • Generates points
    • Hard to verify that a point is on the ray
  • Plane equation is implicit H(P)=nP+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+tRdH(P)=nP+D=0n(Ro+tRd)+D=0t=(D+nRo)nRd

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+tda)n=0t=(ar0)ndn

Ray-triangle intersection

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

((P2r0)×(P1ro))d>0((P3r0)×(P2ro))d>0((P1r0)×(P3ro))d>0
重心坐标(Barycentric Coordinates)

核心思想

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

作用

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

数学原理

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

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

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

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

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

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

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

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

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

2. 算法优化逻辑

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

3. 与先前内容的关联

  • 前序幻灯片中通过叉乘点积判断三边内侧,本质上是重心坐标条件的几何等效(α,β>0,α+β<1)。

| 概念 | 作用 | 算法关联 | |

NOTE
Computer-Graphics
Raycasting
June 1, 2025
CG-Review List
Review
Computer-Graphics
May 31, 2025
06-CG-Texture Mapping

06-CG-Texture Mapping

Texture Mapping

  • Adds visual detail to scenes
  • Without raising geometric complexity
  • A shading trick makes a surface look textured
NOTE
Computer-Graphics
Texture
May 31, 2025
1
2
3
4
5
Next
avatar
AllenYGY
AllenYGY's daily study and life