home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / EffectEdit / Hemisphere.fx < prev    next >
Encoding:
Text File  |  2002-11-15  |  3.4 KB  |  128 lines

  1. //
  2. // Hemisphere Lighting Model
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Note: This effect file works with EffectEdit.
  6. //
  7.  
  8.  
  9. string XFile = "SkullOcc.x";                // model
  10. int    BCLR  = 0xff202080;                  // background
  11.  
  12. // light directions (view space)
  13. float3 DirFromLight < string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
  14.  
  15. // direction of light from sky (view space)
  16. float3 DirFromSky < string UIDirectional = "Direction from Sky"; > = { 0.0f, -1.0f, 0.0f };            
  17.  
  18. // light intensity
  19. float4 I_a = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
  20. float4 I_b = { 0.1f, 0.0f, 0.0f, 1.0f };    // ground
  21. float4 I_c = { 0.9f, 0.9f, 1.0f, 1.0f };    // sky
  22. float4 I_d = { 1.0f, 0.9f, 0.8f, 1.0f };    // diffuse
  23. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  24.  
  25. // material reflectivity
  26. float4 k_a = { 0.8f, 0.8f, 0.8f, 1.0f };    // ambient
  27. float4 k_d = { 0.4f, 0.4f, 0.4f, 1.0f };    // diffuse
  28. float4 k_s = { 0.1f, 0.1f, 0.1f, 1.0f };    // specular
  29. int    n   = 32;                            // power
  30.  
  31.  
  32. // transformations
  33. float4x3 WorldView  : WORLDVIEW;
  34. float4x4 Projection : PROJECTION;
  35.  
  36. struct VS_OUTPUT
  37. {
  38.     float4 Pos  : POSITION;
  39.     float4 Diff : COLOR0;
  40.     float4 Spec : COLOR1;
  41. };
  42.  
  43. VS_OUTPUT VS(
  44.     float3 Pos  : POSITION, 
  45.     float3 Norm : NORMAL, 
  46.     float  Occ  : TEXCOORD0,
  47.     uniform bool bHemi, 
  48.     uniform bool bDiff,
  49.     uniform bool bSpec)
  50. {
  51.     VS_OUTPUT Out = (VS_OUTPUT)0;
  52.  
  53.     float3 L = -DirFromLight;                               // diffuse direction
  54.     float3 Y = -DirFromSky;                                 // hemisphere up axis
  55.     float3 P = mul(float4(Pos, 1), (float4x3)WorldView);    // position (view space)
  56.     float3 N = normalize(mul(Norm, (float3x3)WorldView));   // normal (view space)
  57.     float3 R = normalize(2 * dot(N, L) * N - L);            // reflection vector (view space)
  58.     float3 V = -normalize(P);                               // view direction (view space)
  59.  
  60.     float4 Amb  = k_a * I_a;
  61.     float4 Hemi = k_a * lerp(I_b, I_c, (dot(N, Y) + 1) / 2) * (1 - Occ);
  62.     float  temp = 1 - max(0, dot(N, L));
  63.     float4 Diff = k_d * I_d * (1 - temp * temp);
  64.     float4 Spec = k_s * I_s * pow(max(0, dot(R, V)), n/4);
  65.     float4 Zero = 0;
  66.  
  67.     Out.Pos  = mul(float4(P, 1), Projection);               // position (projected)
  68.     Out.Diff = (bDiff ? Diff : 0)
  69.              + (bHemi ? Hemi : Amb);                        // diffuse + ambient/hemisphere
  70.     Out.Spec = (bSpec ? Spec : 0);                          // specular
  71.  
  72.     return Out;
  73. }
  74.  
  75.  
  76. technique THemisphere
  77. {
  78.     pass P0
  79.     {
  80.         VertexShader = compile vs_1_1 VS(true, false, false);
  81.     }
  82. }
  83.  
  84. technique THemisphereDiffuse
  85. {
  86.     pass P0
  87.     {
  88.         VertexShader = compile vs_1_1 VS(true, true, false);
  89.     }
  90. }
  91.  
  92. technique THemisphereDiffuseSpecular
  93. {
  94.     pass P0
  95.     {
  96.         VertexShader = compile vs_1_1 VS(true, true, true);
  97.  
  98.         SpecularEnable = TRUE;
  99.     }
  100. }
  101.  
  102. technique TAmbient
  103. {
  104.     pass P0
  105.     {
  106.         VertexShader = compile vs_1_1 VS(false, false, false);
  107.     }
  108. }
  109.  
  110. technique TAmbientDiffuse
  111. {
  112.     pass P0
  113.     {
  114.         VertexShader = compile vs_1_1 VS(false, true, false);
  115.     }
  116. }
  117.  
  118. technique TAmbientDiffuseSpecular
  119. {
  120.     pass P0
  121.     {
  122.         VertexShader = compile vs_1_1 VS(false, true, true);
  123.  
  124.         SpecularEnable = TRUE;
  125.     }
  126. }
  127.  
  128.