prim_anim.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/prim_anim.h $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/29/01 5:43p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __PRIM_ANIM_H
  39. #define __PRIM_ANIM_H
  40. #include "simplevec.h"
  41. #include "chunkio.h"
  42. // Forward declarations
  43. class ChunkSaveClass;
  44. class ChunkLoadClass;
  45. /////////////////////////////////////////////////////////////////////
  46. //
  47. // PrimitiveAnimationChannelClass
  48. //
  49. // This template class provides animated 'channels' of data for the
  50. // RingRenderObjClass and SphereRenderObjClass objects.
  51. //
  52. /////////////////////////////////////////////////////////////////////
  53. template<class T>
  54. class PrimitiveAnimationChannelClass
  55. {
  56. public:
  57. /////////////////////////////////////////////////////////
  58. // Public constructors/destructors
  59. /////////////////////////////////////////////////////////
  60. PrimitiveAnimationChannelClass (void)
  61. : m_LastIndex (0) { }
  62. virtual ~PrimitiveAnimationChannelClass (void) { Reset (); }
  63. /////////////////////////////////////////////////////////
  64. // Public data types
  65. /////////////////////////////////////////////////////////
  66. class KeyClass
  67. {
  68. public:
  69. KeyClass (void)
  70. : m_Time (0) {}
  71. KeyClass (const T &value, float time)
  72. : m_Value (value),
  73. m_Time (time) {}
  74. float Get_Time (void) const { return m_Time; }
  75. const T & Get_Value (void) const { return m_Value; }
  76. T & Get_Value (void) { return m_Value; }
  77. float Set_Time (float time) { m_Time = time; }
  78. void Set_Value (const T &value) { m_Value = value; }
  79. private:
  80. T m_Value;
  81. float m_Time;
  82. };
  83. /////////////////////////////////////////////////////////
  84. // Public operators
  85. /////////////////////////////////////////////////////////
  86. const PrimitiveAnimationChannelClass<T> &operator= (const PrimitiveAnimationChannelClass<T> &src);
  87. const KeyClass & operator[] (int index) { return Get_Key (index); }
  88. /////////////////////////////////////////////////////////
  89. // Public methods
  90. /////////////////////////////////////////////////////////
  91. virtual T Evaluate (float time) = 0;
  92. int Get_Key_Count (void) const;
  93. const KeyClass & Get_Key (int index) const;
  94. void Set_Key (int index, const T &value, float time);
  95. void Set_Key_Value (int index, const T &value);
  96. void Add_Key (const T &value, float time);
  97. void Insert_Key (int index, const T &value, float time);
  98. void Delete_Key (int index);
  99. void Reset (void);
  100. virtual void Save (ChunkSaveClass &csave);
  101. virtual void Load (ChunkLoadClass &cload);
  102. protected:
  103. /////////////////////////////////////////////////////////
  104. // Protected constants
  105. /////////////////////////////////////////////////////////
  106. enum
  107. {
  108. CHUNKID_VARIABLES = 0x03150809,
  109. };
  110. enum
  111. {
  112. VARID_KEY = 1,
  113. };
  114. /////////////////////////////////////////////////////////
  115. // Protected methods
  116. /////////////////////////////////////////////////////////
  117. void Load_Variables (ChunkLoadClass &cload);
  118. protected:
  119. /////////////////////////////////////////////////////////
  120. // Protected member data
  121. /////////////////////////////////////////////////////////
  122. SimpleDynVecClass< KeyClass > m_Data;
  123. int m_LastIndex;
  124. };
  125. /////////////////////////////////////////////////////////////////////
  126. //
  127. // LERPAnimationChannelClass
  128. //
  129. // This template class provides a simple LERP implementation of the
  130. // Evaluate () method.
  131. //
  132. /////////////////////////////////////////////////////////////////////
  133. template<class T>
  134. class LERPAnimationChannelClass : public PrimitiveAnimationChannelClass<T>
  135. {
  136. public:
  137. /////////////////////////////////////////////////////////
  138. // Public methods
  139. /////////////////////////////////////////////////////////
  140. virtual T Evaluate (float time);
  141. };
  142. /////////////////////////////////////////////////////////
  143. // Set_Key
  144. /////////////////////////////////////////////////////////
  145. template<class T>
  146. int PrimitiveAnimationChannelClass<T>::Get_Key_Count (void) const
  147. {
  148. return m_Data.Count ();
  149. }
  150. /////////////////////////////////////////////////////////
  151. // Set_Key_Value
  152. /////////////////////////////////////////////////////////
  153. template<class T>
  154. const PrimitiveAnimationChannelClass<T>::KeyClass &PrimitiveAnimationChannelClass<T>::Get_Key (int index) const
  155. {
  156. return m_Data[index];
  157. }
  158. /////////////////////////////////////////////////////////
  159. // Set_Key
  160. /////////////////////////////////////////////////////////
  161. template<class T>
  162. void PrimitiveAnimationChannelClass<T>::Set_Key (int index, const T &value, float time)
  163. {
  164. m_Data[index].Set_Value (value);
  165. m_Data[index].Set_Time (time);
  166. return ;
  167. }
  168. /////////////////////////////////////////////////////////
  169. // Set_Key_Value
  170. /////////////////////////////////////////////////////////
  171. template<class T>
  172. void PrimitiveAnimationChannelClass<T>::Set_Key_Value (int index, const T &value)
  173. {
  174. m_Data[index].Set_Value (value);
  175. return ;
  176. }
  177. /////////////////////////////////////////////////////////
  178. // Add_Key
  179. /////////////////////////////////////////////////////////
  180. template<class T>
  181. void PrimitiveAnimationChannelClass<T>::Add_Key (const T &value, float time)
  182. {
  183. m_Data.Add (KeyClass (value, time));
  184. return ;
  185. }
  186. /////////////////////////////////////////////////////////
  187. // Insert_Key
  188. /////////////////////////////////////////////////////////
  189. template<class T>
  190. void PrimitiveAnimationChannelClass<T>::Insert_Key (int index, const T &value, float time)
  191. {
  192. m_Data.Insert (index, KeyClass (value, time));
  193. return ;
  194. }
  195. /////////////////////////////////////////////////////////
  196. // Delete_Key
  197. /////////////////////////////////////////////////////////
  198. template<class T>
  199. void PrimitiveAnimationChannelClass<T>::Delete_Key (int index)
  200. {
  201. m_Data.Delete (index);
  202. return ;
  203. }
  204. /////////////////////////////////////////////////////////
  205. // Reset
  206. /////////////////////////////////////////////////////////
  207. template<class T>
  208. void PrimitiveAnimationChannelClass<T>::Reset (void)
  209. {
  210. m_Data.Delete_All ();
  211. m_LastIndex = 0;
  212. return ;
  213. }
  214. /////////////////////////////////////////////////////////////////////
  215. // operator=
  216. /////////////////////////////////////////////////////////////////////
  217. template<class T> const PrimitiveAnimationChannelClass<T> &
  218. PrimitiveAnimationChannelClass<T>::operator= (const PrimitiveAnimationChannelClass<T> &src)
  219. {
  220. Reset ();
  221. //
  222. // Copy the data array
  223. //
  224. for (int index = 0; index < src.Get_Key_Count (); index ++) {
  225. m_Data.Add (src.Get_Key (index));
  226. }
  227. m_LastIndex = src.m_LastIndex;
  228. return *this;
  229. }
  230. /////////////////////////////////////////////////////////////////////
  231. // Save
  232. /////////////////////////////////////////////////////////////////////
  233. template<class T> void
  234. PrimitiveAnimationChannelClass<T>::Save (ChunkSaveClass &csave)
  235. {
  236. csave.Begin_Chunk (CHUNKID_VARIABLES);
  237. //
  238. // Save each key
  239. //
  240. for (int index = 0; index < m_Data.Count (); index ++) {
  241. KeyClass &value = m_Data[index];
  242. WRITE_MICRO_CHUNK (csave, VARID_KEY, value);
  243. }
  244. csave.End_Chunk ();
  245. return ;
  246. }
  247. /////////////////////////////////////////////////////////////////////
  248. // Load
  249. /////////////////////////////////////////////////////////////////////
  250. template<class T> void
  251. PrimitiveAnimationChannelClass<T>::Load (ChunkLoadClass &cload)
  252. {
  253. Reset ();
  254. while (cload.Open_Chunk ()) {
  255. switch (cload.Cur_Chunk_ID ()) {
  256. case CHUNKID_VARIABLES:
  257. Load_Variables (cload);
  258. break;
  259. }
  260. cload.Close_Chunk ();
  261. }
  262. return ;
  263. }
  264. /////////////////////////////////////////////////////////////////////
  265. // Load_Variables
  266. /////////////////////////////////////////////////////////////////////
  267. template<class T> void
  268. PrimitiveAnimationChannelClass<T>::Load_Variables (ChunkLoadClass &cload)
  269. {
  270. //
  271. // Loop through all the microchunks that define the variables
  272. //
  273. while (cload.Open_Micro_Chunk ()) {
  274. switch (cload.Cur_Micro_Chunk_ID ()) {
  275. case VARID_KEY:
  276. {
  277. KeyClass value;
  278. cload.Read (&value, sizeof (value));
  279. m_Data.Add (value);
  280. }
  281. break;
  282. }
  283. cload.Close_Micro_Chunk ();
  284. }
  285. return ;
  286. }
  287. /////////////////////////////////////////////////////////////////////
  288. // Evaluate
  289. /////////////////////////////////////////////////////////////////////
  290. template<class T> T
  291. LERPAnimationChannelClass<T>::Evaluate (float time)
  292. {
  293. int key_count = m_Data.Count ();
  294. T value = m_Data[key_count - 1].Get_Value ();
  295. //
  296. // Don't interpolate past the last keyframe
  297. //
  298. if (time < m_Data[key_count - 1].Get_Time ()) {
  299. // Check to see if the last key index is valid
  300. if (time < m_Data[m_LastIndex].Get_Time ()) {
  301. m_LastIndex = 0;
  302. }
  303. KeyClass *key1 = &m_Data[m_LastIndex];
  304. KeyClass *key2 = &m_Data[key_count - 1];
  305. //
  306. // Search, using last_key as our starting point
  307. //
  308. for (int keyidx = m_LastIndex; keyidx < (key_count - 1); keyidx ++) {
  309. if (time < m_Data[keyidx+1].Get_Time ()) {
  310. key1 = &m_Data[keyidx];
  311. key2 = &m_Data[keyidx+1];
  312. m_LastIndex = keyidx;
  313. break;
  314. }
  315. }
  316. // Calculate the linear percent between the two keys
  317. float percent = (time - key1->Get_Time ()) / (key2->Get_Time () - key1->Get_Time ());
  318. // Interpolate the value
  319. value = (key1->Get_Value () + (key2->Get_Value () - key1->Get_Value ()) * percent);
  320. }
  321. return value;
  322. }
  323. #endif //__PRIM_ANIM_H