原文 http://www.cnblogs.com/CGDeveloper/archive/2008/07/03/1234206.html

 

如果物体表面细节很多,我们可以不断的精细化物体的几何数据,但是这样会产生大量的Lighting & Transformation等计算,
为了实现丰富真实的物体表面,除了贴上一般纹理之外,往往还使用Bump mapping(凹凸纹理)技术。
Bump mapping并没有增加物体的几何复杂度,它只是在计算物体的光照效果时作了“弊”,不使用物体本身的法向量,而是
使用了经过处理的法向量。如果这样的法向量使用normal map,我们可以使用GLSL实现凹凸效果。
首先,因为没有改变对象的几何形状,所以bump mapping的实现是在FS之中进行的,因此光照计算就必须在FS之中进行。
由于normal map之中的法向量是在SURFACE_LOCAL COORDINATE SPACE,所以用于光照计算的光源的方向和视向都
必须变换到同一空间进行光照计算。也就是说,必须使用所谓TBN矩阵的逆矩阵对光源的方向和视向进行变换,转换之后的值
作为varying传入到FS之中。
对于三角形mesh而言,TBN的一种计算方法如下:

Bump mapping的GLSL实现 [转]void FindInvTBN(Vertor3f Vertices[3], Vector2f TexCoords[3], Vector3f & InvNormal,
Bump mapping的GLSL实现 [转]                  Vector3f & InvBinormal, Vector3f & InvTangent) 
c3c1_T * v2v1.x() + c2c1_T * v3v1.x()) * fScale1,                                     (-c3c1_T * v2v1.y() + c2c1_T * v3v1.y()) * fScale1,                                     (-c3c1_T * v2v1.z() + c2c1_T * v3v1.z()) * fScale1);                          N = T%B; //Cross product!  /*This is where programmers should break up the function to smooth the tangent, binormal and    normal values. */    //Look at “Derivation of the Tangent Space Matrix” for more information.                          float fScale2 = 1.0f / ((T.x() * B.y() * N.z() - T.z() * B.y() * N.x()) +                                                 (B.x() * N.y() * T.z() - B.z() * N.y() * T.x()) +                                                 (N.x() * T.y() * B.z() - N.z() * T.y() * B.x()));                        InvTangent.set((B%N).x() * fScale2,                                       ((-1.0f * N)%T).x() * fScale2,                                       (T%B).x() * fScale2);                        InvTangent.normalize();                          InvBinormal.set(((-1.0f *B)%N).y() * fScale2,                                        (N%T).y() * fScale2,                                        ((-1.0f * T)%B).y() * fScale2);                        InvBinormal.normalize();                          InvNormal.set((B%N).z() * fScale2,                                      ((-1.0f * N)%T).z() * fScale2,                                      (T%B).z() * fScale2);                        InvNormal.normalize();              }

上述计算中可以只计算T。
相应的VS如下:

Bump mapping的GLSL实现 [转]varying vec3 LightDir;
Bump mapping的GLSL实现 [转]varying vec3 EyeDir;
Bump mapping的GLSL实现 [转]
Bump mapping的GLSL实现 [转]attribute vec3 Tangent;
Bump mapping的GLSL实现 [转]
Bump mapping的GLSL实现 [转]void main()


相应的FS如下:

Bump mapping的GLSL实现 [转]uniform sampler2D BumpTex;
Bump mapping的GLSL实现 [转]uniform sampler2D DecalTex;
Bump mapping的GLSL实现 [转]
Bump mapping的GLSL实现 [转]varying vec3 LightDir;
Bump mapping的GLSL实现 [转]varying vec3 EyeDir;
Bump mapping的GLSL实现 [转]
Bump mapping的GLSL实现 [转]void main() 

相关文章: