shdsimple.cpp 8.4 KB

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