d3d9.hlsl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // part of the Irrlicht Engine Shader example.
  2. // These simple Direct3D9 pixel and vertex shaders will be loaded by the shaders
  3. // example. Please note that these example shaders don't do anything really useful.
  4. // They only demonstrate that shaders can be used in Irrlicht.
  5. //-----------------------------------------------------------------------------
  6. // Global variables
  7. //-----------------------------------------------------------------------------
  8. float4x4 mWorldViewProj; // World * View * Projection transformation
  9. float4x4 mInvWorld; // Inverted world matrix
  10. float4x4 mTransWorld; // Transposed world matrix
  11. float3 mLightPos; // Light position
  12. float4 mLightColor; // Light color
  13. // Vertex shader output structure
  14. struct VS_OUTPUT
  15. {
  16. float4 Position : POSITION; // vertex position
  17. float4 Diffuse : COLOR0; // vertex diffuse color
  18. float2 TexCoord : TEXCOORD0; // tex coords
  19. };
  20. VS_OUTPUT vertexMain(in float4 vPosition : POSITION,
  21. in float3 vNormal : NORMAL,
  22. float2 texCoord : TEXCOORD0 )
  23. {
  24. VS_OUTPUT Output;
  25. // transform position to clip space
  26. Output.Position = mul(vPosition, mWorldViewProj);
  27. // transform normal
  28. float3 normal = mul(float4(vNormal,0.0), mInvWorld);
  29. // renormalize normal
  30. normal = normalize(normal);
  31. // position in world coodinates
  32. float3 worldpos = mul(mTransWorld, vPosition);
  33. // calculate light vector, vtxpos - lightpos
  34. float3 lightVector = worldpos - mLightPos;
  35. // normalize light vector
  36. lightVector = normalize(lightVector);
  37. // calculate light color
  38. float3 tmp = dot(-lightVector, normal);
  39. tmp = lit(tmp.x, tmp.y, 1.0);
  40. tmp = mLightColor * tmp.y;
  41. Output.Diffuse = float4(tmp.x, tmp.y, tmp.z, 0);
  42. Output.TexCoord = texCoord;
  43. return Output;
  44. }
  45. // Pixel shader output structure
  46. struct PS_OUTPUT
  47. {
  48. float4 RGBColor : COLOR0; // Pixel color
  49. };
  50. sampler2D myTexture;
  51. PS_OUTPUT pixelMain(float2 TexCoord : TEXCOORD0,
  52. float4 Position : POSITION,
  53. float4 Diffuse : COLOR0 )
  54. {
  55. PS_OUTPUT Output;
  56. float4 col = tex2D( myTexture, TexCoord ); // sample color map
  57. // multiply with diffuse and do other senseless operations
  58. Output.RGBColor = Diffuse * col;
  59. Output.RGBColor *= 4.0;
  60. return Output;
  61. }