| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***************************************************************************
- *** 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 ***
- ***************************************************************************
- * *
- * Project Name : G *
- * *
- * $Archive:: /Commando/Code/ww3d2/part_buf.cpp $*
- * *
- * $Author:: Jani_p $*
- * *
- * $Modtime:: 9/07/01 12:57p $*
- * *
- * $Revision:: 20 $*
- * *
- *-------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "part_buf.h"
- #include "part_emt.h"
- #include "ww3d.h"
- #include "rinfo.h"
- #include "scene.h"
- #include "camera.h"
- #include "predlod.h"
- #include "pot.h"
- #include "bound.h"
- #include "simplevec.h"
- #include "sphere.h"
- #include "wwprofile.h"
- #include <limits.h>
- #include "vp.h"
- #include "texture.h"
- #include "dx8wrapper.h"
- #include "vector3.h"
- // A random permutation of the numbers 0 to 15 - used for LOD particle decimation.
- // It was generated by the amazingly high-tech method of pulling numbers out of a hat.
- const unsigned int ParticleBufferClass::PermutationArray[16] = {
- 11, 3, 7, 14, 0, 13, 1, 2, 5, 12, 15, 6, 9, 8, 4, 10
- };
- // Maximum size of randomizer tables
- const static unsigned int MAX_RANDOM_ENTRIES = 32; // MUST be power of two!
- // Total Active Particle Buffer Count
- unsigned int ParticleBufferClass::TotalActiveCount = 0;
- // Static array of screen-size clamps for the 17 possible LOD levels a particle buffer can have.
- // We can change these from being global to being per-buffer later if we wish. Default is
- // NO_MAX_SCREEN_SIZE.
- float ParticleBufferClass::LODMaxScreenSizes[17] = {
- NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
- NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
- NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
- NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE, NO_MAX_SCREEN_SIZE,
- NO_MAX_SCREEN_SIZE
- };
- static Random4Class rand_gen;
- const float oo_intmax = 1.0f / (float)INT_MAX;
- // Default Line Emitter Properties
- static const W3dEmitterLinePropertiesStruct _DefaultLineEmitterProps=
- { 0,0,0.0f,1.5f,1.0f,0.0f,0.0f,0,0,0,0,0,0,0,0,0 };
- ParticleBufferClass::ParticleBufferClass
- (
- ParticleEmitterClass *emitter,
- unsigned int buffer_size,
- ParticlePropertyStruct<Vector3> &color,
- ParticlePropertyStruct<float> &opacity,
- ParticlePropertyStruct<float> &size,
- ParticlePropertyStruct<float> &rotation,
- float orient_rnd,
- ParticlePropertyStruct<float> &frame,
- ParticlePropertyStruct<float> &blurtime,
- Vector3 accel,
- float max_age,
- float future_start,
- TextureClass *tex,
- ShaderClass shader,
- bool pingpong,
- int render_mode,
- int frame_mode,
- const W3dEmitterLinePropertiesStruct * line_props
- ) :
- NewParticleQueue(NULL),
- NewParticleQueueStart(0U),
- NewParticleQueueEnd(0U),
- NewParticleQueueCount(0U),
- RenderMode(render_mode),
- FrameMode(frame_mode),
- MaxAge(1000.0f * max_age),
- FutureStartTime(1000.0f * future_start),
- LastUpdateTime(WW3D::Get_Sync_Time()),
- IsEmitterDead(false),
- MaxSize(0.0f),
- MaxNum(buffer_size),
- Start(0U),
- End(0U),
- NewEnd(0U),
- NonNewNum(0),
- NewNum(0),
- BoundingBox(Vector3(0,0,0),Vector3(0,0,0)),
- BoundingBoxDirty(true),
- NumColorKeyFrames(0),
- ColorKeyFrameTimes(NULL),
- ColorKeyFrameValues(NULL),
- ColorKeyFrameDeltas(NULL),
- NumAlphaKeyFrames(0),
- AlphaKeyFrameTimes(NULL),
- AlphaKeyFrameValues(NULL),
- AlphaKeyFrameDeltas(NULL),
- NumSizeKeyFrames(0),
- SizeKeyFrameTimes(NULL),
- SizeKeyFrameValues(NULL),
- SizeKeyFrameDeltas(NULL),
- NumRotationKeyFrames(0),
- RotationKeyFrameTimes(NULL),
- RotationKeyFrameValues(NULL),
- HalfRotationKeyFrameDeltas(NULL),
- OrientationKeyFrameValues(NULL),
- NumFrameKeyFrames(0),
- FrameKeyFrameTimes(NULL),
- FrameKeyFrameValues(NULL),
- FrameKeyFrameDeltas(NULL),
- NumBlurTimeKeyFrames(0),
- BlurTimeKeyFrameTimes(NULL),
- BlurTimeKeyFrameValues(NULL),
- BlurTimeKeyFrameDeltas(NULL),
- NumRandomColorEntriesMinus1(0),
- RandomColorEntries(NULL),
- NumRandomAlphaEntriesMinus1(0),
- RandomAlphaEntries(NULL),
- NumRandomSizeEntriesMinus1(0),
- RandomSizeEntries(NULL),
- ColorRandom(0, 0, 0),
- OpacityRandom(0),
- SizeRandom(0),
- RotationRandom(0),
- FrameRandom(0),
- InitialOrientationRandom(0),
- NumRandomRotationEntriesMinus1(0),
- RandomRotationEntries(NULL),
- NumRandomOrientationEntriesMinus1(0),
- RandomOrientationEntries(NULL),
- NumRandomFrameEntriesMinus1(0),
- RandomFrameEntries(NULL),
- NumRandomBlurTimeEntriesMinus1(0),
- RandomBlurTimeEntries(NULL),
- PointGroup(NULL),
- LineRenderer(NULL),
- LineGroup(NULL),
- Diffuse(NULL),
- TailDiffuse(NULL),
- Color(NULL),
- Alpha(NULL),
- Size(NULL),
- Orientation(NULL),
- Frame(NULL),
- UCoord(NULL),
- TailPosition(NULL),
- APT(NULL),
- GroupID(NULL),
- PingPongPosition(pingpong),
- Velocity(NULL),
- TimeStamp(NULL),
- Emitter(emitter),
- DecimationThreshold(0U),
- ProjectedArea(0.0f),
- DefaultTailDiffuse(0,0,0,0),
- CurrentGroupID(0)
- {
- LodCount = 17;
- LodBias = 1.0f;
- Position[0] = NULL;
- Position[1] = NULL;
- // Create color array, keyframes and randomizer table (if needed)
- Reset_Colors(color);
- // Create alpha array, keyframes and randomizer table (if needed)
- Reset_Opacity(opacity);
- // Create size array, keyframes and randomizer table (if needed)
- Reset_Size(size);
- // Create the rotation array, keyframes, and randomizer table (if needed)
- Reset_Rotations(rotation, orient_rnd);
- // Create the frame array, keyframes, and randomizer table (if needed)
- Reset_Frames(frame);
- // Create the blur time array, keyframes, and randomizer table if needed
- Reset_Blur_Times(blurtime);
- // We do not add a ref for the emitter (see DTor for detailed explanation)
- // if (Emitter) Emitter->Add_Ref();
- // Set up new particle queue:
- NewParticleQueue = W3DNEWARRAY NewParticleStruct[MaxNum];
- // These inputs don't need to be range-checked (emitter did that).
- Accel = accel;
- HasAccel = (accel.X != 0.0f) || (accel.Y != 0.0f) || (accel.Z != 0.0f);
-
- shader.Enable_Fog ("ParticleBufferClass");
- switch (RenderMode)
- {
- case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
- {
- // Set up worldspace point group
- PointGroup = W3DNEW PointGroupClass();
- PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
- PointGroup->Set_Texture(tex);
- PointGroup->Set_Shader(shader);
- PointGroup->Set_Frame_Row_Column_Count_Log2(frame_mode);
- PointGroup->Set_Point_Mode(PointGroupClass::TRIS);
- }
- break;
- case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
- {
- // Set up worldspace point group
- PointGroup = W3DNEW PointGroupClass();
- PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
- PointGroup->Set_Texture(tex);
- PointGroup->Set_Shader(shader);
- PointGroup->Set_Frame_Row_Column_Count_Log2(frame_mode);
- PointGroup->Set_Point_Mode(PointGroupClass::QUADS);
- }
- break;
- case W3D_EMITTER_RENDER_MODE_LINE:
- {
- LineRenderer = W3DNEW SegLineRendererClass;
- LineRenderer->Init(*line_props);
- LineRenderer->Set_Texture(tex);
- LineRenderer->Set_Shader(shader);
- LineRenderer->Set_Width(Get_Particle_Size());
- if (line_props != NULL) {
- LineRenderer->Init(*line_props);
- } else {
- // This code should not be run, but if it does,
- // set line emitters to some reasonable value so
- // it doesn't crash
- WWASSERT(0);
- LineRenderer->Init(_DefaultLineEmitterProps);
- }
- }
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
- {
- LineGroup=W3DNEW LineGroupClass();
- LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
- LineGroup->Set_Texture(tex);
- LineGroup->Set_Shader(shader);
- LineGroup->Set_Line_Mode(LineGroupClass::TETRAHEDRON);
- TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition" ) );
- // TODO: Change TailPosition to Kinematic state and add
- // tail positions to bounding box
- Set_Force_Visible(1);
- }
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
- {
- LineGroup=W3DNEW LineGroupClass();
- LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
- LineGroup->Set_Texture(tex);
- LineGroup->Set_Shader(shader);
- LineGroup->Set_Line_Mode(LineGroupClass::PRISM);
- TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition" ) );
- // TODO: Change TailPosition to Kinematic state and add
- // tail positions to bounding box
- Set_Force_Visible(1);
- }
- break;
- default:
- WWASSERT(0);
- break;
- }
- // Set up circular buffer. Contents are not initialized because the
- // start/end indices currently indicate the buffer is empty.
- Position[0] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
- if (PingPongPosition) {
- Position[1] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
- }
- APT = NEW_REF( ShareBufferClass<unsigned int> , (MaxNum, "ParticleBufferClass::APT") );
- GroupID = NEW_REF( ShareBufferClass<unsigned char> , (MaxNum, "ParticleBufferClass::GroupID") );
- Velocity = W3DNEWARRAY Vector3[MaxNum];
- TimeStamp = W3DNEWARRAY unsigned int[MaxNum];
- // So that the object is ready for use after construction, we will
- // complete its initialization by initializing its cost and value arrays
- // according to a screen area of 1.
- int minlod = Calculate_Cost_Value_Arrays(1.0f, Value, Cost);
- // Ensure lod is no less than minimum allowed
- if (Get_LOD_Level() < minlod) Set_LOD_Level(minlod);
- // Update Global Count
- TotalActiveCount++;
- //lorenzen
- // If the render mode is W3D_EMITTER_RENDER_MODE_LINE and we are supplied with
- // a line properties structure, set up a line renderer
- if (RenderMode == W3D_EMITTER_RENDER_MODE_LINE) {
- if (line_props != NULL) {
- LineRenderer = W3DNEW SegLineRendererClass;
- LineRenderer->Init(*line_props);
- LineRenderer->Set_Texture(tex);
- LineRenderer->Set_Shader(shader);
- LineRenderer->Set_Width(Get_Particle_Size());
- } else {
- // We were in line mode but didn't get any line properties, drop back to triangles
- RenderMode = W3D_EMITTER_RENDER_MODE_TRI_PARTICLES;
- }
- }
- }
- ParticleBufferClass::ParticleBufferClass(const ParticleBufferClass & src) :
- RenderObjClass(src),
- NewParticleQueue(NULL),
- NewParticleQueueStart(0U),
- NewParticleQueueEnd(0U),
- NewParticleQueueCount(0U),
- RenderMode(src.RenderMode),
- FrameMode(src.FrameMode),
- MaxAge(src.MaxAge),
- FutureStartTime(src.FutureStartTime),
- LastUpdateTime(WW3D::Get_Sync_Time()),
- IsEmitterDead(false),
- MaxSize(src.MaxSize),
- MaxNum(src.MaxNum),
- Start(0U),
- End(0U),
- NewEnd(0U),
- NonNewNum(0),
- NewNum(0),
- BoundingBox(Vector3(0,0,0),Vector3(0,0,0)),
- BoundingBoxDirty(true),
- NumColorKeyFrames(src.NumColorKeyFrames),
- ColorKeyFrameTimes(NULL),
- ColorKeyFrameValues(NULL),
- ColorKeyFrameDeltas(NULL),
- NumAlphaKeyFrames(src.NumAlphaKeyFrames),
- AlphaKeyFrameTimes(NULL),
- AlphaKeyFrameValues(NULL),
- AlphaKeyFrameDeltas(NULL),
- NumSizeKeyFrames(src.NumSizeKeyFrames),
- SizeKeyFrameTimes(NULL),
- SizeKeyFrameValues(NULL),
- SizeKeyFrameDeltas(NULL),
- NumRotationKeyFrames(src.NumRotationKeyFrames),
- RotationKeyFrameTimes(NULL),
- RotationKeyFrameValues(NULL),
- HalfRotationKeyFrameDeltas(NULL),
- OrientationKeyFrameValues(NULL),
- NumFrameKeyFrames(src.NumFrameKeyFrames),
- FrameKeyFrameTimes(NULL),
- FrameKeyFrameValues(NULL),
- FrameKeyFrameDeltas(NULL),
- NumBlurTimeKeyFrames(src.NumBlurTimeKeyFrames),
- BlurTimeKeyFrameTimes(NULL),
- BlurTimeKeyFrameValues(NULL),
- BlurTimeKeyFrameDeltas(NULL),
- RandomColorEntries(NULL),
- RandomAlphaEntries(NULL),
- RandomSizeEntries(NULL),
- ColorRandom(src.ColorRandom),
- OpacityRandom(src.OpacityRandom),
- SizeRandom(src.SizeRandom),
- RotationRandom(src.RotationRandom),
- FrameRandom(src.FrameRandom),
- InitialOrientationRandom(src.InitialOrientationRandom),
- NumRandomRotationEntriesMinus1(0),
- RandomRotationEntries(NULL),
- NumRandomOrientationEntriesMinus1(0),
- RandomOrientationEntries(NULL),
- NumRandomFrameEntriesMinus1(0),
- RandomFrameEntries(NULL),
- NumRandomBlurTimeEntriesMinus1(0),
- RandomBlurTimeEntries(NULL),
- PointGroup(NULL),
- LineRenderer(NULL),
- LineGroup(NULL),
- Diffuse(NULL),
- TailDiffuse(NULL),
- Color(NULL),
- Alpha(NULL),
- Size(NULL),
- Orientation(NULL),
- Frame(NULL),
- UCoord(NULL),
- TailPosition(NULL),
- APT(NULL),
- GroupID(NULL),
- PingPongPosition(src.PingPongPosition),
- Velocity(NULL),
- TimeStamp(NULL),
- Emitter(src.Emitter),
- DecimationThreshold(src.DecimationThreshold),
- ProjectedArea(0.0f),
- DefaultTailDiffuse(src.DefaultTailDiffuse)
- {
- Position[0] = NULL;
- Position[1] = NULL;
- unsigned int i;
- LodCount = MIN(MaxNum, 17);
- LodBias = src.LodBias;
- /*
- ** Create visual state arrays, copy keyframes and randomizer tables.
- */
- NumRandomColorEntriesMinus1 = src.NumRandomColorEntriesMinus1;
- if (src.Color) {
- // Create color array
- Color = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Color") );
- // Copy color keyframes
- ColorKeyFrameTimes = W3DNEWARRAY unsigned int [NumColorKeyFrames];
- ColorKeyFrameValues = W3DNEWARRAY Vector3 [NumColorKeyFrames];
- ColorKeyFrameDeltas = W3DNEWARRAY Vector3 [NumColorKeyFrames];
- for (i = 0; i < NumColorKeyFrames; i++) {
- ColorKeyFrameTimes[i] = src.ColorKeyFrameTimes[i];
- ColorKeyFrameValues[i] = src.ColorKeyFrameValues[i];
- ColorKeyFrameDeltas[i] = src.ColorKeyFrameDeltas[i];
- }
- // Copy color randomizer table
- if (src.RandomColorEntries) {
- RandomColorEntries = W3DNEWARRAY Vector3 [NumRandomColorEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomColorEntriesMinus1; j++) {
- RandomColorEntries[j] = src.RandomColorEntries[j];
- }
- }
- } else {
- ColorKeyFrameValues = W3DNEWARRAY Vector3 [1];
- ColorKeyFrameValues[0] = src.ColorKeyFrameValues[0];
- }
- NumRandomAlphaEntriesMinus1 = src.NumRandomAlphaEntriesMinus1;
- if (src.Alpha) {
- // Create alpha array
- Alpha = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Alpha") );
- // Copy alpha keyframes
- AlphaKeyFrameTimes = W3DNEWARRAY unsigned int [NumAlphaKeyFrames];
- AlphaKeyFrameValues = W3DNEWARRAY float [NumAlphaKeyFrames];
- AlphaKeyFrameDeltas = W3DNEWARRAY float [NumAlphaKeyFrames];
- for (i = 0; i < NumAlphaKeyFrames; i++) {
- AlphaKeyFrameTimes[i] = src.AlphaKeyFrameTimes[i];
- AlphaKeyFrameValues[i] = src.AlphaKeyFrameValues[i];
- AlphaKeyFrameDeltas[i] = src.AlphaKeyFrameDeltas[i];
- }
- // Copy alpha randomizer table
- if (src.RandomAlphaEntries) {
- RandomAlphaEntries = W3DNEWARRAY float [NumRandomAlphaEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomAlphaEntriesMinus1; j++) {
- RandomAlphaEntries[j] = src.RandomAlphaEntries[j];
- }
- }
- } else {
- AlphaKeyFrameValues = W3DNEWARRAY float [1];
- AlphaKeyFrameValues[0] = src.AlphaKeyFrameValues[0];
- }
- NumRandomSizeEntriesMinus1 = src.NumRandomSizeEntriesMinus1;
- if (src.Size) {
- // Create size array
- Size = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Size") );
- // Copy size keyframes
- SizeKeyFrameTimes = W3DNEWARRAY unsigned int [NumSizeKeyFrames];
- SizeKeyFrameValues = W3DNEWARRAY float [NumSizeKeyFrames];
- SizeKeyFrameDeltas = W3DNEWARRAY float [NumSizeKeyFrames];
- for (i = 0; i < NumSizeKeyFrames; i++) {
- SizeKeyFrameTimes[i] = src.SizeKeyFrameTimes[i];
- SizeKeyFrameValues[i] = src.SizeKeyFrameValues[i];
- SizeKeyFrameDeltas[i] = src.SizeKeyFrameDeltas[i];
- }
- // Copy size randomizer table
- if (src.RandomSizeEntries) {
- RandomSizeEntries = W3DNEWARRAY float [NumRandomSizeEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomSizeEntriesMinus1; j++) {
- RandomSizeEntries[j] = src.RandomSizeEntries[j];
- }
- }
- } else {
- SizeKeyFrameValues = W3DNEWARRAY float [1];
- SizeKeyFrameValues[0] = src.SizeKeyFrameValues[0];
- }
- // Set up the rotation / orientation keyframes
- NumRandomRotationEntriesMinus1 = src.NumRandomRotationEntriesMinus1;
- NumRandomOrientationEntriesMinus1 = src.NumRandomOrientationEntriesMinus1;
- if (src.Orientation) {
- // Create orientation array
- Orientation = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Orientation") );
- // Copy rotation / orientation keyframes
- RotationKeyFrameTimes = W3DNEWARRAY unsigned int [NumRotationKeyFrames];
- RotationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
- HalfRotationKeyFrameDeltas = W3DNEWARRAY float [NumRotationKeyFrames];
- OrientationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
- for (i = 0; i < NumRotationKeyFrames; i++) {
- RotationKeyFrameTimes[i] = src.RotationKeyFrameTimes[i];
- RotationKeyFrameValues[i] = src.RotationKeyFrameValues[i];
- HalfRotationKeyFrameDeltas[i] = src.HalfRotationKeyFrameDeltas[i];
- OrientationKeyFrameValues[i] = src.OrientationKeyFrameValues[i];
- }
- // Copy rotation randomizer table
- if (src.RandomRotationEntries) {
- RandomRotationEntries = W3DNEWARRAY float [NumRandomRotationEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomRotationEntriesMinus1; j++) {
- RandomRotationEntries[j] = src.RandomRotationEntries[j];
- }
- }
- // Copy starting orientation randomizer table
- if (src.RandomOrientationEntries) {
- RandomOrientationEntries = W3DNEWARRAY float [NumRandomOrientationEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomOrientationEntriesMinus1; j++) {
- RandomOrientationEntries[j] = src.RandomOrientationEntries[j];
- }
- }
- } else {
- // Unlike other properties, if there is no Orientation array then all the arrays are NULL
- // (including the Values array) - there is an implicit starting value of 0.
- }
- // Set up the frame keyframes
- // Frame and UCoord both use Frame Key Frames for the source data
- NumRandomFrameEntriesMinus1 = src.NumRandomFrameEntriesMinus1;
- if (src.Frame || src.UCoord) {
- // Create frame array
- if (src.Frame) {
- Frame = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Frame") );
- } else {
- UCoord = NEW_REF( ShareBufferClass<float>, (MaxNum, "ParticleBufferClass::UCoord") );
- }
- // Copy frame keyframes
- FrameKeyFrameTimes = W3DNEWARRAY unsigned int [NumFrameKeyFrames];
- FrameKeyFrameValues = W3DNEWARRAY float [NumFrameKeyFrames];
- FrameKeyFrameDeltas = W3DNEWARRAY float [NumFrameKeyFrames];
- for (i = 0; i < NumFrameKeyFrames; i++) {
- FrameKeyFrameTimes[i] = src.FrameKeyFrameTimes[i];
- FrameKeyFrameValues[i] = src.FrameKeyFrameValues[i];
- FrameKeyFrameDeltas[i] = src.FrameKeyFrameDeltas[i];
- }
- // Copy frame randomizer table
- if (src.RandomFrameEntries) {
- RandomFrameEntries = W3DNEWARRAY float [NumRandomFrameEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomFrameEntriesMinus1; j++) {
- RandomFrameEntries[j] = src.RandomFrameEntries[j];
- }
- }
- } else {
- FrameKeyFrameValues = W3DNEWARRAY float [1];
- FrameKeyFrameValues[0] = src.FrameKeyFrameValues[0];
- }
- // Set up the blur times keyframes
- NumRandomBlurTimeEntriesMinus1 = src.NumRandomBlurTimeEntriesMinus1;
- if (NumBlurTimeKeyFrames > 0) {
- // Copy blur time keyframes
- BlurTimeKeyFrameTimes = new unsigned int [NumBlurTimeKeyFrames];
- BlurTimeKeyFrameValues = new float [NumBlurTimeKeyFrames];
- BlurTimeKeyFrameDeltas = new float [NumBlurTimeKeyFrames];
- for (i = 0; i < NumBlurTimeKeyFrames; i++) {
- BlurTimeKeyFrameTimes[i] = src.BlurTimeKeyFrameTimes[i];
- BlurTimeKeyFrameValues[i] = src.BlurTimeKeyFrameValues[i];
- BlurTimeKeyFrameDeltas[i] = src.BlurTimeKeyFrameDeltas[i];
- }
- // Copy blur time randomizer table
- if (src.RandomBlurTimeEntries) {
- RandomBlurTimeEntries = new float [NumRandomBlurTimeEntriesMinus1 + 1];
- for (unsigned int j = 0; j <= NumRandomBlurTimeEntriesMinus1; j++) {
- RandomBlurTimeEntries[j] = src.RandomBlurTimeEntries[j];
- }
- }
- } else {
- BlurTimeKeyFrameValues = new float [1];
- BlurTimeKeyFrameValues[0] = src.BlurTimeKeyFrameValues[0];
- }
- // We do not add a ref for the emitter (see DTor for detailed explanation)
- // if (Emitter) Emitter->Add_Ref();
- // Set up new particle queue:
- NewParticleQueue = W3DNEWARRAY NewParticleStruct[MaxNum];
- // Inputs don't need to be range-checked (emitter did that).
- Accel = src.Accel;
- HasAccel = src.HasAccel;
- switch (RenderMode)
- {
- case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
- {
- // Set up worldspace point group
- WWASSERT(src.PointGroup);
- PointGroup = W3DNEW PointGroupClass();
- PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
- PointGroup->Set_Texture(src.PointGroup->Peek_Texture());
- PointGroup->Set_Shader(src.PointGroup->Get_Shader());
- PointGroup->Set_Point_Mode(PointGroupClass::TRIS);
- PointGroup->Set_Frame_Row_Column_Count_Log2(src.PointGroup->Get_Frame_Row_Column_Count_Log2());
- }
- break;
- case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
- {
- // Set up worldspace point group
- WWASSERT(src.PointGroup);
- PointGroup = W3DNEW PointGroupClass();
- PointGroup->Set_Flag(PointGroupClass::TRANSFORM, true);
- PointGroup->Set_Texture(src.PointGroup->Peek_Texture());
- PointGroup->Set_Shader(src.PointGroup->Get_Shader());
- PointGroup->Set_Point_Mode(PointGroupClass::QUADS);
- PointGroup->Set_Frame_Row_Column_Count_Log2(src.PointGroup->Get_Frame_Row_Column_Count_Log2());
- }
- break;
- case W3D_EMITTER_RENDER_MODE_LINE:
- {
- WWASSERT(src.LineRenderer);
- LineRenderer = W3DNEW SegLineRendererClass(*src.LineRenderer);
- }
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
- {
- WWASSERT(src.LineGroup);
- LineGroup = W3DNEW LineGroupClass();
- LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
- LineGroup->Set_Texture(src.LineGroup->Peek_Texture());
- LineGroup->Set_Shader(src.LineGroup->Get_Shader());
- LineGroup->Set_Line_Mode(LineGroupClass::TETRAHEDRON);
- TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition") );
- // TODO: Change TailPosition to Kinematic state and add
- // tail positions to bounding box
- Set_Force_Visible(1);
- }
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
- {
- WWASSERT(src.LineGroup);
- LineGroup = W3DNEW LineGroupClass();
- LineGroup->Set_Flag(LineGroupClass::TRANSFORM, true);
- LineGroup->Set_Texture(src.LineGroup->Peek_Texture());
- LineGroup->Set_Shader(src.LineGroup->Get_Shader());
- LineGroup->Set_Line_Mode(LineGroupClass::PRISM);
- TailPosition = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::TailPosition") );
- // TODO: Change TailPosition to Kinematic state and add
- // tail positions to bounding box
- Set_Force_Visible(1);
- }
- break;
- default:
- WWASSERT(0);
- break;
- }
- // Set up circular buffer. Contents are not initialized because the
- // start/end indices currently indicate the buffer is empty.
- Position[0] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
- if (PingPongPosition) {
- Position[1] = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Position") );
- }
- APT = NEW_REF( ShareBufferClass<unsigned int> , (MaxNum, "ParticleBufferClass::APT") );
- GroupID = NEW_REF( ShareBufferClass<unsigned char> , (MaxNum, "ParticleBufferClass::GroupID") );
- Velocity = W3DNEWARRAY Vector3[MaxNum];
- TimeStamp = W3DNEWARRAY unsigned int[MaxNum];
- // So that the object is ready for use after construction, we will
- // complete its initialization by initializing its cost and value arrays
- // according to a screen area of 1.
- int minlod = Calculate_Cost_Value_Arrays(1.0f, Value, Cost);
- // Ensure lod is no less than minimum allowed
- if (Get_LOD_Level() < minlod) Set_LOD_Level(minlod);
- // Update Global Count
- TotalActiveCount++;
- }
- ParticleBufferClass & ParticleBufferClass::operator = (const ParticleBufferClass & that)
- {
- RenderObjClass::operator = (that);
- if (this != &that) {
- assert(0); // TODO: if you hit this assert, please implement me !!!;-)
- }
- return * this;
- }
- ParticleBufferClass::~ParticleBufferClass(void)
- {
- if (NewParticleQueue) delete [] NewParticleQueue;
- if (ColorKeyFrameTimes) delete [] ColorKeyFrameTimes;
- if (ColorKeyFrameValues) delete [] ColorKeyFrameValues;
- if (ColorKeyFrameDeltas) delete [] ColorKeyFrameDeltas;
- if (AlphaKeyFrameTimes) delete [] AlphaKeyFrameTimes;
- if (AlphaKeyFrameValues) delete [] AlphaKeyFrameValues;
- if (AlphaKeyFrameDeltas) delete [] AlphaKeyFrameDeltas;
- if (SizeKeyFrameTimes) delete [] SizeKeyFrameTimes;
- if (SizeKeyFrameValues) delete [] SizeKeyFrameValues;
- if (SizeKeyFrameDeltas) delete [] SizeKeyFrameDeltas;
- if (RotationKeyFrameTimes) delete [] RotationKeyFrameTimes;
- if (RotationKeyFrameValues) delete [] RotationKeyFrameValues;
- if (HalfRotationKeyFrameDeltas) delete [] HalfRotationKeyFrameDeltas;
- if (OrientationKeyFrameValues) delete [] OrientationKeyFrameValues;
- if (FrameKeyFrameTimes) delete [] FrameKeyFrameTimes;
- if (FrameKeyFrameValues) delete [] FrameKeyFrameValues;
- if (FrameKeyFrameDeltas) delete [] FrameKeyFrameDeltas;
- if (BlurTimeKeyFrameTimes) delete [] BlurTimeKeyFrameTimes;
- if (BlurTimeKeyFrameValues) delete [] BlurTimeKeyFrameValues;
- if (BlurTimeKeyFrameDeltas) delete [] BlurTimeKeyFrameDeltas;
- if (RandomColorEntries) delete [] RandomColorEntries;
- if (RandomAlphaEntries) delete [] RandomAlphaEntries;
- if (RandomSizeEntries) delete [] RandomSizeEntries;
- if (RandomRotationEntries) delete [] RandomRotationEntries;
- if (RandomOrientationEntries) delete [] RandomOrientationEntries;
- if (RandomFrameEntries) delete [] RandomFrameEntries;
- if (RandomBlurTimeEntries) delete [] RandomBlurTimeEntries;
-
- if (PointGroup) delete PointGroup;
- if (LineRenderer) delete LineRenderer;
- if (LineGroup) delete LineGroup;
- REF_PTR_RELEASE(Position[0]);
- REF_PTR_RELEASE(Position[1]);
- REF_PTR_RELEASE(Diffuse);
- REF_PTR_RELEASE(TailDiffuse);
- REF_PTR_RELEASE(Color);
- REF_PTR_RELEASE(Alpha);
- REF_PTR_RELEASE(Size);
- REF_PTR_RELEASE(Orientation);
- REF_PTR_RELEASE(Frame);
- REF_PTR_RELEASE(UCoord);
- REF_PTR_RELEASE(TailPosition);
- REF_PTR_RELEASE(APT);
- REF_PTR_RELEASE(GroupID);
- if (Velocity) delete [] Velocity;
- if (TimeStamp) delete [] TimeStamp;
- if (Emitter) {
- // We should not have an emitter at this point, since the emitter
- // should still have a live ref to us if it still exists which would
- // prevent us from getting killed.
- assert(0);
- // We do not release-ref the emitter pointer because we did not add a
- // ref for it to begin with; the ref is not needed (if the emitter gets
- // deleted it will tell us to clear our emitter pointer) and actually
- // harmful (if emitter and buffer each have refcounted pointers to the
- // other neither would ever get deleted).
- // Emitter->Release_Ref();
- Emitter = NULL;
- }
- // Update Global Count
- TotalActiveCount--;
- }
- RenderObjClass * ParticleBufferClass::Clone(void) const
- {
- return W3DNEW ParticleBufferClass(*this);
- }
- int ParticleBufferClass::Get_Num_Polys(void) const
- {
- // Currently in particle buffers, the cost happens to be equal to thwe polygon count.
- return (int)Get_Cost();
- }
- int ParticleBufferClass::Get_Particle_Count(void) const
- {
- return NonNewNum + NewNum;
- }
- void ParticleBufferClass::Render(RenderInfoClass & rinfo)
- {
- WWPROFILE("ParticleBuffer::Render");
- unsigned int sort_level = SORT_LEVEL_NONE;
- if (!WW3D::Is_Sorting_Enabled())
- sort_level=Get_Shader().Guess_Sort_Level();
- if (WW3D::Are_Static_Sort_Lists_Enabled() && sort_level!=SORT_LEVEL_NONE) {
-
- WW3D::Add_To_Static_Sort_List(this, sort_level);
- } else {
- // Ensure particles' kinematic state is updated
- Update_Kinematic_Particle_State();
- // Since we are rendering the particles, visual state needs to be updated (but not if the
- // entire particle buffer is decimated away)
- if (DecimationThreshold < LodCount - 1) {
- Update_Visual_Particle_State();
- }
- switch( RenderMode )
- {
- case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
- case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
- Render_Particles(rinfo);
- break;
- case W3D_EMITTER_RENDER_MODE_LINE:
- Render_Line(rinfo);
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
- case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
- Render_Line_Group(rinfo);
- break;
- }
- }
- }
- void ParticleBufferClass::Generate_APT(ShareBufferClass <unsigned int> **apt,unsigned int &active_point_count)
- {
- if (NonNewNum < (int)MaxNum || DecimationThreshold > 0) {
- // In the general case, a range in a circular buffer can be composed of up
- // to two subranges. Find the Start - End subranges.
- // This differs from other similar code segments because we want to access
- // the subranges in memory order (rather than in queue order) this time.
- unsigned int sub1_start; // Start of subrange 1.
- unsigned int sub1_end; // End of subrange 1.
- unsigned int sub2_start; // Start of subrange 2.
- unsigned int sub2_end; // End of subrange 2.
- unsigned int i; // Loop index.
- if ((Start < End) || ((Start == End) && NonNewNum == 0)) {
- sub1_start = Start;
- sub1_end = End;
- sub2_start = End;
- sub2_end = End;
- } else {
- sub1_start = 0;
- sub1_end = End;
- sub2_start = Start;
- sub2_end = MaxNum;
- }
- // Generate APT:
- unsigned int *apt_ptr = APT->Get_Array();
- for (i = sub1_start; i < sub1_end; i++) {
- if (PermutationArray[i & 0xF] >= DecimationThreshold) {
- apt_ptr[active_point_count++] = i;
- }
- }
- for (i = sub2_start; i < sub2_end; i++) {
- if (PermutationArray[i & 0xF] >= DecimationThreshold) {
- apt_ptr[active_point_count++] = i;
- }
- }
- *apt = APT;
- } else {
- active_point_count = NonNewNum;
- }
- }
- void ParticleBufferClass::Combine_Color_And_Alpha()
- {
- // Temporary array copying to combine diffuse and alpha to one array.
- if (Color || Alpha) {
- unsigned cnt=MaxNum;
- if (!Diffuse) {
- Diffuse = NEW_REF( ShareBufferClass<Vector4> , (MaxNum, "ParticleBufferClass::Diffuse") );
- }
- if (Color && Alpha) {
- VectorProcessorClass::Copy(
- Diffuse->Get_Array(),
- Color->Get_Array(),
- Alpha->Get_Array(),
- cnt);
- }
- else if (Color) {
- VectorProcessorClass::Copy(
- Diffuse->Get_Array(),
- Color->Get_Array(),
- 1.0f,
- cnt);
- }
- else {
- VectorProcessorClass::Copy(
- Diffuse->Get_Array(),
- Vector3(1.0f,1.0f,1.0f),
- Alpha->Get_Array(),
- cnt);
- }
- VectorProcessorClass::Clamp(
- Diffuse->Get_Array(),
- Diffuse->Get_Array(),
- 0.0f,
- 1.0f,
- cnt);
- }
- else if (Diffuse) {
- Diffuse->Release_Ref();
- Diffuse=NULL;
- }
- }
- void ParticleBufferClass::Render_Particles(RenderInfoClass & rinfo)
- {
- // If the number of active points is less than the maximum or we need to decimate particles
- // (for LOD purposes), build the active point table:
- ShareBufferClass<unsigned int> *apt = NULL;
- unsigned int active_point_count = 0;
- Generate_APT(&apt,active_point_count);
- // Set color, alpha, size defaults if array not present:
- if (!Color) {
- PointGroup->Set_Point_Color(ColorKeyFrameValues[0]);
- }
- if (!Alpha) {
- PointGroup->Set_Point_Alpha(AlphaKeyFrameValues[0]);
- }
- if (!Size) {
- PointGroup->Set_Point_Size(SizeKeyFrameValues[0]);
- }
- if (!Orientation) {
- // The rotation keyframes are used to derive the orientation indirectly, as well as the
- // starting orientation randomizer. If there is no Orientation array that means both are
- // absent so the orientation should just be set to 0.
- PointGroup->Set_Point_Orientation(0);
- }
- if (!Frame) {
- PointGroup->Set_Point_Frame(((int)(FrameKeyFrameValues[0])) & 0xFF);
- }
- // Pass the point buffer to the point group and render it.
- // If we are using pingpong position buffers pass the right one
- int pingpong = 0;
- if (PingPongPosition) {
- pingpong = WW3D::Get_Frame_Count() & 0x1;
- }
- Combine_Color_And_Alpha();
- PointGroup->Set_Arrays(Position[pingpong], Diffuse, apt, Size, Orientation, Frame, active_point_count);
- Update_Bounding_Box();
- PointGroup->Render(rinfo);
- }
- void ParticleBufferClass::Render_Line(RenderInfoClass & rinfo)
- {
- LineRenderer->Set_Freeze_Random(Is_Freeze_Random());
- // Look up the array to use
- int pingpong = 0;
- if (PingPongPosition) {
- pingpong = WW3D::Get_Frame_Count() & 0x1;
- }
- // Unroll the circular buffer while skipping LOD'd particles
- static SimpleDynVecClass<Vector3> tmp_points;
- static SimpleDynVecClass<Vector4> tmp_diffuse;
- static SimpleDynVecClass<unsigned char> tmp_id;
- Vector3 * positions = Position[pingpong]->Get_Array();
- Vector4 * diffuse = 0;
- Vector4 default_diffuse(0, 0, 0, 0);
- unsigned char *ids = GroupID->Get_Array();
- Combine_Color_And_Alpha();
- if (Diffuse) {
- diffuse = Diffuse->Get_Array();
- } else {
- default_diffuse.Set(ColorKeyFrameValues[0].X, ColorKeyFrameValues[0].Y, ColorKeyFrameValues[0].Z,
- AlphaKeyFrameValues[0]);
- }
- unsigned int sub1_end; // End of subrange 1.
- unsigned int sub2_start; // Start of subrange 2.
- unsigned int i; // Loop index.
- if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
- sub1_end = End;
- sub2_start = End;
- } else {
- sub1_end = MaxNum;
- sub2_start = 0;
- }
- tmp_points.Delete_All(false);
- tmp_diffuse.Delete_All(false);
- tmp_id.Delete_All(false);
- Vector4 *last_color = &default_diffuse;
- unsigned char last_id = 0;
- for (i = Start; i < sub1_end; i++) {
- if (PermutationArray[i & 0xF] >= DecimationThreshold) {
- tmp_points.Add(positions[i]);
- last_color = diffuse ? &diffuse[i] : &default_diffuse;
- tmp_diffuse.Add(*last_color);
- last_id = ids[i];
- tmp_id.Add(last_id);
- }
- }
-
- for (i = sub2_start; i < End; i++) {
- if (PermutationArray[i & 0xF] >= DecimationThreshold) {
- tmp_points.Add(positions[i]);
- last_color = diffuse ? &diffuse[i] : &default_diffuse;
- tmp_diffuse.Add(*last_color);
- last_id = ids[i];
- tmp_id.Add(last_id);
- }
- }
- // add in the emitter's position too for the source
- if (Emitter && !Emitter->Is_Stopped() && (last_id == CurrentGroupID)) {
- tmp_points.Add(Emitter->Get_Position());
- // it has the color of the last point
- tmp_diffuse.Add(*last_color);
- tmp_id.Add(last_id);
- }
- // If we got any points, render them
- if (tmp_points.Count() > 0) {
- SphereClass bounding_sphere;
- Get_Obj_Space_Bounding_Sphere(bounding_sphere);
- // Draw line segments only if they are in the same group
- int count = tmp_points.Count();
- int start = 0;
- int end = 0;
- while (end < count) {
- // detect contiguous runs of IDs
- while ( (end < count) && (tmp_id[start] == tmp_id[end])) {
- end++;
- }
- // render from start, excluding end
- if (end - start > 1) {
- LineRenderer->Render(rinfo,
- Transform,
- end - start,
- &(tmp_points[start]),
- bounding_sphere,
- &(tmp_diffuse[start]));
- }
- start = end;
- }
- }
- }
- void ParticleBufferClass::Render_Line_Group(RenderInfoClass & rinfo)
- {
- // If the number of active points is less than the maximum or we need to decimate particles
- // (for LOD purposes), build the active point table:
- ShareBufferClass<unsigned int> *apt = NULL;
- unsigned int active_point_count = 0;
- Generate_APT(&apt,active_point_count);
- // Set color, alpha, size defaults if array not present:
- if (!Color) {
- LineGroup->Set_Line_Color(ColorKeyFrameValues[0]);
- }
- if (!Alpha) {
- LineGroup->Set_Line_Alpha(AlphaKeyFrameValues[0]);
- }
- if (!Size) {
- LineGroup->Set_Line_Size(SizeKeyFrameValues[0]);
- }
- if (!Frame) {
- LineGroup->Set_Line_UCoord(FrameKeyFrameValues[0]);
- }
- // Pass the point buffer to the line group and render it.
- // If we are using pingpong position buffers pass the right one
- int pingpong = 0;
- if (PingPongPosition) {
- pingpong = WW3D::Get_Frame_Count() & 0x1;
- }
- Combine_Color_And_Alpha();
- TailDiffuseTypeEnum tailtype=Determine_Tail_Diffuse();
- switch (tailtype)
- {
- case BLACK:
- REF_PTR_RELEASE(TailDiffuse);
- DefaultTailDiffuse.Set(0,0,0,0);
- break;
- case WHITE:
- REF_PTR_RELEASE(TailDiffuse);
- DefaultTailDiffuse.Set(1,1,1,1);
- break;
- case SAME_AS_HEAD_ALPHA_ZERO:
- // if head is all one color, set tail the same way
- if (!Diffuse) {
- REF_PTR_RELEASE(TailDiffuse);
- DefaultTailDiffuse.Set(ColorKeyFrameValues[0].X,ColorKeyFrameValues[0].Y,ColorKeyFrameValues[0].Z,0);
- } else {
- // otherwise allocate and copy tail diffuse
- if (!TailDiffuse) TailDiffuse=NEW_REF(ShareBufferClass<Vector4>,(MaxNum, "ParticleBufferClass::TailDiffuse"));
- for (unsigned int i=0; i<MaxNum; i++) {
- Vector4 elt=Diffuse->Get_Element(i);
- elt.W=0;
- TailDiffuse->Set_Element(i,elt);
- }
- }
- break;
- case SAME_AS_HEAD:
- // if head is all one color, set tail the same way
- if (!Diffuse) {
- REF_PTR_RELEASE(TailDiffuse);
- DefaultTailDiffuse.Set(ColorKeyFrameValues[0].X,ColorKeyFrameValues[0].Y,ColorKeyFrameValues[0].Z,AlphaKeyFrameValues[0]);
- } else {
- // otherwise allocate and copy tail diffuse
- if (!TailDiffuse) TailDiffuse=NEW_REF(ShareBufferClass<Vector4>,(MaxNum, "ParticleBufferClass::TailDiffuse"));
- VectorProcessorClass::Copy(TailDiffuse->Get_Array(),Diffuse->Get_Array(),MaxNum);
- }
- break;
- default:
- WWASSERT(0);
- break;
- }
- if (!TailDiffuse)
- LineGroup->Set_Tail_Diffuse(DefaultTailDiffuse);
-
- LineGroup->Set_Arrays(Position[pingpong], TailPosition,Diffuse,TailDiffuse, apt, Size, UCoord, active_point_count);
- Update_Bounding_Box();
- LineGroup->Render(rinfo);
- }
- // Scales the size of the individual particles but doesn't affect their
- // position (and therefore the size of the particle system as a whole)
- void ParticleBufferClass::Scale(float scale)
- {
- // Scale all size keyframes, keyframe deltas, random size entries,
- // MaxSize and SizeRandom.
- unsigned int i;
- if (NumSizeKeyFrames) {
- for (i = 0; i < NumSizeKeyFrames; i++) {
- SizeKeyFrameValues[i] *= scale;
- SizeKeyFrameDeltas[i] *= scale;
- }
- } else {
- SizeKeyFrameValues[0] *= scale;
- }
- if (RandomSizeEntries) {
- for (i = 0; i <= NumRandomSizeEntriesMinus1; i++) {
- RandomSizeEntries[i] *= scale;
- }
- }
- if (LineRenderer) {
- LineRenderer->Scale(scale);
- }
- MaxSize *= scale;
- SizeRandom *= scale;
- Accel *= scale;
- }
- // The particle buffer never receives a Set_Transform/Position call,
- // evem though its bounding volume changes. Since bounding volume
- // invalidations ordinarily occur when these functions are called,
- // the cached bounding volumes will not be invalidated unless we do
- // it elsewhere (such as here). We also need to call the particle
- // emitter's Emit() function (done here to avoid order dependence).
- void ParticleBufferClass::On_Frame_Update(void)
- {
- WWPROFILE("ParticleBufferClass::On_Frame_Update");
- Invalidate_Cached_Bounding_Volumes();
- if (Emitter) {
- Emitter->Emit();
- }
-
- if (Is_Complete()) {
- WWASSERT(Scene);
- Scene->Register(this,SceneClass::RELEASE);
- }
- }
- void ParticleBufferClass::Notify_Added(SceneClass * scene)
- {
- RenderObjClass::Notify_Added(scene);
- scene->Register(this,SceneClass::ON_FRAME_UPDATE);
- }
- void ParticleBufferClass::Notify_Removed(SceneClass * scene)
- {
- scene->Unregister(this,SceneClass::ON_FRAME_UPDATE);
- RenderObjClass::Notify_Removed(scene);
- }
- void ParticleBufferClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
- {
- // This ugly cast is done because the alternative is to make everything
- // in the class mutable, which does not seem like a good solution
- // (Update_Bounding_Box can potentially update the particle state)
- ((ParticleBufferClass *)this)->Update_Bounding_Box();
- // The particle buffer's transform is always identity, so
- // objspace == worldspace.
- // Wrap sphere outside bounding box:
- sphere.Center = BoundingBox.Center;
- sphere.Radius = BoundingBox.Extent.Length();
- }
- void ParticleBufferClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
- {
- // This ugly cast is done because the alternative is to make everything
- // in the class mutable, which does not seem like a good solution
- // (Update_Bounding_Box can potentially update the particle state).
- ((ParticleBufferClass *)this)->Update_Bounding_Box();
- // The particle buffer's transform is always identity, so
- // objspace == worldspace.
- box = BoundingBox;
- }
- void ParticleBufferClass::Prepare_LOD(CameraClass &camera)
- {
- if (Is_Not_Hidden_At_All() == false) {
- return;
- }
- // Estimate the screen area of the particle buffer. We shall take the lesser of two
- // metrics: the standard bounding-sphere projection (which for many particle systems may
- // grossly overestimate the actual screen area), and a measurement based on the screen area of
- // individual particles times the maximum number of particles (in the case of densely
- // overlapping particles this metric can also give numbers which are too high, which is why we
- // use the bounding sphere as backup). Note - to find the area of individual particles we
- // treat them as all being the maximum size and being in the center of the bounding sphere).
- Vector3 cam = camera.Get_Position();
- ViewportClass viewport = camera.Get_Viewport();
- Vector2 vpr_min, vpr_max;
- camera.Get_View_Plane(vpr_min, vpr_max);
- float width_factor = viewport.Width() / (vpr_max.X - vpr_min.X);
- float height_factor = viewport.Height() / (vpr_max.Y - vpr_min.Y);
- const SphereClass & sphere = Get_Bounding_Sphere();
- float dist = (sphere.Center - cam).Length();
- float bounding_sphere_projected_radius = 0.0f;
- float particle_projected_radius = 0.0f;
- if (dist) {
- float oo_dist = 1.0f / dist;
- bounding_sphere_projected_radius = sphere.Radius * oo_dist;
- particle_projected_radius = MaxSize * oo_dist;
- }
- float bs_rad_sq = bounding_sphere_projected_radius * bounding_sphere_projected_radius;
- float p_rad_sq = particle_projected_radius * particle_projected_radius * MaxNum;
- float proj_area = WWMATH_PI * MIN(bs_rad_sq, p_rad_sq) * width_factor * height_factor;
- // Filter the area over time so we don't get as many pops in the LOD algorithm
- ProjectedArea = 0.9f * ProjectedArea + 0.1f * proj_area;
- int minlod = Calculate_Cost_Value_Arrays(ProjectedArea, Value, Cost);
- // Ensure lod is no less than minimum allowed
- if (Get_LOD_Level() < minlod) Set_LOD_Level(minlod);
- PredictiveLODOptimizerClass::Add_Object(this);
- }
- void ParticleBufferClass::Increment_LOD(void)
- {
- if (DecimationThreshold > 0) DecimationThreshold--;
- }
- void ParticleBufferClass::Decrement_LOD(void)
- {
- if (DecimationThreshold < LodCount) DecimationThreshold++;
- }
- float ParticleBufferClass::Get_Cost(void) const
- {
- return(Cost[(LodCount - 1) - DecimationThreshold]);
- }
- float ParticleBufferClass::Get_Value(void) const
- {
- return(Value[(LodCount - 1) - DecimationThreshold]);
- }
- float ParticleBufferClass::Get_Post_Increment_Value(void) const
- {
- return(Value[LodCount - DecimationThreshold]);
- }
- void ParticleBufferClass::Set_LOD_Level(int lod)
- {
- lod = Bound(lod, 0, (int)LodCount);
- DecimationThreshold = (LodCount - 1) - lod;
- }
- int ParticleBufferClass::Get_LOD_Level(void) const
- {
- return((LodCount - 1) - DecimationThreshold);
- }
- int ParticleBufferClass::Get_LOD_Count(void) const
- {
- return LodCount;
- }
- int ParticleBufferClass::Calculate_Cost_Value_Arrays(float screen_area, float *values, float *costs) const
- {
- unsigned int lod = 0;
- // Calculate Cost heuristic for each LOD (we currently ignore pixel costs for particle systems)
- // The cost factor is later multiplied by the LOD level. The LOD level is the numerator of the
- // fraction of particles rendered, where 16 is the denominator. For this reason the cost factor
- // is based on a 1/16 (0.0625) of the total.
- float cost_factor=0.0f;
- switch (RenderMode)
- {
- case W3D_EMITTER_RENDER_MODE_TRI_PARTICLES:
- cost_factor = (float)MaxNum * 0.0625f;
- break;
- case W3D_EMITTER_RENDER_MODE_QUAD_PARTICLES:
- cost_factor = (float)MaxNum * 2.0f * 0.0625f;
- break;
- case W3D_EMITTER_RENDER_MODE_LINE:
- cost_factor = (float) (2*MaxNum-1) * 0.0625f;
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA:
- cost_factor = (float)MaxNum * 4.0f * 0.0625f;
- break;
- case W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM:
- cost_factor = (float)MaxNum * 8.0f * 0.0625f;
- break;
- }
- for (lod = 0; lod < LodCount; lod++) {
- costs[lod] = cost_factor * (float)lod;
- // If cost is zero set it to a small nonzero amount to avoid divisions by zero.
- costs[lod] = (costs[lod] != 0) ? costs[lod] : 0.000001f;
- }
- // Calculate Value heuristic. First, all LOD levels for which
- // MaxScreenSize is smaller than screen_area have their Value set to
- // AT_MIN_LOD, as well as the first LOD after that (unless there are no
- // other LODs):
- for (lod = 0; lod < LodCount && LODMaxScreenSizes[lod] < screen_area; lod++) {
- values[lod] = AT_MIN_LOD;
- }
- if (lod >= LodCount) {
- lod = LodCount - 1;
- } else {
- values[lod] = AT_MIN_LOD;
- }
- // Now lod is the lowest allowed - return this value.
- int minlod = lod;
- // Calculate Value heuristic for any remaining LODs based on normalized screen area:
- lod++;
- for (; lod < LodCount; lod++) {
- // Currently the cost happens to be equal to the poly count. We use a floating-
- // point poly count since costs[] contains an approximation to the true polycount which may
- // be less than one in some cases (we want to avoid 0 polycounts except for true null LODs)
- float polycount = costs[lod];
- float benefit_factor = (polycount > WWMATH_EPSILON) ? (1 - (0.5f / (polycount * polycount))) : 0.0f;
- values[lod] = (benefit_factor * screen_area * LodBias) / costs[lod];
- }
- values[LodCount] = AT_MAX_LOD; // Post-inc value will flag max LOD.
- return minlod;
- }
-
- void ParticleBufferClass::Reset_Colors(ParticlePropertyStruct<Vector3> &new_props)
- {
- unsigned int i; // Used in loops
- unsigned int ui_previous_key_time = 0;
- unsigned int ui_current_key_time = 0;
- ColorRandom = new_props.Rand;
- // If the randomizer is effectively zero and there are no keyframes, then we just create a
- // values array with one entry and store the starting value in it (the keyframes and random
- // table will not be used in this case).
- static const float eps_byte = 0.0038f; // Epsilon value - less than 1/255
- 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);
- if (color_rand_zero && new_props.NumKeyFrames == 0) {
- // Release Color, ColorKeyFrameTimes and ColorKeyFrameDeltas if present. Reuse
- // ColorKeyFrameValues if the right size, otherwise release and reallocate.
- if (Color) {
- Color->Release_Ref();
- Color = NULL;
- }
- if (ColorKeyFrameTimes) {
- delete [] ColorKeyFrameTimes;
- ColorKeyFrameTimes = NULL;
- }
- if (ColorKeyFrameDeltas) {
- delete [] ColorKeyFrameDeltas;
- ColorKeyFrameDeltas = NULL;
- }
- if (ColorKeyFrameValues) {
- if (NumColorKeyFrames > 1) {
- delete [] ColorKeyFrameValues;
- ColorKeyFrameValues = W3DNEWARRAY Vector3 [1];
- }
- } else {
- ColorKeyFrameValues = W3DNEWARRAY Vector3 [1];
- }
- NumColorKeyFrames = 0;
- NumRandomColorEntriesMinus1 = 0;
- ColorKeyFrameValues[0] = new_props.Start;
- } else {
- // Create the color array if not present
- if (!Color) {
- Color = NEW_REF( ShareBufferClass<Vector3> , (MaxNum, "ParticleBufferClass::Color") );
- }
- // Check times of color keyframes (each keytime must be larger than the
- // previous one by at least a millisecond, and we stop at the first
- // keytime of MaxAge or larger. (If all keyframes below MaxAge, color is
- // constant during the last segment between last keyframe and MaxAge).
- ui_previous_key_time = 0;
- for (unsigned int ckey = 0; ckey < new_props.NumKeyFrames; ckey++) {
- ui_current_key_time = (unsigned int)(new_props.KeyTimes[ckey] * 1000.0f);
- WWASSERT(ui_current_key_time > ui_previous_key_time);
- if (ui_current_key_time >= MaxAge) break;
- ui_previous_key_time = ui_current_key_time;
- }
- bool color_constant_at_end = (ckey == new_props.NumKeyFrames);
- // Reuse ColorKeyFrameValues, ColorKeyFrameTimes and ColorKeyFrameDeltas if the right size,
- // otherwise release and reallocate.
- unsigned int new_num_color_key_frames = ckey + 1;// Includes start keyframe (keytime == 0).
- if (new_num_color_key_frames != NumColorKeyFrames) {
- if (ColorKeyFrameTimes) {
- delete [] ColorKeyFrameTimes;
- ColorKeyFrameTimes = NULL;
- }
- if (ColorKeyFrameValues) {
- delete [] ColorKeyFrameValues;
- ColorKeyFrameValues = NULL;
- }
- if (ColorKeyFrameDeltas) {
- delete [] ColorKeyFrameDeltas;
- ColorKeyFrameDeltas = NULL;
- }
- NumColorKeyFrames = new_num_color_key_frames;
- ColorKeyFrameTimes = W3DNEWARRAY unsigned int [NumColorKeyFrames];
- ColorKeyFrameValues = W3DNEWARRAY Vector3 [NumColorKeyFrames];
- ColorKeyFrameDeltas = W3DNEWARRAY Vector3 [NumColorKeyFrames];
- }
- // Set color keyframes (deltas will be set later)
- ColorKeyFrameTimes[0] = 0;
- ColorKeyFrameValues[0] = new_props.Start;
- for (i = 1; i < NumColorKeyFrames; i++) {
- unsigned int im1 = i - 1;
- ColorKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
- ColorKeyFrameValues[i] = new_props.Values[im1];
- }
- // Do deltas for all color keyframes except last
- for (i = 0; i < NumColorKeyFrames - 1; i++) {
- ColorKeyFrameDeltas[i] = (ColorKeyFrameValues[i + 1] - ColorKeyFrameValues[i]) /
- (float)(ColorKeyFrameTimes[i + 1] - ColorKeyFrameTimes[i]);
- }
- // Do delta for last color keyframe (i is NumColorKeyFrames - 1)
- if (color_constant_at_end) {
- ColorKeyFrameDeltas[i].Set(0.0, 0.0, 0.0);
- } else {
- // This is OK because if color_constant_at_end is false, NumColorKeyFrames is equal or
- // smaller than color.NumKeyFrames so color.Values[NumColorKeyFrames - 1] and
- // color.KeyTimes[NumColorKeyFrames - 1] exist.
- ColorKeyFrameDeltas[i] = (new_props.Values[i] - ColorKeyFrameValues[i]) /
- (new_props.KeyTimes[i] * 1000.0f - (float)ColorKeyFrameTimes[i]);
- }
- // Set up color randomizer table
- if (color_rand_zero) {
- if (RandomColorEntries) {
- // Reuse RandomColorEntries if the right size, otherwise release and reallocate.
- if (NumRandomColorEntriesMinus1 != 0) {
- delete [] RandomColorEntries;
- RandomColorEntries = W3DNEWARRAY Vector3 [1];
- }
- } else {
- RandomColorEntries = W3DNEWARRAY Vector3 [1];
- }
- NumRandomColorEntriesMinus1 = 0;
- RandomColorEntries[0].X = 0.0f;
- RandomColorEntries[0].Y = 0.0f;
- RandomColorEntries[0].Z = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomColorEntries) {
- // Reuse RandomColorEntries if the right size, otherwise release and reallocate.
- if (NumRandomColorEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomColorEntries;
- RandomColorEntries = W3DNEWARRAY Vector3 [default_randomizer_entries];
- }
- } else {
- RandomColorEntries = W3DNEWARRAY Vector3 [default_randomizer_entries];
- }
- NumRandomColorEntriesMinus1 = default_randomizer_entries - 1;
- float rscale = new_props.Rand.X * oo_intmax;
- float gscale = new_props.Rand.Y * oo_intmax;
- float bscale = new_props.Rand.Z * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomColorEntriesMinus1; j++) {
- RandomColorEntries[j] = Vector3(rand_gen * rscale, rand_gen * gscale, rand_gen * bscale);
- }
- }
- }
- }
- void ParticleBufferClass::Reset_Opacity(ParticlePropertyStruct<float> &new_props)
- {
- unsigned int i; // Used in loops
- unsigned int ui_previous_key_time = 0;
- unsigned int ui_current_key_time = 0;
- OpacityRandom = new_props.Rand;
- // If the randomizer is effectively zero and there are no keyframes, then we just create a
- // values array with one entry and store the starting value in it (the keyframes and random
- // table will not be used in this case).
- static const float eps_byte = 0.0038f; // Epsilon value - less than 1/255
- bool alpha_rand_zero = (fabs(new_props.Rand) < eps_byte);
- if (alpha_rand_zero && new_props.NumKeyFrames == 0) {
- // Release Alpha, AlphaKeyFrameTimes and AlphaKeyFrameDeltas if present. Reuse
- // AlphaKeyFrameValues if the right size, otherwise release and reallocate.
- if (Alpha) {
- Alpha->Release_Ref();
- Alpha = NULL;
- }
- if (AlphaKeyFrameTimes) {
- delete [] AlphaKeyFrameTimes;
- AlphaKeyFrameTimes = NULL;
- }
- if (AlphaKeyFrameDeltas) {
- delete [] AlphaKeyFrameDeltas;
- AlphaKeyFrameDeltas = NULL;
- }
- if (AlphaKeyFrameValues) {
- if (NumAlphaKeyFrames > 1) {
- delete [] AlphaKeyFrameValues;
- AlphaKeyFrameValues = W3DNEWARRAY float [1];
- }
- } else {
- AlphaKeyFrameValues = W3DNEWARRAY float [1];
- }
- NumAlphaKeyFrames = 0;
- NumRandomAlphaEntriesMinus1 = 0;
- AlphaKeyFrameValues[0] = new_props.Start;
- } else {
- // Create the alpha array if not present
- if (!Alpha) {
- Alpha = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Alpha") );
- }
- // Check times of opacity keyframes (each keytime must be larger than the
- // previous one by at least a millisecond, and we stop at the first
- // keytime of MaxAge or larger. (If all keyframes below MaxAge, alpha is
- // constant during the last segment between last keyframe and MaxAge).
- ui_previous_key_time = 0;
- for (unsigned int akey = 0; akey < new_props.NumKeyFrames; akey++) {
- ui_current_key_time = (unsigned int)(new_props.KeyTimes[akey] * 1000.0f);
- WWASSERT(ui_current_key_time > ui_previous_key_time);
- if (ui_current_key_time >= MaxAge) break;
- ui_previous_key_time = ui_current_key_time;
- }
- bool alpha_constant_at_end = (akey == new_props.NumKeyFrames);
- // Reuse AlphaKeyFrameValues, AlphaKeyFrameTimes and AlphaKeyFrameDeltas if the right size,
- // otherwise release and reallocate.
- unsigned int new_num_alpha_key_frames = akey + 1;// Includes start keyframe (keytime == 0).
- if (new_num_alpha_key_frames != NumAlphaKeyFrames) {
- if (AlphaKeyFrameTimes) {
- delete [] AlphaKeyFrameTimes;
- AlphaKeyFrameTimes = NULL;
- }
- if (AlphaKeyFrameValues) {
- delete [] AlphaKeyFrameValues;
- AlphaKeyFrameValues = NULL;
- }
- if (AlphaKeyFrameDeltas) {
- delete [] AlphaKeyFrameDeltas;
- AlphaKeyFrameDeltas = NULL;
- }
- NumAlphaKeyFrames = new_num_alpha_key_frames;
- AlphaKeyFrameTimes = W3DNEWARRAY unsigned int [NumAlphaKeyFrames];
- AlphaKeyFrameValues = W3DNEWARRAY float [NumAlphaKeyFrames];
- AlphaKeyFrameDeltas = W3DNEWARRAY float [NumAlphaKeyFrames];
- }
- // Set alpha keyframes (deltas will be set later)
- AlphaKeyFrameTimes[0] = 0;
- AlphaKeyFrameValues[0] = new_props.Start;
- for (i = 1; i < NumAlphaKeyFrames; i++) {
- unsigned int im1 = i - 1;
- AlphaKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
- AlphaKeyFrameValues[i] = new_props.Values[im1];
- }
- // Do deltas for all alpha keyframes except last
- for (i = 0; i < NumAlphaKeyFrames - 1; i++) {
- AlphaKeyFrameDeltas[i] = (AlphaKeyFrameValues[i + 1] - AlphaKeyFrameValues[i]) /
- (float)(AlphaKeyFrameTimes[i + 1] - AlphaKeyFrameTimes[i]);
- }
- // Do delta for last alpha keyframe (i is NumAlphaKeyFrames - 1)
- if (alpha_constant_at_end) {
- AlphaKeyFrameDeltas[i] = 0.0f;
- } else {
- // This is OK because if alpha_constant_at_end is false, NumAlphaKeyFrames is equal or
- // smaller than opacity.NumKeyFrames so opacity.Values[NumAlphaKeyFrames - 1] and
- // opacity.KeyTimes[NumAlphaKeyFrames - 1] exist.
- AlphaKeyFrameDeltas[i] = (new_props.Values[i] - AlphaKeyFrameValues[i]) /
- (new_props.KeyTimes[i] * 1000.0f - (float)AlphaKeyFrameTimes[i]);
- }
- // Set up alpha randomizer table
- if (alpha_rand_zero) {
- if (RandomAlphaEntries) {
- // Reuse RandomAlphaEntries if the right size, otherwise release and reallocate.
- if (NumRandomAlphaEntriesMinus1 != 0) {
- delete [] RandomAlphaEntries;
- RandomAlphaEntries = W3DNEWARRAY float [1];
- }
- } else {
- RandomAlphaEntries = W3DNEWARRAY float [1];
- }
- NumRandomAlphaEntriesMinus1 = 0;
- RandomAlphaEntries[0] = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomAlphaEntries) {
- // Reuse RandomAlphaEntries if the right size, otherwise release and reallocate.
- if (NumRandomAlphaEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomAlphaEntries;
- RandomAlphaEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- } else {
- RandomAlphaEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- NumRandomAlphaEntriesMinus1 = default_randomizer_entries - 1;
- float ascale = new_props.Rand * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomAlphaEntriesMinus1; j++) {
- RandomAlphaEntries[j] = rand_gen * ascale;
- }
- }
- }
- }
- void ParticleBufferClass::Reset_Size(ParticlePropertyStruct<float> &new_props)
- {
- unsigned int i; // Used in loops
- unsigned int ui_previous_key_time = 0;
- unsigned int ui_current_key_time = 0;
- SizeRandom = new_props.Rand;
- // If the randomizer is effectively zero and there are no keyframes, then we just create a
- // values array with one entry and store the starting value in it (the keyframes and random
- // table will not be used in this case).
- static const float eps_size = 1.0e-12f; // Size scale unknown so must use very small epsilon
- bool size_rand_zero = (fabs(new_props.Rand) < eps_size);
- if (size_rand_zero && new_props.NumKeyFrames == 0) {
- // Release Size, SizeKeyFrameTimes and SizeaKeyFrameDeltas if present. Reuse
- // SizeKeyFrameValues if the right size, otherwise release and reallocate.
- if (Size) {
- Size->Release_Ref();
- Size = NULL;
- }
- if (SizeKeyFrameTimes) {
- delete [] SizeKeyFrameTimes;
- SizeKeyFrameTimes = NULL;
- }
- if (SizeKeyFrameDeltas) {
- delete [] SizeKeyFrameDeltas;
- SizeKeyFrameDeltas = NULL;
- }
- if (SizeKeyFrameValues) {
- if (NumSizeKeyFrames > 1) {
- delete [] SizeKeyFrameValues;
- SizeKeyFrameValues = W3DNEWARRAY float [1];
- }
- } else {
- SizeKeyFrameValues = W3DNEWARRAY float [1];
- }
- NumSizeKeyFrames = 0;
- NumRandomSizeEntriesMinus1 = 0;
- SizeKeyFrameValues[0] = new_props.Start;
- MaxSize = SizeKeyFrameValues[0];
- } else {
- // Create the size array if not present
- if (!Size) {
- Size = NEW_REF( ShareBufferClass<float> , (MaxNum, "ParticleBufferClass::Size") );
- }
- // Check times of size keyframes (each keytime must be larger than the
- // previous one by at least a millisecond, and we stop at the first
- // keytime of MaxAge or larger. (If all keyframes below MaxAge, size is
- // constant during the last segment between last keyframe and MaxAge).
- ui_previous_key_time = 0;
- for (unsigned int skey = 0; skey < new_props.NumKeyFrames; skey++) {
- ui_current_key_time = (unsigned int)(new_props.KeyTimes[skey] * 1000.0f);
- WWASSERT(ui_current_key_time > ui_previous_key_time);
- if (ui_current_key_time >= MaxAge) break;
- ui_previous_key_time = ui_current_key_time;
- }
- bool size_constant_at_end = (skey == new_props.NumKeyFrames);
- // Reuse SizeKeyFrameValues, SizeKeyFrameTimes and SizeKeyFrameDeltas if the right size,
- // otherwise release and reallocate.
- unsigned int new_num_size_key_frames = skey + 1;// Includes start keyframe (keytime == 0).
- if (new_num_size_key_frames != NumSizeKeyFrames) {
- if (SizeKeyFrameTimes) {
- delete [] SizeKeyFrameTimes;
- SizeKeyFrameTimes = NULL;
- }
- if (SizeKeyFrameValues) {
- delete [] SizeKeyFrameValues;
- SizeKeyFrameValues = NULL;
- }
- if (SizeKeyFrameDeltas) {
- delete [] SizeKeyFrameDeltas;
- SizeKeyFrameDeltas = NULL;
- }
- NumSizeKeyFrames = new_num_size_key_frames;
- SizeKeyFrameTimes = W3DNEWARRAY unsigned int [NumSizeKeyFrames];
- SizeKeyFrameValues = W3DNEWARRAY float [NumSizeKeyFrames];
- SizeKeyFrameDeltas = W3DNEWARRAY float [NumSizeKeyFrames];
- }
- // Set size keyframes (deltas will be set later)
- SizeKeyFrameTimes[0] = 0;
- SizeKeyFrameValues[0] = new_props.Start;
- for (i = 1; i < NumSizeKeyFrames; i++) {
- unsigned int im1 = i - 1;
- SizeKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
- SizeKeyFrameValues[i] = new_props.Values[im1];
- }
- // Do deltas for all size keyframes except last
- for (i = 0; i < NumSizeKeyFrames - 1; i++) {
- SizeKeyFrameDeltas[i] = (SizeKeyFrameValues[i + 1] - SizeKeyFrameValues[i]) /
- (float)(SizeKeyFrameTimes[i + 1] - SizeKeyFrameTimes[i]);
- }
- // Do delta for last size keyframe (i is NumSizeKeyFrames - 1)
- if (size_constant_at_end) {
- SizeKeyFrameDeltas[i] = 0.0f;
- } else {
- // This is OK because if size_constant_at_end is false, NumSizeKeyFrames is equal or
- // smaller than new_props.NumKeyFrames so new_props.Values[NumSizeKeyFrames - 1] and
- // new_props.KeyTimes[NumSizeKeyFrames - 1] exist.
- SizeKeyFrameDeltas[i] = (new_props.Values[i] - SizeKeyFrameValues[i]) /
- (new_props.KeyTimes[i] * 1000.0f - (float)SizeKeyFrameTimes[i]);
- }
- // Find maximum size (for BBox updates)
- MaxSize = SizeKeyFrameValues[0];
- for (i = 1; i < NumSizeKeyFrames; i++) {
- MaxSize = MAX(MaxSize, SizeKeyFrameValues[i]);
- }
- // If last delta is positive, there may be a larger size keyframe:
- float last_size = SizeKeyFrameValues[NumSizeKeyFrames - 1] + SizeKeyFrameDeltas[NumSizeKeyFrames - 1] *
- (float)(MaxAge - SizeKeyFrameTimes[NumSizeKeyFrames - 1]);
- MaxSize = MAX(MaxSize, last_size);
- MaxSize += fabs(new_props.Rand);
- // Set up size randomizer table
- if (size_rand_zero) {
- if (RandomSizeEntries) {
- // Reuse RandomSizeEntries if the right size, otherwise release and reallocate.
- if (NumRandomSizeEntriesMinus1 != 0) {
- delete [] RandomSizeEntries;
- RandomSizeEntries = W3DNEWARRAY float [1];
- }
- } else {
- RandomSizeEntries = W3DNEWARRAY float [1];
- }
- NumRandomSizeEntriesMinus1 = 0;
- RandomSizeEntries[0] = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomSizeEntries) {
- // Reuse RandomSizeEntries if the right size, otherwise release and reallocate.
- if (NumRandomSizeEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomSizeEntries;
- RandomSizeEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- } else {
- RandomSizeEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- NumRandomSizeEntriesMinus1 = default_randomizer_entries - 1;
- float sscale = new_props.Rand * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomSizeEntriesMinus1; j++) {
- RandomSizeEntries[j] = rand_gen * sscale;
- }
- }
- }
- }
- void ParticleBufferClass::Reset_Rotations(ParticlePropertyStruct<float> &new_props, float orient_rnd)
- {
- unsigned int i; // Used in loops
- float oo_intmax = 1.0f / (float)INT_MAX;
- unsigned int ui_previous_key_time = 0;
- unsigned int ui_current_key_time = 0;
- /*
- ** NOTE: Input rotations are in rotations per second. These will be converted to rotations per millisecond.
- */
- RotationRandom = new_props.Rand * 0.001f;
- InitialOrientationRandom = orient_rnd;
- // If both randomizers are effectively zero and rotation is constant zero, then all arrays are NULL.
- static const float eps_orientation = 2.77777778e-4f; // Epsilon is equivalent to 0.1 degree
- static const float eps_rotation = 2.77777778e-4f; // Epsilon is equivalent to one rotation per hour (in rotations / second)
- bool orientation_rand_zero = fabs(orient_rnd) < eps_orientation;
- bool rotation_rand_zero = fabs(new_props.Rand) < eps_rotation;
- if (orientation_rand_zero && rotation_rand_zero && new_props.NumKeyFrames == 0 && fabs(new_props.Start) < eps_rotation) {
- // Release Arrays,
- REF_PTR_RELEASE(Orientation);
- if (RotationKeyFrameTimes) {
- delete [] RotationKeyFrameTimes;
- RotationKeyFrameTimes = NULL;
- }
- if (HalfRotationKeyFrameDeltas) {
- delete [] HalfRotationKeyFrameDeltas;
- HalfRotationKeyFrameDeltas = NULL;
- }
- if (RotationKeyFrameValues) {
- delete [] RotationKeyFrameValues;
- RotationKeyFrameValues = NULL;
- }
- if (OrientationKeyFrameValues) {
- delete [] OrientationKeyFrameValues;
- OrientationKeyFrameValues = NULL;
- }
- NumRotationKeyFrames = 0;
- NumRandomRotationEntriesMinus1 = 0;
- NumRandomOrientationEntriesMinus1 = 0;
-
- } else {
- // Create the array if not present
- if (!Orientation) {
- Orientation = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Orientation") );
- }
- // Check times of the keyframes (each keytime must be larger than the
- // previous one by at least a millisecond, and we stop at the first
- // keytime of MaxAge or larger. (If all keyframes below MaxAge, the value is
- // constant during the last segment between last keyframe and MaxAge).
- ui_previous_key_time = 0;
- for (unsigned int key = 0; key < new_props.NumKeyFrames; key++) {
- ui_current_key_time = (unsigned int)(new_props.KeyTimes[key] * 1000.0f);
- WWASSERT(ui_current_key_time > ui_previous_key_time);
- if (ui_current_key_time >= MaxAge) break;
- ui_previous_key_time = ui_current_key_time;
- }
- bool rotation_constant_at_end = (key == new_props.NumKeyFrames);
- // Reuse RotationKeyFrameValues, RotationKeyFrameTimes, RotationKeyFrameDeltas and
- // OrientationKeyFrameValues if the right size, otherwise release and reallocate.
- unsigned int new_num_key_frames = key + 1;// Includes start keyframe (keytime == 0).
- if (new_num_key_frames != NumRotationKeyFrames) {
- if (RotationKeyFrameTimes) {
- delete [] RotationKeyFrameTimes;
- RotationKeyFrameTimes = NULL;
- }
- if (RotationKeyFrameValues) {
- delete [] RotationKeyFrameValues;
- RotationKeyFrameValues = NULL;
- }
- if (HalfRotationKeyFrameDeltas) {
- delete [] HalfRotationKeyFrameDeltas;
- HalfRotationKeyFrameDeltas = NULL;
- }
- if (OrientationKeyFrameValues) {
- delete [] OrientationKeyFrameValues;
- OrientationKeyFrameValues = NULL;
- }
- NumRotationKeyFrames = new_num_key_frames;
- RotationKeyFrameTimes = W3DNEWARRAY unsigned int [NumRotationKeyFrames];
- RotationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
- HalfRotationKeyFrameDeltas = W3DNEWARRAY float [NumRotationKeyFrames];
- OrientationKeyFrameValues = W3DNEWARRAY float [NumRotationKeyFrames];
- }
- // Set rotation keyframes (deltas will be set later)
- RotationKeyFrameTimes[0] = 0;
- RotationKeyFrameValues[0] = new_props.Start * 0.001f;
- for (i = 1; i < NumRotationKeyFrames; i++) {
- unsigned int im1 = i - 1;
- RotationKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
- RotationKeyFrameValues[i] = new_props.Values[im1] * 0.001f;
- }
- // Do deltas for all rotation keyframes except last
- for (i = 0; i < NumRotationKeyFrames - 1; i++) {
- HalfRotationKeyFrameDeltas[i] = 0.5f * ( (RotationKeyFrameValues[i + 1] - RotationKeyFrameValues[i]) /
- (float)(RotationKeyFrameTimes[i + 1] - RotationKeyFrameTimes[i]) );
- }
- // Do delta for last rotation keyframe (i is NumRotationKeyFrames - 1)
- if (rotation_constant_at_end) {
- HalfRotationKeyFrameDeltas[i] = 0.0f;
- } else {
- // This is OK because if rotation_constant_at_end is false, NumRotationKeyFrames is equal or
- // smaller than new_props.NumKeyFrames so new_props.Values[NumRotationKeyFrames - 1] and
- // new_props.KeyTimes[NumRotationKeyFrames - 1] exist.
- HalfRotationKeyFrameDeltas[i] = 0.5f * (new_props.Values[i] * 0.001f - RotationKeyFrameValues[i]) /
- (new_props.KeyTimes[i] * 1000.0f - (float)RotationKeyFrameTimes[i]);
- }
- // Calculate orientation keyframes by integrating the rotation at each keyframe
- OrientationKeyFrameValues[0] = 0.0f;
- for (i = 1; i < NumRotationKeyFrames; i++) {
- float delta_t = (float)(RotationKeyFrameTimes[i] - RotationKeyFrameTimes[i - 1]);
- OrientationKeyFrameValues[i] = OrientationKeyFrameValues[i - 1] + delta_t *
- (RotationKeyFrameValues[i - 1] + HalfRotationKeyFrameDeltas[i - 1] * delta_t);
- }
- // Set up rotation randomizer table
- if (rotation_rand_zero) {
- if (RandomRotationEntries) {
- // Reuse RandomRotationEntries if the right size, otherwise release and reallocate.
- if (NumRandomRotationEntriesMinus1 != 0) {
- delete [] RandomRotationEntries;
- RandomRotationEntries = W3DNEWARRAY float [1];
- }
- } else {
- RandomRotationEntries = W3DNEWARRAY float [1];
- }
- NumRandomRotationEntriesMinus1 = 0;
- RandomRotationEntries[0] = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomRotationEntries) {
- // Reuse RandomRotationEntries if the right size, otherwise release and reallocate.
- if (NumRandomRotationEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomRotationEntries;
- RandomRotationEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- } else {
- RandomRotationEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- NumRandomRotationEntriesMinus1 = default_randomizer_entries - 1;
- float scale = new_props.Rand * 0.001f * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomRotationEntriesMinus1; j++) {
- RandomRotationEntries[j] = rand_gen * scale;
- }
- }
- // Set up orientation randomizer table
- if (orientation_rand_zero) {
- if (RandomOrientationEntries) {
- // Reuse RandomOrientationEntries if the right size, otherwise release and reallocate.
- if (NumRandomOrientationEntriesMinus1 != 0) {
- delete [] RandomOrientationEntries;
- RandomOrientationEntries = W3DNEWARRAY float [1];
- }
- } else {
- RandomOrientationEntries = W3DNEWARRAY float [1];
- }
- NumRandomOrientationEntriesMinus1 = 0;
- RandomOrientationEntries[0] = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomOrientationEntries) {
- // Reuse RandomOrientationEntries if the right size, otherwise release and reallocate.
- if (NumRandomOrientationEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomOrientationEntries;
- RandomOrientationEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- } else {
- RandomOrientationEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- NumRandomOrientationEntriesMinus1 = default_randomizer_entries - 1;
- float scale = orient_rnd * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomOrientationEntriesMinus1; j++) {
- RandomOrientationEntries[j] = rand_gen * scale;
- }
- }
- }
- }
- void ParticleBufferClass::Reset_Frames(ParticlePropertyStruct<float> &new_props)
- {
- unsigned int i; // Used in loops
- float oo_intmax = 1.0f / (float)INT_MAX;
- unsigned int ui_previous_key_time = 0;
- unsigned int ui_current_key_time = 0;
- FrameRandom = new_props.Rand;
- // If the randomizer is effectively zero and there are no keyframes, then we just create a
- // values array with one entry and store the starting value in it (the keyframes and random
- // table will not be used in this case).
- static const float eps_frame = 0.1f; // Epsilon is equivalent to 0.1 frame
- bool frame_rand_zero = (fabs(new_props.Rand) < eps_frame);
- if (frame_rand_zero && new_props.NumKeyFrames == 0) {
- // Release Arrays, Reuse KeyFrameValues if the right size,
- // otherwise release and reallocate.
- REF_PTR_RELEASE(Frame);
- REF_PTR_RELEASE(UCoord);
- if (FrameKeyFrameTimes) {
- delete [] FrameKeyFrameTimes;
- FrameKeyFrameTimes = NULL;
- }
- if (FrameKeyFrameDeltas) {
- delete [] FrameKeyFrameDeltas;
- FrameKeyFrameDeltas = NULL;
- }
- if (FrameKeyFrameValues) {
- if (NumFrameKeyFrames > 1) {
- delete [] FrameKeyFrameValues;
- FrameKeyFrameValues = W3DNEWARRAY float [1];
- }
- } else {
- FrameKeyFrameValues = W3DNEWARRAY float [1];
- }
- NumFrameKeyFrames = 0;
- NumRandomFrameEntriesMinus1 = 0;
- FrameKeyFrameValues[0] = new_props.Start;
-
- } else {
- // Create the array if not present
- if ((RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA) ||
- (RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM)) {
- if (!UCoord) {
- UCoord = NEW_REF( ShareBufferClass<float>, (MaxNum, "ParticleBufferClass::UCoord") );
- }
- } else {
- if (!Frame) {
- Frame = NEW_REF( ShareBufferClass<uint8> , (MaxNum, "ParticleBufferClass::Frame") );
- }
- }
- // Check times of the keyframes (each keytime must be larger than the
- // previous one by at least a millisecond, and we stop at the first
- // keytime of MaxAge or larger. (If all keyframes below MaxAge, the value is
- // constant during the last segment between last keyframe and MaxAge).
- ui_previous_key_time = 0;
- for (unsigned int key = 0; key < new_props.NumKeyFrames; key++) {
- ui_current_key_time = (unsigned int)(new_props.KeyTimes[key] * 1000.0f);
- WWASSERT(ui_current_key_time > ui_previous_key_time);
- if (ui_current_key_time >= MaxAge) break;
- ui_previous_key_time = ui_current_key_time;
- }
- bool frame_constant_at_end = (key == new_props.NumKeyFrames);
- // Reuse FrameKeyFrameValues, FrameKeyFrameTimes and FrameKeyFrameDeltas if the right size,
- // otherwise release and reallocate.
- unsigned int new_num_key_frames = key + 1;// Includes start keyframe (keytime == 0).
- if (new_num_key_frames != NumFrameKeyFrames) {
- if (FrameKeyFrameTimes) {
- delete [] FrameKeyFrameTimes;
- FrameKeyFrameTimes = NULL;
- }
- if (FrameKeyFrameValues) {
- delete [] FrameKeyFrameValues;
- FrameKeyFrameValues = NULL;
- }
- if (FrameKeyFrameDeltas) {
- delete [] FrameKeyFrameDeltas;
- FrameKeyFrameDeltas = NULL;
- }
- NumFrameKeyFrames = new_num_key_frames;
- FrameKeyFrameTimes = W3DNEWARRAY unsigned int [NumFrameKeyFrames];
- FrameKeyFrameValues = W3DNEWARRAY float [NumFrameKeyFrames];
- FrameKeyFrameDeltas = W3DNEWARRAY float [NumFrameKeyFrames];
- }
- // Set keyframes (deltas will be set later)
- FrameKeyFrameTimes[0] = 0;
- FrameKeyFrameValues[0] = new_props.Start;
- for (i = 1; i < NumFrameKeyFrames; i++) {
- unsigned int im1 = i - 1;
- FrameKeyFrameTimes[i] = (unsigned int)(new_props.KeyTimes[im1] * 1000.0f);
- FrameKeyFrameValues[i] = new_props.Values[im1];
- }
- // Do deltas for all frame keyframes except last
- for (i = 0; i < NumFrameKeyFrames - 1; i++) {
- FrameKeyFrameDeltas[i] = (FrameKeyFrameValues[i + 1] - FrameKeyFrameValues[i]) /
- (float)(FrameKeyFrameTimes[i + 1] - FrameKeyFrameTimes[i]);
- }
- // Do delta for last frame keyframe (i is NumFrameKeyFrames - 1)
- if (frame_constant_at_end) {
- FrameKeyFrameDeltas[i] = 0.0f;
- } else {
- // This is OK because if frame_constant_at_end is false, NumFrameKeyFrames is equal or
- // smaller than new_props.NumKeyFrames so new_props.Values[NumFrameKeyFrames - 1] and
- // new_props.KeyTimes[NumFrameKeyFrames - 1] exist.
- FrameKeyFrameDeltas[i] = (new_props.Values[i] - FrameKeyFrameValues[i]) /
- (new_props.KeyTimes[i] * 1000.0f - (float)FrameKeyFrameTimes[i]);
- }
- // Set up frame randomizer table
- if (frame_rand_zero) {
- if (RandomFrameEntries) {
- // Reuse RandomFrameEntries if the right size, otherwise release and reallocate.
- if (NumRandomFrameEntriesMinus1 != 0) {
- delete [] RandomFrameEntries;
- RandomFrameEntries = W3DNEWARRAY float [1];
- }
- } else {
- RandomFrameEntries = W3DNEWARRAY float [1];
- }
- NumRandomFrameEntriesMinus1 = 0;
- RandomFrameEntries[0] = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomFrameEntries) {
- // Reuse RandomFrameEntries if the right size, otherwise release and reallocate.
- if (NumRandomFrameEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomFrameEntries;
- RandomFrameEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- } else {
- RandomFrameEntries = W3DNEWARRAY float [default_randomizer_entries];
- }
- NumRandomFrameEntriesMinus1 = default_randomizer_entries - 1;
- float scale = new_props.Rand * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomFrameEntriesMinus1; j++) {
- RandomFrameEntries[j] = rand_gen * scale;
- }
- }
- }
- }
- void ParticleBufferClass::Reset_Blur_Times(ParticlePropertyStruct<float> &new_blur_times)
- {
- unsigned int i; // Used in loops
- float oo_intmax = 1.0f / (float)INT_MAX;
- unsigned int ui_previous_key_time = 0;
- unsigned int ui_current_key_time = 0;
- BlurTimeRandom = new_blur_times.Rand;
- // If the randomizer is effectively zero and there are no keyframes, then we just create a
- // values array with one entry and store the starting value in it (the keyframes and random
- // table will not be used in this case).
- static const float eps_blur = 1e-5f; // Epsilon is equivalent to 1e-5 units per second
- bool blurtime_rand_zero = (fabs(new_blur_times.Rand) < eps_blur);
- if (blurtime_rand_zero && new_blur_times.NumKeyFrames == 0) {
- // Release Arrays, Reuse KeyFrameValues if the right size,
- // otherwise release and reallocate.
- if (BlurTimeKeyFrameTimes) {
- delete [] BlurTimeKeyFrameTimes;
- BlurTimeKeyFrameTimes = NULL;
- }
- if (BlurTimeKeyFrameDeltas) {
- delete [] BlurTimeKeyFrameDeltas;
- BlurTimeKeyFrameDeltas = NULL;
- }
- if (BlurTimeKeyFrameValues) {
- if (NumBlurTimeKeyFrames > 1) {
- delete [] BlurTimeKeyFrameValues;
- BlurTimeKeyFrameValues = new float [1];
- }
- } else {
- BlurTimeKeyFrameValues = new float [1];
- }
- NumBlurTimeKeyFrames = 0;
- NumRandomBlurTimeEntriesMinus1 = 0;
- BlurTimeKeyFrameValues[0] = new_blur_times.Start;
-
- } else {
- // Check times of the keyframes (each keytime must be larger than the
- // previous one by at least a millisecond, and we stop at the first
- // keytime of MaxAge or larger. (If all keyframes below MaxAge, the value is
- // constant during the last segment between last keyframe and MaxAge).
- ui_previous_key_time = 0;
- for (unsigned int key = 0; key < new_blur_times.NumKeyFrames; key++) {
- ui_current_key_time = (unsigned int)(new_blur_times.KeyTimes[key] * 1000.0f);
- WWASSERT(ui_current_key_time > ui_previous_key_time);
- if (ui_current_key_time >= MaxAge) break;
- ui_previous_key_time = ui_current_key_time;
- }
- bool blurtime_constant_at_end = (key == new_blur_times.NumKeyFrames);
- // Reuse BlurTimeKeyFrameValues, BlurTimeKeyFrameTimes and BlurTimeKeyFrameDeltas if the right size,
- // otherwise release and reallocate.
- unsigned int new_num_key_frames = key + 1;// Includes start keyframe (keytime == 0).
- if (new_num_key_frames != NumBlurTimeKeyFrames) {
- if (BlurTimeKeyFrameTimes) {
- delete [] BlurTimeKeyFrameTimes;
- BlurTimeKeyFrameTimes = NULL;
- }
- if (BlurTimeKeyFrameValues) {
- delete [] BlurTimeKeyFrameValues;
- BlurTimeKeyFrameValues = NULL;
- }
- if (BlurTimeKeyFrameDeltas) {
- delete [] BlurTimeKeyFrameDeltas;
- BlurTimeKeyFrameDeltas = NULL;
- }
- NumBlurTimeKeyFrames = new_num_key_frames;
- BlurTimeKeyFrameTimes = new unsigned int [NumBlurTimeKeyFrames];
- BlurTimeKeyFrameValues = new float [NumBlurTimeKeyFrames];
- BlurTimeKeyFrameDeltas = new float [NumBlurTimeKeyFrames];
- }
- // Set keyframes (deltas will be set later)
- BlurTimeKeyFrameTimes[0] = 0;
- BlurTimeKeyFrameValues[0] = new_blur_times.Start;
- for (i = 1; i < NumBlurTimeKeyFrames; i++) {
- unsigned int im1 = i - 1;
- BlurTimeKeyFrameTimes[i] = (unsigned int)(new_blur_times.KeyTimes[im1] * 1000.0f);
- BlurTimeKeyFrameValues[i] = new_blur_times.Values[im1];
- }
- // Do deltas for all frame keyframes except last
- for (i = 0; i < NumBlurTimeKeyFrames - 1; i++) {
- BlurTimeKeyFrameDeltas[i] = (BlurTimeKeyFrameValues[i + 1] - BlurTimeKeyFrameValues[i]) /
- (float)(BlurTimeKeyFrameTimes[i + 1] - BlurTimeKeyFrameTimes[i]);
- }
- // Do delta for last frame keyframe (i is NumBlurTimeKeyFrames - 1)
- if (blurtime_constant_at_end) {
- BlurTimeKeyFrameDeltas[i] = 0.0f;
- } else {
- // This is OK because if frame_constant_at_end is false, NumBlurTimeKeyFrames is equal or
- // smaller than new_props.NumKeyFrames so new_props.Values[NumBlurTimeKeyFrames - 1] and
- // new_props.KeyTimes[NumBlurTimeKeyFrames - 1] exist.
- BlurTimeKeyFrameDeltas[i] = (new_blur_times.Values[i] - BlurTimeKeyFrameValues[i]) /
- (new_blur_times.KeyTimes[i] * 1000.0f - (float)BlurTimeKeyFrameTimes[i]);
- }
- // Set up frame randomizer table
- if (blurtime_rand_zero) {
- if (RandomBlurTimeEntries) {
- // Reuse RandomBlurTimeEntries if the right size, otherwise release and reallocate.
- if (NumRandomBlurTimeEntriesMinus1 != 0) {
- delete [] RandomBlurTimeEntries;
- RandomBlurTimeEntries = new float [1];
- }
- } else {
- RandomBlurTimeEntries = new float [1];
- }
- NumRandomBlurTimeEntriesMinus1 = 0;
- RandomBlurTimeEntries[0] = 0.0f;
- } else {
- // Default size of randomizer tables (tables for non-zero randomizers will be this size)
- unsigned int pot_num = Find_POT(MaxNum);
- unsigned int default_randomizer_entries = MIN(pot_num, MAX_RANDOM_ENTRIES);
- if (RandomBlurTimeEntries) {
- // Reuse RandomBlurTimeEntries if the right size, otherwise release and reallocate.
- if (NumRandomBlurTimeEntriesMinus1 != (default_randomizer_entries - 1)) {
- delete [] RandomBlurTimeEntries;
- RandomBlurTimeEntries = new float [default_randomizer_entries];
- }
- } else {
- RandomBlurTimeEntries = new float [default_randomizer_entries];
- }
- NumRandomBlurTimeEntriesMinus1 = default_randomizer_entries - 1;
- float scale = new_blur_times.Rand * oo_intmax;
- for (unsigned int j = 0; j <= NumRandomBlurTimeEntriesMinus1; j++) {
- RandomBlurTimeEntries[j] = rand_gen * scale;
- }
- }
- }
- }
- // This informs the buffer that the emitter is dead, so it can release
- // its pointer to it and be removed itself after all its particles dies
- // out.
- void ParticleBufferClass::Emitter_Is_Dead(void)
- {
- IsEmitterDead = true;
- // We do not have a ref for the emitter (see DTor for detailed explanation)
- // Emitter->Release_Ref();
- Emitter = NULL;
- }
- // This set's the buffer's current emitter - this should usually be
- // called only by the emitter's copy constructor after it clones a
- // buffer.
- void ParticleBufferClass::Set_Emitter(ParticleEmitterClass *emitter)
- {
- if (Emitter) {
- // We do not have a ref for the emitter (see DTor for detailed explanation)
- // Emitter->Release_Ref();
- Emitter = NULL;
- }
- Emitter = emitter;
- if (Emitter) {
- // We do not add a ref for the emitter (see DTor for detailed explanation)
- // Emitter->Add_Ref();
- }
- }
-
- NewParticleStruct * ParticleBufferClass::Add_Uninitialized_New_Particle(void)
- {
- // Note that this function does not initialize the new particle - it
- // returns its address to a different function which performs the actual
- // initialization.
- // Push new particle on new particle queue. If it overflows, just adjust
- // queue to remove oldest member (which is the one which was overwritten).
- NewParticleStruct *ptr = &(NewParticleQueue[NewParticleQueueEnd]);
- if (++NewParticleQueueEnd == MaxNum) NewParticleQueueEnd = 0;
- if (++NewParticleQueueCount == (signed)(MaxNum + 1)) {
- // Overflow - advance queue start:
- if (++NewParticleQueueStart == MaxNum) NewParticleQueueStart = 0;
- NewParticleQueueCount--;
- }
- return ptr;
- }
- void ParticleBufferClass::Update_Cached_Bounding_Volumes(void) const
- {
- // This ugly cast is done because the alternative is to make everything
- // in the class mutable, which does not seem like a good solution
- // (Update_Bounding_Box can potentially update the particle state).
- ((ParticleBufferClass *)this)->Update_Bounding_Box();
- // Update cached bounding box and sphere according to the bounding box:
- CachedBoundingSphere.Init(BoundingBox.Center, BoundingBox.Extent.Length());
- CachedBoundingBox = BoundingBox;
- Validate_Cached_Bounding_Volumes();
- }
- void ParticleBufferClass::Update_Kinematic_Particle_State(void)
- {
- // Note: elapsed may be very large indeed the first time the object is
- // updated, but this doesn't matter, since it is actually only used in
- // Update_Non_New_Particles(), which is never called on the first update.
- unsigned int elapsed = WW3D::Get_Sync_Time() - LastUpdateTime;
- if (elapsed == 0U) return;
- // Get new particles from the input buffer and write them into the circular
- // particle buffer, possibly overwriting older particles. Update each
- // according to its age.
- Get_New_Particles();
- // Kill all remaining particles which will pass their max age this update.
- Kill_Old_Particles();
- // Update all living, non-new particles by a uniform time interval.
- if (NonNewNum > 0) Update_Non_New_Particles(elapsed);
- // Mark all new particles as non-new.
- End = NewEnd;
- NonNewNum += NewNum;
- NewNum = 0;
- LastUpdateTime = WW3D::Get_Sync_Time();
- BoundingBoxDirty = true;
- }
- void ParticleBufferClass::Update_Visual_Particle_State(void)
- {
- // NOTE: The visual state (color/alpha/size) is "stateless" in that each time it is calculated
- // without referring to what it was before. This is important for when we optimize the particle
- // systems/pointgroups in the future to chunk triangles into reusable small buffers.
- // If all visual state is constant do nothing.
- // Linegroup modes have a visual state that always have to be updated though
- bool is_linegroup=( (RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_TETRA) ||
- (RenderMode==W3D_EMITTER_RENDER_MODE_LINEGRP_PRISM));
- if (!Color && !Alpha && !Size && !Orientation && !Frame && !UCoord && !is_linegroup) return;
- // In the general case, a range in a circular buffer can be composed of up
- // to two subranges. Find the Start - End subranges.
- unsigned int sub1_end; // End of subrange 1.
- unsigned int sub2_start; // Start of subrange 2.
- if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
- sub1_end = End;
- sub2_start = End;
- } else {
- sub1_end = MaxNum;
- sub2_start = 0;
- }
- unsigned int current_time = WW3D::Get_Sync_Time();
- // The following back-to-back pair of "for" loops traverses the circular
- // buffer subranges in proper order.
- unsigned int ckey = NumColorKeyFrames - 1;
- unsigned int akey = NumAlphaKeyFrames - 1;
- unsigned int skey = NumSizeKeyFrames - 1;
- unsigned int rkey = NumRotationKeyFrames - 1;
- unsigned int fkey = NumFrameKeyFrames - 1;
- unsigned int bkey = NumBlurTimeKeyFrames -1;
- unsigned int part;
- Vector3 *color = Color ? Color->Get_Array(): NULL;
- float *alpha = Alpha ? Alpha->Get_Array(): NULL;
- float *size = Size ? Size->Get_Array(): NULL;
- uint8 *orientation = Orientation ? Orientation->Get_Array(): NULL;
- uint8 *frame = Frame ? Frame->Get_Array(): NULL;
- float *ucoord = UCoord ? UCoord->Get_Array() : NULL;
- Vector3 *tailposition = TailPosition ? TailPosition->Get_Array() : NULL;
- Vector3 *position=NULL;
- if (PingPongPosition) {
- int pingpong = WW3D::Get_Frame_Count() & 0x1;
- position = Position[pingpong]->Get_Array();
- } else {
- position = Position[0]->Get_Array();
- }
- for (part = Start; part < sub1_end; part++) {
- unsigned int part_age = current_time - TimeStamp[part];
- // Ensure the current color keyframe is correct, and calculate color state
- if (color) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < ColorKeyFrameTimes[ckey]; ckey--);
- color[part] = ColorKeyFrameValues[ckey] +
- ColorKeyFrameDeltas[ckey] * (float)(part_age - ColorKeyFrameTimes[ckey]) +
- RandomColorEntries[part & NumRandomColorEntriesMinus1];
- }
- // Ensure the current alpha keyframe is correct, and calculate alpha state
- if (alpha) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < AlphaKeyFrameTimes[akey]; akey--);
- alpha[part] = AlphaKeyFrameValues[akey] +
- AlphaKeyFrameDeltas[akey] * (float)(part_age - AlphaKeyFrameTimes[akey]) +
- RandomAlphaEntries[part & NumRandomAlphaEntriesMinus1];
- }
- // Ensure the current size keyframe is correct, and calculate size state
- if (size) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < SizeKeyFrameTimes[skey]; skey--);
- size[part] = SizeKeyFrameValues[skey] +
- SizeKeyFrameDeltas[skey] * (float)(part_age - SizeKeyFrameTimes[skey]) +
- RandomSizeEntries[part & NumRandomSizeEntriesMinus1];
- // Size (unlike color and alpha) isn't clamped in the engine, so we need to clamp
- // negative values to zero here:
- size[part] = (size[part] >= 0.0f) ? size[part] : 0.0f;
- }
- // Ensure the current rotation keyframe is correct, and calculate orientation state
- if (orientation) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < RotationKeyFrameTimes[rkey]; rkey--);
- float f_delta_t = (float)(part_age - RotationKeyFrameTimes[rkey]);
- float tmp_orient = OrientationKeyFrameValues[rkey] +
- (RotationKeyFrameValues[rkey] + HalfRotationKeyFrameDeltas[rkey] * f_delta_t) * f_delta_t +
- RandomRotationEntries[part & NumRandomRotationEntriesMinus1] * (float)part_age +
- RandomOrientationEntries[part & NumRandomOrientationEntriesMinus1];
-
- orientation[part] = (uint)(((int)(tmp_orient * 256.0f)) & 0xFF);
- }
- // Ensure the current frame keyframe is correct, and calculate frame state
- if (frame) {
- // Frame and ucoord are mutually exclusive
- WWASSERT(ucoord==NULL);
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
- float tmp_frame = FrameKeyFrameValues[fkey] +
- FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
- RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
-
- frame[part] = (uint)(((int)(tmp_frame)) & 0xFF);
- }
- // Ensure the current frame keyframe is correct, and calculate frame state
- // ucoord is the same as frame but in float
- if (ucoord) {
- // Frame and ucoord are mutually exclusive
- WWASSERT(frame==NULL);
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
- ucoord[part] = FrameKeyFrameValues[fkey] +
- FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
- RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
- }
- if (tailposition) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- float blur_time = BlurTimeKeyFrameValues[0];
- if (BlurTimeKeyFrameTimes) {
- for (; part_age < BlurTimeKeyFrameTimes[bkey]; bkey--);
- blur_time = BlurTimeKeyFrameValues[bkey] +
- BlurTimeKeyFrameDeltas[bkey] * (float)(part_age - BlurTimeKeyFrameTimes[bkey]) +
- RandomBlurTimeEntries[part & NumRandomBlurTimeEntriesMinus1];
- }
- tailposition[part]=position[part]-Velocity[part]*blur_time*1000;
- }
- }
- for (part = sub2_start; part < End; part++) {
- unsigned int part_age = current_time - TimeStamp[part];
- // Ensure the current color keyframe is correct, and calculate color state
- if (color) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < ColorKeyFrameTimes[ckey]; ckey--);
- color[part] =
- ColorKeyFrameValues[ckey] +
- ColorKeyFrameDeltas[ckey] * (float)(part_age - ColorKeyFrameTimes[ckey]) +
- RandomColorEntries[part & NumRandomColorEntriesMinus1];
- }
- // Ensure the current alpha keyframe is correct, and calculate alpha state
- if (alpha) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < AlphaKeyFrameTimes[akey]; akey--);
- alpha[part] = AlphaKeyFrameValues[akey] +
- AlphaKeyFrameDeltas[akey] * (float)(part_age - AlphaKeyFrameTimes[akey]) +
- RandomAlphaEntries[part & NumRandomAlphaEntriesMinus1];
- }
- // Ensure the current size keyframe is correct, and calculate size state
- if (size) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < SizeKeyFrameTimes[skey]; skey--);
- size[part] = SizeKeyFrameValues[skey] +
- SizeKeyFrameDeltas[skey] * (float)(part_age - SizeKeyFrameTimes[skey]) +
- RandomSizeEntries[part & NumRandomSizeEntriesMinus1];
- // Size (unlike color) isn't clamped in the engine, so we need to
- // clamp negative values to zero here:
- size[part] = (size[part] >= 0.0f) ? size[part] : 0.0f;
- }
- // Ensure the current rotation keyframe is correct, and calculate orientation state
- if (orientation) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < RotationKeyFrameTimes[rkey]; rkey--);
- float f_delta_t = (float)(part_age - RotationKeyFrameTimes[rkey]);
- float tmp_orient = OrientationKeyFrameValues[rkey] +
- (RotationKeyFrameValues[rkey] + HalfRotationKeyFrameDeltas[rkey] * f_delta_t) * f_delta_t +
- RandomRotationEntries[part & NumRandomRotationEntriesMinus1] * (float)part_age +
- RandomOrientationEntries[part & NumRandomOrientationEntriesMinus1];
-
- orientation[part] = (uint)(((int)(tmp_orient * 256.0f)) & 0xFF);
- }
- // Ensure the current frame keyframe is correct, and calculate frame state
- if (frame) {
- // Frame and ucoord are mutually exclusive
- WWASSERT(ucoord==NULL);
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
- float tmp_frame = FrameKeyFrameValues[fkey] +
- FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
- RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
-
- frame[part] = (uint)(((int)(tmp_frame)) & 0xFF);
- }
- // Ensure the current frame keyframe is correct, and calculate frame state
- // ucoord is the same as frame but in float
- if (ucoord) {
- // Frame and ucoord are mutually exclusive
- WWASSERT(frame==NULL);
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- for (; part_age < FrameKeyFrameTimes[fkey]; fkey--);
- ucoord[part] = FrameKeyFrameValues[fkey] +
- FrameKeyFrameDeltas[fkey] * (float)(part_age - FrameKeyFrameTimes[fkey]) +
- RandomFrameEntries[part & NumRandomFrameEntriesMinus1];
- }
- if (tailposition) {
- // We go from older to younger particles, so we go backwards from the last keyframe until
- // age >= keytime. This loop must terminate because the 0th keytime is 0.
- float blur_time = BlurTimeKeyFrameValues[0];
- if (BlurTimeKeyFrameTimes) {
- for (; part_age < BlurTimeKeyFrameTimes[bkey]; bkey--);
- blur_time = BlurTimeKeyFrameValues[bkey] +
- BlurTimeKeyFrameDeltas[bkey] * (float)(part_age - BlurTimeKeyFrameTimes[bkey]) +
- RandomBlurTimeEntries[part & NumRandomBlurTimeEntriesMinus1];
- }
- tailposition[part]=position[part]-Velocity[part]*blur_time*1000;
- }
- }
- }
- void ParticleBufferClass::Update_Bounding_Box(void)
- {
- // Ensure all particle positions are updated. If bounding box still not
- // dirty, return.
- Update_Kinematic_Particle_State();
- if (!BoundingBoxDirty) return;
- // If there are no particles, generate a dummy bounding box:
- if (NonNewNum == 0U) {
- BoundingBox.Init(Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0));
- BoundingBoxDirty = false;
- return;
- }
- // Find min/max coord values for all points:
- int pingpong = 0;
- if (PingPongPosition) {
- pingpong = WW3D::Get_Frame_Count() & 0x1;
- }
- Vector3 *position = Position[pingpong]->Get_Array();
- Vector3 max_coords = position[Start];
- Vector3 min_coords = position[Start];
- // In the general case, a range in a circular buffer can be composed of up
- // to two subranges. Find the Start - End subranges.
- unsigned int sub1_end; // End of subrange 1.
- unsigned int sub2_start; // Start of subrange 2.
- unsigned int i; // Loop index.
- if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
- sub1_end = End;
- sub2_start = End;
- } else {
- sub1_end = MaxNum;
- sub2_start = 0;
- }
- for (i = Start; i < sub1_end; i++) {
- max_coords.X = max_coords.X >= position[i].X ? max_coords.X : position[i].X;
- max_coords.Y = max_coords.Y >= position[i].Y ? max_coords.Y : position[i].Y;
- max_coords.Z = max_coords.Z >= position[i].Z ? max_coords.Z : position[i].Z;
- min_coords.X = min_coords.X <= position[i].X ? min_coords.X : position[i].X;
- min_coords.Y = min_coords.Y <= position[i].Y ? min_coords.Y : position[i].Y;
- min_coords.Z = min_coords.Z <= position[i].Z ? min_coords.Z : position[i].Z;
- }
- for (i = sub2_start; i < End; i++) {
- max_coords.X = max_coords.X >= position[i].X ? max_coords.X : position[i].X;
- max_coords.Y = max_coords.Y >= position[i].Y ? max_coords.Y : position[i].Y;
- max_coords.Z = max_coords.Z >= position[i].Z ? max_coords.Z : position[i].Z;
- min_coords.X = min_coords.X <= position[i].X ? min_coords.X : position[i].X;
- min_coords.Y = min_coords.Y <= position[i].Y ? min_coords.Y : position[i].Y;
- min_coords.Z = min_coords.Z <= position[i].Z ? min_coords.Z : position[i].Z;
- }
-
- // Extend by maximum possible particle size:
- Vector3 size(MaxSize, MaxSize, MaxSize);
- max_coords += size;
- min_coords -= size;
- // Update bounding box:
- BoundingBox.Init(MinMaxAABoxClass(min_coords,max_coords));
- BoundingBoxDirty = false;
- }
- // NOTE: typically, the number of new particles created in a frame is small
- // relative to the total number of particles, so this is not the most
- // performance-critical particle function. New particles are copied from the
- // new particle vector into the circular buffer, overwriting any older
- // particles (including possibly other new particles) so that the newest
- // particles are preserved. The particles are initialized to their state at
- // the end of the current interval.
- void ParticleBufferClass::Get_New_Particles(void)
- {
- unsigned int current_time = WW3D::Get_Sync_Time();
- // position is the current frame position, prev_pos is the previous frames position (only if
- // we have enabled pingpong position buffers)
- Vector3 *position;
- Vector3 *prev_pos;
- if (PingPongPosition) {
- int pingpong = WW3D::Get_Frame_Count() & 0x1;
- position = Position[pingpong]->Get_Array();
- prev_pos = Position[pingpong ^ 0x1]->Get_Array();
- } else {
- position = Position[0]->Get_Array();
- prev_pos = NULL;
- }
- unsigned char * ids = GroupID->Get_Array();
- for (; NewParticleQueueCount;) {
- // Get particle off new particle queue:
- NewParticleStruct &new_particle = NewParticleQueue[NewParticleQueueStart];
- if (++NewParticleQueueStart == MaxNum) NewParticleQueueStart = 0U;
- NewParticleQueueCount--;
- // Get particle birth time stamp, calculate age. If not under maxage
- // skip this particle.
- TimeStamp[NewEnd] = new_particle.TimeStamp;
- unsigned int age = current_time - TimeStamp[NewEnd];
- if (age >= MaxAge) continue;
- float fp_age = (float)age;
- // Apply velocity and acceleration if present. Otherwise, just apply
- // velocity.
- if (HasAccel) {
- position[NewEnd] = new_particle.Position +
- (new_particle.Velocity + 0.5f * Accel * fp_age) * fp_age;
-
- Velocity[NewEnd] = new_particle.Velocity + (Accel * fp_age);
- } else {
- position[NewEnd] =new_particle.Position +
- (new_particle.Velocity * fp_age);
- Velocity[NewEnd] = new_particle.Velocity;
- }
- // If pingpong enabled, store starting position in prev_pos[].
- if (PingPongPosition) {
- prev_pos[NewEnd] = new_particle.Position;
- }
- // upate the group id
- ids[NewEnd] = new_particle.GroupID;
- // Advance the 'end of new particles' index.
- NewEnd++;
- if (NewEnd == MaxNum) NewEnd = 0;
- // Update the new particles count.
- NewNum++;
- // If we have just overflowed the total buffer, advance Start.
- if ((NewNum + NonNewNum) == (signed)(MaxNum + 1)) {
- Start++;
- if (Start == MaxNum) Start = 0;
- NonNewNum--;
- // If this underflows the 'non-new' buffer, advance End.
- if (NonNewNum == -1) {
- End++;
- if (End == MaxNum) End = 0;
- NonNewNum = 0;
- NewNum--;
- }
- }
- }
- }
- void ParticleBufferClass::Kill_Old_Particles(void)
- {
- // Scan from Start and find the first particle which has an age less than
- // MaxAge - set Start to that position.
- // In the general case, a range in a circular buffer can be composed of up
- // to two subranges. Find the Start - End subranges.
- unsigned int sub1_end; // End of subrange 1.
- unsigned int sub2_start; // Start of subrange 2.
- unsigned int i; // Loop index.
- if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
- sub1_end = End;
- sub2_start = End;
- } else {
- sub1_end = MaxNum;
- sub2_start = 0;
- }
- unsigned int current_time = WW3D::Get_Sync_Time();
- // Stop when the current particle is young enough to be alive.
- bool broke = false;
- for (i = Start; i < sub1_end; i++) {
- if ((current_time - TimeStamp[i]) < MaxAge) {
- broke = true;
- break;
- }
- NonNewNum--;
- }
- if (!broke) {
- for (i = sub2_start; i < End; i++) {
- if ((current_time - TimeStamp[i]) < MaxAge) break;
- NonNewNum--;
- }
- }
- Start = i;
- // NOTE: we do not scan the new particles, because they have been already
- // preculled to be under MaxAge.
- }
- void ParticleBufferClass::Update_Non_New_Particles(unsigned int elapsed)
- {
- // In the general case, a range in a circular buffer can be composed of up
- // to two subranges. Find the Start - End subranges.
- unsigned int sub1_end; // End of subrange 1.
- unsigned int sub2_start; // Start of subrange 2.
- unsigned int i; // Loop index.
- if ((Start < End) || ((Start == End) && NonNewNum ==0)) {
- sub1_end = End;
- sub2_start = End;
- } else {
- sub1_end = MaxNum;
- sub2_start = 0;
- }
- float fp_elapsed_time = (float)elapsed;
- // Update position and velocity for all particles.
- if (PingPongPosition) {
- int pingpong = WW3D::Get_Frame_Count() & 0x1;
- Vector3 *position = Position[pingpong]->Get_Array();
- Vector3 *prev_pos = Position[pingpong ^ 0x1]->Get_Array();
- if (HasAccel) {
- Vector3 delta_v = Accel * fp_elapsed_time;
- Vector3 accel_p = Accel * (0.5f * fp_elapsed_time * fp_elapsed_time);
- for (i = Start; i < sub1_end; i++) {
- position[i] = prev_pos[i] + Velocity[i] * fp_elapsed_time + accel_p;
- Velocity[i] += delta_v;
- }
- for (i = sub2_start; i < End; i++) {
- position[i] = prev_pos[i] + Velocity[i] * fp_elapsed_time + accel_p;
- Velocity[i] += delta_v;
- }
- } else {
- for (i = Start; i < sub1_end; i++) {
- position[i] += Velocity[i] * fp_elapsed_time;
- }
- for (i = sub2_start; i < End; i++) {
- position[i] += Velocity[i] * fp_elapsed_time;
- }
- }
- } else {
- Vector3 *position = Position[0]->Get_Array();
- if (HasAccel) {
- Vector3 delta_v = Accel * fp_elapsed_time;
- Vector3 accel_p = Accel * (0.5f * fp_elapsed_time * fp_elapsed_time);
- for (i = Start; i < sub1_end; i++) {
- position[i] += Velocity[i] * fp_elapsed_time + accel_p;
- Velocity[i] += delta_v;
- }
- for (i = sub2_start; i < End; i++) {
- position[i] += Velocity[i] * fp_elapsed_time + accel_p;
- Velocity[i] += delta_v;
- }
- } else {
- for (i = Start; i < sub1_end; i++) {
- position[i] += Velocity[i] * fp_elapsed_time;
- }
- for (i = sub2_start; i < End; i++) {
- position[i] += Velocity[i] * fp_elapsed_time;
- }
- }
- }
- }
- void ParticleBufferClass::Get_Color_Key_Frames (ParticlePropertyStruct<Vector3> &colors) const
- {
- int real_keyframe_count = (NumColorKeyFrames > 0) ? (NumColorKeyFrames - 1) : 0;
- bool create_last_keyframe = false;
- //
- // Determine if there is a keyframe at the very end of the particle's lifetime
- //
- if ((ColorKeyFrameDeltas != NULL) &&
- ((ColorKeyFrameDeltas[NumColorKeyFrames - 1].X != 0) ||
- (ColorKeyFrameDeltas[NumColorKeyFrames - 1].Y != 0) ||
- (ColorKeyFrameDeltas[NumColorKeyFrames - 1].Z != 0))) {
- real_keyframe_count ++;
- create_last_keyframe = true;
- }
- colors.Start = ColorKeyFrameValues[0];
- colors.Rand = ColorRandom;
- colors.NumKeyFrames = real_keyframe_count;
- colors.KeyTimes = NULL;
- colors.Values = NULL;
- //
- // If we have more than just the start color, build
- // an array of key times and color vatues
- //
- if (real_keyframe_count > 0) {
- colors.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
- colors.Values = W3DNEWARRAY Vector3[real_keyframe_count];
- //
- // Copy the keytimes and color values
- //
- unsigned int index;
- for (index = 1; index < NumColorKeyFrames; index ++) {
- colors.KeyTimes[index - 1] = ((float)ColorKeyFrameTimes[index]) / 1000;
- colors.Values[index - 1] = ColorKeyFrameValues[index];
- }
- //
- // Add a keyframe at the very end of the timeline if necessary
- //
- if (create_last_keyframe) {
- colors.KeyTimes[index - 1] = ((float)MaxAge / 1000);
- //
- // Determine what the value of the last keyframe should be
- //
- Vector3 start_color = ColorKeyFrameValues[index - 1];
- Vector3 &delta = ColorKeyFrameDeltas[NumColorKeyFrames - 1];
- float time_delta = MaxAge - ColorKeyFrameTimes[index - 1];
- colors.Values[index - 1] = start_color + (delta * time_delta);
- }
- }
- return ;
- }
- void ParticleBufferClass::Get_Opacity_Key_Frames (ParticlePropertyStruct<float> &opacities) const
- {
- int real_keyframe_count = (NumAlphaKeyFrames > 0) ? (NumAlphaKeyFrames - 1) : 0;
- bool create_last_keyframe = false;
- //
- // Determine if there is a keyframe at the very end of the particle's lifetime
- //
- if ((AlphaKeyFrameDeltas != NULL) &&
- (AlphaKeyFrameDeltas[NumAlphaKeyFrames - 1] != 0)) {
- real_keyframe_count ++;
- create_last_keyframe = true;
- }
- opacities.Start = AlphaKeyFrameValues[0];
- opacities.Rand = OpacityRandom;
- opacities.NumKeyFrames = real_keyframe_count;
- opacities.KeyTimes = NULL;
- opacities.Values = NULL;
- //
- // If we have more than just the start opacity, build
- // an array of key times and opacity values
- //
- if (real_keyframe_count > 0) {
- opacities.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
- opacities.Values = W3DNEWARRAY float[real_keyframe_count];
- //
- // Copy the keytimes and opacity values
- //
- unsigned int index;
- for (index = 1; index < NumAlphaKeyFrames; index ++) {
- opacities.KeyTimes[index - 1] = ((float)AlphaKeyFrameTimes[index]) / 1000;
- opacities.Values[index - 1] = AlphaKeyFrameValues[index];
- }
- //
- // Add a keyframe at the very end of the timeline if necessary
- //
- if (create_last_keyframe) {
- opacities.KeyTimes[index - 1] = ((float)MaxAge / 1000);
- //
- // Determine what the value of the last keyframe should be
- //
- float start_alpha = AlphaKeyFrameValues[index - 1];
- float &delta = AlphaKeyFrameDeltas[NumAlphaKeyFrames - 1];
- float time_delta = MaxAge - AlphaKeyFrameTimes[index - 1];
- opacities.Values[index - 1] = start_alpha + (delta * time_delta);
- }
- }
- return ;
- }
- void ParticleBufferClass::Get_Size_Key_Frames (ParticlePropertyStruct<float> &sizes) const
- {
- int real_keyframe_count = (NumSizeKeyFrames > 0) ? (NumSizeKeyFrames - 1) : 0;
- bool create_last_keyframe = false;
- //
- // Determine if there is a keyframe at the very end of the particle's lifetime
- //
- if ((SizeKeyFrameDeltas != NULL) &&
- (SizeKeyFrameDeltas[NumSizeKeyFrames - 1] != 0)) {
- real_keyframe_count ++;
- create_last_keyframe = true;
- }
- sizes.Start = SizeKeyFrameValues[0];
- sizes.Rand = SizeRandom;
- sizes.NumKeyFrames = real_keyframe_count;
- sizes.KeyTimes = NULL;
- sizes.Values = NULL;
- //
- // If we have more than just the start opacity, build
- // an array of key times and opacity values
- //
- if (real_keyframe_count > 0) {
- sizes.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
- sizes.Values = W3DNEWARRAY float[real_keyframe_count];
- //
- // Copy the keytimes and size values
- //
- unsigned int index;
- for (index = 1; index < NumSizeKeyFrames; index ++) {
- sizes.KeyTimes[index - 1] = ((float)SizeKeyFrameTimes[index]) / 1000;
- sizes.Values[index - 1] = SizeKeyFrameValues[index];
- }
- //
- // Add a keyframe at the very end of the timeline if necessary
- //
- if (create_last_keyframe) {
- sizes.KeyTimes[index - 1] = ((float)MaxAge / 1000);
- //
- // Determine what the value of the last keyframe should be
- //
- float start_size = SizeKeyFrameValues[index - 1];
- float &delta = SizeKeyFrameDeltas[NumSizeKeyFrames - 1];
- float time_delta = MaxAge - SizeKeyFrameTimes[index - 1];
- sizes.Values[index - 1] = start_size + (delta * time_delta);
- }
- }
- return ;
- }
- void ParticleBufferClass::Get_Rotation_Key_Frames (ParticlePropertyStruct<float> &rotations) const
- {
- int real_keyframe_count = (NumRotationKeyFrames > 0) ? (NumRotationKeyFrames - 1) : 0;
- bool create_last_keyframe = false;
- /*
- ** NOTE: Rotations are stored internally in rotations per millisecond. These will be converted to rotations per second.
- */
- //
- // Determine if there is a keyframe at the very end of the particle's lifetime
- //
- if ((HalfRotationKeyFrameDeltas != NULL) &&
- (HalfRotationKeyFrameDeltas[NumRotationKeyFrames - 1] != 0)) {
- real_keyframe_count ++;
- create_last_keyframe = true;
- }
- // Convert the rotation values from rotations per millisecond to rotations per second.
- rotations.Start = RotationKeyFrameValues ? RotationKeyFrameValues[0] * 1000.0f : 0;
- rotations.Rand = RotationRandom * 1000.0f;
- rotations.NumKeyFrames = real_keyframe_count;
- rotations.KeyTimes = NULL;
- rotations.Values = NULL;
- //
- // If we have more than just the start rotation, build
- // an array of key times and rotation values
- //
- if (real_keyframe_count > 0) {
- rotations.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
- rotations.Values = W3DNEWARRAY float[real_keyframe_count];
- //
- // Copy the keytimes and rotation values
- //
- unsigned int index;
- for (index = 1; index < NumRotationKeyFrames; index ++) {
- rotations.KeyTimes[index - 1] = ((float)RotationKeyFrameTimes[index]) / 1000;
- rotations.Values[index - 1] = RotationKeyFrameValues[index] * 1000.0f;
- }
- //
- // Add a keyframe at the very end of the timeline if necessary
- //
- if (create_last_keyframe) {
- rotations.KeyTimes[index - 1] = ((float)MaxAge / 1000);
- //
- // Determine what the value of the last keyframe should be
- //
- float start_rotation = RotationKeyFrameValues[index - 1];
- float delta = 2.0f * HalfRotationKeyFrameDeltas[NumRotationKeyFrames - 1];
- float time_delta = MaxAge - RotationKeyFrameTimes[index - 1];
- rotations.Values[index - 1] = (start_rotation + (delta * time_delta)) * 1000.0f;
- }
- }
- return ;
- }
- void ParticleBufferClass::Get_Frame_Key_Frames (ParticlePropertyStruct<float> &frames) const
- {
- int real_keyframe_count = (NumFrameKeyFrames > 0) ? (NumFrameKeyFrames - 1) : 0;
- bool create_last_keyframe = false;
- //
- // Determine if there is a keyframe at the very end of the particle's lifetime
- //
- if ((FrameKeyFrameDeltas != NULL) &&
- (FrameKeyFrameDeltas[NumFrameKeyFrames - 1] != 0)) {
- real_keyframe_count ++;
- create_last_keyframe = true;
- }
- frames.Start = FrameKeyFrameValues[0];
- frames.Rand = FrameRandom;
- frames.NumKeyFrames = real_keyframe_count;
- frames.KeyTimes = NULL;
- frames.Values = NULL;
- //
- // If we have more than just the start rotation, build
- // an array of key times and frame values
- //
- if (real_keyframe_count > 0) {
- frames.KeyTimes = W3DNEWARRAY float[real_keyframe_count];
- frames.Values = W3DNEWARRAY float[real_keyframe_count];
- //
- // Copy the keytimes and frame values
- //
- unsigned int index;
- for (index = 1; index < NumFrameKeyFrames; index ++) {
- frames.KeyTimes[index - 1] = ((float)FrameKeyFrameTimes[index]) / 1000;
- frames.Values[index - 1] = FrameKeyFrameValues[index];
- }
- //
- // Add a keyframe at the very end of the timeline if necessary
- //
- if (create_last_keyframe) {
- frames.KeyTimes[index - 1] = ((float)MaxAge / 1000);
- //
- // Determine what the value of the last keyframe should be
- //
- float start_frame = FrameKeyFrameValues[index - 1];
- float &delta = FrameKeyFrameDeltas[NumFrameKeyFrames - 1];
- float time_delta = MaxAge - FrameKeyFrameTimes[index - 1];
- frames.Values[index - 1] = start_frame + (delta * time_delta);
- }
- }
- return ;
- }
- void ParticleBufferClass::Get_Blur_Time_Key_Frames (ParticlePropertyStruct<float> &blurtimes) const
- {
- int real_keyframe_count = (NumBlurTimeKeyFrames > 0) ? (NumBlurTimeKeyFrames - 1) : 0;
- bool create_last_keyframe = false;
- //
- // Determine if there is a keyframe at the very end of the particle's lifetime
- //
- if ((BlurTimeKeyFrameDeltas != NULL) &&
- (BlurTimeKeyFrameDeltas[NumBlurTimeKeyFrames - 1] != 0)) {
- real_keyframe_count ++;
- create_last_keyframe = true;
- }
- blurtimes.Start = BlurTimeKeyFrameValues[0];
- blurtimes.Rand = BlurTimeRandom;
- blurtimes.NumKeyFrames = real_keyframe_count;
- blurtimes.KeyTimes = NULL;
- blurtimes.Values = NULL;
- //
- // If we have more than just the start rotation, build
- // an array of key times and blur time values
- //
- if (real_keyframe_count > 0) {
- blurtimes.KeyTimes = new float[real_keyframe_count];
- blurtimes.Values = new float[real_keyframe_count];
- //
- // Copy the keytimes and frame values
- //
- unsigned int index;
- for (index = 1; index < NumBlurTimeKeyFrames; index ++) {
- blurtimes.KeyTimes[index - 1] = ((float)BlurTimeKeyFrameTimes[index]) / 1000;
- blurtimes.Values[index - 1] = BlurTimeKeyFrameValues[index];
- }
- //
- // Add a keyframe at the very end of the timeline if necessary
- //
- if (create_last_keyframe) {
- blurtimes.KeyTimes[index - 1] = ((float)MaxAge / 1000);
- //
- // Determine what the value of the last keyframe should be
- //
- float start_blurtime = BlurTimeKeyFrameValues[index - 1];
- float &delta = BlurTimeKeyFrameDeltas[NumBlurTimeKeyFrames - 1];
- float time_delta = MaxAge - BlurTimeKeyFrameTimes[index - 1];
- blurtimes.Values[index - 1] = start_blurtime + (delta * time_delta);
- }
- }
- return ;
- }
- void ParticleBufferClass::Set_LOD_Max_Screen_Size(int lod_level,float max_screen_size)
- {
- if ((lod_level <0) || (lod_level > 17)) {
- return;
- }
- LODMaxScreenSizes[lod_level] = max_screen_size;
- }
- float ParticleBufferClass::Get_LOD_Max_Screen_Size(int lod_level)
- {
- if ((lod_level <0) || (lod_level > 17)) {
- return NO_MAX_SCREEN_SIZE;
- }
- return LODMaxScreenSizes[lod_level];
- }
- int ParticleBufferClass::Get_Line_Texture_Mapping_Mode(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Get_Texture_Mapping_Mode();
- }
- return SegLineRendererClass::UNIFORM_WIDTH_TEXTURE_MAP;
- }
- int ParticleBufferClass::Is_Merge_Intersections(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Is_Merge_Intersections();
- }
- return false;
- }
- int ParticleBufferClass::Is_Freeze_Random(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Is_Freeze_Random();
- }
- return false;
- }
- int ParticleBufferClass::Is_Sorting_Disabled(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Is_Sorting_Disabled();
- }
- return false;
- }
- int ParticleBufferClass::Are_End_Caps_Enabled(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Are_End_Caps_Enabled();
- }
- return false;
- }
- int ParticleBufferClass::Get_Subdivision_Level(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Get_Current_Subdivision_Level();
- }
- return 0;
- }
- float ParticleBufferClass::Get_Noise_Amplitude(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Get_Noise_Amplitude();
- }
- return 0.0f;
- }
- float ParticleBufferClass::Get_Merge_Abort_Factor(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Get_Merge_Abort_Factor();
- }
- return 0.0f;
- }
- float ParticleBufferClass::Get_Texture_Tile_Factor(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Get_Texture_Tile_Factor();
- }
- return 1.0f;
- }
- Vector2 ParticleBufferClass::Get_UV_Offset_Rate(void) const
- {
- if (LineRenderer != NULL) {
- return LineRenderer->Get_UV_Offset_Rate();
- }
- return Vector2(0.0f,0.0f);
- }
- ParticleBufferClass::TailDiffuseTypeEnum ParticleBufferClass::Determine_Tail_Diffuse()
- {
- // if there is a texture, the assumption is that the artist
- // is controlling the fadeoff ramp using the texture
- // thus, the ARGB of the tail should be the same as the head
- TextureClass *tex=Get_Texture();
- if (tex)
- {
- REF_PTR_RELEASE(tex);
- return SAME_AS_HEAD;
- }
- ShaderClass shader=Get_Shader();
-
- //Multiplicative RGB is white (A is don't care)
- //Additive RGB is Black (A is don't care)
- //Screen RGB is Black (A is don't care)
- //Alpha Same RGB as head but A is 0
- //Alpha test blend Same ARGB as head but A is 0
- //Alpha test Same ARGB as head
- //Opaque Same ARGB as head
- // Multiplicative
- if (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_SRC_COLOR) return WHITE;
- // Additive
- else if ((shader.Get_Src_Blend_Func()==ShaderClass::SRCBLEND_ONE) && (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_ONE)) return BLACK;
- // Screen
- else if ((shader.Get_Src_Blend_Func()==ShaderClass::SRCBLEND_ONE) && (shader.Get_Dst_Blend_Func()==ShaderClass::DSTBLEND_ONE_MINUS_SRC_COLOR)) return BLACK;
- // Alpha
- 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;
- // Alpha test
- else if (shader.Get_Alpha_Test()==ShaderClass::ALPHATEST_ENABLE) return SAME_AS_HEAD_ALPHA_ZERO;
- return SAME_AS_HEAD;
- }
- TextureClass * ParticleBufferClass::Get_Texture (void) const
- {
- if (PointGroup) return PointGroup->Get_Texture();
- else if (LineGroup) return LineGroup->Get_Texture();
- else if (LineRenderer) return LineRenderer->Get_Texture();
- return NULL;
- }
- void ParticleBufferClass::Set_Texture (TextureClass *tex)
- {
- if (PointGroup) PointGroup->Set_Texture(tex);
- else if (LineGroup) LineGroup->Set_Texture(tex);
- else if (LineRenderer) LineRenderer->Set_Texture(tex);
- }
- ShaderClass ParticleBufferClass::Get_Shader (void) const
- {
- if (PointGroup) return PointGroup->Get_Shader();
- else if (LineGroup) return LineGroup->Get_Shader();
- else if (LineRenderer) return LineRenderer->Get_Shader();
- WWASSERT(0);
- return ShaderClass::_PresetOpaqueShader;
- }
|