Reading

Computer Graphics


Fanyi Pu · Gemini 4 Carbon · Gemini 4 Neon · Gemini 4 Hydrogen · GPT-6 Astra · Claude Opus 5.5

· Updated

15362

Introduction & The Graphics Pipeline

Components Of Graphics:

  1. Rasterization: 如何把三角形转换成屏幕上的 pixels?
  2. Mesh Editing: 如何用数学和数据结构表示一个 3D 物体?
  3. Path Tracing: 如何模拟真实世界中的光线传播?
  4. Animation: 如何让 3D 场景随时间变化?

The Graphics Pipeline:

From vertices to a framebufferOne triangle passes through the pipeline. In 3D geometry, the input assembler builds the triangle from vertices 0, 1 and 2; the vertex shader moves it; optional tessellation subdivides it; an optional geometry shader emits a second triangle whose vertex 3 lies outside the screen; clipping, perspective division and viewport mapping trim the geometry to the viewport. In screen space, rasterization marks the pixels whose centers lie inside the clipped triangles, the fragment shader computes their colors from interpolated attributes, and blending combines those colors with pixels already in the framebuffer, producing the final 2D image. Tessellation combines programmable and fixed-function stages.3D geometryvertices and triangles · object → world → camera → clip spaceScreen spacefragments → pixelsInput assembler012Vertex shader012Tessellation012Geometry shader0123Clip & viewportscreen-space triangles: (x, y) + depthRasterizationFragment shaderBlendingFramebufferFixed functionProgrammableOptional
One triangle from vertex buffer to framebuffer. Clip & viewport = clipping, perspective divide to NDC, and viewport mapping. Tessellation mixes programmable and fixed stages; depth and stencil tests are omitted. Adapted from the Vulkan Tutorial.

从 3D 数据到 2D 数据,使得计算更容易。

Shaders are any string of code run on the GPU

Coordinate Spaces & Transformations

Homogeneous Coordinates: 从二维视角来看,对于 p′=(x,y),如果有 p=(a,b,c)(c≠0)使得 (ac,bc)=(x,y),则称 p 是 p′ 的 homogeneous coordinates。

这样子的话,比如说 translation 就变成了 shear。

01 / Normalize
One point, many representatives
A line through the origin meets the plane c = 1P = (2, 2, 2) and Q = (1, 1, 1) lie on the same line through the origin O. The line crosses the plane c = 1 at Q, so dividing by the last coordinate maps both representatives to the two-dimensional point (1, 1). Parts of the axes and line below the plane are dashed. Only the positive half of the line is shown.abcc = 1QPO

(2,2,2)∼(1,1,1)↦(1,1).

02 / Transform
Translation as a shear
Shear a by c; translate x by oneIn the b = 0 cross-section, the shear maps (a, c) to (a + c, c). At c = 1 the point Q moves by one to Q′, while at c = 2 the point P moves by two to P′. The triangles OQQ′ and OPP′ are similar, so dividing by c gives the same two-dimensional translation x to x + 1. On the c = 0 axis the shear moves nothing: a direction vector is not translated.c = 1acc = 0: unchangedQQ′+11PP′+22OBeforeAfter

△OQQ′∼△OPP′,Δa/c=1.

(10tx01ty001)(abc)=(a+txcb+tycc)⟼(x+txy+ty).
Left: every point on a line through O except O itself represents the same 2D point; dividing by c ≠ 0 picks the one on c = 1. Right: the b = 0 slice under translation (1, 0). The shift grows with c, so it is exactly one unit after division, and the c = 0 axis, where directions live, does not move.

通常情况下,我们用 c=1 表示点,而 c=0 代表一个方向,我们认为它是 vector。

Scene Graph: 每个部件只保存相对父节点的变换,沿树组合就得到 world coordinates;移动身体会带动整个人,移动大腿会连带小腿。

