shdglossmask.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/shdglossmask.cpp $*
  25. * *
  26. * $Org Author:: Kenny_m
  27. *
  28. * $Author:: Kenny_m
  29. *
  30. * $Modtime:: 8/01/02 11:39a $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include <d3dx8math.h>
  38. #include "dx8fvf.h"
  39. #include "dx8wrapper.h"
  40. #include "assetmgr.h"
  41. #include "shdglossmask.h"
  42. #include "editable.h"
  43. #include "shdclassids.h"
  44. #include "shddeffactory.h"
  45. #include "shdinterface.h"
  46. #include "shdhwshader.h"
  47. #include "persistfactory.h"
  48. #include "wwhack.h"
  49. DECLARE_FORCE_LINK(GlossMaskShader);
  50. REGISTER_SHDDEF(ShdGlossMaskDefClass,SHDDEF_CLASSID_GLOSSMASK,"Gloss Mask");
  51. // Save-Load methods for ShdDefClass
  52. enum
  53. {
  54. CHUNKID_VARIABLES = 0x16490460,
  55. VARID_TEXTURE_NAME = 0x00,
  56. VARID_AMBIENT_COLOR,
  57. VARID_DIFFUSE_COLOR,
  58. VARID_SPECULAR_COLOR
  59. };
  60. ShdGlossMaskDefClass::ShdGlossMaskDefClass()
  61. : ShdDefClass(SHDDEF_CLASSID_GLOSSMASK),
  62. Ambient(1,1,1),
  63. Diffuse(1,1,1),
  64. Specular(1,1,1)
  65. {
  66. NAMED_TEXTURE_FILENAME_PARAM(ShdGlossMaskDefClass, TextureName, "Texture Name", "Targa Files", ".tga");
  67. NAMED_EDITABLE_PARAM(ShdGlossMaskDefClass, ParameterClass::TYPE_COLOR, Ambient, "Ambient");
  68. NAMED_EDITABLE_PARAM(ShdGlossMaskDefClass, ParameterClass::TYPE_COLOR, Diffuse, "Diffuse");
  69. NAMED_EDITABLE_PARAM(ShdGlossMaskDefClass, ParameterClass::TYPE_COLOR, Specular, "Specular");
  70. }
  71. ShdGlossMaskDefClass::ShdGlossMaskDefClass(const ShdGlossMaskDefClass& that)
  72. : ShdDefClass(that),
  73. Ambient(that.Ambient),
  74. Diffuse(that.Diffuse),
  75. Specular(that.Specular)
  76. {
  77. TextureName=that.TextureName;
  78. }
  79. ShdGlossMaskDefClass::~ShdGlossMaskDefClass()
  80. {
  81. }
  82. bool ShdGlossMaskDefClass::Is_Valid_Config(StringClass &message)
  83. {
  84. return true;
  85. }
  86. bool ShdGlossMaskDefClass::Save(ChunkSaveClass &csave)
  87. {
  88. ShdDefClass::Save(csave);
  89. csave.Begin_Chunk(CHUNKID_VARIABLES);
  90. bool retval = true;
  91. // only save the file name
  92. char fname[_MAX_PATH];
  93. _splitpath(TextureName,NULL,NULL,fname,NULL);
  94. strcat(fname,".tga");
  95. TextureName=fname;
  96. WRITE_MICRO_CHUNK_WWSTRING(csave, VARID_TEXTURE_NAME, TextureName);
  97. WRITE_MICRO_CHUNK(csave, VARID_AMBIENT_COLOR, Ambient);
  98. WRITE_MICRO_CHUNK(csave, VARID_DIFFUSE_COLOR, Diffuse);
  99. WRITE_MICRO_CHUNK(csave, VARID_SPECULAR_COLOR, Specular);
  100. csave.End_Chunk();
  101. return retval;
  102. }
  103. bool ShdGlossMaskDefClass::Load(ChunkLoadClass &cload)
  104. {
  105. ShdDefClass::Load(cload);
  106. // Loop through all the microchunks that define the variables
  107. while (cload.Open_Chunk()) {
  108. switch (cload.Cur_Chunk_ID())
  109. {
  110. case CHUNKID_VARIABLES:
  111. while (cload.Open_Micro_Chunk())
  112. {
  113. switch (cload.Cur_Micro_Chunk_ID())
  114. {
  115. READ_MICRO_CHUNK_WWSTRING(cload, VARID_TEXTURE_NAME, TextureName);
  116. READ_MICRO_CHUNK(cload, VARID_AMBIENT_COLOR, Ambient);
  117. READ_MICRO_CHUNK(cload, VARID_DIFFUSE_COLOR, Diffuse);
  118. READ_MICRO_CHUNK(cload, VARID_SPECULAR_COLOR, Specular);
  119. }
  120. cload.Close_Micro_Chunk();
  121. }
  122. break;
  123. default:
  124. break;
  125. }
  126. cload.Close_Chunk();
  127. }
  128. return true;
  129. }
  130. void ShdGlossMaskDefClass::Init()
  131. {
  132. Shd6GlossMaskClass::Init();
  133. }
  134. void ShdGlossMaskDefClass::Shutdown()
  135. {
  136. Shd6GlossMaskClass::Shutdown();
  137. }
  138. ShdInterfaceClass* ShdGlossMaskDefClass::Create() const
  139. {
  140. return new Shd6GlossMaskClass(this);
  141. }
  142. Matrix4x4 Shd6GlossMaskClass::View_Projection_Matrix;
  143. Shd6GlossMaskClass::Shd6GlossMaskClass(const ShdDefClass* def)
  144. : ShdInterfaceClass(def,SHDDEF_CLASSID_GLOSSMASK),
  145. Texture(NULL)
  146. {
  147. ShdGlossMaskDefClass* Definition=(ShdGlossMaskDefClass*)def;
  148. Texture=WW3DAssetManager::Get_Instance()->Get_Texture
  149. (
  150. Definition->Get_Texture_Name()
  151. );
  152. const Vector3& a=Definition->Get_Ambient();
  153. Ambient.Set(a.X,a.Y,a.Z,1.0f);
  154. const Vector3& d=Definition->Get_Diffuse();
  155. Diffuse.Set(d.X,d.Y,d.Z,1.0f);
  156. const Vector3& s=Definition->Get_Specular();
  157. Specular.Set(s.X,s.Y,s.Z,1.0f);
  158. Material=new D3DMATERIAL8;
  159. memset(Material,0,sizeof(D3DMATERIAL8));
  160. Material->Ambient.r=a.X; Material->Ambient.g=a.Y; Material->Ambient.b=a.Z;
  161. Material->Diffuse.r=d.X; Material->Diffuse.g=d.Y; Material->Diffuse.b=d.Z;
  162. Material->Specular.r=s.X; Material->Specular.g=s.Y; Material->Specular.b=s.Z;
  163. Material->Power=20;
  164. }
  165. Shd6GlossMaskClass::~Shd6GlossMaskClass()
  166. {
  167. REF_PTR_RELEASE(Texture);
  168. }
  169. void Shd6GlossMaskClass::Init()
  170. {
  171. }
  172. void Shd6GlossMaskClass::Shutdown()
  173. {
  174. }
  175. /**********************************************************************************************
  176. //! Apply shared states for 1 pass DX6
  177. /*! 7/12/02 3:39p KJM Created
  178. */
  179. void Shd6GlossMaskClass::Apply_Shared(int pass, RenderInfoClass& rinfo)
  180. {
  181. // fixed function uses pass through by default
  182. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  183. // set vertex shader
  184. DX8Wrapper::Set_Vertex_Shader(DX8_FVF_XYZNDUV1);
  185. // set texture stage settings
  186. if (DX8Wrapper::Get_Current_Caps()->Support_ModAlphaAddClr())
  187. {
  188. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  189. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  190. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  191. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  192. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  193. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG1, D3DTA_CURRENT );
  194. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_MODULATEALPHA_ADDCOLOR);
  195. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG2, D3DTA_SPECULAR );
  196. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
  197. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  198. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  199. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  200. }
  201. else
  202. {
  203. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  204. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  205. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  206. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  207. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  208. }
  209. DX8Wrapper::Set_DX8_Render_State(D3DRS_LIGHTING, TRUE);
  210. DX8Wrapper::Set_DX8_Render_State(D3DRS_AMBIENTMATERIALSOURCE,D3DMCS_MATERIAL);
  211. DX8Wrapper::Set_DX8_Render_State(D3DRS_DIFFUSEMATERIALSOURCE,D3DMCS_MATERIAL);
  212. DX8Wrapper::Set_DX8_Render_State(D3DRS_SPECULARMATERIALSOURCE,D3DMCS_MATERIAL);
  213. DX8Wrapper::Set_DX8_Render_State(D3DRS_EMISSIVEMATERIALSOURCE,D3DMCS_MATERIAL);
  214. DX8Wrapper::Set_DX8_Material(Material);
  215. }
  216. //**********************************************************************************************
  217. //! Apply per instance states for 1 pass DX6
  218. /*! 7/10/02 5:39p KJM Created
  219. */
  220. void Shd6GlossMaskClass::Apply_Instance(int cur_pass, RenderInfoClass& rinfo)
  221. {
  222. DX8Wrapper::Set_Texture(0, Texture);
  223. if (DX8Wrapper::Get_Current_Caps()->Support_ModAlphaAddClr())
  224. {
  225. DX8Wrapper::Set_Texture(1, Texture);
  226. }
  227. DX8Wrapper::Set_DX8_Material(Material);
  228. }
  229. unsigned Shd6GlossMaskClass::Get_Vertex_Stream_Count() const
  230. {
  231. return 1;
  232. }
  233. unsigned Shd6GlossMaskClass::Get_Vertex_Size(unsigned stream) const
  234. {
  235. return sizeof(VertexFormatXYZNDUV1);
  236. }
  237. void Shd6GlossMaskClass::Copy_Vertex_Stream
  238. (
  239. unsigned stream,
  240. void* dest_buffer,
  241. const VertexStreamStruct& vss,
  242. unsigned vertex_count
  243. )
  244. {
  245. VertexFormatXYZNDUV1* verts=(VertexFormatXYZNDUV1*)dest_buffer;
  246. for (unsigned i=0; i<vertex_count; ++i)
  247. {
  248. if (vss.Locations)
  249. {
  250. verts[i].x=vss.Locations[i][0];
  251. verts[i].y=vss.Locations[i][1];
  252. verts[i].z=vss.Locations[i][2];
  253. }
  254. else
  255. {
  256. verts[i].x=0.0f;
  257. verts[i].y=0.0f;
  258. verts[i].z=0.0f;
  259. }
  260. if (vss.DiffuseInt)
  261. {
  262. verts[i].diffuse=vss.DiffuseInt[i];
  263. }
  264. else
  265. {
  266. verts[i].diffuse=0xffffffff;
  267. }
  268. if (vss.Normals)
  269. {
  270. verts[i].nx=vss.Normals[i][0];
  271. verts[i].ny=vss.Normals[i][1];
  272. verts[i].nz=vss.Normals[i][2];
  273. }
  274. else
  275. {
  276. verts[i].nx=0.0f;
  277. verts[i].ny=0.0f;
  278. verts[i].nz=0.0f;
  279. }
  280. if (vss.UV[0])
  281. {
  282. verts[i].u1=vss.UV[0][i].U;
  283. verts[i].v1=vss.UV[0][i].V;
  284. }
  285. else
  286. {
  287. verts[i].u1=0.0f;
  288. verts[i].v1=0.0f;
  289. }
  290. }
  291. }