To shade a diffuse surface, we need the cosine of the angle between the
direction to the light and the surface's normal. We can perform a vector
operation called the dot product to directly compute this cosine. First, you
must normalize the surface normal and the vector to the light. Normalizing means
rescaling a vector so that it has a length of 1. Normalized vectors are the norm
in reflection models. As we'll see, if a dot product of two normalized vectors
gives a value between negative 1 and 1, which will prove useful in computing the
effect of lighting. For example, say, I have the vector 3, negative 4, 0. To
normalize it, I find the length of the vector. This is simply the Pythagorean
theorem. Take each component of the vector and square it. 3 squared is 9,
negative 4 squared is 16, and 0 squared is 0. Take the square root of 25 and you
get the length of the vector, 5. It's lucky that turned out so easily. By
dividing the vector by its length, you get to normalize vector. So, 3, negative
4, 0 normalized is 0.6, negative 0.8, 0. Looking at this vector, it goes the
same direction, but only travels one unit. Note that normalizing a vector that's
already normalized leaves it unchanged. Try normalizing this vector again and
you'll find the length is 1. Dividing the vector by a length of 1, of course,
does nothing. It's very handy to normalize vectors in this way. Usually, we
store the surface normals as normalized vectors. We'll see exactly why in the
next lesson. For now, a quick quiz.
拡散反射で陰影をつけるために必要なのは
ライトの方向と面法線の角度の余弦です
この余弦はドット積という方法を用いて計算します
最初にすることは
面法線とライトのベクトルの正規化です
正規化とはベクトルの数値を1に変えることです
これは反射モデルの基準になります
正規化した2つのベクトルの値が-1から1の間になれば
ライトの効果の計算が簡単になります
例えばここに(3,-4,0)のベクトルがあるとします
最初にピタゴラスの定理でベクトルの長さを求めます
3つの値をそれぞれ2乗していきましょう
3の2乗で9、-4の2乗で16、0の2乗で0です
平方根を計算すると長さは5となります
実に分かりやすい解が出ました
ベクトルの値を長さで割ると正規化できます
(3,-4,0)を正規化すると
(0.6,-0.8,0)となります
元のベクトルと方向は同じで長さは1面分です
一度正規化してしまえばもう十分です
再度このベクトルを正規化しても長さは1です
1で割っても値は変わりません
正規化はとても便利な方法です
面法線は正規化された値で保存されます
理由は次に説明しますが その前に簡単な小テストです
要替漫射表面上色,我們需要往光的方向跟表面法向量夾角的餘弦
我們可以直接用內積(Dot Product)取得餘弦的值
我們必須先正規化法向量跟往光的向量
正規化(Normalize)即使向量長度為 1
反射模型裡都是用正規化向量
兩個正規化向量的內積值域在 -1 ~ 1
在計算光照效果上非常有用
假設我有 (3, -4, 0) 向量
要正規化我需要其長度
這是簡單的畢氏定理(Pythagorean Theorem)
先取各元素的平方
3 平方是 9、-4 平方是 16、0 平方是 0
25 開根號你就會得到向量長度 5
根號好開還蠻幸運的
向量除其長度,即其正規化向量
(3, -4, 0) 正規化成 (0.6, -0.8, 0)
這個向量方向相同,但是只移動 1 單位
正規化已經正規化過的向量不會造成改變
嘗試正規化這個向量,其長度為 1
向量除 1 沒有效果
正規化向量非常方便
通常我們將表面法向量儲存成正規化向量
下一堂課會解釋原因,但先來個小考