animobj.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/animobj.cpp $*
  25. * *
  26. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 12/13/01 6:56p $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Animatable3DObjClass::Animatable3DObjClass -- constructor *
  35. * Animatable3DObjClass::Animatable3DObjClass -- copy constructor *
  36. * Animatable3DObjClass::~Animatable3DObjClass -- destructor *
  37. * Animatable3DObjClass::operator = -- assignment operator *
  38. * Animatable3DObjClass::Release -- Releases any anims being held by this object *
  39. * Animatable3DObjClass::Render -- Update this object for rendering *
  40. * Animatable3DObjClass::Special_Render -- "special render" function for animatables *
  41. * Animatable3DObjClass::Set_Transform -- sets the transform and marks sub-objects as dirty *
  42. * Animatable3DObjClass::Set_Position -- Sets the position and marks sub-objects as dirty *
  43. * Animatable3DObjClass::Get_Num_Bones -- returns number of bones in this object *
  44. * Animatable3DObjClass::Get_Bone_Name -- returns the name of the given bone *
  45. * Animatable3DObjClass::Get_Bone_Index -- returns the index of the given bone *
  46. * Animatable3DObjClass::Set_Animation -- set the animation state to "none" (base pose) *
  47. * Animatable3DObjClass::Set_Animation -- Set the animation state to the given anim/frame *
  48. * Animatable3DObjClass::Set_Animation -- set the animation state to a blend of two anims *
  49. * Animatable3DObjClass::Set_Animation -- Set animation state with an anim combo *
  50. * Animatable3DObjClass::Get_Bone_Transform -- return the transform for the given bone *
  51. * Animatable3DObjClass::Get_Bone_Transform -- return the transform for the given bone *
  52. * Animatable3DObjClass::Capture_Bone -- capture the specified bone (override animation) *
  53. * Animatable3DObjClass::Release_Bone -- release the specified bone (allow animation) *
  54. * Animatable3DObjClass::Is_Bone_Captured -- returns whether the specified bone is captured *
  55. * Animatable3DObjClass::Control_Bone -- sets the transform for the bone *
  56. * Animatable3DObjClass::Update_Sub_Object_Transforms -- recalculate the transforms for our *
  57. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  58. #include "animobj.h"
  59. #include "htree.h"
  60. #include "assetmgr.h"
  61. #include "hanim.h"
  62. #include "hcanim.h"
  63. #include "ww3d.h"
  64. #include "wwmemlog.h"
  65. #include "animatedsoundmgr.h"
  66. /***********************************************************************************************
  67. * Animatable3DObjClass::Animatable3DObjClass -- constructor *
  68. * *
  69. * INPUT: *
  70. * htree_name -- name of the hierarchy tree which defines the "bone" structure for this object *
  71. * *
  72. * OUTPUT: *
  73. * *
  74. * WARNINGS: *
  75. * *
  76. * HISTORY: *
  77. * 12/8/98 GTH : Created. *
  78. *=============================================================================================*/
  79. Animatable3DObjClass::Animatable3DObjClass(const char * htree_name) :
  80. IsTreeValid(0),
  81. CurMotionMode(BASE_POSE)
  82. {
  83. // Inline struct members can't be initialized in init list for some reason...
  84. ModeAnim.Motion=NULL;
  85. ModeAnim.Frame=0.0f;
  86. ModeAnim.PrevFrame=0.0f;
  87. ModeAnim.LastSyncTime=WW3D::Get_Sync_Time();
  88. ModeAnim.frameRateMultiplier=1.0; // 020607 srj -- added
  89. ModeAnim.animDirection=1.0; // 020607 srj -- added
  90. ModeInterp.Motion0=NULL;
  91. ModeInterp.Motion1=NULL;
  92. ModeInterp.Frame0=0.0f;
  93. ModeInterp.PrevFrame0=0.0f;
  94. ModeInterp.PrevFrame1=0.0f;
  95. ModeInterp.Frame1=0.0f;
  96. ModeInterp.Percentage=0.0f;
  97. ModeCombo.AnimCombo=NULL;
  98. /*
  99. ** Store a pointer to the htree
  100. */
  101. if (htree_name == NULL) {
  102. HTree = NULL;
  103. } else if (htree_name[0] == 0) {
  104. HTree = W3DNEW HTreeClass;
  105. HTree->Init_Default ();
  106. } else {
  107. HTreeClass * source = WW3DAssetManager::Get_Instance()->Get_HTree(htree_name);
  108. if (source != NULL) {
  109. HTree = W3DNEW HTreeClass(*source);
  110. } else {
  111. WWDEBUG_SAY(("Unable to find HTree: %s\r\n",htree_name));
  112. HTree = W3DNEW HTreeClass;
  113. HTree->Init_Default();
  114. }
  115. }
  116. }
  117. /***********************************************************************************************
  118. * Animatable3DObjClass::Animatable3DObjClass -- copy constructor *
  119. * *
  120. * INPUT: *
  121. * src -- animatable object to copy. *
  122. * *
  123. * OUTPUT: *
  124. * *
  125. * WARNINGS: *
  126. * *
  127. * HISTORY: *
  128. * 12/8/98 GTH : Created. *
  129. *=============================================================================================*/
  130. Animatable3DObjClass::Animatable3DObjClass(const Animatable3DObjClass & src) :
  131. CompositeRenderObjClass(src),
  132. IsTreeValid(0),
  133. CurMotionMode(BASE_POSE),
  134. HTree(NULL)
  135. {
  136. // Inline struct members can't be initialized in init list for some reason...
  137. ModeAnim.Motion=NULL;
  138. ModeAnim.Frame=0.0f;
  139. ModeAnim.PrevFrame=0.0f;
  140. ModeAnim.LastSyncTime=WW3D::Get_Sync_Time();
  141. ModeAnim.frameRateMultiplier=1.0; // 020607 srj -- added
  142. ModeAnim.animDirection=1.0; // 020607 srj -- added
  143. ModeInterp.Motion0=NULL;
  144. ModeInterp.Motion1=NULL;
  145. ModeInterp.Frame0=0.0f;
  146. ModeInterp.PrevFrame0=0.0f;
  147. ModeInterp.PrevFrame1=0.0f;
  148. ModeInterp.Frame1=0.0f;
  149. ModeInterp.Percentage=0.0f;
  150. ModeCombo.AnimCombo=NULL;
  151. *this = src;
  152. }
  153. /***********************************************************************************************
  154. * Animatable3DObjClass::~Animatable3DObjClass -- destructor *
  155. * *
  156. * INPUT: *
  157. * *
  158. * OUTPUT: *
  159. * *
  160. * WARNINGS: *
  161. * *
  162. * HISTORY: *
  163. * 12/8/98 GTH : Created. *
  164. *=============================================================================================*/
  165. Animatable3DObjClass::~Animatable3DObjClass(void)
  166. {
  167. Release();
  168. if (HTree) {
  169. delete HTree;
  170. }
  171. }
  172. /***********************************************************************************************
  173. * Animatable3DObjClass::operator = -- assignment operator *
  174. * *
  175. * INPUT: *
  176. * *
  177. * OUTPUT: *
  178. * *
  179. * WARNINGS: *
  180. * *
  181. * HISTORY: *
  182. * 3/2/99 GTH : Created. *
  183. *=============================================================================================*/
  184. Animatable3DObjClass & Animatable3DObjClass::operator = (const Animatable3DObjClass & that)
  185. {
  186. if (&that != this) {
  187. Release();
  188. if (HTree) {
  189. delete HTree;
  190. }
  191. CompositeRenderObjClass::operator = (that);
  192. IsTreeValid = 0;
  193. CurMotionMode = BASE_POSE;
  194. ModeAnim.Motion = NULL;
  195. ModeAnim.Frame = 0.0f;
  196. ModeAnim.PrevFrame = 0.0f;
  197. ModeAnim.LastSyncTime = WW3D::Get_Sync_Time();
  198. ModeAnim.frameRateMultiplier=1.0; // 020607 srj -- added
  199. ModeAnim.animDirection=1.0; // 020607 srj -- added
  200. ModeInterp.Motion0 = NULL;
  201. ModeInterp.Motion1 = NULL;
  202. ModeInterp.Frame0 = 0.0f;
  203. ModeInterp.PrevFrame0 = 0.0f;
  204. ModeInterp.PrevFrame1 = 0.0f;
  205. ModeInterp.Frame1 = 0.0f;
  206. ModeInterp.Percentage = 0.0f;
  207. ModeCombo.AnimCombo = NULL;
  208. HTree = W3DNEW HTreeClass(*that.HTree);
  209. }
  210. return *this;
  211. }
  212. /***********************************************************************************************
  213. * Animatable3DObjClass::Release -- Releases any anims being held by this object *
  214. * *
  215. * INPUT: *
  216. * *
  217. * OUTPUT: *
  218. * *
  219. * WARNINGS: *
  220. * *
  221. * HISTORY: *
  222. * 12/8/98 GTH : Created. *
  223. *=============================================================================================*/
  224. void Animatable3DObjClass::Release( void )
  225. {
  226. switch (CurMotionMode) {
  227. case BASE_POSE:
  228. break;
  229. case SINGLE_ANIM:
  230. if ( ModeAnim.Motion != NULL ) {
  231. ModeAnim.Motion->Release_Ref();
  232. ModeAnim.Motion = NULL;
  233. }
  234. break;
  235. case DOUBLE_ANIM:
  236. if ( ModeInterp.Motion0 != NULL ) {
  237. ModeInterp.Motion0->Release_Ref();
  238. ModeInterp.Motion0 = NULL;
  239. }
  240. if ( ModeInterp.Motion1 != NULL ) {
  241. ModeInterp.Motion1->Release_Ref();
  242. ModeInterp.Motion1 = NULL;
  243. }
  244. break;
  245. case MULTIPLE_ANIM:
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. /***********************************************************************************************
  252. * Animatable3DObjClass::Render -- Update this object for rendering *
  253. * *
  254. * INPUT: *
  255. * *
  256. * OUTPUT: *
  257. * *
  258. * WARNINGS: *
  259. * *
  260. * HISTORY: *
  261. * 12/8/98 GTH : Created. *
  262. *=============================================================================================*/
  263. void Animatable3DObjClass::Render(RenderInfoClass & rinfo)
  264. {
  265. if (HTree == NULL) return;
  266. if (Is_Not_Hidden_At_All() == false) {
  267. return;
  268. }
  269. if ( CurMotionMode == SINGLE_ANIM ) {
  270. if ( ModeAnim.AnimMode != ANIM_MODE_MANUAL ) {
  271. Single_Anim_Progress();
  272. }
  273. }
  274. if (!Is_Hierarchy_Valid() || Are_Sub_Object_Transforms_Dirty()) {
  275. Update_Sub_Object_Transforms();
  276. }
  277. }
  278. /***********************************************************************************************
  279. * Animatable3DObjClass::Special_Render -- "special render" function for animatables *
  280. * *
  281. * INPUT: *
  282. * *
  283. * OUTPUT: *
  284. * *
  285. * WARNINGS: *
  286. * *
  287. * HISTORY: *
  288. * 12/10/98 GTH : Created. *
  289. *=============================================================================================*/
  290. void Animatable3DObjClass::Special_Render(SpecialRenderInfoClass & rinfo)
  291. {
  292. if (HTree == NULL) return;
  293. if ( CurMotionMode == SINGLE_ANIM ) {
  294. if ( ModeAnim.AnimMode != ANIM_MODE_MANUAL ) {
  295. Single_Anim_Progress();
  296. }
  297. }
  298. if (!Is_Hierarchy_Valid()) {
  299. Update_Sub_Object_Transforms();
  300. }
  301. }
  302. /***********************************************************************************************
  303. * Animatable3DObjClass::Set_Transform -- sets the transform and marks sub-objects as dirty *
  304. * *
  305. * INPUT: *
  306. * *
  307. * OUTPUT: *
  308. * *
  309. * WARNINGS: *
  310. * *
  311. * HISTORY: *
  312. * 3/2/99 GTH : Created. *
  313. *=============================================================================================*/
  314. void Animatable3DObjClass::Set_Transform(const Matrix3D &m)
  315. {
  316. CompositeRenderObjClass::Set_Transform(m);
  317. Set_Hierarchy_Valid(false);
  318. }
  319. /***********************************************************************************************
  320. * Animatable3DObjClass::Set_Position -- Sets the position and marks sub-objects as dirty *
  321. * *
  322. * INPUT: *
  323. * *
  324. * OUTPUT: *
  325. * *
  326. * WARNINGS: *
  327. * *
  328. * HISTORY: *
  329. * 3/2/99 GTH : Created. *
  330. *=============================================================================================*/
  331. void Animatable3DObjClass::Set_Position(const Vector3 &v)
  332. {
  333. CompositeRenderObjClass::Set_Position(v);
  334. Set_Hierarchy_Valid(false);
  335. }
  336. /***********************************************************************************************
  337. * Animatable3DObjClass::Get_Num_Bones -- returns number of bones in this object *
  338. * *
  339. * INPUT: *
  340. * *
  341. * OUTPUT: *
  342. * *
  343. * WARNINGS: *
  344. * *
  345. * HISTORY: *
  346. * 3/2/99 GTH : Created. *
  347. *=============================================================================================*/
  348. int Animatable3DObjClass::Get_Num_Bones(void)
  349. {
  350. if (HTree) {
  351. return HTree->Num_Pivots();
  352. } else {
  353. return 1;
  354. }
  355. }
  356. /***********************************************************************************************
  357. * Animatable3DObjClass::Get_Bone_Name -- returns the name of the given bone *
  358. * *
  359. * INPUT: *
  360. * *
  361. * OUTPUT: *
  362. * *
  363. * WARNINGS: *
  364. * *
  365. * HISTORY: *
  366. * 3/2/99 GTH : Created. *
  367. *=============================================================================================*/
  368. const char * Animatable3DObjClass::Get_Bone_Name(int bone_index)
  369. {
  370. if (HTree) {
  371. return HTree->Get_Bone_Name(bone_index);
  372. } else {
  373. return "RootTransform";
  374. }
  375. }
  376. /***********************************************************************************************
  377. * Animatable3DObjClass::Get_Bone_Index -- returns the index of the given bone *
  378. * *
  379. * INPUT: *
  380. * *
  381. * OUTPUT: *
  382. * *
  383. * WARNINGS: *
  384. * *
  385. * HISTORY: *
  386. * 3/2/99 GTH : Created. *
  387. *=============================================================================================*/
  388. int Animatable3DObjClass::Get_Bone_Index(const char * bonename)
  389. {
  390. if (HTree) {
  391. return HTree->Get_Bone_Index(bonename);
  392. } else {
  393. return 0;
  394. }
  395. }
  396. /***********************************************************************************************
  397. * Animatable3DObjClass::Set_Animation -- set the animation state to "none" (base pose) *
  398. * *
  399. * INPUT: *
  400. * *
  401. * OUTPUT: *
  402. * *
  403. * WARNINGS: *
  404. * *
  405. * HISTORY: *
  406. * 12/8/98 GTH : Created. *
  407. *=============================================================================================*/
  408. void Animatable3DObjClass::Set_Animation(void)
  409. {
  410. Release();
  411. CurMotionMode = BASE_POSE;
  412. Set_Hierarchy_Valid(false);
  413. }
  414. /***********************************************************************************************
  415. * Animatable3DObjClass::Set_Animation -- Set the animation state to the given anim/frame *
  416. * *
  417. * INPUT: *
  418. * *
  419. * OUTPUT: *
  420. * *
  421. * WARNINGS: *
  422. * *
  423. * HISTORY: *
  424. * 12/8/98 GTH : Created. *
  425. *=============================================================================================*/
  426. void Animatable3DObjClass::Set_Animation(HAnimClass * motion, float frame, int mode)
  427. {
  428. if ( motion ) {
  429. // Add_Ref before we remove, in case it is the same one.
  430. motion->Add_Ref();
  431. Release();
  432. CurMotionMode = SINGLE_ANIM;
  433. ModeAnim.Motion = motion;
  434. ModeAnim.PrevFrame = ModeAnim.Frame;
  435. ModeAnim.Frame = frame;
  436. ModeAnim.LastSyncTime = WW3D::Get_Sync_Time();
  437. ModeAnim.frameRateMultiplier=1.0; // 020607 srj -- added
  438. ModeAnim.animDirection=1.0; // 020607 srj -- added
  439. ModeAnim.AnimMode = mode;
  440. if (mode < ANIM_MODE_LOOP_BACKWARDS)
  441. ModeAnim.animDirection = 1.0f; //assume playing forwards
  442. else
  443. ModeAnim.animDirection = -1.0f; //reverse animation playback
  444. const char* sound_name = AnimatedSoundMgrClass::Get_Embedded_Sound_Name(motion);
  445. if (sound_name) {
  446. int bone_index = Get_Bone_Index(sound_name);
  447. motion->Set_Embedded_Sound_Bone_Index(bone_index);
  448. }
  449. } else {
  450. CurMotionMode = BASE_POSE;
  451. Release();
  452. }
  453. Set_Hierarchy_Valid(false);
  454. }
  455. /***********************************************************************************************
  456. * Animatable3DObjClass::Set_Animation -- set the animation state to a blend of two anims *
  457. * *
  458. * INPUT: *
  459. * *
  460. * OUTPUT: *
  461. * *
  462. * WARNINGS: *
  463. * *
  464. * HISTORY: *
  465. * 12/8/98 GTH : Created. *
  466. *=============================================================================================*/
  467. void Animatable3DObjClass::Set_Animation
  468. (
  469. HAnimClass * motion0,
  470. float frame0,
  471. HAnimClass * motion1,
  472. float frame1,
  473. float percentage
  474. )
  475. {
  476. Release();
  477. CurMotionMode = DOUBLE_ANIM;
  478. ModeInterp.Motion0 = motion0;
  479. ModeInterp.Motion1 = motion1;
  480. ModeInterp.PrevFrame0 = ModeInterp.Frame0;
  481. ModeInterp.PrevFrame1 = ModeInterp.Frame1;
  482. ModeInterp.Frame0 = frame0;
  483. ModeInterp.Frame1 = frame1;
  484. ModeInterp.Percentage = percentage;
  485. Set_Hierarchy_Valid(false);
  486. if ( ModeInterp.Motion0 != NULL ) {
  487. ModeInterp.Motion0->Add_Ref();
  488. const char* sound_name = AnimatedSoundMgrClass::Get_Embedded_Sound_Name(motion0);
  489. if (sound_name) {
  490. int bone_index = Get_Bone_Index(sound_name);
  491. motion0->Set_Embedded_Sound_Bone_Index(bone_index);
  492. }
  493. }
  494. if ( ModeInterp.Motion1 != NULL ) {
  495. ModeInterp.Motion1->Add_Ref();
  496. const char* sound_name = AnimatedSoundMgrClass::Get_Embedded_Sound_Name(motion1);
  497. if (sound_name) {
  498. int bone_index = Get_Bone_Index(sound_name);
  499. motion1->Set_Embedded_Sound_Bone_Index(bone_index);
  500. }
  501. }
  502. }
  503. /***********************************************************************************************
  504. * Animatable3DObjClass::Set_Animation -- Set animation state with an anim combo *
  505. * *
  506. * INPUT: *
  507. * *
  508. * OUTPUT: *
  509. * *
  510. * WARNINGS: *
  511. * *
  512. * HISTORY: *
  513. * 12/8/98 GTH : Created. *
  514. *=============================================================================================*/
  515. void Animatable3DObjClass::Set_Animation
  516. (
  517. HAnimComboClass * anim_combo
  518. )
  519. {
  520. Release();
  521. CurMotionMode = MULTIPLE_ANIM;
  522. ModeCombo.AnimCombo = anim_combo;
  523. Set_Hierarchy_Valid(false);
  524. if (anim_combo) {
  525. int count = anim_combo->Get_Num_Anims();
  526. for (int index = 0; index < count; index ++) {
  527. HAnimClass *motion = anim_combo->Peek_Motion(index);
  528. const char* sound_name = AnimatedSoundMgrClass::Get_Embedded_Sound_Name(motion);
  529. if (sound_name) {
  530. int bone_index = Get_Bone_Index(sound_name);
  531. motion->Set_Embedded_Sound_Bone_Index(bone_index);
  532. }
  533. }
  534. }
  535. }
  536. /***********************************************************************************************
  537. * Animatable3DObjClass::Peek_Animation *
  538. * *
  539. * INPUT: *
  540. * *
  541. * OUTPUT: *
  542. * *
  543. * WARNINGS: *
  544. * *
  545. * HISTORY: *
  546. * 12/8/98 GTH : Created. *
  547. *=============================================================================================*/
  548. HAnimClass * Animatable3DObjClass::Peek_Animation( void )
  549. {
  550. if ( CurMotionMode == SINGLE_ANIM ) {
  551. return ModeAnim.Motion;
  552. } else {
  553. return NULL;
  554. }
  555. }
  556. /***********************************************************************************************
  557. * Animatable3DObjClass::Get_Bone_Transform -- return the transform for the given bone *
  558. * *
  559. * INPUT: *
  560. * *
  561. * OUTPUT: *
  562. * *
  563. * WARNINGS: *
  564. * *
  565. * HISTORY: *
  566. * 12/8/98 GTH : Created. *
  567. *=============================================================================================*/
  568. const Matrix3D & Animatable3DObjClass::Get_Bone_Transform(const char * bonename)
  569. {
  570. if (HTree) {
  571. WWASSERT(HTree);
  572. WWASSERT(bonename);
  573. int idx = HTree->Get_Bone_Index(bonename);
  574. return Get_Bone_Transform(idx);
  575. } else {
  576. return Get_Transform();
  577. }
  578. }
  579. /***********************************************************************************************
  580. * Animatable3DObjClass::Get_Bone_Transform -- return the transform for the given bone *
  581. * *
  582. * INPUT: *
  583. * *
  584. * OUTPUT: *
  585. * *
  586. * WARNINGS: *
  587. * *
  588. * HISTORY: *
  589. * 12/8/98 GTH : Created. *
  590. *=============================================================================================*/
  591. const Matrix3D & Animatable3DObjClass::Get_Bone_Transform(int boneindex)
  592. {
  593. Validate_Transform();
  594. if (HTree) {
  595. /*
  596. ** If our hierarchy isn't valid, we just need to evaluate our animation
  597. ** state.
  598. */
  599. if (!Is_Hierarchy_Valid()) {
  600. Update_Sub_Object_Transforms();
  601. }
  602. return HTree->Get_Transform(boneindex);
  603. } else {
  604. return Transform;
  605. }
  606. }
  607. /***********************************************************************************************
  608. * Animatable3DObjClass::Capture_Bone -- capture the specified bone (override animation) *
  609. * *
  610. * INPUT: *
  611. * *
  612. * OUTPUT: *
  613. * *
  614. * WARNINGS: *
  615. * *
  616. * HISTORY: *
  617. * 3/2/99 GTH : Created. *
  618. *=============================================================================================*/
  619. void Animatable3DObjClass::Capture_Bone(int boneindex)
  620. {
  621. if (HTree) {
  622. HTree->Capture_Bone(boneindex);
  623. }
  624. }
  625. /***********************************************************************************************
  626. * Animatable3DObjClass::Release_Bone -- release the specified bone (allow animation) *
  627. * *
  628. * INPUT: *
  629. * *
  630. * OUTPUT: *
  631. * *
  632. * WARNINGS: *
  633. * *
  634. * HISTORY: *
  635. * 3/2/99 GTH : Created. *
  636. *=============================================================================================*/
  637. void Animatable3DObjClass::Release_Bone(int boneindex)
  638. {
  639. if (HTree) {
  640. HTree->Release_Bone(boneindex);
  641. }
  642. }
  643. /***********************************************************************************************
  644. * Animatable3DObjClass::Is_Bone_Captured -- returns whether the specified bone is captured *
  645. * *
  646. * INPUT: *
  647. * *
  648. * OUTPUT: *
  649. * *
  650. * WARNINGS: *
  651. * *
  652. * HISTORY: *
  653. * 3/2/99 GTH : Created. *
  654. *=============================================================================================*/
  655. bool Animatable3DObjClass::Is_Bone_Captured(int boneindex) const
  656. {
  657. if (HTree) {
  658. return HTree->Is_Bone_Captured(boneindex);
  659. } else {
  660. return false;
  661. }
  662. }
  663. /***********************************************************************************************
  664. * Animatable3DObjClass::Control_Bone -- sets the transform for the bone *
  665. * *
  666. * INPUT: *
  667. * *
  668. * OUTPUT: *
  669. * *
  670. * WARNINGS: *
  671. * *
  672. * HISTORY: *
  673. * 3/2/99 GTH : Created. *
  674. *=============================================================================================*/
  675. void Animatable3DObjClass::Control_Bone(int bindex,const Matrix3D & objtm,bool world_space_translation)
  676. {
  677. #ifdef WWDEBUG
  678. for (int j=0; j<3; j++) {
  679. for (int i=0; i<4; i++) {
  680. WWASSERT(WWMath::Is_Valid_Float(objtm[j][i]));
  681. }
  682. }
  683. #endif
  684. if (HTree) {
  685. HTree->Control_Bone(bindex,objtm,world_space_translation);
  686. Set_Hierarchy_Valid(false);
  687. }
  688. }
  689. /***********************************************************************************************
  690. * Animatable3DObjClass::Update_Sub_Object_Transforms -- recalculate the transforms for our su *
  691. * *
  692. * INPUT: *
  693. * *
  694. * OUTPUT: *
  695. * *
  696. * WARNINGS: *
  697. * *
  698. * HISTORY: *
  699. * 12/8/98 GTH : Created. *
  700. *=============================================================================================*/
  701. void Animatable3DObjClass::Update_Sub_Object_Transforms(void)
  702. {
  703. /*
  704. ** The RenderObj impementation will cause our 'container'
  705. ** to update if we are not valid yet
  706. */
  707. CompositeRenderObjClass::Update_Sub_Object_Transforms();
  708. /*
  709. ** Update the transforms
  710. */
  711. switch (CurMotionMode) {
  712. case BASE_POSE:
  713. Base_Update(Transform);
  714. break;
  715. case SINGLE_ANIM:
  716. if ( ModeAnim.AnimMode != ANIM_MODE_MANUAL ) {
  717. Single_Anim_Progress();
  718. }
  719. Anim_Update(Transform,ModeAnim.Motion,ModeAnim.Frame);
  720. /*
  721. ** Play any sounds that are triggered by this frame of animation
  722. */
  723. if ( ModeAnim.Motion->Has_Embedded_Sounds() ) {
  724. ModeAnim.PrevFrame = AnimatedSoundMgrClass::Trigger_Sound(ModeAnim.Motion, ModeAnim.PrevFrame, ModeAnim.Frame, HTree->Get_Transform(ModeAnim.Motion->Get_Embedded_Sound_Bone_Index()));
  725. }
  726. break;
  727. case DOUBLE_ANIM:
  728. Blend_Update(Transform,ModeInterp.Motion0,ModeInterp.Frame0,
  729. ModeInterp.Motion1,ModeInterp.Frame1,ModeInterp.Percentage);
  730. /*
  731. ** Play any sounds that are triggered by this frame of animation
  732. */
  733. if ( ModeInterp.Motion0->Has_Embedded_Sounds() ) {
  734. ModeInterp.PrevFrame0 = AnimatedSoundMgrClass::Trigger_Sound(ModeInterp.Motion0, ModeInterp.PrevFrame0, ModeInterp.Frame0, HTree->Get_Transform(ModeInterp.Motion0->Get_Embedded_Sound_Bone_Index()));
  735. }
  736. if ( ModeInterp.Motion1->Has_Embedded_Sounds() ) {
  737. ModeInterp.PrevFrame1 = AnimatedSoundMgrClass::Trigger_Sound(ModeInterp.Motion1, ModeInterp.PrevFrame1, ModeInterp.Frame1, HTree->Get_Transform(ModeInterp.Motion1->Get_Embedded_Sound_Bone_Index()));
  738. }
  739. break;
  740. case MULTIPLE_ANIM:
  741. {
  742. Combo_Update(Transform,ModeCombo.AnimCombo);
  743. /*
  744. ** Play any sounds that are triggered by this frame of animation
  745. */
  746. int count = ModeCombo.AnimCombo->Get_Num_Anims();
  747. for (int index = 0; index < count; index ++) {
  748. HAnimClass *motion = ModeCombo.AnimCombo->Peek_Motion(index);
  749. if ( motion != NULL && motion->Has_Embedded_Sounds() ) {
  750. float prev_frame = AnimatedSoundMgrClass::Trigger_Sound(motion, ModeCombo.AnimCombo->Get_Prev_Frame(index),
  751. ModeCombo.AnimCombo->Get_Frame(index), HTree->Get_Transform(motion->Get_Embedded_Sound_Bone_Index()));
  752. ModeCombo.AnimCombo->Set_Prev_Frame(index, prev_frame);
  753. }
  754. }
  755. break;
  756. }
  757. default:
  758. break;
  759. }
  760. Set_Hierarchy_Valid(true);
  761. }
  762. /***********************************************************************************************
  763. * Animatable3DObjClass::Simple_Evaluate_Bone -- If the animation is 'single', evaluate the *
  764. * given pivot and return its transform. *
  765. * *
  766. * INPUT: *
  767. * *
  768. * OUTPUT: *
  769. * *
  770. * WARNINGS: *
  771. * *
  772. * HISTORY: *
  773. * 04/13/2000 PDS : Created. *
  774. *=============================================================================================*/
  775. bool Animatable3DObjClass::Simple_Evaluate_Bone(int boneindex, Matrix3D *tm) const
  776. {
  777. bool retval = false;
  778. //
  779. // Only do this for simple animations
  780. //
  781. if ( CurMotionMode == NONE ||
  782. CurMotionMode == BASE_POSE ||
  783. CurMotionMode == SINGLE_ANIM)
  784. {
  785. //
  786. // Determine which frame we should be on, then use this
  787. // information to determine the bone's transform.
  788. //
  789. float curr_frame = Compute_Current_Frame ();
  790. retval = Simple_Evaluate_Bone (boneindex, curr_frame, tm);
  791. } else {
  792. const_cast <Animatable3DObjClass *>(this)->Update_Sub_Object_Transforms();
  793. *tm = HTree->Get_Transform(boneindex);
  794. }
  795. return retval;
  796. }
  797. /***********************************************************************************************
  798. * Animatable3DObjClass::Simple_Evaluate_Bone -- If the animation is 'single', evaluate the *
  799. * given pivot and return its transform. *
  800. * *
  801. * INPUT: *
  802. * *
  803. * OUTPUT: *
  804. * *
  805. * WARNINGS: *
  806. * *
  807. * HISTORY: *
  808. * 04/13/2000 PDS : Created. *
  809. *=============================================================================================*/
  810. bool Animatable3DObjClass::Simple_Evaluate_Bone(int boneindex, float frame, Matrix3D *tm) const
  811. {
  812. bool retval = false;
  813. //
  814. // Only do this for simple animations
  815. //
  816. if (HTree != NULL) {
  817. if (CurMotionMode == SINGLE_ANIM) {
  818. retval = HTree->Simple_Evaluate_Pivot (ModeAnim.Motion, boneindex, frame, Get_Transform (), tm);
  819. } else if (CurMotionMode == NONE || CurMotionMode == BASE_POSE) {
  820. retval = HTree->Simple_Evaluate_Pivot (boneindex, Get_Transform (), tm);
  821. } else {
  822. *tm = Transform;
  823. }
  824. } else {
  825. *tm = Transform;
  826. }
  827. return retval;
  828. }
  829. /***********************************************************************************************
  830. * Animatable3DObjClass::Compute_Current_Frame -- Returns the animation frame for the next rend*
  831. * *
  832. * INPUT: *
  833. * *
  834. * OUTPUT: *
  835. * *
  836. * WARNINGS: Only works for Single and CSingle! *
  837. * *
  838. * HISTORY: *
  839. * 04/13/2000 PDS : Created. *
  840. *=============================================================================================*/
  841. float Animatable3DObjClass::Compute_Current_Frame(float *newDirection) const
  842. {
  843. float frame = 0;
  844. float direction = ModeAnim.animDirection;
  845. switch (CurMotionMode)
  846. {
  847. case SINGLE_ANIM:
  848. {
  849. frame = ModeAnim.Frame;
  850. //
  851. // Compute the current frame based on elapsed time.
  852. //
  853. if (ModeAnim.AnimMode != ANIM_MODE_MANUAL) {
  854. float sync_time_diff = WW3D::Get_Sync_Time() - ModeAnim.LastSyncTime;
  855. float delta = ModeAnim.Motion->Get_Frame_Rate() * ModeAnim.frameRateMultiplier * ModeAnim.animDirection * sync_time_diff * 0.001f;
  856. frame += delta;
  857. //
  858. // Wrap the frame
  859. //
  860. switch (ModeAnim.AnimMode)
  861. {
  862. case ANIM_MODE_ONCE:
  863. if (frame >= ModeAnim.Motion->Get_Num_Frames() - 1) {
  864. frame = ModeAnim.Motion->Get_Num_Frames() - 1;
  865. }
  866. break;
  867. case ANIM_MODE_LOOP:
  868. if ( frame >= ModeAnim.Motion->Get_Num_Frames() - 1 ) {
  869. frame -= ModeAnim.Motion->Get_Num_Frames() - 1;
  870. }
  871. // If it is still too far out, reset
  872. if ( frame >= ModeAnim.Motion->Get_Num_Frames() - 1 ) {
  873. frame = 0;
  874. }
  875. break;
  876. case ANIM_MODE_ONCE_BACKWARDS: //play animation one time but backwards
  877. if (frame < 0) {
  878. frame = 0;
  879. }
  880. break;
  881. case ANIM_MODE_LOOP_BACKWARDS: //play animation backwards in a loop
  882. if ( frame < 0 ) {
  883. frame += ModeAnim.Motion->Get_Num_Frames() - 1;
  884. }
  885. // If it is still too far out, reset
  886. if ( frame < 0 ) {
  887. frame = ModeAnim.Motion->Get_Num_Frames() - 1;
  888. }
  889. break;
  890. case ANIM_MODE_LOOP_PINGPONG:
  891. if (ModeAnim.animDirection >= 1.0f)
  892. { //playing forwards, reverse direction
  893. if (frame >= (ModeAnim.Motion->Get_Num_Frames() - 1))
  894. { //step backwards in animation by excess time
  895. frame = (ModeAnim.Motion->Get_Num_Frames() - 1)*2 - frame;
  896. // If it is still too far out, reset
  897. if ( frame >= ModeAnim.Motion->Get_Num_Frames() - 1 )
  898. frame = (ModeAnim.Motion->Get_Num_Frames() - 1);
  899. direction = ModeAnim.animDirection * -1.0f;
  900. }
  901. }
  902. else
  903. { //playing backwards, reverse direction
  904. if (frame < 0)
  905. { //step forwards in animation by excess time
  906. frame = -frame;
  907. // If it is still too far out, reset
  908. if ( frame >= ModeAnim.Motion->Get_Num_Frames() - 1 )
  909. frame = 0;
  910. direction = ModeAnim.animDirection * -1.0f;
  911. }
  912. }
  913. break;
  914. }
  915. }
  916. }
  917. break;
  918. }
  919. if (newDirection)
  920. *newDirection = direction;
  921. return frame;
  922. }
  923. /***********************************************************************************************
  924. * Animatable3DObjClass::Single_Anim_Progress -- progess anims for loop and once *
  925. * *
  926. * INPUT: *
  927. * *
  928. * OUTPUT: *
  929. * *
  930. * WARNINGS: Only works for Single and CSingle *
  931. * *
  932. * HISTORY: *
  933. * 10/26/99 BMG : Created. *
  934. *=============================================================================================*/
  935. void Animatable3DObjClass::Single_Anim_Progress (void)
  936. {
  937. //
  938. // Update the current frame (only works in "SINGLE_ANIM" mode!)
  939. //
  940. if (CurMotionMode == SINGLE_ANIM) {
  941. //
  942. // Update the frame number and sync time
  943. //
  944. float oldprev = ModeAnim.PrevFrame;
  945. ModeAnim.PrevFrame = ModeAnim.Frame;
  946. ModeAnim.Frame = Compute_Current_Frame(&ModeAnim.animDirection);
  947. ModeAnim.LastSyncTime = WW3D::Get_Sync_Time();
  948. if (ModeAnim.Frame == ModeAnim.PrevFrame) {
  949. // This function was somehow called twice per frame.
  950. // Since ModeAnim.Frame hasn't changed, reset the ModeAnim.PrevFrame.
  951. // If you don't do this sounds won't be triggered properly because Frame and PrevFrame will be the same.
  952. ModeAnim.PrevFrame = oldprev;
  953. }
  954. //
  955. // Force the heirarchy to be recalculated
  956. //
  957. Set_Hierarchy_Valid (false);
  958. }
  959. }
  960. /***********************************************************************************************
  961. * Animatable3DObjClass::Is_Animation_Complete -- is the current animation on the last frame? *
  962. * *
  963. * INPUT: *
  964. * *
  965. * OUTPUT: *
  966. * *
  967. * WARNINGS: Only works for Single, ONCE anims *
  968. * *
  969. * HISTORY: *
  970. * 4/13/99 BMG : Created. *
  971. *=============================================================================================*/
  972. bool Animatable3DObjClass::Is_Animation_Complete( void ) const
  973. {
  974. if (CurMotionMode == SINGLE_ANIM) {
  975. if ( ModeAnim.AnimMode == ANIM_MODE_ONCE ) {
  976. return ( ModeAnim.Frame == ModeAnim.Motion->Get_Num_Frames() - 1 );
  977. }
  978. else
  979. if ( ModeAnim.AnimMode == ANIM_MODE_ONCE_BACKWARDS)
  980. { return ( ModeAnim.Frame == 0);
  981. }
  982. }
  983. return false;
  984. }
  985. /***********************************************************************************************
  986. * Animatable3DObjClass::Peek_Animation_And_Info *
  987. *=============================================================================================*/
  988. HAnimClass * Animatable3DObjClass::Peek_Animation_And_Info(float& frame, int& numFrames, int& mode, float& mult)
  989. {
  990. if ( CurMotionMode == SINGLE_ANIM ) {
  991. frame = ModeAnim.Frame;
  992. numFrames = ModeAnim.Motion ? ModeAnim.Motion->Get_Num_Frames() : 0;
  993. mode = ModeAnim.AnimMode;
  994. mult = ModeAnim.frameRateMultiplier;
  995. return ModeAnim.Motion;
  996. } else {
  997. return NULL;
  998. }
  999. }
  1000. /***********************************************************************************************
  1001. * Animatable3DObjClass::Set_Animation_Frame_Rate_Multiplier *
  1002. *=============================================================================================*/
  1003. void Animatable3DObjClass::Set_Animation_Frame_Rate_Multiplier(float multiplier)
  1004. {
  1005. // 020607 srj -- added
  1006. ModeAnim.frameRateMultiplier = multiplier;
  1007. }
  1008. // (gth) TESTING DYNAMICALLY SWAPPING SKELETONS!
  1009. void Animatable3DObjClass::Set_HTree(HTreeClass * new_htree)
  1010. {
  1011. WWMEMLOG(MEM_ANIMATION);
  1012. // try to ensure that the htree we're using has the same structure...
  1013. WWASSERT(new_htree->Num_Pivots() == HTree->Num_Pivots());
  1014. // just assign it...
  1015. if (HTree != NULL) {
  1016. delete HTree;
  1017. }
  1018. HTree = W3DNEW HTreeClass(*new_htree);
  1019. }
  1020. // EOF - animobj.cpp