Simple_vp.cg 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Simple Vertex Program
  3. //
  4. // - Calculates simple diffuse and specular
  5. //
  6. // Define 'vendor-specific' diffuse color ;)
  7. float3 diffuseColor = float3(0.8, 1, 0.1); // NVidia
  8. //float3 diffuseColor = float3(1.0, 0.0, 0.0); // ATI
  9. // Define white specular color
  10. float3 specularColor = float3(1.0, 1.0, 1.0);
  11. // Define inputs from application
  12. struct appin
  13. {
  14. float4 Position : POSITION;
  15. float4 Normal : NORMAL;
  16. };
  17. // Define outputs from vertex shader
  18. struct vertout
  19. {
  20. float4 HPosition : POSITION;
  21. float4 Color : COLOR;
  22. };
  23. vertout main(appin IN,
  24. uniform float4x4 ModelViewProj,
  25. uniform float4x4 ModelViewIT,
  26. uniform float4 LightVec)
  27. {
  28. vertout OUT;
  29. // Transform vertex position into homogenous clip-space.
  30. OUT.HPosition = mul(ModelViewProj, IN.Position);
  31. // Transform normal from model-space to view-space.
  32. float3 normalVec = normalize(mul(ModelViewIT, IN.Normal).xyz);
  33. // Store normalized light vector.
  34. float3 lightVec = normalize(LightVec.xyz);
  35. // Calculate half angle vector.
  36. float3 eyeVec = float3(0.0, 0.0, 1.0);
  37. float3 halfVec = normalize(lightVec + eyeVec);
  38. // Calculate diffuse component.
  39. float diffuse = dot(normalVec, lightVec);
  40. // Calculate specular component.
  41. float specular = dot(normalVec, halfVec);
  42. // Use the lit function to compute lighting vector from
  43. // diffuse and specular values.
  44. float4 lighting = lit(diffuse, specular, 32);
  45. // Combine diffuse and specular contributions and
  46. // output final vertex color.
  47. OUT.Color.rgb = lighting.y * diffuseColor +
  48. lighting.z * specularColor;
  49. OUT.Color.a = 1.0;
  50. return OUT;
  51. }