home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 July & August / PCWorld_2007-07-08_cd.bin / temacd / chess3d / Chess3D_110.exe / media / data.c3d / CelShading.cg < prev    next >
Text File  |  2004-03-28  |  2KB  |  64 lines

  1.  
  2. /* Cel shading vertex program for single-pass rendering
  3.    In this program, we want to calculate the diffuse and specular
  4.    ramp components, and the edge factor (for doing simple outlining)
  5.    For the outlining to look good, we need a pretty well curved model.
  6. */
  7. void main_vp(float4 position    : POSITION,
  8.              float3 normal        : NORMAL,
  9.              // outputs
  10.              out float4 oPosition : POSITION,
  11.              out float  diffuse         : TEXCOORD0,
  12.              out float  specular     : TEXCOORD1,
  13.              out float  edge         : TEXCOORD2,
  14.              // parameters
  15.              uniform float3 lightPosition, // object space
  16.              uniform float3 eyePosition,   // object space
  17.              uniform float4  shininess,
  18.              uniform float4x4 worldViewProj)
  19. {
  20.     // calculate output position
  21.     oPosition = mul(worldViewProj, position);
  22.  
  23.     // calculate light vector
  24.     float3 N = normalize(normal);
  25.     float3 L = normalize(lightPosition - position.xyz);
  26.     
  27.     // Calculate diffuse component
  28.     diffuse = max(dot(N, L) , 0);
  29.  
  30.     // Calculate specular component
  31.     float3 E = normalize(eyePosition - position.xyz);
  32.     float3 H = normalize(L + E);
  33.     specular = pow(max(dot(N, H), 0), shininess);
  34.     // Mask off specular if diffuse is 0
  35.     if (diffuse == 0) specular = 0;
  36.  
  37.     // Edge detection, dot eye and normal vectors
  38.     edge = max(dot(N, E), 0);
  39. }
  40.  
  41. void main_fp(float diffuseIn     : TEXCOORD0,
  42.              float specularIn    : TEXCOORD1,
  43.              float edge        : TEXCOORD2,
  44.              
  45.              out float4 colour    : COLOR,
  46.              
  47.              uniform float4 diffuse,
  48.              uniform float4 specular,
  49.              
  50.              uniform sampler1D diffuseRamp,
  51.              uniform sampler1D specularRamp,
  52.              uniform sampler1D edgeRamp)
  53. {
  54.     // Step functions from textures
  55.     diffuseIn = tex1D(diffuseRamp, diffuseIn).x;
  56.     specularIn = tex1D(specularRamp, specularIn).x;
  57.     edge = tex1D(edgeRamp, edge).x;
  58.  
  59.     colour = edge * ((diffuse * diffuseIn) + 
  60.                     (specular * specularIn));
  61. }
  62.              
  63.              
  64.