shd8bumpspec.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/shd8bumpspec.cpp $*
  25. * *
  26. * $Author:: Kenny_m
  27. *
  28. * $Modtime:: 5/27/02 3:40p $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "dx8fvf.h"
  36. #include "dx8wrapper.h"
  37. #include "assetmgr.h"
  38. #include "rinfo.h"
  39. #include "camera.h"
  40. #include "shdmesh.h"
  41. #include "texproject.h"
  42. #include "shdbumpspec.h"
  43. #include "shd8bumpspec.h"
  44. #include "shd8bumpspec_constants.h"
  45. #include "shdclassids.h"
  46. // shader code declarations
  47. #include "shd8bumpspec.vsh_code.h"
  48. #include "shd8bumpspec.psh_code.h"
  49. #include "shd8ssbumpspec.vsh_code.h"
  50. #include "shd8ssbumpspec.psh_code.h"
  51. ShdHWVertexShader Shd8BumpSpecClass::Vertex_Shader;
  52. ShdHWPixelShader Shd8BumpSpecClass::Pixel_Shader;
  53. ShdHWPixelShader Shd8BumpSpecClass::Self_Shadow_Pixel_Shader;
  54. ShdHWVertexShader Shd8BumpSpecClass::Self_Shadow_Vertex_Shader;
  55. Matrix4x4 Shd8BumpSpecClass::View_Projection_Matrix;
  56. Shd8BumpSpecClass::Shd8BumpSpecClass(const ShdDefClass* def)
  57. : ShdInterfaceClass(def,SHDDEF_CLASSID_BUMPSPEC),
  58. Texture(NULL),
  59. NormalMap(NULL)
  60. {
  61. ShdBumpSpecDefClass* Definition=(ShdBumpSpecDefClass*)def;
  62. Texture=WW3DAssetManager::Get_Instance()->Get_Texture
  63. (
  64. Definition->Get_Texture_Name()
  65. );
  66. NormalMap=WW3DAssetManager::Get_Instance()->Get_Texture
  67. (
  68. Definition->Get_Bump_Map_Name()
  69. );
  70. const Vector3& a=Definition->Get_Ambient();
  71. Ambient.Set(a.X,a.Y,a.Z,0.0f);
  72. const Vector3& d=Definition->Get_Diffuse();
  73. Diffuse.Set(d.X,d.Y,d.Z,0.0f);
  74. const Vector3& s=Definition->Get_Specular();
  75. Specular.Set(s.X,s.Y,s.Z,0.0f);
  76. const Vector2& db=Definition->Get_Diffuse_Bumpiness();
  77. const Vector2& ds=Definition->Get_Specular_Bumpiness();
  78. Bumpiness.Set(db.X,db.Y,ds.X,ds.Y);
  79. }
  80. Shd8BumpSpecClass::~Shd8BumpSpecClass(void)
  81. {
  82. REF_PTR_RELEASE(Texture);
  83. REF_PTR_RELEASE(NormalMap);
  84. }
  85. void Shd8BumpSpecClass::Init(void)
  86. {
  87. // Create vertex shader
  88. DWORD vertex_shader_declaration[]=
  89. {
  90. D3DVSD_STREAM(0),
  91. (D3DVSD_REG(0, D3DVSDT_FLOAT3)), // vertex position
  92. (D3DVSD_REG(1, D3DVSDT_FLOAT3)), // vertex normal
  93. (D3DVSD_REG(2, D3DVSDT_D3DCOLOR)), // vertex color
  94. (D3DVSD_REG(3, D3DVSDT_FLOAT2)), // vertex texture coords
  95. (D3DVSD_REG(4, D3DVSDT_FLOAT3)), // vertex S basis
  96. (D3DVSD_REG(5, D3DVSDT_FLOAT3)), // vertex T basis
  97. (D3DVSD_REG(6, D3DVSDT_FLOAT3)), // vertex SxT basis
  98. D3DVSD_END()
  99. };
  100. Pixel_Shader.Create
  101. (
  102. shd8bumpspec_psh_code
  103. );
  104. Vertex_Shader.Create
  105. (
  106. shd8bumpspec_vsh_code,
  107. vertex_shader_declaration
  108. );
  109. Self_Shadow_Pixel_Shader.Create
  110. (
  111. shd8ssbumpspec_psh_code
  112. );
  113. Self_Shadow_Vertex_Shader.Create
  114. (
  115. shd8ssbumpspec_vsh_code,
  116. vertex_shader_declaration
  117. );
  118. }
  119. void Shd8BumpSpecClass::Shutdown(void)
  120. {
  121. Vertex_Shader.Destroy();
  122. Pixel_Shader.Destroy();
  123. Self_Shadow_Pixel_Shader.Destroy();
  124. Self_Shadow_Vertex_Shader.Destroy();
  125. }
  126. void Shd8BumpSpecClass::Apply_Shared(int cur_pass, RenderInfoClass& rinfo)
  127. {
  128. // set vertex shader
  129. if (cur_pass==0)
  130. {
  131. DX8Wrapper::Set_Vertex_Shader(Vertex_Shader.Peek_Shader());
  132. }
  133. else
  134. {
  135. DX8Wrapper::Set_Vertex_Shader(Self_Shadow_Vertex_Shader.Peek_Shader());
  136. }
  137. // set vertex shader constants
  138. DX8Wrapper::Set_Vertex_Shader_Constant(CV_CONST, D3DXVECTOR4(0.0f, 1.0f, 0.5f, 2.0f), 1);
  139. const Matrix3D& cam=rinfo.Camera.Get_Transform();
  140. // set constants
  141. DX8Wrapper::Set_Vertex_Shader_Constant
  142. (
  143. CV_EYE_WORLD,
  144. D3DXVECTOR4
  145. (
  146. cam.Get_X_Translation(),
  147. cam.Get_Y_Translation(),
  148. cam.Get_Z_Translation(),
  149. 1.0f
  150. ),
  151. 1
  152. );
  153. // set pixel shader
  154. if (cur_pass==0)
  155. {
  156. DX8Wrapper::Set_Pixel_Shader(Pixel_Shader.Peek_Shader());
  157. }
  158. else
  159. {
  160. DX8Wrapper::Set_Pixel_Shader(Self_Shadow_Pixel_Shader.Peek_Shader());
  161. }
  162. // set constants
  163. DX8Wrapper::Set_Vertex_Shader_Constant(CV_CONST, D3DXVECTOR4(0.0f, 1.0f, 0.5f, 2.0f), 1);
  164. // calculate shader view projection matrix
  165. Matrix4x4 view_matrix;
  166. DX8Wrapper::Get_Transform(D3DTS_VIEW, view_matrix);
  167. Matrix4x4 proj_matrix;
  168. DX8Wrapper::Get_Transform(D3DTS_PROJECTION, proj_matrix);
  169. Matrix4x4::Multiply(proj_matrix, view_matrix, &View_Projection_Matrix);
  170. }
  171. void Shd8BumpSpecClass::Apply_Instance(int cur_pass, RenderInfoClass& rinfo)
  172. {
  173. if (cur_pass==0)
  174. {
  175. DX8Wrapper::Set_Texture(0, NormalMap);
  176. DX8Wrapper::Set_Texture(1, Texture);
  177. }
  178. else
  179. {
  180. ZTextureClass* ztex=DX8Wrapper::Get_Shadow_Map(0);
  181. DX8Wrapper::Set_Texture(0, Texture);
  182. DX8Wrapper::Set_Vertex_Shader_Constant(CV_TEXMAP,&Self_Shadow_Transform,4);
  183. }
  184. // set vertex shader constants
  185. Matrix4x4 world;
  186. DX8Wrapper::Get_Transform(D3DTS_WORLD, world);
  187. Matrix4x4 world_view_proj_matrix;
  188. Matrix4x4::Multiply(View_Projection_Matrix,world,&world_view_proj_matrix);
  189. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD_VIEW_PROJECTION, &world_view_proj_matrix, 4);
  190. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD, &world, 4);
  191. ShdHWVertexShader::Light
  192. (
  193. rinfo,
  194. Ambient,
  195. Diffuse,
  196. Specular
  197. );
  198. DX8Wrapper::Set_Vertex_Shader_Constant(CV_BUMPINESS, &Bumpiness, 1);
  199. }
  200. unsigned Shd8BumpSpecClass::Get_Vertex_Stream_Count() const
  201. {
  202. return 1;
  203. }
  204. unsigned Shd8BumpSpecClass::Get_Vertex_Size(unsigned stream) const
  205. {
  206. return sizeof(VertexFormatXYZNDUV1TG3);
  207. }
  208. void Shd8BumpSpecClass::Copy_Vertex_Stream
  209. (
  210. unsigned stream,
  211. void* dest_buffer,
  212. const VertexStreamStruct& vss,
  213. unsigned vertex_count
  214. )
  215. {
  216. VertexFormatXYZNDUV1TG3* verts=(VertexFormatXYZNDUV1TG3*)dest_buffer;
  217. for (unsigned i=0; i<vertex_count; ++i)
  218. {
  219. if (vss.Locations)
  220. {
  221. verts[i].x=vss.Locations[i][0];
  222. verts[i].y=vss.Locations[i][1];
  223. verts[i].z=vss.Locations[i][2];
  224. }
  225. else
  226. {
  227. verts[i].x=0.0f;
  228. verts[i].y=0.0f;
  229. verts[i].z=0.0f;
  230. }
  231. if (vss.DiffuseInt)
  232. {
  233. verts[i].diffuse=vss.DiffuseInt[i];
  234. }
  235. else
  236. {
  237. verts[i].diffuse=0xffffffff;
  238. }
  239. if (vss.Normals)
  240. {
  241. verts[i].nx=vss.Normals[i][0];
  242. verts[i].ny=vss.Normals[i][1];
  243. verts[i].nz=vss.Normals[i][2];
  244. }
  245. else
  246. {
  247. verts[i].nx=0.0f;
  248. verts[i].ny=0.0f;
  249. verts[i].nz=0.0f;
  250. }
  251. if (vss.UV[0])
  252. {
  253. verts[i].u1=vss.UV[0][i].U;
  254. verts[i].v1=vss.UV[0][i].V;
  255. }
  256. else
  257. {
  258. verts[i].u1=0.0f;
  259. verts[i].v1=0.0f;
  260. }
  261. if (vss.S)
  262. {
  263. verts[i].Sx=vss.S[i].X;
  264. verts[i].Sy=vss.S[i].Y;
  265. verts[i].Sz=vss.S[i].Z;
  266. }
  267. else
  268. {
  269. verts[i].Sx=0.0f;
  270. verts[i].Sy=0.0f;
  271. verts[i].Sz=0.0f;
  272. }
  273. if (vss.T)
  274. {
  275. verts[i].Tx=vss.T[i].X;
  276. verts[i].Ty=vss.T[i].Y;
  277. verts[i].Tz=vss.T[i].Z;
  278. }
  279. else
  280. {
  281. verts[i].Tx=0.0f;
  282. verts[i].Ty=0.0f;
  283. verts[i].Tz=0.0f;
  284. }
  285. if (vss.SxT)
  286. {
  287. verts[i].SxTx=vss.SxT[i].X;
  288. verts[i].SxTy=vss.SxT[i].Y;
  289. verts[i].SxTz=vss.SxT[i].Z;
  290. }
  291. else
  292. {
  293. verts[i].SxTx=0.0f;
  294. verts[i].SxTy=0.0f;
  295. verts[i].SxTz=0.0f;
  296. }
  297. }
  298. }
  299. bool Shd8BumpSpecClass::Pass_Selection
  300. (
  301. ShdMeshClass* mesh,
  302. RenderInfoClass* rinfo,
  303. int pass
  304. )
  305. {
  306. if (mesh->Is_Applying_Shadow_Map())
  307. {
  308. if (pass==1) // just do once
  309. {
  310. // is rendering self shadowed object
  311. Setup_Self_Shadow_Info(*mesh,*rinfo);
  312. }
  313. return (pass==1);
  314. }
  315. return (pass==0);
  316. }
  317. /**********************************************************************************************
  318. //! Set up transform for shadow maps
  319. /*! 06/07/02 KM created
  320. */
  321. void Shd8BumpSpecClass::Setup_Self_Shadow_Info(ShdMeshClass& mesh, RenderInfoClass& rinfo)
  322. {
  323. Matrix4x4 tex_mat=mesh.Peek_Texture_Projector()->Peek_Mapper()->Get_Texture_Transform();
  324. //set special texture matrix for shadow mapping
  325. ZTextureClass* smap=DX8Wrapper::Get_Shadow_Map(0); // todo KJM assign shadow map from rinfo
  326. if (!smap) return;
  327. float off_x=0.5f+(0.5f/(float)smap->Get_Width());
  328. float off_y=0.5f+(0.5f/(float)smap->Get_Height());
  329. unsigned int bits=Get_Num_Depth_Bits(smap->Get_Texture_Format());
  330. float range=(float)(0xFFFFFFFF>>(32-bits));
  331. float bias=0.0f;//-0.001f*range;
  332. Matrix4x4 sb_mat
  333. (
  334. Vector4(0.5f, 0.0f, 0.0f, 0.0f),
  335. Vector4(0.0f, -0.5f, 0.0f, 0.0f),
  336. Vector4(0.0f, 0.0f, range, 0.0f),
  337. Vector4(off_x, off_y, bias, 1.0f)
  338. );
  339. Matrix4x4 view2tex;
  340. Matrix4x4::Multiply(sb_mat,tex_mat,&view2tex);
  341. Matrix4x4 world;
  342. world.Init(mesh.Get_Transform());
  343. Matrix4x4 view;
  344. view.Init(rinfo.Camera.Get_View_Matrix());
  345. Matrix4x4 vw_mat;
  346. Matrix4x4::Multiply(world,view,&vw_mat);
  347. Matrix4x4::Multiply(view2tex,vw_mat,&Self_Shadow_Transform);
  348. }