shd8bumpdiff.cpp 9.6 KB

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