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
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.
光线投射:通过数学方法模拟光线传播,解决三维场景到二维图像的投影问题,核心是可见性计算。 着色:在可见性基础上,结合物理光照模型和材质属性,为每个像素赋予真实感颜色,核心是光照模拟。
光线投射是光线追踪的子集:前者是后者的“第一步”(确定可见性),后者通过扩展次级光线实现更复杂的物理模拟。
Ray Representation
- Origin - Point
- Direction - Vector
- normalized is better
- Parametric line
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)
Simplified Pinhole Camera
Eye-image pyramid (view frustum)
Note that the distance/size of image are arbitrary
Eye point
(center) Orthobasis
(horizontal, up, direction) Field of view angle
Image rectangle aspect ratio
Ray Generation in 2D
then we just normalize r to get the ray
3D Cases
1. 三维正交基与向量分解
- 视图坐标系的基向量:
:水平向右(Right), :垂直向上(Up), :视图方向(View Direction,指向图像平面的正前方)。
- 向量
的构造: 从视点 到图像平面点 的向量可分解为: :水平方向的偏移( 为归一化坐标), :垂直方向的偏移,通过纵横比缩放 以匹配水平方向的单位长度, :视点到图像平面的距离(通常设为 简化计算)。
2. 归一化方向向量
- 目的:提取光线方向,忽略向量长度。
- 公式:
由于 是单位正交向量,模长计算简化为各分量平方和的平方根。
3. 光线方程
三维光线的参数方程与二维一致:
其中
Orthographic Camera
- Direction is constant:
Ray-Plane Intersection
Find intersection with the ray
Ray representation
3D Plane Representation (Infinite) plane defined by
point in the plane normal vector of the plane
Implicit plane equation
Point-Plane distance
- If
is normalized, distance to plane is - It is a signed distance!
- If
Ray equation is explicit
- Parametric
- Generates points
- Hard to verify that a point is on the ray
Plane equation is implicit
- 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
if
then ray is parallel to plane
Additional Bookkeeping
- Verify that intersection is closer than previous
- 确保交点为最近可见点
- 正确处理遮挡关系,实现真实渲染
- Verify that it is not out of range (behind eye)
- 排除视点后方的无效交点
- 避免渲染不可见区域,提升正确性
Ray-triangle intersection
光线与三角形相交的计算是计算机图形学的核心问题之一(如光线追踪、碰撞检测),其核心在于判断光线是否与三角形所在平面相交,且交点是否位于三角形内部。
重心坐标(Barycentric Coordinates)
核心思想
- 任意三角形内的点可表示为三个顶点的加权和,权重称为重心坐标(
),满足 且 。 - 图示中,点
可表示为: 当 时,点位于三角形内部。
作用
- 判断光线与平面的交点是否在三角形内部:通过求解重心坐标的权重,验证其是否满足非负且和为1的条件。
克拉默法则(Cramer's Rule)**
数学原理
- 用于求解线性方程组的变量,核心利用行列式比值计算唯一解。
- 对于方程组
,解为:
在光线追踪中的应用
- 求解交点参数: 在光线与三角形相交的Möller-Trumbore算法中,通过构建线性方程组:
利用克拉默法则计算重心坐标 和参数 ,避免直接求解矩阵逆,提升效率。
光线与三角形相交的两步法
步骤1:光线与平面相交
- 与三角形所在平面相交,求得参数
和交点 (同光线-平面相交算法)。
步骤2:判断交点是否在三角形内部
方法1:投影到2D平面判断
- 核心思路: 将三维三角形顶点和交点投影到二维坐标系(如选择平面内的x-y平面),转化为二维点与三角形的包含性检测。
- 投影方法: 选择平面法线方向作为投影轴(如法线的最大分量方向),忽略该轴坐标,保留二维坐标。
- 2D检测: 使用重心坐标或叉乘法判断二维点是否在三角形内(如之前讨论的三边内侧条件)。
方法2:三维空间直接判断(避免投影)
- 核心思路: 利用三角形边的正交法向量判断交点是否在边的内侧,无需显式投影。
- 关键公式: 对边
,计算正交法向量: 若 ,则交点 在边 的内侧(朝向对顶点 )。 - 优势: 避免投影带来的精度损失,直接在三维空间中通过向量点积完成判断,与Möller-Trumbore算法逻辑一致。
1. 重心坐标与克拉默法则的结合
- 通过克拉默法则求解重心坐标
,若满足 且 ,则点在三角形内。
2. 算法优化逻辑
- 投影法:直观但需处理坐标系转换,
- 三维直接法:高效,适合硬件加速(如SIMD指令),是实时渲染(如游戏引擎)的首选。
3. 与先前内容的关联
- 前序幻灯片中通过叉乘点积判断三边内侧,本质上是重心坐标条件的几何等效(
)。
概念 | 作用 | 算法关联 |
---|---|---|
重心坐标 | 用权重表示三角形内点,判断包含性 | 交点内侧条件 |
克拉默法则 | 高效求解线性方程组,得到交点参数 | Möller-Trumbore算法 |
投影到2D检测 | 将三维问题降维,简化判断逻辑 | 传统几何方法 |
三维直接法(叉乘/点积) | 避免投影,直接利用向量方向判断内侧 | 现代光线追踪算法 |
Ray-Sphere Intersection
Explicit Ray:
Implicit Sphere
Insert explicit equation of ray into implicit equation of sphere & solve for
Quadratic:
(remember, )
With discriminant:
And solutions:
:光线与球体相交于两点, 和 (假设 ),取最小正根作为有效交点(光线从外向内入射),
:光线与球体相切,仅有一个交点( ), :光线与球体无交点。
交点的有效性判断筛选有效参数
- 若
和 均为正:取较小的 值(最近交点), - 若
为负、 为正:取 (光线从外部入射,交点在球体表面), - 若两者均为负:光线起点在球体外部且远离球体,无有效交点。
Intersection Point
Intersection Normal:
For spheres centered at origin
Sphere Normal