home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / Dema / ankh_demo_en.exe / media / shared / programs / hw-skinning.cg.c < prev    next >
C/C++ Source or Header  |  2005-10-01  |  2KB  |  86 lines

  1. // define inputs from application
  2. struct appin
  3. {
  4.   float4 position : POSITION;
  5.   float3 normal   : NORMAL;
  6.   float2 uv       : TEXCOORD0;
  7.   float2 uv2      : TEXCOORD1;
  8.   float4 blendIdx : BLENDINDICES;
  9.   float4 blendWgt : BLENDWEIGHT;
  10. };
  11.  
  12. // define outputs from vertex program
  13. struct vertout
  14. {
  15.   float4 position : POSITION;
  16.   float2 uv       : TEXCOORD0;
  17.   float2 uv2      : TEXCOORD1;
  18.   float4 color    : COLOR;
  19. };
  20.  
  21. /* 
  22.   Four-weight-per-vertex hardware skinning, one (directed) light
  23.   The number of bones supported for this is program is 24 (vs_1_1)
  24. */ 
  25. vertout main
  26.  appin v_in,
  27.  
  28.  // Support up to 24 bones of float3x4 
  29.  uniform float3x4 worldMatrix3x4Array[24],
  30.  //uniform float4x4 worldMatrix,
  31.  uniform float4x4 viewProjectionMatrix, 
  32.  uniform float4   lightPos, // is interpreted as directional
  33.  uniform float4   lightDiffuseColour,
  34.  uniform float4   ambient,
  35.  uniform float4   diffuse) 
  36.   vertout v_out;
  37.  
  38.   // transform by indexed matrix 
  39.   float4 blendPos = float4(0,0,0,0); 
  40.   int i; 
  41.  
  42.   /*float weight = 0;
  43.   for(i=0;i<4;++i) {
  44.     weight += v_in.blendWgt[i];
  45.     }*/
  46.  
  47.   for(i=0;i<3;++i) { 
  48.     blendPos += 
  49.       float4(mul(worldMatrix3x4Array[v_in.blendIdx[i]], v_in.position).xyz, 1.0) * 
  50.       v_in.blendWgt[i]; 
  51.   } 
  52.  
  53.   // view / projection 
  54.   v_out.position = mul(viewProjectionMatrix, blendPos); 
  55.  
  56.   // transform normal 
  57.   float3 norm = float3(0,0,0); 
  58.  
  59.   for(i=0;i<4;++i) { 
  60.     norm += mul((float3x3)worldMatrix3x4Array[v_in.blendIdx[i]], v_in.normal) * 
  61.       v_in.blendWgt[i]; 
  62.   } 
  63.   norm = normalize(norm); 
  64.  
  65.   // directed light
  66.   //float3 lightDir0 = mul(worldMatrix, lightPos).xyz;
  67.   float3 lightDir0 = lightPos.xyz;
  68.   // point light would be:
  69.   //float3 lightDir0 = normalize(lightPos - blendPos);
  70.   
  71.   v_out.uv = v_in.uv;
  72.   v_out.uv2 = v_in.uv2;
  73.   ambient = 0.3.xxxx;
  74.   v_out.color = ambient + 
  75.     (saturate(dot(lightDir0, norm)) * lightDiffuseColour);
  76.  
  77.   //v_out.color = diffuse;
  78.   //v_out.color = 1.0.xxxx;
  79.  
  80.   return v_out;
  81. }    
  82.  
  83.  
  84.