shd7bumpspec.cpp 11 KB

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