A scene graph and its articulated block figureThe body is the parent of the head, both arms and both upper legs. Each upper leg is the parent of its lower leg. Matching colors highlight the body, left upper leg and left lower leg in the tree and the block figure, where each draws its local x and y axes at its joint; the world frame sits beside the figure. Highlighted arrows map local coordinates to the parent: A2 from left lower leg to left upper leg, A1 from left upper leg to body, and A0 from body to world. Moving a parent carries its descendants and their frames.BodyLeftarmRightarmHeadRightupper legLeftupper legRightlower legLeftlower legWorldRight lower legRight lower legRight lower legRight upper legLeft lower legRight armLeft lower legRight upper legRight upper legLeft lower legRight armRight armLeft upper legLeft upper legLeft upper legBodyBodyBodyHeadHeadHeadLeft armLeft armLeft armWorldLocal → parentSame hierarchy, assembledA2A1A0
Local coordinates → world coordinates. Each colored axis pair is a part's local frame; Ai maps it into its parent's frame.

plocal 是一个点在左小腿 local space 中的 homogeneous coordinates,pworld 是同一个点在整个场景的 world space 中的 homogeneous coordinates。两者满足

pworld=A0A1A2plocal,plocal=(A0A1A2)−1pworld.

因此递归时只需传下 Wchild=WparentAchild;inverse transform 存在时,world→local 为 A2−1A1−1A0−1。

Instancing: 不同的地方 point to 一个物体。这样子 Scene Graph 就可以变成 DAG 了。

Instancing turns a scene tree into a DAGOn the left, World points directly to Tree, to Dandelion with transform A1, and to Hill with transform A0. Hill also points to the same Dandelion node with transform A2, giving Dandelion two parents and turning the tree into a directed acyclic graph. Dandelion owns one shared Stem and Head subgraph. On the right, the single Dandelion prototype is instantiated twice in world space: upright on flat ground via A1, and smaller and tilted on the slope via A0 A2.Shared geometry (stored once)WorldTreeHillDandelionStemHeadWorldScene DAG (in-degree 2 at Dandelion)Same DAG, two world instancesA1A0A2A1A0A2
Two paths in the scene graph (A1 from World and A0A2 through Hill) reference the same Dandelion subgraph, turning the tree into a DAG. Each reference applies its own transform, while editing the shared geometry updates every instance.

Quaternions: H:=span(1,i,j,k),三个正交的虚部,所以 i2=j2=k2=ijk=−1。Quaternion 能代表三维的旋转,但因为三维旋转不满足交换律,所以 quaternion 的乘法也不满足交换律。

01 / Cyclic 90° turns
Left-multiplying by i, j, k
i, j, k as right-hand 90-degree rotations in orthogonal planesThree orthogonal imaginary axes i, j, and k form a right-handed frame on the unit sphere. Left-multiplying by i rotates the jk-plane by +90 degrees, mapping j to k and then k to -j (so i squared equals -1). Similarly, left-multiplying by j maps k to i, and left-multiplying by k maps i to j.−jij = kik = −jjk = iki = jijk

ij=k,jk=i,ki=j⟹i(jk)=i2=−1.

02 / Order matters
Non-commutative 3D rotations
Applying 90-degree rotations about j and i in opposite orders gives different posesAn asymmetric 3D block starts with its blue nose along +i, ochre wing along +j, and sage fin along +k. Top row: rotating +90 degrees about j then +90 degrees about i leaves the nose along +j, wing along +k, and fin along +i. Bottom row: rotating +90 degrees about i then +90 degrees about j leaves the nose along -k, wing along +i, and fin along -j. The two final poses differ because ij = k while ji = -k.Row 1: +90° about j, then +90° about i+90° (j)+90° (i)StartStep 1Result AA ≠ BRow 2: +90° about i, then +90° about j+90° (i)+90° (j)StartStep 1Result B+i nose+j wing+k fin

qiqj=1+i+j+k2≠1+i+j−k2=qjqi,qu=1+u2.

Left: i,j,k form a right-handed orthonormal imaginary basis; left-multiplying by an imaginary unit rotates the perpendicular plane by +90∘ (for example j→i⋅k→i⋅−j, so i2=j2=k2=ijk=−1), while reversing the order flips the sign (ji=−k). Right: unit quaternions encode a 3D rotation of angle θ about axis u via q=cos⁡θ2+usin⁡θ2 (so i,j,k themselves act as 180∘ turns, and qu=1+u2 gives +90∘). Composing qi and qj in opposite orders lands in different 3D orientations because ij=k≠−k=ji.

