Ogre引擎渲染系列之Normal Specular Mapping

发表于2017-09-21
评论0 989浏览

比较流行的开发引擎无非就是Unity3D引擎和UE4虚幻4,但是作为开源的Ogre引擎还是被一部分玩家所喜欢,由于其开源的,而且是针对图形处理的。所以对于学习3D游戏开发者来说,学习Ogre的GPU渲染是一条捷径,各个引擎的渲染Shader编写其实是类似的,它们的区别是在细节方面,虚幻做的最好,其他就是Unity和Ogre了。下面把Ogre的处理方式给大家介绍一下:


高光,法线,环境映射对于Shader来说必须要掌握的,其中Ogre中的渲染需要几个文件一是cg文件还有material文件,先给读者展示cg文件,代码如下所示:copy

  1. struct VIn  
  2. {  
  3.     float4 p    : POSITION;  
  4.     float3 n    : NORMAL;  
  5.     float3 t    : TANGENT;  
  6.     float2 uv   : TEXCOORD0;  
  7. };  
  8.    
  9. struct VOut  
  10. {  
  11.     float4 p    : POSITION;  
  12.    
  13.     float2 uv   : TEXCOORD0;  
  14.     float4 wp   : TEXCOORD1;  
  15.     float3 n    : TEXCOORD2;  
  16.     float3 t    : TEXCOORD3;  
  17.     float3 b    : TEXCOORD4;  
  18.     float4 lp   : TEXCOORD5;  
  19.     float3 sdir : TEXCOORD6;  
  20. };  
  21.    
  22. struct PIn  
  23. {  
  24.     float2 uv   : TEXCOORD0;  
  25.     float4 wp   : TEXCOORD1;  
  26.     float3 n    : TEXCOORD2;  
  27.     float3 t    : TEXCOORD3;  
  28.     float3 b    : TEXCOORD4;  
  29.     float4 lp   : TEXCOORD5;  
  30.     float3 sdir : TEXCOORD6;  
  31. };  
  32.    
  33. void ambient_vs(VIn IN,  
  34.     uniform float4x4 wvpMat,  
  35.     out float4 oPos : POSITION,  
  36.     out float2 oUV : TEXCOORD0)  
  37. {  
  38.     oPos = mul(wvpMat, IN.p);  
  39.     oUV = IN.uv;  
  40. }  
  41.    
  42. float4 ambient_ps(in float2 uv : TEXCOORD0,  
  43.     uniform float3 ambient,  
  44.     uniform float4 matDif,  
  45.     uniform sampler2D dMap,  
  46.     uniform sampler2D aoMap): COLOR0  
  47. {  
  48.     return tex2D(dMap, uv) * tex2D(aoMap, uv) *  
  49.         float4(ambient, 1) * float4(matDif.rgb, 1);  
  50. }  
  51.    
  52. VOut diffuse_vs(VIn IN,  
  53.     uniform float4x4 wMat,  
  54.     uniform float4x4 wvpMat,  
  55.     uniform float4x4 tvpMat,  
  56.     uniform float4 spotlightDir)  
  57. {  
  58.     VOut OUT;  
  59.     OUT.wp = mul(wMat, IN.p);  
  60.     OUT.p = mul(wvpMat, IN.p);  
  61.    
  62.     OUT.uv = IN.uv;  
  63.    
  64.     OUT.n = IN.n;  
  65.     OUT.t = IN.t;  
  66.     OUT.b = cross(IN.t, IN.n);  
  67.     OUT.sdir = mul(wMat, spotlightDir).xyz; // spotlight dir in world space  
  68.    
  69.     OUT.lp = mul(tvpMat, OUT.wp);  
  70.    
  71.     return OUT;  
  72. }  
  73.    
  74. float4 diffuse_ps(  
  75.     PIn IN,  
  76.     uniform float3 lightDif0,  
  77.     uniform float4 lightPos0,  
  78.     uniform float4 lightAtt0,  
  79.     uniform float3 lightSpec0,  
  80.     uniform float4 matDif,  
  81.     uniform float4 matSpec,  
  82.     uniform float matShininess,  
  83.     uniform float3 camPos,  
  84.     uniform float4 invSMSize,  
  85.     uniform float4 spotlightParams,  
  86.     uniform float4x4 iTWMat,  
  87.     uniform sampler2D diffuseMap : TEXUNIT0,  
  88.     uniform sampler2D specMap : TEXUNIT1,  
  89.     uniform sampler2D normalMap : TEXUNIT2): COLOR0  
  90. {  
  91.     // direction  
  92.     float3 ld0 = normalize(lightPos0.xyz - (lightPos0.w * IN.wp.xyz));  
  93.    
  94.     half lightDist = length(lightPos0.xyz - IN.wp.xyz) / lightAtt0.r;  
  95.     // attenuation  
  96.     half ila = lightDist * lightDist; // quadratic falloff  
  97.     half la = 1.0 - ila;  
  98.    
  99.     float4 normalTex = tex2D(normalMap, IN.uv);  
  100.    
  101.     float3x3 tbn = float3x3(IN.t, IN.b, IN.n);  
  102.     float3 normal = mul(transpose(tbn), normalTex.xyz * 2 - 1); // to object space  
  103.     normal = normalize(mul((float3x3)iTWMat, normal));  
  104.    
  105.     float3 diffuse = max(dot(ld0, normal), 0);  
  106.    
  107.     // calculate the spotlight effect  
  108.     float spot = (spotlightParams.x == 1 &&  
  109.         spotlightParams.y == 0 &&  
  110.         spotlightParams.z == 0 &&  
  111.         spotlightParams.w == 1 ? 1 : // if so, then it's not a spot light  
  112.         saturate(  
  113.             (dot(ld0, normalize(-IN.sdir)) - spotlightParams.y) /  
  114.             (spotlightParams.x - spotlightParams.y)));  
  115.    
  116.     float3 camDir = normalize(camPos - IN.wp.xyz);  
  117.     float3 halfVec = normalize(ld0   camDir);  
  118.     float3 specular = pow(max(dot(normal, halfVec), 0), matShininess);  
  119.    
  120.     float4 diffuseTex = tex2D(diffuseMap, IN.uv);  
  121.     float4 specTex = tex2D(specMap, IN.uv);  
  122.    
  123.     float3 diffuseContrib = (diffuse * lightDif0 * diffuseTex.rgb * matDif.rgb);  
  124.     float3 specularContrib = (specular * lightSpec0 * specTex.rgb * matSpec.rgb);  
  125.     float3 light0C = (diffuseContrib   specularContrib) * la * spot;  
  126.    
  127.     return float4(light0C, diffuseTex.a);  
  128. }  

