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

  1. //
  2. // Shaders used by the ClipVolume D3D sample to draw the teapot and sphere
  3. //
  4. // Note: This effect file does not work with EffectEdit.
  5. //
  6.  
  7. VertexShader ClipVS = asm
  8. {
  9.     vs.1.1 
  10.  
  11.     dcl_position0 v0   // v0 = position
  12.     dcl_normal0   v1   // v1 = normal
  13.  
  14.     // c0...c3  = rotation matrix (from Arcball)  (set externally)
  15.     // c4...c7  = translation matrix              (set externally)
  16.     // c8...c11 = projection matrix               (set externally)
  17.     // c12.xyz  = center of sphere in world space (set externally)
  18.     // c12.w    = radius of sphere                (set externally)
  19.     // c91      = green color - red color (c91 + c92 = green color)
  20.     // c92      = red color
  21.     def c91, -1, 1, 0, 0 
  22.     def c92, 1, 0, 0, 0 
  23.  
  24.     //-------------------------------------------------------------------------
  25.     // POSITION
  26.     //-------------------------------------------------------------------------
  27.  
  28.     m4x4 r0, v0, c0 
  29.     m4x4 r1, r0, c4 
  30.     m4x4 oPos, r1, c8 
  31.  
  32.     //-------------------------------------------------------------------------
  33.     // Calculate per-vertex distance from center of sphere
  34.     //-------------------------------------------------------------------------
  35.  
  36.     sub r2.xyz, r1.xyz, c12.xyz 
  37.     dp3 r2.x, r2, r2 
  38.     rsq r2.x, r2.x 
  39.     rcp r2.x, r2.x 
  40.     // r2.x = distance between current vertex and center of sphere
  41.  
  42.     sub oT0, r2.x, c12.w 
  43.     // if the current vertex is inside the sphere, oT0 < 0
  44.     // else                                      , oT0 >= 0
  45.  
  46.     //-------------------------------------------------------------------------
  47.     // DIFFUSE (simple two-sided lighting)
  48.     //-------------------------------------------------------------------------
  49.  
  50.     m3x3 r3.xyz, v1, c0 
  51.     // NORMAL : rotation (from Arcball)
  52.  
  53.     sge r3.x, -r3.z, c91.w 
  54.     // assume light vector = (0, 0, -1), -r3.z = [light] dot [normal]
  55.     // if front-facing, r3.x = 1
  56.     // else           , r3.x = 0
  57.  
  58.     mov r4, c91 
  59.     mad r5, r4, r3.x, c92 
  60.     // if r3.x == 1, r5 = (0, 1, 0, 0) = green (front-facing)
  61.     // else        , r5 = (1, 0, 0, 0) = red (back-facing)
  62.  
  63.     max r6.x, -r3.z, r3.z 
  64.     mul oD0, r5, r6.x 
  65.     // output DIFFUSE : modulate with abs(LAMBERTIAN TERM) before output
  66. };
  67.  
  68. PixelShader ClipPS = asm 
  69. {
  70.     ps.1.1
  71.  
  72.     texkill t0
  73.     // discard the current pixel if any of t0.x | t0.y | t0.z | t0.w < 0
  74.  
  75.     mov r0, v0 
  76.     // output DIFFUSE : r0.w is used as alpha-blending factor later
  77. };
  78.  
  79. // Shader used by the ClipVolume D3D sample to draw the sphere
  80.  
  81. VertexShader SphereVS = asm
  82. {
  83.     vs.1.1
  84.  
  85.     // c4...c7  = translation matrix (set externally)
  86.     // c8...c11 = projection matrix  (set externally)
  87.     // c13      = color of sphere (.w is the alpha-blending factor)
  88.     def c13, 0, 0, 0.5, 0.5
  89.  
  90.     dcl_position0 v0   // v0 = position (we don't use normal v1 here)
  91.  
  92.     m4x4 r0, v0, c4 
  93.     m4x4 oPos, r0, c8 
  94.     // output POSITION
  95.  
  96.     mov oD0, c13 
  97.     // output DIFFUSE : .w is used as alpha-blending factor later
  98. };
  99.  
  100. technique Teapot
  101. {
  102.     pass p0
  103.     {
  104.         CullMode = None;
  105.         AlphaBlendEnable = False;
  106.         VertexShader = (ClipVS);
  107.         PixelShader  = (ClipPS);
  108.     }
  109. }
  110.  
  111. technique Sphere
  112. {
  113.     pass p0
  114.     {
  115.         CullMode = CCW;
  116.         AlphaBlendEnable = True;
  117.         SrcBlend = SrcAlpha;
  118.         DestBlend = InvSrcAlpha;
  119.         VertexShader = (SphereVS);
  120.         PixelShader  = Null;
  121.     }
  122. }
  123.  
  124.