对于一个三维坐标中的点 (x,y,z),我们可以用 quaternion 0+xi+yj+zk 来表示。也就是说对于三维点 p,我们有 p∈Im(H)。

现在的目标是对于 p=xi+yj+zk,我们希望其绕 unit vector u=xui+yuj+zuk(‖u‖=1)旋转 θ。

我们可以构造

q=cos⁡θ2+sin⁡θ2u,‖q‖2=1

那么旋转后的结果就是 qpq−1。

这里的数学证明可以问 GPT 就不赘述了。从感性上来讲,用 θ2 是为了 q 和 q−1 每次转一半,保持实部永远为 0。

Perspective Projection & Rasterization

基础模型是 The Pinhole Camera。

View frustum 规定了 the region the camera can see。是一个锥形区域。

Pinhole camera and perspective view frustum in right-handed camera spaceRight-handed coordinate axes at the pinhole origin (0, 0, 0) with +x down-left, +y up, and +z left behind the pinhole so the camera looks down -z. Four corner rays connect the sensor plane on the +z side through the pinhole to the view frustum on the -z side, clipped between the near plane -z_near and far plane -z_far. A ray from scene point P passes through (0, 0, 0) to the inverted point P prime on the sensor, with a perpendicular drop from P to the optical axis showing planar depth.yz−zxSensorPinhole (0, 0, 0)PP′−znear−zfar
Pinhole camera and view frustum in right-handed camera space (looking along −z): a ray from P passes through the pinhole (0,0,0) onto the inverted sensor at P′, with planar depth measured along −z. Corner rays from the sensor bound the visible frustum between −znear and −zfar.

我们会将 view frustum 转化为 Normalized Device Coordinates (NDC):

从视锥截面到 NDCn = 1 · f = 10 · near 半宽 = 0.5
原空间:横轴 x,纵轴深度 d横线代表深度切片。A、B、C 位于同一视线上;变换后横坐标相同,深度仍然不同。ABC相机空间 · x / dnear: d = 1 → far: d = 10变换结果:横轴归一化 x,纵轴归一化深度横线代表深度切片。A、B、C 位于同一视线上;变换后横坐标相同,深度仍然不同。ABC变换结果 · x / 深度near: −1 → far: +1
左图为相机空间截面,右图展示透视映射结果;横线为等距深度切片(d = 1…10),A、B、C 位于同一条视线上。变换后直线仍为直线,远处切片更加密集;中间过渡仅用于示意。

具体的线性变换可以看 这里,不赘述。

值得注意的是,假设原坐标是 (xe,ye,ze),经过 projection matrix 与 perspective division 转成 NDC 后是 (xn,yn,zn),depth 满足:

zn=f+nf−n+2fn(f−n)ze

其中 n=znear,f=zfar 分别为 near plane 和 far plane 到相机的距离,它们在 camera space 中的 z 坐标为 −n 和 −f。

考虑到

|dzndze|=2fn(f−n)ze2=2(1n−1f)ze2

当 f≫n 时,1n−1f≈1n,斜率近似为 2nze2。因此在 n 较小时,一旦物体远离相机(|ze| 较大、靠近 far plane −f),zn 随 depth 的变化就会非常平缓,使得两个深度不同的物体在 quantization 前 zn 的差值就已经很小。

因为 depth buffer 只能以有限精度存储 zn,在远处这段几乎水平的区间里,两个距离相机略有不同的表面(比如一张纸放在桌子上)经过 quantization 和 rasterization interpolation 舍入后,很容易落到同一个深度值甚至发生大小反转。于是 pixel 前后关系随视角微动或跨 pixel 随机翻转,画面上就会出现交错闪烁的撕裂条纹,即 z-fighting。