其中VIn表示的是顶点输入的结构体,VOut表示的是顶点输出的结构体,PIn表示的是片段着色器的输入结构体,在下面的函数实现中首先实现的是ambient occlusion mapping也包括顶点和片段处理。最后是diffuse的顶点和片段处理也就是高光specluar和法线normal的处理,这样整个Shader就完成了。
下面实现的是material的编写,代码如下所示: copy
  1. material base_material  
  2. {  
  3.     set $diffuseCol "1 1 1 1"  
  4.     set $specularCol "1 1 1"  
  5.     set $shininess "32"  
  6.    
  7.     technique  
  8.     {  
  9.         pass  
  10.         {  
  11.             illumination_stage ambient  
  12.    
  13.             ambient 1 1 1 1  
  14.             diffuse $diffuseCol  
  15.             specular 0 0 0 0  
  16.             emissive 0 0 0 0  
  17.    
  18.             vertex_program_ref ambient_vs  
  19.             {  
  20.             }  
  21.    
  22.             fragment_program_ref ambient_ps  
  23.             {  
  24.             }  
  25.    
  26.             texture_unit diffuseMap  
  27.             {  
  28.                 texture white.png  
  29.             }  
  30.    
  31.             texture_unit aoMap  
  32.             {  
  33.                 texture white.png  
  34.             }  
  35.         }  
  36.    
  37.         pass  
  38.         {  
  39.             illumination_stage per_light  
  40.    
  41.             scene_blend add  
  42. //            iteration once_per_light   not needed while   illumination_stage per_light   is used  
  43.    
  44.             vertex_program_ref diffuse_vs  
  45.             {  
  46.             }  
  47.    
  48.             fragment_program_ref diffuse_ps  
  49.             {  
  50.             }  
  51.    
  52.             diffuse $diffuseCol  
  53.             specular $specularCol $shininess  
  54.             ambient 0 0 0 0  
  55.    
  56.             texture_unit diffuseMap  
  57.             {  
  58.                 texture white.png  
  59.             }  
  60.    
  61.             texture_unit specMap  
  62.             {  
  63.                 texture white.png  
  64.             }  
  65.    
  66.             texture_unit normalMap  
  67.             {  
  68.                 texture flat_n.png  
  69.             }  
  70.         }  
  71.     }  
  72. }  
  73.    
  74. // examples (require the appropriate [[textures]], all found in the Ogre samples)  
  75.    
  76. material rockwall : base_material  
  77. {  
  78.     set_texture_alias diffuseMap rockwall.tga  
  79.     set_texture_alias specMap rockwall.tga  
  80.     set_texture_alias normalMap rockwall_NH.tga  
  81. }  
  82.    
  83. material metal : base_material  
  84. {  
  85.     set_texture_alias diffuseMap RustedMetal.jpg  
  86.     set_texture_alias specMap RustedMetal.jpg  
  87. }  
  88.    
  89. material ogre : base_material  
  90. {  
  91.     set_texture_alias diffuseMap GreenSkin.jpg  
  92.     set_texture_alias specMap GreenSkin.jpg  
  93.     set_texture_alias normalMap NMHollyBumps.png  
  94. }  

