shdcubemap.cpp 9.0 KB

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