I left out a step that happens after projection and before division by W.
Clipping. Say we have 2 points: 0, 4, negative 6 after perspective
transformation that turns into 0, 4, 5, 6 and 0, 6, negative 4, which turns
into this value. They form a line segment. These two points in this line
segment are shown on this zoomed in part of our frustum. The second point is
outside of the frustum. We want to have all coordinates inside our final view
volume in NDC space so that we can render them. Clipping is done for line
segments and triangle edges that poke out through the frustum. An edge can be
clipped by any numbers of faces of the frustum. What happens here is that all
the coordinates between the two points get linearly interpolated. For our
example the point on the frustum faces halfway between our two points. The
interpolated point is then 0, 5, 3.85. We then divide this point by W as usual
to get a point in normalized device coordinates. Here's a rough overview of how
clipping can work. You start with a triangle that's poking out through the
frustum. Say the left face of the frustum first cuts the triangle. This creates
two triangles. Later, the top face of the frustum chops off the other part of
the triangle. This create three triangles total that will then be rasterized
separately by the GPU. You as a user don't really have to know or care much
about this clipping process. It happens automatically. It's worth knowing about
mostly if you need to do similar testing operations on the CPU side. In
computer graphics we often make a big deal about how we store 3D points in
vectors as homogeneous coordinates, with the fourth element W. In reality, for
almost every operation, the W value is either 0, meaning a vector, or 1,
meaning a point. It's only after projection and during clipping that the W
value is anything but 1. Once clipping is done and we're using normalized
device coordinates, we're done with homogeneous coordinates. However, these
homogeneous coordinates are important in that they're what the vertex shader
produces. When the coordinate is transformed by the projection matrix, but
before the division is performed, the coordinates are called clip coordinates.
The vertex shader can produce other intermediate results, such as computing
location once the model and view matrices are applied. It is required that the
vertex shader produce a position on the screen for the vertex. This position is
a homogeneous coordinate. The rasterizer then takes this position and performs
clipping.
このwで割る前の工程を言い忘れていました
クリッピングです 投影変換した後
(0,4,-6)が(0,4,5,6)になり
(0,6,-4)が(0,6,2.6,4)になった2つの点があります
この2点が線分を作ります 線分の2つの点は
すい台の一部を拡大させます
この2つ目の点はすい台の外側にあります
最終的には正規化デバイス座標のビューボリュームに
すべての座標を入れるとレンダリングできます
クリッピングは線分と三角形の頂点で行われ
すい台を突き抜けます
頂点はすい台の面のどの値でもクリッピングされます
2点の間にすべての座標が直線的に挿入されます
すい台上の点はこの2点のちょうど真ん中にあります
挿入された点は(0,5,3.8,5)です 次にこの点を
正規化デバイス座標に入れるためにwで割ります
これはクリッピングの概要を簡単に表したものです
まずすい台から突き出た三角形を描きます
すい台の左の面が三角形を切り取ります
2つの三角形ができ
すい台の上面が別の部分を切り取ります
こうして計3つの三角形が
GPUによってラスタ化され生成されます
このプロセスは自動的に行われるので
知らなくても構いません
CPU側で同じような演算を行うことが必要かどうかを
知っておく方がもっと重要です
CGでは同次座標のベクトル上に
4つ目の要素wと3Dの点を
どう保存するかが大事です
ほとんどの演算においてwの値は
ベクトルを表すゼロか点を表す1です
wの値が1以外のどの数でもいいのは
投影後とクリッピングの時だけです
クリッピング終了後は正規化デバイス座標を使います
これまでは同次座標で行いましたが
この同次座標は頂点シェーダが
生成する物において重要です
座標が割り算を実行する前の
投影変換行列が実行される座標を
クリップ座標と言います
頂点シェーダはモデルビュー行列が使われると
演算記憶装置のように中間結果を生成できます
頂点シェーダは頂点の位置を同次座標上に
生成することを求められます
次にラスタライザがこの位置を取り込み
クリッピングを実行します
我在投影後跟除以 W 之間跳過了一步「裁切」
假設有兩個點,一個是 (0, 4, -6)
透視座標變換後變成 (0, 4, 5, 6)
然後 (0, 6, -4) 變成這個值
它們連成一個線段
這兩點跟線段顯示在這個視錐放大圖上
第二個點在視錐外
我們希望所有座標都落在視野容體跟 NDC 的範圍內,這樣才能繪製
裁切作用在超出視錐的線段或是三角形的邊
一個邊可能被多個視錐的面裁切
發生的事情是所有座標值被線性內插
在這個例子視錐面上的位置在兩點正中間
內插結果點是 (0, 5, 3.8, 5)
然後照常除以 W 以取得 Normalized Device Coordinates 中的點
接下來是裁切的概觀
有個三角形超出視錐
假設視錐左面先裁切三角形,這樣產生兩個新三角形
接著視錐頂面裁切掉三角形另一部分
總共製造出三個三角形,之後會各自被 GPU 柵格化
你做為使用者不太需要關心裁切流程,都是自動幫你完成
如果你想在 CPU 上做類似的測試的話可以熟悉一下
在電腦圖學我們會強調我們將 3D 點跟向量存成齊次座標
加上第四個元素 W
實際上絕大部分的運算中 W 是 0,即向量,或是 1,即點
只有在投影後裁切中 W 才會出現 1 以外的值
當裁切結束,進入 Normalized Device Coordinates 我們就不需要齊次座標了
但 Vertex Shader 必須產出齊次座標
在座標被投影矩陣變換但還沒有除 W 之前,這個座標叫裁切座標(Clip Coordinate)
Vertex Shader 能產生其他中間值,像是施加了模型視角矩陣的位置
Vertex Shader 必須要產生螢幕上的位置
這位置是齊次座標
然後柵格單元拿這個座標進行裁切