about 图形学的裁剪算法,发生在投影之后,投影已经确定好了二维的坐标,clipping 的工作就是把在视野范围外的部分去除。到这一步时,我们把得到的 clip coordinate (四维齐次坐标)转换为 Normalized devcie coordinate (NDC, 三维坐标)

属于图形学系列文章

Culling

定义: discarding invisible polygons

Back-face culling

Clipping

定义:Remove parts of primitives (e.g., lines and polygons) that are not visible

在这个过程中,可能还会 generate new primitives

OpenGL performs clipping in the clip space (hence the name) against the view frustum (a cube)

Line Clipping in 2D 直线剪裁

定义:使用一个框定范围的窗口裁剪线段

Cohen-Sutherland Clipping

把窗口分为9个区域,每个区域由不同的代码表示:

$$ b_1 = y > y_{max} ? 1 : 0 \\ b_2 = y < y_{min} ? 1 : 0 \\ b_3 = x > x_{max} ? 1 : 0 \\ b_4 = x < x_{min} ? 1 : 0 $$

找到线段两端点的代码分别为(C1, C2),算法的流程为:

  1. C1 = C2 = 0 -> Accept the line
  2. C1 = 0 or C2 = 0 -> 连接 zero bits 与 nonzero bits 和窗口的交点
  3. C1 & C2 != 0 -> Both endpoints outside the same edge of the window, cull completely
  4. C1 & C2 = 0 -> Both endpoints outside, but different edges -> Find the first intersection and find its binary code -> Apply recursively on this culled line

Polygon Clipping 多边形裁剪

Sutherland-Hodgeman Algorithm

Repeated clip a polygon using a clipping line

To clip a polygon against a line, one can examine its vertices one by one:

  • Test first vertex, output if inside else skip
  • Then loop through the list, testing transitions
    • In-to-out: Output intersection
    • In-to-in: Output vertex
    • Out-to-in: Output intersection and vertex
    • Out-to-out: Output nothing

例子(按照left bottom right top 的顺序)来自 YouTube

扩展到三维