GameCode4.fx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //--------------------------------------------------------------------------------------
  2. // GameCode4.fx - the shader file for GameCode4 (D3D9 renderer)
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //--------------------------------------------------------------------------------------
  38. //--------------------------------------------------------------------------------------
  39. // This shader is not described in the 4th edition of Game Coding Complete.
  40. // It was described in the 3rd edition, Chapter 14, page 509-514
  41. //--------------------------------------------------------------------------------------
  42. //--------------------------------------------------------------------------------------
  43. // Global variables
  44. //--------------------------------------------------------------------------------------
  45. float4 g_MaterialAmbientColor; // Material's ambient color
  46. float4 g_MaterialDiffuseColor; // Material's diffuse color
  47. int g_nNumLights;
  48. // This shader can handle eight directional lights with a diffuse component and one ambient light
  49. float4 g_LightDir[8]; // Light's direction in world space
  50. float4 g_LightDiffuse[8]; // Light's diffuse color
  51. float4 g_LightAmbient; // Light's ambient color
  52. float g_fAlpha; // Alpha value (0.0 totally transparent, 1.0 opaque)
  53. texture g_MeshTexture; // Color texture for mesh
  54. float g_fTime; // App's time in seconds
  55. float4x4 g_mWorld; // World matrix for object
  56. float4x4 g_mWorldViewProjection; // World * View * Projection matrix
  57. //--------------------------------------------------------------------------------------
  58. // Texture samplers
  59. //--------------------------------------------------------------------------------------
  60. sampler MeshTextureSampler =
  61. sampler_state
  62. {
  63. Texture = <g_MeshTexture>;
  64. MipFilter = LINEAR;
  65. MinFilter = LINEAR;
  66. MagFilter = LINEAR;
  67. };
  68. //--------------------------------------------------------------------------------------
  69. // Vertex shader output structure
  70. //--------------------------------------------------------------------------------------
  71. struct VS_OUTPUT
  72. {
  73. float4 Position : POSITION; // vertex position
  74. float4 Diffuse : COLOR0; // vertex diffuse color (note that COLOR0 is clamped from 0..1)
  75. float2 TextureUV : TEXCOORD0; // vertex texture coords
  76. };
  77. //--------------------------------------------------------------------------------------
  78. // This shader computes standard transform and lighting
  79. //--------------------------------------------------------------------------------------
  80. VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
  81. float3 vNormal : NORMAL,
  82. float2 vTexCoord0 : TEXCOORD0,
  83. uniform bool bTexture )
  84. {
  85. VS_OUTPUT Output;
  86. float3 vNormalWorldSpace;
  87. // Transform the position from object space to homogeneous projection space
  88. Output.Position = mul(vPos, g_mWorldViewProjection);
  89. // Transform the normal from object space to world space
  90. vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)
  91. // Compute simple directional lighting equation
  92. float4 vTotalLightDiffuse = float4(0,0,0,0);
  93. for(int i=0; i<g_nNumLights; i++ )
  94. vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));
  95. Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse +
  96. g_MaterialAmbientColor * g_LightAmbient;
  97. Output.Diffuse.a = g_fAlpha;
  98. // Just copy the texture coordinate through
  99. if( bTexture )
  100. Output.TextureUV = vTexCoord0;
  101. else
  102. Output.TextureUV = 0;
  103. return Output;
  104. }
  105. //--------------------------------------------------------------------------------------
  106. // Pixel shader output structure
  107. //--------------------------------------------------------------------------------------
  108. struct PS_OUTPUT
  109. {
  110. float4 RGBColor : COLOR0; // Pixel color
  111. };
  112. //--------------------------------------------------------------------------------------
  113. // This shader outputs the pixel's color by modulating the texture's
  114. // color with diffuse material color
  115. //--------------------------------------------------------------------------------------
  116. PS_OUTPUT RenderScenePS( VS_OUTPUT In,
  117. uniform bool bTexture )
  118. {
  119. PS_OUTPUT Output;
  120. // Lookup mesh texture and modulate it with diffuse
  121. if( bTexture )
  122. Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
  123. else
  124. Output.RGBColor = In.Diffuse;
  125. return Output;
  126. }
  127. //--------------------------------------------------------------------------------------
  128. // Renders scene to render target
  129. //--------------------------------------------------------------------------------------
  130. technique RenderScene
  131. {
  132. pass P0
  133. {
  134. VertexShader = compile vs_2_0 RenderSceneVS( false );
  135. PixelShader = compile ps_2_0 RenderScenePS( false ); // trivial pixel shader (could use FF instead if desired)
  136. }
  137. }
  138. technique RenderSceneWithAlpha
  139. {
  140. pass P0
  141. {
  142. AlphaBlendEnable = true;
  143. SrcBlend = SrcAlpha;
  144. DestBlend = InvSrcAlpha;
  145. VertexShader = compile vs_2_0 RenderSceneVS( false );
  146. PixelShader = compile ps_2_0 RenderScenePS( false ); // trivial pixel shader (could use FF instead if desired)
  147. }
  148. }
  149. technique RenderSceneWithTexture
  150. {
  151. pass P0
  152. {
  153. VertexShader = compile vs_2_0 RenderSceneVS( true );
  154. PixelShader = compile ps_2_0 RenderScenePS( true ); // trivial pixel shader (could use FF instead if desired)
  155. }
  156. }