因此,在保证不裁掉前景的前提下尽量增大 near plane 距离 n 能有效改善远处的 depth precision;此外,现代 graphics pipeline 也常在 [0,1] depth 范围下采用 floating-point depth buffer 配合 reversed-Z(将 near plane 映射到 1、far plane 映射到 0),利用浮点数在 0 附近更高的指数密度来抵消 1ze 带来的远端压缩。

较窄深度范围当近平面与远平面距离较近时,z_n 在 [-1, 1] 内随 z_e 变化较为平缓均匀。较窄深度范围narrowzezn1−1−f−n
较宽深度范围当远平面与近平面比值过大时,曲线在靠近 -f 处几乎平贴 z_n = 1,不同深度映射后几乎相等,造成 z-fighting。较宽深度范围widezezn1−1−f−n深度精度不足(z-fighting)
透视除法使 zn 按 1 / ze 非线性变化。右图中 [−f, −n] 跨度更大(f / n 更大),大部分深度值挤在近平面 −n 附近,远端曲线几乎贴平 zn = 1,从而引发 z-fighting。

得到 NDC 之后,我们就可以把所有东西投影到二维。接下来考虑怎么在二维上把图画出来,rasterization 是做这个过程,把三角形转化为 pixels。

对于直线,我们可以使用 Bresenham's line algorithm。大致思想就是假设斜率 yr−ylxr−xl∈[0,1],如果不在范围可以用翻转啥的分类讨论一下。然后 x 从左往右扫,对于每个 xi+12 算出对应的

yi+12=⌊yr−ylxr−xl(xi+12−xl)+yl⌋+12

其中 +12 是为了算中心;实际实现时,Bresenham 通过递推维护增量 error term,把每步的浮点乘除化为纯整数加减。

Bresenham 直线光栅化(0.5, 0.5) → (10.5, 4.5) · 斜率 m = 0.4
全局像素网格:沿列中心从左向右逐列扫描直线11 乘 6 像素网格,理想直线从 (0.5, 0.5) 连到 (10.5, 4.5),斜率为 0.4;绿色方格为已选出的像素,蓝色虚线框标出右侧放大的 2 乘 2 局部窗口。全局扫描理想直线选中像素02468100246xyx = 7.5
局部放大:在 x = 7.5 处比较上下两个候选像素放大相邻两列、上下两行的 2 乘 2 像素窗口。水平红色虚线为上下两候选像素的整数分界线,理想直线高度越过该分界时取整上移一行,否则保持同行。局部放大 · x = 7.5y = 3.3432y6.5x = 7.5✓ 中心 3.5分界 y = 3中心 2.5
采样 x = 7.5高度 y = 3.33.3 ≥ 3 → 上移选 (7, 3)
左图沿列中心 xi+12 逐列扫描;右图放大当前 2 × 2 窗口,只需判断交点高度 y 是否越过上下两候选像素的整数分界线 yi−1+1 即可决定是否上移一行。点击左图任意列或拖动滑块可逐步查看。

对于三角形,我们先考虑一个点是否在三角形内。这个用三个 cross product 很容易判断。假设三角形 △ABC,三个点 A,B,C 逆时针转。对于有向边 AB→,我们定义 edge function

EAB(x,y)=(xB−xA)(y−yA)−(yB−yA)(x−xA)

那么 P=(x,y) 要在三角形里,需要

EAB≥0,EBC≥0,ECA≥0.

但判断一个点显然是非常费力的。对于一个区块 [xl,xr]×[yl,yr],我们可以将其分成 early-in,early-out 和 else 三种。我们发现 E 是线性函数,那么对于任意一个区块,很容易 O(1) 判别这三者。

这样我们就可以做分治了。

