一、Refract & Reflect
Snell定律描述了光線從一個(gè)介質(zhì)傳播到另外一個(gè)介質(zhì)時(shí),入射角、折射角以及介質(zhì)折射率的關(guān)系。通過(guò)snell定律,可以根據(jù)入射光的方向向量求取折射光的方向向量。
Fresnel定律完善了光的衍射理論,當(dāng)光線到達(dá)材質(zhì)交界面時(shí),一部分光被反射,另外一部分發(fā)生折射,這個(gè)現(xiàn)象被稱為Fresnel Effect。菲涅爾現(xiàn)象混合了反射與折射,使得物體更加真實(shí),理論物理中的Fresnel公式很復(fù)雜,因而在實(shí)時(shí)計(jì)算機(jī)圖形學(xué)中,只要在最終效果上滿足人們的視覺(jué)感受就可以了。在Cook-Torrance光照模型中計(jì)算specular光的rough surface時(shí)用到了Fresnel系數(shù)。
F = f + (1-f) * (1 - V · H)FresnelPower
其中f入射角為0時(shí)的菲涅爾反射系數(shù),V視角向量,H半向量;
color = factor * 反射光顏色 + (1 - factor) * 折射光顏色
Snell中的折射率本質(zhì)上反映的是光在介質(zhì)中傳播速度以及折射方向;
Fresnel中折射系系數(shù)反映的是光在透明介質(zhì)表面被折射的光通量的比率;
這些可以模擬水表面的折射與反射現(xiàn)象等等
Vertex shader:
1
const float Eta=0.6;
2
const float FresnelPower=5.0;
3
const float F=((1.0-Eta)*(1.0-Eta))/((1.0+Eta)*(1.0+Eta));
4
varying vec3 Reflect;
5
varying vec3 Refract;
6
varying float Ratio;
7
void main(void)
{
8
9
vec4 ecposition = gl_ModelViewMatrix* gl_Vertex;
10
vec3 ecposition3 = ecposition.xyz/ecposition.w;
11
vec3 i = normalize(ecposition3);
12
vec3 n = normalize(gl_NormalMatrix*gl_Normal);
13
Ratio = F + (1.0-F) * pow(1.0 - dot(i, n),FresnelPower);
14
15
Refract = refract(i,n,Eta);
16
Refract = vec3(gl_TextureMatrix[0] * vec4(Refract,1.0));
17
18
Reflect = reflect(i,n);
19
Reflect = vec3(gl_TextureMatrix[1] * vec4(Reflect,1.0));
20
21
gl_Position = ftransform();
22
}
Frag Shader:
1
varying vec3 Reflect;
2
varying vec3 Refract;
3
varying float Ratio;
4
uniform sampler2d tex;
5
void main(void)
{
6
vec3 refractColor = vec3 (texture2D(tex,Refract));
7
vec3 reflectColor = vec3 (texture2D(tex,Reflect));
8
vec3 Color = mix(refractColor, reflectColor, Ratio);
9
gl_FragColor = vec4( Color,1.0);
10
}
11


如果要突出某種顏色RGB,產(chǎn)生色散,可以分離折射的rgb值,得到另外一種效果:
1
const float EtaR=0.3;
2
const float EtaG=0.67;
3
const float EtaB=0.9;
4
varying vec3 RefractR;
5
varying vec3 RefractG;
6
varying vec3 RefractB;
7
8
RefractR = refract(i,n,EtaR);
9
RefractR = vec3(gl_TextureMatrix[0] * vec4(RefractR,1.0));
10
11
RefractG = refract(i,n,EtaG);
12
RefractG = vec3(gl_TextureMatrix[0] * vec4(RefractG,1.0));
13
14
RefractB = refract(i,n,EtaB);
15
RefractB = vec3(gl_TextureMatrix[0] * vec4(RefractB,1.0));
1
vec3 refractColor ;
2
refractColor.r= vec3 (texture2D(tex,RefractR));
3
refractColor.g= vec3 (texture2D(tex,RefractG));
4
refractColor.b= vec3 (texture2D(tex,RefractB));

二、Diffraction
GPU Gem1里面有一章講解
光的衍射:光在傳播過(guò)程中,遇到透明或者不透明的障礙物,繞過(guò)障礙物產(chǎn)生偏離直線傳播的現(xiàn)象稱為光的衍射。
當(dāng)表面的細(xì)節(jié)比光的波長(zhǎng)小很多時(shí),對(duì)于這些小尺寸的細(xì)節(jié),例如CD,波效應(yīng)就不能忽略,
shader一般步驟是:先把可見(jiàn)光的波長(zhǎng)轉(zhuǎn)換到0~1之間,計(jì)算半向量在切線的投影來(lái)計(jì)算u,
原理不是很懂
就這樣吧。。
vertex shader
1
uniform vec3 eyeposition;
2
uniform vec3 lightposition;
3
vec4 HighlightColor=(1.0,1.0,1.0,1.0);
4
varying vec4 Color;
5
attribute vec3 Tangent;
6
vec3 lambda2rgb(float lambda)
{
7
const float ultraviolet =400.0;
8
const float infrared =700.0;
9
//轉(zhuǎn)換可見(jiàn)光到0~1
10
float a = (lambda - ultraviolet)/(infrared - ultraviolet);
11
const float c = 10.0; //圖的寬度
12
vec3 b = vec3(a) - vec3(0.75,0.5,0.25);
13
return max((1.0 - c*b*b), 0.0);
14
}
15
void main(void)
{
16
vec3 objPosition = vec3 (gl_ModelViewMatrix * gl_Vertex);
17
vec3 Normal = normalize (gl_NormalMatrix * gl_Normal);
18
vec3 LightDir = normalize (lightposition - objPosition);
19
vec3 EyeDir = normalize (eyeposition - objPosition);
20
vec3 HalfVec = normalize (LightDir + EyeDir);
21
22
vec3 T=normalize(gl_NormalMatrix * Tangent);
23
float u = abs(dot(T, HalfVec));
24
25
vec3 diffColor=vec3(0.0);
26
27
const int numspectralorders=3;
28
for(int m=1; m<=numspectralorders;++m)
{
29
float lambda =660 * u / float(m);
30
diffColor += lambda2rgb(lambda);
31
}
32
33
float w = dot(Normal , HalfVec);
34
float e = 2 * u / w;
35
vec3 hilight = exp(-e * e) * HighlightColor.rgb;
36
37
38
const float diffAtten = 0.8;
39
Color = vec4(diffAtten * diffColor + hilight, 1.0);
40
41
gl_Position = ftransform();
42
43
44
} Frag shader
1
varying vec4 Color;
2
void main(void)
3

{
4
gl_FragColor =Color ;
5
} 
