home *** CD-ROM | disk | FTP | other *** search
/ DarkBasic Professional / DarkBasicPro.iso / data1.cab / Lang_Files_(English) / Help / examples / basic3d / fx / simple / Simple.fx < prev    next >
Encoding:
Text File  |  2004-09-22  |  4.1 KB  |  163 lines

  1. //
  2. // Simple
  3. //
  4.  
  5. string XFile = "sphere.x";   // model
  6. int    BCLR = 0xff202080;   // background
  7.  
  8. // light direction (view space)
  9. float3 lightDir <  string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
  10.  
  11. // light intensity
  12. float4 I_a = { 0.1f, 0.1f, 0.1f, 1.0f };    // ambient
  13. float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  14. float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  15.  
  16. // material reflectivity
  17. float4 k_a : MATERIALAMBIENT = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
  18. float4 k_d : MATERIALDIFFUSE = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  19. float4 k_s : MATERIALSPECULAR= { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  20. int    n   : MATERIALPOWER = 32;                            // power
  21.  
  22. // texture
  23. texture Tex0 < string name = "base.tga"; >;
  24.  
  25. // transformations
  26. float4x4 World      : WORLD;
  27. float4x4 View       : VIEW;
  28. float4x4 Projection : PROJECTION;
  29.  
  30. struct VS_OUTPUT
  31. {
  32.     float4 Pos  : POSITION;
  33.     float4 Diff : COLOR0;
  34.     float4 Spec : COLOR1;
  35.     float2 Tex  : TEXCOORD0;
  36. };
  37.  
  38. VS_OUTPUT VS(
  39.     float3 Pos  : POSITION, 
  40.     float3 Norm : NORMAL, 
  41.     float2 Tex  : TEXCOORD0)
  42. {
  43.     VS_OUTPUT Out = (VS_OUTPUT)0;
  44.  
  45.     float3 L = -lightDir;
  46.  
  47.     float4x4 WorldView = mul(World, View);
  48.  
  49.     float3 P = mul(float4(Pos, 1), (float4x3)WorldView);  // position (view space)
  50.     float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (view space)
  51.  
  52.     float3 R = normalize(2 * dot(N, L) * N - L);          // reflection vector (view space)
  53.     float3 V = -normalize(P);                             // view direction (view space)
  54.  
  55.     Out.Pos  = mul(float4(P, 1), Projection);             // position (projected)
  56.     Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
  57.     Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4);   // specular
  58.     Out.Tex  = Tex;                                       
  59.  
  60.     return Out;
  61. }
  62.  
  63. sampler Sampler = sampler_state
  64. {
  65.     Texture   = (Tex0);
  66.     MipFilter = LINEAR;
  67.     MinFilter = LINEAR;
  68.     MagFilter = LINEAR;
  69. };
  70.  
  71. float4 PS(
  72.     float4 Diff : COLOR0,
  73.     float4 Spec : COLOR1,
  74.     float2 Tex  : TEXCOORD0) : COLOR
  75. {
  76.     return tex2D(Sampler, Tex) * Diff + Spec;
  77. }
  78.  
  79. technique TNoShader
  80. {
  81.     pass P0
  82.     {
  83.         // transforms
  84.         WorldTransform[0]   = (World);
  85.         ViewTransform       = (View);
  86.         ProjectionTransform = (Projection);
  87.  
  88.         // material
  89.         MaterialAmbient  = (k_a); 
  90.         MaterialDiffuse  = (k_d); 
  91.         MaterialSpecular = (k_s); 
  92.         MaterialPower    = (n);
  93.         
  94.         // lighting
  95.         LightType[0]      = DIRECTIONAL;
  96.         LightAmbient[0]   = (I_a);
  97.         LightDiffuse[0]   = (I_d);
  98.         LightSpecular[0]  = (I_s); 
  99.         LightDirection[0] = (lightDir);
  100.         LightRange[0]     = 100000.0f;
  101.  
  102.         LightEnable[0] = TRUE;
  103.         Lighting       = TRUE;
  104.         SpecularEnable = TRUE;
  105.         
  106.         // samplers
  107.         Sampler[0] = (Sampler);
  108.         
  109.         // texture stages
  110.         ColorOp[0]   = MODULATE;
  111.         ColorArg1[0] = TEXTURE;
  112.         ColorArg2[0] = DIFFUSE;
  113.         AlphaOp[0]   = MODULATE;
  114.         AlphaArg1[0] = TEXTURE;
  115.         AlphaArg2[0] = DIFFUSE;
  116.  
  117.         ColorOp[1]   = DISABLE;
  118.         AlphaOp[1]   = DISABLE;
  119.  
  120.         // shaders
  121.         VertexShader = NULL;
  122.         PixelShader  = NULL;
  123.     }
  124. }
  125.  
  126. technique TVertexShaderOnly
  127. {
  128.     pass P0
  129.     {
  130.         // lighting
  131.         Lighting       = FALSE;
  132.         SpecularEnable = TRUE;
  133.  
  134.         // samplers
  135.         Sampler[0] = (Sampler);
  136.  
  137.         // texture stages
  138.         ColorOp[0]   = MODULATE;
  139.         ColorArg1[0] = TEXTURE;
  140.         ColorArg2[0] = DIFFUSE;
  141.         AlphaOp[0]   = MODULATE;
  142.         AlphaArg1[0] = TEXTURE;
  143.         AlphaArg2[0] = DIFFUSE;
  144.  
  145.         ColorOp[1]   = DISABLE;
  146.         AlphaOp[1]   = DISABLE;
  147.  
  148.         // shaders
  149.         VertexShader = compile vs_1_1 VS();
  150.         PixelShader  = NULL;
  151.     }
  152. }
  153.  
  154. technique TVertexAndPixelShader
  155. {
  156.     pass P0
  157.     {
  158.         // shaders
  159.         VertexShader = compile vs_1_1 VS();
  160.         PixelShader  = compile ps_1_1 PS();
  161.     }  
  162. }
  163.