三角形区块粗筛:early-in / early-out / else16 × 16 像素 · 4 × 4 区块
16 乘 16 像素网格上逆时针三角形 ABC 的 4 乘 4 区块覆盖分类(early-in、else 与 early-out)16 乘 16 像素网格被划分为 16 个 4 乘 4 像素区块,按与逆时针三角形 ABC 的位置关系分别着色为 early-in(绿色)、else(红色)与 early-out(空白)。4 × 4 区块粗筛early-inelseearly-outABC00448812121616xy
局部放大:区块 (2, 3) 的采样包围盒与 else 判定放大单个 4 乘 4 像素区块,显示四角测试采样框 [x_l, x_r] 乘 [y_l, y_r] 与 16 个像素中心的覆盖状态。区块 (2, 3) · else采样框 [xₗ, xᵣ]×[yₗ, yᵣ]命中像素1213141516y89101112x
区块 (2, 3) · else(8 / 16 命中)采样框 [8.5, 11.5] × [12.5, 15.5] · 仅 1 / 4 角在内,需细分
将 16 × 16 像素划分为 4 × 4 区块后,线性边函数 EAB,EBC,ECA 在采样框 [xl,xr]×[yl,yr] 上的极值必在四角取到:三边最小值均 ≥0 时为 early-in(整块命中);某边最大值 <0 时为 early-out(整块剔除);其余为 else,需继续细分。点击左图任意区块可切换查看。

现在我们回到 pixel 和很多个三角形。首先我们需要知道怎么才算三角形覆盖一个 pixel。我们看的是三角形是否覆盖了这个 pixel 最中间的点,也就是 (x+12,y+12)。

不过对于每个 pixel,一个简单的想法是,假设一个红色三角形覆盖了某个 pixel 的 10%,这个区块就应该是 10% 红色。

但直接按覆盖面积比例混色在一般 3D rendering 中并不成立:当多个三角形在同一个 pixel 内交叠并存在前后 occlusion 时,要在 pixel 内部做精确的 geometric clipping 来计算各自可见的覆盖面积,计算开销极高且不适合 GPU 并行;而若沿用“逐三角形渲染 + 每 pixel 单一 depth”的 pipeline,则无法表达 sub-pixel 级的 occlusion 关系(例如仅占 10% 的前景更新 depth 会误剔除其余 90% 的远景,不更新又会被远景覆盖)。

因此,graphics pipeline 改用离散 subsamples 同时记录 sub-pixel 级的 coverage 与 depth,最后再 resolve(averaging 或 filtering)成最终 pixel 颜色:

  • SSAA (Supersampling Anti-Aliasing):在每个 pixel 内放置 k 个 subsamples(如 2×2),每个点独立执行 coverage test、depth test 和 shading,等价于高分辨率渲染后再 downsampling。效果最完整,但 shading 与显存开销都放大 k 倍。
  • MSAA (Multisampling Anti-Aliasing):同样对 k 个 subsamples 分别做 coverage test 和 depth test,但对同一个三角形只在 pixel center(或 centroid)执行一次 shading,并将结果复制到所有通过测试的 subsamples。这样能以接近单 pixel 的 shading 成本消除几何边缘 aliasing(texture 与 shader 内部的高频变化仍需 mipmap 等手段配合)。
4× SSAA(超采样)三角形覆盖 2×2 子采样网格中的 3 个点。SSAA 对每个被覆盖点分别着色(c1、c2、c3),再将 4 个子样本平均得到像素颜色。4× SSAA(超采样)bgc₁c₂c₃子样本缓冲(4 槽)bgc₁c₂c₃resolve 平均(bg + c₁ + c₂ + c₃) / 4Coverage & Depth Test:4 次 / 像素Shading:3 次(每个覆盖点独立计算)
4× MSAA(多重采样)三角形覆盖 2×2 子采样网格中的 3 个点。MSAA 仅在像素中心计算一次着色值 c,写入 3 个通过覆盖测试的子样本,再平均得到像素颜色。4× MSAA(多重采样)bgcccshade子样本缓冲(4 槽)bgcccresolve 平均(bg + 3c) / 4Coverage & Depth Test:4 次 / 像素Shading:1 次(像素中心着色,复制到覆盖点)
4× SSAA 与 4× MSAA 对比(3/4 子采样点被覆盖):两者都以子采样点粒度执行覆盖测试与深度测试,区别在于 SSAA 对每个被覆盖子点分别着色,而 MSAA 每个三角形仅在像素中心着色一次并广播到通过测试的子点。

Transparency & Texturing

Barycentric Coordinates: (ϕi,ϕj,ϕk) represent weighted average, ϕi+ϕj+ϕk=1.

