shd6bumpdiff.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWSHADE *
  23. * *
  24. * $Archive:: wwshade/shd6bumpdiff.cpp $*
  25. * *
  26. * $Org Author:: Kenny_m
  27. *
  28. * $Author:: Kenny_m
  29. *
  30. * $Modtime:: 7/11/02 10:36p $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "dx8fvf.h"
  38. #include "dx8wrapper.h"
  39. #include "assetmgr.h"
  40. #include "rinfo.h"
  41. #include "camera.h"
  42. #include "shdbumpdiff.h"
  43. #include "shd6bumpdiff.h"
  44. #include "shd6bumpdiff_constants.h"
  45. #include "shdclassids.h"
  46. // shader code declarations
  47. #include "shd6bumpdiff.vsh_code.h"
  48. ShdHWVertexShader Shd6BumpDiffClass::Vertex_Shader;
  49. Matrix4x4 Shd6BumpDiffClass::View_Projection_Matrix;
  50. Shd6BumpDiffClass::Shd6BumpDiffClass(const ShdDefClass* def)
  51. : ShdInterfaceClass(def,SHDDEF_CLASSID_BUMPDIFF),
  52. Texture(NULL)
  53. {
  54. ShdBumpDiffDefClass* Definition=(ShdBumpDiffDefClass*)def;
  55. Texture=WW3DAssetManager::Get_Instance()->Get_Texture
  56. (
  57. Definition->Get_Texture_Name()
  58. );
  59. const Vector3& a=Definition->Get_Ambient();
  60. Ambient.Set(a.X,a.Y,a.Z,1.0f);
  61. const Vector3& d=Definition->Get_Diffuse();
  62. Diffuse.Set(d.X,d.Y,d.Z,1.0f);
  63. }
  64. Shd6BumpDiffClass::~Shd6BumpDiffClass()
  65. {
  66. REF_PTR_RELEASE(Texture);
  67. }
  68. void Shd6BumpDiffClass::Init()
  69. {
  70. // Create vertex shader
  71. DWORD vertex_shader_declaration[]=
  72. {
  73. D3DVSD_STREAM(0),
  74. (D3DVSD_REG(0, D3DVSDT_FLOAT3)), // vertex position
  75. (D3DVSD_REG(1, D3DVSDT_FLOAT3)), // vertex normal
  76. (D3DVSD_REG(2, D3DVSDT_D3DCOLOR)), // vertex color
  77. (D3DVSD_REG(3, D3DVSDT_FLOAT2)), // vertex texture coords
  78. D3DVSD_END()
  79. };
  80. Vertex_Shader.Create
  81. (
  82. shd6bumpdiff_vsh_code,
  83. vertex_shader_declaration
  84. );
  85. }
  86. void Shd6BumpDiffClass::Shutdown()
  87. {
  88. Vertex_Shader.Destroy();
  89. }
  90. /**********************************************************************************************
  91. //! Apply shared states for 1 pass DX6 (no bump)
  92. /*! 7/10/02 5:39p KJM Created
  93. */
  94. void Shd6BumpDiffClass::Apply_Shared(int pass, RenderInfoClass& rinfo)
  95. {
  96. // vertex processing behavior
  97. DX8Wrapper::Set_DX8_Render_State(D3DRS_SOFTWAREVERTEXPROCESSING,!Vertex_Shader.Is_Using_Hardware());
  98. // fixed function uses pass through by default
  99. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  100. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  101. // set vertex shader
  102. DX8Wrapper::Set_Vertex_Shader(Vertex_Shader.Peek_Shader());
  103. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  104. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  105. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  106. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  107. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  108. // set constants
  109. DX8Wrapper::Set_Vertex_Shader_Constant(CV_CONST, D3DXVECTOR4(0.0f, 1.0f, 0.5f, 2.0f), 1);
  110. // calculate shader view projection matrix
  111. Matrix4x4 view_matrix;
  112. DX8Wrapper::Get_Transform(D3DTS_VIEW, view_matrix);
  113. Matrix4x4 proj_matrix;
  114. DX8Wrapper::Get_Transform(D3DTS_PROJECTION, proj_matrix);
  115. Matrix4x4::Multiply(proj_matrix, view_matrix, &View_Projection_Matrix);
  116. }
  117. //**********************************************************************************************
  118. //! Apply per instance states for 1 pass DX6 specular with gloss map (no bump)
  119. /*! 7/10/02 5:39p KJM Created
  120. */
  121. void Shd6BumpDiffClass::Apply_Instance(int cur_pass, RenderInfoClass& rinfo)
  122. {
  123. DX8Wrapper::Set_Texture(0, Texture);
  124. // set vertex shader constants
  125. Matrix4x4 world;
  126. DX8Wrapper::Get_Transform(D3DTS_WORLD, world);
  127. Matrix4x4 world_view_proj_matrix;
  128. Matrix4x4::Multiply(View_Projection_Matrix,world,&world_view_proj_matrix);
  129. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD_VIEW_PROJECTION, &world_view_proj_matrix, 4);
  130. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD, &world, 4);
  131. ShdHWVertexShader::Light
  132. (
  133. rinfo,
  134. Ambient,
  135. Diffuse
  136. );
  137. }
  138. unsigned Shd6BumpDiffClass::Get_Vertex_Stream_Count() const
  139. {
  140. return 1;
  141. }
  142. unsigned Shd6BumpDiffClass::Get_Vertex_Size(unsigned stream) const
  143. {
  144. return sizeof(VertexFormatXYZNDUV1);
  145. }
  146. void Shd6BumpDiffClass::Copy_Vertex_Stream
  147. (
  148. unsigned stream,
  149. void* dest_buffer,
  150. const VertexStreamStruct& vss,
  151. unsigned vertex_count
  152. )
  153. {
  154. VertexFormatXYZNDUV1* verts=(VertexFormatXYZNDUV1*)dest_buffer;
  155. for (unsigned i=0; i<vertex_count; ++i)
  156. {
  157. if (vss.Locations)
  158. {
  159. verts[i].x=vss.Locations[i][0];
  160. verts[i].y=vss.Locations[i][1];
  161. verts[i].z=vss.Locations[i][2];
  162. }
  163. else
  164. {
  165. verts[i].x=0.0f;
  166. verts[i].y=0.0f;
  167. verts[i].z=0.0f;
  168. }
  169. if (vss.DiffuseInt)
  170. {
  171. verts[i].diffuse=vss.DiffuseInt[i];
  172. }
  173. else
  174. {
  175. verts[i].diffuse=0xffffffff;
  176. }
  177. if (vss.Normals)
  178. {
  179. verts[i].nx=vss.Normals[i][0];
  180. verts[i].ny=vss.Normals[i][1];
  181. verts[i].nz=vss.Normals[i][2];
  182. }
  183. else
  184. {
  185. verts[i].nx=0.0f;
  186. verts[i].ny=0.0f;
  187. verts[i].nz=0.0f;
  188. }
  189. if (vss.UV[0])
  190. {
  191. verts[i].u1=vss.UV[0][i].U;
  192. verts[i].v1=vss.UV[0][i].V;
  193. }
  194. else
  195. {
  196. verts[i].u1=0.0f;
  197. verts[i].v1=0.0f;
  198. }
  199. }
  200. }