afxResidueMgr.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "ts/tsShapeInstance.h"
  26. #include "afx/ce/afxZodiacMgr.h"
  27. #include "afx/ce/afxModel.h"
  28. #include "afx/afxResidueMgr.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. int QSORT_CALLBACK afxResidueMgr::ResidueList::compare_residue(const void* p1, const void* p2)
  31. {
  32. const afxResidueMgr::Residue** pd1 = (const afxResidueMgr::Residue**)p1;
  33. const afxResidueMgr::Residue** pd2 = (const afxResidueMgr::Residue**)p2;
  34. return int(((char*)(*pd1)->data.simobject) - ((char*)(*pd2)->data.simobject));
  35. }
  36. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  37. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  38. inline void afxResidueMgr::ResidueList::swap_array_ptrs()
  39. {
  40. Vector<Residue*>* tmp = m_array;
  41. m_array = m_scratch_array;
  42. m_scratch_array = tmp;
  43. }
  44. void afxResidueMgr::ResidueList::free_residue(Residue* residue)
  45. {
  46. if (the_mgr == NULL)
  47. return;
  48. if (the_mgr->requires_delete_tracking(residue))
  49. the_mgr->disable_delete_tracking(residue);
  50. the_mgr->free_residue(residue);
  51. }
  52. afxResidueMgr::ResidueList::ResidueList()
  53. {
  54. VECTOR_SET_ASSOCIATION(m_array_a);
  55. VECTOR_SET_ASSOCIATION(m_array_b);
  56. m_array = &m_array_a;
  57. m_scratch_array = &m_array_b ;
  58. m_dirty = false;
  59. m_pending = -1;
  60. }
  61. afxResidueMgr::ResidueList::~ResidueList()
  62. {
  63. clear();
  64. }
  65. void afxResidueMgr::ResidueList::clear()
  66. {
  67. if (the_mgr)
  68. {
  69. for (S32 i = 0; i < m_array->size(); i++)
  70. {
  71. Residue* r = (*m_array)[i];
  72. the_mgr->free_residue(r);
  73. }
  74. }
  75. m_array_a.clear();
  76. m_array_b.clear();
  77. }
  78. void afxResidueMgr::ResidueList::sort()
  79. {
  80. dQsort(m_array->address(), m_array->size(), sizeof(Residue*), compare_residue);
  81. m_dirty = false;
  82. }
  83. void afxResidueMgr::ResidueList::fadeAndCull(U32 now)
  84. {
  85. for (S32 i = 0; i < m_array->size(); i++)
  86. {
  87. Residue* r = (*m_array)[i];
  88. // done
  89. if (now >= r->stop_time)
  90. {
  91. free_residue(r);
  92. }
  93. // fading
  94. else if (now >= r->fade_time)
  95. {
  96. r->fade = 1.0f - ((F32)(now - r->fade_time))/((F32)(r->stop_time - r->fade_time));
  97. m_scratch_array->push_back(r);
  98. }
  99. // opaque
  100. else
  101. {
  102. r->fade = 1.0f;
  103. m_scratch_array->push_back(r);
  104. }
  105. }
  106. m_array->clear();
  107. swap_array_ptrs();
  108. }
  109. // removes all residue with datablock matching obj
  110. void afxResidueMgr::ResidueList::stripMatchingObjects(SimObject* db, bool del_notify)
  111. {
  112. if (del_notify)
  113. {
  114. for (S32 i = 0; i < m_array->size(); i++)
  115. {
  116. Residue* r = (*m_array)[i];
  117. if (db == r->data.simobject && the_mgr != NULL)
  118. the_mgr->free_residue(r);
  119. else
  120. m_scratch_array->push_back(r);
  121. }
  122. }
  123. else
  124. {
  125. for (S32 i = 0; i < m_array->size(); i++)
  126. {
  127. Residue* r = (*m_array)[i];
  128. if (db == r->data.simobject)
  129. free_residue(r);
  130. else
  131. m_scratch_array->push_back(r);
  132. }
  133. }
  134. m_array->clear();
  135. swap_array_ptrs();
  136. }
  137. void afxResidueMgr::ResidueList::add(Residue* residue)
  138. {
  139. m_array->push_back(residue);
  140. m_dirty = true;
  141. }
  142. void afxResidueMgr::manage_residue(const Residue* r)
  143. {
  144. if (r == NULL || r->fade < 0.01f)
  145. return;
  146. if (r->type == ZODIAC)
  147. {
  148. LinearColorF zode_color = ColorI(r->params.zodiac.r, r->params.zodiac.g, r->params.zodiac.b, r->params.zodiac.a);
  149. afxZodiacData* zd = (afxZodiacData*) r->data.zodiac;
  150. if (zd->blend_flags == afxZodiacDefs::BLEND_SUBTRACTIVE)
  151. zode_color *= r->fade;
  152. else
  153. zode_color.alpha *= r->fade;
  154. Point3F zode_pos(r->params.zodiac.pos_x, r->params.zodiac.pos_y, r->params.zodiac.pos_z);
  155. Point2F zode_vrange(r->params.zodiac.vrange_dn, r->params.zodiac.vrange_dn);
  156. if (r->params.zodiac.on_terrain)
  157. {
  158. afxZodiacMgr::addTerrainZodiac(zode_pos, r->params.zodiac.rad, zode_color, r->params.zodiac.ang, zd);
  159. }
  160. else
  161. {
  162. afxZodiacMgr::addInteriorZodiac(zode_pos, r->params.zodiac.rad, zode_vrange, zode_color, r->params.zodiac.ang, zd);
  163. }
  164. }
  165. else if (r->type == MODEL)
  166. {
  167. r->data.model->setFadeAmount(r->fade);
  168. }
  169. }
  170. void afxResidueMgr::ResidueList::manage()
  171. {
  172. if (the_mgr == NULL)
  173. return;
  174. S32 n_residue = m_array->size();
  175. for (S32 x = 0; x < n_residue; x++)
  176. the_mgr->manage_residue((*m_array)[x]);
  177. }
  178. U32 afxResidueMgr::ResidueList::findPendingBestBump(U32 look_max)
  179. {
  180. U32 soonest = 1000*60*60*24;
  181. m_pending = -1;
  182. U32 n = m_array->size();
  183. for (U32 i = 0; i < n && i < look_max; i++)
  184. {
  185. Residue* r = (*m_array)[i];
  186. if (r->stop_time < soonest)
  187. {
  188. soonest = r->stop_time;
  189. m_pending = i;
  190. }
  191. }
  192. return soonest;
  193. }
  194. void afxResidueMgr::ResidueList::bumpPending()
  195. {
  196. if (m_pending >= 0 && m_pending < m_array->size())
  197. {
  198. Residue* r = (*m_array)[m_pending];
  199. m_array->erase(m_pending);
  200. free_residue(r);
  201. }
  202. m_pending = -1;
  203. }
  204. bool afxResidueMgr::requires_delete_tracking(Residue* r)
  205. {
  206. return (r->type == MODEL);
  207. }
  208. void afxResidueMgr::enable_delete_tracking(Residue* r)
  209. {
  210. deleteNotify(r->data.simobject);
  211. }
  212. void afxResidueMgr::disable_delete_tracking(Residue* r)
  213. {
  214. clearNotify(r->data.simobject);
  215. r->data.simobject->deleteObject();
  216. r->data.simobject = 0;
  217. }
  218. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  219. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  220. afxResidueMgr* afxResidueMgr::the_mgr = NULL;
  221. U32 afxResidueMgr::m_max_residue_objs = 256;
  222. bool afxResidueMgr::enabled = true;
  223. IMPLEMENT_CONOBJECT(afxResidueMgr);
  224. ConsoleDocClass( afxResidueMgr,
  225. "@brief A class that manages certain AFX effects that can persist for long durations.\n\n"
  226. "A class that manages certain AFX effects that can persist much longer than the duration of choreographers.\n"
  227. "@ingroup AFX\n"
  228. );
  229. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  230. // free-list management
  231. afxResidueMgr::Residue* afxResidueMgr::alloc_free_pool_block()
  232. {
  233. // allocate new block for the free-list
  234. m_free_pool_blocks.push_back(new Residue[FREE_POOL_BLOCK_SIZE]);
  235. // link them onto the free-list
  236. Residue* new_block = m_free_pool_blocks.last();
  237. for (U32 i = 0; i < FREE_POOL_BLOCK_SIZE - 1; i++)
  238. new_block[i].next = &new_block[i + 1];
  239. // tail of free-list points to NULL
  240. new_block[FREE_POOL_BLOCK_SIZE - 1].next = NULL;
  241. return new_block;
  242. }
  243. afxResidueMgr::Residue* afxResidueMgr::alloc_residue()
  244. {
  245. // need new free-list-block if m_next_free is null
  246. if (!m_next_free)
  247. m_next_free = alloc_free_pool_block();
  248. // pop new residue from head of free-list
  249. Residue* residue = m_next_free;
  250. m_next_free = residue->next;
  251. residue->next = NULL;
  252. return residue;
  253. }
  254. void afxResidueMgr::free_residue(Residue* residue)
  255. {
  256. if (residue && residue->type == ZODIAC)
  257. {
  258. if (residue->data.zodiac && residue->data.zodiac->isTempClone())
  259. {
  260. delete residue->data.zodiac;
  261. residue->data.zodiac = 0;
  262. }
  263. }
  264. // push residue onto head of free-list
  265. residue->next = m_next_free;
  266. m_next_free = residue;
  267. }
  268. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  269. void afxResidueMgr::deleteResidueObject(SimObject* obj, bool del_notify)
  270. {
  271. m_managed.stripMatchingObjects(obj, del_notify);
  272. }
  273. void afxResidueMgr::bump_residue()
  274. {
  275. if (m_managed.findPendingBestBump())
  276. m_managed.bumpPending();
  277. }
  278. void afxResidueMgr::add_residue(Residue* residue)
  279. {
  280. AssertFatal(residue != NULL, "residue pointer is NULL.");
  281. if (m_managed.size() >= m_max_residue_objs)
  282. bump_residue();
  283. m_managed.add(residue);
  284. manage_residue(residue);
  285. if (requires_delete_tracking(residue))
  286. enable_delete_tracking(residue);
  287. }
  288. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  289. afxResidueMgr::afxResidueMgr()
  290. {
  291. mObjBox.minExtents.set(-1e7, -1e7, -1e7);
  292. mObjBox.maxExtents.set( 1e7, 1e7, 1e7);
  293. mWorldBox.minExtents.set(-1e7, -1e7, -1e7);
  294. mWorldBox.maxExtents.set( 1e7, 1e7, 1e7);
  295. m_next_free = NULL;
  296. VECTOR_SET_ASSOCIATION(m_free_pool_blocks);
  297. }
  298. afxResidueMgr::~afxResidueMgr()
  299. {
  300. cleanup();
  301. }
  302. void afxResidueMgr::cleanup()
  303. {
  304. m_managed.clear();
  305. m_next_free = NULL;
  306. for (S32 i = 0; i < m_free_pool_blocks.size(); i++)
  307. delete [] m_free_pool_blocks[i];
  308. m_free_pool_blocks.clear();
  309. }
  310. void afxResidueMgr::onDeleteNotify(SimObject* obj)
  311. {
  312. deleteResidueObject(obj, true);
  313. Parent::onDeleteNotify(obj);
  314. }
  315. void afxResidueMgr::residueAdvanceTime()
  316. {
  317. U32 now = Platform::getVirtualMilliseconds();
  318. m_managed.fadeAndCull(now);
  319. m_managed.sortIfDirty();
  320. m_managed.manage();
  321. }
  322. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  323. // add ZODIAC residue
  324. void afxResidueMgr::add_interior_zodiac(F32 dur, F32 fade_dur, afxZodiacData* zode, const Point3F& pos,
  325. F32 rad, const Point2F& vrange, const LinearColorF& col, F32 ang)
  326. {
  327. add_zodiac(dur, fade_dur, zode, pos, rad, vrange, col, ang, false);
  328. }
  329. void afxResidueMgr::add_terrain_zodiac(F32 dur, F32 fade_dur, afxZodiacData* zode, const Point3F& pos,
  330. F32 rad, const LinearColorF& col, F32 ang)
  331. {
  332. static Point2F vrange(0.0, 0.0);
  333. add_zodiac(dur, fade_dur, zode, pos, rad, vrange, col, ang, true);
  334. }
  335. void afxResidueMgr::add_zodiac(F32 dur, F32 fade_dur, afxZodiacData* zode, const Point3F& pos,
  336. F32 rad, const Point2F& vrange, const LinearColorF& col, F32 ang, bool on_terrain)
  337. {
  338. if (m_max_residue_objs == 0 || dur <= 0 || the_mgr == NULL)
  339. return;
  340. ColorI col_i = LinearColorF(col).toColorI();
  341. U32 now = Platform::getVirtualMilliseconds();
  342. Residue* residue = the_mgr->alloc_residue();
  343. //
  344. residue->type = ZODIAC;
  345. residue->data.zodiac = zode;
  346. residue->fade_time = now + (U32)(dur*1000);
  347. residue->stop_time = residue->fade_time + (U32)(fade_dur*1000);
  348. residue->fade = 1.0f;
  349. //
  350. residue->params.zodiac.pos_x = pos.x;
  351. residue->params.zodiac.pos_y = pos.y;
  352. residue->params.zodiac.pos_z = pos.z;
  353. residue->params.zodiac.rad = rad;
  354. residue->params.zodiac.vrange_dn = vrange.x;
  355. residue->params.zodiac.vrange_up = vrange.y;
  356. residue->params.zodiac.r = col_i.red;
  357. residue->params.zodiac.g = col_i.green;
  358. residue->params.zodiac.b = col_i.blue;
  359. residue->params.zodiac.a = col_i.alpha;
  360. residue->params.zodiac.ang = ang;
  361. residue->params.zodiac.on_terrain = on_terrain;
  362. //
  363. residue->next = 0;
  364. the_mgr->add_residue(residue);
  365. }
  366. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  367. // add MODEL residue
  368. void afxResidueMgr::add(F32 dur, F32 fade_dur, afxModel* model)
  369. {
  370. if (m_max_residue_objs == 0 || dur <= 0 || the_mgr == NULL)
  371. return;
  372. U32 now = Platform::getVirtualMilliseconds();
  373. Residue* residue = the_mgr->alloc_residue();
  374. //
  375. residue->type = MODEL;
  376. residue->data.model = model;
  377. residue->fade_time = now + (U32)(dur*1000);
  378. residue->stop_time = residue->fade_time + (U32)(fade_dur*1000);
  379. residue->fade = 1.0f;
  380. //
  381. residue->next = 0;
  382. the_mgr->add_residue(residue);
  383. }
  384. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//