shd7bumpdiff.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/shd7bumpdiff.cpp $*
  25. * *
  26. * $Author:: Kenny_m
  27. *
  28. * $Modtime:: 6/06/02 11:18p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. * 06/06/02 KM added software vertex shader fallback check
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "dx8fvf.h"
  37. #include "dx8wrapper.h"
  38. #include "assetmgr.h"
  39. #include "rinfo.h"
  40. #include "camera.h"
  41. #include "shdhwshader.h"
  42. #include "shdbumpdiff.h"
  43. #include "shd7bumpdiff.h"
  44. #include "shd7bumpdiff_constants.h"
  45. #include "shdclassids.h"
  46. // shader code declarations
  47. #include "shd7bumpdiffpass0.vsh_code.h"
  48. #include "shd7bumpdiffpass1.vsh_code.h"
  49. ShdHWVertexShader Shd7BumpDiffClass::Pass_0_Vertex_Shader;
  50. ShdHWVertexShader Shd7BumpDiffClass::Pass_1_Vertex_Shader;
  51. Matrix4x4 Shd7BumpDiffClass::View_Projection_Matrix;
  52. Shd7BumpDiffClass::Shd7BumpDiffClass(const ShdDefClass* def)
  53. : ShdInterfaceClass(def,SHDDEF_CLASSID_BUMPDIFF),
  54. Texture(NULL),
  55. NormalMap(NULL)
  56. {
  57. ShdBumpDiffDefClass* Definition=(ShdBumpDiffDefClass*)def;
  58. Texture=WW3DAssetManager::Get_Instance()->Get_Texture
  59. (
  60. Definition->Get_Texture_Name()
  61. );
  62. NormalMap=WW3DAssetManager::Get_Instance()->Get_Texture
  63. (
  64. Definition->Get_Bump_Map_Name()
  65. );
  66. const Vector3& a=Definition->Get_Ambient();
  67. Ambient.Set(a.X,a.Y,a.Z,1.0f);
  68. const Vector3& d=Definition->Get_Diffuse();
  69. Diffuse.Set(d.X,d.Y,d.Z,1.0f);
  70. const Vector2& db=Definition->Get_Diffuse_Bumpiness();
  71. Bumpiness.Set(db.X,db.Y,0.0f,0.0f);
  72. }
  73. Shd7BumpDiffClass::~Shd7BumpDiffClass()
  74. {
  75. REF_PTR_RELEASE(Texture);
  76. REF_PTR_RELEASE(NormalMap);
  77. }
  78. void Shd7BumpDiffClass::Init()
  79. {
  80. // Create vertex shader
  81. DWORD vertex_shader_declaration[]=
  82. {
  83. D3DVSD_STREAM(0),
  84. (D3DVSD_REG(0, D3DVSDT_FLOAT3)), // vertex position
  85. (D3DVSD_REG(1, D3DVSDT_FLOAT3)), // vertex normal
  86. (D3DVSD_REG(2, D3DVSDT_D3DCOLOR)), // vertex color
  87. (D3DVSD_REG(3, D3DVSDT_FLOAT2)), // vertex texture coords
  88. (D3DVSD_REG(4, D3DVSDT_FLOAT3)), // vertex S basis
  89. (D3DVSD_REG(5, D3DVSDT_FLOAT3)), // vertex T basis
  90. (D3DVSD_REG(6, D3DVSDT_FLOAT3)), // vertex SxT basis
  91. D3DVSD_END()
  92. };
  93. Pass_0_Vertex_Shader.Create
  94. (
  95. shd7bumpdiffpass0_vsh_code,
  96. vertex_shader_declaration
  97. );
  98. Pass_1_Vertex_Shader.Create
  99. (
  100. shd7bumpdiffpass1_vsh_code,
  101. vertex_shader_declaration
  102. );
  103. }
  104. void Shd7BumpDiffClass::Shutdown()
  105. {
  106. Pass_0_Vertex_Shader.Destroy();
  107. Pass_1_Vertex_Shader.Destroy();
  108. }
  109. //**********************************************************************************************
  110. //! Apply shared states for 2 pass DX7 bump diffuse
  111. /*! 6/03/02 8:12a KJM Created
  112. */
  113. void Shd7BumpDiffClass::Apply_Shared(int pass, RenderInfoClass& rinfo)
  114. {
  115. // vertex processing behavior
  116. DX8Wrapper::Set_DX8_Render_State(D3DRS_SOFTWAREVERTEXPROCESSING,!Pass_0_Vertex_Shader.Is_Using_Hardware());
  117. // fixed function uses pass through by default
  118. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  119. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  120. if (pass==0)
  121. {
  122. // set vertex shader
  123. DX8Wrapper::Set_Vertex_Shader(Pass_0_Vertex_Shader.Peek_Shader());
  124. // set texture stage settings
  125. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  126. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_DOTPRODUCT3 );
  127. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  128. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
  129. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  130. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  131. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  132. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG2, D3DTA_SPECULAR );
  133. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
  134. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_ADDSMOOTH );
  135. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
  136. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  137. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  138. DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE, TRUE);
  139. DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  140. DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND, D3DBLEND_ZERO);
  141. }
  142. else
  143. {
  144. // set vertex shader
  145. DX8Wrapper::Set_Vertex_Shader(Pass_1_Vertex_Shader.Peek_Shader());
  146. // set texture stage settings
  147. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  148. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_DOTPRODUCT3 );
  149. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  150. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
  151. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  152. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  153. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
  154. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG2, D3DTA_SPECULAR );
  155. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
  156. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  157. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
  158. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  159. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  160. DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE, TRUE);
  161. DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  162. DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND, D3DBLEND_ONE);
  163. }
  164. // set constants
  165. DX8Wrapper::Set_Vertex_Shader_Constant(CV_CONST, D3DXVECTOR4(0.0f, 1.0f, 0.5f, 2.0f), 1);
  166. // calculate shader view projection matrix
  167. Matrix4x4 view_matrix;
  168. DX8Wrapper::Get_Transform(D3DTS_VIEW, view_matrix);
  169. Matrix4x4 proj_matrix;
  170. DX8Wrapper::Get_Transform(D3DTS_PROJECTION, proj_matrix);
  171. Matrix4x4::Multiply(proj_matrix, view_matrix, &View_Projection_Matrix);
  172. }
  173. //**********************************************************************************************
  174. //! Apply per instance states for 2 pass DX7 bump diffuse
  175. /*! 6/03/02 8:12a KJM Created
  176. */
  177. void Shd7BumpDiffClass::Apply_Instance(int cur_pass, RenderInfoClass& rinfo)
  178. {
  179. DX8Wrapper::Set_Texture(0, NormalMap);
  180. DX8Wrapper::Set_Texture(1, Texture);
  181. // set vertex shader constants
  182. Matrix4x4 world;
  183. DX8Wrapper::Get_Transform(D3DTS_WORLD, world);
  184. Matrix4x4 world_view_proj_matrix;
  185. Matrix4x4::Multiply(View_Projection_Matrix,world,&world_view_proj_matrix);
  186. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD_VIEW_PROJECTION, &world_view_proj_matrix, 4);
  187. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD, &world, 4);
  188. ShdHWVertexShader::Light
  189. (
  190. rinfo,
  191. Ambient,
  192. Diffuse
  193. );
  194. DX8Wrapper::Set_Vertex_Shader_Constant(CV_BUMPINESS, &Bumpiness, 1);
  195. }
  196. unsigned Shd7BumpDiffClass::Get_Vertex_Stream_Count() const
  197. {
  198. return 1;
  199. }
  200. unsigned Shd7BumpDiffClass::Get_Vertex_Size(unsigned stream) const
  201. {
  202. return sizeof(VertexFormatXYZNDUV1TG3);
  203. }
  204. void Shd7BumpDiffClass::Copy_Vertex_Stream
  205. (
  206. unsigned stream,
  207. void* dest_buffer,
  208. const VertexStreamStruct& vss,
  209. unsigned vertex_count
  210. )
  211. {
  212. VertexFormatXYZNDUV1TG3* verts=(VertexFormatXYZNDUV1TG3*)dest_buffer;
  213. for (unsigned i=0; i<vertex_count; ++i)
  214. {
  215. if (vss.Locations)
  216. {
  217. verts[i].x=vss.Locations[i][0];
  218. verts[i].y=vss.Locations[i][1];
  219. verts[i].z=vss.Locations[i][2];
  220. }
  221. else
  222. {
  223. verts[i].x=0.0f;
  224. verts[i].y=0.0f;
  225. verts[i].z=0.0f;
  226. }
  227. if (vss.DiffuseInt)
  228. {
  229. verts[i].diffuse=vss.DiffuseInt[i];
  230. }
  231. else
  232. {
  233. verts[i].diffuse=0xffffffff;
  234. }
  235. if (vss.Normals)
  236. {
  237. verts[i].nx=vss.Normals[i][0];
  238. verts[i].ny=vss.Normals[i][1];
  239. verts[i].nz=vss.Normals[i][2];
  240. }
  241. else
  242. {
  243. verts[i].nx=0.0f;
  244. verts[i].ny=0.0f;
  245. verts[i].nz=0.0f;
  246. }
  247. if (vss.UV[0])
  248. {
  249. verts[i].u1=vss.UV[0][i].U;
  250. verts[i].v1=vss.UV[0][i].V;
  251. }
  252. else
  253. {
  254. verts[i].u1=0.0f;
  255. verts[i].v1=0.0f;
  256. }
  257. if (vss.S)
  258. {
  259. verts[i].Sx=vss.S[i].X;
  260. verts[i].Sy=vss.S[i].Y;
  261. verts[i].Sz=vss.S[i].Z;
  262. }
  263. else
  264. {
  265. verts[i].Sx=0.0f;
  266. verts[i].Sy=0.0f;
  267. verts[i].Sz=0.0f;
  268. }
  269. if (vss.T)
  270. {
  271. verts[i].Tx=vss.T[i].X;
  272. verts[i].Ty=vss.T[i].Y;
  273. verts[i].Tz=vss.T[i].Z;
  274. }
  275. else
  276. {
  277. verts[i].Tx=0.0f;
  278. verts[i].Ty=0.0f;
  279. verts[i].Tz=0.0f;
  280. }
  281. if (vss.SxT)
  282. {
  283. verts[i].SxTx=vss.SxT[i].X;
  284. verts[i].SxTy=vss.SxT[i].Y;
  285. verts[i].SxTz=vss.SxT[i].Z;
  286. }
  287. else
  288. {
  289. verts[i].SxTx=0.0f;
  290. verts[i].SxTy=0.0f;
  291. verts[i].SxTz=0.0f;
  292. }
  293. }
  294. }