part_buf.cpp 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539
  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 : G *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/part_buf.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 9/07/01 12:57p $*
  29. * *
  30. * $Revision:: 20 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "part_buf.h"
  36. #include "part_emt.h"
  37. #include "ww3d.h"
  38. #include "rinfo.h"
  39. #include "scene.h"
  40. #include "camera.h"
  41. #include "predlod.h"
  42. #include "pot.h"
  43. #include "bound.h"
  44. #include "simplevec.h"
  45. #include "sphere.h"
  46. #include "wwprofile.h"
  47. #include <limits.h>
  48. #include "vp.h"
  49. #include "texture.h"
  50. #include "dx8wrapper.h"
  51. #include "vector3.h"
  52. // A random permutation of the numbers 0 to 15 - used for LOD particle decimation.
  53. // It was generated by the amazingly high-tech method of pulling numbers out of a hat.
  54. const unsigned int ParticleBufferClass::PermutationArray[16] = {
  55. 11, 3, 7, 14, 0, 13, 1, 2, 5, 12, 15, 6, 9, 8, 4, 10
  56. };
  57. // Maximum size of randomizer tables
  58. const static unsigned int MAX_RANDOM_ENTRIES = 32; // MUST be power of two!
  59. // Total Active Particle Buffer Count
  60. unsigned int ParticleBufferClass::TotalActiveCount = 0;
  61. // Static array of screen-size clamps for the 17 possible LOD levels a particle buffer can have.
  62. // We can change these from being global to being per-buffer later if we wish. Default is
  63. // NO_MAX_SCREEN_SIZE.
  64. float ParticleBufferClass::LODMaxScreenSizes[17] = {
  65. NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
  66. NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
  67. NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
  68. NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
  69. NO_MAX_SCREEN_SIZE
  70. };
  71. static Random4Class rand_gen;
  72. const float oo_intmax = 1.0f / (float)INT_MAX;
  73. // Default Line Emitter Properties
  74. static const W3dEmitterLinePropertiesStruct _DefaultLineEmitterProps=
  75. { 0,0,0.0f,1.5f,1.0f,0.0f,0.0f,0,0,0,0,0,0,0,0,0 };
  76. ParticleBufferClass::ParticleBufferClass
  77. (
  78. ParticleEmitterClass *emitter,
  79. unsigned int buffer_size,
  80. ParticlePropertyStruct<Vector3> &color,
  81. ParticlePropertyStruct<float> &opacity,
  82. ParticlePropertyStruct<float> &size,
  83. ParticlePropertyStruct<float> &rotation,
  84. float orient_rnd,
  85. ParticlePropertyStruct<float> &frame,
  86. ParticlePropertyStruct<float> &blurtime,
  87. Vector3 accel,
  88. float max_age,
  89. float future_start,
  90. TextureClass *tex,
  91. ShaderClass shader,
  92. bool pingpong,
  93. int render_mode,
  94. int frame_mode,
  95. const W3dEmitterLinePropertiesStruct * line_props
  96. ) :
  97. NewParticleQueue(NULL),
  98. NewParticleQueueStart(0U),
  99. NewParticleQueueEnd(0U),
  100. NewParticleQueueCount(0U),
  101. RenderMode(render_mode),
  102. FrameMode(frame_mode),
  103. MaxAge(1000.0f * max_age),
  104. FutureStartTime(1000.0f * future_start),
  105. LastUpdateTime(WW3D::Get_Sync_Time()),
  106. IsEmitterDead(false),
  107. MaxSize(0.0f),
  108. MaxNum(buffer_size),
  109. Start(0U),
  110. End(0U),
  111. NewEnd(0U),
  112. NonNewNum(0),
  113. NewNum(0),
  114. BoundingBox(Vector3(0,0,0),Vector3(0,0,0)),
  115. BoundingBoxDirty(true),
  116. NumColorKeyFrames(0),
  117. ColorKeyFrameTimes(NULL),
  118. ColorKeyFrameValues(NULL),
  119. ColorKeyFrameDeltas(NULL),
  120. NumAlphaKeyFrames(0),
  121. AlphaKeyFrameTimes(NULL),
  122. AlphaKeyFrameValues(NULL),
  123. AlphaKeyFrameDeltas(NULL),
  124. NumSizeKeyFrames(0),
  125. SizeKeyFrameTimes(NULL),
  126. SizeKeyFrameValues(NULL),
  127. SizeKeyFrameDeltas(NULL),
  128. NumRotationKeyFrames(0),
  129. RotationKeyFrameTimes(NULL),
  130. RotationKeyFrameValues(NULL),
  131. HalfRotationKeyFrameDeltas(NULL),
  132. OrientationKeyFrameValues(NULL),
  133. NumFrameKeyFrames(0),
  134. FrameKeyFrameTimes(NULL),
  135. FrameKeyFrameValues(NULL),
  136. FrameKeyFrameDeltas(NULL),
  137. NumBlurTimeKeyFrames(0),
  138. BlurTimeKeyFrameTimes(NULL),
  139. BlurTimeKeyFrameValues(NULL),
  140. BlurTimeKeyFrameDeltas(NULL),
  141. NumRandomColorEntriesMinus1(0),
  142. RandomColorEntries(NULL),
  143. NumRandomAlphaEntriesMinus1(0),
  144. RandomAlphaEntries(NULL),
  145. NumRandomSizeEntriesMinus1(0),
  146. RandomSizeEntries(NULL),
  147. ColorRandom(0, 0, 0),
  148. OpacityRandom(0),
  149. SizeRandom(0),
  150. RotationRandom(0),
  151. FrameRandom(0),
  152. InitialOrientationRandom(0),
  153. NumRandomRotationEntriesMinus1(0),
  154. RandomRotationEntries(NULL),
  155. NumRandomOrientationEntriesMinus1(0),
  156. RandomOrientationEntries(NULL),
  157. NumRandomFrameEntriesMinus1(0),
  158. RandomFrameEntries(NULL),
  159. NumRandomBlurTimeEntriesMinus1(0),
  160. RandomBlurTimeEntries(NULL),
  161. PointGroup(NULL),
  162. LineRenderer(NULL),
  163. LineGroup(NULL),
  164. Diffuse(NULL),
  165. TailDiffuse(NULL),
  166. Color(NULL),
  167. Alpha(NULL),
  168. Size(NULL),
  169. Orientation(NULL),
  170. Frame(NULL),
  171. UCoord(NULL),
  172. TailPosition(NULL),
  173. APT(NULL),
  174. GroupID(NULL),
  175. PingPongPosition(pingpong),
  176. Velocity(NULL),
  177. TimeStamp(NULL),
  178. Emitter(emitter),
  179. DecimationThreshold(0U),
  180. ProjectedArea(0.0f),
  181. DefaultTailDiffuse(0,0,0,0),
  182. CurrentGroupID(0)
  183. {
  184. LodCount = 17;
  185. LodBias = 1.0f;
  186. Position[0] = NULL;
  187. Position[1] = NULL;
  188. // Create color array, keyframes and randomizer table (if needed)
  189. Reset_Colors(color);
  190. // Create alpha array, keyframes and randomizer table (if needed)
  191. Reset_Opacity(opacity);
  192. // Create size array, keyframes and randomizer table (if needed)
  193. Reset_Size(size);
  194. // Create the rotation array, keyframes, and randomizer table (if needed)
  195. Reset_Rotations(rotation, orient_rnd);
  196. // Create the frame array, keyframes, and randomizer table (if needed)
  197. Reset_Frames(frame);
  198. // Create the blur time array, keyframes, and randomizer table if needed
  199. Reset_Blur_Times(blurtime);
  200. // We do not add a ref for the emitter (see DTor for detailed explanation)
  201. // if (Emitter) Emitter->Add_Ref();
  202. // Set up new particle queue:
  203. NewParticleQueue = W3DNEWARRAY NewParticleStruct[MaxNum];
  204. // These inputs don't need to be range-checked (emitter did that).
  205. Accel = accel;
  206. HasAccel = (accel.X != 0.0f) || (accel.Y != 0.0f) || (accel.Z != 0.0f);
  207. shader.Enable_Fog ("ParticleBufferClass");
  208. switch (RenderMode)
  209. {
  210. case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
  211. {
  212. // Set up worldspace point group
  213. PointGroup = W3DNEW PointGroupClass();
  214. PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
  215. PointGroup->Set_Texture(tex);
  216. PointGroup->Set_Shader(shader);
  217. PointGroup->Set_Frame_Row_Column_Count_Log2(frame_mode);
  218. PointGroup->Set_Point_Mode(PointGroupClass::TRIS);
  219. }
  220. break;
  221. case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
  222. {
  223. // Set up worldspace point group
  224. PointGroup = W3DNEW PointGroupClass();
  225. PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
  226. PointGroup->Set_Texture(tex);
  227. PointGroup->Set_Shader(shader);
  228. PointGroup->Set_Frame_Row_Column_Count_Log2(frame_mode);
  229. PointGroup->Set_Point_Mode(PointGroupClass::QUADS);
  230. }
  231. break;
  232. case W3D_EMITTER_RENDER_MODE_LINE:
  233. {
  234. LineRenderer = W3DNEW SegLineRendererClass;
  235. LineRenderer->Init(*line_props);
  236. LineRenderer->Set_Texture(tex);
  237. LineRenderer->Set_Shader(shader);
  238. LineRenderer->Set_Width(Get_Particle_Size());
  239. if (line_props != NULL) {
  240. LineRenderer->Init(*line_props);
  241. } else {
  242. // This code should not be run, but if it does,
  243. // set line emitters to some reasonable value so
  244. // it doesn't crash
  245. WWASSERT(0);
  246. LineRenderer->Init(_DefaultLineEmitterProps);
  247. }
  248. }
  249. break;
  250. case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
  251. {
  252. LineGroup=W3DNEW LineGroupClass();
  253. LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
  254. LineGroup->Set_Texture(tex);
  255. LineGroup->Set_Shader(shader);
  256. LineGroup->Set_Line_Mode(LineGroupClass::TETRAHEDRON);
  257. TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition" ) );
  258. // TODO: Change TailPosition to Kinematic state and add
  259. // tail positions to bounding box
  260. Set_Force_Visible(1);
  261. }
  262. break;
  263. case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
  264. {
  265. LineGroup=W3DNEW LineGroupClass();
  266. LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
  267. LineGroup->Set_Texture(tex);
  268. LineGroup->Set_Shader(shader);
  269. LineGroup->Set_Line_Mode(LineGroupClass::PRISM);
  270. TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition" ) );
  271. // TODO: Change TailPosition to Kinematic state and add
  272. // tail positions to bounding box
  273. Set_Force_Visible(1);
  274. }
  275. break;
  276. default:
  277. WWASSERT(0);
  278. break;
  279. }
  280. // Set up circular buffer. Contents are not initialized because the
  281. // start/end indices currently indicate the buffer is empty.
  282. Position[0] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
  283. if (PingPongPosition) {
  284. Position[1] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
  285. }
  286. APT = NEW_REF( ShareBufferClass<unsigned int> , (MaxNum, "ParticleBufferClass::APT") );
  287. GroupID = NEW_REF( ShareBufferClass<unsigned char> , (MaxNum, "ParticleBufferClass::GroupID") );
  288. Velocity = W3DNEWARRAY Vector3[MaxNum];
  289. TimeStamp = W3DNEWARRAY unsigned int[MaxNum];
  290. // So that the object is ready for use after construction, we will
  291. // complete its initialization by initializing its cost and value arrays
  292. // according to a screen area of 1.
  293. int minlod = Calculate_Cost_Value_Arrays(1.0f, Value, Cost);
  294. // Ensure lod is no less than minimum allowed
  295. if (Get_LOD_Level() < minlod) Set_LOD_Level(minlod);
  296. // Update Global Count
  297. TotalActiveCount++;
  298. //lorenzen
  299. // If the render mode is W3D_EMITTER_RENDER_MODE_LINE and we are supplied with
  300. // a line properties structure, set up a line renderer
  301. if (RenderMode == W3D_EMITTER_RENDER_MODE_LINE) {
  302. if (line_props != NULL) {
  303. LineRenderer = W3DNEW SegLineRendererClass;
  304. LineRenderer->Init(*line_props);
  305. LineRenderer->Set_Texture(tex);
  306. LineRenderer->Set_Shader(shader);
  307. LineRenderer->Set_Width(Get_Particle_Size());
  308. } else {
  309. // We were in line mode but didn't get any line properties, drop back to triangles
  310. RenderMode = W3D_EMITTER_RENDER_MODE_TRI_PARTICLES;
  311. }
  312. }
  313. }
  314. ParticleBufferClass::ParticleBufferClass(const ParticleBufferClass & src) :
  315. RenderObjClass(src),
  316. NewParticleQueue(NULL),
  317. NewParticleQueueStart(0U),
  318. NewParticleQueueEnd(0U),
  319. NewParticleQueueCount(0U),
  320. RenderMode(src.RenderMode),
  321. FrameMode(src.FrameMode),
  322. MaxAge(src.MaxAge),
  323. FutureStartTime(src.FutureStartTime),
  324. LastUpdateTime(WW3D::Get_Sync_Time()),
  325. IsEmitterDead(false),
  326. MaxSize(src.MaxSize),
  327. MaxNum(src.MaxNum),
  328. Start(0U),
  329. End(0U),
  330. NewEnd(0U),
  331. NonNewNum(0),
  332. NewNum(0),
  333. BoundingBox(Vector3(0,0,0),Vector3(0,0,0)),
  334. BoundingBoxDirty(true),
  335. NumColorKeyFrames(src.NumColorKeyFrames),
  336. ColorKeyFrameTimes(NULL),
  337. ColorKeyFrameValues(NULL),
  338. ColorKeyFrameDeltas(NULL),
  339. NumAlphaKeyFrames(src.NumAlphaKeyFrames),
  340. AlphaKeyFrameTimes(NULL),
  341. AlphaKeyFrameValues(NULL),
  342. AlphaKeyFrameDeltas(NULL),
  343. NumSizeKeyFrames(src.NumSizeKeyFrames),
  344. SizeKeyFrameTimes(NULL),
  345. SizeKeyFrameValues(NULL),
  346. SizeKeyFrameDeltas(NULL),
  347. NumRotationKeyFrames(src.NumRotationKeyFrames),
  348. RotationKeyFrameTimes(NULL),
  349. RotationKeyFrameValues(NULL),
  350. HalfRotationKeyFrameDeltas(NULL),
  351. OrientationKeyFrameValues(NULL),
  352. NumFrameKeyFrames(src.NumFrameKeyFrames),
  353. FrameKeyFrameTimes(NULL),
  354. FrameKeyFrameValues(NULL),
  355. FrameKeyFrameDeltas(NULL),
  356. NumBlurTimeKeyFrames(src.NumBlurTimeKeyFrames),
  357. BlurTimeKeyFrameTimes(NULL),
  358. BlurTimeKeyFrameValues(NULL),
  359. BlurTimeKeyFrameDeltas(NULL),
  360. RandomColorEntries(NULL),
  361. RandomAlphaEntries(NULL),
  362. RandomSizeEntries(NULL),
  363. ColorRandom(src.ColorRandom),
  364. OpacityRandom(src.OpacityRandom),
  365. SizeRandom(src.SizeRandom),
  366. RotationRandom(src.RotationRandom),
  367. FrameRandom(src.FrameRandom),
  368. InitialOrientationRandom(src.InitialOrientationRandom),
  369. NumRandomRotationEntriesMinus1(0),
  370. RandomRotationEntries(NULL),
  371. NumRandomOrientationEntriesMinus1(0),
  372. RandomOrientationEntries(NULL),
  373. NumRandomFrameEntriesMinus1(0),
  374. RandomFrameEntries(NULL),
  375. NumRandomBlurTimeEntriesMinus1(0),
  376. RandomBlurTimeEntries(NULL),
  377. PointGroup(NULL),
  378. LineRenderer(NULL),
  379. LineGroup(NULL),
  380. Diffuse(NULL),
  381. TailDiffuse(NULL),
  382. Color(NULL),
  383. Alpha(NULL),
  384. Size(NULL),
  385. Orientation(NULL),
  386. Frame(NULL),
  387. UCoord(NULL),
  388. TailPosition(NULL),
  389. APT(NULL),
  390. GroupID(NULL),
  391. PingPongPosition(src.PingPongPosition),
  392. Velocity(NULL),
  393. TimeStamp(NULL),
  394. Emitter(src.Emitter),
  395. DecimationThreshold(src.DecimationThreshold),
  396. ProjectedArea(0.0f),
  397. DefaultTailDiffuse(src.DefaultTailDiffuse)
  398. {
  399. Position[0] = NULL;
  400. Position[1] = NULL;
  401. unsigned int i;
  402. LodCount = MIN(MaxNum, 17);
  403. LodBias = src.LodBias;
  404. /*
  405. ** Create visual state arrays, copy keyframes and randomizer tables.
  406. */
  407. NumRandomColorEntriesMinus1 = src.NumRandomColorEntriesMinus1;
  408. if (src.Color) {
  409. // Create color array
  410. Color = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Color") );
  411. // Copy color keyframes
  412. ColorKeyFrameTimes = W3DNEWARRAY unsigned int [NumColorKeyFrames];
  413. ColorKeyFrameValues = W3DNEWARRAY Vector3 [NumColorKeyFrames];
  414. ColorKeyFrameDeltas = W3DNEWARRAY Vector3 [NumColorKeyFrames];
  415. for (i = 0; i < NumColorKeyFrames; i++) {
  416. ColorKeyFrameTimes[i] = src.ColorKeyFrameTimes[i];
  417. ColorKeyFrameValues[i] = src.ColorKeyFrameValues[i];
  418. ColorKeyFrameDeltas[i] = src.ColorKeyFrameDeltas[i];
  419. }
  420. // Copy color randomizer table
  421. if (src.RandomColorEntries) {
  422. RandomColorEntries = W3DNEWARRAY Vector3 [NumRandomColorEntriesMinus1 + 1];
  423. for (unsigned int j = 0; j <= NumRandomColorEntriesMinus1; j++) {
  424. RandomColorEntries[j] = src.RandomColorEntries[j];
  425. }
  426. }
  427. } else {
  428. ColorKeyFrameValues = W3DNEWARRAY Vector3 [1];
  429. ColorKeyFrameValues[0] = src.ColorKeyFrameValues[0];
  430. }
  431. NumRandomAlphaEntriesMinus1 = src.NumRandomAlphaEntriesMinus1;
  432. if (src.Alpha) {
  433. // Create alpha array
  434. Alpha = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Alpha") );
  435. // Copy alpha keyframes
  436. AlphaKeyFrameTimes = W3DNEWARRAY unsigned int [NumAlphaKeyFrames];
  437. AlphaKeyFrameValues = W3DNEWARRAY float [NumAlphaKeyFrames];
  438. AlphaKeyFrameDeltas = W3DNEWARRAY float [NumAlphaKeyFrames];
  439. for (i = 0; i < NumAlphaKeyFrames; i++) {
  440. AlphaKeyFrameTimes[i] = src.AlphaKeyFrameTimes[i];
  441. AlphaKeyFrameValues[i] = src.AlphaKeyFrameValues[i];
  442. AlphaKeyFrameDeltas[i] = src.AlphaKeyFrameDeltas[i];
  443. }
  444. // Copy alpha randomizer table
  445. if (src.RandomAlphaEntries) {
  446. RandomAlphaEntries = W3DNEWARRAY float [NumRandomAlphaEntriesMinus1 + 1];
  447. for (unsigned int j = 0; j <= NumRandomAlphaEntriesMinus1; j++) {
  448. RandomAlphaEntries[j] = src.RandomAlphaEntries[j];
  449. }
  450. }
  451. } else {
  452. AlphaKeyFrameValues = W3DNEWARRAY float [1];
  453. AlphaKeyFrameValues[0] = src.AlphaKeyFrameValues[0];
  454. }
  455. NumRandomSizeEntriesMinus1 = src.NumRandomSizeEntriesMinus1;
  456. if (src.Size) {
  457. // Create size array
  458. Size = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Size") );
  459. // Copy size keyframes
  460. SizeKeyFrameTimes = W3DNEWARRAY unsigned int [NumSizeKeyFrames];
  461. SizeKeyFrameValues = W3DNEWARRAY float [NumSizeKeyFrames];
  462. SizeKeyFrameDeltas = W3DNEWARRAY float [NumSizeKeyFrames];
  463. for (i = 0; i < NumSizeKeyFrames; i++) {
  464. SizeKeyFrameTimes[i] = src.SizeKeyFrameTimes[i];
  465. SizeKeyFrameValues[i] = src.SizeKeyFrameValues[i];
  466. SizeKeyFrameDeltas[i] = src.SizeKeyFrameDeltas[i];
  467. }
  468. // Copy size randomizer table
  469. if (src.RandomSizeEntries) {
  470. RandomSizeEntries = W3DNEWARRAY float [NumRandomSizeEntriesMinus1 + 1];
  471. for (unsigned int j = 0; j <= NumRandomSizeEntriesMinus1; j++) {
  472. RandomSizeEntries[j] = src.RandomSizeEntries[j];
  473. }
  474. }
  475. } else {
  476. SizeKeyFrameValues = W3DNEWARRAY float [1];
  477. SizeKeyFrameValues[0] = src.SizeKeyFrameValues[0];
  478. }
  479. // Set up the rotation / orientation keyframes
  480. NumRandomRotationEntriesMinus1 = src.NumRandomRotationEntriesMinus1;
  481. NumRandomOrientationEntriesMinus1 = src.NumRandomOrientationEntriesMinus1;
  482. if (src.Orientation) {
  483. // Create orientation array
  484. Orientation = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Orientation") );
  485. // Copy rotation / orientation keyframes
  486. RotationKeyFrameTimes = W3DNEWARRAY unsigned int [NumRotationKeyFrames];
  487. RotationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
  488. HalfRotationKeyFrameDeltas = W3DNEWARRAY float [NumRotationKeyFrames];
  489. OrientationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
  490. for (i = 0; i < NumRotationKeyFrames; i++) {
  491. RotationKeyFrameTimes[i] = src.RotationKeyFrameTimes[i];
  492. RotationKeyFrameValues[i] = src.RotationKeyFrameValues[i];
  493. HalfRotationKeyFrameDeltas[i] = src.HalfRotationKeyFrameDeltas[i];
  494. OrientationKeyFrameValues[i] = src.OrientationKeyFrameValues[i];
  495. }
  496. // Copy rotation randomizer table
  497. if (src.RandomRotationEntries) {
  498. RandomRotationEntries = W3DNEWARRAY float [NumRandomRotationEntriesMinus1 + 1];
  499. for (unsigned int j = 0; j <= NumRandomRotationEntriesMinus1; j++) {
  500. RandomRotationEntries[j] = src.RandomRotationEntries[j];
  501. }
  502. }
  503. // Copy starting orientation randomizer table
  504. if (src.RandomOrientationEntries) {
  505. RandomOrientationEntries = W3DNEWARRAY float [NumRandomOrientationEntriesMinus1 + 1];
  506. for (unsigned int j = 0; j <= NumRandomOrientationEntriesMinus1; j++) {
  507. RandomOrientationEntries[j] = src.RandomOrientationEntries[j];
  508. }
  509. }
  510. } else {
  511. // Unlike other properties, if there is no Orientation array then all the arrays are NULL
  512. // (including the Values array) - there is an implicit starting value of 0.
  513. }
  514. // Set up the frame keyframes
  515. // Frame and UCoord both use Frame Key Frames for the source data
  516. NumRandomFrameEntriesMinus1 = src.NumRandomFrameEntriesMinus1;
  517. if (src.Frame || src.UCoord) {
  518. // Create frame array
  519. if (src.Frame) {
  520. Frame = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Frame") );
  521. } else {
  522. UCoord = NEW_REF( ShareBufferClass<float>, (MaxNum, "ParticleBufferClass::UCoord") );
  523. }
  524. // Copy frame keyframes
  525. FrameKeyFrameTimes = W3DNEWARRAY unsigned int [NumFrameKeyFrames];
  526. FrameKeyFrameValues = W3DNEWARRAY float [NumFrameKeyFrames];
  527. FrameKeyFrameDeltas = W3DNEWARRAY float [NumFrameKeyFrames];
  528. for (i = 0; i < NumFrameKeyFrames; i++) {
  529. FrameKeyFrameTimes[i] = src.FrameKeyFrameTimes[i];
  530. FrameKeyFrameValues[i] = src.FrameKeyFrameValues[i];
  531. FrameKeyFrameDeltas[i] = src.FrameKeyFrameDeltas[i];
  532. }
  533. // Copy frame randomizer table
  534. if (src.RandomFrameEntries) {
  535. RandomFrameEntries = W3DNEWARRAY float [NumRandomFrameEntriesMinus1 + 1];
  536. for (unsigned int j = 0; j <= NumRandomFrameEntriesMinus1; j++) {
  537. RandomFrameEntries[j] = src.RandomFrameEntries[j];
  538. }
  539. }
  540. } else {
  541. FrameKeyFrameValues = W3DNEWARRAY float [1];
  542. FrameKeyFrameValues[0] = src.FrameKeyFrameValues[0];
  543. }
  544. // Set up the blur times keyframes
  545. NumRandomBlurTimeEntriesMinus1 = src.NumRandomBlurTimeEntriesMinus1;
  546. if (NumBlurTimeKeyFrames > 0) {
  547. // Copy blur time keyframes
  548. BlurTimeKeyFrameTimes = new unsigned int [NumBlurTimeKeyFrames];
  549. BlurTimeKeyFrameValues = new float [NumBlurTimeKeyFrames];
  550. BlurTimeKeyFrameDeltas = new float [NumBlurTimeKeyFrames];
  551. for (i = 0; i < NumBlurTimeKeyFrames; i++) {
  552. BlurTimeKeyFrameTimes[i] = src.BlurTimeKeyFrameTimes[i];
  553. BlurTimeKeyFrameValues[i] = src.BlurTimeKeyFrameValues[i];
  554. BlurTimeKeyFrameDeltas[i] = src.BlurTimeKeyFrameDeltas[i];
  555. }
  556. // Copy blur time randomizer table
  557. if (src.RandomBlurTimeEntries) {
  558. RandomBlurTimeEntries = new float [NumRandomBlurTimeEntriesMinus1 + 1];
  559. for (unsigned int j = 0; j <= NumRandomBlurTimeEntriesMinus1; j++) {
  560. RandomBlurTimeEntries[j] = src.RandomBlurTimeEntries[j];
  561. }
  562. }
  563. } else {
  564. BlurTimeKeyFrameValues = new float [1];
  565. BlurTimeKeyFrameValues[0] = src.BlurTimeKeyFrameValues[0];
  566. }
  567. // We do not add a ref for the emitter (see DTor for detailed explanation)
  568. // if (Emitter) Emitter->Add_Ref();
  569. // Set up new particle queue:
  570. NewParticleQueue = W3DNEWARRAY NewParticleStruct[MaxNum];
  571. // Inputs don't need to be range-checked (emitter did that).
  572. Accel = src.Accel;
  573. HasAccel = src.HasAccel;
  574. switch (RenderMode)
  575. {
  576. case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
  577. {
  578. // Set up worldspace point group
  579. WWASSERT(src.PointGroup);
  580. PointGroup = W3DNEW PointGroupClass();
  581. PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
  582. PointGroup->Set_Texture(src.PointGroup->Peek_Texture());
  583. PointGroup->Set_Shader(src.PointGroup->Get_Shader());
  584. PointGroup->Set_Point_Mode(PointGroupClass::TRIS);
  585. PointGroup->Set_Frame_Row_Column_Count_Log2(src.PointGroup->Get_Frame_Row_Column_Count_Log2());
  586. }
  587. break;
  588. case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
  589. {
  590. // Set up worldspace point group
  591. WWASSERT(src.PointGroup);
  592. PointGroup = W3DNEW PointGroupClass();
  593. PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
  594. PointGroup->Set_Texture(src.PointGroup->Peek_Texture());
  595. PointGroup->Set_Shader(src.PointGroup->Get_Shader());
  596. PointGroup->Set_Point_Mode(PointGroupClass::QUADS);
  597. PointGroup->Set_Frame_Row_Column_Count_Log2(src.PointGroup->Get_Frame_Row_Column_Count_Log2());
  598. }
  599. break;
  600. case W3D_EMITTER_RENDER_MODE_LINE:
  601. {
  602. WWASSERT(src.LineRenderer);
  603. LineRenderer = W3DNEW SegLineRendererClass(*src.LineRenderer);
  604. }
  605. break;
  606. case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
  607. {
  608. WWASSERT(src.LineGroup);
  609. LineGroup = W3DNEW LineGroupClass();
  610. LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
  611. LineGroup->Set_Texture(src.LineGroup->Peek_Texture());
  612. LineGroup->Set_Shader(src.LineGroup->Get_Shader());
  613. LineGroup->Set_Line_Mode(LineGroupClass::TETRAHEDRON);
  614. TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition") );
  615. // TODO: Change TailPosition to Kinematic state and add
  616. // tail positions to bounding box
  617. Set_Force_Visible(1);
  618. }
  619. break;
  620. case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
  621. {
  622. WWASSERT(src.LineGroup);
  623. LineGroup = W3DNEW LineGroupClass();
  624. LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
  625. LineGroup->Set_Texture(src.LineGroup->Peek_Texture());
  626. LineGroup->Set_Shader(src.LineGroup->Get_Shader());
  627. LineGroup->Set_Line_Mode(LineGroupClass::PRISM);
  628. TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition") );
  629. // TODO: Change TailPosition to Kinematic state and add
  630. // tail positions to bounding box
  631. Set_Force_Visible(1);
  632. }
  633. break;
  634. default:
  635. WWASSERT(0);
  636. break;
  637. }
  638. // Set up circular buffer. Contents are not initialized because the
  639. // start/end indices currently indicate the buffer is empty.
  640. Position[0] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
  641. if (PingPongPosition) {
  642. Position[1] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
  643. }
  644. APT = NEW_REF( ShareBufferClass<unsigned int> , (MaxNum, "ParticleBufferClass::APT") );
  645. GroupID = NEW_REF( ShareBufferClass<unsigned char> , (MaxNum, "ParticleBufferClass::GroupID") );
  646. Velocity = W3DNEWARRAY Vector3[MaxNum];
  647. TimeStamp = W3DNEWARRAY unsigned int[MaxNum];
  648. // So that the object is ready for use after construction, we will
  649. // complete its initialization by initializing its cost and value arrays
  650. // according to a screen area of 1.
  651. int minlod = Calculate_Cost_Value_Arrays(1.0f, Value, Cost);
  652. // Ensure lod is no less than minimum allowed
  653. if (Get_LOD_Level() < minlod) Set_LOD_Level(minlod);
  654. // Update Global Count
  655. TotalActiveCount++;
  656. }
  657. ParticleBufferClass & ParticleBufferClass::operator = (const ParticleBufferClass & that)
  658. {
  659. RenderObjClass::operator = (that);
  660. if (this != &that) {
  661. assert(0); // TODO: if you hit this assert, please implement me !!!;-)
  662. }
  663. return * this;
  664. }
  665. ParticleBufferClass::~ParticleBufferClass(void)
  666. {
  667. if (NewParticleQueue) delete [] NewParticleQueue;
  668. if (ColorKeyFrameTimes) delete [] ColorKeyFrameTimes;
  669. if (ColorKeyFrameValues) delete [] ColorKeyFrameValues;
  670. if (ColorKeyFrameDeltas) delete [] ColorKeyFrameDeltas;
  671. if (AlphaKeyFrameTimes) delete [] AlphaKeyFrameTimes;
  672. if (AlphaKeyFrameValues) delete [] AlphaKeyFrameValues;
  673. if (AlphaKeyFrameDeltas) delete [] AlphaKeyFrameDeltas;
  674. if (SizeKeyFrameTimes) delete [] SizeKeyFrameTimes;
  675. if (SizeKeyFrameValues) delete [] SizeKeyFrameValues;
  676. if (SizeKeyFrameDeltas) delete [] SizeKeyFrameDeltas;
  677. if (RotationKeyFrameTimes) delete [] RotationKeyFrameTimes;
  678. if (RotationKeyFrameValues) delete [] RotationKeyFrameValues;
  679. if (HalfRotationKeyFrameDeltas) delete [] HalfRotationKeyFrameDeltas;
  680. if (OrientationKeyFrameValues) delete [] OrientationKeyFrameValues;
  681. if (FrameKeyFrameTimes) delete [] FrameKeyFrameTimes;
  682. if (FrameKeyFrameValues) delete [] FrameKeyFrameValues;
  683. if (FrameKeyFrameDeltas) delete [] FrameKeyFrameDeltas;
  684. if (BlurTimeKeyFrameTimes) delete [] BlurTimeKeyFrameTimes;
  685. if (BlurTimeKeyFrameValues) delete [] BlurTimeKeyFrameValues;
  686. if (BlurTimeKeyFrameDeltas) delete [] BlurTimeKeyFrameDeltas;
  687. if (RandomColorEntries) delete [] RandomColorEntries;
  688. if (RandomAlphaEntries) delete [] RandomAlphaEntries;
  689. if (RandomSizeEntries) delete [] RandomSizeEntries;
  690. if (RandomRotationEntries) delete [] RandomRotationEntries;
  691. if (RandomOrientationEntries) delete [] RandomOrientationEntries;
  692. if (RandomFrameEntries) delete [] RandomFrameEntries;
  693. if (RandomBlurTimeEntries) delete [] RandomBlurTimeEntries;
  694. if (PointGroup) delete PointGroup;
  695. if (LineRenderer) delete LineRenderer;
  696. if (LineGroup) delete LineGroup;
  697. REF_PTR_RELEASE(Position[0]);
  698. REF_PTR_RELEASE(Position[1]);
  699. REF_PTR_RELEASE(Diffuse);
  700. REF_PTR_RELEASE(TailDiffuse);
  701. REF_PTR_RELEASE(Color);
  702. REF_PTR_RELEASE(Alpha);
  703. REF_PTR_RELEASE(Size);
  704. REF_PTR_RELEASE(Orientation);
  705. REF_PTR_RELEASE(Frame);
  706. REF_PTR_RELEASE(UCoord);
  707. REF_PTR_RELEASE(TailPosition);
  708. REF_PTR_RELEASE(APT);
  709. REF_PTR_RELEASE(GroupID);
  710. if (Velocity) delete [] Velocity;
  711. if (TimeStamp) delete [] TimeStamp;
  712. if (Emitter) {
  713. // We should not have an emitter at this point, since the emitter
  714. // should still have a live ref to us if it still exists which would
  715. // prevent us from getting killed.
  716. assert(0);
  717. // We do not release-ref the emitter pointer because we did not add a
  718. // ref for it to begin with; the ref is not needed (if the emitter gets
  719. // deleted it will tell us to clear our emitter pointer) and actually
  720. // harmful (if emitter and buffer each have refcounted pointers to the
  721. // other neither would ever get deleted).
  722. // Emitter->Release_Ref();
  723. Emitter = NULL;
  724. }
  725. // Update Global Count
  726. TotalActiveCount--;
  727. }
  728. RenderObjClass * ParticleBufferClass::Clone(void) const
  729. {
  730. return W3DNEW ParticleBufferClass(*this);
  731. }
  732. int ParticleBufferClass::Get_Num_Polys(void) const
  733. {
  734. // Currently in particle buffers, the cost happens to be equal to thwe polygon count.
  735. return (int)Get_Cost();
  736. }
  737. int ParticleBufferClass::Get_Particle_Count(void) const
  738. {
  739. return NonNewNum + NewNum;
  740. }
  741. void ParticleBufferClass::Render(RenderInfoClass & rinfo)
  742. {
  743. WWPROFILE("ParticleBuffer::Render");
  744. unsigned int sort_level = SORT_LEVEL_NONE;
  745. if (!WW3D::Is_Sorting_Enabled())
  746. sort_level=Get_Shader().Guess_Sort_Level();
  747. if (WW3D::Are_Static_Sort_Lists_Enabled() && sort_level!=SORT_LEVEL_NONE) {
  748. WW3D::Add_To_Static_Sort_List(this, sort_level);
  749. } else {
  750. // Ensure particles' kinematic state is updated
  751. Update_Kinematic_Particle_State();
  752. // Since we are rendering the particles, visual state needs to be updated (but not if the
  753. // entire particle buffer is decimated away)
  754. if (DecimationThreshold < LodCount - 1) {
  755. Update_Visual_Particle_State();
  756. }
  757. switch( RenderMode )
  758. {
  759. case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
  760. case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
  761. Render_Particles(rinfo);
  762. break;
  763. case W3D_EMITTER_RENDER_MODE_LINE:
  764. Render_Line(rinfo);
  765. break;
  766. case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
  767. case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
  768. Render_Line_Group(rinfo);
  769. break;
  770. }
  771. }
  772. }
  773. void ParticleBufferClass::Generate_APT(ShareBufferClass <unsigned int> **apt,unsigned int &active_point_count)
  774. {
  775. if (NonNewNum < (int)MaxNum || DecimationThreshold > 0) {
  776. // In the general case, a range in a circular buffer can be composed of up
  777. // to two subranges. Find the Start - End subranges.
  778. // This differs from other similar code segments because we want to access
  779. // the subranges in memory order (rather than in queue order) this time.
  780. unsigned int sub1_start; // Start of subrange 1.
  781. unsigned int sub1_end; // End of subrange 1.
  782. unsigned int sub2_start; // Start of subrange 2.
  783. unsigned int sub2_end; // End of subrange 2.
  784. unsigned int i; // Loop index.
  785. if ((Start < End) || ((Start == End) && NonNewNum == 0)) {
  786. sub1_start = Start;
  787. sub1_end = End;
  788. sub2_start = End;
  789. sub2_end = End;
  790. } else {
  791. sub1_start = 0;
  792. sub1_end = End;
  793. sub2_start = Start;
  794. sub2_end = MaxNum;
  795. }
  796. // Generate APT:
  797. unsigned int *apt_ptr = APT->Get_Array();
  798. for (i = sub1_start; i < sub1_end; i++) {
  799. if (PermutationArray[i & 0xF] >= DecimationThreshold) {
  800. apt_ptr[active_point_count++] = i;
  801. }
  802. }
  803. for (i = sub2_start; i < sub2_end; i++) {
  804. if (PermutationArray[i & 0xF] >= DecimationThreshold) {
  805. apt_ptr[active_point_count++] = i;
  806. }
  807. }
  808. *apt = APT;
  809. } else {
  810. active_point_count = NonNewNum;
  811. }
  812. }
  813. void ParticleBufferClass::Combine_Color_And_Alpha()
  814. {
  815. // Temporary array copying to combine diffuse and alpha to one array.
  816. if (Color || Alpha) {
  817. unsigned cnt=MaxNum;
  818. if (!Diffuse) {
  819. Diffuse = NEW_REF( ShareBufferClass<Vector4> , (MaxNum, "ParticleBufferClass::Diffuse") );
  820. }
  821. if (Color && Alpha) {
  822. VectorProcessorClass::Copy(
  823. Diffuse->Get_Array(),
  824. Color->Get_Array(),
  825. Alpha->Get_Array(),
  826. cnt);
  827. }
  828. else if (Color) {
  829. VectorProcessorClass::Copy(
  830. Diffuse->Get_Array(),
  831. Color->Get_Array(),
  832. 1.0f,
  833. cnt);
  834. }
  835. else {
  836. VectorProcessorClass::Copy(
  837. Diffuse->Get_Array(),
  838. Vector3(1.0f,1.0f,1.0f),
  839. Alpha->Get_Array(),
  840. cnt);
  841. }
  842. VectorProcessorClass::Clamp(
  843. Diffuse->Get_Array(),
  844. Diffuse->Get_Array(),
  845. 0.0f,
  846. 1.0f,
  847. cnt);
  848. }
  849. else if (Diffuse) {
  850. Diffuse->Release_Ref();
  851. Diffuse=NULL;
  852. }
  853. }
  854. void ParticleBufferClass::Render_Particles(RenderInfoClass & rinfo)
  855. {
  856. // If the number of active points is less than the maximum or we need to decimate particles
  857. // (for LOD purposes), build the active point table:
  858. ShareBufferClass<unsigned int> *apt = NULL;
  859. unsigned int active_point_count = 0;
  860. Generate_APT(&apt,active_point_count);
  861. // Set color, alpha, size defaults if array not present:
  862. if (!Color) {
  863. PointGroup->Set_Point_Color(ColorKeyFrameValues[0]);
  864. }
  865. if (!Alpha) {
  866. PointGroup->Set_Point_Alpha(AlphaKeyFrameValues[0]);
  867. }
  868. if (!Size) {
  869. PointGroup->Set_Point_Size(SizeKeyFrameValues[0]);
  870. }
  871. if (!Orientation) {
  872. // The rotation keyframes are used to derive the orientation indirectly, as well as the
  873. // starting orientation randomizer. If there is no Orientation array that means both are
  874. // absent so the orientation should just be set to 0.
  875. PointGroup->Set_Point_Orientation(0);
  876. }
  877. if (!Frame) {
  878. PointGroup->Set_Point_Frame(((int)(FrameKeyFrameValues[0])) & 0xFF);
  879. }
  880. // Pass the point buffer to the point group and render it.
  881. // If we are using pingpong position buffers pass the right one
  882. int pingpong = 0;
  883. if (PingPongPosition) {
  884. pingpong = WW3D::Get_Frame_Count() & 0x1;
  885. }
  886. Combine_Color_And_Alpha();
  887. PointGroup->Set_Arrays(Position[pingpong], Diffuse, apt, Size, Orientation, Frame, active_point_count);
  888. Update_Bounding_Box();
  889. PointGroup->Render(rinfo);
  890. }
  891. void ParticleBufferClass::Render_Line(RenderInfoClass & rinfo)
  892. {
  893. LineRenderer->Set_Freeze_Random(Is_Freeze_Random());
  894. // Look up the array to use
  895. int pingpong = 0;
  896. if (PingPongPosition) {
  897. pingpong = WW3D::Get_Frame_Count() & 0x1;
  898. }
  899. // Unroll the circular buffer while skipping LOD'd particles
  900. static SimpleDynVecClass<Vector3> tmp_points;
  901. static SimpleDynVecClass<Vector4> tmp_diffuse;
  902. static SimpleDynVecClass<unsigned char> tmp_id;
  903. Vector3 * positions = Position[pingpong]->Get_Array();
  904. Vector4 * diffuse = 0;
  905. Vector4 default_diffuse(0, 0, 0, 0);
  906. unsigned char *ids = GroupID->Get_Array();
  907. Combine_Color_And_Alpha();
  908. if (Diffuse) {
  909. diffuse = Diffuse->Get_Array();
  910. } else {
  911. default_diffuse.Set(ColorKeyFrameValues[0].X, ColorKeyFrameValues[0].Y, ColorKeyFrameValues[0].Z,
  912. AlphaKeyFrameValues[0]);
  913. }
  914. unsigned int sub1_end; // End of subrange 1.
  915. unsigned int sub2_start; // Start of subrange 2.
  916. unsigned int i; // Loop index.
  917. if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
  918. sub1_end = End;
  919. sub2_start = End;
  920. } else {
  921. sub1_end = MaxNum;
  922. sub2_start = 0;
  923. }
  924. tmp_points.Delete_All(false);
  925. tmp_diffuse.Delete_All(false);
  926. tmp_id.Delete_All(false);
  927. Vector4 *last_color = &default_diffuse;
  928. unsigned char last_id = 0;
  929. for (i = Start; i < sub1_end; i++) {
  930. if (PermutationArray[i & 0xF] >= DecimationThreshold) {
  931. tmp_points.Add(positions[i]);
  932. last_color = diffuse ? &diffuse[i] : &default_diffuse;
  933. tmp_diffuse.Add(*last_color);
  934. last_id = ids[i];
  935. tmp_id.Add(last_id);
  936. }
  937. }
  938. for (i = sub2_start; i < End; i++) {
  939. if (PermutationArray[i & 0xF] >= DecimationThreshold) {
  940. tmp_points.Add(positions[i]);
  941. last_color = diffuse ? &diffuse[i] : &default_diffuse;
  942. tmp_diffuse.Add(*last_color);
  943. last_id = ids[i];
  944. tmp_id.Add(last_id);
  945. }
  946. }
  947. // add in the emitter's position too for the source
  948. if (Emitter && !Emitter->Is_Stopped() && (last_id == CurrentGroupID)) {
  949. tmp_points.Add(Emitter->Get_Position());
  950. // it has the color of the last point
  951. tmp_diffuse.Add(*last_color);
  952. tmp_id.Add(last_id);
  953. }
  954. // If we got any points, render them
  955. if (tmp_points.Count() > 0) {
  956. SphereClass bounding_sphere;
  957. Get_Obj_Space_Bounding_Sphere(bounding_sphere);
  958. // Draw line segments only if they are in the same group
  959. int count = tmp_points.Count();
  960. int start = 0;
  961. int end = 0;
  962. while (end < count) {
  963. // detect contiguous runs of IDs
  964. while ( (end < count) && (tmp_id[start] == tmp_id[end])) {
  965. end++;
  966. }
  967. // render from start, excluding end
  968. if (end - start > 1) {
  969. LineRenderer->Render(rinfo,
  970. Transform,
  971. end - start,
  972. &(tmp_points[start]),
  973. bounding_sphere,
  974. &(tmp_diffuse[start]));
  975. }
  976. start = end;
  977. }
  978. }
  979. }
  980. void ParticleBufferClass::Render_Line_Group(RenderInfoClass & rinfo)
  981. {
  982. // If the number of active points is less than the maximum or we need to decimate particles
  983. // (for LOD purposes), build the active point table:
  984. ShareBufferClass<unsigned int> *apt = NULL;
  985. unsigned int active_point_count = 0;
  986. Generate_APT(&apt,active_point_count);
  987. // Set color, alpha, size defaults if array not present:
  988. if (!Color) {
  989. LineGroup->Set_Line_Color(ColorKeyFrameValues[0]);
  990. }
  991. if (!Alpha) {
  992. LineGroup->Set_Line_Alpha(AlphaKeyFrameValues[0]);
  993. }
  994. if (!Size) {
  995. LineGroup->Set_Line_Size(SizeKeyFrameValues[0]);
  996. }
  997. if (!Frame) {
  998. LineGroup->Set_Line_UCoord(FrameKeyFrameValues[0]);
  999. }
  1000. // Pass the point buffer to the line group and render it.
  1001. // If we are using pingpong position buffers pass the right one
  1002. int pingpong = 0;
  1003. if (PingPongPosition) {
  1004. pingpong = WW3D::Get_Frame_Count() & 0x1;
  1005. }
  1006. Combine_Color_And_Alpha();
  1007. TailDiffuseTypeEnum tailtype=Determine_Tail_Diffuse();
  1008. switch (tailtype)
  1009. {
  1010. case BLACK:
  1011. REF_PTR_RELEASE(TailDiffuse);
  1012. DefaultTailDiffuse.Set(0,0,0,0);
  1013. break;
  1014. case WHITE:
  1015. REF_PTR_RELEASE(TailDiffuse);
  1016. DefaultTailDiffuse.Set(1,1,1,1);
  1017. break;
  1018. case SAME_AS_HEAD_ALPHA_ZERO:
  1019. // if head is all one color, set tail the same way
  1020. if (!Diffuse) {
  1021. REF_PTR_RELEASE(TailDiffuse);
  1022. DefaultTailDiffuse.Set(ColorKeyFrameValues[0].X,ColorKeyFrameValues[0].Y,ColorKeyFrameValues[0].Z,0);
  1023. } else {
  1024. // otherwise allocate and copy tail diffuse
  1025. if (!TailDiffuse) TailDiffuse=NEW_REF(ShareBufferClass<Vector4>,(MaxNum, "ParticleBufferClass::TailDiffuse"));
  1026. for (unsigned int i=0; i<MaxNum; i++) {
  1027. Vector4 elt=Diffuse->Get_Element(i);
  1028. elt.W=0;
  1029. TailDiffuse->Set_Element(i,elt);
  1030. }
  1031. }
  1032. break;
  1033. case SAME_AS_HEAD:
  1034. // if head is all one color, set tail the same way
  1035. if (!Diffuse) {
  1036. REF_PTR_RELEASE(TailDiffuse);
  1037. DefaultTailDiffuse.Set(ColorKeyFrameValues[0].X,ColorKeyFrameValues[0].Y,ColorKeyFrameValues[0].Z,AlphaKeyFrameValues[0]);
  1038. } else {
  1039. // otherwise allocate and copy tail diffuse
  1040. if (!TailDiffuse) TailDiffuse=NEW_REF(ShareBufferClass<Vector4>,(MaxNum, "ParticleBufferClass::TailDiffuse"));
  1041. VectorProcessorClass::Copy(TailDiffuse->Get_Array(),Diffuse->Get_Array(),MaxNum);
  1042. }
  1043. break;
  1044. default:
  1045. WWASSERT(0);
  1046. break;
  1047. }
  1048. if (!TailDiffuse)
  1049. LineGroup->Set_Tail_Diffuse(DefaultTailDiffuse);
  1050. LineGroup->Set_Arrays(Position[pingpong], TailPosition,Diffuse,TailDiffuse, apt, Size, UCoord, active_point_count);
  1051. Update_Bounding_Box();
  1052. LineGroup->Render(rinfo);
  1053. }
  1054. // Scales the size of the individual particles but doesn't affect their
  1055. // position (and therefore the size of the particle system as a whole)
  1056. void ParticleBufferClass::Scale(float scale)
  1057. {
  1058. // Scale all size keyframes, keyframe deltas, random size entries,
  1059. // MaxSize and SizeRandom.
  1060. unsigned int i;
  1061. if (NumSizeKeyFrames) {
  1062. for (i = 0; i < NumSizeKeyFrames; i++) {
  1063. SizeKeyFrameValues[i] *= scale;
  1064. SizeKeyFrameDeltas[i] *= scale;
  1065. }
  1066. } else {
  1067. SizeKeyFrameValues[0] *= scale;
  1068. }
  1069. if (RandomSizeEntries) {
  1070. for (i = 0; i <= NumRandomSizeEntriesMinus1; i++) {
  1071. RandomSizeEntries[i] *= scale;
  1072. }
  1073. }
  1074. if (LineRenderer) {
  1075. LineRenderer->Scale(scale);
  1076. }
  1077. MaxSize *= scale;
  1078. SizeRandom *= scale;
  1079. Accel *= scale;
  1080. }
  1081. // The particle buffer never receives a Set_Transform/Position call,
  1082. // evem though its bounding volume changes. Since bounding volume
  1083. // invalidations ordinarily occur when these functions are called,
  1084. // the cached bounding volumes will not be invalidated unless we do
  1085. // it elsewhere (such as here). We also need to call the particle
  1086. // emitter's Emit() function (done here to avoid order dependence).
  1087. void ParticleBufferClass::On_Frame_Update(void)
  1088. {
  1089. WWPROFILE("ParticleBufferClass::On_Frame_Update");
  1090. Invalidate_Cached_Bounding_Volumes();
  1091. if (Emitter) {
  1092. Emitter->Emit();
  1093. }
  1094. if (Is_Complete()) {
  1095. WWASSERT(Scene);
  1096. Scene->Register(this,SceneClass::RELEASE);
  1097. }
  1098. }
  1099. void ParticleBufferClass::Notify_Added(SceneClass * scene)
  1100. {
  1101. RenderObjClass::Notify_Added(scene);
  1102. scene->Register(this,SceneClass::ON_FRAME_UPDATE);
  1103. }
  1104. void ParticleBufferClass::Notify_Removed(SceneClass * scene)
  1105. {
  1106. scene->Unregister(this,SceneClass::ON_FRAME_UPDATE);
  1107. RenderObjClass::Notify_Removed(scene);
  1108. }
  1109. void ParticleBufferClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  1110. {
  1111. // This ugly cast is done because the alternative is to make everything
  1112. // in the class mutable, which does not seem like a good solution
  1113. // (Update_Bounding_Box can potentially update the particle state)
  1114. ((ParticleBufferClass *)this)->Update_Bounding_Box();
  1115. // The particle buffer's transform is always identity, so
  1116. // objspace == worldspace.
  1117. // Wrap sphere outside bounding box:
  1118. sphere.Center = BoundingBox.Center;
  1119. sphere.Radius = BoundingBox.Extent.Length();
  1120. }
  1121. void ParticleBufferClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  1122. {
  1123. // This ugly cast is done because the alternative is to make everything
  1124. // in the class mutable, which does not seem like a good solution
  1125. // (Update_Bounding_Box can potentially update the particle state).
  1126. ((ParticleBufferClass *)this)->Update_Bounding_Box();
  1127. // The particle buffer's transform is always identity, so
  1128. // objspace == worldspace.
  1129. box = BoundingBox;
  1130. }
  1131. void ParticleBufferClass::Prepare_LOD(CameraClass &camera)
  1132. {
  1133. if (Is_Not_Hidden_At_All() == false) {
  1134. return;
  1135. }
  1136. // Estimate the screen area of the particle buffer. We shall take the lesser of two
  1137. // metrics: the standard bounding-sphere projection (which for many particle systems may
  1138. // grossly overestimate the actual screen area), and a measurement based on the screen area of
  1139. // individual particles times the maximum number of particles (in the case of densely
  1140. // overlapping particles this metric can also give numbers which are too high, which is why we
  1141. // use the bounding sphere as backup). Note - to find the area of individual particles we
  1142. // treat them as all being the maximum size and being in the center of the bounding sphere).
  1143. Vector3 cam = camera.Get_Position();
  1144. ViewportClass viewport = camera.Get_Viewport();
  1145. Vector2 vpr_min, vpr_max;
  1146. camera.Get_View_Plane(vpr_min, vpr_max);
  1147. float width_factor = viewport.Width() / (vpr_max.X - vpr_min.X);
  1148. float height_factor = viewport.Height() / (vpr_max.Y - vpr_min.Y);
  1149. const SphereClass & sphere = Get_Bounding_Sphere();
  1150. float dist = (sphere.Center - cam).Length();
  1151. float bounding_sphere_projected_radius = 0.0f;
  1152. float particle_projected_radius = 0.0f;
  1153. if (dist) {
  1154. float oo_dist = 1.0f / dist;
  1155. bounding_sphere_projected_radius = sphere.Radius * oo_dist;
  1156. particle_projected_radius = MaxSize * oo_dist;
  1157. }
  1158. float bs_rad_sq = bounding_sphere_projected_radius * bounding_sphere_projected_radius;
  1159. float p_rad_sq = particle_projected_radius * particle_projected_radius * MaxNum;
  1160. float proj_area = WWMATH_PI * MIN(bs_rad_sq, p_rad_sq) * width_factor * height_factor;
  1161. // Filter the area over time so we don't get as many pops in the LOD algorithm
  1162. ProjectedArea = 0.9f * ProjectedArea + 0.1f * proj_area;
  1163. int minlod = Calculate_Cost_Value_Arrays(ProjectedArea, Value, Cost);
  1164. // Ensure lod is no less than minimum allowed
  1165. if (Get_LOD_Level() < minlod) Set_LOD_Level(minlod);
  1166. PredictiveLODOptimizerClass::Add_Object(this);
  1167. }
  1168. void ParticleBufferClass::Increment_LOD(void)
  1169. {
  1170. if (DecimationThreshold > 0) DecimationThreshold--;
  1171. }
  1172. void ParticleBufferClass::Decrement_LOD(void)
  1173. {
  1174. if (DecimationThreshold < LodCount) DecimationThreshold++;
  1175. }
  1176. float ParticleBufferClass::Get_Cost(void) const
  1177. {
  1178. return(Cost[(LodCount - 1) - DecimationThreshold]);
  1179. }
  1180. float ParticleBufferClass::Get_Value(void) const
  1181. {
  1182. return(Value[(LodCount - 1) - DecimationThreshold]);
  1183. }
  1184. float ParticleBufferClass::Get_Post_Increment_Value(void) const
  1185. {
  1186. return(Value[LodCount - DecimationThreshold]);
  1187. }
  1188. void ParticleBufferClass::Set_LOD_Level(int lod)
  1189. {
  1190. lod = Bound(lod, 0, (int)LodCount);
  1191. DecimationThreshold = (LodCount - 1) - lod;
  1192. }
  1193. int ParticleBufferClass::Get_LOD_Level(void) const
  1194. {
  1195. return((LodCount - 1) - DecimationThreshold);
  1196. }
  1197. int ParticleBufferClass::Get_LOD_Count(void) const
  1198. {
  1199. return LodCount;
  1200. }
  1201. int ParticleBufferClass::Calculate_Cost_Value_Arrays(float screen_area, float *values, float *costs) const
  1202. {
  1203. unsigned int lod = 0;
  1204. // Calculate Cost heuristic for each LOD (we currently ignore pixel costs for particle systems)
  1205. // The cost factor is later multiplied by the LOD level. The LOD level is the numerator of the
  1206. // fraction of particles rendered, where 16 is the denominator. For this reason the cost factor
  1207. // is based on a 1/16 (0.0625) of the total.
  1208. float cost_factor=0.0f;
  1209. switch (RenderMode)
  1210. {
  1211. case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
  1212. cost_factor = (float)MaxNum * 0.0625f;
  1213. break;
  1214. case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
  1215. cost_factor = (float)MaxNum * 2.0f * 0.0625f;
  1216. break;
  1217. case W3D_EMITTER_RENDER_MODE_LINE:
  1218. cost_factor = (float) (2*MaxNum-1) * 0.0625f;
  1219. break;
  1220. case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
  1221. cost_factor = (float)MaxNum * 4.0f * 0.0625f;
  1222. break;
  1223. case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
  1224. cost_factor = (float)MaxNum * 8.0f * 0.0625f;
  1225. break;
  1226. }
  1227. for (lod = 0; lod < LodCount; lod++) {
  1228. costs[lod] = cost_factor * (float)lod;
  1229. // If cost is zero set it to a small nonzero amount to avoid divisions by zero.
  1230. costs[lod] = (costs[lod] != 0) ? costs[lod] : 0.000001f;
  1231. }
  1232. // Calculate Value heuristic. First, all LOD levels for which
  1233. // MaxScreenSize is smaller than screen_area have their Value set to
  1234. // AT_MIN_LOD, as well as the first LOD after that (unless there are no
  1235. // other LODs):
  1236. for (lod = 0; lod < LodCount && LODMaxScreenSizes[lod] < screen_area; lod++) {
  1237. values[lod] = AT_MIN_LOD;
  1238. }
  1239. if (lod >= LodCount) {
  1240. lod = LodCount - 1;
  1241. } else {
  1242. values[lod] = AT_MIN_LOD;
  1243. }
  1244. // Now lod is the lowest allowed - return this value.
  1245. int minlod = lod;
  1246. // Calculate Value heuristic for any remaining LODs based on normalized screen area:
  1247. lod++;
  1248. for (; lod < LodCount; lod++) {
  1249. // Currently the cost happens to be equal to the poly count. We use a floating-
  1250. // point poly count since costs[] contains an approximation to the true polycount which may
  1251. // be less than one in some cases (we want to avoid 0 polycounts except for true null LODs)
  1252. float polycount = costs[lod];
  1253. float benefit_factor = (polycount > WWMATH_EPSILON) ? (1 - (0.5f / (polycount * polycount))) : 0.0f;
  1254. values[lod] = (benefit_factor * screen_area * LodBias) / costs[lod];
  1255. }
  1256. values[LodCount] = AT_MAX_LOD; // Post-inc value will flag max LOD.
  1257. return minlod;
  1258. }
  1259. void ParticleBufferClass::Reset_Colors(ParticlePropertyStruct<Vector3> &new_props)
  1260. {
  1261. unsigned int i; // Used in loops
  1262. unsigned int ui_previous_key_time = 0;
  1263. unsigned int ui_current_key_time = 0;
  1264. ColorRandom = new_props.Rand;
  1265. // If the randomizer is effectively zero and there are no keyframes, then we just create a
  1266. // values array with one entry and store the starting value in it (the keyframes and random
  1267. // table will not be used in this case).
  1268. static const float eps_byte = 0.0038f; // Epsilon value - less than 1/255
  1269. bool color_rand_zero = (fabs(new_props.Rand.X) < eps_byte && fabs(new_props.Rand.Y) < eps_byte && fabs(new_props.Rand.Z) < eps_byte);
  1270. if (color_rand_zero && new_props.NumKeyFrames == 0) {
  1271. // Release Color, ColorKeyFrameTimes and ColorKeyFrameDeltas if present. Reuse
  1272. // ColorKeyFrameValues if the right size, otherwise release and reallocate.
  1273. if (Color) {
  1274. Color->Release_Ref();
  1275. Color = NULL;
  1276. }
  1277. if (ColorKeyFrameTimes) {
  1278. delete [] ColorKeyFrameTimes;
  1279. ColorKeyFrameTimes = NULL;
  1280. }
  1281. if (ColorKeyFrameDeltas) {
  1282. delete [] ColorKeyFrameDeltas;
  1283. ColorKeyFrameDeltas = NULL;
  1284. }
  1285. if (ColorKeyFrameValues) {
  1286. if (NumColorKeyFrames > 1) {
  1287. delete [] ColorKeyFrameValues;
  1288. ColorKeyFrameValues = W3DNEWARRAY Vector3 [1];
  1289. }
  1290. } else {
  1291. ColorKeyFrameValues = W3DNEWARRAY Vector3 [1];
  1292. }
  1293. NumColorKeyFrames = 0;
  1294. NumRandomColorEntriesMinus1 = 0;
  1295. ColorKeyFrameValues[0] = new_props.Start;
  1296. } else {
  1297. // Create the color array if not present
  1298. if (!Color) {
  1299. Color = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Color") );
  1300. }
  1301. // Check times of color keyframes (each keytime must be larger than the
  1302. // previous one by at least a millisecond, and we stop at the first
  1303. // keytime of MaxAge or larger. (If all keyframes below MaxAge, color is
  1304. // constant during the last segment between last keyframe and MaxAge).
  1305. ui_previous_key_time = 0;
  1306. for (unsigned int ckey = 0; ckey < new_props.NumKeyFrames; ckey++) {
  1307. ui_current_key_time = (unsigned int)(new_props.KeyTimes[ckey] * 1000.0f);
  1308. WWASSERT(ui_current_key_time > ui_previous_key_time);
  1309. if (ui_current_key_time >= MaxAge) break;
  1310. ui_previous_key_time = ui_current_key_time;
  1311. }
  1312. bool color_constant_at_end = (ckey == new_props.NumKeyFrames);
  1313. // Reuse ColorKeyFrameValues, ColorKeyFrameTimes and ColorKeyFrameDeltas if the right size,
  1314. // otherwise release and reallocate.
  1315. unsigned int new_num_color_key_frames = ckey + 1;// Includes start keyframe (keytime == 0).
  1316. if (new_num_color_key_frames != NumColorKeyFrames) {
  1317. if (ColorKeyFrameTimes) {
  1318. delete [] ColorKeyFrameTimes;
  1319. ColorKeyFrameTimes = NULL;
  1320. }
  1321. if (ColorKeyFrameValues) {
  1322. delete [] ColorKeyFrameValues;
  1323. ColorKeyFrameValues = NULL;
  1324. }
  1325. if (ColorKeyFrameDeltas) {
  1326. delete [] ColorKeyFrameDeltas;
  1327. ColorKeyFrameDeltas = NULL;
  1328. }
  1329. NumColorKeyFrames = new_num_color_key_frames;
  1330. ColorKeyFrameTimes = W3DNEWARRAY unsigned int [NumColorKeyFrames];
  1331. ColorKeyFrameValues = W3DNEWARRAY Vector3 [NumColorKeyFrames];
  1332. ColorKeyFrameDeltas = W3DNEWARRAY Vector3 [NumColorKeyFrames];
  1333. }
  1334. // Set color keyframes (deltas will be set later)
  1335. ColorKeyFrameTimes[0] = 0;
  1336. ColorKeyFrameValues[0] = new_props.Start;
  1337. for (i = 1; i < NumColorKeyFrames; i++) {
  1338. unsigned int im1 = i - 1;
  1339. ColorKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
  1340. ColorKeyFrameValues[i] = new_props.Values[im1];
  1341. }
  1342. // Do deltas for all color keyframes except last
  1343. for (i = 0; i < NumColorKeyFrames - 1; i++) {
  1344. ColorKeyFrameDeltas[i] = (ColorKeyFrameValues[i + 1] - ColorKeyFrameValues[i]) /
  1345. (float)(ColorKeyFrameTimes[i + 1] - ColorKeyFrameTimes[i]);
  1346. }
  1347. // Do delta for last color keyframe (i is NumColorKeyFrames - 1)
  1348. if (color_constant_at_end) {
  1349. ColorKeyFrameDeltas[i].Set(0.0, 0.0, 0.0);
  1350. } else {
  1351. // This is OK because if color_constant_at_end is false, NumColorKeyFrames is equal or
  1352. // smaller than color.NumKeyFrames so color.Values[NumColorKeyFrames - 1] and
  1353. // color.KeyTimes[NumColorKeyFrames - 1] exist.
  1354. ColorKeyFrameDeltas[i] = (new_props.Values[i] - ColorKeyFrameValues[i]) /
  1355. (new_props.KeyTimes[i] * 1000.0f - (float)ColorKeyFrameTimes[i]);
  1356. }
  1357. // Set up color randomizer table
  1358. if (color_rand_zero) {
  1359. if (RandomColorEntries) {
  1360. // Reuse RandomColorEntries if the right size, otherwise release and reallocate.
  1361. if (NumRandomColorEntriesMinus1 != 0) {
  1362. delete [] RandomColorEntries;
  1363. RandomColorEntries = W3DNEWARRAY Vector3 [1];
  1364. }
  1365. } else {
  1366. RandomColorEntries = W3DNEWARRAY Vector3 [1];
  1367. }
  1368. NumRandomColorEntriesMinus1 = 0;
  1369. RandomColorEntries[0].X = 0.0f;
  1370. RandomColorEntries[0].Y = 0.0f;
  1371. RandomColorEntries[0].Z = 0.0f;
  1372. } else {
  1373. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  1374. unsigned int pot_num = Find_POT(MaxNum);
  1375. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  1376. if (RandomColorEntries) {
  1377. // Reuse RandomColorEntries if the right size, otherwise release and reallocate.
  1378. if (NumRandomColorEntriesMinus1 != (default_randomizer_entries - 1)) {
  1379. delete [] RandomColorEntries;
  1380. RandomColorEntries = W3DNEWARRAY Vector3 [default_randomizer_entries];
  1381. }
  1382. } else {
  1383. RandomColorEntries = W3DNEWARRAY Vector3 [default_randomizer_entries];
  1384. }
  1385. NumRandomColorEntriesMinus1 = default_randomizer_entries - 1;
  1386. float rscale = new_props.Rand.X * oo_intmax;
  1387. float gscale = new_props.Rand.Y * oo_intmax;
  1388. float bscale = new_props.Rand.Z * oo_intmax;
  1389. for (unsigned int j = 0; j <= NumRandomColorEntriesMinus1; j++) {
  1390. RandomColorEntries[j] = Vector3(rand_gen * rscale, rand_gen * gscale, rand_gen * bscale);
  1391. }
  1392. }
  1393. }
  1394. }
  1395. void ParticleBufferClass::Reset_Opacity(ParticlePropertyStruct<float> &new_props)
  1396. {
  1397. unsigned int i; // Used in loops
  1398. unsigned int ui_previous_key_time = 0;
  1399. unsigned int ui_current_key_time = 0;
  1400. OpacityRandom = new_props.Rand;
  1401. // If the randomizer is effectively zero and there are no keyframes, then we just create a
  1402. // values array with one entry and store the starting value in it (the keyframes and random
  1403. // table will not be used in this case).
  1404. static const float eps_byte = 0.0038f; // Epsilon value - less than 1/255
  1405. bool alpha_rand_zero = (fabs(new_props.Rand) < eps_byte);
  1406. if (alpha_rand_zero && new_props.NumKeyFrames == 0) {
  1407. // Release Alpha, AlphaKeyFrameTimes and AlphaKeyFrameDeltas if present. Reuse
  1408. // AlphaKeyFrameValues if the right size, otherwise release and reallocate.
  1409. if (Alpha) {
  1410. Alpha->Release_Ref();
  1411. Alpha = NULL;
  1412. }
  1413. if (AlphaKeyFrameTimes) {
  1414. delete [] AlphaKeyFrameTimes;
  1415. AlphaKeyFrameTimes = NULL;
  1416. }
  1417. if (AlphaKeyFrameDeltas) {
  1418. delete [] AlphaKeyFrameDeltas;
  1419. AlphaKeyFrameDeltas = NULL;
  1420. }
  1421. if (AlphaKeyFrameValues) {
  1422. if (NumAlphaKeyFrames > 1) {
  1423. delete [] AlphaKeyFrameValues;
  1424. AlphaKeyFrameValues = W3DNEWARRAY float [1];
  1425. }
  1426. } else {
  1427. AlphaKeyFrameValues = W3DNEWARRAY float [1];
  1428. }
  1429. NumAlphaKeyFrames = 0;
  1430. NumRandomAlphaEntriesMinus1 = 0;
  1431. AlphaKeyFrameValues[0] = new_props.Start;
  1432. } else {
  1433. // Create the alpha array if not present
  1434. if (!Alpha) {
  1435. Alpha = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Alpha") );
  1436. }
  1437. // Check times of opacity keyframes (each keytime must be larger than the
  1438. // previous one by at least a millisecond, and we stop at the first
  1439. // keytime of MaxAge or larger. (If all keyframes below MaxAge, alpha is
  1440. // constant during the last segment between last keyframe and MaxAge).
  1441. ui_previous_key_time = 0;
  1442. for (unsigned int akey = 0; akey < new_props.NumKeyFrames; akey++) {
  1443. ui_current_key_time = (unsigned int)(new_props.KeyTimes[akey] * 1000.0f);
  1444. WWASSERT(ui_current_key_time > ui_previous_key_time);
  1445. if (ui_current_key_time >= MaxAge) break;
  1446. ui_previous_key_time = ui_current_key_time;
  1447. }
  1448. bool alpha_constant_at_end = (akey == new_props.NumKeyFrames);
  1449. // Reuse AlphaKeyFrameValues, AlphaKeyFrameTimes and AlphaKeyFrameDeltas if the right size,
  1450. // otherwise release and reallocate.
  1451. unsigned int new_num_alpha_key_frames = akey + 1;// Includes start keyframe (keytime == 0).
  1452. if (new_num_alpha_key_frames != NumAlphaKeyFrames) {
  1453. if (AlphaKeyFrameTimes) {
  1454. delete [] AlphaKeyFrameTimes;
  1455. AlphaKeyFrameTimes = NULL;
  1456. }
  1457. if (AlphaKeyFrameValues) {
  1458. delete [] AlphaKeyFrameValues;
  1459. AlphaKeyFrameValues = NULL;
  1460. }
  1461. if (AlphaKeyFrameDeltas) {
  1462. delete [] AlphaKeyFrameDeltas;
  1463. AlphaKeyFrameDeltas = NULL;
  1464. }
  1465. NumAlphaKeyFrames = new_num_alpha_key_frames;
  1466. AlphaKeyFrameTimes = W3DNEWARRAY unsigned int [NumAlphaKeyFrames];
  1467. AlphaKeyFrameValues = W3DNEWARRAY float [NumAlphaKeyFrames];
  1468. AlphaKeyFrameDeltas = W3DNEWARRAY float [NumAlphaKeyFrames];
  1469. }
  1470. // Set alpha keyframes (deltas will be set later)
  1471. AlphaKeyFrameTimes[0] = 0;
  1472. AlphaKeyFrameValues[0] = new_props.Start;
  1473. for (i = 1; i < NumAlphaKeyFrames; i++) {
  1474. unsigned int im1 = i - 1;
  1475. AlphaKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
  1476. AlphaKeyFrameValues[i] = new_props.Values[im1];
  1477. }
  1478. // Do deltas for all alpha keyframes except last
  1479. for (i = 0; i < NumAlphaKeyFrames - 1; i++) {
  1480. AlphaKeyFrameDeltas[i] = (AlphaKeyFrameValues[i + 1] - AlphaKeyFrameValues[i]) /
  1481. (float)(AlphaKeyFrameTimes[i + 1] - AlphaKeyFrameTimes[i]);
  1482. }
  1483. // Do delta for last alpha keyframe (i is NumAlphaKeyFrames - 1)
  1484. if (alpha_constant_at_end) {
  1485. AlphaKeyFrameDeltas[i] = 0.0f;
  1486. } else {
  1487. // This is OK because if alpha_constant_at_end is false, NumAlphaKeyFrames is equal or
  1488. // smaller than opacity.NumKeyFrames so opacity.Values[NumAlphaKeyFrames - 1] and
  1489. // opacity.KeyTimes[NumAlphaKeyFrames - 1] exist.
  1490. AlphaKeyFrameDeltas[i] = (new_props.Values[i] - AlphaKeyFrameValues[i]) /
  1491. (new_props.KeyTimes[i] * 1000.0f - (float)AlphaKeyFrameTimes[i]);
  1492. }
  1493. // Set up alpha randomizer table
  1494. if (alpha_rand_zero) {
  1495. if (RandomAlphaEntries) {
  1496. // Reuse RandomAlphaEntries if the right size, otherwise release and reallocate.
  1497. if (NumRandomAlphaEntriesMinus1 != 0) {
  1498. delete [] RandomAlphaEntries;
  1499. RandomAlphaEntries = W3DNEWARRAY float [1];
  1500. }
  1501. } else {
  1502. RandomAlphaEntries = W3DNEWARRAY float [1];
  1503. }
  1504. NumRandomAlphaEntriesMinus1 = 0;
  1505. RandomAlphaEntries[0] = 0.0f;
  1506. } else {
  1507. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  1508. unsigned int pot_num = Find_POT(MaxNum);
  1509. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  1510. if (RandomAlphaEntries) {
  1511. // Reuse RandomAlphaEntries if the right size, otherwise release and reallocate.
  1512. if (NumRandomAlphaEntriesMinus1 != (default_randomizer_entries - 1)) {
  1513. delete [] RandomAlphaEntries;
  1514. RandomAlphaEntries = W3DNEWARRAY float [default_randomizer_entries];
  1515. }
  1516. } else {
  1517. RandomAlphaEntries = W3DNEWARRAY float [default_randomizer_entries];
  1518. }
  1519. NumRandomAlphaEntriesMinus1 = default_randomizer_entries - 1;
  1520. float ascale = new_props.Rand * oo_intmax;
  1521. for (unsigned int j = 0; j <= NumRandomAlphaEntriesMinus1; j++) {
  1522. RandomAlphaEntries[j] = rand_gen * ascale;
  1523. }
  1524. }
  1525. }
  1526. }
  1527. void ParticleBufferClass::Reset_Size(ParticlePropertyStruct<float> &new_props)
  1528. {
  1529. unsigned int i; // Used in loops
  1530. unsigned int ui_previous_key_time = 0;
  1531. unsigned int ui_current_key_time = 0;
  1532. SizeRandom = new_props.Rand;
  1533. // If the randomizer is effectively zero and there are no keyframes, then we just create a
  1534. // values array with one entry and store the starting value in it (the keyframes and random
  1535. // table will not be used in this case).
  1536. static const float eps_size = 1.0e-12f; // Size scale unknown so must use very small epsilon
  1537. bool size_rand_zero = (fabs(new_props.Rand) < eps_size);
  1538. if (size_rand_zero && new_props.NumKeyFrames == 0) {
  1539. // Release Size, SizeKeyFrameTimes and SizeaKeyFrameDeltas if present. Reuse
  1540. // SizeKeyFrameValues if the right size, otherwise release and reallocate.
  1541. if (Size) {
  1542. Size->Release_Ref();
  1543. Size = NULL;
  1544. }
  1545. if (SizeKeyFrameTimes) {
  1546. delete [] SizeKeyFrameTimes;
  1547. SizeKeyFrameTimes = NULL;
  1548. }
  1549. if (SizeKeyFrameDeltas) {
  1550. delete [] SizeKeyFrameDeltas;
  1551. SizeKeyFrameDeltas = NULL;
  1552. }
  1553. if (SizeKeyFrameValues) {
  1554. if (NumSizeKeyFrames > 1) {
  1555. delete [] SizeKeyFrameValues;
  1556. SizeKeyFrameValues = W3DNEWARRAY float [1];
  1557. }
  1558. } else {
  1559. SizeKeyFrameValues = W3DNEWARRAY float [1];
  1560. }
  1561. NumSizeKeyFrames = 0;
  1562. NumRandomSizeEntriesMinus1 = 0;
  1563. SizeKeyFrameValues[0] = new_props.Start;
  1564. MaxSize = SizeKeyFrameValues[0];
  1565. } else {
  1566. // Create the size array if not present
  1567. if (!Size) {
  1568. Size = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Size") );
  1569. }
  1570. // Check times of size keyframes (each keytime must be larger than the
  1571. // previous one by at least a millisecond, and we stop at the first
  1572. // keytime of MaxAge or larger. (If all keyframes below MaxAge, size is
  1573. // constant during the last segment between last keyframe and MaxAge).
  1574. ui_previous_key_time = 0;
  1575. for (unsigned int skey = 0; skey < new_props.NumKeyFrames; skey++) {
  1576. ui_current_key_time = (unsigned int)(new_props.KeyTimes[skey] * 1000.0f);
  1577. WWASSERT(ui_current_key_time > ui_previous_key_time);
  1578. if (ui_current_key_time >= MaxAge) break;
  1579. ui_previous_key_time = ui_current_key_time;
  1580. }
  1581. bool size_constant_at_end = (skey == new_props.NumKeyFrames);
  1582. // Reuse SizeKeyFrameValues, SizeKeyFrameTimes and SizeKeyFrameDeltas if the right size,
  1583. // otherwise release and reallocate.
  1584. unsigned int new_num_size_key_frames = skey + 1;// Includes start keyframe (keytime == 0).
  1585. if (new_num_size_key_frames != NumSizeKeyFrames) {
  1586. if (SizeKeyFrameTimes) {
  1587. delete [] SizeKeyFrameTimes;
  1588. SizeKeyFrameTimes = NULL;
  1589. }
  1590. if (SizeKeyFrameValues) {
  1591. delete [] SizeKeyFrameValues;
  1592. SizeKeyFrameValues = NULL;
  1593. }
  1594. if (SizeKeyFrameDeltas) {
  1595. delete [] SizeKeyFrameDeltas;
  1596. SizeKeyFrameDeltas = NULL;
  1597. }
  1598. NumSizeKeyFrames = new_num_size_key_frames;
  1599. SizeKeyFrameTimes = W3DNEWARRAY unsigned int [NumSizeKeyFrames];
  1600. SizeKeyFrameValues = W3DNEWARRAY float [NumSizeKeyFrames];
  1601. SizeKeyFrameDeltas = W3DNEWARRAY float [NumSizeKeyFrames];
  1602. }
  1603. // Set size keyframes (deltas will be set later)
  1604. SizeKeyFrameTimes[0] = 0;
  1605. SizeKeyFrameValues[0] = new_props.Start;
  1606. for (i = 1; i < NumSizeKeyFrames; i++) {
  1607. unsigned int im1 = i - 1;
  1608. SizeKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
  1609. SizeKeyFrameValues[i] = new_props.Values[im1];
  1610. }
  1611. // Do deltas for all size keyframes except last
  1612. for (i = 0; i < NumSizeKeyFrames - 1; i++) {
  1613. SizeKeyFrameDeltas[i] = (SizeKeyFrameValues[i + 1] - SizeKeyFrameValues[i]) /
  1614. (float)(SizeKeyFrameTimes[i + 1] - SizeKeyFrameTimes[i]);
  1615. }
  1616. // Do delta for last size keyframe (i is NumSizeKeyFrames - 1)
  1617. if (size_constant_at_end) {
  1618. SizeKeyFrameDeltas[i] = 0.0f;
  1619. } else {
  1620. // This is OK because if size_constant_at_end is false, NumSizeKeyFrames is equal or
  1621. // smaller than new_props.NumKeyFrames so new_props.Values[NumSizeKeyFrames - 1] and
  1622. // new_props.KeyTimes[NumSizeKeyFrames - 1] exist.
  1623. SizeKeyFrameDeltas[i] = (new_props.Values[i] - SizeKeyFrameValues[i]) /
  1624. (new_props.KeyTimes[i] * 1000.0f - (float)SizeKeyFrameTimes[i]);
  1625. }
  1626. // Find maximum size (for BBox updates)
  1627. MaxSize = SizeKeyFrameValues[0];
  1628. for (i = 1; i < NumSizeKeyFrames; i++) {
  1629. MaxSize = MAX(MaxSize, SizeKeyFrameValues[i]);
  1630. }
  1631. // If last delta is positive, there may be a larger size keyframe:
  1632. float last_size = SizeKeyFrameValues[NumSizeKeyFrames - 1] + SizeKeyFrameDeltas[NumSizeKeyFrames - 1] *
  1633. (float)(MaxAge - SizeKeyFrameTimes[NumSizeKeyFrames - 1]);
  1634. MaxSize = MAX(MaxSize, last_size);
  1635. MaxSize += fabs(new_props.Rand);
  1636. // Set up size randomizer table
  1637. if (size_rand_zero) {
  1638. if (RandomSizeEntries) {
  1639. // Reuse RandomSizeEntries if the right size, otherwise release and reallocate.
  1640. if (NumRandomSizeEntriesMinus1 != 0) {
  1641. delete [] RandomSizeEntries;
  1642. RandomSizeEntries = W3DNEWARRAY float [1];
  1643. }
  1644. } else {
  1645. RandomSizeEntries = W3DNEWARRAY float [1];
  1646. }
  1647. NumRandomSizeEntriesMinus1 = 0;
  1648. RandomSizeEntries[0] = 0.0f;
  1649. } else {
  1650. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  1651. unsigned int pot_num = Find_POT(MaxNum);
  1652. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  1653. if (RandomSizeEntries) {
  1654. // Reuse RandomSizeEntries if the right size, otherwise release and reallocate.
  1655. if (NumRandomSizeEntriesMinus1 != (default_randomizer_entries - 1)) {
  1656. delete [] RandomSizeEntries;
  1657. RandomSizeEntries = W3DNEWARRAY float [default_randomizer_entries];
  1658. }
  1659. } else {
  1660. RandomSizeEntries = W3DNEWARRAY float [default_randomizer_entries];
  1661. }
  1662. NumRandomSizeEntriesMinus1 = default_randomizer_entries - 1;
  1663. float sscale = new_props.Rand * oo_intmax;
  1664. for (unsigned int j = 0; j <= NumRandomSizeEntriesMinus1; j++) {
  1665. RandomSizeEntries[j] = rand_gen * sscale;
  1666. }
  1667. }
  1668. }
  1669. }
  1670. void ParticleBufferClass::Reset_Rotations(ParticlePropertyStruct<float> &new_props, float orient_rnd)
  1671. {
  1672. unsigned int i; // Used in loops
  1673. float oo_intmax = 1.0f / (float)INT_MAX;
  1674. unsigned int ui_previous_key_time = 0;
  1675. unsigned int ui_current_key_time = 0;
  1676. /*
  1677. ** NOTE: Input rotations are in rotations per second. These will be converted to rotations per millisecond.
  1678. */
  1679. RotationRandom = new_props.Rand * 0.001f;
  1680. InitialOrientationRandom = orient_rnd;
  1681. // If both randomizers are effectively zero and rotation is constant zero, then all arrays are NULL.
  1682. static const float eps_orientation = 2.77777778e-4f; // Epsilon is equivalent to 0.1 degree
  1683. static const float eps_rotation = 2.77777778e-4f; // Epsilon is equivalent to one rotation per hour (in rotations / second)
  1684. bool orientation_rand_zero = fabs(orient_rnd) < eps_orientation;
  1685. bool rotation_rand_zero = fabs(new_props.Rand) < eps_rotation;
  1686. if (orientation_rand_zero && rotation_rand_zero && new_props.NumKeyFrames == 0 && fabs(new_props.Start) < eps_rotation) {
  1687. // Release Arrays,
  1688. REF_PTR_RELEASE(Orientation);
  1689. if (RotationKeyFrameTimes) {
  1690. delete [] RotationKeyFrameTimes;
  1691. RotationKeyFrameTimes = NULL;
  1692. }
  1693. if (HalfRotationKeyFrameDeltas) {
  1694. delete [] HalfRotationKeyFrameDeltas;
  1695. HalfRotationKeyFrameDeltas = NULL;
  1696. }
  1697. if (RotationKeyFrameValues) {
  1698. delete [] RotationKeyFrameValues;
  1699. RotationKeyFrameValues = NULL;
  1700. }
  1701. if (OrientationKeyFrameValues) {
  1702. delete [] OrientationKeyFrameValues;
  1703. OrientationKeyFrameValues = NULL;
  1704. }
  1705. NumRotationKeyFrames = 0;
  1706. NumRandomRotationEntriesMinus1 = 0;
  1707. NumRandomOrientationEntriesMinus1 = 0;
  1708. } else {
  1709. // Create the array if not present
  1710. if (!Orientation) {
  1711. Orientation = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Orientation") );
  1712. }
  1713. // Check times of the keyframes (each keytime must be larger than the
  1714. // previous one by at least a millisecond, and we stop at the first
  1715. // keytime of MaxAge or larger. (If all keyframes below MaxAge, the value is
  1716. // constant during the last segment between last keyframe and MaxAge).
  1717. ui_previous_key_time = 0;
  1718. for (unsigned int key = 0; key < new_props.NumKeyFrames; key++) {
  1719. ui_current_key_time = (unsigned int)(new_props.KeyTimes[key] * 1000.0f);
  1720. WWASSERT(ui_current_key_time > ui_previous_key_time);
  1721. if (ui_current_key_time >= MaxAge) break;
  1722. ui_previous_key_time = ui_current_key_time;
  1723. }
  1724. bool rotation_constant_at_end = (key == new_props.NumKeyFrames);
  1725. // Reuse RotationKeyFrameValues, RotationKeyFrameTimes, RotationKeyFrameDeltas and
  1726. // OrientationKeyFrameValues if the right size, otherwise release and reallocate.
  1727. unsigned int new_num_key_frames = key + 1;// Includes start keyframe (keytime == 0).
  1728. if (new_num_key_frames != NumRotationKeyFrames) {
  1729. if (RotationKeyFrameTimes) {
  1730. delete [] RotationKeyFrameTimes;
  1731. RotationKeyFrameTimes = NULL;
  1732. }
  1733. if (RotationKeyFrameValues) {
  1734. delete [] RotationKeyFrameValues;
  1735. RotationKeyFrameValues = NULL;
  1736. }
  1737. if (HalfRotationKeyFrameDeltas) {
  1738. delete [] HalfRotationKeyFrameDeltas;
  1739. HalfRotationKeyFrameDeltas = NULL;
  1740. }
  1741. if (OrientationKeyFrameValues) {
  1742. delete [] OrientationKeyFrameValues;
  1743. OrientationKeyFrameValues = NULL;
  1744. }
  1745. NumRotationKeyFrames = new_num_key_frames;
  1746. RotationKeyFrameTimes = W3DNEWARRAY unsigned int [NumRotationKeyFrames];
  1747. RotationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
  1748. HalfRotationKeyFrameDeltas = W3DNEWARRAY float [NumRotationKeyFrames];
  1749. OrientationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
  1750. }
  1751. // Set rotation keyframes (deltas will be set later)
  1752. RotationKeyFrameTimes[0] = 0;
  1753. RotationKeyFrameValues[0] = new_props.Start * 0.001f;
  1754. for (i = 1; i < NumRotationKeyFrames; i++) {
  1755. unsigned int im1 = i - 1;
  1756. RotationKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
  1757. RotationKeyFrameValues[i] = new_props.Values[im1] * 0.001f;
  1758. }
  1759. // Do deltas for all rotation keyframes except last
  1760. for (i = 0; i < NumRotationKeyFrames - 1; i++) {
  1761. HalfRotationKeyFrameDeltas[i] = 0.5f * ( (RotationKeyFrameValues[i + 1] - RotationKeyFrameValues[i]) /
  1762. (float)(RotationKeyFrameTimes[i + 1] - RotationKeyFrameTimes[i]) );
  1763. }
  1764. // Do delta for last rotation keyframe (i is NumRotationKeyFrames - 1)
  1765. if (rotation_constant_at_end) {
  1766. HalfRotationKeyFrameDeltas[i] = 0.0f;
  1767. } else {
  1768. // This is OK because if rotation_constant_at_end is false, NumRotationKeyFrames is equal or
  1769. // smaller than new_props.NumKeyFrames so new_props.Values[NumRotationKeyFrames - 1] and
  1770. // new_props.KeyTimes[NumRotationKeyFrames - 1] exist.
  1771. HalfRotationKeyFrameDeltas[i] = 0.5f * (new_props.Values[i] * 0.001f - RotationKeyFrameValues[i]) /
  1772. (new_props.KeyTimes[i] * 1000.0f - (float)RotationKeyFrameTimes[i]);
  1773. }
  1774. // Calculate orientation keyframes by integrating the rotation at each keyframe
  1775. OrientationKeyFrameValues[0] = 0.0f;
  1776. for (i = 1; i < NumRotationKeyFrames; i++) {
  1777. float delta_t = (float)(RotationKeyFrameTimes[i] - RotationKeyFrameTimes[i - 1]);
  1778. OrientationKeyFrameValues[i] = OrientationKeyFrameValues[i - 1] + delta_t *
  1779. (RotationKeyFrameValues[i - 1] + HalfRotationKeyFrameDeltas[i - 1] * delta_t);
  1780. }
  1781. // Set up rotation randomizer table
  1782. if (rotation_rand_zero) {
  1783. if (RandomRotationEntries) {
  1784. // Reuse RandomRotationEntries if the right size, otherwise release and reallocate.
  1785. if (NumRandomRotationEntriesMinus1 != 0) {
  1786. delete [] RandomRotationEntries;
  1787. RandomRotationEntries = W3DNEWARRAY float [1];
  1788. }
  1789. } else {
  1790. RandomRotationEntries = W3DNEWARRAY float [1];
  1791. }
  1792. NumRandomRotationEntriesMinus1 = 0;
  1793. RandomRotationEntries[0] = 0.0f;
  1794. } else {
  1795. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  1796. unsigned int pot_num = Find_POT(MaxNum);
  1797. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  1798. if (RandomRotationEntries) {
  1799. // Reuse RandomRotationEntries if the right size, otherwise release and reallocate.
  1800. if (NumRandomRotationEntriesMinus1 != (default_randomizer_entries - 1)) {
  1801. delete [] RandomRotationEntries;
  1802. RandomRotationEntries = W3DNEWARRAY float [default_randomizer_entries];
  1803. }
  1804. } else {
  1805. RandomRotationEntries = W3DNEWARRAY float [default_randomizer_entries];
  1806. }
  1807. NumRandomRotationEntriesMinus1 = default_randomizer_entries - 1;
  1808. float scale = new_props.Rand * 0.001f * oo_intmax;
  1809. for (unsigned int j = 0; j <= NumRandomRotationEntriesMinus1; j++) {
  1810. RandomRotationEntries[j] = rand_gen * scale;
  1811. }
  1812. }
  1813. // Set up orientation randomizer table
  1814. if (orientation_rand_zero) {
  1815. if (RandomOrientationEntries) {
  1816. // Reuse RandomOrientationEntries if the right size, otherwise release and reallocate.
  1817. if (NumRandomOrientationEntriesMinus1 != 0) {
  1818. delete [] RandomOrientationEntries;
  1819. RandomOrientationEntries = W3DNEWARRAY float [1];
  1820. }
  1821. } else {
  1822. RandomOrientationEntries = W3DNEWARRAY float [1];
  1823. }
  1824. NumRandomOrientationEntriesMinus1 = 0;
  1825. RandomOrientationEntries[0] = 0.0f;
  1826. } else {
  1827. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  1828. unsigned int pot_num = Find_POT(MaxNum);
  1829. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  1830. if (RandomOrientationEntries) {
  1831. // Reuse RandomOrientationEntries if the right size, otherwise release and reallocate.
  1832. if (NumRandomOrientationEntriesMinus1 != (default_randomizer_entries - 1)) {
  1833. delete [] RandomOrientationEntries;
  1834. RandomOrientationEntries = W3DNEWARRAY float [default_randomizer_entries];
  1835. }
  1836. } else {
  1837. RandomOrientationEntries = W3DNEWARRAY float [default_randomizer_entries];
  1838. }
  1839. NumRandomOrientationEntriesMinus1 = default_randomizer_entries - 1;
  1840. float scale = orient_rnd * oo_intmax;
  1841. for (unsigned int j = 0; j <= NumRandomOrientationEntriesMinus1; j++) {
  1842. RandomOrientationEntries[j] = rand_gen * scale;
  1843. }
  1844. }
  1845. }
  1846. }
  1847. void ParticleBufferClass::Reset_Frames(ParticlePropertyStruct<float> &new_props)
  1848. {
  1849. unsigned int i; // Used in loops
  1850. float oo_intmax = 1.0f / (float)INT_MAX;
  1851. unsigned int ui_previous_key_time = 0;
  1852. unsigned int ui_current_key_time = 0;
  1853. FrameRandom = new_props.Rand;
  1854. // If the randomizer is effectively zero and there are no keyframes, then we just create a
  1855. // values array with one entry and store the starting value in it (the keyframes and random
  1856. // table will not be used in this case).
  1857. static const float eps_frame = 0.1f; // Epsilon is equivalent to 0.1 frame
  1858. bool frame_rand_zero = (fabs(new_props.Rand) < eps_frame);
  1859. if (frame_rand_zero && new_props.NumKeyFrames == 0) {
  1860. // Release Arrays, Reuse KeyFrameValues if the right size,
  1861. // otherwise release and reallocate.
  1862. REF_PTR_RELEASE(Frame);
  1863. REF_PTR_RELEASE(UCoord);
  1864. if (FrameKeyFrameTimes) {
  1865. delete [] FrameKeyFrameTimes;
  1866. FrameKeyFrameTimes = NULL;
  1867. }
  1868. if (FrameKeyFrameDeltas) {
  1869. delete [] FrameKeyFrameDeltas;
  1870. FrameKeyFrameDeltas = NULL;
  1871. }
  1872. if (FrameKeyFrameValues) {
  1873. if (NumFrameKeyFrames > 1) {
  1874. delete [] FrameKeyFrameValues;
  1875. FrameKeyFrameValues = W3DNEWARRAY float [1];
  1876. }
  1877. } else {
  1878. FrameKeyFrameValues = W3DNEWARRAY float [1];
  1879. }
  1880. NumFrameKeyFrames = 0;
  1881. NumRandomFrameEntriesMinus1 = 0;
  1882. FrameKeyFrameValues[0] = new_props.Start;
  1883. } else {
  1884. // Create the array if not present
  1885. if ((RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA) ||
  1886. (RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM)) {
  1887. if (!UCoord) {
  1888. UCoord = NEW_REF( ShareBufferClass<float>, (MaxNum, "ParticleBufferClass::UCoord") );
  1889. }
  1890. } else {
  1891. if (!Frame) {
  1892. Frame = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Frame") );
  1893. }
  1894. }
  1895. // Check times of the keyframes (each keytime must be larger than the
  1896. // previous one by at least a millisecond, and we stop at the first
  1897. // keytime of MaxAge or larger. (If all keyframes below MaxAge, the value is
  1898. // constant during the last segment between last keyframe and MaxAge).
  1899. ui_previous_key_time = 0;
  1900. for (unsigned int key = 0; key < new_props.NumKeyFrames; key++) {
  1901. ui_current_key_time = (unsigned int)(new_props.KeyTimes[key] * 1000.0f);
  1902. WWASSERT(ui_current_key_time > ui_previous_key_time);
  1903. if (ui_current_key_time >= MaxAge) break;
  1904. ui_previous_key_time = ui_current_key_time;
  1905. }
  1906. bool frame_constant_at_end = (key == new_props.NumKeyFrames);
  1907. // Reuse FrameKeyFrameValues, FrameKeyFrameTimes and FrameKeyFrameDeltas if the right size,
  1908. // otherwise release and reallocate.
  1909. unsigned int new_num_key_frames = key + 1;// Includes start keyframe (keytime == 0).
  1910. if (new_num_key_frames != NumFrameKeyFrames) {
  1911. if (FrameKeyFrameTimes) {
  1912. delete [] FrameKeyFrameTimes;
  1913. FrameKeyFrameTimes = NULL;
  1914. }
  1915. if (FrameKeyFrameValues) {
  1916. delete [] FrameKeyFrameValues;
  1917. FrameKeyFrameValues = NULL;
  1918. }
  1919. if (FrameKeyFrameDeltas) {
  1920. delete [] FrameKeyFrameDeltas;
  1921. FrameKeyFrameDeltas = NULL;
  1922. }
  1923. NumFrameKeyFrames = new_num_key_frames;
  1924. FrameKeyFrameTimes = W3DNEWARRAY unsigned int [NumFrameKeyFrames];
  1925. FrameKeyFrameValues = W3DNEWARRAY float [NumFrameKeyFrames];
  1926. FrameKeyFrameDeltas = W3DNEWARRAY float [NumFrameKeyFrames];
  1927. }
  1928. // Set keyframes (deltas will be set later)
  1929. FrameKeyFrameTimes[0] = 0;
  1930. FrameKeyFrameValues[0] = new_props.Start;
  1931. for (i = 1; i < NumFrameKeyFrames; i++) {
  1932. unsigned int im1 = i - 1;
  1933. FrameKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
  1934. FrameKeyFrameValues[i] = new_props.Values[im1];
  1935. }
  1936. // Do deltas for all frame keyframes except last
  1937. for (i = 0; i < NumFrameKeyFrames - 1; i++) {
  1938. FrameKeyFrameDeltas[i] = (FrameKeyFrameValues[i + 1] - FrameKeyFrameValues[i]) /
  1939. (float)(FrameKeyFrameTimes[i + 1] - FrameKeyFrameTimes[i]);
  1940. }
  1941. // Do delta for last frame keyframe (i is NumFrameKeyFrames - 1)
  1942. if (frame_constant_at_end) {
  1943. FrameKeyFrameDeltas[i] = 0.0f;
  1944. } else {
  1945. // This is OK because if frame_constant_at_end is false, NumFrameKeyFrames is equal or
  1946. // smaller than new_props.NumKeyFrames so new_props.Values[NumFrameKeyFrames - 1] and
  1947. // new_props.KeyTimes[NumFrameKeyFrames - 1] exist.
  1948. FrameKeyFrameDeltas[i] = (new_props.Values[i] - FrameKeyFrameValues[i]) /
  1949. (new_props.KeyTimes[i] * 1000.0f - (float)FrameKeyFrameTimes[i]);
  1950. }
  1951. // Set up frame randomizer table
  1952. if (frame_rand_zero) {
  1953. if (RandomFrameEntries) {
  1954. // Reuse RandomFrameEntries if the right size, otherwise release and reallocate.
  1955. if (NumRandomFrameEntriesMinus1 != 0) {
  1956. delete [] RandomFrameEntries;
  1957. RandomFrameEntries = W3DNEWARRAY float [1];
  1958. }
  1959. } else {
  1960. RandomFrameEntries = W3DNEWARRAY float [1];
  1961. }
  1962. NumRandomFrameEntriesMinus1 = 0;
  1963. RandomFrameEntries[0] = 0.0f;
  1964. } else {
  1965. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  1966. unsigned int pot_num = Find_POT(MaxNum);
  1967. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  1968. if (RandomFrameEntries) {
  1969. // Reuse RandomFrameEntries if the right size, otherwise release and reallocate.
  1970. if (NumRandomFrameEntriesMinus1 != (default_randomizer_entries - 1)) {
  1971. delete [] RandomFrameEntries;
  1972. RandomFrameEntries = W3DNEWARRAY float [default_randomizer_entries];
  1973. }
  1974. } else {
  1975. RandomFrameEntries = W3DNEWARRAY float [default_randomizer_entries];
  1976. }
  1977. NumRandomFrameEntriesMinus1 = default_randomizer_entries - 1;
  1978. float scale = new_props.Rand * oo_intmax;
  1979. for (unsigned int j = 0; j <= NumRandomFrameEntriesMinus1; j++) {
  1980. RandomFrameEntries[j] = rand_gen * scale;
  1981. }
  1982. }
  1983. }
  1984. }
  1985. void ParticleBufferClass::Reset_Blur_Times(ParticlePropertyStruct<float> &new_blur_times)
  1986. {
  1987. unsigned int i; // Used in loops
  1988. float oo_intmax = 1.0f / (float)INT_MAX;
  1989. unsigned int ui_previous_key_time = 0;
  1990. unsigned int ui_current_key_time = 0;
  1991. BlurTimeRandom = new_blur_times.Rand;
  1992. // If the randomizer is effectively zero and there are no keyframes, then we just create a
  1993. // values array with one entry and store the starting value in it (the keyframes and random
  1994. // table will not be used in this case).
  1995. static const float eps_blur = 1e-5f; // Epsilon is equivalent to 1e-5 units per second
  1996. bool blurtime_rand_zero = (fabs(new_blur_times.Rand) < eps_blur);
  1997. if (blurtime_rand_zero && new_blur_times.NumKeyFrames == 0) {
  1998. // Release Arrays, Reuse KeyFrameValues if the right size,
  1999. // otherwise release and reallocate.
  2000. if (BlurTimeKeyFrameTimes) {
  2001. delete [] BlurTimeKeyFrameTimes;
  2002. BlurTimeKeyFrameTimes = NULL;
  2003. }
  2004. if (BlurTimeKeyFrameDeltas) {
  2005. delete [] BlurTimeKeyFrameDeltas;
  2006. BlurTimeKeyFrameDeltas = NULL;
  2007. }
  2008. if (BlurTimeKeyFrameValues) {
  2009. if (NumBlurTimeKeyFrames > 1) {
  2010. delete [] BlurTimeKeyFrameValues;
  2011. BlurTimeKeyFrameValues = new float [1];
  2012. }
  2013. } else {
  2014. BlurTimeKeyFrameValues = new float [1];
  2015. }
  2016. NumBlurTimeKeyFrames = 0;
  2017. NumRandomBlurTimeEntriesMinus1 = 0;
  2018. BlurTimeKeyFrameValues[0] = new_blur_times.Start;
  2019. } else {
  2020. // Check times of the keyframes (each keytime must be larger than the
  2021. // previous one by at least a millisecond, and we stop at the first
  2022. // keytime of MaxAge or larger. (If all keyframes below MaxAge, the value is
  2023. // constant during the last segment between last keyframe and MaxAge).
  2024. ui_previous_key_time = 0;
  2025. for (unsigned int key = 0; key < new_blur_times.NumKeyFrames; key++) {
  2026. ui_current_key_time = (unsigned int)(new_blur_times.KeyTimes[key] * 1000.0f);
  2027. WWASSERT(ui_current_key_time > ui_previous_key_time);
  2028. if (ui_current_key_time >= MaxAge) break;
  2029. ui_previous_key_time = ui_current_key_time;
  2030. }
  2031. bool blurtime_constant_at_end = (key == new_blur_times.NumKeyFrames);
  2032. // Reuse BlurTimeKeyFrameValues, BlurTimeKeyFrameTimes and BlurTimeKeyFrameDeltas if the right size,
  2033. // otherwise release and reallocate.
  2034. unsigned int new_num_key_frames = key + 1;// Includes start keyframe (keytime == 0).
  2035. if (new_num_key_frames != NumBlurTimeKeyFrames) {
  2036. if (BlurTimeKeyFrameTimes) {
  2037. delete [] BlurTimeKeyFrameTimes;
  2038. BlurTimeKeyFrameTimes = NULL;
  2039. }
  2040. if (BlurTimeKeyFrameValues) {
  2041. delete [] BlurTimeKeyFrameValues;
  2042. BlurTimeKeyFrameValues = NULL;
  2043. }
  2044. if (BlurTimeKeyFrameDeltas) {
  2045. delete [] BlurTimeKeyFrameDeltas;
  2046. BlurTimeKeyFrameDeltas = NULL;
  2047. }
  2048. NumBlurTimeKeyFrames = new_num_key_frames;
  2049. BlurTimeKeyFrameTimes = new unsigned int [NumBlurTimeKeyFrames];
  2050. BlurTimeKeyFrameValues = new float [NumBlurTimeKeyFrames];
  2051. BlurTimeKeyFrameDeltas = new float [NumBlurTimeKeyFrames];
  2052. }
  2053. // Set keyframes (deltas will be set later)
  2054. BlurTimeKeyFrameTimes[0] = 0;
  2055. BlurTimeKeyFrameValues[0] = new_blur_times.Start;
  2056. for (i = 1; i < NumBlurTimeKeyFrames; i++) {
  2057. unsigned int im1 = i - 1;
  2058. BlurTimeKeyFrameTimes[i] = (unsigned int)(new_blur_times.KeyTimes[im1] * 1000.0f);
  2059. BlurTimeKeyFrameValues[i] = new_blur_times.Values[im1];
  2060. }
  2061. // Do deltas for all frame keyframes except last
  2062. for (i = 0; i < NumBlurTimeKeyFrames - 1; i++) {
  2063. BlurTimeKeyFrameDeltas[i] = (BlurTimeKeyFrameValues[i + 1] - BlurTimeKeyFrameValues[i]) /
  2064. (float)(BlurTimeKeyFrameTimes[i + 1] - BlurTimeKeyFrameTimes[i]);
  2065. }
  2066. // Do delta for last frame keyframe (i is NumBlurTimeKeyFrames - 1)
  2067. if (blurtime_constant_at_end) {
  2068. BlurTimeKeyFrameDeltas[i] = 0.0f;
  2069. } else {
  2070. // This is OK because if frame_constant_at_end is false, NumBlurTimeKeyFrames is equal or
  2071. // smaller than new_props.NumKeyFrames so new_props.Values[NumBlurTimeKeyFrames - 1] and
  2072. // new_props.KeyTimes[NumBlurTimeKeyFrames - 1] exist.
  2073. BlurTimeKeyFrameDeltas[i] = (new_blur_times.Values[i] - BlurTimeKeyFrameValues[i]) /
  2074. (new_blur_times.KeyTimes[i] * 1000.0f - (float)BlurTimeKeyFrameTimes[i]);
  2075. }
  2076. // Set up frame randomizer table
  2077. if (blurtime_rand_zero) {
  2078. if (RandomBlurTimeEntries) {
  2079. // Reuse RandomBlurTimeEntries if the right size, otherwise release and reallocate.
  2080. if (NumRandomBlurTimeEntriesMinus1 != 0) {
  2081. delete [] RandomBlurTimeEntries;
  2082. RandomBlurTimeEntries = new float [1];
  2083. }
  2084. } else {
  2085. RandomBlurTimeEntries = new float [1];
  2086. }
  2087. NumRandomBlurTimeEntriesMinus1 = 0;
  2088. RandomBlurTimeEntries[0] = 0.0f;
  2089. } else {
  2090. // Default size of randomizer tables (tables for non-zero randomizers will be this size)
  2091. unsigned int pot_num = Find_POT(MaxNum);
  2092. unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
  2093. if (RandomBlurTimeEntries) {
  2094. // Reuse RandomBlurTimeEntries if the right size, otherwise release and reallocate.
  2095. if (NumRandomBlurTimeEntriesMinus1 != (default_randomizer_entries - 1)) {
  2096. delete [] RandomBlurTimeEntries;
  2097. RandomBlurTimeEntries = new float [default_randomizer_entries];
  2098. }
  2099. } else {
  2100. RandomBlurTimeEntries = new float [default_randomizer_entries];
  2101. }
  2102. NumRandomBlurTimeEntriesMinus1 = default_randomizer_entries - 1;
  2103. float scale = new_blur_times.Rand * oo_intmax;
  2104. for (unsigned int j = 0; j <= NumRandomBlurTimeEntriesMinus1; j++) {
  2105. RandomBlurTimeEntries[j] = rand_gen * scale;
  2106. }
  2107. }
  2108. }
  2109. }
  2110. // This informs the buffer that the emitter is dead, so it can release
  2111. // its pointer to it and be removed itself after all its particles dies
  2112. // out.
  2113. void ParticleBufferClass::Emitter_Is_Dead(void)
  2114. {
  2115. IsEmitterDead = true;
  2116. // We do not have a ref for the emitter (see DTor for detailed explanation)
  2117. // Emitter->Release_Ref();
  2118. Emitter = NULL;
  2119. }
  2120. // This set's the buffer's current emitter - this should usually be
  2121. // called only by the emitter's copy constructor after it clones a
  2122. // buffer.
  2123. void ParticleBufferClass::Set_Emitter(ParticleEmitterClass *emitter)
  2124. {
  2125. if (Emitter) {
  2126. // We do not have a ref for the emitter (see DTor for detailed explanation)
  2127. // Emitter->Release_Ref();
  2128. Emitter = NULL;
  2129. }
  2130. Emitter = emitter;
  2131. if (Emitter) {
  2132. // We do not add a ref for the emitter (see DTor for detailed explanation)
  2133. // Emitter->Add_Ref();
  2134. }
  2135. }
  2136. NewParticleStruct * ParticleBufferClass::Add_Uninitialized_New_Particle(void)
  2137. {
  2138. // Note that this function does not initialize the new particle - it
  2139. // returns its address to a different function which performs the actual
  2140. // initialization.
  2141. // Push new particle on new particle queue. If it overflows, just adjust
  2142. // queue to remove oldest member (which is the one which was overwritten).
  2143. NewParticleStruct *ptr = &(NewParticleQueue[NewParticleQueueEnd]);
  2144. if (++NewParticleQueueEnd == MaxNum) NewParticleQueueEnd = 0;
  2145. if (++NewParticleQueueCount == (signed)(MaxNum + 1)) {
  2146. // Overflow - advance queue start:
  2147. if (++NewParticleQueueStart == MaxNum) NewParticleQueueStart = 0;
  2148. NewParticleQueueCount--;
  2149. }
  2150. return ptr;
  2151. }
  2152. void ParticleBufferClass::Update_Cached_Bounding_Volumes(void) const
  2153. {
  2154. // This ugly cast is done because the alternative is to make everything
  2155. // in the class mutable, which does not seem like a good solution
  2156. // (Update_Bounding_Box can potentially update the particle state).
  2157. ((ParticleBufferClass *)this)->Update_Bounding_Box();
  2158. // Update cached bounding box and sphere according to the bounding box:
  2159. CachedBoundingSphere.Init(BoundingBox.Center, BoundingBox.Extent.Length());
  2160. CachedBoundingBox = BoundingBox;
  2161. Validate_Cached_Bounding_Volumes();
  2162. }
  2163. void ParticleBufferClass::Update_Kinematic_Particle_State(void)
  2164. {
  2165. // Note: elapsed may be very large indeed the first time the object is
  2166. // updated, but this doesn't matter, since it is actually only used in
  2167. // Update_Non_New_Particles(), which is never called on the first update.
  2168. unsigned int elapsed = WW3D::Get_Sync_Time() - LastUpdateTime;
  2169. if (elapsed == 0U) return;
  2170. // Get new particles from the input buffer and write them into the circular
  2171. // particle buffer, possibly overwriting older particles. Update each
  2172. // according to its age.
  2173. Get_New_Particles();
  2174. // Kill all remaining particles which will pass their max age this update.
  2175. Kill_Old_Particles();
  2176. // Update all living, non-new particles by a uniform time interval.
  2177. if (NonNewNum > 0) Update_Non_New_Particles(elapsed);
  2178. // Mark all new particles as non-new.
  2179. End = NewEnd;
  2180. NonNewNum += NewNum;
  2181. NewNum = 0;
  2182. LastUpdateTime = WW3D::Get_Sync_Time();
  2183. BoundingBoxDirty = true;
  2184. }
  2185. void ParticleBufferClass::Update_Visual_Particle_State(void)
  2186. {
  2187. // NOTE: The visual state (color/alpha/size) is "stateless" in that each time it is calculated
  2188. // without referring to what it was before. This is important for when we optimize the particle
  2189. // systems/pointgroups in the future to chunk triangles into reusable small buffers.
  2190. // If all visual state is constant do nothing.
  2191. // Linegroup modes have a visual state that always have to be updated though
  2192. bool is_linegroup=( (RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA) ||
  2193. (RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM));
  2194. if (!Color && !Alpha && !Size && !Orientation && !Frame && !UCoord && !is_linegroup) return;
  2195. // In the general case, a range in a circular buffer can be composed of up
  2196. // to two subranges. Find the Start - End subranges.
  2197. unsigned int sub1_end; // End of subrange 1.
  2198. unsigned int sub2_start; // Start of subrange 2.
  2199. if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
  2200. sub1_end = End;
  2201. sub2_start = End;
  2202. } else {
  2203. sub1_end = MaxNum;
  2204. sub2_start = 0;
  2205. }
  2206. unsigned int current_time = WW3D::Get_Sync_Time();
  2207. // The following back-to-back pair of "for" loops traverses the circular
  2208. // buffer subranges in proper order.
  2209. unsigned int ckey = NumColorKeyFrames - 1;
  2210. unsigned int akey = NumAlphaKeyFrames - 1;
  2211. unsigned int skey = NumSizeKeyFrames - 1;
  2212. unsigned int rkey = NumRotationKeyFrames - 1;
  2213. unsigned int fkey = NumFrameKeyFrames - 1;
  2214. unsigned int bkey = NumBlurTimeKeyFrames -1;
  2215. unsigned int part;
  2216. Vector3 *color = Color ? Color->Get_Array(): NULL;
  2217. float *alpha = Alpha ? Alpha->Get_Array(): NULL;
  2218. float *size = Size ? Size->Get_Array(): NULL;
  2219. uint8 *orientation = Orientation ? Orientation->Get_Array(): NULL;
  2220. uint8 *frame = Frame ? Frame->Get_Array(): NULL;
  2221. float *ucoord = UCoord ? UCoord->Get_Array() : NULL;
  2222. Vector3 *tailposition = TailPosition ? TailPosition->Get_Array() : NULL;
  2223. Vector3 *position=NULL;
  2224. if (PingPongPosition) {
  2225. int pingpong = WW3D::Get_Frame_Count() & 0x1;
  2226. position = Position[pingpong]->Get_Array();
  2227. } else {
  2228. position = Position[0]->Get_Array();
  2229. }
  2230. for (part = Start; part < sub1_end; part++) {
  2231. unsigned int part_age = current_time - TimeStamp[part];
  2232. // Ensure the current color keyframe is correct, and calculate color state
  2233. if (color) {
  2234. // We go from older to younger particles, so we go backwards from the last keyframe until
  2235. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2236. for (; part_age < ColorKeyFrameTimes[ckey]; ckey--);
  2237. color[part] = ColorKeyFrameValues[ckey] +
  2238. ColorKeyFrameDeltas[ckey] * (float)(part_age - ColorKeyFrameTimes[ckey]) +
  2239. RandomColorEntries[part & NumRandomColorEntriesMinus1];
  2240. }
  2241. // Ensure the current alpha keyframe is correct, and calculate alpha state
  2242. if (alpha) {
  2243. // We go from older to younger particles, so we go backwards from the last keyframe until
  2244. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2245. for (; part_age < AlphaKeyFrameTimes[akey]; akey--);
  2246. alpha[part] = AlphaKeyFrameValues[akey] +
  2247. AlphaKeyFrameDeltas[akey] * (float)(part_age - AlphaKeyFrameTimes[akey]) +
  2248. RandomAlphaEntries[part & NumRandomAlphaEntriesMinus1];
  2249. }
  2250. // Ensure the current size keyframe is correct, and calculate size state
  2251. if (size) {
  2252. // We go from older to younger particles, so we go backwards from the last keyframe until
  2253. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2254. for (; part_age < SizeKeyFrameTimes[skey]; skey--);
  2255. size[part] = SizeKeyFrameValues[skey] +
  2256. SizeKeyFrameDeltas[skey] * (float)(part_age - SizeKeyFrameTimes[skey]) +
  2257. RandomSizeEntries[part & NumRandomSizeEntriesMinus1];
  2258. // Size (unlike color and alpha) isn't clamped in the engine, so we need to clamp
  2259. // negative values to zero here:
  2260. size[part] = (size[part] >= 0.0f) ? size[part] : 0.0f;
  2261. }
  2262. // Ensure the current rotation keyframe is correct, and calculate orientation state
  2263. if (orientation) {
  2264. // We go from older to younger particles, so we go backwards from the last keyframe until
  2265. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2266. for (; part_age < RotationKeyFrameTimes[rkey]; rkey--);
  2267. float f_delta_t = (float)(part_age - RotationKeyFrameTimes[rkey]);
  2268. float tmp_orient = OrientationKeyFrameValues[rkey] +
  2269. (RotationKeyFrameValues[rkey] + HalfRotationKeyFrameDeltas[rkey] * f_delta_t) * f_delta_t +
  2270. RandomRotationEntries[part & NumRandomRotationEntriesMinus1] * (float)part_age +
  2271. RandomOrientationEntries[part & NumRandomOrientationEntriesMinus1];
  2272. orientation[part] = (uint)(((int)(tmp_orient * 256.0f)) & 0xFF);
  2273. }
  2274. // Ensure the current frame keyframe is correct, and calculate frame state
  2275. if (frame) {
  2276. // Frame and ucoord are mutually exclusive
  2277. WWASSERT(ucoord==NULL);
  2278. // We go from older to younger particles, so we go backwards from the last keyframe until
  2279. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2280. for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
  2281. float tmp_frame = FrameKeyFrameValues[fkey] +
  2282. FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
  2283. RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
  2284. frame[part] = (uint)(((int)(tmp_frame)) & 0xFF);
  2285. }
  2286. // Ensure the current frame keyframe is correct, and calculate frame state
  2287. // ucoord is the same as frame but in float
  2288. if (ucoord) {
  2289. // Frame and ucoord are mutually exclusive
  2290. WWASSERT(frame==NULL);
  2291. // We go from older to younger particles, so we go backwards from the last keyframe until
  2292. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2293. for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
  2294. ucoord[part] = FrameKeyFrameValues[fkey] +
  2295. FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
  2296. RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
  2297. }
  2298. if (tailposition) {
  2299. // We go from older to younger particles, so we go backwards from the last keyframe until
  2300. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2301. float blur_time = BlurTimeKeyFrameValues[0];
  2302. if (BlurTimeKeyFrameTimes) {
  2303. for (; part_age < BlurTimeKeyFrameTimes[bkey]; bkey--);
  2304. blur_time = BlurTimeKeyFrameValues[bkey] +
  2305. BlurTimeKeyFrameDeltas[bkey] * (float)(part_age - BlurTimeKeyFrameTimes[bkey]) +
  2306. RandomBlurTimeEntries[part & NumRandomBlurTimeEntriesMinus1];
  2307. }
  2308. tailposition[part]=position[part]-Velocity[part]*blur_time*1000;
  2309. }
  2310. }
  2311. for (part = sub2_start; part < End; part++) {
  2312. unsigned int part_age = current_time - TimeStamp[part];
  2313. // Ensure the current color keyframe is correct, and calculate color state
  2314. if (color) {
  2315. // We go from older to younger particles, so we go backwards from the last keyframe until
  2316. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2317. for (; part_age < ColorKeyFrameTimes[ckey]; ckey--);
  2318. color[part] =
  2319. ColorKeyFrameValues[ckey] +
  2320. ColorKeyFrameDeltas[ckey] * (float)(part_age - ColorKeyFrameTimes[ckey]) +
  2321. RandomColorEntries[part & NumRandomColorEntriesMinus1];
  2322. }
  2323. // Ensure the current alpha keyframe is correct, and calculate alpha state
  2324. if (alpha) {
  2325. // We go from older to younger particles, so we go backwards from the last keyframe until
  2326. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2327. for (; part_age < AlphaKeyFrameTimes[akey]; akey--);
  2328. alpha[part] = AlphaKeyFrameValues[akey] +
  2329. AlphaKeyFrameDeltas[akey] * (float)(part_age - AlphaKeyFrameTimes[akey]) +
  2330. RandomAlphaEntries[part & NumRandomAlphaEntriesMinus1];
  2331. }
  2332. // Ensure the current size keyframe is correct, and calculate size state
  2333. if (size) {
  2334. // We go from older to younger particles, so we go backwards from the last keyframe until
  2335. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2336. for (; part_age < SizeKeyFrameTimes[skey]; skey--);
  2337. size[part] = SizeKeyFrameValues[skey] +
  2338. SizeKeyFrameDeltas[skey] * (float)(part_age - SizeKeyFrameTimes[skey]) +
  2339. RandomSizeEntries[part & NumRandomSizeEntriesMinus1];
  2340. // Size (unlike color) isn't clamped in the engine, so we need to
  2341. // clamp negative values to zero here:
  2342. size[part] = (size[part] >= 0.0f) ? size[part] : 0.0f;
  2343. }
  2344. // Ensure the current rotation keyframe is correct, and calculate orientation state
  2345. if (orientation) {
  2346. // We go from older to younger particles, so we go backwards from the last keyframe until
  2347. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2348. for (; part_age < RotationKeyFrameTimes[rkey]; rkey--);
  2349. float f_delta_t = (float)(part_age - RotationKeyFrameTimes[rkey]);
  2350. float tmp_orient = OrientationKeyFrameValues[rkey] +
  2351. (RotationKeyFrameValues[rkey] + HalfRotationKeyFrameDeltas[rkey] * f_delta_t) * f_delta_t +
  2352. RandomRotationEntries[part & NumRandomRotationEntriesMinus1] * (float)part_age +
  2353. RandomOrientationEntries[part & NumRandomOrientationEntriesMinus1];
  2354. orientation[part] = (uint)(((int)(tmp_orient * 256.0f)) & 0xFF);
  2355. }
  2356. // Ensure the current frame keyframe is correct, and calculate frame state
  2357. if (frame) {
  2358. // Frame and ucoord are mutually exclusive
  2359. WWASSERT(ucoord==NULL);
  2360. // We go from older to younger particles, so we go backwards from the last keyframe until
  2361. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2362. for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
  2363. float tmp_frame = FrameKeyFrameValues[fkey] +
  2364. FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
  2365. RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
  2366. frame[part] = (uint)(((int)(tmp_frame)) & 0xFF);
  2367. }
  2368. // Ensure the current frame keyframe is correct, and calculate frame state
  2369. // ucoord is the same as frame but in float
  2370. if (ucoord) {
  2371. // Frame and ucoord are mutually exclusive
  2372. WWASSERT(frame==NULL);
  2373. // We go from older to younger particles, so we go backwards from the last keyframe until
  2374. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2375. for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
  2376. ucoord[part] = FrameKeyFrameValues[fkey] +
  2377. FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
  2378. RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
  2379. }
  2380. if (tailposition) {
  2381. // We go from older to younger particles, so we go backwards from the last keyframe until
  2382. // age >= keytime. This loop must terminate because the 0th keytime is 0.
  2383. float blur_time = BlurTimeKeyFrameValues[0];
  2384. if (BlurTimeKeyFrameTimes) {
  2385. for (; part_age < BlurTimeKeyFrameTimes[bkey]; bkey--);
  2386. blur_time = BlurTimeKeyFrameValues[bkey] +
  2387. BlurTimeKeyFrameDeltas[bkey] * (float)(part_age - BlurTimeKeyFrameTimes[bkey]) +
  2388. RandomBlurTimeEntries[part & NumRandomBlurTimeEntriesMinus1];
  2389. }
  2390. tailposition[part]=position[part]-Velocity[part]*blur_time*1000;
  2391. }
  2392. }
  2393. }
  2394. void ParticleBufferClass::Update_Bounding_Box(void)
  2395. {
  2396. // Ensure all particle positions are updated. If bounding box still not
  2397. // dirty, return.
  2398. Update_Kinematic_Particle_State();
  2399. if (!BoundingBoxDirty) return;
  2400. // If there are no particles, generate a dummy bounding box:
  2401. if (NonNewNum == 0U) {
  2402. BoundingBox.Init(Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0));
  2403. BoundingBoxDirty = false;
  2404. return;
  2405. }
  2406. // Find min/max coord values for all points:
  2407. int pingpong = 0;
  2408. if (PingPongPosition) {
  2409. pingpong = WW3D::Get_Frame_Count() & 0x1;
  2410. }
  2411. Vector3 *position = Position[pingpong]->Get_Array();
  2412. Vector3 max_coords = position[Start];
  2413. Vector3 min_coords = position[Start];
  2414. // In the general case, a range in a circular buffer can be composed of up
  2415. // to two subranges. Find the Start - End subranges.
  2416. unsigned int sub1_end; // End of subrange 1.
  2417. unsigned int sub2_start; // Start of subrange 2.
  2418. unsigned int i; // Loop index.
  2419. if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
  2420. sub1_end = End;
  2421. sub2_start = End;
  2422. } else {
  2423. sub1_end = MaxNum;
  2424. sub2_start = 0;
  2425. }
  2426. for (i = Start; i < sub1_end; i++) {
  2427. max_coords.X = max_coords.X >= position[i].X ? max_coords.X : position[i].X;
  2428. max_coords.Y = max_coords.Y >= position[i].Y ? max_coords.Y : position[i].Y;
  2429. max_coords.Z = max_coords.Z >= position[i].Z ? max_coords.Z : position[i].Z;
  2430. min_coords.X = min_coords.X <= position[i].X ? min_coords.X : position[i].X;
  2431. min_coords.Y = min_coords.Y <= position[i].Y ? min_coords.Y : position[i].Y;
  2432. min_coords.Z = min_coords.Z <= position[i].Z ? min_coords.Z : position[i].Z;
  2433. }
  2434. for (i = sub2_start; i < End; i++) {
  2435. max_coords.X = max_coords.X >= position[i].X ? max_coords.X : position[i].X;
  2436. max_coords.Y = max_coords.Y >= position[i].Y ? max_coords.Y : position[i].Y;
  2437. max_coords.Z = max_coords.Z >= position[i].Z ? max_coords.Z : position[i].Z;
  2438. min_coords.X = min_coords.X <= position[i].X ? min_coords.X : position[i].X;
  2439. min_coords.Y = min_coords.Y <= position[i].Y ? min_coords.Y : position[i].Y;
  2440. min_coords.Z = min_coords.Z <= position[i].Z ? min_coords.Z : position[i].Z;
  2441. }
  2442. // Extend by maximum possible particle size:
  2443. Vector3 size(MaxSize, MaxSize, MaxSize);
  2444. max_coords += size;
  2445. min_coords -= size;
  2446. // Update bounding box:
  2447. BoundingBox.Init(MinMaxAABoxClass(min_coords,max_coords));
  2448. BoundingBoxDirty = false;
  2449. }
  2450. // NOTE: typically, the number of new particles created in a frame is small
  2451. // relative to the total number of particles, so this is not the most
  2452. // performance-critical particle function. New particles are copied from the
  2453. // new particle vector into the circular buffer, overwriting any older
  2454. // particles (including possibly other new particles) so that the newest
  2455. // particles are preserved. The particles are initialized to their state at
  2456. // the end of the current interval.
  2457. void ParticleBufferClass::Get_New_Particles(void)
  2458. {
  2459. unsigned int current_time = WW3D::Get_Sync_Time();
  2460. // position is the current frame position, prev_pos is the previous frames position (only if
  2461. // we have enabled pingpong position buffers)
  2462. Vector3 *position;
  2463. Vector3 *prev_pos;
  2464. if (PingPongPosition) {
  2465. int pingpong = WW3D::Get_Frame_Count() & 0x1;
  2466. position = Position[pingpong]->Get_Array();
  2467. prev_pos = Position[pingpong ^ 0x1]->Get_Array();
  2468. } else {
  2469. position = Position[0]->Get_Array();
  2470. prev_pos = NULL;
  2471. }
  2472. unsigned char * ids = GroupID->Get_Array();
  2473. for (; NewParticleQueueCount;) {
  2474. // Get particle off new particle queue:
  2475. NewParticleStruct &new_particle = NewParticleQueue[NewParticleQueueStart];
  2476. if (++NewParticleQueueStart == MaxNum) NewParticleQueueStart = 0U;
  2477. NewParticleQueueCount--;
  2478. // Get particle birth time stamp, calculate age. If not under maxage
  2479. // skip this particle.
  2480. TimeStamp[NewEnd] = new_particle.TimeStamp;
  2481. unsigned int age = current_time - TimeStamp[NewEnd];
  2482. if (age >= MaxAge) continue;
  2483. float fp_age = (float)age;
  2484. // Apply velocity and acceleration if present. Otherwise, just apply
  2485. // velocity.
  2486. if (HasAccel) {
  2487. position[NewEnd] = new_particle.Position +
  2488. (new_particle.Velocity + 0.5f * Accel * fp_age) * fp_age;
  2489. Velocity[NewEnd] = new_particle.Velocity + (Accel * fp_age);
  2490. } else {
  2491. position[NewEnd] =new_particle.Position +
  2492. (new_particle.Velocity * fp_age);
  2493. Velocity[NewEnd] = new_particle.Velocity;
  2494. }
  2495. // If pingpong enabled, store starting position in prev_pos[].
  2496. if (PingPongPosition) {
  2497. prev_pos[NewEnd] = new_particle.Position;
  2498. }
  2499. // upate the group id
  2500. ids[NewEnd] = new_particle.GroupID;
  2501. // Advance the 'end of new particles' index.
  2502. NewEnd++;
  2503. if (NewEnd == MaxNum) NewEnd = 0;
  2504. // Update the new particles count.
  2505. NewNum++;
  2506. // If we have just overflowed the total buffer, advance Start.
  2507. if ((NewNum + NonNewNum) == (signed)(MaxNum + 1)) {
  2508. Start++;
  2509. if (Start == MaxNum) Start = 0;
  2510. NonNewNum--;
  2511. // If this underflows the 'non-new' buffer, advance End.
  2512. if (NonNewNum == -1) {
  2513. End++;
  2514. if (End == MaxNum) End = 0;
  2515. NonNewNum = 0;
  2516. NewNum--;
  2517. }
  2518. }
  2519. }
  2520. }
  2521. void ParticleBufferClass::Kill_Old_Particles(void)
  2522. {
  2523. // Scan from Start and find the first particle which has an age less than
  2524. // MaxAge - set Start to that position.
  2525. // In the general case, a range in a circular buffer can be composed of up
  2526. // to two subranges. Find the Start - End subranges.
  2527. unsigned int sub1_end; // End of subrange 1.
  2528. unsigned int sub2_start; // Start of subrange 2.
  2529. unsigned int i; // Loop index.
  2530. if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
  2531. sub1_end = End;
  2532. sub2_start = End;
  2533. } else {
  2534. sub1_end = MaxNum;
  2535. sub2_start = 0;
  2536. }
  2537. unsigned int current_time = WW3D::Get_Sync_Time();
  2538. // Stop when the current particle is young enough to be alive.
  2539. bool broke = false;
  2540. for (i = Start; i < sub1_end; i++) {
  2541. if ((current_time - TimeStamp[i]) < MaxAge) {
  2542. broke = true;
  2543. break;
  2544. }
  2545. NonNewNum--;
  2546. }
  2547. if (!broke) {
  2548. for (i = sub2_start; i < End; i++) {
  2549. if ((current_time - TimeStamp[i]) < MaxAge) break;
  2550. NonNewNum--;
  2551. }
  2552. }
  2553. Start = i;
  2554. // NOTE: we do not scan the new particles, because they have been already
  2555. // preculled to be under MaxAge.
  2556. }
  2557. void ParticleBufferClass::Update_Non_New_Particles(unsigned int elapsed)
  2558. {
  2559. // In the general case, a range in a circular buffer can be composed of up
  2560. // to two subranges. Find the Start - End subranges.
  2561. unsigned int sub1_end; // End of subrange 1.
  2562. unsigned int sub2_start; // Start of subrange 2.
  2563. unsigned int i; // Loop index.
  2564. if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
  2565. sub1_end = End;
  2566. sub2_start = End;
  2567. } else {
  2568. sub1_end = MaxNum;
  2569. sub2_start = 0;
  2570. }
  2571. float fp_elapsed_time = (float)elapsed;
  2572. // Update position and velocity for all particles.
  2573. if (PingPongPosition) {
  2574. int pingpong = WW3D::Get_Frame_Count() & 0x1;
  2575. Vector3 *position = Position[pingpong]->Get_Array();
  2576. Vector3 *prev_pos = Position[pingpong ^ 0x1]->Get_Array();
  2577. if (HasAccel) {
  2578. Vector3 delta_v = Accel * fp_elapsed_time;
  2579. Vector3 accel_p = Accel * (0.5f * fp_elapsed_time * fp_elapsed_time);
  2580. for (i = Start; i < sub1_end; i++) {
  2581. position[i] = prev_pos[i] + Velocity[i] * fp_elapsed_time + accel_p;
  2582. Velocity[i] += delta_v;
  2583. }
  2584. for (i = sub2_start; i < End; i++) {
  2585. position[i] = prev_pos[i] + Velocity[i] * fp_elapsed_time + accel_p;
  2586. Velocity[i] += delta_v;
  2587. }
  2588. } else {
  2589. for (i = Start; i < sub1_end; i++) {
  2590. position[i] += Velocity[i] * fp_elapsed_time;
  2591. }
  2592. for (i = sub2_start; i < End; i++) {
  2593. position[i] += Velocity[i] * fp_elapsed_time;
  2594. }
  2595. }
  2596. } else {
  2597. Vector3 *position = Position[0]->Get_Array();
  2598. if (HasAccel) {
  2599. Vector3 delta_v = Accel * fp_elapsed_time;
  2600. Vector3 accel_p = Accel * (0.5f * fp_elapsed_time * fp_elapsed_time);
  2601. for (i = Start; i < sub1_end; i++) {
  2602. position[i] += Velocity[i] * fp_elapsed_time + accel_p;
  2603. Velocity[i] += delta_v;
  2604. }
  2605. for (i = sub2_start; i < End; i++) {
  2606. position[i] += Velocity[i] * fp_elapsed_time + accel_p;
  2607. Velocity[i] += delta_v;
  2608. }
  2609. } else {
  2610. for (i = Start; i < sub1_end; i++) {
  2611. position[i] += Velocity[i] * fp_elapsed_time;
  2612. }
  2613. for (i = sub2_start; i < End; i++) {
  2614. position[i] += Velocity[i] * fp_elapsed_time;
  2615. }
  2616. }
  2617. }
  2618. }
  2619. void ParticleBufferClass::Get_Color_Key_Frames (ParticlePropertyStruct<Vector3> &colors) const
  2620. {
  2621. int real_keyframe_count = (NumColorKeyFrames > 0) ? (NumColorKeyFrames - 1) : 0;
  2622. bool create_last_keyframe = false;
  2623. //
  2624. // Determine if there is a keyframe at the very end of the particle's lifetime
  2625. //
  2626. if ((ColorKeyFrameDeltas != NULL) &&
  2627. ((ColorKeyFrameDeltas[NumColorKeyFrames - 1].X != 0) ||
  2628. (ColorKeyFrameDeltas[NumColorKeyFrames - 1].Y != 0) ||
  2629. (ColorKeyFrameDeltas[NumColorKeyFrames - 1].Z != 0))) {
  2630. real_keyframe_count ++;
  2631. create_last_keyframe = true;
  2632. }
  2633. colors.Start = ColorKeyFrameValues[0];
  2634. colors.Rand = ColorRandom;
  2635. colors.NumKeyFrames = real_keyframe_count;
  2636. colors.KeyTimes = NULL;
  2637. colors.Values = NULL;
  2638. //
  2639. // If we have more than just the start color, build
  2640. // an array of key times and color vatues
  2641. //
  2642. if (real_keyframe_count > 0) {
  2643. colors.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
  2644. colors.Values = W3DNEWARRAY Vector3[real_keyframe_count];
  2645. //
  2646. // Copy the keytimes and color values
  2647. //
  2648. unsigned int index;
  2649. for (index = 1; index < NumColorKeyFrames; index ++) {
  2650. colors.KeyTimes[index - 1] = ((float)ColorKeyFrameTimes[index]) / 1000;
  2651. colors.Values[index - 1] = ColorKeyFrameValues[index];
  2652. }
  2653. //
  2654. // Add a keyframe at the very end of the timeline if necessary
  2655. //
  2656. if (create_last_keyframe) {
  2657. colors.KeyTimes[index - 1] = ((float)MaxAge / 1000);
  2658. //
  2659. // Determine what the value of the last keyframe should be
  2660. //
  2661. Vector3 start_color = ColorKeyFrameValues[index - 1];
  2662. Vector3 &delta = ColorKeyFrameDeltas[NumColorKeyFrames - 1];
  2663. float time_delta = MaxAge - ColorKeyFrameTimes[index - 1];
  2664. colors.Values[index - 1] = start_color + (delta * time_delta);
  2665. }
  2666. }
  2667. return ;
  2668. }
  2669. void ParticleBufferClass::Get_Opacity_Key_Frames (ParticlePropertyStruct<float> &opacities) const
  2670. {
  2671. int real_keyframe_count = (NumAlphaKeyFrames > 0) ? (NumAlphaKeyFrames - 1) : 0;
  2672. bool create_last_keyframe = false;
  2673. //
  2674. // Determine if there is a keyframe at the very end of the particle's lifetime
  2675. //
  2676. if ((AlphaKeyFrameDeltas != NULL) &&
  2677. (AlphaKeyFrameDeltas[NumAlphaKeyFrames - 1] != 0)) {
  2678. real_keyframe_count ++;
  2679. create_last_keyframe = true;
  2680. }
  2681. opacities.Start = AlphaKeyFrameValues[0];
  2682. opacities.Rand = OpacityRandom;
  2683. opacities.NumKeyFrames = real_keyframe_count;
  2684. opacities.KeyTimes = NULL;
  2685. opacities.Values = NULL;
  2686. //
  2687. // If we have more than just the start opacity, build
  2688. // an array of key times and opacity values
  2689. //
  2690. if (real_keyframe_count > 0) {
  2691. opacities.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
  2692. opacities.Values = W3DNEWARRAY float[real_keyframe_count];
  2693. //
  2694. // Copy the keytimes and opacity values
  2695. //
  2696. unsigned int index;
  2697. for (index = 1; index < NumAlphaKeyFrames; index ++) {
  2698. opacities.KeyTimes[index - 1] = ((float)AlphaKeyFrameTimes[index]) / 1000;
  2699. opacities.Values[index - 1] = AlphaKeyFrameValues[index];
  2700. }
  2701. //
  2702. // Add a keyframe at the very end of the timeline if necessary
  2703. //
  2704. if (create_last_keyframe) {
  2705. opacities.KeyTimes[index - 1] = ((float)MaxAge / 1000);
  2706. //
  2707. // Determine what the value of the last keyframe should be
  2708. //
  2709. float start_alpha = AlphaKeyFrameValues[index - 1];
  2710. float &delta = AlphaKeyFrameDeltas[NumAlphaKeyFrames - 1];
  2711. float time_delta = MaxAge - AlphaKeyFrameTimes[index - 1];
  2712. opacities.Values[index - 1] = start_alpha + (delta * time_delta);
  2713. }
  2714. }
  2715. return ;
  2716. }
  2717. void ParticleBufferClass::Get_Size_Key_Frames (ParticlePropertyStruct<float> &sizes) const
  2718. {
  2719. int real_keyframe_count = (NumSizeKeyFrames > 0) ? (NumSizeKeyFrames - 1) : 0;
  2720. bool create_last_keyframe = false;
  2721. //
  2722. // Determine if there is a keyframe at the very end of the particle's lifetime
  2723. //
  2724. if ((SizeKeyFrameDeltas != NULL) &&
  2725. (SizeKeyFrameDeltas[NumSizeKeyFrames - 1] != 0)) {
  2726. real_keyframe_count ++;
  2727. create_last_keyframe = true;
  2728. }
  2729. sizes.Start = SizeKeyFrameValues[0];
  2730. sizes.Rand = SizeRandom;
  2731. sizes.NumKeyFrames = real_keyframe_count;
  2732. sizes.KeyTimes = NULL;
  2733. sizes.Values = NULL;
  2734. //
  2735. // If we have more than just the start opacity, build
  2736. // an array of key times and opacity values
  2737. //
  2738. if (real_keyframe_count > 0) {
  2739. sizes.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
  2740. sizes.Values = W3DNEWARRAY float[real_keyframe_count];
  2741. //
  2742. // Copy the keytimes and size values
  2743. //
  2744. unsigned int index;
  2745. for (index = 1; index < NumSizeKeyFrames; index ++) {
  2746. sizes.KeyTimes[index - 1] = ((float)SizeKeyFrameTimes[index]) / 1000;
  2747. sizes.Values[index - 1] = SizeKeyFrameValues[index];
  2748. }
  2749. //
  2750. // Add a keyframe at the very end of the timeline if necessary
  2751. //
  2752. if (create_last_keyframe) {
  2753. sizes.KeyTimes[index - 1] = ((float)MaxAge / 1000);
  2754. //
  2755. // Determine what the value of the last keyframe should be
  2756. //
  2757. float start_size = SizeKeyFrameValues[index - 1];
  2758. float &delta = SizeKeyFrameDeltas[NumSizeKeyFrames - 1];
  2759. float time_delta = MaxAge - SizeKeyFrameTimes[index - 1];
  2760. sizes.Values[index - 1] = start_size + (delta * time_delta);
  2761. }
  2762. }
  2763. return ;
  2764. }
  2765. void ParticleBufferClass::Get_Rotation_Key_Frames (ParticlePropertyStruct<float> &rotations) const
  2766. {
  2767. int real_keyframe_count = (NumRotationKeyFrames > 0) ? (NumRotationKeyFrames - 1) : 0;
  2768. bool create_last_keyframe = false;
  2769. /*
  2770. ** NOTE: Rotations are stored internally in rotations per millisecond. These will be converted to rotations per second.
  2771. */
  2772. //
  2773. // Determine if there is a keyframe at the very end of the particle's lifetime
  2774. //
  2775. if ((HalfRotationKeyFrameDeltas != NULL) &&
  2776. (HalfRotationKeyFrameDeltas[NumRotationKeyFrames - 1] != 0)) {
  2777. real_keyframe_count ++;
  2778. create_last_keyframe = true;
  2779. }
  2780. // Convert the rotation values from rotations per millisecond to rotations per second.
  2781. rotations.Start = RotationKeyFrameValues ? RotationKeyFrameValues[0] * 1000.0f : 0;
  2782. rotations.Rand = RotationRandom * 1000.0f;
  2783. rotations.NumKeyFrames = real_keyframe_count;
  2784. rotations.KeyTimes = NULL;
  2785. rotations.Values = NULL;
  2786. //
  2787. // If we have more than just the start rotation, build
  2788. // an array of key times and rotation values
  2789. //
  2790. if (real_keyframe_count > 0) {
  2791. rotations.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
  2792. rotations.Values = W3DNEWARRAY float[real_keyframe_count];
  2793. //
  2794. // Copy the keytimes and rotation values
  2795. //
  2796. unsigned int index;
  2797. for (index = 1; index < NumRotationKeyFrames; index ++) {
  2798. rotations.KeyTimes[index - 1] = ((float)RotationKeyFrameTimes[index]) / 1000;
  2799. rotations.Values[index - 1] = RotationKeyFrameValues[index] * 1000.0f;
  2800. }
  2801. //
  2802. // Add a keyframe at the very end of the timeline if necessary
  2803. //
  2804. if (create_last_keyframe) {
  2805. rotations.KeyTimes[index - 1] = ((float)MaxAge / 1000);
  2806. //
  2807. // Determine what the value of the last keyframe should be
  2808. //
  2809. float start_rotation = RotationKeyFrameValues[index - 1];
  2810. float delta = 2.0f * HalfRotationKeyFrameDeltas[NumRotationKeyFrames - 1];
  2811. float time_delta = MaxAge - RotationKeyFrameTimes[index - 1];
  2812. rotations.Values[index - 1] = (start_rotation + (delta * time_delta)) * 1000.0f;
  2813. }
  2814. }
  2815. return ;
  2816. }
  2817. void ParticleBufferClass::Get_Frame_Key_Frames (ParticlePropertyStruct<float> &frames) const
  2818. {
  2819. int real_keyframe_count = (NumFrameKeyFrames > 0) ? (NumFrameKeyFrames - 1) : 0;
  2820. bool create_last_keyframe = false;
  2821. //
  2822. // Determine if there is a keyframe at the very end of the particle's lifetime
  2823. //
  2824. if ((FrameKeyFrameDeltas != NULL) &&
  2825. (FrameKeyFrameDeltas[NumFrameKeyFrames - 1] != 0)) {
  2826. real_keyframe_count ++;
  2827. create_last_keyframe = true;
  2828. }
  2829. frames.Start = FrameKeyFrameValues[0];
  2830. frames.Rand = FrameRandom;
  2831. frames.NumKeyFrames = real_keyframe_count;
  2832. frames.KeyTimes = NULL;
  2833. frames.Values = NULL;
  2834. //
  2835. // If we have more than just the start rotation, build
  2836. // an array of key times and frame values
  2837. //
  2838. if (real_keyframe_count > 0) {
  2839. frames.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
  2840. frames.Values = W3DNEWARRAY float[real_keyframe_count];
  2841. //
  2842. // Copy the keytimes and frame values
  2843. //
  2844. unsigned int index;
  2845. for (index = 1; index < NumFrameKeyFrames; index ++) {
  2846. frames.KeyTimes[index - 1] = ((float)FrameKeyFrameTimes[index]) / 1000;
  2847. frames.Values[index - 1] = FrameKeyFrameValues[index];
  2848. }
  2849. //
  2850. // Add a keyframe at the very end of the timeline if necessary
  2851. //
  2852. if (create_last_keyframe) {
  2853. frames.KeyTimes[index - 1] = ((float)MaxAge / 1000);
  2854. //
  2855. // Determine what the value of the last keyframe should be
  2856. //
  2857. float start_frame = FrameKeyFrameValues[index - 1];
  2858. float &delta = FrameKeyFrameDeltas[NumFrameKeyFrames - 1];
  2859. float time_delta = MaxAge - FrameKeyFrameTimes[index - 1];
  2860. frames.Values[index - 1] = start_frame + (delta * time_delta);
  2861. }
  2862. }
  2863. return ;
  2864. }
  2865. void ParticleBufferClass::Get_Blur_Time_Key_Frames (ParticlePropertyStruct<float> &blurtimes) const
  2866. {
  2867. int real_keyframe_count = (NumBlurTimeKeyFrames > 0) ? (NumBlurTimeKeyFrames - 1) : 0;
  2868. bool create_last_keyframe = false;
  2869. //
  2870. // Determine if there is a keyframe at the very end of the particle's lifetime
  2871. //
  2872. if ((BlurTimeKeyFrameDeltas != NULL) &&
  2873. (BlurTimeKeyFrameDeltas[NumBlurTimeKeyFrames - 1] != 0)) {
  2874. real_keyframe_count ++;
  2875. create_last_keyframe = true;
  2876. }
  2877. blurtimes.Start = BlurTimeKeyFrameValues[0];
  2878. blurtimes.Rand = BlurTimeRandom;
  2879. blurtimes.NumKeyFrames = real_keyframe_count;
  2880. blurtimes.KeyTimes = NULL;
  2881. blurtimes.Values = NULL;
  2882. //
  2883. // If we have more than just the start rotation, build
  2884. // an array of key times and blur time values
  2885. //
  2886. if (real_keyframe_count > 0) {
  2887. blurtimes.KeyTimes = new float[real_keyframe_count];
  2888. blurtimes.Values = new float[real_keyframe_count];
  2889. //
  2890. // Copy the keytimes and frame values
  2891. //
  2892. unsigned int index;
  2893. for (index = 1; index < NumBlurTimeKeyFrames; index ++) {
  2894. blurtimes.KeyTimes[index - 1] = ((float)BlurTimeKeyFrameTimes[index]) / 1000;
  2895. blurtimes.Values[index - 1] = BlurTimeKeyFrameValues[index];
  2896. }
  2897. //
  2898. // Add a keyframe at the very end of the timeline if necessary
  2899. //
  2900. if (create_last_keyframe) {
  2901. blurtimes.KeyTimes[index - 1] = ((float)MaxAge / 1000);
  2902. //
  2903. // Determine what the value of the last keyframe should be
  2904. //
  2905. float start_blurtime = BlurTimeKeyFrameValues[index - 1];
  2906. float &delta = BlurTimeKeyFrameDeltas[NumBlurTimeKeyFrames - 1];
  2907. float time_delta = MaxAge - BlurTimeKeyFrameTimes[index - 1];
  2908. blurtimes.Values[index - 1] = start_blurtime + (delta * time_delta);
  2909. }
  2910. }
  2911. return ;
  2912. }
  2913. void ParticleBufferClass::Set_LOD_Max_Screen_Size(int lod_level,float max_screen_size)
  2914. {
  2915. if ((lod_level <0) || (lod_level > 17)) {
  2916. return;
  2917. }
  2918. LODMaxScreenSizes[lod_level] = max_screen_size;
  2919. }
  2920. float ParticleBufferClass::Get_LOD_Max_Screen_Size(int lod_level)
  2921. {
  2922. if ((lod_level <0) || (lod_level > 17)) {
  2923. return NO_MAX_SCREEN_SIZE;
  2924. }
  2925. return LODMaxScreenSizes[lod_level];
  2926. }
  2927. int ParticleBufferClass::Get_Line_Texture_Mapping_Mode(void) const
  2928. {
  2929. if (LineRenderer != NULL) {
  2930. return LineRenderer->Get_Texture_Mapping_Mode();
  2931. }
  2932. return SegLineRendererClass::UNIFORM_WIDTH_TEXTURE_MAP;
  2933. }
  2934. int ParticleBufferClass::Is_Merge_Intersections(void) const
  2935. {
  2936. if (LineRenderer != NULL) {
  2937. return LineRenderer->Is_Merge_Intersections();
  2938. }
  2939. return false;
  2940. }
  2941. int ParticleBufferClass::Is_Freeze_Random(void) const
  2942. {
  2943. if (LineRenderer != NULL) {
  2944. return LineRenderer->Is_Freeze_Random();
  2945. }
  2946. return false;
  2947. }
  2948. int ParticleBufferClass::Is_Sorting_Disabled(void) const
  2949. {
  2950. if (LineRenderer != NULL) {
  2951. return LineRenderer->Is_Sorting_Disabled();
  2952. }
  2953. return false;
  2954. }
  2955. int ParticleBufferClass::Are_End_Caps_Enabled(void) const
  2956. {
  2957. if (LineRenderer != NULL) {
  2958. return LineRenderer->Are_End_Caps_Enabled();
  2959. }
  2960. return false;
  2961. }
  2962. int ParticleBufferClass::Get_Subdivision_Level(void) const
  2963. {
  2964. if (LineRenderer != NULL) {
  2965. return LineRenderer->Get_Current_Subdivision_Level();
  2966. }
  2967. return 0;
  2968. }
  2969. float ParticleBufferClass::Get_Noise_Amplitude(void) const
  2970. {
  2971. if (LineRenderer != NULL) {
  2972. return LineRenderer->Get_Noise_Amplitude();
  2973. }
  2974. return 0.0f;
  2975. }
  2976. float ParticleBufferClass::Get_Merge_Abort_Factor(void) const
  2977. {
  2978. if (LineRenderer != NULL) {
  2979. return LineRenderer->Get_Merge_Abort_Factor();
  2980. }
  2981. return 0.0f;
  2982. }
  2983. float ParticleBufferClass::Get_Texture_Tile_Factor(void) const
  2984. {
  2985. if (LineRenderer != NULL) {
  2986. return LineRenderer->Get_Texture_Tile_Factor();
  2987. }
  2988. return 1.0f;
  2989. }
  2990. Vector2 ParticleBufferClass::Get_UV_Offset_Rate(void) const
  2991. {
  2992. if (LineRenderer != NULL) {
  2993. return LineRenderer->Get_UV_Offset_Rate();
  2994. }
  2995. return Vector2(0.0f,0.0f);
  2996. }
  2997. ParticleBufferClass::TailDiffuseTypeEnum ParticleBufferClass::Determine_Tail_Diffuse()
  2998. {
  2999. // if there is a texture, the assumption is that the artist
  3000. // is controlling the fadeoff ramp using the texture
  3001. // thus, the ARGB of the tail should be the same as the head
  3002. TextureClass *tex=Get_Texture();
  3003. if (tex)
  3004. {
  3005. REF_PTR_RELEASE(tex);
  3006. return SAME_AS_HEAD;
  3007. }
  3008. ShaderClass shader=Get_Shader();
  3009. //Multiplicative RGB is white (A is don't care)
  3010. //Additive RGB is Black (A is don't care)
  3011. //Screen RGB is Black (A is don't care)
  3012. //Alpha Same RGB as head but A is 0
  3013. //Alpha test blend Same ARGB as head but A is 0
  3014. //Alpha test Same ARGB as head
  3015. //Opaque Same ARGB as head
  3016. // Multiplicative
  3017. if (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_SRC_COLOR) return WHITE;
  3018. // Additive
  3019. else if ((shader.Get_Src_Blend_Func()==ShaderClass::SRCBLEND_ONE) && (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_ONE)) return BLACK;
  3020. // Screen
  3021. else if ((shader.Get_Src_Blend_Func()==ShaderClass::SRCBLEND_ONE) && (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_ONE_MINUS_SRC_COLOR)) return BLACK;
  3022. // Alpha
  3023. else if ((shader.Get_Src_Blend_Func()==ShaderClass::SRCBLEND_SRC_ALPHA) && (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA)) return SAME_AS_HEAD_ALPHA_ZERO;
  3024. // Alpha test
  3025. else if (shader.Get_Alpha_Test()==ShaderClass::ALPHATEST_ENABLE) return SAME_AS_HEAD_ALPHA_ZERO;
  3026. return SAME_AS_HEAD;
  3027. }
  3028. TextureClass * ParticleBufferClass::Get_Texture (void) const
  3029. {
  3030. if (PointGroup) return PointGroup->Get_Texture();
  3031. else if (LineGroup) return LineGroup->Get_Texture();
  3032. else if (LineRenderer) return LineRenderer->Get_Texture();
  3033. return NULL;
  3034. }
  3035. void ParticleBufferClass::Set_Texture (TextureClass *tex)
  3036. {
  3037. if (PointGroup) PointGroup->Set_Texture(tex);
  3038. else if (LineGroup) LineGroup->Set_Texture(tex);
  3039. else if (LineRenderer) LineRenderer->Set_Texture(tex);
  3040. }
  3041. ShaderClass ParticleBufferClass::Get_Shader (void) const
  3042. {
  3043. if (PointGroup) return PointGroup->Get_Shader();
  3044. else if (LineGroup) return LineGroup->Get_Shader();
  3045. else if (LineRenderer) return LineRenderer->Get_Shader();
  3046. WWASSERT(0);
  3047. return ShaderClass::_PresetOpaqueShader;
  3048. }