该文件实现了一个基材质 base_material其它材质可以继承,这个对于开发者来说非常灵活。另外还需要一个program程序文件用于shader和图片的配置,代码如下所示:
  1. vertex_program diffuse_vs cg  
  2. {  
  3.     source general.cg  
  4.     profiles vs_1_1 arbvp1  
  5.     entry_point diffuse_vs  
  6.    
  7.     default_params  
  8.     {  
  9.         param_named_auto wMat world_matrix  
  10.         param_named_auto wvpMat worldviewproj_matrix  
  11.         param_named_auto tvpMat texture_viewproj_matrix 0  
  12.         param_named_auto spotlightDir light_direction_object_space 0  
  13.     }  
  14. }  
  15.    
  16. vertex_program ambient_vs cg  
  17. {  
  18.     source general.cg  
  19.     profiles vs_1_1 arbvp1  
  20.     entry_point ambient_vs  
  21.    
  22.     default_params  
  23.     {  
  24.         param_named_auto wvpMat worldviewproj_matrix  
  25.     }  
  26. }  
  27.    
  28. fragment_program ambient_ps cg  
  29. {  
  30.     source general.cg  
  31.     profiles ps_2_0 arbfp1  
  32.     entry_point ambient_ps  
  33.    
  34.     default_params  
  35.     {  
  36.         param_named_auto ambient ambient_light_colour  
  37.         param_named_auto matDif surface_diffuse_colour  
  38.     }  
  39. }  
  40.    
  41. fragment_program diffuse_ps cg  
  42. {  
  43.     source general.cg  
  44.     profiles ps_2_x arbfp1  
  45.     entry_point diffuse_ps  
  46.    
  47.     default_params  
  48.     {  
  49.         param_named_auto lightDif0 light_diffuse_colour 0  
  50.         param_named_auto lightSpec0 light_specular_colour 0  
  51.         param_named_auto camPos camera_position  
  52.         param_named_auto matShininess surface_shininess  
  53.         param_named_auto matDif surface_diffuse_colour  
  54.         param_named_auto matSpec surface_specular_colour  
  55.         param_named_auto lightPos0 light_position 0  
  56.         param_named_auto lightAtt0 light_attenuation 0  
  57.         param_named_auto iTWMat inverse_transpose_world_matrix  
  58.         param_named_auto spotlightParams spotlight_params 0  
  59.     }  
  60. }  

以上就完成了高光,法线以及ambient Shader的实现,当然学习Shader编程不能仅限于功能实现,还需要举一反三,比如我们上面封装的base_material。还是可以继续去扩展的,代码如下所示: copy
  1.  material some_material : base_material  
  2. {  
  3.     // any of these maps can be left out if you don't have one  
  4.     set_texture_alias diffuseMap some_dif.png  
  5.     set_texture_alias specMap some_spec.png  
  6.     set_texture_alias normalMap some_norm.png  
  7.     set_texture_alias aoMap some_ao.png  
  8.    
  9.     // diffuse colour multiplier (for example, green-ish)  
  10.     set $diffuseCol "0.1 1 0.1"  
  11.     // specular colour multiplier (for example, red-ish)  
  12.     set $specularCol "1 0.1 0.1"  
  13.     // specular power (shininess) (the higher, the sharper the highlights)  
  14.     set $shininess "128"  
  15.    
  16.     // once again, you can leave any of these configurables out if you don't need them  
  17. }  

我们还可以在此Shader的基础上实现UV镜像处理,修改代码如下所示:

一、首先将VIn输入结构体修改如下所示:

  1. struct VIn  
  2. {  
  3.     float4 p    : POSITION;  
  4.     float3 n    : NORMAL;  
  5.     float4 t    : TANGENT; // <- this was changed  
  6.     float2 uv   : TEXCOORD0;  
  7. };  


二、修改Vout输出结构体代码如下:

  1. struct VOut  
  2. {  
  3.     float4 p    : POSITION;  
  4.    
  5.     float2 uv   : TEXCOORD0;  
  6.     float4 wp   : TEXCOORD1;  
  7.     float3 n    : TEXCOORD2;  
  8.     float4 t    : TEXCOORD3; //<- this was changed  
  9.     float3 b    : TEXCOORD4;  
  10.     float4 lp   : TEXCOORD5;  
  11.     float3 sdir : TEXCOORD6;  
  12. };  

三、修改diffuse_vs函数代码修改如下所示:

  1. VOut diffuse_vs(VIn IN,  
  2.     uniform float4x4 wMat,  
  3.     uniform float4x4 wvpMat,  
  4.     uniform float4x4 tvpMat,  
  5.     uniform float4 spotlightDir)  
  6. {  
  7.     VOut OUT;  
  8.     OUT.wp = mul(wMat, IN.p);  
  9.     OUT.p = mul(wvpMat, IN.p);  
  10.    
  11.     OUT.uv = IN.uv;  
  12.    
  13.     OUT.n = IN.n;  
  14.     OUT.t = IN.t;  
  15.     OUT.b = cross(IN.t.xyz, IN.n) * IN.t.w; //<-this was changed  
  16.     OUT.sdir = mul(wMat, spotlightDir).xyz; // spotlight dir in world space  
  17.    
  18.     OUT.lp = mul(tvpMat, OUT.wp);  
  19.    
  20.     return OUT;  
  21. }  
关于变动的代码都加了注释,对比源代码既可以看明白,到此结束。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论