OgreMax中使用offsetmapping

第一个texture unit 里是凹凸贴图,第二个texutre unit里是漫反射贴图

vertex shader和pixex shader如下:

offsetmapping.program:

 

  1. // Bump map with Parallax offset vertex program, support for this is required 
  2. vertex_program my/OffsetMappingVP cg 
  3.    source OffsetMapping.cg 
  4.    default_params
  5.    {
  6.   param_named scale float 1.0
  7.             param_named_auto lightPosition light_position_object_space 0 
  8.             param_named_auto eyePosition camera_position_object_space 
  9.             param_named_auto worldViewProj worldviewproj_matrix 
  10.    }
  11.    entry_point main_vp 
  12.    profiles vs_1_1 arbvp1 
  13. // Bump map with parallax fragment program 
  14. fragment_program my/OffsetMappingFP cg
  15.    source OffsetMapping.cg
  16.    default_params
  17.    {
  18.             param_named_auto lightDiffuse light_diffuse_colour 0 
  19.             param_named_auto lightSpecular light_specular_colour 0 
  20.             // Parallax Height scale and bias 
  21.             param_named scaleBias float4 0.04 -0.02 1 0 
  22.    }
  23.    entry_point main_fp
  24.    profiles ps_2_0 arbfp1

OffsetMapping.cg:

  1. /* Bump mapping with Parallax offset vertex program 
  2.    In this program, we want to calculate the tangent space light end eye vectors 
  3.    which will get passed to the fragment program to produce the per-pixel bump map 
  4.    with parallax offset effect. 
  5. */
  6. struct outPixel
  7. {
  8.   float4 colour  : COLOR0;
  9. };
  10. // General functions
  11. // Expand a range-compressed vector
  12. half4 expand(half4 v)
  13. {
  14.     return v * 2 - 1;
  15. }
  16. half3 expand(half3 v)
  17. {
  18.     return v * 2 - 1;
  19. }
  20. half2 expand(half2 v)
  21. {
  22.     return v * 2 - 1;
  23. }
  24. half1 expand(half1 v)
  25. {
  26.     return v * 2 - 1;
  27. }
  28. // Returns light direction from light position and vertex position
  29. half3 getLightDirection(float4 lightPosition, float4 position)
  30. {
  31.     // calculate tangent space light vector
  32.     // Get object space light direction
  33.     // Non-normalised since we'll do that in the fragment program anyway
  34.     return lightPosition.xyz -  (position.xyz * lightPosition.w);
  35. }
  36. // Returns eye direction from eye position and vertex position
  37. half3 getEyeDirection(float3 eyePosition, float4 position)
  38. {
  39.     return eyePosition - position.xyz;
  40. }
  41. // Returns a Tangent Binormal Normal matrix
  42. half3x3 getTBNMatrix(float3 tangent, float3 normal)
  43. {
  44.     // Calculate the binormal (NB we assume both normal and tangent are
  45.     // already normalised)
  46.     // NB looks like nvidia cross params are BACKWARDS to what you'd expect
  47.     // this equates to NxT, not TxN
  48.     float3 binormal = cross(tangent, normal);
  49.     
  50.     // Form a rotation matrix out of the vectors
  51.     return half3x3(tangent, binormal, normal);
  52. }
  53. // Returns expanded normal vector from texture map
  54. half3 getNormalMapVector(sampler2D normalMap, float2 uv)
  55. {
  56.     // get bump map vector, again expand from range-compressed
  57.     return expand(tex2D(normalMap, uv).xyz);
  58. }
  59. // Returns displacement vector from normalmaps alpha channel
  60. half getDisplacement(sampler2D normalMap, float2 uv, half scale, half bias)
  61. {
  62.     // get the height using the tex coords
  63.     half height = tex2D(normalMap, uv).a;
  64.     // calculate displacement   
  65.     return (height * scale) + bias;
  66. }
  67. // Returns a specular component from normal vector, specular colour and specular power
  68. half3 getSpecularComponent(float3 normal, float3 halfAngle, float3 specularcolour, float specularPower)
  69. {
  70.     return pow(saturate(dot(normal, halfAngle)), specularPower) * specularcolour;
  71. }
  72. // Returns a per-pixel lighted component from normal vector, lightdir and colour
  73. half3 getLightingComponent(float3 normal, float3 lightDir, float3 colour)
  74. {
  75.     return saturate(dot(normal, lightDir)) * colour;
  76. }
  77. float4 lightPosition;
  78. float3 eyePosition;
  79. float4x4 worldViewProj;
  80. float3 lightDiffuse;
  81. float3 lightSpecular;
  82. float4 scaleBias;
  83. sampler2D normalHeightMap;
  84. sampler2D diffuseMap;
  85. struct app2vertOffsetMapping
  86. {
  87.     float4 position : POSITION;
  88.     float3 normal   : NORMAL;
  89.     float2 uv       : TEXCOORD0;
  90.     float3 tangent      : TANGENT0;
  91. };
  92. struct vert2fragOffsetMapping
  93. {
  94.     float4 position     : POSITION;
  95.     float2 uv       : TEXCOORD0;
  96.     float3 lightDir     : TEXCOORD1;
  97.     float3 eyeDir       : TEXCOORD2;
  98.     float3 halfAngle    : TEXCOORD3;
  99. };
  100. /* Vertex program that moves light and eye vectors into texture tangent space at vertex */ 
  101. vert2fragOffsetMapping main_vp(uniform float scale,app2vertOffsetMapping IN) 
  102. {
  103.     vert2fragOffsetMapping OUT;
  104.     // calculate output position 
  105.     OUT.position = mul(worldViewProj, IN.position); 
  106.     // pass the main uvs straight through unchanged 
  107.     OUT.uv = IN.uv*scale; 
  108.     half3 lightDir = getLightDirection(lightPosition, IN.position);
  109.     half3 eyeDir = getEyeDirection(eyePosition, IN.position);
  110.     
  111.     // Form a rotation matrix out of the vectors 
  112.     half3x3 TBN = getTBNMatrix(IN.tangent, IN.normal); 
  113.     
  114.     // Transform the light vector according to this matrix 
  115.     OUT.lightDir = normalize(mul(TBN, lightDir)); 
  116.     OUT.eyeDir = normalize(mul(TBN, eyeDir)); 
  117.     OUT.halfAngle = normalize(OUT.eyeDir + OUT.lightDir);
  118.     return OUT;
  119. }
  120. outPixel main_fp(vert2fragOffsetMapping IN)
  121. {
  122.     outPixel OUT;
  123.     half displacement = getDisplacement(normalHeightMap, IN.uv, scaleBias.x, scaleBias.y);
  124.     
  125.     float3 uv2 = float3(IN.uv, 1);
  126.     
  127.     // calculate the new tex coord to use for normal and diffuse
  128.     float2 newTexCoord = ((IN.eyeDir * displacement) + uv2).xy;
  129.     
  130.     // get the new normal and diffuse values
  131.     half3 normal = getNormalMapVector(normalHeightMap, newTexCoord);
  132.     half3 diffuse = tex2D(diffuseMap, newTexCoord).xyz;
  133.     
  134.     half3 specular = getSpecularComponent(normal, IN.halfAngle, lightSpecular, 32);
  135.     half3 col = diffuse * getLightingComponent(normal, IN.lightDir, lightDiffuse) + specular;
  136.         
  137.     OUT.colour = float4(col, 1);
  138.     return OUT;
  139. }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值