part_ldr.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/part_ldr.cpp $*
  25. * *
  26. * Author:: Patrick Smith
  27. * *
  28. * $Modtime:: 8/01/01 3:35p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33. #include "part_ldr.h"
  34. #include "part_emt.h"
  35. #include "w3derr.h"
  36. #include "chunkio.h"
  37. #include "win.h" // for lstrcpy, can this be improved?
  38. #include "assetmgr.h"
  39. #include "texture.h"
  40. #ifndef SAFE_DELETE
  41. #define SAFE_DELETE(pointer) \
  42. { \
  43. if (pointer) { \
  44. delete pointer; \
  45. pointer = 0; \
  46. } \
  47. }
  48. #endif //SAFE_DELETE
  49. #ifndef SAFE_DELETE_ARRAY
  50. #define SAFE_DELETE_ARRAY(pointer) \
  51. if (pointer) { \
  52. delete [] pointer; \
  53. pointer = 0; \
  54. } \
  55. #endif //SAFE_DELETE
  56. ///////////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Global variable initialization
  59. //
  60. ParticleEmitterLoaderClass _ParticleEmitterLoader;
  61. // This array is declared in "W3D_File.H"
  62. const char *EMITTER_TYPE_NAMES[EMITTER_TYPEID_COUNT] =
  63. {
  64. "Default"
  65. };
  66. ///////////////////////////////////////////////////////////////////////////////////
  67. //
  68. // ParticleEmitterDefClass
  69. //
  70. ParticleEmitterDefClass::ParticleEmitterDefClass (void)
  71. : m_pName (NULL),
  72. m_Version (0L),
  73. m_pUserString (NULL),
  74. m_iUserType (EMITTER_TYPEID_DEFAULT),
  75. m_InitialOrientationRandom (0),
  76. m_pCreationVolume (NULL),
  77. m_pVelocityRandomizer (NULL)
  78. {
  79. ::memset (&m_Info, 0, sizeof (m_Info));
  80. ::memset (&m_InfoV2, 0, sizeof (m_InfoV2));
  81. ::memset (&m_ColorKeyframes, 0, sizeof (m_ColorKeyframes));
  82. ::memset (&m_OpacityKeyframes, 0, sizeof (m_OpacityKeyframes));
  83. ::memset (&m_SizeKeyframes, 0, sizeof (m_SizeKeyframes));
  84. ::memset (&m_RotationKeyframes, 0, sizeof (m_RotationKeyframes));
  85. ::memset (&m_FrameKeyframes, 0, sizeof (m_FrameKeyframes));
  86. ::memset (&m_LineProperties, 0, sizeof (m_LineProperties));
  87. return ;
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////////
  90. //
  91. // ParticleEmitterDefClass
  92. //
  93. ParticleEmitterDefClass::ParticleEmitterDefClass (const ParticleEmitterDefClass &src)
  94. : m_pName (NULL),
  95. m_Version (0L),
  96. m_pUserString (NULL),
  97. m_iUserType (EMITTER_TYPEID_DEFAULT),
  98. m_InitialOrientationRandom (src.m_InitialOrientationRandom),
  99. m_pCreationVolume (NULL),
  100. m_pVelocityRandomizer (NULL)
  101. {
  102. ::memset (&m_Info, 0, sizeof (m_Info));
  103. ::memset (&m_InfoV2, 0, sizeof (m_InfoV2));
  104. ::memset (&m_ColorKeyframes, 0, sizeof (m_ColorKeyframes));
  105. ::memset (&m_OpacityKeyframes, 0, sizeof (m_OpacityKeyframes));
  106. ::memset (&m_SizeKeyframes, 0, sizeof (m_SizeKeyframes));
  107. ::memset (&m_RotationKeyframes, 0, sizeof (m_RotationKeyframes));
  108. ::memset (&m_FrameKeyframes, 0, sizeof (m_FrameKeyframes));
  109. ::memset (&m_LineProperties, 0, sizeof (m_LineProperties));
  110. (*this) = src;
  111. return ;
  112. }
  113. ///////////////////////////////////////////////////////////////////////////////////
  114. //
  115. // ~ParticleEmitterDefClass
  116. //
  117. ParticleEmitterDefClass::~ParticleEmitterDefClass (void)
  118. {
  119. // Free the name buffer if necessary
  120. if (m_pName != NULL) {
  121. // free() is used because the buffer was allocated with ::_strdup().
  122. ::free (m_pName);
  123. m_pName = NULL;
  124. }
  125. // Free the user-string buffer if necessary
  126. if (m_pUserString != NULL) {
  127. // free() is used because the buffer was allocated with ::malloc() or ::_strdup().
  128. ::free (m_pUserString);
  129. m_pUserString = NULL;
  130. }
  131. Free_Props ();
  132. SAFE_DELETE (m_pCreationVolume);
  133. SAFE_DELETE (m_pVelocityRandomizer);
  134. return ;
  135. }
  136. ///////////////////////////////////////////////////////////////////////////////////
  137. //
  138. // ParticleEmitterDefClass
  139. //
  140. const ParticleEmitterDefClass &
  141. ParticleEmitterDefClass::operator= (const ParticleEmitterDefClass &src)
  142. {
  143. //
  144. // Copy the user structures
  145. //
  146. Set_Name (src.Get_Name ());
  147. Set_User_String (src.Get_User_String ());
  148. Set_User_Type (src.Get_User_Type ());
  149. m_Version = src.m_Version;
  150. //
  151. // Copy the information structures
  152. //
  153. ::memcpy (&m_Info, &src.m_Info, sizeof (m_Info));
  154. ::memcpy (&m_InfoV2, &src.m_InfoV2, sizeof (m_InfoV2));
  155. ::memcpy (&m_LineProperties, &src.m_LineProperties, sizeof(m_LineProperties));
  156. //
  157. // Copy the keyframes
  158. //
  159. Free_Props ();
  160. ::Copy_Emitter_Property_Struct (m_ColorKeyframes, src.m_ColorKeyframes);
  161. ::Copy_Emitter_Property_Struct (m_OpacityKeyframes, src.m_OpacityKeyframes);
  162. ::Copy_Emitter_Property_Struct (m_SizeKeyframes, src.m_SizeKeyframes);
  163. ::Copy_Emitter_Property_Struct (m_RotationKeyframes, src.m_RotationKeyframes);
  164. ::Copy_Emitter_Property_Struct (m_FrameKeyframes, src.m_FrameKeyframes);
  165. m_InitialOrientationRandom = src.m_InitialOrientationRandom;
  166. //
  167. // Create the randomizers
  168. //
  169. SAFE_DELETE (m_pCreationVolume);
  170. SAFE_DELETE (m_pVelocityRandomizer);
  171. m_pCreationVolume = Create_Randomizer (m_InfoV2.CreationVolume);
  172. m_pVelocityRandomizer = Create_Randomizer (m_InfoV2.VelRandom);
  173. return (*this);
  174. }
  175. ///////////////////////////////////////////////////////////////////////////////////
  176. //
  177. // Free_Props
  178. //
  179. void
  180. ParticleEmitterDefClass::Free_Props (void)
  181. {
  182. m_ColorKeyframes.NumKeyFrames = 0;
  183. m_OpacityKeyframes.NumKeyFrames = 0;
  184. m_SizeKeyframes.NumKeyFrames = 0;
  185. m_RotationKeyframes.NumKeyFrames = 0;
  186. m_FrameKeyframes.NumKeyFrames = 0;
  187. SAFE_DELETE_ARRAY (m_ColorKeyframes.KeyTimes);
  188. SAFE_DELETE_ARRAY (m_ColorKeyframes.Values);
  189. SAFE_DELETE_ARRAY (m_OpacityKeyframes.KeyTimes);
  190. SAFE_DELETE_ARRAY (m_OpacityKeyframes.Values);
  191. SAFE_DELETE_ARRAY (m_SizeKeyframes.KeyTimes);
  192. SAFE_DELETE_ARRAY (m_SizeKeyframes.Values);
  193. SAFE_DELETE_ARRAY (m_RotationKeyframes.KeyTimes);
  194. SAFE_DELETE_ARRAY (m_RotationKeyframes.Values);
  195. SAFE_DELETE_ARRAY (m_FrameKeyframes.KeyTimes);
  196. SAFE_DELETE_ARRAY (m_FrameKeyframes.Values);
  197. return ;
  198. }
  199. ///////////////////////////////////////////////////////////////////////////////////
  200. //
  201. // Set_Velocity_Random
  202. //
  203. void
  204. ParticleEmitterDefClass::Set_Velocity_Random (Vector3Randomizer *randomizer)
  205. {
  206. SAFE_DELETE (m_pVelocityRandomizer);
  207. m_pVelocityRandomizer = randomizer;
  208. //
  209. // Ensure our persistent structure is up-to-date so it will save correctly
  210. //
  211. if (m_pVelocityRandomizer != NULL) {
  212. Initialize_Randomizer_Struct (*m_pVelocityRandomizer, m_InfoV2.VelRandom);
  213. }
  214. return ;
  215. }
  216. ///////////////////////////////////////////////////////////////////////////////////
  217. //
  218. // Set_Creation_Volume
  219. //
  220. void
  221. ParticleEmitterDefClass::Set_Creation_Volume (Vector3Randomizer *randomizer)
  222. {
  223. SAFE_DELETE (m_pCreationVolume);
  224. m_pCreationVolume = randomizer;
  225. //
  226. // Ensure our persistent structure is up-to-date so it will save correctly
  227. //
  228. if (m_pCreationVolume != NULL) {
  229. Initialize_Randomizer_Struct (*m_pCreationVolume, m_InfoV2.CreationVolume);
  230. }
  231. return ;
  232. }
  233. ///////////////////////////////////////////////////////////////////////////////////
  234. //
  235. // Set_User_String
  236. //
  237. void
  238. ParticleEmitterDefClass::Set_User_String (const char *pstring)
  239. {
  240. SAFE_FREE (m_pUserString);
  241. m_pUserString = ::_strdup (pstring);
  242. return ;
  243. }
  244. ///////////////////////////////////////////////////////////////////////////////////
  245. //
  246. // Set_Name
  247. //
  248. void
  249. ParticleEmitterDefClass::Set_Name (const char *pname)
  250. {
  251. SAFE_FREE (m_pName);
  252. m_pName = ::_strdup (pname);
  253. return ;
  254. }
  255. ///////////////////////////////////////////////////////////////////////////////////
  256. //
  257. // Set_Texture_Filename
  258. //
  259. void
  260. ParticleEmitterDefClass::Set_Texture_Filename (const char *pname)
  261. {
  262. ::lstrcpy (m_Info.TextureFilename, pname);
  263. Normalize_Filename ();
  264. return ;
  265. }
  266. ///////////////////////////////////////////////////////////////////////////////////
  267. //
  268. // Normalize_Filename
  269. //
  270. void
  271. ParticleEmitterDefClass::Normalize_Filename (void)
  272. {
  273. TCHAR path[MAX_PATH];
  274. ::lstrcpy (path, m_Info.TextureFilename);
  275. // Find the last occurance of the directory deliminator
  276. LPCTSTR filename = ::strrchr (path, '\\');
  277. if (filename != NULL) {
  278. // Increment past the directory deliminator
  279. filename ++;
  280. // Now copy the filename protion of the path to the structure
  281. ::lstrcpy (m_Info.TextureFilename, filename);
  282. }
  283. return ;
  284. }
  285. ///////////////////////////////////////////////////////////////////////////////////
  286. //
  287. // Load
  288. //
  289. WW3DErrorType
  290. ParticleEmitterDefClass::Load_W3D (ChunkLoadClass &chunk_load)
  291. {
  292. // Assume error
  293. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  294. Initialize_To_Ver2 ();
  295. // Attempt to read the different sections of the emitter definition
  296. if ((Read_Header (chunk_load) == WW3D_ERROR_OK) &&
  297. (Read_User_Data (chunk_load) == WW3D_ERROR_OK) &&
  298. (Read_Info (chunk_load) == WW3D_ERROR_OK)) {
  299. if (m_Version > 0x00010000) {
  300. //
  301. // Read the version 2.0 structures from the chunk
  302. //
  303. if ((Read_InfoV2 (chunk_load) == WW3D_ERROR_OK) &&
  304. (Read_Props (chunk_load) == WW3D_ERROR_OK)) {
  305. // Success!
  306. ret_val = WW3D_ERROR_OK;
  307. }
  308. } else {
  309. // Make sure the data fits version 2
  310. Convert_To_Ver2 ();
  311. ret_val = WW3D_ERROR_OK;
  312. }
  313. }
  314. // (gth) Handle all future additions to the particle emitter file format
  315. // in the typical chunk fashion.
  316. while (chunk_load.Open_Chunk() && ret_val == WW3D_ERROR_OK) {
  317. switch (chunk_load.Cur_Chunk_ID())
  318. {
  319. case W3D_CHUNK_EMITTER_LINE_PROPERTIES:
  320. ret_val = Read_Line_Properties(chunk_load);
  321. break;
  322. case W3D_CHUNK_EMITTER_ROTATION_KEYFRAMES:
  323. ret_val = Read_Rotation_Keyframes(chunk_load);
  324. break;
  325. case W3D_CHUNK_EMITTER_FRAME_KEYFRAMES:
  326. ret_val = Read_Frame_Keyframes(chunk_load);
  327. break;
  328. default:
  329. WWDEBUG_SAY(("Unhandled Chunk! File: %s Line: %d\r\n",__FILE__,__LINE__));
  330. break;
  331. }
  332. chunk_load.Close_Chunk();
  333. }
  334. // Return the WW3DErrorType return code
  335. return ret_val;
  336. }
  337. ///////////////////////////////////////////////////////////////////////////////////
  338. //
  339. // Initialize_To_Ver2
  340. //
  341. void
  342. ParticleEmitterDefClass::Initialize_To_Ver2 (void)
  343. {
  344. ::memset (&m_Info, 0, sizeof (m_Info));
  345. ::memset (&m_InfoV2, 0, sizeof (m_InfoV2));
  346. //
  347. // Set the version 2 values using defaults from version 1
  348. //
  349. m_InfoV2.BurstSize = 1;
  350. m_InfoV2.OutwardVel = 0;
  351. m_InfoV2.VelInherit = 0;
  352. W3dUtilityClass::Convert_Shader (ShaderClass::_PresetAdditiveSpriteShader, &m_InfoV2.Shader);
  353. m_InfoV2.CreationVolume.ClassID = Vector3Randomizer::CLASSID_SOLIDBOX;
  354. m_InfoV2.CreationVolume.Value1 = 0;
  355. m_InfoV2.CreationVolume.Value2 = 0;
  356. m_InfoV2.CreationVolume.Value3 = 0;
  357. m_InfoV2.VelRandom.ClassID = Vector3Randomizer::CLASSID_SOLIDBOX;
  358. m_InfoV2.VelRandom.Value1 = 0;
  359. m_InfoV2.VelRandom.Value2 = 0;
  360. m_InfoV2.VelRandom.Value3 = 0;
  361. Free_Props ();
  362. return ;
  363. }
  364. ///////////////////////////////////////////////////////////////////////////////////
  365. //
  366. // Convert_To_Ver2
  367. //
  368. void
  369. ParticleEmitterDefClass::Convert_To_Ver2 (void)
  370. {
  371. if (m_Version < 0x00020000) {
  372. m_InfoV2.BurstSize = 1;
  373. m_InfoV2.OutwardVel = 0;
  374. m_InfoV2.VelInherit = 0;
  375. //
  376. // Determine which shader to use...
  377. //
  378. ShaderClass shader = ShaderClass::_PresetAdditiveSpriteShader;
  379. TextureClass *ptexture = WW3DAssetManager::Get_Instance ()->Get_Texture (m_Info.TextureFilename);
  380. if (ptexture != NULL) {
  381. // If texture has an alpha channel do alpha blending instead of additive
  382. // (which is the default for point groups):
  383. SurfaceClass::SurfaceDescription surf_desc;
  384. ::ZeroMemory(&surf_desc, sizeof(SurfaceClass::SurfaceDescription));
  385. ptexture->Get_Level_Description(surf_desc);
  386. if (Has_Alpha(surf_desc.Format)) {
  387. shader = ShaderClass::_PresetAlphaSpriteShader;
  388. }
  389. ptexture->Release_Ref();
  390. }
  391. W3dUtilityClass::Convert_Shader (shader, &m_InfoV2.Shader);
  392. //
  393. // Convert the randomziers
  394. //
  395. m_InfoV2.CreationVolume.ClassID = Vector3Randomizer::CLASSID_SOLIDBOX;
  396. m_InfoV2.CreationVolume.Value1 = m_Info.PositionRandom / 1000.0f;
  397. m_InfoV2.CreationVolume.Value2 = m_Info.PositionRandom / 1000.0f;
  398. m_InfoV2.CreationVolume.Value3 = m_Info.PositionRandom / 1000.0f;
  399. m_InfoV2.VelRandom.ClassID = Vector3Randomizer::CLASSID_SOLIDBOX;
  400. m_InfoV2.VelRandom.Value1 = m_Info.VelocityRandom;
  401. m_InfoV2.VelRandom.Value2 = m_Info.VelocityRandom;
  402. m_InfoV2.VelRandom.Value3 = m_Info.VelocityRandom;
  403. //
  404. // Recreate the randomizers
  405. //
  406. SAFE_DELETE (m_pCreationVolume);
  407. SAFE_DELETE (m_pVelocityRandomizer);
  408. m_pCreationVolume = Create_Randomizer (m_InfoV2.CreationVolume);
  409. m_pVelocityRandomizer = Create_Randomizer (m_InfoV2.VelRandom);
  410. //
  411. // Convert the colors, opacities, and sizes
  412. //
  413. Free_Props ();
  414. m_ColorKeyframes.Start = RGBA_TO_VECTOR3 (m_Info.StartColor);
  415. m_ColorKeyframes.Rand = Vector3 (0, 0, 0);
  416. m_ColorKeyframes.NumKeyFrames = 1;
  417. m_ColorKeyframes.KeyTimes = W3DNEW float(m_Info.FadeTime);
  418. m_ColorKeyframes.Values = W3DNEW Vector3(RGBA_TO_VECTOR3 (m_Info.EndColor));
  419. m_OpacityKeyframes.Start = ((float)(m_Info.StartColor.A)) / 255;
  420. m_OpacityKeyframes.Rand = 0;
  421. m_OpacityKeyframes.NumKeyFrames = 1;
  422. m_OpacityKeyframes.KeyTimes = W3DNEW float(m_Info.FadeTime);
  423. m_OpacityKeyframes.Values = W3DNEW float(((float)(m_Info.EndColor.A)) / 255);
  424. m_SizeKeyframes.Start = m_Info.StartSize;
  425. m_SizeKeyframes.Rand = 0;
  426. m_SizeKeyframes.NumKeyFrames = 0;
  427. }
  428. return ;
  429. }
  430. ///////////////////////////////////////////////////////////////////////////////////
  431. //
  432. // Read_Header
  433. //
  434. WW3DErrorType
  435. ParticleEmitterDefClass::Read_Header (ChunkLoadClass &chunk_load)
  436. {
  437. // Assume error
  438. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  439. // Is this the header chunk?
  440. if (chunk_load.Open_Chunk () &&
  441. (chunk_load.Cur_Chunk_ID () == W3D_CHUNK_EMITTER_HEADER)) {
  442. W3dEmitterHeaderStruct header = { 0 };
  443. if (chunk_load.Read (&header, sizeof (header)) == sizeof (header)) {
  444. // Copy the name from the header structure
  445. m_pName = ::_strdup (header.Name);
  446. m_Version = header.Version;
  447. // Success!
  448. ret_val = WW3D_ERROR_OK;
  449. }
  450. // Close the chunk, so the next read will be successful.
  451. chunk_load.Close_Chunk ();
  452. }
  453. // Return the WW3DErrorType return code
  454. return ret_val;
  455. }
  456. ///////////////////////////////////////////////////////////////////////////////////
  457. //
  458. // Read_User_Data
  459. //
  460. WW3DErrorType
  461. ParticleEmitterDefClass::Read_User_Data (ChunkLoadClass &chunk_load)
  462. {
  463. // Assume error
  464. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  465. // Is this the user chunk?
  466. if (chunk_load.Open_Chunk () &&
  467. (chunk_load.Cur_Chunk_ID () == W3D_CHUNK_EMITTER_USER_DATA)) {
  468. W3dEmitterUserInfoStruct user_info = { 0 };
  469. if (chunk_load.Read (&user_info, sizeof (user_info)) == sizeof (user_info)) {
  470. // Assume success from here on out
  471. ret_val = WW3D_ERROR_OK;
  472. // Record the user information into our member data
  473. m_iUserType = user_info.Type;
  474. // Should we read the user string from the file?
  475. if (user_info.SizeofStringParam > 0) {
  476. m_pUserString = (char *)::malloc (sizeof (char) * (user_info.SizeofStringParam+1));
  477. m_pUserString[0] = 0;
  478. // Attempt to read the user-string from the chunk
  479. if (chunk_load.Read (m_pUserString, user_info.SizeofStringParam) != user_info.SizeofStringParam) {
  480. ret_val = WW3D_ERROR_LOAD_FAILED;
  481. }
  482. }
  483. }
  484. // Close the chunk, so the next read will be successful.
  485. chunk_load.Close_Chunk ();
  486. }
  487. // Return the WW3DErrorType return code
  488. return ret_val;
  489. }
  490. ///////////////////////////////////////////////////////////////////////////////////
  491. //
  492. // Read_Info
  493. //
  494. WW3DErrorType
  495. ParticleEmitterDefClass::Read_Info (ChunkLoadClass &chunk_load)
  496. {
  497. // Assume error
  498. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  499. // Is this the user chunk?
  500. if (chunk_load.Open_Chunk () &&
  501. (chunk_load.Cur_Chunk_ID () == W3D_CHUNK_EMITTER_INFO)) {
  502. // Read the chunk straight into our member structure
  503. ::memset (&m_Info, 0, sizeof (m_Info));
  504. if (chunk_load.Read (&m_Info, sizeof (m_Info)) == sizeof (m_Info)) {
  505. // Success!
  506. ret_val = WW3D_ERROR_OK;
  507. }
  508. // Close the chunk, so the next read will be successful.
  509. chunk_load.Close_Chunk ();
  510. }
  511. // Return the WW3DErrorType return code
  512. return ret_val;
  513. }
  514. ///////////////////////////////////////////////////////////////////////////////////
  515. //
  516. // Create_Randomizer
  517. //
  518. Vector3Randomizer *
  519. ParticleEmitterDefClass::Create_Randomizer (W3dVolumeRandomizerStruct &info)
  520. {
  521. Vector3Randomizer *randomizer = NULL;
  522. switch (info.ClassID)
  523. {
  524. case Vector3Randomizer::CLASSID_SOLIDBOX:
  525. randomizer = W3DNEW Vector3SolidBoxRandomizer (Vector3 (info.Value1, info.Value2, info.Value3));
  526. break;
  527. case Vector3Randomizer::CLASSID_SOLIDSPHERE:
  528. randomizer = W3DNEW Vector3SolidSphereRandomizer (info.Value1);
  529. break;
  530. case Vector3Randomizer::CLASSID_HOLLOWSPHERE:
  531. randomizer = W3DNEW Vector3HollowSphereRandomizer (info.Value1);
  532. break;
  533. case Vector3Randomizer::CLASSID_SOLIDCYLINDER:
  534. randomizer = W3DNEW Vector3SolidCylinderRandomizer (info.Value1, info.Value2);
  535. break;
  536. }
  537. return randomizer;
  538. }
  539. ///////////////////////////////////////////////////////////////////////////////////
  540. //
  541. // Initialize_Randomizer_Struct
  542. //
  543. void
  544. ParticleEmitterDefClass::Initialize_Randomizer_Struct
  545. (
  546. const Vector3Randomizer & randomizer,
  547. W3dVolumeRandomizerStruct & info
  548. )
  549. {
  550. info.ClassID = randomizer.Class_ID ();
  551. switch (randomizer.Class_ID ())
  552. {
  553. case Vector3Randomizer::CLASSID_SOLIDBOX:
  554. {
  555. Vector3 extents = ((Vector3SolidBoxRandomizer &)randomizer).Get_Extents ();
  556. info.Value1 = extents.X;
  557. info.Value2 = extents.Y;
  558. info.Value3 = extents.Z;
  559. }
  560. break;
  561. case Vector3Randomizer::CLASSID_SOLIDSPHERE:
  562. info.Value1 = ((Vector3SolidSphereRandomizer &)randomizer).Get_Radius ();
  563. break;
  564. case Vector3Randomizer::CLASSID_HOLLOWSPHERE:
  565. info.Value1 = ((Vector3HollowSphereRandomizer &)randomizer).Get_Radius ();
  566. break;
  567. case Vector3Randomizer::CLASSID_SOLIDCYLINDER:
  568. info.Value1 = ((Vector3SolidCylinderRandomizer &)randomizer).Get_Height ();
  569. info.Value2 = ((Vector3SolidCylinderRandomizer &)randomizer).Get_Radius ();
  570. break;
  571. }
  572. return ;
  573. }
  574. ///////////////////////////////////////////////////////////////////////////////////
  575. //
  576. // Read_InfoV2
  577. //
  578. WW3DErrorType
  579. ParticleEmitterDefClass::Read_InfoV2 (ChunkLoadClass &chunk_load)
  580. {
  581. // Assume error
  582. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  583. // Is this the user chunk?
  584. if (chunk_load.Open_Chunk () &&
  585. (chunk_load.Cur_Chunk_ID () == W3D_CHUNK_EMITTER_INFOV2)) {
  586. // Read the chunk straight into our member structure
  587. ::memset (&m_InfoV2, 0, sizeof (m_InfoV2));
  588. if (chunk_load.Read (&m_InfoV2, sizeof (m_InfoV2)) == sizeof (m_InfoV2)) {
  589. //
  590. // Recreate the randomizers
  591. //
  592. SAFE_DELETE (m_pCreationVolume);
  593. SAFE_DELETE (m_pVelocityRandomizer);
  594. m_pCreationVolume = Create_Randomizer (m_InfoV2.CreationVolume);
  595. m_pVelocityRandomizer = Create_Randomizer (m_InfoV2.VelRandom);
  596. // Success!
  597. ret_val = WW3D_ERROR_OK;
  598. }
  599. // Close the chunk, so the next read will be successful.
  600. chunk_load.Close_Chunk ();
  601. }
  602. // Return the WW3DErrorClass::ErrorType return code
  603. return ret_val;
  604. }
  605. ///////////////////////////////////////////////////////////////////////////////////
  606. //
  607. // Read_Props
  608. //
  609. WW3DErrorType
  610. ParticleEmitterDefClass::Read_Props (ChunkLoadClass &chunk_load)
  611. {
  612. // Assume error
  613. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  614. Free_Props ();
  615. // Is this the user chunk?
  616. if (chunk_load.Open_Chunk () &&
  617. (chunk_load.Cur_Chunk_ID () == W3D_CHUNK_EMITTER_PROPS)) {
  618. W3dEmitterPropertyStruct info = { 0 };
  619. if (chunk_load.Read (&info, sizeof (info)) == sizeof (info)) {
  620. unsigned int index=0;
  621. //ParticlePropertyStruct<Vector3>
  622. m_ColorKeyframes.NumKeyFrames = info.ColorKeyframes - 1;
  623. m_OpacityKeyframes.NumKeyFrames = info.OpacityKeyframes - 1;
  624. m_SizeKeyframes.NumKeyFrames = info.SizeKeyframes - 1;
  625. m_ColorKeyframes.Rand = RGBA_TO_VECTOR3 (info.ColorRandom);
  626. m_OpacityKeyframes.Rand = info.OpacityRandom;
  627. m_SizeKeyframes.Rand = info.SizeRandom;
  628. //
  629. // Allocate the array of color keyframes
  630. //
  631. if (m_ColorKeyframes.NumKeyFrames > 0) {
  632. m_ColorKeyframes.KeyTimes = W3DNEWARRAY float[m_ColorKeyframes.NumKeyFrames];
  633. m_ColorKeyframes.Values = W3DNEWARRAY Vector3[m_ColorKeyframes.NumKeyFrames];
  634. }
  635. //
  636. // Allocate the array of opacity keyframes
  637. //
  638. if (m_OpacityKeyframes.NumKeyFrames > 0) {
  639. m_OpacityKeyframes.KeyTimes = W3DNEWARRAY float[m_OpacityKeyframes.NumKeyFrames];
  640. m_OpacityKeyframes.Values = W3DNEWARRAY float[m_OpacityKeyframes.NumKeyFrames];
  641. }
  642. //
  643. // Allocate the array of size keyframes
  644. //
  645. if (m_SizeKeyframes.NumKeyFrames > 0) {
  646. m_SizeKeyframes.KeyTimes = W3DNEWARRAY float[m_SizeKeyframes.NumKeyFrames];
  647. m_SizeKeyframes.Values = W3DNEWARRAY float[m_SizeKeyframes.NumKeyFrames];
  648. }
  649. //
  650. // Read the color keyframes from the chunk
  651. //
  652. Read_Color_Keyframe (chunk_load, NULL, &m_ColorKeyframes.Start);
  653. for (index = 0; index < m_ColorKeyframes.NumKeyFrames; index ++) {
  654. Read_Color_Keyframe (chunk_load,
  655. &m_ColorKeyframes.KeyTimes[index],
  656. &m_ColorKeyframes.Values[index]);
  657. }
  658. //
  659. // If the last keyframe is 'black' and we are using a color randomizer,
  660. // then make sure the last color is less then 0 so any randomized color
  661. // will end up as black
  662. //
  663. int last_keyframe = (m_ColorKeyframes.NumKeyFrames - 1);
  664. if ( last_keyframe > 0 &&
  665. m_ColorKeyframes.Values[last_keyframe].X == 0 &&
  666. m_ColorKeyframes.Values[last_keyframe].Y == 0 &&
  667. m_ColorKeyframes.Values[last_keyframe].Z == 0 &&
  668. (m_ColorKeyframes.Rand.X > 0 || m_ColorKeyframes.Rand.Y > 0 || m_ColorKeyframes.Rand.Z > 0))
  669. {
  670. m_ColorKeyframes.Values[last_keyframe].X = -m_ColorKeyframes.Rand.X;
  671. m_ColorKeyframes.Values[last_keyframe].Y = -m_ColorKeyframes.Rand.Y;
  672. m_ColorKeyframes.Values[last_keyframe].Z = -m_ColorKeyframes.Rand.Z;
  673. }
  674. //
  675. // Read the opacity keyframes from the chunk
  676. //
  677. Read_Opacity_Keyframe (chunk_load, NULL, &m_OpacityKeyframes.Start);
  678. for (index = 0; index < m_OpacityKeyframes.NumKeyFrames; index ++) {
  679. Read_Opacity_Keyframe (chunk_load,
  680. &m_OpacityKeyframes.KeyTimes[index],
  681. &m_OpacityKeyframes.Values[index]);
  682. }
  683. //
  684. // Read the size keyframes from the chunk
  685. //
  686. Read_Size_Keyframe (chunk_load, NULL, &m_SizeKeyframes.Start);
  687. for (index = 0; index < m_SizeKeyframes.NumKeyFrames; index ++) {
  688. Read_Size_Keyframe (chunk_load,
  689. &m_SizeKeyframes.KeyTimes[index],
  690. &m_SizeKeyframes.Values[index]);
  691. }
  692. // Success!
  693. ret_val = WW3D_ERROR_OK;
  694. }
  695. // Close the chunk, so the next read will be successful.
  696. chunk_load.Close_Chunk ();
  697. }
  698. // Return the WW3DErrorClass::ErrorType return code
  699. return ret_val;
  700. }
  701. //////////////////////////////////////////////////////////////////////////////////
  702. //
  703. // Read_Color_Keyframe
  704. //
  705. bool
  706. ParticleEmitterDefClass::Read_Color_Keyframe
  707. (
  708. ChunkLoadClass & chunk_load,
  709. float * key_time,
  710. Vector3 * value
  711. )
  712. {
  713. bool retval = false;
  714. //
  715. // Read the color key frame from the chunk
  716. //
  717. W3dEmitterColorKeyframeStruct key_frame = { 0 };
  718. if (chunk_load.Read (&key_frame, sizeof (key_frame)) == sizeof (key_frame)) {
  719. // Pass the key time to the caller
  720. if (key_time != NULL) {
  721. (*key_time) = key_frame.Time;
  722. }
  723. // Pass the oclor back to the caller
  724. if (value != NULL) {
  725. (*value) = RGBA_TO_VECTOR3 (key_frame.Color);
  726. }
  727. // Success!
  728. retval = true;
  729. }
  730. return retval;
  731. }
  732. //////////////////////////////////////////////////////////////////////////////////
  733. //
  734. // Read_Opacity_Keyframe
  735. //
  736. bool
  737. ParticleEmitterDefClass::Read_Opacity_Keyframe
  738. (
  739. ChunkLoadClass & chunk_load,
  740. float * key_time,
  741. float * value
  742. )
  743. {
  744. bool retval = false;
  745. //
  746. // Read the key frame from the chunk
  747. //
  748. W3dEmitterOpacityKeyframeStruct key_frame = { 0 };
  749. if (chunk_load.Read (&key_frame, sizeof (key_frame)) == sizeof (key_frame)) {
  750. // Pass the key time to the caller
  751. if (key_time != NULL) {
  752. (*key_time) = key_frame.Time;
  753. }
  754. // Pass the value back to the caller
  755. if (value != NULL) {
  756. (*value) = key_frame.Opacity;
  757. }
  758. // Success!
  759. retval = true;
  760. }
  761. return retval;
  762. }
  763. //////////////////////////////////////////////////////////////////////////////////
  764. //
  765. // Read_Size_Keyframe
  766. //
  767. bool
  768. ParticleEmitterDefClass::Read_Size_Keyframe
  769. (
  770. ChunkLoadClass & chunk_load,
  771. float * key_time,
  772. float * value
  773. )
  774. {
  775. bool retval = false;
  776. //
  777. // Read the key frame from the chunk
  778. //
  779. W3dEmitterSizeKeyframeStruct key_frame = { 0 };
  780. if (chunk_load.Read (&key_frame, sizeof (key_frame)) == sizeof (key_frame)) {
  781. // Pass the key time to the caller
  782. if (key_time != NULL) {
  783. (*key_time) = key_frame.Time;
  784. }
  785. // Pass the value back to the caller
  786. if (value != NULL) {
  787. (*value) = key_frame.Size;
  788. }
  789. // Success!
  790. retval = true;
  791. }
  792. return retval;
  793. }
  794. //////////////////////////////////////////////////////////////////////////////////
  795. //
  796. // Read_Line_Properties
  797. //
  798. WW3DErrorType
  799. ParticleEmitterDefClass::Read_Line_Properties(ChunkLoadClass & chunk_load)
  800. {
  801. // Assume error
  802. WW3DErrorType ret_val = WW3D_ERROR_LOAD_FAILED;
  803. // Is this the user chunk?
  804. if (chunk_load.Cur_Chunk_ID () == W3D_CHUNK_EMITTER_INFO) {
  805. // Read the chunk straight into our member structure
  806. if (chunk_load.Read (&m_LineProperties, sizeof (m_LineProperties)) == sizeof (m_LineProperties)) {
  807. // Success!
  808. ret_val = WW3D_ERROR_OK;
  809. }
  810. }
  811. // Return the WW3DErrorType return code
  812. return ret_val;
  813. }
  814. //////////////////////////////////////////////////////////////////////////////////
  815. //
  816. // Read_Rotation_Keyframes
  817. //
  818. WW3DErrorType
  819. ParticleEmitterDefClass::Read_Rotation_Keyframes (ChunkLoadClass &chunk_load)
  820. {
  821. // Assume success
  822. WW3DErrorType ret_val = WW3D_ERROR_OK;
  823. // Read the header
  824. W3dEmitterRotationHeaderStruct header;
  825. if (chunk_load.Read(&header,sizeof(header)) != sizeof(header)) {
  826. ret_val = WW3D_ERROR_LOAD_FAILED;
  827. }
  828. m_RotationKeyframes.NumKeyFrames = header.KeyframeCount;
  829. m_RotationKeyframes.Rand = header.Random;
  830. m_InitialOrientationRandom = header.OrientationRandom;
  831. // Read in the first key
  832. W3dEmitterRotationKeyframeStruct key;
  833. if (chunk_load.Read(&key,sizeof(key)) == sizeof(key)) {
  834. m_RotationKeyframes.Start = key.Rotation;
  835. }
  836. // Allocate the rotation keys
  837. if (m_RotationKeyframes.NumKeyFrames > 0) {
  838. m_RotationKeyframes.KeyTimes = W3DNEWARRAY float[m_RotationKeyframes.NumKeyFrames];
  839. m_RotationKeyframes.Values = W3DNEWARRAY float[m_RotationKeyframes.NumKeyFrames];
  840. }
  841. // Read in the keys
  842. for (unsigned int i=0; (i<header.KeyframeCount) && (ret_val == WW3D_ERROR_OK); i++) {
  843. W3dEmitterRotationKeyframeStruct key;
  844. if (chunk_load.Read(&key,sizeof(key)) == sizeof(key)) {
  845. m_RotationKeyframes.KeyTimes[i] = key.Time;
  846. m_RotationKeyframes.Values[i] = key.Rotation;
  847. } else {
  848. m_RotationKeyframes.KeyTimes[i] = 0.0f;
  849. m_RotationKeyframes.Values[i] = 0.0f;
  850. ret_val = WW3D_ERROR_LOAD_FAILED;
  851. }
  852. }
  853. return ret_val;
  854. }
  855. //////////////////////////////////////////////////////////////////////////////////
  856. //
  857. // Read_Frame_Keyframes
  858. //
  859. WW3DErrorType
  860. ParticleEmitterDefClass::Read_Frame_Keyframes (ChunkLoadClass &chunk_load)
  861. {
  862. // Assume success
  863. WW3DErrorType ret_val = WW3D_ERROR_OK;
  864. // Read the header
  865. W3dEmitterFrameHeaderStruct header;
  866. if (chunk_load.Read(&header,sizeof(header)) != sizeof(header)) {
  867. ret_val = WW3D_ERROR_LOAD_FAILED;
  868. }
  869. // Read in the first key
  870. W3dEmitterFrameKeyframeStruct key;
  871. if (chunk_load.Read(&key,sizeof(key)) == sizeof(key)) {
  872. m_FrameKeyframes.Start = key.Frame;
  873. }
  874. // Allocate the keys
  875. m_FrameKeyframes.NumKeyFrames = header.KeyframeCount;
  876. m_FrameKeyframes.Rand = header.Random;
  877. if (m_FrameKeyframes.NumKeyFrames > 0) {
  878. m_FrameKeyframes.KeyTimes = W3DNEWARRAY float[m_FrameKeyframes.NumKeyFrames];
  879. m_FrameKeyframes.Values = W3DNEWARRAY float[m_FrameKeyframes.NumKeyFrames];
  880. }
  881. // Read in the keys
  882. for (unsigned int i=0; (i<header.KeyframeCount) && (ret_val == WW3D_ERROR_OK); i++) {
  883. W3dEmitterFrameKeyframeStruct key;
  884. if (chunk_load.Read(&key,sizeof(key)) != sizeof(key)) {
  885. ret_val = WW3D_ERROR_LOAD_FAILED;
  886. }
  887. m_FrameKeyframes.KeyTimes[i] = key.Time;
  888. m_FrameKeyframes.Values[i] = key.Frame;
  889. }
  890. return ret_val;
  891. }
  892. //////////////////////////////////////////////////////////////////////////////////
  893. //
  894. // Save
  895. //
  896. WW3DErrorType
  897. ParticleEmitterDefClass::Save_W3D (ChunkSaveClass &chunk_save)
  898. {
  899. // Assume error
  900. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  901. // Begin a chunk that identifies an emitter
  902. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER) == TRUE) {
  903. // Attempt to save the different sections of the emitter definition
  904. if ((Save_Header (chunk_save) == WW3D_ERROR_OK) &&
  905. (Save_User_Data (chunk_save) == WW3D_ERROR_OK) &&
  906. (Save_Info (chunk_save) == WW3D_ERROR_OK) &&
  907. (Save_InfoV2 (chunk_save) == WW3D_ERROR_OK) &&
  908. (Save_Props (chunk_save) == WW3D_ERROR_OK) &&
  909. (Save_Rotation_Keyframes (chunk_save) == WW3D_ERROR_OK) &&
  910. (Save_Frame_Keyframes (chunk_save) == WW3D_ERROR_OK))
  911. {
  912. // Success!
  913. ret_val = WW3D_ERROR_OK;
  914. }
  915. // Close the emitter chunk
  916. chunk_save.End_Chunk ();
  917. }
  918. // Return the WW3DErrorType return code
  919. return ret_val;
  920. }
  921. ///////////////////////////////////////////////////////////////////////////////////
  922. //
  923. // Save_Header
  924. //
  925. WW3DErrorType
  926. ParticleEmitterDefClass::Save_Header (ChunkSaveClass &chunk_save)
  927. {
  928. // Assume error
  929. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  930. // Begin a chunk that identifies the emitter
  931. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_HEADER) == TRUE) {
  932. // Fill the header structure
  933. W3dEmitterHeaderStruct header = { 0 };
  934. header.Version = W3D_CURRENT_EMITTER_VERSION;
  935. ::lstrcpyn (header.Name, m_pName, sizeof (header.Name));
  936. header.Name[sizeof (header.Name) - 1] = 0;
  937. // Write the header out to the chunk
  938. if (chunk_save.Write (&header, sizeof (header)) == sizeof (header))
  939. {
  940. // Success!
  941. ret_val = WW3D_ERROR_OK;
  942. }
  943. // End the header chunk
  944. chunk_save.End_Chunk ();
  945. }
  946. // Return the WW3DErrorType return code
  947. return ret_val;
  948. }
  949. ///////////////////////////////////////////////////////////////////////////////////
  950. //
  951. // Save_User_Data
  952. //
  953. WW3DErrorType
  954. ParticleEmitterDefClass::Save_User_Data (ChunkSaveClass &chunk_save)
  955. {
  956. // Assume error
  957. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  958. // Begin a chunk that contains user information
  959. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_USER_DATA) == TRUE) {
  960. DWORD string_len = m_pUserString ? (::lstrlen (m_pUserString) + 1) : 0;
  961. // Fill the header structure
  962. W3dEmitterUserInfoStruct user_info = { 0 };
  963. user_info.Type = m_iUserType;
  964. user_info.SizeofStringParam = string_len;
  965. // Write the user information structure out to the chunk
  966. if (chunk_save.Write (&user_info, sizeof (user_info)) == sizeof (user_info))
  967. {
  968. // Assume success
  969. ret_val = WW3D_ERROR_OK;
  970. // Do we need to write the user string to the file?
  971. if (m_pUserString != NULL) {
  972. // Now write the user string param to the file
  973. if (chunk_save.Write (m_pUserString, string_len) != string_len) {
  974. // Something went wrong
  975. ret_val = WW3D_ERROR_SAVE_FAILED;
  976. }
  977. }
  978. }
  979. // End the user information chunk
  980. chunk_save.End_Chunk ();
  981. }
  982. // Return the WW3DErrorType return code
  983. return ret_val;
  984. }
  985. ///////////////////////////////////////////////////////////////////////////////////
  986. //
  987. // Save_Info
  988. //
  989. WW3DErrorType
  990. ParticleEmitterDefClass::Save_Info (ChunkSaveClass &chunk_save)
  991. {
  992. // Assume error
  993. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  994. // Begin a chunk that identifies the generic emitter settings
  995. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_INFO) == TRUE) {
  996. // Write the settings structure out to the chunk
  997. if (chunk_save.Write (&m_Info, sizeof (m_Info)) == sizeof (m_Info))
  998. {
  999. // Success!
  1000. ret_val = WW3D_ERROR_OK;
  1001. }
  1002. // End the settings chunk
  1003. chunk_save.End_Chunk ();
  1004. }
  1005. // Return the WW3DErrorType return code
  1006. return ret_val;
  1007. }
  1008. ///////////////////////////////////////////////////////////////////////////////////
  1009. //
  1010. // Save_InfoV2
  1011. //
  1012. WW3DErrorType
  1013. ParticleEmitterDefClass::Save_InfoV2 (ChunkSaveClass &chunk_save)
  1014. {
  1015. // Assume error
  1016. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1017. // Begin a chunk that identifies the generic emitter settings
  1018. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_INFOV2) == TRUE) {
  1019. // Write the settings structure out to the chunk
  1020. if (chunk_save.Write (&m_InfoV2, sizeof (m_InfoV2)) == sizeof (m_InfoV2))
  1021. {
  1022. // Success!
  1023. ret_val = WW3D_ERROR_OK;
  1024. }
  1025. // End the settings chunk
  1026. chunk_save.End_Chunk ();
  1027. }
  1028. // Return the WW3DErrorType return code
  1029. return ret_val;
  1030. }
  1031. ///////////////////////////////////////////////////////////////////////////////////
  1032. //
  1033. // Save_Props
  1034. //
  1035. WW3DErrorType
  1036. ParticleEmitterDefClass::Save_Props (ChunkSaveClass &chunk_save)
  1037. {
  1038. // Assume error
  1039. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1040. // Begin a chunk that identifies the generic emitter settings
  1041. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_PROPS) == TRUE) {
  1042. //
  1043. // Fill in the property struct
  1044. //
  1045. W3dEmitterPropertyStruct info = { 0 };
  1046. info.ColorKeyframes = m_ColorKeyframes.NumKeyFrames + 1;
  1047. info.OpacityKeyframes = m_OpacityKeyframes.NumKeyFrames + 1;
  1048. info.SizeKeyframes = m_SizeKeyframes.NumKeyFrames + 1;
  1049. info.OpacityRandom = m_OpacityKeyframes.Rand;
  1050. info.SizeRandom = m_SizeKeyframes.Rand;
  1051. VECTOR3_TO_RGBA (m_ColorKeyframes.Rand, info.ColorRandom);
  1052. //
  1053. // Write the property structure out to the chunk
  1054. //
  1055. if (chunk_save.Write (&info, sizeof (info)) == sizeof (info)) {
  1056. //
  1057. // Save the keyframes
  1058. //
  1059. if ((Save_Color_Keyframes (chunk_save) == WW3D_ERROR_OK) &&
  1060. (Save_Opacity_Keyframes (chunk_save) == WW3D_ERROR_OK) &&
  1061. (Save_Size_Keyframes (chunk_save) == WW3D_ERROR_OK)) {
  1062. // Success!
  1063. ret_val = WW3D_ERROR_OK;
  1064. }
  1065. }
  1066. // End the settings chunk
  1067. chunk_save.End_Chunk ();
  1068. }
  1069. // Return the WW3DErrorType return code
  1070. return ret_val;
  1071. }
  1072. ///////////////////////////////////////////////////////////////////////////////////
  1073. //
  1074. // Save_Color_Keyframes
  1075. //
  1076. WW3DErrorType
  1077. ParticleEmitterDefClass::Save_Color_Keyframes (ChunkSaveClass &chunk_save)
  1078. {
  1079. // Assume error
  1080. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1081. W3dEmitterColorKeyframeStruct info = { 0 };
  1082. info.Time = 0;
  1083. VECTOR3_TO_RGBA (m_ColorKeyframes.Start, info.Color);
  1084. //
  1085. // Write the starting color keyframe to the chunk
  1086. //
  1087. if (chunk_save.Write (&info, sizeof (info)) == sizeof (info)) {
  1088. //
  1089. // Write each of the remaining color keyframes to the chunk
  1090. //
  1091. int count = m_ColorKeyframes.NumKeyFrames;
  1092. bool success = true;
  1093. for (int index = 0; (index < count) && success; index ++) {
  1094. info.Time = m_ColorKeyframes.KeyTimes[index];
  1095. VECTOR3_TO_RGBA (m_ColorKeyframes.Values[index], info.Color);
  1096. success = (chunk_save.Write (&info, sizeof (info)) == sizeof (info));
  1097. }
  1098. ret_val = success ? WW3D_ERROR_OK : WW3D_ERROR_SAVE_FAILED;
  1099. }
  1100. // Return the WW3DErrorType return code
  1101. return ret_val;
  1102. }
  1103. ///////////////////////////////////////////////////////////////////////////////////
  1104. //
  1105. // Save_Opacity_Keyframes
  1106. //
  1107. WW3DErrorType
  1108. ParticleEmitterDefClass::Save_Opacity_Keyframes (ChunkSaveClass &chunk_save)
  1109. {
  1110. // Assume error
  1111. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1112. W3dEmitterOpacityKeyframeStruct info = { 0 };
  1113. info.Time = 0;
  1114. info.Opacity = m_OpacityKeyframes.Start;
  1115. //
  1116. // Write the starting keyframe to the chunk
  1117. //
  1118. if (chunk_save.Write (&info, sizeof (info)) == sizeof (info)) {
  1119. //
  1120. // Write each of the remaining keyframes to the chunk
  1121. //
  1122. int count = m_OpacityKeyframes.NumKeyFrames;
  1123. bool success = true;
  1124. for (int index = 0; (index < count) && success; index ++) {
  1125. info.Time = m_OpacityKeyframes.KeyTimes[index];
  1126. info.Opacity = m_OpacityKeyframes.Values[index];
  1127. success = (chunk_save.Write (&info, sizeof (info)) == sizeof (info));
  1128. }
  1129. ret_val = success ? WW3D_ERROR_OK : WW3D_ERROR_SAVE_FAILED;
  1130. }
  1131. // Return the WW3DErrorType return code
  1132. return ret_val;
  1133. }
  1134. ///////////////////////////////////////////////////////////////////////////////////
  1135. //
  1136. // Save_Size_Keyframes
  1137. //
  1138. WW3DErrorType
  1139. ParticleEmitterDefClass::Save_Size_Keyframes (ChunkSaveClass &chunk_save)
  1140. {
  1141. // Assume error
  1142. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1143. W3dEmitterSizeKeyframeStruct info = { 0 };
  1144. info.Time = 0;
  1145. info.Size = m_SizeKeyframes.Start;
  1146. //
  1147. // Write the starting keyframe to the chunk
  1148. //
  1149. if (chunk_save.Write (&info, sizeof (info)) == sizeof (info)) {
  1150. //
  1151. // Write each of the remaining keyframes to the chunk
  1152. //
  1153. int count = m_SizeKeyframes.NumKeyFrames;
  1154. bool success = true;
  1155. for (int index = 0; (index < count) && success; index ++) {
  1156. info.Time = m_SizeKeyframes.KeyTimes[index];
  1157. info.Size = m_SizeKeyframes.Values[index];
  1158. success = (chunk_save.Write (&info, sizeof (info)) == sizeof (info));
  1159. }
  1160. ret_val = success ? WW3D_ERROR_OK : WW3D_ERROR_SAVE_FAILED;
  1161. }
  1162. // Return the WW3DErrorType return code
  1163. return ret_val;
  1164. }
  1165. WW3DErrorType
  1166. ParticleEmitterDefClass::Save_Line_Properties (ChunkSaveClass &chunk_save)
  1167. {
  1168. // Assume error
  1169. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1170. // Begin a chunk that identifies the line properties
  1171. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_LINE_PROPERTIES) == TRUE) {
  1172. // Write the line properties structure out to the chunk
  1173. if (chunk_save.Write (&m_LineProperties, sizeof (m_LineProperties)) == sizeof (m_LineProperties))
  1174. {
  1175. // Success!
  1176. ret_val = WW3D_ERROR_OK;
  1177. }
  1178. // End the chunk
  1179. chunk_save.End_Chunk ();
  1180. }
  1181. // Return the WW3DErrorType return code
  1182. return ret_val;
  1183. }
  1184. ///////////////////////////////////////////////////////////////////////////////////
  1185. //
  1186. // Save_Rotation_Keyframes
  1187. // NOTE: Rotation keyframes are saved in a separate chunk unlike color,size,and
  1188. // opacity which are embedded inside the PROPS chunk.
  1189. //
  1190. WW3DErrorType
  1191. ParticleEmitterDefClass::Save_Rotation_Keyframes (ChunkSaveClass & chunk_save)
  1192. {
  1193. // Assume error
  1194. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1195. // Begin a chunk that identifies the rotation keyframes
  1196. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_ROTATION_KEYFRAMES) == TRUE) {
  1197. // Write the header
  1198. W3dEmitterRotationHeaderStruct header;
  1199. header.KeyframeCount = m_RotationKeyframes.NumKeyFrames;
  1200. header.Random = m_RotationKeyframes.Rand;
  1201. header.OrientationRandom = m_InitialOrientationRandom;
  1202. chunk_save.Write (&header, sizeof (W3dEmitterRotationHeaderStruct));
  1203. // Write the keyframes
  1204. bool success = true;
  1205. W3dEmitterRotationKeyframeStruct key;
  1206. // Write the start keyframe
  1207. key.Time = 0;
  1208. key.Rotation = m_RotationKeyframes.Start;
  1209. chunk_save.Write (&key, sizeof (key));
  1210. // Write the remaining keyframes
  1211. for (unsigned int index = 0; (index < header.KeyframeCount) && success; index ++) {
  1212. key.Time = m_RotationKeyframes.KeyTimes[index];
  1213. key.Rotation = m_RotationKeyframes.Values[index];
  1214. success = (chunk_save.Write (&key, sizeof (key)) == sizeof (key));
  1215. }
  1216. ret_val = success ? WW3D_ERROR_OK : WW3D_ERROR_SAVE_FAILED;
  1217. // End the chunk
  1218. chunk_save.End_Chunk ();
  1219. }
  1220. // Return the WW3DErrorType return code
  1221. return ret_val;
  1222. }
  1223. ///////////////////////////////////////////////////////////////////////////////////
  1224. //
  1225. // Save_Frame_Keyframes
  1226. // NOTE: Rotation keyframes are saved in a separate chunk unlike color,size,and
  1227. // opacity which are embedded inside the PROPS chunk.
  1228. //
  1229. WW3DErrorType
  1230. ParticleEmitterDefClass::Save_Frame_Keyframes (ChunkSaveClass & chunk_save)
  1231. {
  1232. // Assume error
  1233. WW3DErrorType ret_val = WW3D_ERROR_SAVE_FAILED;
  1234. // Begin a chunk that identifies the rotation keyframes
  1235. if (chunk_save.Begin_Chunk (W3D_CHUNK_EMITTER_FRAME_KEYFRAMES) == TRUE) {
  1236. // Write the header
  1237. W3dEmitterFrameHeaderStruct header;
  1238. header.KeyframeCount = m_FrameKeyframes.NumKeyFrames;
  1239. header.Random = m_FrameKeyframes.Rand;
  1240. chunk_save.Write (&header, sizeof (W3dEmitterFrameHeaderStruct));
  1241. // Write the keyframes
  1242. bool success = true;
  1243. W3dEmitterFrameKeyframeStruct key;
  1244. // Write the start keyframe
  1245. key.Time = 0;
  1246. key.Frame = m_FrameKeyframes.Start;
  1247. chunk_save.Write (&key, sizeof (key));
  1248. // Write the remaining keyframes
  1249. for (unsigned int index = 0; (index < header.KeyframeCount) && success; index ++) {
  1250. key.Time = m_FrameKeyframes.KeyTimes[index];
  1251. key.Frame = m_FrameKeyframes.Values[index];
  1252. success = (chunk_save.Write (&key, sizeof (key)) == sizeof (key));
  1253. }
  1254. ret_val = success ? WW3D_ERROR_OK : WW3D_ERROR_SAVE_FAILED;
  1255. // End the chunk
  1256. chunk_save.End_Chunk ();
  1257. }
  1258. // Return the WW3DErrorType return code
  1259. return ret_val;
  1260. }
  1261. ///////////////////////////////////////////////////////////////////////////////////
  1262. //
  1263. // Set_Color_Keyframes
  1264. //
  1265. void
  1266. ParticleEmitterDefClass::Set_Color_Keyframes (ParticlePropertyStruct<Vector3> &keyframes)
  1267. {
  1268. SAFE_DELETE_ARRAY (m_ColorKeyframes.KeyTimes);
  1269. SAFE_DELETE_ARRAY (m_ColorKeyframes.Values);
  1270. ::Copy_Emitter_Property_Struct (m_ColorKeyframes, keyframes);
  1271. return ;
  1272. }
  1273. ///////////////////////////////////////////////////////////////////////////////////
  1274. //
  1275. // Set_Opacity_Keyframes
  1276. //
  1277. void
  1278. ParticleEmitterDefClass::Set_Opacity_Keyframes (ParticlePropertyStruct<float> &keyframes)
  1279. {
  1280. SAFE_DELETE_ARRAY (m_OpacityKeyframes.KeyTimes);
  1281. SAFE_DELETE_ARRAY (m_OpacityKeyframes.Values);
  1282. ::Copy_Emitter_Property_Struct (m_OpacityKeyframes, keyframes);
  1283. return ;
  1284. }
  1285. ///////////////////////////////////////////////////////////////////////////////////
  1286. //
  1287. // Set_Size_Keyframes
  1288. //
  1289. void
  1290. ParticleEmitterDefClass::Set_Size_Keyframes (ParticlePropertyStruct<float> &keyframes)
  1291. {
  1292. SAFE_DELETE_ARRAY (m_SizeKeyframes.KeyTimes);
  1293. SAFE_DELETE_ARRAY (m_SizeKeyframes.Values);
  1294. ::Copy_Emitter_Property_Struct (m_SizeKeyframes, keyframes);
  1295. return ;
  1296. }
  1297. ///////////////////////////////////////////////////////////////////////////////////
  1298. //
  1299. // Set_Rotation_Keyframes
  1300. //
  1301. void
  1302. ParticleEmitterDefClass::Set_Rotation_Keyframes (ParticlePropertyStruct<float> &keyframes, float orient_rnd)
  1303. {
  1304. SAFE_DELETE_ARRAY (m_RotationKeyframes.KeyTimes);
  1305. SAFE_DELETE_ARRAY (m_RotationKeyframes.Values);
  1306. ::Copy_Emitter_Property_Struct (m_RotationKeyframes, keyframes);
  1307. m_InitialOrientationRandom = orient_rnd;
  1308. return ;
  1309. }
  1310. ///////////////////////////////////////////////////////////////////////////////////
  1311. //
  1312. // Set_Frame_Keyframes
  1313. //
  1314. void
  1315. ParticleEmitterDefClass::Set_Frame_Keyframes (ParticlePropertyStruct<float> &keyframes)
  1316. {
  1317. SAFE_DELETE_ARRAY (m_FrameKeyframes.KeyTimes);
  1318. SAFE_DELETE_ARRAY (m_FrameKeyframes.Values);
  1319. ::Copy_Emitter_Property_Struct (m_FrameKeyframes, keyframes);
  1320. return ;
  1321. }
  1322. ///////////////////////////////////////////////////////////////////////////////////
  1323. //
  1324. // Get_Color_Keyframes
  1325. //
  1326. void
  1327. ParticleEmitterDefClass::Get_Color_Keyframes (ParticlePropertyStruct<Vector3> &keyframes) const
  1328. {
  1329. ::Copy_Emitter_Property_Struct (keyframes, m_ColorKeyframes);
  1330. return ;
  1331. }
  1332. ///////////////////////////////////////////////////////////////////////////////////
  1333. //
  1334. // Get_Opacity_Keyframes
  1335. //
  1336. void
  1337. ParticleEmitterDefClass::Get_Opacity_Keyframes (ParticlePropertyStruct<float> &keyframes) const
  1338. {
  1339. ::Copy_Emitter_Property_Struct (keyframes, m_OpacityKeyframes);
  1340. return ;
  1341. }
  1342. ///////////////////////////////////////////////////////////////////////////////////
  1343. //
  1344. // Get_Size_Keyframes
  1345. //
  1346. void
  1347. ParticleEmitterDefClass::Get_Size_Keyframes (ParticlePropertyStruct<float> &keyframes) const
  1348. {
  1349. ::Copy_Emitter_Property_Struct (keyframes, m_SizeKeyframes);
  1350. return ;
  1351. }
  1352. ///////////////////////////////////////////////////////////////////////////////////
  1353. //
  1354. // Get_Rotation_Keyframes
  1355. //
  1356. void
  1357. ParticleEmitterDefClass::Get_Rotation_Keyframes (ParticlePropertyStruct<float> &keyframes) const
  1358. {
  1359. ::Copy_Emitter_Property_Struct (keyframes, m_RotationKeyframes);
  1360. return ;
  1361. }
  1362. ///////////////////////////////////////////////////////////////////////////////////
  1363. //
  1364. // Get_Frame_Keyframes
  1365. //
  1366. void
  1367. ParticleEmitterDefClass::Get_Frame_Keyframes (ParticlePropertyStruct<float> &keyframes) const
  1368. {
  1369. ::Copy_Emitter_Property_Struct (keyframes, m_FrameKeyframes);
  1370. return ;
  1371. }
  1372. ///////////////////////////////////////////////////////////////////////////////////
  1373. //
  1374. // Create
  1375. //
  1376. RenderObjClass *
  1377. ParticleEmitterPrototypeClass::Create (void)
  1378. {
  1379. return ParticleEmitterClass::Create_From_Definition (*m_pDefinition);
  1380. }
  1381. ///////////////////////////////////////////////////////////////////////////////////
  1382. //
  1383. // Load
  1384. //
  1385. PrototypeClass *
  1386. ParticleEmitterLoaderClass::Load_W3D (ChunkLoadClass &chunk_load)
  1387. {
  1388. // Assume failure
  1389. ParticleEmitterPrototypeClass *pprototype = NULL;
  1390. // Create a definition object
  1391. ParticleEmitterDefClass *pdefinition = W3DNEW ParticleEmitterDefClass;
  1392. if (pdefinition != NULL) {
  1393. // Ask the definition object to load the emitter data
  1394. if (pdefinition->Load_W3D (chunk_load) != WW3D_ERROR_OK) {
  1395. // Error! Free the definition
  1396. delete pdefinition;
  1397. pdefinition = NULL;
  1398. } else {
  1399. // Success! Create a prototype from the definition
  1400. pprototype = W3DNEW ParticleEmitterPrototypeClass (pdefinition);
  1401. }
  1402. }
  1403. // Return a pointer to the prototype
  1404. return pprototype;
  1405. }