grideffect.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ** Command & Conquer Renegade(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 : wwphys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/grideffect.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 7/23/01 4:46p $*
  31. * *
  32. * $Revision:: 4 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "grideffect.h"
  38. #include "ww3d.h"
  39. #include "physresourcemgr.h"
  40. #include "vertmaterial.h"
  41. #include "matpass.h"
  42. #include "matrixmapper.h"
  43. #include "rinfo.h"
  44. #include "camera.h"
  45. #include "phys.h"
  46. /***********************************************************************************************
  47. **
  48. ** GridEffectClass Implementation
  49. **
  50. ***********************************************************************************************/
  51. const float DEFAULT_GRID_PARAMTER_RATE = 0.5f;
  52. GridEffectClass::GridEffectClass(void) :
  53. CurrentParameter(0.0f),
  54. TargetParameter(1.0f),
  55. ParameterRate(DEFAULT_GRID_PARAMTER_RATE),
  56. LastRenderTime(0),
  57. RenderBaseMaterial(true),
  58. RenderGridMaterial(false),
  59. Stage0Mapper(NULL),
  60. Stage1Mapper(NULL),
  61. MaterialPass(NULL)
  62. {
  63. LastRenderTime = WW3D::Get_Sync_Time();
  64. MaterialPass = NEW_REF(MaterialPassClass,());
  65. VertexMaterialClass * vmtl = PhysResourceMgrClass::Create_Emissive_Material();
  66. Stage0Mapper = NEW_REF(MatrixMapperClass,(0));
  67. Stage0Mapper->Set_Type(MatrixMapperClass::DEPTH_GRADIENT);
  68. vmtl->Set_Mapper(Stage0Mapper,0);
  69. Stage1Mapper = NEW_REF(MatrixMapperClass,(1));
  70. Stage1Mapper->Set_Type(MatrixMapperClass::DEPTH_GRADIENT);
  71. vmtl->Set_Mapper(Stage1Mapper,1);
  72. MaterialPass->Set_Material(vmtl);
  73. REF_PTR_RELEASE(vmtl);
  74. ShaderClass shader = ShaderClass::_PresetAdditiveShader;
  75. shader.Set_Post_Detail_Color_Func(ShaderClass::DETAILCOLOR_ADD);
  76. MaterialPass->Set_Shader(shader);
  77. MaterialPass->Set_Texture(PhysResourceMgrClass::Peek_Grid_Texture(),0);
  78. MaterialPass->Set_Texture(PhysResourceMgrClass::Peek_Grid_Texture(),1);
  79. }
  80. GridEffectClass::~GridEffectClass(void)
  81. {
  82. REF_PTR_RELEASE(Stage0Mapper);
  83. REF_PTR_RELEASE(Stage1Mapper);
  84. REF_PTR_RELEASE(MaterialPass);
  85. }
  86. void GridEffectClass::Render_Push(RenderInfoClass & rinfo,PhysClass * obj)
  87. {
  88. if (CurrentParameter == TargetParameter) {
  89. if (CurrentParameter < 0.5f) {
  90. TargetParameter = 1.0f;
  91. } else {
  92. TargetParameter = 0.0f;
  93. }
  94. }
  95. /*
  96. ** Update the grid properties
  97. ** - compute dt since last render
  98. ** - update the parameter
  99. ** - compute the intensity, if it is above 0.0 then:
  100. ** - compute the texture transform given this camera, update the mappers
  101. ** - compute the texel row, update the mappers
  102. */
  103. int millisecs = WW3D::Get_Sync_Time() - LastRenderTime;
  104. LastRenderTime = WW3D::Get_Sync_Time();
  105. float dt = (float)millisecs / 1000.0f;
  106. float parameter_step = ParameterRate * dt;
  107. if (parameter_step > WWMath::Fabs(TargetParameter - CurrentParameter)) {
  108. CurrentParameter = TargetParameter;
  109. } else {
  110. if (TargetParameter < CurrentParameter) {
  111. CurrentParameter -= parameter_step;
  112. } else {
  113. CurrentParameter += parameter_step;
  114. }
  115. }
  116. static float _angle = 0.0f;
  117. static float _angle_delta = 0.001f;
  118. _angle += _angle_delta;
  119. GridTransform = obj->Get_Transform();
  120. Matrix3D camera_tm = rinfo.Camera.Get_Transform();
  121. Matrix3D xgridtm;
  122. GridTransform.Get_Inverse(xgridtm);
  123. // xgridtm.Rotate_Y(DEG_TO_RADF(90.0f));
  124. Matrix4 tm = Matrix4(xgridtm) * Matrix4(camera_tm);
  125. Stage0Mapper->Set_Texture_Transform(Matrix4(tm),64.0f);
  126. Stage0Mapper->Set_Gradient_U_Coord(CurrentParameter);
  127. Matrix3D ygridtm;
  128. GridTransform.Get_Inverse(ygridtm);
  129. // ygridtm.Rotate_X(DEG_TO_RADF(90.0f));
  130. tm = Matrix4(ygridtm) * Matrix4(camera_tm);
  131. Stage1Mapper->Set_Texture_Transform(tm,64.0f);
  132. Stage1Mapper->Set_Gradient_U_Coord(CurrentParameter);
  133. RenderBaseMaterial = (CurrentParameter < 0.5f);
  134. /*
  135. ** Push this pass if it is visible, decide whether or not to render the base passes
  136. */
  137. rinfo.Push_Material_Pass(MaterialPass);
  138. if (RenderBaseMaterial == false) {
  139. rinfo.Push_Override_Flags(RenderInfoClass::RINFO_OVERRIDE_ADDITIONAL_PASSES_ONLY);
  140. }
  141. }
  142. void GridEffectClass::Render_Pop(RenderInfoClass & rinfo)
  143. {
  144. if (RenderBaseMaterial == false) {
  145. rinfo.Pop_Override_Flags();
  146. }
  147. rinfo.Pop_Material_Pass();
  148. }
  149. void GridEffectClass::Set_Grid_Transform(const Matrix3D & tm)
  150. {
  151. GridTransform = tm;
  152. }
  153. const Matrix3D & GridEffectClass::Get_Grid_Transform(void)
  154. {
  155. return GridTransform;
  156. }
  157. void GridEffectClass::Set_Texture(TextureClass * tex)
  158. {
  159. MaterialPass->Set_Texture(tex);
  160. }
  161. TextureClass * GridEffectClass::Peek_Texture(void)
  162. {
  163. return MaterialPass->Peek_Texture();
  164. }