W3DRopeDraw.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DRopeDraw.cpp ////////////////////////////////////////////////////////////////////////
  24. // Author: Steven Johnson, Aug 2002
  25. // Desc: Rope drawing
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include <stdlib.h>
  29. #include <windows.h>
  30. #include "Common/Thing.h"
  31. #include "Common/ThingTemplate.h"
  32. #include "Common/Xfer.h"
  33. #include "GameClient/ClientRandomValue.h"
  34. #include "GameClient/Color.h"
  35. #include "GameClient/Drawable.h"
  36. #include "GameClient/GameClient.h"
  37. #include "GameLogic/GameLogic.h"
  38. #include "W3DDevice/GameClient/W3DDisplay.h"
  39. #include "W3DDevice/GameClient/Module/W3DRopeDraw.h"
  40. #include "WW3D2/Line3D.h"
  41. #include "W3DDevice/GameClient/W3DScene.h"
  42. #include "Common/GameState.h"
  43. #ifdef _INTERNAL
  44. // for occasional debugging...
  45. //#pragma optimize("", off)
  46. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  47. #endif
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////
  49. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////
  51. //-------------------------------------------------------------------------------------------------
  52. //-------------------------------------------------------------------------------------------------
  53. W3DRopeDraw::W3DRopeDraw( Thing *thing, const ModuleData* moduleData ) : DrawModule( thing, moduleData )
  54. {
  55. m_curLen = 0.0f;
  56. m_maxLen = 1.0f;
  57. m_width = 0.5f;
  58. m_color.red = 0.0f;
  59. m_color.green = 0.0f;
  60. m_color.blue = 0.0f;
  61. m_curSpeed = 0.0f;
  62. m_maxSpeed = 0.0f;
  63. m_accel = 0.0f;
  64. m_wobbleLen = m_maxLen; // huge
  65. m_wobbleAmp = 0.0f;
  66. m_segments.clear();
  67. m_curWobblePhase = 0.0f;
  68. m_curZOffset = 0.0f;
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. //-------------------------------------------------------------------------------------------------
  72. void W3DRopeDraw::buildSegments()
  73. {
  74. DEBUG_ASSERTCRASH(m_segments.empty(), ("Hmmn, not empty"));
  75. m_segments.clear();
  76. Int numSegs = ceil(m_maxLen / m_wobbleLen);
  77. Real eachLen = m_maxLen / (Real)numSegs;
  78. Coord3D pos = *getDrawable()->getPosition();
  79. for (int i = 0; i < numSegs; ++i, pos.z += eachLen)
  80. {
  81. SegInfo info;
  82. Real axis = GameClientRandomValueReal(0, 2*PI);
  83. info.wobbleAxisX = Cos(axis);
  84. info.wobbleAxisY = Sin(axis);
  85. info.line = NEW Line3DClass( Vector3(pos.x,pos.y,pos.z),
  86. Vector3(pos.x,pos.y,pos.z+eachLen),
  87. m_width * 0.5f, // width
  88. m_color.red, // red
  89. m_color.green, // green
  90. m_color.blue, // blue
  91. 1.0f ); // transparency
  92. info.softLine = NEW Line3DClass( Vector3(pos.x,pos.y,pos.z),
  93. Vector3(pos.x,pos.y,pos.z+eachLen),
  94. m_width, // width
  95. m_color.red, // red
  96. m_color.green, // green
  97. m_color.blue, // blue
  98. 0.5f ); // transparency
  99. W3DDisplay::m_3DScene->Add_Render_Object( info.line );
  100. W3DDisplay::m_3DScene->Add_Render_Object( info.softLine );
  101. m_segments.push_back(info);
  102. }
  103. }
  104. //-------------------------------------------------------------------------------------------------
  105. //-------------------------------------------------------------------------------------------------
  106. void W3DRopeDraw::tossSegments()
  107. {
  108. // remove tracer from the scene and delete
  109. for (std::vector<SegInfo>::iterator it = m_segments.begin(); it != m_segments.end(); ++it)
  110. {
  111. if (it->line)
  112. {
  113. W3DDisplay::m_3DScene->Remove_Render_Object(it->line);
  114. REF_PTR_RELEASE((it->line));
  115. }
  116. if (it->softLine)
  117. {
  118. W3DDisplay::m_3DScene->Remove_Render_Object(it->softLine);
  119. REF_PTR_RELEASE((it->softLine));
  120. }
  121. }
  122. m_segments.clear();
  123. }
  124. //-------------------------------------------------------------------------------------------------
  125. //-------------------------------------------------------------------------------------------------
  126. void W3DRopeDraw::initRopeParms(Real length, Real width, const RGBColor& color, Real wobbleLen, Real wobbleAmp, Real wobbleRate)
  127. {
  128. m_maxLen = max(1.0f, length);
  129. m_curLen = 0.0f;
  130. m_width = width;
  131. m_color = color;
  132. m_wobbleLen = min(m_maxLen, wobbleLen);
  133. m_wobbleAmp = wobbleAmp;
  134. m_wobbleRate = wobbleRate;
  135. m_curZOffset = 0.0f;
  136. tossSegments();
  137. buildSegments();
  138. }
  139. //-------------------------------------------------------------------------------------------------
  140. //-------------------------------------------------------------------------------------------------
  141. void W3DRopeDraw::setRopeCurLen(Real length)
  142. {
  143. m_curLen = length;
  144. }
  145. //-------------------------------------------------------------------------------------------------
  146. //-------------------------------------------------------------------------------------------------
  147. void W3DRopeDraw::setRopeSpeed(Real curSpeed, Real maxSpeed, Real accel)
  148. {
  149. m_curSpeed = curSpeed;
  150. m_maxSpeed = maxSpeed;
  151. m_accel = accel;
  152. }
  153. //-------------------------------------------------------------------------------------------------
  154. //-------------------------------------------------------------------------------------------------
  155. W3DRopeDraw::~W3DRopeDraw()
  156. {
  157. tossSegments();
  158. }
  159. //-------------------------------------------------------------------------------------------------
  160. //-------------------------------------------------------------------------------------------------
  161. void W3DRopeDraw::doDrawModule(const Matrix3D* transformMtx)
  162. {
  163. if (m_segments.empty())
  164. {
  165. buildSegments();
  166. }
  167. if (!m_segments.empty())
  168. {
  169. Real deflection = Sin(m_curWobblePhase) * m_wobbleAmp;
  170. const Coord3D* pos = getDrawable()->getPosition();
  171. Vector3 start(pos->x, pos->y, pos->z + m_curZOffset);
  172. Real eachLen = m_curLen / m_segments.size();
  173. for (std::vector<SegInfo>::iterator it = m_segments.begin(); it != m_segments.end(); ++it)
  174. {
  175. Vector3 end(pos->x + deflection*it->wobbleAxisX, pos->y + deflection*it->wobbleAxisY, start.Z - eachLen);
  176. if (it->line)
  177. (it->line)->Reset(start, end);
  178. if (it->softLine)
  179. (it->softLine)->Reset(start, end);
  180. start = end;
  181. }
  182. }
  183. m_curWobblePhase += m_wobbleRate;
  184. if (m_curWobblePhase > 2*PI)
  185. m_curWobblePhase -= 2*PI;
  186. m_curZOffset += m_curSpeed;
  187. m_curSpeed += m_accel;
  188. if (m_curSpeed > m_maxSpeed)
  189. m_curSpeed = m_maxSpeed;
  190. else if (m_curSpeed < -m_maxSpeed)
  191. m_curSpeed = -m_maxSpeed;
  192. }
  193. // ------------------------------------------------------------------------------------------------
  194. /** CRC */
  195. // ------------------------------------------------------------------------------------------------
  196. void W3DRopeDraw::crc( Xfer *xfer )
  197. {
  198. // extend base class
  199. DrawModule::crc( xfer );
  200. } // end crc
  201. // ------------------------------------------------------------------------------------------------
  202. /** Xfer method
  203. * Version Info:
  204. * 1: Initial version */
  205. // ------------------------------------------------------------------------------------------------
  206. void W3DRopeDraw::xfer( Xfer *xfer )
  207. {
  208. // version
  209. const XferVersion currentVersion = 1;
  210. XferVersion version = currentVersion;
  211. xfer->xferVersion( &version, currentVersion );
  212. // extend base class
  213. DrawModule::xfer( xfer );
  214. // m_segments is not saved
  215. // cur len
  216. xfer->xferReal( &m_curLen );
  217. // max len
  218. xfer->xferReal( &m_maxLen );
  219. // width
  220. xfer->xferReal( &m_width );
  221. // color
  222. xfer->xferRGBColor( &m_color );
  223. // cur speed
  224. xfer->xferReal( &m_curSpeed );
  225. // max speed
  226. xfer->xferReal( &m_maxSpeed );
  227. // acceleration
  228. xfer->xferReal( &m_accel );
  229. // wobble len
  230. xfer->xferReal( &m_wobbleLen );
  231. // wobble amp
  232. xfer->xferReal( &m_wobbleAmp );
  233. // wobble rate
  234. xfer->xferReal( &m_wobbleRate );
  235. // current wobble phase
  236. xfer->xferReal( &m_curWobblePhase );
  237. // cur Z offset
  238. xfer->xferReal( &m_curZOffset );
  239. if (xfer->getXferMode() == XFER_LOAD)
  240. tossSegments();
  241. } // end xfer
  242. // ------------------------------------------------------------------------------------------------
  243. /** Load post process */
  244. // ------------------------------------------------------------------------------------------------
  245. void W3DRopeDraw::loadPostProcess( void )
  246. {
  247. // extend base class
  248. DrawModule::loadPostProcess();
  249. } // end loadPostProcess