CCPUVertexEmitter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCPUVertexEmitter.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. //// Constants
  25. //const unsigned short Particle3DVertexEmitter::DEFAULT_STEP = 1;
  26. //const unsigned short Particle3DVertexEmitter::DEFAULT_SEGMENTS = 1;
  27. //const unsigned short Particle3DVertexEmitter::DEFAULT_ITERATIONS = 1;
  28. //
  29. ////-----------------------------------------------------------------------
  30. //Particle3DVertexEmitter::Particle3DVertexEmitter(void) : ParticleEmitter(),
  31. // mPositionElement(0),
  32. // mVertexData(0),
  33. // mVertexBuffer(0),
  34. // mMesh(0),
  35. // mSubMesh(0),
  36. // mVertexSize(12),
  37. // mMaxVertexCount(0),
  38. // mVertexCount(0),
  39. // mStep(DEFAULT_STEP),
  40. // mSegments(DEFAULT_SEGMENTS),
  41. // mMeshProcessed(false),
  42. // mAllVerticesProcessed(false),
  43. // mSubMeshNumber(0),
  44. // mIterations(DEFAULT_ITERATIONS),
  45. // mMeshName(StringUtil::BLANK)
  46. //{
  47. // mBuffer.setNull();
  48. //}
  49. ////-----------------------------------------------------------------------
  50. //unsigned short Particle3DVertexEmitter::getIterations(void) const
  51. //{
  52. // return mIterations;
  53. //}
  54. ////-----------------------------------------------------------------------
  55. //void Particle3DVertexEmitter::setIterations(unsigned short iterations)
  56. //{
  57. // mIterations = iterations;
  58. //}
  59. ////-----------------------------------------------------------------------
  60. //unsigned short Particle3DVertexEmitter::getSegments(void) const
  61. //{
  62. // return mSegments;
  63. //}
  64. ////-----------------------------------------------------------------------
  65. //void Particle3DVertexEmitter::setSegments(unsigned short segments)
  66. //{
  67. // mSegments = segments;
  68. //}
  69. ////-----------------------------------------------------------------------
  70. //unsigned short Particle3DVertexEmitter::getStep(void) const
  71. //{
  72. // return mStep;
  73. //}
  74. ////-----------------------------------------------------------------------
  75. //void Particle3DVertexEmitter::setStep(unsigned short step)
  76. //{
  77. // mStep = step;
  78. //}
  79. ////-----------------------------------------------------------------------
  80. //const std::string& Particle3DVertexEmitter::getMeshName(void) const
  81. //{
  82. // return mMeshName;
  83. //}
  84. ////-----------------------------------------------------------------------
  85. //void Particle3DVertexEmitter::setMeshName(const std::string& meshName)
  86. //{
  87. // _setDefaults(); // Triggers loading the new mesh
  88. // mMeshName = meshName;
  89. //}
  90. ////-----------------------------------------------------------------------
  91. //void Particle3DVertexEmitter::_notifyStart (void)
  92. //{
  93. // ParticleEmitter::_notifyStart();
  94. // _setDefaults();
  95. //}
  96. ////-----------------------------------------------------------------------
  97. //unsigned short Particle3DVertexEmitter::_calculateRequestedParticles(float timeElapsed)
  98. //{
  99. // // Only return the requested number if the emitter can actually emit them
  100. // unsigned short requested = ParticleEmitter::_calculateRequestedParticles(timeElapsed);
  101. // if (requested <= mSpawnPositionList.size())
  102. // {
  103. // return requested;
  104. // }
  105. //
  106. // return 0;
  107. //}
  108. ////-----------------------------------------------------------------------
  109. //void Particle3DVertexEmitter::_initParticlePosition(Particle* particle)
  110. //{
  111. // // Get the first one from the list.
  112. // // The _calculateRequestedParticles() ensures that there are particles in the list.
  113. // // All positions in mSpawnPositionList are in localspace
  114. // ParticleSystem* sys = mParentTechnique->getParentSystem();
  115. // if (sys)
  116. // {
  117. // particle->position = getDerivedPosition() +
  118. // sys->getDerivedOrientation() *
  119. // (_mEmitterScale * *mSpawnPositionList.begin());
  120. // }
  121. // else
  122. // {
  123. // particle->position = getDerivedPosition() + _mEmitterScale * *mSpawnPositionList.begin();
  124. // }
  125. //
  126. // mSpawnPositionList.pop_front();
  127. // particle->originalPosition = particle->position;
  128. //}
  129. ////-----------------------------------------------------------------------
  130. //void Particle3DVertexEmitter::_preProcessParticles(ParticleTechnique* technique, float timeElapsed)
  131. //{
  132. // if (mAllVerticesProcessed)
  133. // {
  134. // return;
  135. // }
  136. //
  137. // // Generate the next few spawn points if not all vertices are processed.
  138. // for (unsigned short i = 0; i < mIterations; ++i)
  139. // {
  140. // _generatePoints();
  141. // if (mAllVerticesProcessed)
  142. // {
  143. // return;
  144. // }
  145. // }
  146. //
  147. // // Calculate the emitters position
  148. // getDerivedPosition();
  149. //}
  150. ////-----------------------------------------------------------------------
  151. //void Particle3DVertexEmitter::_setDefaults(void)
  152. //{
  153. // // Set the default values except the name of the mesh, which may only be changed by the
  154. // // setMeshName() function
  155. // mSpawnPositionList.clear();
  156. // mPositionElement = 0;
  157. // mVertexData = 0;
  158. // mVertexBuffer = 0;
  159. // mMesh = 0;
  160. // mSubMesh = 0;
  161. // mMaxVertexCount = 0;
  162. // mVertexCount = 0;
  163. // mMeshProcessed = false;
  164. // mAllVerticesProcessed = false;
  165. // mSubMeshNumber = 0;
  166. //
  167. // if (!mBuffer.isNull())
  168. // {
  169. // if (mBuffer->isLocked())
  170. // {
  171. // mBuffer->unlock();
  172. // }
  173. // mBuffer.setNull();
  174. // }
  175. //}
  176. ////-----------------------------------------------------------------------
  177. //void Particle3DVertexEmitter::_loadMesh (void)
  178. //{
  179. // Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().load(mMeshName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  180. // mMesh = mesh.getPointer();
  181. // mMeshProcessed = false;
  182. //
  183. // if (mMesh->getNumSubMeshes() > 0)
  184. // {
  185. // mSubMesh = mMesh->getSubMesh(0);
  186. // }
  187. // else
  188. // {
  189. // mSubMesh = 0;
  190. // }
  191. //}
  192. ////-----------------------------------------------------------------------
  193. //void Particle3DVertexEmitter::_generatePoints (void)
  194. //{
  195. // if (mAllVerticesProcessed)
  196. // {
  197. // return;
  198. // }
  199. //
  200. // if (!mMesh && mMeshName != StringUtil::BLANK)
  201. // {
  202. // _loadMesh();
  203. // }
  204. //
  205. // // At least process the vertices of the mesh itself and then all its submeshes
  206. // if (!mMeshProcessed)
  207. // {
  208. // _generatePointsFromMesh();
  209. // }
  210. // else
  211. // {
  212. // if (mSubMesh && !mSubMesh->useSharedVertices)
  213. // {
  214. // _generatePointsFromSubMesh();
  215. // }
  216. // }
  217. //
  218. // if (mVertexCount > 0 && mVertexCount >= mMaxVertexCount)
  219. // {
  220. // // The vertices of 1 mesh/subMesh are processed. Determine whether more submeshes need to
  221. // // be processed.
  222. // mBuffer->unlock();
  223. // mBuffer.setNull();
  224. // mVertexCount = 0;
  225. // mMaxVertexCount = 0;
  226. // mVertexData = 0;
  227. // mVertexBuffer = 0;
  228. // mSubMeshNumber++;
  229. // if (mSubMeshNumber >= mMesh->getNumSubMeshes())
  230. // {
  231. // mAllVerticesProcessed = true;
  232. // }
  233. // else
  234. // {
  235. // mSubMesh = mMesh->getSubMesh(mSubMeshNumber);
  236. // }
  237. // }
  238. //}
  239. ////-----------------------------------------------------------------------
  240. //void Particle3DVertexEmitter::_generatePointsFromMesh (void)
  241. //{
  242. // if (mMaxVertexCount == 0 && mMesh)
  243. // {
  244. // mVertexData = mMesh->sharedVertexData;
  245. // if (!mVertexData)
  246. // {
  247. // mMeshProcessed = true;
  248. // return;
  249. // }
  250. // _fillVertexBuffer();
  251. // }
  252. //
  253. // _generatePointsFromVertexBuffer();
  254. // if (mVertexCount >= mMaxVertexCount)
  255. // {
  256. // mMeshProcessed = true;
  257. // }
  258. //}
  259. ////-----------------------------------------------------------------------
  260. //void Particle3DVertexEmitter::_generatePointsFromSubMesh (void)
  261. //{
  262. // if (mMaxVertexCount == 0 && mSubMesh)
  263. // {
  264. // mVertexData = mSubMesh->vertexData;
  265. // if (!mVertexData)
  266. // {
  267. // return;
  268. // }
  269. // _fillVertexBuffer();
  270. // }
  271. //
  272. // _generatePointsFromVertexBuffer();
  273. //}
  274. ////-----------------------------------------------------------------------
  275. //void Particle3DVertexEmitter::_fillVertexBuffer(void)
  276. //{
  277. // mVertexCount = 0;
  278. // mMaxVertexCount = mVertexData->vertexCount - mStep;
  279. // mPositionElement = mVertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
  280. // mBuffer = mVertexData->vertexBufferBinding->getBuffer(mPositionElement->getSource());
  281. // mVertexSize = mBuffer->getVertexSize();
  282. // mVertexBuffer = static_cast<unsigned char*>(mBuffer->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
  283. //}
  284. ////-----------------------------------------------------------------------
  285. //void Particle3DVertexEmitter::_generatePointsFromVertexBuffer(void)
  286. //{
  287. // if (mMaxVertexCount > 1)
  288. // {
  289. // float* pReal;
  290. // mPositionElement->baseVertexPointerToElement(mVertexBuffer, &pReal);
  291. // Vec3 v1(pReal[0], pReal[1], pReal[2]);
  292. // mVertexBuffer += mStep * mVertexSize; // No validation; assume that it doesn't exceed.
  293. // mPositionElement->baseVertexPointerToElement(mVertexBuffer, &pReal);
  294. // Vec3 v2(pReal[0], pReal[1], pReal[2]);
  295. // _generatePoints (v1, v2);
  296. // mVertexCount += mStep;
  297. // }
  298. //}
  299. ////-----------------------------------------------------------------------
  300. //void Particle3DVertexEmitter::_generatePoints (const Vec3& startVector,
  301. // const Vec3& endVector,
  302. // float lengthIncrement)
  303. //{
  304. // /* Two possible ways of generating spawn points.
  305. // 1. Generate (mSegments + 2) Vectors (including start and end vector), equally distributed.
  306. // 2. Generate number of Vectors depending on the length between startVector and endVector. The
  307. // longer the length, the more vectors are generated.
  308. //
  309. // The distribution of the spawn points in the list is as followed.
  310. // first iteration p1, 3 points:
  311. // - p1.3, p1.2, p1.1
  312. // second iteration, p2, 4 points:
  313. // - p2.4, p1.3, p2.3, p1.2, p2.2, p1.1, p2.1
  314. // second iteration, p3, 3 points:
  315. // - p3.3, p2.4, p3.2, p1.3, p3.1, p2.3, p1.2, p2.2, p1.1, p2.1
  316. // @remarks
  317. // Use objectspace for the vectors and only transform as soon as a particle is emitted.
  318. // */
  319. // list<Vec3>::iterator iterator = mSpawnPositionList.end();
  320. // Vec3 start = startVector;
  321. // if (lengthIncrement > 0.0f)
  322. // {
  323. // // Determine number of dividers, based on the length increment
  324. // mSegments = (unsigned short)(((endVector - startVector).length()) / lengthIncrement - 1);
  325. // }
  326. // Vec3 increment = (endVector - startVector) / mSegments;
  327. //
  328. // for (unsigned short i = 0; i <= mSegments; i++)
  329. // {
  330. // mSpawnPositionList.insert (iterator, start);
  331. // start += increment;
  332. // iterator--;
  333. // if (iterator != mSpawnPositionList.begin())
  334. // iterator--;
  335. // }
  336. //}
  337. NS_CC_END