shd6bumpspec.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/shd6bumpspec.cpp $*
  25. * *
  26. * $Org Author:: Kenny_m
  27. *
  28. * $Author:: Kenny_m
  29. *
  30. * $Modtime:: 7/11/02 10:36p $*
  31. * *
  32. * $Revision:: 1 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "dx8fvf.h"
  38. #include "dx8wrapper.h"
  39. #include "assetmgr.h"
  40. #include "rinfo.h"
  41. #include "camera.h"
  42. #include "shdbumpspec.h"
  43. #include "shd6bumpspec.h"
  44. #include "shd6bumpspec_constants.h"
  45. #include "shdclassids.h"
  46. // shader code declarations
  47. #include "shd6bumpspec.vsh_code.h"
  48. ShdHWVertexShader Shd6BumpSpecClass::Vertex_Shader;
  49. Matrix4x4 Shd6BumpSpecClass::View_Projection_Matrix;
  50. Shd6BumpSpecClass::Shd6BumpSpecClass(const ShdDefClass* def)
  51. : ShdInterfaceClass(def,SHDDEF_CLASSID_BUMPSPEC),
  52. Texture(NULL)
  53. {
  54. ShdBumpSpecDefClass* Definition=(ShdBumpSpecDefClass*)def;
  55. Texture=WW3DAssetManager::Get_Instance()->Get_Texture
  56. (
  57. Definition->Get_Texture_Name()
  58. );
  59. const Vector3& a=Definition->Get_Ambient();
  60. Ambient.Set(a.X,a.Y,a.Z,1.0f);
  61. const Vector3& d=Definition->Get_Diffuse();
  62. Diffuse.Set(d.X,d.Y,d.Z,1.0f);
  63. const Vector3& s=Definition->Get_Specular();
  64. Specular.Set(s.X,s.Y,s.Z,1.0f);
  65. }
  66. Shd6BumpSpecClass::~Shd6BumpSpecClass()
  67. {
  68. REF_PTR_RELEASE(Texture);
  69. }
  70. void Shd6BumpSpecClass::Init()
  71. {
  72. // Create vertex shader
  73. DWORD vertex_shader_declaration[]=
  74. {
  75. D3DVSD_STREAM(0),
  76. (D3DVSD_REG(0, D3DVSDT_FLOAT3)), // vertex position
  77. (D3DVSD_REG(1, D3DVSDT_FLOAT3)), // vertex normal
  78. (D3DVSD_REG(2, D3DVSDT_D3DCOLOR)), // vertex color
  79. (D3DVSD_REG(3, D3DVSDT_FLOAT2)), // vertex texture coords
  80. D3DVSD_END()
  81. };
  82. Vertex_Shader.Create
  83. (
  84. shd6bumpspec_vsh_code,
  85. vertex_shader_declaration
  86. );
  87. }
  88. void Shd6BumpSpecClass::Shutdown()
  89. {
  90. Vertex_Shader.Destroy();
  91. }
  92. /**********************************************************************************************
  93. //! Apply shared states for 1 pass DX6 specular with gloss map (no bump)
  94. /*! 7/10/02 5:39p KJM Created
  95. */
  96. void Shd6BumpSpecClass::Apply_Shared(int pass, RenderInfoClass& rinfo)
  97. {
  98. // vertex processing behavior
  99. DX8Wrapper::Set_DX8_Render_State(D3DRS_SOFTWAREVERTEXPROCESSING,!Vertex_Shader.Is_Using_Hardware());
  100. // fixed function uses pass through by default
  101. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  102. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU);
  103. // set vertex shader
  104. DX8Wrapper::Set_Vertex_Shader(Vertex_Shader.Peek_Shader());
  105. const Matrix3D& cam=rinfo.Camera.Get_Transform();
  106. // set constants
  107. DX8Wrapper::Set_Vertex_Shader_Constant
  108. (
  109. CV_EYE_WORLD,
  110. D3DXVECTOR4
  111. (
  112. cam.Get_X_Translation(),
  113. cam.Get_Y_Translation(),
  114. cam.Get_Z_Translation(),
  115. 1.0f
  116. ),
  117. 1
  118. );
  119. // set texture stage settings
  120. if (DX8Wrapper::Get_Current_Caps()->Support_ModAlphaAddClr())
  121. {
  122. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  123. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  124. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  125. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  126. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  127. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG1, D3DTA_CURRENT );
  128. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_MODULATEALPHA_ADDCOLOR);
  129. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG2, D3DTA_SPECULAR );
  130. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
  131. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
  132. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_COLOROP, D3DTOP_DISABLE );
  133. DX8Wrapper::Set_DX8_Texture_Stage_State(2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  134. }
  135. else
  136. {
  137. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  138. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE );
  139. DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  140. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_DISABLE );
  141. DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
  142. }
  143. // set constants
  144. DX8Wrapper::Set_Vertex_Shader_Constant(CV_CONST, D3DXVECTOR4(0.0f, 1.0f, 0.5f, 2.0f), 1);
  145. // calculate shader view projection matrix
  146. Matrix4x4 view_matrix;
  147. DX8Wrapper::Get_Transform(D3DTS_VIEW, view_matrix);
  148. Matrix4x4 proj_matrix;
  149. DX8Wrapper::Get_Transform(D3DTS_PROJECTION, proj_matrix);
  150. Matrix4x4::Multiply(proj_matrix, view_matrix, &View_Projection_Matrix);
  151. }
  152. //**********************************************************************************************
  153. //! Apply per instance states for 1 pass DX6 specular with gloss map (no bump)
  154. /*! 7/10/02 5:39p KJM Created
  155. */
  156. void Shd6BumpSpecClass::Apply_Instance(int cur_pass, RenderInfoClass& rinfo)
  157. {
  158. DX8Wrapper::Set_Texture(0, Texture);
  159. // set vertex shader constants
  160. Matrix4x4 world;
  161. DX8Wrapper::Get_Transform(D3DTS_WORLD, world);
  162. Matrix4x4 world_view_proj_matrix;
  163. Matrix4x4::Multiply(View_Projection_Matrix,world,&world_view_proj_matrix);
  164. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD_VIEW_PROJECTION, &world_view_proj_matrix, 4);
  165. DX8Wrapper::Set_Vertex_Shader_Constant(CV_WORLD, &world, 4);
  166. ShdHWVertexShader::Light
  167. (
  168. rinfo,
  169. Ambient,
  170. Diffuse,
  171. Specular
  172. );
  173. }
  174. unsigned Shd6BumpSpecClass::Get_Vertex_Stream_Count() const
  175. {
  176. return 1;
  177. }
  178. unsigned Shd6BumpSpecClass::Get_Vertex_Size(unsigned stream) const
  179. {
  180. return sizeof(VertexFormatXYZNDUV1);
  181. }
  182. void Shd6BumpSpecClass::Copy_Vertex_Stream
  183. (
  184. unsigned stream,
  185. void* dest_buffer,
  186. const VertexStreamStruct& vss,
  187. unsigned vertex_count
  188. )
  189. {
  190. VertexFormatXYZNDUV1* verts=(VertexFormatXYZNDUV1*)dest_buffer;
  191. for (unsigned i=0; i<vertex_count; ++i)
  192. {
  193. if (vss.Locations)
  194. {
  195. verts[i].x=vss.Locations[i][0];
  196. verts[i].y=vss.Locations[i][1];
  197. verts[i].z=vss.Locations[i][2];
  198. }
  199. else
  200. {
  201. verts[i].x=0.0f;
  202. verts[i].y=0.0f;
  203. verts[i].z=0.0f;
  204. }
  205. if (vss.DiffuseInt)
  206. {
  207. verts[i].diffuse=vss.DiffuseInt[i];
  208. }
  209. else
  210. {
  211. verts[i].diffuse=0xffffffff;
  212. }
  213. if (vss.Normals)
  214. {
  215. verts[i].nx=vss.Normals[i][0];
  216. verts[i].ny=vss.Normals[i][1];
  217. verts[i].nz=vss.Normals[i][2];
  218. }
  219. else
  220. {
  221. verts[i].nx=0.0f;
  222. verts[i].ny=0.0f;
  223. verts[i].nz=0.0f;
  224. }
  225. if (vss.UV[0])
  226. {
  227. verts[i].u1=vss.UV[0][i].U;
  228. verts[i].v1=vss.UV[0][i].V;
  229. }
  230. else
  231. {
  232. verts[i].u1=0.0f;
  233. verts[i].v1=0.0f;
  234. }
  235. }
  236. }