这个过程可以可视化在三角形中:

ϕi(x)=area(x,xj,xk)area(xi,xj,xk)=di(x)hi
面积比:对边子三角形占比
面积比视角:对边子三角形占比内部点 x 与三个顶点 x_i、x_j、x_k 相连,将三角形划分为三个子三角形。顶点 x_i 对应的权重 phi_i = 0.35 等于其对边子三角形 (x, x_j, x_k) 与总三角形的面积比;同理 phi_j = 0.20,phi_k = 0.45,三者之和为 1。ϕi=0.35ϕj=0.20ϕk=0.45xixjxkx

ϕi=area(x,xj,xk)/area(xi,xj,xk)=0.35

ϕi+ϕj+ϕk=0.35+0.20+0.45=1

高度比:同底化为垂距比
高度比视角:同底化为垂距比高亮共享底边 x_j x_k 以及子三角形 (x, x_j, x_k)。从顶点 x_i 到底边的高为 h_i,从点 x 到底边的垂距为 d_i(x),过 x 平行于底边的虚线上所有点具有相同的 phi_i = d_i(x) / h_i。hidi(x)ϕi 等值线xixjxkx

共享底边 xjxk,面积比约去底边长 =di(x)/hi

边上 ϕi=0,顶点 xi 处 ϕi=1,沿高线性变化

Barycentric Coordinates 的两种等价几何解释(以 (ϕi,ϕj,ϕk)=(0.35,0.20,0.45) 为例)。左图:内部点 x 将三角形分成三个子三角形,每个顶点的权重等于其对边子三角形的面积占比;右图:子三角形 (x,xj,xk) 与总三角形共享底边 xjxk,因此面积比直接化简为垂距比 di(x)/hi,且平行于底边的直线构成 ϕi 的线性等值线。

由于 barycentric coordinates 有一些优雅的性质,我们用它来表示 pixel,在计算的时候,考虑我们之前 rasterization 已经算过 edge function EAB(x,y),我们只需要除以总面积 Etotal 就可以得到该 pixel 的 barycentric coordinate。

现在我们考虑一个三角形投影之后的结果。假设三个顶点的 texture 是 a(v1),a(v2),a(v3)。

Flat Shading: 随便取一个点作为整个三角形的 texture

a(ϕx,ϕy)=a(v1)

Smooth Shading: 做简单的线性插值,开销比较小但是没有立体感

a(ϕx,ϕy)=a(v1)⋅ϕx+a(v2)⋅ϕy+a(v3)⋅(1−ϕx−ϕy)

Perspective Correct Shading: 算出真实的 z,然后根据这个 z 做插值:

1z(ϕx,ϕy)=1z1⋅ϕx+1z2⋅ϕy+1z3⋅(1−ϕx−ϕy),a(ϕx,ϕy)=z(ϕx,ϕy)[a(v1)z1⋅ϕx+a(v2)z2⋅ϕy+a(v3)z3⋅(1−ϕx−ϕy)]=a(v1)z1⋅ϕx+a(v2)z2⋅ϕy+a(v3)z3⋅(1−ϕx−ϕy)1z1⋅ϕx+1z2⋅ϕy+1z3⋅(1−ϕx−ϕy).

为什么可以这样算?在 2D 屏幕上,只要一个量是屏幕坐标 (xs,ys) 的线性函数,就可以直接用屏幕空间的 barycentric coordinates 精确插值。由于 3D 三角形落在某个平面 Ax+By+Cz=D 上,两侧同除以 z 并代入 pinhole camera 投影关系 xs=xz, ys=yz,可得

Axs+Bys+C=Dz⟹1z=ADxs+BDys+CD,

因此 1z 是 (xs,ys) 的线性函数。同理,在 3D 三角形上呈线性的属性 a=αx+βy+γz+δ 除以 z 后变为 az=αxs+βys+γ+δ⋅1z,同样是 (xs,ys) 的线性函数。于是我们可以直接在屏幕空间分别线性插值 1z 与 az,最后乘回 z(ϕx,ϕy)(即两者相除)即可还原真实的 a(ϕx,ϕy)。

