ParticleEmitterResource.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "anki/resource/ParticleEmitterResource.h"
  2. #include "anki/resource/Model.h"
  3. #include "anki/util/Exception.h"
  4. #include "anki/util/StringList.h"
  5. #include "anki/misc/Xml.h"
  6. #include <cstring>
  7. namespace anki {
  8. //==============================================================================
  9. // Misc =
  10. //==============================================================================
  11. //==============================================================================
  12. static const char* errMsg = "Incorrect or missing value ";
  13. #define PE_EXCEPTION(x) ANKI_EXCEPTION("Particle emmiter: " + x)
  14. //==============================================================================
  15. static void xmlReadVec3(const XmlElement& el_, const char* str, Vec3& out)
  16. {
  17. XmlElement el = el_.getChildElementOptional(str);
  18. if(!el)
  19. {
  20. return;
  21. }
  22. StringList list;
  23. list = StringList::splitString(el.getText(), ' ');
  24. if(list.size() != 3)
  25. {
  26. throw ANKI_EXCEPTION("Expecting 3 floats for Vec3");
  27. }
  28. for(U i = 0; i < 3; i++)
  29. {
  30. out[i] = std::stof(list[i]);
  31. }
  32. }
  33. //==============================================================================
  34. static void xmlReadFloat(const XmlElement& el_, const char* str, F32& out)
  35. {
  36. XmlElement el = el_.getChildElementOptional(str);
  37. if(!el)
  38. {
  39. return;
  40. }
  41. out = std::stof(el.getText());
  42. }
  43. //==============================================================================
  44. static void xmlReadU(const XmlElement& el_, const char* str, U32& out)
  45. {
  46. XmlElement el = el_.getChildElementOptional(str);
  47. if(!el)
  48. {
  49. return;
  50. }
  51. out = (U32)std::stoi(el.getText());
  52. }
  53. //==============================================================================
  54. // ParticleEmitterProperties =
  55. //==============================================================================
  56. //==============================================================================
  57. ParticleEmitterProperties& ParticleEmitterProperties::operator=(
  58. const ParticleEmitterProperties& b)
  59. {
  60. memcpy(this, &b, sizeof(ParticleEmitterProperties));
  61. return *this;
  62. }
  63. //==============================================================================
  64. void ParticleEmitterProperties::updateFlags()
  65. {
  66. forceEnabled = !isZero(particle.forceDirection.getLengthSquared());
  67. forceEnabled = forceEnabled
  68. || !isZero(particle.forceDirectionDeviation.getLengthSquared());
  69. forceEnabled = forceEnabled
  70. && (particle.forceMagnitude != 0.0
  71. || particle.forceMagnitudeDeviation != 0.0);
  72. wordGravityEnabled = isZero(particle.gravity.getLengthSquared());
  73. }
  74. //==============================================================================
  75. // ParticleEmitterResource =
  76. //==============================================================================
  77. //==============================================================================
  78. ParticleEmitterResource::ParticleEmitterResource()
  79. {}
  80. //==============================================================================
  81. ParticleEmitterResource::~ParticleEmitterResource()
  82. {}
  83. //==============================================================================
  84. void ParticleEmitterResource::load(const char* filename)
  85. {
  86. try
  87. {
  88. XmlDocument doc;
  89. doc.loadFile(filename);
  90. loadInternal(doc.getChildElement("particleEmitter"));
  91. }
  92. catch(std::exception& e)
  93. {
  94. throw ANKI_EXCEPTION("Failed to load file: " + filename) << e;
  95. }
  96. }
  97. //==============================================================================
  98. void ParticleEmitterResource::loadInternal(const XmlElement& rootel)
  99. {
  100. // XML load
  101. //
  102. xmlReadFloat(rootel, "life", particle.life);
  103. xmlReadFloat(rootel, "lifeDeviation", particle.lifeDeviation);
  104. xmlReadFloat(rootel, "mass", particle.mass);
  105. xmlReadFloat(rootel, "massDeviation", particle.massDeviation);
  106. xmlReadFloat(rootel, "size", particle.size);
  107. xmlReadFloat(rootel, "sizeDeviation", particle.sizeDeviation);
  108. xmlReadFloat(rootel, "sizeAnimation", particle.sizeAnimation);
  109. xmlReadFloat(rootel, "alpha", particle.alpha);
  110. xmlReadFloat(rootel, "alphaDeviation", particle.alphaDeviation);
  111. xmlReadVec3(rootel, "forceDirection", particle.forceDirection);
  112. xmlReadVec3(rootel, "forceDirectionDeviation",
  113. particle.forceDirectionDeviation);
  114. xmlReadFloat(rootel, "forceMagnitude", particle.forceMagnitude);
  115. xmlReadFloat(rootel, "forceMagnitudeDeviation",
  116. particle.forceMagnitudeDeviation);
  117. xmlReadVec3(rootel, "gravity", particle.gravity);
  118. xmlReadVec3(rootel, "gravityDeviation", particle.gravityDeviation);
  119. xmlReadVec3(rootel, "startingPosition", particle.startingPos);
  120. xmlReadVec3(rootel, "startingPositionDeviation",
  121. particle.startingPosDeviation);
  122. xmlReadU(rootel, "maxNumberOfParticles", maxNumOfParticles);
  123. xmlReadFloat(rootel, "emissionPeriod", emissionPeriod);
  124. xmlReadU(rootel, "particlesPerEmittion", particlesPerEmittion);
  125. U32 u = usePhysicsEngine;
  126. xmlReadU(rootel, "usePhysicsEngine", u);
  127. usePhysicsEngine = u;
  128. XmlElement el = rootel.getChildElement("model");
  129. model.load(el.getText());
  130. // sanity checks
  131. //
  132. if(particle.life <= 0.0)
  133. {
  134. throw PE_EXCEPTION(errMsg + "life");
  135. }
  136. if(particle.life - particle.lifeDeviation <= 0.0)
  137. {
  138. throw PE_EXCEPTION(errMsg + "lifeDeviation");
  139. }
  140. if(particle.size <= 0.0)
  141. {
  142. throw PE_EXCEPTION(errMsg + "size");
  143. }
  144. if(maxNumOfParticles < 1)
  145. {
  146. throw PE_EXCEPTION(errMsg + "maxNumOfParticles");
  147. }
  148. if(emissionPeriod <= 0.0)
  149. {
  150. throw PE_EXCEPTION(errMsg + "emissionPeriod");
  151. }
  152. if(particlesPerEmittion < 1)
  153. {
  154. throw PE_EXCEPTION(errMsg + "particlesPerEmission");
  155. }
  156. // Calc some stuff
  157. //
  158. updateFlags();
  159. }
  160. } // end namespace anki