GLSL.Parallax mapping

GLSL.Parallax mapping
The pseudo code to implement parallax mapping is the following :

float parallaxHeight = 0.035f
float height = tex2D ( HeightMap ).r;
float offset = parallaxHeight * ( 2.0f * height - 1.0f);
float2 parallaxTexCoord = texCoord + offset * viewVec.xy;

The height is a value between 0 and 1 but the offset is a value in the range [-parallaxHeight, +parallaxHeight]. The reason for this mapping is to distribute the distortion of the texture map equally in 2 directions.
 1 attribute vec3 rm_Binormal;
 2 attribute vec3 rm_Tangent;
 3
 4 varying vec3 vCameraDir;
 5    
 6 void  main(  void  )
 7 {
 8   gl_Position = ftransform();
 9   gl_TexCoord[0= gl_MultiTexCoord0;
10
11   vec4 view_position = gl_ModelViewMatrix * gl_Vertex;
12   vec3 camera_dir = normalize(view_position.xyz);
13   
14   mat3x3  TangentSpace;
15   TangentSpace[0= gl_NormalMatrix * rm_Tangent;
16   TangentSpace[1= gl_NormalMatrix * rm_Binormal;
17   TangentSpace[2= gl_NormalMatrix * gl_Normal;
18
19   vCameraDir = camera_dir * TangentSpace;
20   // equal to .
21   //vCameraDir.x = dot(camera_dir, TangentSpace[0]);
22   //vCameraDir.y = dot(camera_dir, TangentSpace[1]);
23   //vCameraDir.z = dot(camera_dir, TangentSpace[2]);
24}




 1 varying vec3 vCameraDir;
 2
 3 uniform sampler2D DiffuseMap;
 4 uniform sampler2D HeightMap;
 5
 6 uniform  float  fHeight;
 7
 8 void  main(  void  )
 9
10 {
11   vCameraDir = normalize(vCameraDir);
12   
13   vec3  texcoord = vec3(gl_TexCoord[0].xy, fHeight);
14   
15   float depth = (2.0*texture2D(HeightMap, texcoord).r-1.0*fHeight;
16   vec3  texcoord_corrected = texcoord + vCameraDir.xyz * depth;
17    
18   gl_FragColor = texture2D(DiffuseMap, texcoord_corrected.xy); 
19}




http://knol.google.com/k/shader-fx-parallax-mapping#
http://www.ownself.org/oswpblog/?p=59
http://blog.csdn.net/soilwork/article/details/1452437

你可能感兴趣的:(GLSL.Parallax mapping)