同一倾斜纹理平面的三种插值渲染对比倾斜角 62° · 深度比 z_far / z_near ≈ 3.04× · 2 个三角形
Flat Shading
Flat Shading 渲染结果Flat Shading 渲染的倾斜棋盘平面:两个三角形各自只采样单个顶点颜色,整块纹理退化为两块纯色三角
a(ϕx,ϕy)=a(v1)

每个三角形仅采样单个顶点,纹理细节完全丢失。

Smooth Shading
Smooth Shading 渲染结果Smooth Shading 渲染的倾斜棋盘平面:在屏幕空间直接线性插值 UV,棋盘格与圆环在两个三角形的共享对角线处发生明显折弯畸变
a=∑ia(vi)ϕi

在 2D 屏幕空间线性插值 UV,沿三角对角线出现明显折弯。

Perspective Correct
Perspective Correct 渲染结果Perspective Correct Shading 渲染的倾斜棋盘平面:先插值 1/z 与 a/z 再乘回真实深度 z,网格直线与圆环跨对角线连续且符合近大远小透视规律
a=z∑ia(vi)ziϕi

插值 1/z 与 a/z 后再乘回真实深度,直线与透视比例严格保持。

同一块由两个三角形拼成的棋盘纹理平面(带中心圆环)在三种插值模型下的直接渲染对比:Flat Shading 退化为两块纯色三角;Smooth Shading 因忽略深度变化,棋盘直线与圆环沿共享对角线严重折弯;Perspective Correct Shading 正确还原近大远小的透视关系。拖动倾斜角到 0°(正对相机、各顶点等深)时,后两者完全一致。

A single pixel of a texture is a texel.

为了将二维 texture 贴到三维 surface 上,美术或建模工具会把 surface unwrap 到二维平面,为网格上的每个顶点分配对应的 (u,v) 坐标,建立 3D 表面到 2D texture 的映射。这一过程称为 UV mapping。

3D Surface(网格表面)
3D Surface 上的 UV 坐标分布三维环面由 20x10 个网格单元剖分为 400 个三角形。颜色由 (u, v) 坐标双线性混合得到,并叠加柔和的漫反射光照以呈现立体形态;虚线标出展开时沿 u=0 与 v=0 切开的接缝,正面右上方高亮一个样本三角形 v1、v2、v3。v1v2v3
网格表面按各点对应的 (u,v) 着色;外圈与内圈三角形在 3D 空间尺寸不同,但共享同一套 UV 参数化。
2D Texture Space(UV 平面)
2D Texture Space 中的展开网格三维环面沿 u=0 与 v=0 接缝切开并展开到二维单位正方形 [0, 1] x [0, 1]。横轴 u 从 0 到 1,纵轴 v 从 0 到 1,网格上的颜色与左侧三维表面一一对应,样本三角形 v1、v2、v3 对应其各自的二维顶点坐标。uv011v1v2v3
网格沿两条接缝展开为 [0,1]2 上的二维平面;每个顶点都记录着唯一的纹理坐标 (u,v)。
高亮三角形顶点映射:v1↦(0.25,0.20),v2↦(0.30,0.20),v3↦(0.30,0.30)接缝 u=0/1接缝 v=0/1
UV mapping 示例:左图为三维环面网格,右图为沿两条虚线接缝(u=0 与 v=0)展开到二维纹理空间 [0,1]2 后的平面网格。相同颜色表示同一组 (u,v) 坐标;每个三维顶点(如高亮三角形的 v1,v2,v3)都关联到纹理平面上的对应位置,渲染时再借助重心坐标插值求得三角形内部各像素的纹理坐标。

Cite this post

@misc{pu2026cscsrevisitcomputergraphics,
  author = {Pu, Fanyi and {Gemini 4 Carbon} and {Gemini 4 Neon} and {Gemini 4 Hydrogen} and {GPT-6 Astra} and {Claude Opus 5.5}},
  title  = {Computer Graphics},
  year   = {2026},
  month  = {11},
  url    = {https://pufanyi.com/blog/cs/cs-revisit/computer-graphics}
}