一、Refract & Reflect
Snell定律描述了光线从一个介质传播到另外一个介质时,入射角、折射角以及介质折射率的关系。通过snell定律,可以根据入射光的方向向量求取折射光的方向向量。
Fresnel定律完善了光的衍射理论,当光线到达材质交界面时,一部分光被反射,另外一部分发生折射,这个现象被称为Fresnel Effect。菲涅尔现象混合了反射与折射,使得物体更加真实,理论物理中的Fresnel公式很复杂,因而在实时计算机图形学中,只要在最终效果上满足人们的视觉感受就可以了。在Cook-Torrance光照模型中计算specular光的rough surface时用到了Fresnel系数。
F = f + (1-f) * (1 - V · H)FresnelPower
其中f入射角为0时的菲涅尔反射系数,V视角向量,H半向量;
color = factor * 反射光颜色 + (1 - factor) * 折射光颜色
Snell中的折射率本质上反映的是光在介质中传播速度以及折射方向;
Fresnel中折射系系数反映的是光在透明介质表面被折射的光通量的比率;
这些可以模拟水表面的折射与反射现象等等
Vertex shader:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | const float Eta = 0.6; const float FresnelPower = 5.0; const float F = ((1.0 - Eta)*(1.0 - Eta)) / ((1.0 + Eta)*(1.0 + Eta)); varying vec3 Reflect; varying vec3 Refract; varying float Ratio; void main(void) { vec4 ecposition = gl_ModelViewMatrix * gl_Vertex; vec3 ecposition3 = ecposition.xyz / ecposition.w; vec3 i = normalize(ecposition3); vec3 n = normalize(gl_NormalMatrix*gl_Normal); Ratio = F + (1.0 - F) * pow(1.0 - dot(i, n), FresnelPower); Refract = refract(i, n, Eta); Refract = vec3(gl_TextureMatrix[0] * vec4(Refract, 1.0)); Reflect = reflect(i, n); Reflect = vec3(gl_TextureMatrix[1] * vec4(Reflect, 1.0)); gl_Position = ftransform(); } |
Frag Shader:
1 2 3 4 5 6 7 8 9 10 11 12 | varying vec3 Reflect; varying vec3 Refract; varying float Ratio; uniform sampler2d tex; void main(void) { vec3 refractColor = vec3(texture2D(tex, Refract)); vec3 reflectColor = vec3(texture2D(tex, Reflect)); vec3 Color = mix(refractColor, reflectColor, Ratio); gl_FragColor = vec4(Color, 1.0); } |
如果要突出某种颜色RGB,产生色散,可以分离折射的rgb值,得到另外一种效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | const float EtaR = 0.3; const float EtaG = 0.67; const float EtaB = 0.9; varying vec3 RefractR; varying vec3 RefractG; varying vec3 RefractB; RefractR = refract(i, n, EtaR); RefractR = vec3(gl_TextureMatrix[0] * vec4(RefractR, 1.0)); RefractG = refract(i, n, EtaG); RefractG = vec3(gl_TextureMatrix[0] * vec4(RefractG, 1.0)); RefractB = refract(i, n, EtaB); RefractB = vec3(gl_TextureMatrix[0] * vec4(RefractB, 1.0)); |
1 2 3 4 | vec3 refractColor; refractColor.r = vec3(texture2D(tex, RefractR)); refractColor.g = vec3(texture2D(tex, RefractG)); refractColor.b = vec3(texture2D(tex, RefractB)); |
二、Diffraction
GPU Gem1里面有一章讲解
光的衍射:光在传播过程中,遇到透明或者不透明的障碍物,绕过障碍物产生偏离直线传播的现象称为光的衍射。
当表面的细节比光的波长小很多时,对于这些小尺寸的细节,例如CD,波效应就不能忽略,
shader一般步骤是:先把可见光的波长转换到0~1之间,计算半向量在切线的投影来计算u,
原理不是很懂
就这样吧。。
vertex shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | uniform vec3 eyeposition; uniform vec3 lightposition; vec4 HighlightColor = (1.0, 1.0, 1.0, 1.0); varying vec4 Color; attribute vec3 Tangent; vec3 lambda2rgb(float lambda) { const float ultraviolet = 400.0; const float infrared = 700.0; //转换可见光到0~1 float a = (lambda - ultraviolet) / (infrared - ultraviolet); const float c = 10.0; //图的宽度 vec3 b = vec3(a) - vec3(0.75, 0.5, 0.25); return max((1.0 - c * b*b), 0.0); } void main(void) { vec3 objPosition = vec3(gl_ModelViewMatrix * gl_Vertex); vec3 Normal = normalize(gl_NormalMatrix * gl_Normal); vec3 LightDir = normalize(lightposition - objPosition); vec3 EyeDir = normalize(eyeposition - objPosition); vec3 HalfVec = normalize(LightDir + EyeDir); vec3 T = normalize(gl_NormalMatrix * Tangent); float u = abs(dot(T, HalfVec)); vec3 diffColor = vec3(0.0); const int numspectralorders = 3; for (int m = 1; m <= numspectralorders; ++m) { float lambda = 660 * u / float(m); diffColor += lambda2rgb(lambda); float w = dot(Normal, HalfVec); float e = 2 * u / w; vec3 hilight = exp(-e * e) * HighlightColor.rgb; const float diffAtten = 0.8; Color = vec4(diffAtten * diffColor + hilight, 1.0); gl_Position = ftransform(); } |
Frag shader
1 2 3 4 5 | varying vec4 Color; void main(void) { gl_FragColor = Color; } |
后面我会把c++代码补上,建一个完整的工程,供大家下载
参考文档:http://www.cppblog.com/init/archive/2012/03/29/169406.html