Tube.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. #include "gosFXHeaders.hpp"
  2. #include <MLR\MLRIndexedTriangleCloud.hpp>
  3. //==========================================================================//
  4. // File: gosFX_Tube.cpp //
  5. // Contents: Base gosFX::Tube Component //
  6. //---------------------------------------------------------------------------//
  7. // Copyright (C) Microsoft Corporation. All rights reserved. //
  8. //===========================================================================//
  9. //
  10. //############################################################################
  11. //######################## gosFX::Tube__Specification #############################
  12. //############################################################################
  13. //------------------------------------------------------------------------------
  14. //
  15. gosFX::Tube__Specification::Tube__Specification(
  16. Stuff::MemoryStream *stream,
  17. int gfx_version
  18. ):
  19. Effect__Specification(TubeClassID, stream, gfx_version)
  20. {
  21. Check_Pointer(this);
  22. Check_Object(stream);
  23. Verify(gos_GetCurrentHeap() == Heap);
  24. //
  25. //-------------------
  26. // Load in the curves
  27. //-------------------
  28. //
  29. m_profilesPerSecond.Load(stream, gfx_version);
  30. m_pLifeSpan.Load(stream, gfx_version);
  31. m_emitterSizeX.Load(stream, gfx_version);
  32. m_emitterSizeY.Load(stream, gfx_version);
  33. m_emitterSizeZ.Load(stream, gfx_version);
  34. m_minimumDeviation.Load(stream, gfx_version);
  35. m_maximumDeviation.Load(stream, gfx_version);
  36. m_pDisplacement.Load(stream, gfx_version);
  37. if (gfx_version < 12)
  38. {
  39. m_pScale.m_ageCurve.SetCurve(1.0f);
  40. m_pScale.m_seeded = false;
  41. m_pScale.m_seedCurve.SetCurve(1.0f);
  42. }
  43. else
  44. m_pScale.Load(stream, gfx_version);
  45. m_pRed.Load(stream, gfx_version);
  46. m_pGreen.Load(stream, gfx_version);
  47. m_pBlue.Load(stream, gfx_version);
  48. m_pAlpha.Load(stream, gfx_version);
  49. m_pUOffset.Load(stream, gfx_version);
  50. m_pVOffset.Load(stream, gfx_version);
  51. m_pUSize.Load(stream, gfx_version);
  52. m_pVSize.Load(stream, gfx_version);
  53. int type;
  54. *stream >> m_maxProfileCount >> type >> m_insideOut;
  55. m_profileType = static_cast<ProfileType>(type);
  56. CalculateUBias(true);
  57. BuildTemplate();
  58. }
  59. //------------------------------------------------------------------------------
  60. //
  61. gosFX::Tube__Specification::Tube__Specification():
  62. Effect__Specification(TubeClassID)
  63. {
  64. Verify(gos_GetCurrentHeap() == Heap);
  65. m_maxProfileCount = 0;
  66. m_profileType = e_Ribbon;
  67. m_insideOut = false;
  68. m_UBias = 0.0f;
  69. Check_Pointer(this);
  70. }
  71. //------------------------------------------------------------------------------
  72. //
  73. gosFX::Tube__Specification*
  74. gosFX::Tube__Specification::Make(
  75. Stuff::MemoryStream *stream,
  76. int gfx_version
  77. )
  78. {
  79. Check_Object(stream);
  80. gos_PushCurrentHeap(Heap);
  81. Tube__Specification *spec =
  82. new gosFX::Tube__Specification(stream, gfx_version);
  83. gos_PopCurrentHeap();
  84. return spec;
  85. }
  86. //------------------------------------------------------------------------------
  87. //
  88. void
  89. gosFX::Tube__Specification::Save(Stuff::MemoryStream *stream)
  90. {
  91. Check_Object(this);
  92. Check_Object(stream);
  93. Effect__Specification::Save(stream);
  94. //
  95. //----------------
  96. // Save our curves
  97. //----------------
  98. //
  99. m_profilesPerSecond.Save(stream);
  100. m_pLifeSpan.Save(stream);
  101. m_emitterSizeX.Save(stream);
  102. m_emitterSizeY.Save(stream);
  103. m_emitterSizeZ.Save(stream);
  104. m_minimumDeviation.Save(stream);
  105. m_maximumDeviation.Save(stream);
  106. m_pDisplacement.Save(stream);
  107. m_pScale.Save(stream);
  108. m_pRed.Save(stream);
  109. m_pGreen.Save(stream);
  110. m_pBlue.Save(stream);
  111. m_pAlpha.Save(stream);
  112. m_pUOffset.Save(stream);
  113. m_pVOffset.Save(stream);
  114. m_pUSize.Save(stream);
  115. m_pVSize.Save(stream);
  116. *stream << m_maxProfileCount << static_cast<int>(m_profileType);
  117. *stream << m_insideOut;
  118. }
  119. //------------------------------------------------------------------------------
  120. //
  121. void
  122. gosFX::Tube__Specification::BuildDefaults()
  123. {
  124. Check_Object(this);
  125. Effect__Specification::BuildDefaults();
  126. m_profilesPerSecond.SetCurve(4.0f);
  127. m_pLifeSpan.SetCurve(1.0f);
  128. m_emitterSizeX.SetCurve(0.0f);
  129. m_emitterSizeY.SetCurve(0.0f);
  130. m_emitterSizeZ.SetCurve(0.0f);
  131. m_minimumDeviation.SetCurve(0.0f);
  132. m_maximumDeviation.SetCurve(0.0f);
  133. m_pDisplacement.m_ageCurve.SetCurve(0.0f);
  134. m_pDisplacement.m_seeded = false;
  135. m_pDisplacement.m_seedCurve.SetCurve(1.0f);
  136. m_pScale.m_ageCurve.SetCurve(1.0f);
  137. m_pScale.m_seeded = false;
  138. m_pScale.m_seedCurve.SetCurve(1.0f);
  139. m_pRed.m_ageCurve.SetCurve(1.0f);
  140. m_pRed.m_seeded = false;
  141. m_pRed.m_seedCurve.SetCurve(1.0f);
  142. m_pGreen.m_ageCurve.SetCurve(1.0f);
  143. m_pGreen.m_seeded = false;
  144. m_pGreen.m_seedCurve.SetCurve(1.0f);
  145. m_pBlue.m_ageCurve.SetCurve(1.0f);
  146. m_pBlue.m_seeded = false;
  147. m_pBlue.m_seedCurve.SetCurve(1.0f);
  148. m_pAlpha.m_ageCurve.SetCurve(1.0f);
  149. m_pAlpha.m_seeded = false;
  150. m_pAlpha.m_seedCurve.SetCurve(1.0f);
  151. m_pUOffset.m_ageCurve.SetCurve(0.0f);
  152. m_pUOffset.m_seeded = false;
  153. m_pUOffset.m_seedCurve.SetCurve(1.0f);
  154. m_pVOffset.m_ageCurve.SetCurve(0.0f);
  155. m_pVOffset.m_seeded = false;
  156. m_pVOffset.m_seedCurve.SetCurve(1.0f);
  157. m_pUSize.m_ageCurve.SetCurve(1.0f);
  158. m_pUSize.m_seeded = false;
  159. m_pUSize.m_seedCurve.SetCurve(1.0f);
  160. m_pVSize.m_ageCurve.SetCurve(1.0f);
  161. m_pVSize.m_seeded = false;
  162. m_pVSize.m_seedCurve.SetCurve(1.0f);
  163. m_maxProfileCount = 2;
  164. m_profileType = e_Triangle;
  165. m_insideOut = false;
  166. CalculateUBias(false);
  167. BuildTemplate();
  168. }
  169. //------------------------------------------------------------------------------
  170. //
  171. bool
  172. gosFX::Tube__Specification::IsDataValid(bool fix_data)
  173. {
  174. Check_Object(this);
  175. if(m_maxProfileCount<2)
  176. if(fix_data)
  177. {
  178. m_maxProfileCount=2;
  179. PAUSE(("Warning: Value \"maxProfileCount\" in Effect \"%s\" Is Out of Range and has been Reset",(char *)m_name));
  180. }
  181. else
  182. return false;
  183. Stuff::Scalar min,max;
  184. m_pScale.ExpensiveComputeRange(&min,&max);
  185. if(min<0.0f)
  186. if(fix_data)
  187. {
  188. m_pScale.m_ageCurve.SetCurve(1.0f);
  189. m_pScale.m_seeded = false;
  190. m_pScale.m_seedCurve.SetCurve(1.0f);
  191. PAUSE(("Warning: Curve \"pScale\" in Effect \"%s\" Is Out of Range and has been Reset",(char *)m_name));
  192. }
  193. else
  194. return false;
  195. Stuff::Scalar max_offset, min_offset;
  196. Stuff::Scalar max_scale, min_scale;
  197. m_pUSize.ExpensiveComputeRange(&min_scale, &max_scale);
  198. Stuff::Scalar lower = min_scale;
  199. if (lower > 0.0f)
  200. lower = 0.0f;
  201. Stuff::Scalar upper = max_scale;
  202. //
  203. //------------------------------------
  204. // Calculate the worst case UV offsets
  205. //------------------------------------
  206. //
  207. m_pVOffset.ExpensiveComputeRange(&min_offset, &max_offset);
  208. lower += min_offset;
  209. upper += max_offset;
  210. if (upper > 99.0f || lower < -99.0f)
  211. {
  212. if(fix_data)
  213. {
  214. m_pVOffset.m_ageCurve.SetCurve(0.0f);
  215. m_pVOffset.m_seeded = false;
  216. m_pVOffset.m_seedCurve.SetCurve(1.0f);
  217. PAUSE(("Warning: Curve \"VOffset\" in Effect \"%s\" Is Out of Range and has been Reset",(char *)m_name));
  218. }
  219. else
  220. return false;
  221. }
  222. m_pVSize.ExpensiveComputeRange(&min_scale, &max_scale);
  223. lower = min_scale;
  224. if (lower > 0.0f)
  225. lower = 0.0f;
  226. upper = max_scale;
  227. //
  228. //------------------------------------
  229. // Calculate the worst case UV offsets
  230. //------------------------------------
  231. //
  232. max_offset, min_offset;
  233. m_pUOffset.ExpensiveComputeRange(&min_offset, &max_offset);
  234. lower += min_offset;
  235. upper += max_offset;
  236. if (upper > 99.0f || lower < -99.0f)
  237. {
  238. if(fix_data)
  239. {
  240. m_pUOffset.m_ageCurve.SetCurve(0.0f);
  241. m_pUOffset.m_seeded = false;
  242. m_pUOffset.m_seedCurve.SetCurve(1.0f);
  243. PAUSE(("Warning: Curve \"UOffset\" in Effect \"%s\" Is Out of Range and has been Reset",(char *)m_name));
  244. }
  245. else
  246. return false;
  247. }
  248. return Effect__Specification::IsDataValid(fix_data);
  249. }
  250. //------------------------------------------------------------------------------
  251. //
  252. void
  253. gosFX::Tube__Specification::Copy(Tube__Specification *spec)
  254. {
  255. Check_Object(this);
  256. Check_Object(spec);
  257. Verify(gos_GetCurrentHeap() == Heap);
  258. Effect__Specification::Copy(spec);
  259. //
  260. //----------------
  261. // Copy the curves
  262. //----------------
  263. //
  264. gos_PushCurrentHeap(Heap);
  265. m_profilesPerSecond = spec->m_profilesPerSecond;
  266. m_pLifeSpan = spec->m_pLifeSpan;
  267. m_emitterSizeX = spec->m_emitterSizeX;
  268. m_emitterSizeY = spec->m_emitterSizeY;
  269. m_emitterSizeZ = spec->m_emitterSizeZ;
  270. m_minimumDeviation = spec->m_minimumDeviation;
  271. m_maximumDeviation = spec->m_maximumDeviation;
  272. m_pDisplacement = spec->m_pDisplacement;
  273. m_pScale = spec->m_pScale;
  274. m_pRed = spec->m_pRed;
  275. m_pGreen = spec->m_pGreen;
  276. m_pBlue = spec->m_pBlue;
  277. m_pAlpha = spec->m_pAlpha;
  278. m_pUOffset = spec->m_pUOffset;
  279. m_pVOffset = spec->m_pVOffset;
  280. m_pUSize = spec->m_pUSize;
  281. m_pVSize = spec->m_pVSize;
  282. m_maxProfileCount = spec->m_maxProfileCount;
  283. m_profileType = spec->m_profileType;
  284. m_vertices = spec->m_vertices;
  285. m_uvs = spec->m_uvs;
  286. m_insideOut = spec->m_insideOut;
  287. m_UBias = spec->m_UBias;
  288. gos_PopCurrentHeap();
  289. }
  290. //------------------------------------------------------------------------------
  291. //
  292. void
  293. gosFX::Tube__Specification::BuildTemplate()
  294. {
  295. Check_Object(this);
  296. switch (m_profileType)
  297. {
  298. case e_Ribbon:
  299. case e_AlignedRibbon:
  300. m_vertices.SetLength(2);
  301. m_vertices[0] = Stuff::Vector3D(1.0f, 0.0f, 0.0f);
  302. m_vertices[1] = Stuff::Vector3D(-1.0f, 0.0f, 0.0f);
  303. m_uvs.SetLength(2);
  304. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  305. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  306. break;
  307. case e_VerticalRibbon:
  308. m_vertices.SetLength(2);
  309. m_vertices[0] = Stuff::Vector3D(0.0f, 0.0f, -1.0f);
  310. m_vertices[1] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  311. m_uvs.SetLength(2);
  312. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  313. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  314. break;
  315. case e_Triangle:
  316. m_vertices.SetLength(4);
  317. m_vertices[0] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  318. m_vertices[1] = Stuff::Vector3D(-0.8660254f, 0.0f, -0.5f);
  319. m_vertices[2] = Stuff::Vector3D(0.8660254f, 0.0f, -0.5f);
  320. m_vertices[3] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  321. m_uvs.SetLength(4);
  322. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  323. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f/3.0f);
  324. m_uvs[2] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 2.0f/3.0f);
  325. m_uvs[3] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  326. break;
  327. case e_Square:
  328. m_vertices.SetLength(5);
  329. m_vertices[0] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  330. m_vertices[1] = Stuff::Vector3D(-1.0f, 0.0f, 0.0f);
  331. m_vertices[2] = Stuff::Vector3D(0.0f, 0.0f, -1.0f);
  332. m_vertices[3] = Stuff::Vector3D(1.0f, 0.0f, 0.0f);
  333. m_vertices[4] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  334. m_uvs.SetLength(5);
  335. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  336. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.25f);
  337. m_uvs[2] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.5f);
  338. m_uvs[3] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.75f);
  339. m_uvs[4] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  340. break;
  341. case e_Cross:
  342. m_vertices.SetLength(5);
  343. m_vertices[0] = Stuff::Vector3D(0.0f, 0.0f, 0.0f);
  344. m_vertices[1] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  345. m_vertices[2] = Stuff::Vector3D(-1.0f, 0.0f, 0.0f);
  346. m_vertices[3] = Stuff::Vector3D(0.0f, 0.0f, -1.0f);
  347. m_vertices[4] = Stuff::Vector3D(1.0f, 0.0f, 0.0f);
  348. m_uvs.SetLength(5);
  349. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  350. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  351. m_uvs[2] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  352. m_uvs[3] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  353. m_uvs[4] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  354. break;
  355. case e_Pentagon:
  356. m_vertices.SetLength(6);
  357. m_vertices[0] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  358. m_vertices[1] = Stuff::Vector3D(-0.9510565f, 0.0f, 0.309017f);
  359. m_vertices[2] = Stuff::Vector3D(-0.58778525f, 0.0f, -0.8090167f);
  360. m_vertices[3] = Stuff::Vector3D(0.58778525f, 0.0f, -0.8090167f);
  361. m_vertices[4] = Stuff::Vector3D(0.9510565f, 0.0f, 0.309017f);
  362. m_vertices[5] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  363. m_uvs.SetLength(6);
  364. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  365. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.2f);
  366. m_uvs[2] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.4f);
  367. m_uvs[3] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.6f);
  368. m_uvs[4] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.8f);
  369. m_uvs[5] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  370. break;
  371. case e_Hexagon:
  372. m_vertices.SetLength(7);
  373. m_vertices[0] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  374. m_vertices[1] = Stuff::Vector3D(-0.8660254f, 0.0f, 0.5f);
  375. m_vertices[2] = Stuff::Vector3D(-0.8660254f, 0.0f, -0.5f);
  376. m_vertices[3] = Stuff::Vector3D(0.0f, 0.0f, -1.0f);
  377. m_vertices[4] = Stuff::Vector3D(0.8660254f, 0.0f, -0.5f);
  378. m_vertices[5] = Stuff::Vector3D(0.8660254f, 0.0f, 0.5f);
  379. m_vertices[6] = Stuff::Vector3D(0.0f, 0.0f, 1.0f);
  380. m_uvs.SetLength(7);
  381. m_uvs[0] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.0f);
  382. m_uvs[1] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f/6.0f);
  383. m_uvs[2] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 2.0f/6.0f);
  384. m_uvs[3] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 0.5f);
  385. m_uvs[4] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 4.0f/6.0f);
  386. m_uvs[5] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 5.0f/6.0f);
  387. m_uvs[6] = Stuff::Vector2DOf<Stuff::Scalar>(0.0f, 1.0f);
  388. break;
  389. }
  390. }
  391. //------------------------------------------------------------------------------
  392. //
  393. bool
  394. gosFX::Tube__Specification::CalculateUBias(bool adjust)
  395. {
  396. Check_Object(this);
  397. //
  398. //----------------------------------
  399. // Calculate the worst case UV scale
  400. //----------------------------------
  401. //
  402. int max_index = m_maxProfileCount-1;
  403. Stuff::Scalar max_scale, min_scale;
  404. Retry:
  405. m_pUSize.ExpensiveComputeRange(&min_scale, &max_scale);
  406. Stuff::Scalar lower = min_scale * max_index;
  407. if (lower > 0.0f)
  408. lower = 0.0f;
  409. Stuff::Scalar upper = max_scale * max_index;
  410. //
  411. //------------------------------------
  412. // Calculate the worst case UV offsets
  413. //------------------------------------
  414. //
  415. Stuff::Scalar max_offset, min_offset;
  416. m_pUOffset.ExpensiveComputeRange(&min_offset, &max_offset);
  417. lower += min_offset;
  418. upper += max_offset;
  419. if (upper - lower >= 198.0f)
  420. {
  421. if (!adjust)
  422. return false;
  423. lower = max_offset - min_offset;
  424. Stuff::Scalar scale = static_cast<Stuff::Scalar>(floor((198.0f-lower)/max_index));
  425. m_pUSize.m_ageCurve.SetCurve(scale);
  426. m_pUSize.m_seeded = false;
  427. goto Retry;
  428. }
  429. lower += 99.0f;
  430. if (lower < 0.0f)
  431. {
  432. m_UBias = static_cast<Stuff::Scalar>(floor(0.5f-lower));
  433. return true;
  434. }
  435. upper -= 99.0f;
  436. if (upper > 0.0f)
  437. {
  438. m_UBias = static_cast<Stuff::Scalar>(floor(0.5f-upper));
  439. return true;
  440. }
  441. m_UBias = 0.0f;
  442. return true;
  443. }
  444. //############################################################################
  445. //############################ gosFX::Tube ###############################
  446. //############################################################################
  447. gosFX::Tube::ClassData*
  448. gosFX::Tube::DefaultData = NULL;
  449. //------------------------------------------------------------------------------
  450. //
  451. void
  452. gosFX::Tube::InitializeClass()
  453. {
  454. Verify(!DefaultData);
  455. Verify(gos_GetCurrentHeap() == Heap);
  456. DefaultData =
  457. new ClassData(
  458. TubeClassID,
  459. "gosFX::Tube",
  460. Effect::DefaultData,
  461. (Effect::Factory)&Make,
  462. (Specification::Factory)&Specification::Make
  463. );
  464. Register_Object(DefaultData);
  465. }
  466. //------------------------------------------------------------------------------
  467. //
  468. void
  469. gosFX::Tube::TerminateClass()
  470. {
  471. Unregister_Object(DefaultData);
  472. delete DefaultData;
  473. DefaultData = NULL;
  474. }
  475. //------------------------------------------------------------------------------
  476. //
  477. gosFX::Tube::Tube(
  478. Specification *spec,
  479. unsigned flags
  480. ):
  481. Effect(DefaultData, spec, flags)
  482. {
  483. Check_Pointer(this);
  484. Check_Object(spec);
  485. Verify(gos_GetCurrentHeap() == Heap);
  486. //
  487. //----------------------------------
  488. // Figure out how much space we need
  489. //----------------------------------
  490. //
  491. m_profiles.SetLength(spec->m_maxProfileCount);
  492. unsigned vertex_count = spec->m_vertices.GetLength();
  493. Verify(vertex_count < 8);
  494. unsigned index_count = (vertex_count - 1)*6;
  495. unsigned size =
  496. sizeof(Stuff::Point3D)
  497. + sizeof(Stuff::RGBAColor)
  498. + sizeof(Stuff::Vector2DOf<Stuff::Scalar>);
  499. size *= vertex_count*spec->m_maxProfileCount;
  500. size += sizeof(unsigned short)*(spec->m_maxProfileCount-1)*index_count;
  501. //
  502. //-----------------------
  503. // Allocate the tube mesh
  504. //-----------------------
  505. //
  506. gos_PushCurrentHeap(MidLevelRenderer::Heap);
  507. m_mesh =
  508. new MidLevelRenderer::MLRIndexedTriangleCloud(
  509. spec->m_maxProfileCount*(vertex_count-1)*2
  510. );
  511. Register_Object(m_mesh);
  512. gos_PopCurrentHeap();
  513. //
  514. //------------------------------------------------
  515. // Set up the data pointers into the channel block
  516. //------------------------------------------------
  517. //
  518. m_triangleCount = 0;
  519. m_vertexCount = 0;
  520. m_data.SetLength(size);
  521. m_P_vertices = Cast_Pointer(Stuff::Point3D*, &m_data[0]);
  522. size = spec->m_maxProfileCount*vertex_count*sizeof(Stuff::Point3D);
  523. m_P_colors = Cast_Pointer(Stuff::RGBAColor*, &m_data[size]);
  524. size += spec->m_maxProfileCount*vertex_count*sizeof(Stuff::RGBAColor);
  525. m_P_uvs = Cast_Pointer(Stuff::Vector2DOf<Stuff::Scalar>*, &m_data[size]);
  526. size += spec->m_maxProfileCount*vertex_count*sizeof(Stuff::Vector2DOf<Stuff::Scalar>);
  527. unsigned short *mesh_indices = Cast_Pointer(unsigned short*, &m_data[size]);
  528. m_mesh->SetData(
  529. &m_triangleCount,
  530. &m_vertexCount,
  531. mesh_indices,
  532. m_P_vertices,
  533. m_P_colors,
  534. m_P_uvs
  535. );
  536. BuildMesh(mesh_indices);
  537. //
  538. //-------------------------------
  539. // Set up an empty profile cloud
  540. //-------------------------------
  541. //
  542. m_activeProfileCount = 0;
  543. m_headProfile = -1;
  544. m_tailProfile = -1;
  545. m_birthAccumulator = 0.0f;
  546. }
  547. //------------------------------------------------------------------------------
  548. //
  549. void
  550. gosFX::Tube::BuildMesh(unsigned short *indices)
  551. {
  552. Check_Object(this);
  553. Check_Pointer(indices);
  554. //
  555. //--------------------------------------
  556. // Figure out the parameters of the mesh
  557. //--------------------------------------
  558. //
  559. Specification *spec = GetSpecification();
  560. Check_Object(spec);
  561. unsigned vertex_count = spec->m_vertices.GetLength();
  562. Verify(vertex_count < 8);
  563. //
  564. //-------------------------------------------------
  565. // Crosses are built different from everything else
  566. //-------------------------------------------------
  567. //
  568. if (spec->m_profileType != Specification::e_Cross)
  569. {
  570. for (unsigned short profile=0; profile<spec->m_maxProfileCount-1; ++profile)
  571. {
  572. unsigned short base = static_cast<short>(profile * vertex_count);
  573. for (unsigned short panel=0; panel<vertex_count-1; ++panel)
  574. {
  575. if (spec->m_insideOut)
  576. {
  577. *indices++ = static_cast<short>(base+panel+1);
  578. *indices++ = static_cast<short>(base+panel);
  579. *indices++ = static_cast<short>(base+panel+vertex_count+1);
  580. *indices++ = static_cast<short>(base+panel+vertex_count);
  581. *indices++ = static_cast<short>(base+panel+vertex_count+1);
  582. *indices++ = static_cast<short>(base+panel);
  583. }
  584. else
  585. {
  586. *indices++ = static_cast<short>(base+panel);
  587. *indices++ = static_cast<short>(base+panel+1);
  588. *indices++ = static_cast<short>(base+panel+vertex_count+1);
  589. *indices++ = static_cast<short>(base+panel+vertex_count+1);
  590. *indices++ = static_cast<short>(base+panel+vertex_count);
  591. *indices++ = static_cast<short>(base+panel);
  592. }
  593. }
  594. }
  595. }
  596. else
  597. {
  598. Verify(vertex_count==5);
  599. for (unsigned short profile=0; profile<spec->m_maxProfileCount-1; ++profile)
  600. {
  601. unsigned short base = static_cast<short>(profile * vertex_count);
  602. for (unsigned short panel=0; panel<4; ++panel)
  603. {
  604. if (spec->m_insideOut)
  605. {
  606. *indices++ = static_cast<short>(base+panel+1);
  607. *indices++ = base;
  608. *indices++ = static_cast<short>(base+panel+6);
  609. *indices++ = static_cast<short>(base+5);
  610. *indices++ = static_cast<short>(base+panel+6);
  611. *indices++ = base;
  612. }
  613. else
  614. {
  615. *indices++ = base;
  616. *indices++ = static_cast<short>(base+panel+1);
  617. *indices++ = static_cast<short>(base+panel+6);
  618. *indices++ = static_cast<short>(base+panel+6);
  619. *indices++ = static_cast<short>(base+5);
  620. *indices++ = base;
  621. }
  622. }
  623. }
  624. }
  625. }
  626. //------------------------------------------------------------------------------
  627. //
  628. gosFX::Tube::~Tube()
  629. {
  630. Unregister_Object(m_mesh);
  631. delete m_mesh;
  632. }
  633. //------------------------------------------------------------------------------
  634. //
  635. gosFX::Tube*
  636. gosFX::Tube::Make(
  637. Specification *spec,
  638. unsigned flags
  639. )
  640. {
  641. Check_Object(spec);
  642. gos_PushCurrentHeap(Heap);
  643. Tube *tube = new gosFX::Tube(spec, flags);
  644. gos_PopCurrentHeap();
  645. return tube;
  646. }
  647. //------------------------------------------------------------------------------
  648. //
  649. void
  650. gosFX::Tube::Start(ExecuteInfo *info)
  651. {
  652. Check_Object(this);
  653. Check_Pointer(info);
  654. //
  655. //----------------------
  656. // Let effect initialize
  657. //----------------------
  658. //
  659. Effect::Start(info);
  660. //
  661. //--------------------------------------------------------------------------
  662. // If the effect is off, we will create two profiles. If they effect is on,
  663. // we just keep doing what we do
  664. //--------------------------------------------------------------------------
  665. //
  666. m_birthAccumulator = 1.0f;
  667. }
  668. //------------------------------------------------------------------------------
  669. //
  670. bool gosFX::Tube::Execute(ExecuteInfo *info)
  671. {
  672. Check_Object(this);
  673. Check_Object(info);
  674. //
  675. //----------------------------------------
  676. // If we aren't supposed to execute, don't
  677. //----------------------------------------
  678. //
  679. if (!IsExecuted())
  680. return false;
  681. //
  682. //----------------------------------------------------------------------
  683. // Update the head of the tube to the current location, then compute the
  684. // inverse transformation to keep all particles in their place of origin
  685. //----------------------------------------------------------------------
  686. //
  687. Stuff::LinearMatrix4D new_world_to_local;
  688. Stuff::LinearMatrix4D local_to_world;
  689. local_to_world.Multiply(m_localToParent, *info->m_parentToWorld);
  690. new_world_to_local.Invert(local_to_world);
  691. //
  692. //--------------------------
  693. // Figure out the birth rate
  694. //--------------------------
  695. //
  696. Specification *spec = GetSpecification();
  697. Check_Object(spec);
  698. Stuff::Scalar dT =
  699. static_cast<Stuff::Scalar>(info->m_time - m_lastRan);
  700. Verify(dT >= 0.0f);
  701. Stuff::Scalar prev_age = m_age;
  702. m_age += dT * m_ageRate;
  703. if (m_age >= 1.0f)
  704. m_birthAccumulator = 0.0f;
  705. else
  706. {
  707. Stuff::Scalar new_life =
  708. spec->m_profilesPerSecond.ComputeValue(m_age, m_seed);
  709. Min_Clamp(new_life, 0.0f);
  710. m_birthAccumulator += dT * new_life;
  711. //
  712. //------------------------------------------------------------------
  713. // If it is time for a new child and there is room, move the head of
  714. // the tube and create the new profile, then clear out the integer
  715. // part of the accumulator - stacking up would look stupid
  716. //------------------------------------------------------------------
  717. //
  718. if (m_birthAccumulator >= 1.0f && m_activeProfileCount < spec->m_maxProfileCount)
  719. {
  720. if (!m_activeProfileCount)
  721. {
  722. m_headProfile = 1;
  723. m_tailProfile = spec->m_maxProfileCount-1;
  724. CreateNewProfile(0, local_to_world);
  725. m_activeProfileCount = 2;
  726. }
  727. else
  728. {
  729. m_profiles[m_headProfile].m_profileToWorld = local_to_world;
  730. if (++m_headProfile == spec->m_maxProfileCount)
  731. m_headProfile = 0;
  732. }
  733. CreateNewProfile(m_headProfile, local_to_world);
  734. m_birthAccumulator -= static_cast<Stuff::Scalar>(floor(m_birthAccumulator));
  735. }
  736. else
  737. m_profiles[m_headProfile].m_profileToWorld = local_to_world;
  738. }
  739. //
  740. //--------------------------------------------------------------------
  741. // If we don't have any profiles, just execute the children and return
  742. //--------------------------------------------------------------------
  743. //
  744. if (!m_activeProfileCount)
  745. {
  746. m_age = prev_age;
  747. return Effect::Execute(info);
  748. }
  749. //
  750. //----------------------------------
  751. // Deal with all the active profiles
  752. //----------------------------------
  753. //
  754. Stuff::ExtentBox box(Stuff::Point3D::Identity, Stuff::Point3D::Identity);
  755. int i = m_headProfile;
  756. int profile_count = 0;
  757. Verify(i >= 0);
  758. do
  759. {
  760. //
  761. //----------------------------------------------------------------------
  762. // Age and animate the profile. If the profile should die, it becomes
  763. // the end of the tail
  764. //----------------------------------------------------------------------
  765. //
  766. Profile *profile = GetProfile(i);
  767. Check_Object(profile);
  768. Verify (profile->m_age < 1.0f);
  769. profile->m_age += dT*profile->m_ageRate;
  770. Stuff::Sphere bounds;
  771. if (!AnimateProfile(i, profile_count, new_world_to_local, info->m_time, &bounds))
  772. {
  773. m_tailProfile = i;
  774. break;
  775. }
  776. //
  777. //---------------------
  778. // Deal with the bounds
  779. //---------------------
  780. //
  781. if (i == m_headProfile)
  782. {
  783. box.maxX = bounds.center.x + bounds.radius;
  784. box.minX = bounds.center.x - bounds.radius;
  785. box.maxY = bounds.center.y + bounds.radius;
  786. box.minY = bounds.center.y - bounds.radius;
  787. box.maxZ = bounds.center.z + bounds.radius;
  788. box.minZ = bounds.center.z - bounds.radius;
  789. }
  790. else
  791. {
  792. Stuff::ExtentBox local_box;
  793. local_box.maxX = bounds.center.x + bounds.radius;
  794. local_box.minX = bounds.center.x - bounds.radius;
  795. local_box.maxY = bounds.center.y + bounds.radius;
  796. local_box.minY = bounds.center.y - bounds.radius;
  797. local_box.maxZ = bounds.center.z + bounds.radius;
  798. local_box.minZ = bounds.center.z - bounds.radius;
  799. box.Union(box, local_box);
  800. }
  801. //
  802. //---------------------------------------------------------------------------
  803. // Move to the previous profile and wrap to the end of the list if necessary
  804. //---------------------------------------------------------------------------
  805. //
  806. ++profile_count;
  807. if (--i < 0)
  808. i = spec->m_maxProfileCount-1;
  809. } while (i != m_tailProfile);
  810. m_activeProfileCount = profile_count;
  811. //
  812. //-----------------------------------------------
  813. // Put the age back and run the base effect stuff
  814. //-----------------------------------------------
  815. //
  816. m_age = prev_age;
  817. if (!Effect::Execute(info))
  818. return false;
  819. //
  820. //--------------------------------------------
  821. // Now, build a info->m_bounds around this box
  822. //--------------------------------------------
  823. //
  824. Verify(box.maxX >= box.minX);
  825. Verify(box.maxY >= box.minY);
  826. Verify(box.maxZ >= box.minZ);
  827. Stuff::OBB local_bounds = Stuff::OBB::Identity;
  828. local_bounds.axisExtents.x = 0.5f * (box.maxX - box.minX);
  829. local_bounds.axisExtents.y = 0.5f * (box.maxY - box.minY);
  830. local_bounds.axisExtents.z = 0.5f * (box.maxZ - box.minZ);
  831. local_bounds.localToParent(3,0) = box.minX + local_bounds.axisExtents.x;
  832. local_bounds.localToParent(3,1) = box.minY + local_bounds.axisExtents.y;
  833. local_bounds.localToParent(3,2) = box.minZ + local_bounds.axisExtents.z;
  834. local_bounds.sphereRadius = local_bounds.axisExtents.GetLength();
  835. Stuff::OBB parent_bounds;
  836. parent_bounds.Multiply(local_bounds, m_localToParent);
  837. info->m_bounds->Union(*info->m_bounds, parent_bounds);
  838. return true;
  839. }
  840. //------------------------------------------------------------------------------
  841. //
  842. void gosFX::Tube::Kill()
  843. {
  844. Check_Object(this);
  845. //
  846. //-------------------------------------------------------------
  847. // Destroy all the profiles and set up an empty profile cloud
  848. //-------------------------------------------------------------
  849. //
  850. Specification *spec = GetSpecification();
  851. Check_Object(spec);
  852. if (m_activeProfileCount>0)
  853. {
  854. do
  855. {
  856. DestroyProfile(m_headProfile--);
  857. if (m_headProfile<0)
  858. m_headProfile = spec->m_maxProfileCount-1;
  859. } while (m_headProfile != m_tailProfile);
  860. }
  861. m_activeProfileCount = 0;
  862. m_headProfile = -1;
  863. m_tailProfile = -1;
  864. m_birthAccumulator = 0.0f;
  865. //
  866. //----------------------------------------
  867. // Now let the base effect handle stopping
  868. //----------------------------------------
  869. //
  870. Effect::Kill();
  871. }
  872. //------------------------------------------------------------------------------
  873. //
  874. bool gosFX::Tube::HasFinished()
  875. {
  876. Check_Object(this);
  877. return Effect::HasFinished() && (m_activeProfileCount == 0);
  878. }
  879. //------------------------------------------------------------------------------
  880. //
  881. void
  882. gosFX::Tube::CreateNewProfile(
  883. unsigned index,
  884. const Stuff::LinearMatrix4D &origin
  885. )
  886. {
  887. Check_Object(this);
  888. Check_Object(&origin);
  889. //
  890. //----------------------------------------------------
  891. // Figure out the age and age rate of the new profile
  892. //----------------------------------------------------
  893. //
  894. Specification *spec = GetSpecification();
  895. Check_Object(spec);
  896. Profile *profile = GetProfile(index);
  897. Check_Object(profile);
  898. profile->m_age = 0.0f;
  899. Stuff::Scalar min_seed =
  900. spec->m_minimumChildSeed.ComputeValue(m_age, m_seed);
  901. Stuff::Scalar seed_range =
  902. spec->m_maximumChildSeed.ComputeValue(m_age, m_seed) - min_seed;
  903. Stuff::Scalar seed =
  904. Stuff::Random::GetFraction()*seed_range + min_seed;
  905. Clamp(seed, 0.0f, 1.0f);
  906. profile->m_seed = seed;
  907. Stuff::Scalar lifetime =
  908. spec->m_pLifeSpan.ComputeValue(m_age, seed);
  909. Min_Clamp(lifetime, 0.0333333f);
  910. profile->m_ageRate = 1.0f / lifetime;
  911. //
  912. //------------------------------------------------------------------
  913. // Establish the base position and figure out the direction of drift
  914. //------------------------------------------------------------------
  915. //
  916. profile->m_profileToWorld = origin;
  917. Stuff::Scalar pitch_min =
  918. spec->m_minimumDeviation.ComputeValue(m_age, seed);
  919. Stuff::Scalar pitch_range =
  920. spec->m_maximumDeviation.ComputeValue(m_age, seed) - pitch_min;
  921. if (pitch_range < 0.0f)
  922. pitch_range = 0.0f;
  923. Stuff::Radian angle = pitch_min + Stuff::Random::GetFraction() * pitch_range;
  924. Stuff::SinCosPair xy(angle);
  925. profile->m_direction.x = (Stuff::Random::GetFraction() >= 0.5f) ? xy.sine : -xy.sine;
  926. profile->m_direction.y = 0.0f;
  927. profile->m_direction.z = xy.cosine;
  928. Check_Object(&profile->m_direction);
  929. }
  930. //------------------------------------------------------------------------------
  931. //
  932. bool
  933. gosFX::Tube::AnimateProfile(
  934. unsigned index,
  935. unsigned profile_index,
  936. const Stuff::LinearMatrix4D &world_to_new_local,
  937. Stuff::Time till,
  938. Stuff::Sphere *bounds
  939. )
  940. {
  941. Check_Object(this);
  942. //
  943. //----------------------------------------------------
  944. // If the profile gets too old, don't do anything else
  945. //----------------------------------------------------
  946. //
  947. Profile *profile = GetProfile(index);
  948. Check_Object(profile);
  949. Stuff::Scalar age = profile->m_age;
  950. if (age >= 1.0f)
  951. return false;
  952. //
  953. //--------------------------------------------------------------------
  954. // Figure out the scale and displacement of the profile in world space
  955. //--------------------------------------------------------------------
  956. //
  957. Set_Statistic(Profile_Count, Profile_Count+1);
  958. Stuff::Scalar seed = profile->m_seed;
  959. Specification *spec = GetSpecification();
  960. Check_Object(spec);
  961. Stuff::Scalar scale = spec->m_pScale.ComputeValue(age, seed);
  962. Verify(scale >= 0.0f);
  963. Stuff::Scalar disp = spec->m_pDisplacement.ComputeValue(age, seed);
  964. Stuff::Point3D offset;
  965. offset.Multiply(profile->m_direction, disp);
  966. //
  967. //-------------------------------------------------
  968. // Now build the template to new local space matrix
  969. //-------------------------------------------------
  970. //
  971. Stuff::AffineMatrix4D template_to_profile(true);
  972. template_to_profile(0,0) = scale;
  973. template_to_profile(1,1) = scale;
  974. template_to_profile(2,2) = scale;
  975. template_to_profile.BuildTranslation(offset);
  976. Stuff::AffineMatrix4D template_to_world;
  977. template_to_world.Multiply(template_to_profile, profile->m_profileToWorld);
  978. Stuff::AffineMatrix4D template_to_new_local;
  979. template_to_new_local.Multiply(template_to_world, world_to_new_local);
  980. bounds->center = template_to_new_local;
  981. bounds->radius = scale;
  982. //
  983. //------------------------------------------------------------------------
  984. // Now we just multiply the template through to the vertices unless we are
  985. // doing the aligned ribbon
  986. //------------------------------------------------------------------------
  987. //
  988. unsigned i;
  989. unsigned vertex_count = spec->m_vertices.GetLength();
  990. Verify(vertex_count < 8);
  991. unsigned vertex_index = profile_index * vertex_count;
  992. if (spec->m_profileType != Specification::e_AlignedRibbon)
  993. {
  994. Check_Pointer(m_P_vertices);
  995. for (i=0; i<vertex_count; ++i)
  996. {
  997. m_P_vertices[vertex_index+i].Multiply(
  998. spec->m_vertices[i],
  999. template_to_new_local
  1000. );
  1001. }
  1002. }
  1003. //
  1004. //------------------------------
  1005. // Figure out the UV adjustments
  1006. //------------------------------
  1007. //
  1008. Check_Pointer(m_P_uvs);
  1009. Stuff::Scalar u = spec->m_pUOffset.ComputeValue(age, seed);
  1010. Stuff::Scalar v = spec->m_pVOffset.ComputeValue(age, seed);
  1011. Stuff::Scalar u2 = spec->m_pUSize.ComputeValue(age, seed);
  1012. Stuff::Scalar v2 = spec->m_pVSize.ComputeValue(age, seed);
  1013. u += u2*profile_index + spec->m_UBias;
  1014. for (i=0; i<vertex_count; ++i)
  1015. {
  1016. m_P_uvs[vertex_index+i].x = u;
  1017. m_P_uvs[vertex_index+i].y = spec->m_uvs[i].y*v2 + v;
  1018. }
  1019. //
  1020. //---------------------------
  1021. // Lastly, animate the colors
  1022. //---------------------------
  1023. //
  1024. Check_Pointer(m_P_colors);
  1025. Stuff::RGBAColor color;
  1026. color.red = spec->m_pRed.ComputeValue(age, seed);
  1027. color.green = spec->m_pGreen.ComputeValue(age, seed);
  1028. color.blue = spec->m_pBlue.ComputeValue(age, seed);
  1029. color.alpha = spec->m_pAlpha.ComputeValue(age, seed);
  1030. for (i=0; i<vertex_count; ++i)
  1031. m_P_colors[vertex_index+i] = color;
  1032. return true;
  1033. }
  1034. //------------------------------------------------------------------------------
  1035. //
  1036. void gosFX::Tube::DestroyProfile(unsigned index)
  1037. {
  1038. Profile *profile = GetProfile(index);
  1039. Check_Object(profile);
  1040. profile->m_age = 1.0f;
  1041. }
  1042. //------------------------------------------------------------------------------
  1043. //
  1044. void gosFX::Tube::Draw(DrawInfo *info)
  1045. {
  1046. Check_Object(this);
  1047. Check_Object(info);
  1048. //
  1049. //---------------------------------------------------------
  1050. // If we have active particles, set up the draw information
  1051. //---------------------------------------------------------
  1052. //
  1053. if (m_activeProfileCount>1)
  1054. {
  1055. MidLevelRenderer::DrawEffectInformation dInfo;
  1056. dInfo.effect = m_mesh;
  1057. Specification *spec = GetSpecification();
  1058. Check_Object(spec);
  1059. dInfo.state.Combine(info->m_state, spec->m_state);
  1060. dInfo.clippingFlags = info->m_clippingFlags;
  1061. Stuff::LinearMatrix4D local_to_world;
  1062. local_to_world.Multiply(m_localToParent, *info->m_parentToWorld);
  1063. dInfo.effectToWorld = &local_to_world;
  1064. unsigned vertex_count = spec->m_vertices.GetLength();
  1065. m_vertexCount = m_activeProfileCount * vertex_count;
  1066. m_triangleCount = 2 * (m_activeProfileCount-1) * (vertex_count-1);
  1067. //
  1068. //-------------------------------------------------------------------
  1069. // If we are doing the aligned ribbon, we will have to orient each of
  1070. // the profiles and then compute its vertex positions accordingly
  1071. //-------------------------------------------------------------------
  1072. //
  1073. if (spec->m_profileType == Specification::e_AlignedRibbon)
  1074. {
  1075. int i = m_headProfile;
  1076. int vertex = 0;
  1077. unsigned vertex_count = spec->m_vertices.GetLength();
  1078. Verify(vertex_count < 8);
  1079. Stuff::Point3D
  1080. camera_in_world(info->m_clipper->GetCameraToWorldMatrix());
  1081. Stuff::LinearMatrix4D world_to_local;
  1082. world_to_local.Invert(local_to_world);
  1083. //
  1084. //----------------------------------------------------------------
  1085. // go thru all the profiles and figure out where they are in local
  1086. // space
  1087. //----------------------------------------------------------------
  1088. //
  1089. Verify(i >= 0);
  1090. do
  1091. {
  1092. Profile *profile = GetProfile(i);
  1093. Check_Object(profile);
  1094. Stuff::Point3D camera_in_profile;
  1095. camera_in_profile.MultiplyByInverse(camera_in_world, profile->m_profileToWorld);
  1096. //
  1097. //---------------------------------------------------------
  1098. // Figure out the scale and displacement of the template in
  1099. // profile space
  1100. //---------------------------------------------------------
  1101. //
  1102. Stuff::Scalar age = profile->m_age;
  1103. Stuff::Scalar seed = profile->m_seed;
  1104. Stuff::Scalar scale = spec->m_pScale.ComputeValue(age, seed);
  1105. Stuff::Scalar disp = spec->m_pDisplacement.ComputeValue(age, seed);
  1106. Stuff::Point3D offset_in_profile;
  1107. offset_in_profile.Multiply(profile->m_direction, disp);
  1108. //
  1109. //----------------------------------------------------------
  1110. // Figure out the direction that we want the profile to face
  1111. // and build a rotation vector that does it
  1112. //----------------------------------------------------------
  1113. //
  1114. Stuff::Vector3D direction;
  1115. direction.Subtract(camera_in_profile, offset_in_profile);
  1116. Stuff::LinearMatrix4D template_rotation(true);
  1117. template_rotation.AlignLocalAxisToWorldVector(
  1118. direction,
  1119. Stuff::Z_Axis,
  1120. Stuff::Y_Axis,
  1121. -1
  1122. );
  1123. //
  1124. //-------------------------------------------------
  1125. // Now build the template to new local space matrix
  1126. //-------------------------------------------------
  1127. //
  1128. Stuff::AffineMatrix4D template_to_profile;
  1129. template_to_profile(0,0) = scale*template_rotation(0,0);
  1130. template_to_profile(0,1) = scale*template_rotation(0,1);
  1131. template_to_profile(0,2) = scale*template_rotation(0,2);
  1132. template_to_profile(1,0) = scale*template_rotation(1,0);
  1133. template_to_profile(1,1) = scale*template_rotation(1,1);
  1134. template_to_profile(1,2) = scale*template_rotation(1,2);
  1135. template_to_profile(2,0) = scale*template_rotation(2,0);
  1136. template_to_profile(2,1) = scale*template_rotation(2,1);
  1137. template_to_profile(2,2) = scale*template_rotation(2,2);
  1138. template_to_profile.BuildTranslation(offset_in_profile);
  1139. Stuff::AffineMatrix4D template_to_world;
  1140. template_to_world.Multiply(template_to_profile, profile->m_profileToWorld);
  1141. Stuff::AffineMatrix4D template_to_local;
  1142. template_to_local.Multiply(template_to_world, world_to_local);
  1143. //
  1144. //------------------------------------
  1145. // Multiply the points thru the matrix
  1146. //------------------------------------
  1147. //
  1148. for (int v=0; v<vertex_count; ++v)
  1149. {
  1150. m_P_vertices[vertex++].Multiply(
  1151. spec->m_vertices[v],
  1152. template_to_local
  1153. );
  1154. }
  1155. //
  1156. //---------------------------------------------------------------------------
  1157. // Move to the previous profile and wrap to the end of the list if necessary
  1158. //---------------------------------------------------------------------------
  1159. //
  1160. if (--i < 0)
  1161. i = spec->m_maxProfileCount-1;
  1162. } while (i != m_tailProfile);
  1163. }
  1164. //
  1165. //--------------------
  1166. // Now draw the effect
  1167. //--------------------
  1168. //
  1169. info->m_clipper->DrawEffect(&dInfo);
  1170. }
  1171. Effect::Draw(info);
  1172. }
  1173. //------------------------------------------------------------------------------
  1174. //
  1175. void
  1176. gosFX::Tube::TestInstance() const
  1177. {
  1178. Verify(IsDerivedFrom(DefaultData));
  1179. }