CCParticleSystemQuad.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2009 Leonardo Kasperavičius
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2017 Chukong Technologies Inc.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCParticleSystemQuad.h"
  25. #include <algorithm>
  26. #include "2d/CCSpriteFrame.h"
  27. #include "2d/CCParticleBatchNode.h"
  28. #include "renderer/CCTextureAtlas.h"
  29. #include "renderer/ccGLStateCache.h"
  30. #include "renderer/CCRenderer.h"
  31. #include "base/CCDirector.h"
  32. #include "base/CCEventType.h"
  33. #include "base/CCConfiguration.h"
  34. #include "base/CCEventListenerCustom.h"
  35. #include "base/CCEventDispatcher.h"
  36. #include "base/ccUTF8.h"
  37. NS_CC_BEGIN
  38. ParticleSystemQuad::ParticleSystemQuad()
  39. :_quads(nullptr)
  40. ,_indices(nullptr)
  41. ,_VAOname(0)
  42. {
  43. memset(_buffersVBO, 0, sizeof(_buffersVBO));
  44. }
  45. ParticleSystemQuad::~ParticleSystemQuad()
  46. {
  47. if (nullptr == _batchNode)
  48. {
  49. CC_SAFE_FREE(_quads);
  50. CC_SAFE_FREE(_indices);
  51. glDeleteBuffers(2, &_buffersVBO[0]);
  52. if (Configuration::getInstance()->supportsShareableVAO())
  53. {
  54. glDeleteVertexArrays(1, &_VAOname);
  55. GL::bindVAO(0);
  56. }
  57. }
  58. }
  59. // implementation ParticleSystemQuad
  60. ParticleSystemQuad * ParticleSystemQuad::create(const std::string& filename)
  61. {
  62. ParticleSystemQuad *ret = new (std::nothrow) ParticleSystemQuad();
  63. if (ret && ret->initWithFile(filename))
  64. {
  65. ret->autorelease();
  66. return ret;
  67. }
  68. CC_SAFE_DELETE(ret);
  69. return ret;
  70. }
  71. ParticleSystemQuad * ParticleSystemQuad::createWithTotalParticles(int numberOfParticles) {
  72. ParticleSystemQuad *ret = new (std::nothrow) ParticleSystemQuad();
  73. if (ret && ret->initWithTotalParticles(numberOfParticles))
  74. {
  75. ret->autorelease();
  76. return ret;
  77. }
  78. CC_SAFE_DELETE(ret);
  79. return ret;
  80. }
  81. ParticleSystemQuad * ParticleSystemQuad::create(ValueMap &dictionary)
  82. {
  83. ParticleSystemQuad *ret = new (std::nothrow) ParticleSystemQuad();
  84. if (ret && ret->initWithDictionary(dictionary))
  85. {
  86. ret->autorelease();
  87. return ret;
  88. }
  89. CC_SAFE_DELETE(ret);
  90. return ret;
  91. }
  92. //implementation ParticleSystemQuad
  93. // overriding the init method
  94. bool ParticleSystemQuad::initWithTotalParticles(int numberOfParticles)
  95. {
  96. // base initialization
  97. if( ParticleSystem::initWithTotalParticles(numberOfParticles) )
  98. {
  99. // allocating data space
  100. if( ! this->allocMemory() ) {
  101. this->release();
  102. return false;
  103. }
  104. initIndices();
  105. if (Configuration::getInstance()->supportsShareableVAO())
  106. {
  107. setupVBOandVAO();
  108. }
  109. else
  110. {
  111. setupVBO();
  112. }
  113. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
  114. #if CC_ENABLE_CACHE_TEXTURE_DATA
  115. // Need to listen the event only when not use batchnode, because it will use VBO
  116. auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(ParticleSystemQuad::listenRendererRecreated, this));
  117. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  118. #endif
  119. return true;
  120. }
  121. return false;
  122. }
  123. // pointRect should be in Texture coordinates, not pixel coordinates
  124. void ParticleSystemQuad::initTexCoordsWithRect(const Rect& pointRect)
  125. {
  126. // convert to Tex coords
  127. Rect rect = Rect(
  128. pointRect.origin.x * CC_CONTENT_SCALE_FACTOR(),
  129. pointRect.origin.y * CC_CONTENT_SCALE_FACTOR(),
  130. pointRect.size.width * CC_CONTENT_SCALE_FACTOR(),
  131. pointRect.size.height * CC_CONTENT_SCALE_FACTOR());
  132. GLfloat wide = (GLfloat) pointRect.size.width;
  133. GLfloat high = (GLfloat) pointRect.size.height;
  134. if (_texture)
  135. {
  136. wide = (GLfloat)_texture->getPixelsWide();
  137. high = (GLfloat)_texture->getPixelsHigh();
  138. }
  139. #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  140. GLfloat left = (rect.origin.x*2+1) / (wide*2);
  141. GLfloat bottom = (rect.origin.y*2+1) / (high*2);
  142. GLfloat right = left + (rect.size.width*2-2) / (wide*2);
  143. GLfloat top = bottom + (rect.size.height*2-2) / (high*2);
  144. #else
  145. GLfloat left = rect.origin.x / wide;
  146. GLfloat bottom = rect.origin.y / high;
  147. GLfloat right = left + rect.size.width / wide;
  148. GLfloat top = bottom + rect.size.height / high;
  149. #endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  150. // Important. Texture in cocos2d are inverted, so the Y component should be inverted
  151. std::swap(top, bottom);
  152. V3F_C4B_T2F_Quad *quads = nullptr;
  153. unsigned int start = 0, end = 0;
  154. if (_batchNode)
  155. {
  156. quads = _batchNode->getTextureAtlas()->getQuads();
  157. start = _atlasIndex;
  158. end = _atlasIndex + _totalParticles;
  159. }
  160. else
  161. {
  162. quads = _quads;
  163. start = 0;
  164. end = _totalParticles;
  165. }
  166. for(unsigned int i=start; i<end; i++)
  167. {
  168. // bottom-left vertex:
  169. quads[i].bl.texCoords.u = left;
  170. quads[i].bl.texCoords.v = bottom;
  171. // bottom-right vertex:
  172. quads[i].br.texCoords.u = right;
  173. quads[i].br.texCoords.v = bottom;
  174. // top-left vertex:
  175. quads[i].tl.texCoords.u = left;
  176. quads[i].tl.texCoords.v = top;
  177. // top-right vertex:
  178. quads[i].tr.texCoords.u = right;
  179. quads[i].tr.texCoords.v = top;
  180. }
  181. }
  182. void ParticleSystemQuad::updateTexCoords()
  183. {
  184. if (_texture)
  185. {
  186. const Size& s = _texture->getContentSize();
  187. initTexCoordsWithRect(Rect(0, 0, s.width, s.height));
  188. }
  189. }
  190. void ParticleSystemQuad::setTextureWithRect(Texture2D *texture, const Rect& rect)
  191. {
  192. // Only update the texture if is different from the current one
  193. if( !_texture || texture->getName() != _texture->getName() )
  194. {
  195. ParticleSystem::setTexture(texture);
  196. }
  197. this->initTexCoordsWithRect(rect);
  198. }
  199. void ParticleSystemQuad::setTexture(Texture2D* texture)
  200. {
  201. const Size& s = texture->getContentSize();
  202. this->setTextureWithRect(texture, Rect(0, 0, s.width, s.height));
  203. }
  204. void ParticleSystemQuad::setDisplayFrame(SpriteFrame *spriteFrame)
  205. {
  206. CCASSERT(spriteFrame->getOffsetInPixels().isZero(),
  207. "QuadParticle only supports SpriteFrames with no offsets");
  208. this->setTextureWithRect(spriteFrame->getTexture(), spriteFrame->getRect());
  209. }
  210. void ParticleSystemQuad::initIndices()
  211. {
  212. for(int i = 0; i < _totalParticles; ++i)
  213. {
  214. const unsigned int i6 = i*6;
  215. const unsigned int i4 = i*4;
  216. _indices[i6+0] = (GLushort) i4+0;
  217. _indices[i6+1] = (GLushort) i4+1;
  218. _indices[i6+2] = (GLushort) i4+2;
  219. _indices[i6+5] = (GLushort) i4+1;
  220. _indices[i6+4] = (GLushort) i4+2;
  221. _indices[i6+3] = (GLushort) i4+3;
  222. }
  223. }
  224. inline void updatePosWithParticle(V3F_C4B_T2F_Quad *quad, const Vec2& newPosition,float size,float rotation)
  225. {
  226. // vertices
  227. GLfloat size_2 = size/2;
  228. GLfloat x1 = -size_2;
  229. GLfloat y1 = -size_2;
  230. GLfloat x2 = size_2;
  231. GLfloat y2 = size_2;
  232. GLfloat x = newPosition.x;
  233. GLfloat y = newPosition.y;
  234. GLfloat r = (GLfloat)-CC_DEGREES_TO_RADIANS(rotation);
  235. GLfloat cr = cosf(r);
  236. GLfloat sr = sinf(r);
  237. GLfloat ax = x1 * cr - y1 * sr + x;
  238. GLfloat ay = x1 * sr + y1 * cr + y;
  239. GLfloat bx = x2 * cr - y1 * sr + x;
  240. GLfloat by = x2 * sr + y1 * cr + y;
  241. GLfloat cx = x2 * cr - y2 * sr + x;
  242. GLfloat cy = x2 * sr + y2 * cr + y;
  243. GLfloat dx = x1 * cr - y2 * sr + x;
  244. GLfloat dy = x1 * sr + y2 * cr + y;
  245. // bottom-left
  246. quad->bl.vertices.x = ax;
  247. quad->bl.vertices.y = ay;
  248. // bottom-right vertex:
  249. quad->br.vertices.x = bx;
  250. quad->br.vertices.y = by;
  251. // top-left vertex:
  252. quad->tl.vertices.x = dx;
  253. quad->tl.vertices.y = dy;
  254. // top-right vertex:
  255. quad->tr.vertices.x = cx;
  256. quad->tr.vertices.y = cy;
  257. }
  258. void ParticleSystemQuad::updateParticleQuads()
  259. {
  260. if (_particleCount <= 0) {
  261. return;
  262. }
  263. Vec2 currentPosition;
  264. if (_positionType == PositionType::FREE)
  265. {
  266. currentPosition = this->convertToWorldSpace(Vec2::ZERO);
  267. }
  268. else if (_positionType == PositionType::RELATIVE)
  269. {
  270. currentPosition = _position;
  271. }
  272. V3F_C4B_T2F_Quad *startQuad;
  273. Vec2 pos = Vec2::ZERO;
  274. if (_batchNode)
  275. {
  276. V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
  277. startQuad = &(batchQuads[_atlasIndex]);
  278. pos = _position;
  279. }
  280. else
  281. {
  282. startQuad = &(_quads[0]);
  283. }
  284. if( _positionType == PositionType::FREE )
  285. {
  286. Vec3 p1(currentPosition.x, currentPosition.y, 0);
  287. Mat4 worldToNodeTM = getWorldToNodeTransform();
  288. worldToNodeTM.transformPoint(&p1);
  289. Vec3 p2;
  290. Vec2 newPos;
  291. float* startX = _particleData.startPosX;
  292. float* startY = _particleData.startPosY;
  293. float* x = _particleData.posx;
  294. float* y = _particleData.posy;
  295. float* s = _particleData.size;
  296. float* r = _particleData.rotation;
  297. V3F_C4B_T2F_Quad* quadStart = startQuad;
  298. for (int i = 0 ; i < _particleCount; ++i, ++startX, ++startY, ++x, ++y, ++quadStart, ++s, ++r)
  299. {
  300. p2.set(*startX, *startY, 0);
  301. worldToNodeTM.transformPoint(&p2);
  302. newPos.set(*x,*y);
  303. p2 = p1 - p2;
  304. newPos.x -= p2.x - pos.x;
  305. newPos.y -= p2.y - pos.y;
  306. updatePosWithParticle(quadStart, newPos, *s, *r);
  307. }
  308. }
  309. else if( _positionType == PositionType::RELATIVE )
  310. {
  311. Vec2 newPos;
  312. float* startX = _particleData.startPosX;
  313. float* startY = _particleData.startPosY;
  314. float* x = _particleData.posx;
  315. float* y = _particleData.posy;
  316. float* s = _particleData.size;
  317. float* r = _particleData.rotation;
  318. V3F_C4B_T2F_Quad* quadStart = startQuad;
  319. for (int i = 0 ; i < _particleCount; ++i, ++startX, ++startY, ++x, ++y, ++quadStart, ++s, ++r)
  320. {
  321. newPos.set(*x, *y);
  322. newPos.x = *x - (currentPosition.x - *startX);
  323. newPos.y = *y - (currentPosition.y - *startY);
  324. newPos += pos;
  325. updatePosWithParticle(quadStart, newPos, *s, *r);
  326. }
  327. }
  328. else
  329. {
  330. Vec2 newPos;
  331. float* startX = _particleData.startPosX;
  332. float* startY = _particleData.startPosY;
  333. float* x = _particleData.posx;
  334. float* y = _particleData.posy;
  335. float* s = _particleData.size;
  336. float* r = _particleData.rotation;
  337. V3F_C4B_T2F_Quad* quadStart = startQuad;
  338. for (int i = 0 ; i < _particleCount; ++i, ++startX, ++startY, ++x, ++y, ++quadStart, ++s, ++r)
  339. {
  340. newPos.set(*x + pos.x, *y + pos.y);
  341. updatePosWithParticle(quadStart, newPos, *s, *r);
  342. }
  343. }
  344. //set color
  345. if(_opacityModifyRGB)
  346. {
  347. V3F_C4B_T2F_Quad* quad = startQuad;
  348. float* r = _particleData.colorR;
  349. float* g = _particleData.colorG;
  350. float* b = _particleData.colorB;
  351. float* a = _particleData.colorA;
  352. for (int i = 0; i < _particleCount; ++i,++quad,++r,++g,++b,++a)
  353. {
  354. GLubyte colorR = *r * *a * 255;
  355. GLubyte colorG = *g * *a * 255;
  356. GLubyte colorB = *b * *a * 255;
  357. GLubyte colorA = *a * 255;
  358. quad->bl.colors.set(colorR, colorG, colorB, colorA);
  359. quad->br.colors.set(colorR, colorG, colorB, colorA);
  360. quad->tl.colors.set(colorR, colorG, colorB, colorA);
  361. quad->tr.colors.set(colorR, colorG, colorB, colorA);
  362. }
  363. }
  364. else
  365. {
  366. V3F_C4B_T2F_Quad* quad = startQuad;
  367. float* r = _particleData.colorR;
  368. float* g = _particleData.colorG;
  369. float* b = _particleData.colorB;
  370. float* a = _particleData.colorA;
  371. for (int i = 0; i < _particleCount; ++i,++quad,++r,++g,++b,++a)
  372. {
  373. GLubyte colorR = *r * 255;
  374. GLubyte colorG = *g * 255;
  375. GLubyte colorB = *b * 255;
  376. GLubyte colorA = *a * 255;
  377. quad->bl.colors.set(colorR, colorG, colorB, colorA);
  378. quad->br.colors.set(colorR, colorG, colorB, colorA);
  379. quad->tl.colors.set(colorR, colorG, colorB, colorA);
  380. quad->tr.colors.set(colorR, colorG, colorB, colorA);
  381. }
  382. }
  383. }
  384. void ParticleSystemQuad::postStep()
  385. {
  386. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  387. // Option 1: Sub Data
  388. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_quads[0])*_totalParticles, _quads);
  389. // Option 2: Data
  390. // glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * particleCount, quads_, GL_DYNAMIC_DRAW);
  391. // Option 3: Orphaning + glMapBuffer
  392. // glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0])*_totalParticles, nullptr, GL_STREAM_DRAW);
  393. // void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
  394. // memcpy(buf, _quads, sizeof(_quads[0])*_totalParticles);
  395. // glUnmapBuffer(GL_ARRAY_BUFFER);
  396. glBindBuffer(GL_ARRAY_BUFFER, 0);
  397. CHECK_GL_ERROR_DEBUG();
  398. }
  399. // overriding draw method
  400. void ParticleSystemQuad::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  401. {
  402. //quad command
  403. if(_particleCount > 0)
  404. {
  405. _quadCommand.init(_globalZOrder, _texture, getGLProgramState(), _blendFunc, _quads, _particleCount, transform, flags);
  406. renderer->addCommand(&_quadCommand);
  407. }
  408. }
  409. void ParticleSystemQuad::setTotalParticles(int tp)
  410. {
  411. // If we are setting the total number of particles to a number higher
  412. // than what is allocated, we need to allocate new arrays
  413. if( tp > _allocatedParticles )
  414. {
  415. // Allocate new memory
  416. size_t quadsSize = sizeof(_quads[0]) * tp * 1;
  417. size_t indicesSize = sizeof(_indices[0]) * tp * 6 * 1;
  418. _particleData.release();
  419. if (!_particleData.init(tp))
  420. {
  421. CCLOG("Particle system: not enough memory");
  422. return;
  423. }
  424. V3F_C4B_T2F_Quad* quadsNew = (V3F_C4B_T2F_Quad*)realloc(_quads, quadsSize);
  425. GLushort* indicesNew = (GLushort*)realloc(_indices, indicesSize);
  426. if (quadsNew && indicesNew)
  427. {
  428. // Assign pointers
  429. _quads = quadsNew;
  430. _indices = indicesNew;
  431. // Clear the memory
  432. memset(_quads, 0, quadsSize);
  433. memset(_indices, 0, indicesSize);
  434. _allocatedParticles = tp;
  435. }
  436. else
  437. {
  438. // Out of memory, failed to resize some array
  439. if (quadsNew) _quads = quadsNew;
  440. if (indicesNew) _indices = indicesNew;
  441. CCLOG("Particle system: out of memory");
  442. return;
  443. }
  444. _totalParticles = tp;
  445. // Init particles
  446. if (_batchNode)
  447. {
  448. for (int i = 0; i < _totalParticles; i++)
  449. {
  450. _particleData.atlasIndex[i] = i;
  451. }
  452. }
  453. initIndices();
  454. if (Configuration::getInstance()->supportsShareableVAO())
  455. {
  456. setupVBOandVAO();
  457. }
  458. else
  459. {
  460. setupVBO();
  461. }
  462. // fixed http://www.cocos2d-x.org/issues/3990
  463. // Updates texture coords.
  464. updateTexCoords();
  465. }
  466. else
  467. {
  468. _totalParticles = tp;
  469. }
  470. // fixed issue #5762
  471. // reset the emission rate
  472. setEmissionRate(_totalParticles / _life);
  473. resetSystem();
  474. }
  475. void ParticleSystemQuad::setupVBOandVAO()
  476. {
  477. // clean VAO
  478. glDeleteBuffers(2, &_buffersVBO[0]);
  479. glDeleteVertexArrays(1, &_VAOname);
  480. GL::bindVAO(0);
  481. glGenVertexArrays(1, &_VAOname);
  482. GL::bindVAO(_VAOname);
  483. #define kQuadSize sizeof(_quads[0].bl)
  484. glGenBuffers(2, &_buffersVBO[0]);
  485. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  486. glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW);
  487. // vertices
  488. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  489. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices));
  490. // colors
  491. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  492. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
  493. // tex coords
  494. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  495. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
  496. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
  497. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
  498. // Must unbind the VAO before changing the element buffer.
  499. GL::bindVAO(0);
  500. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  501. glBindBuffer(GL_ARRAY_BUFFER, 0);
  502. CHECK_GL_ERROR_DEBUG();
  503. }
  504. void ParticleSystemQuad::setupVBO()
  505. {
  506. glDeleteBuffers(2, &_buffersVBO[0]);
  507. glGenBuffers(2, &_buffersVBO[0]);
  508. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  509. glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW);
  510. glBindBuffer(GL_ARRAY_BUFFER, 0);
  511. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
  512. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _totalParticles * 6, _indices, GL_STATIC_DRAW);
  513. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  514. CHECK_GL_ERROR_DEBUG();
  515. }
  516. void ParticleSystemQuad::listenRendererRecreated(EventCustom* /*event*/)
  517. {
  518. //when comes to foreground in android, _buffersVBO and _VAOname is a wild handle
  519. //before recreating, we need to reset them to 0
  520. memset(_buffersVBO, 0, sizeof(_buffersVBO));
  521. if (Configuration::getInstance()->supportsShareableVAO())
  522. {
  523. _VAOname = 0;
  524. setupVBOandVAO();
  525. }
  526. else
  527. {
  528. setupVBO();
  529. }
  530. }
  531. bool ParticleSystemQuad::allocMemory()
  532. {
  533. CCASSERT( !_batchNode, "Memory should not be alloced when not using batchNode");
  534. CC_SAFE_FREE(_quads);
  535. CC_SAFE_FREE(_indices);
  536. _quads = (V3F_C4B_T2F_Quad*)malloc(_totalParticles * sizeof(V3F_C4B_T2F_Quad));
  537. _indices = (GLushort*)malloc(_totalParticles * 6 * sizeof(GLushort));
  538. if( !_quads || !_indices)
  539. {
  540. CCLOG("cocos2d: Particle system: not enough memory");
  541. CC_SAFE_FREE(_quads);
  542. CC_SAFE_FREE(_indices);
  543. return false;
  544. }
  545. memset(_quads, 0, _totalParticles * sizeof(V3F_C4B_T2F_Quad));
  546. memset(_indices, 0, _totalParticles * 6 * sizeof(GLushort));
  547. return true;
  548. }
  549. void ParticleSystemQuad::setBatchNode(ParticleBatchNode * batchNode)
  550. {
  551. if( _batchNode != batchNode )
  552. {
  553. ParticleBatchNode* oldBatch = _batchNode;
  554. ParticleSystem::setBatchNode(batchNode);
  555. // NEW: is self render ?
  556. if( ! batchNode )
  557. {
  558. allocMemory();
  559. initIndices();
  560. setTexture(oldBatch->getTexture());
  561. if (Configuration::getInstance()->supportsShareableVAO())
  562. {
  563. setupVBOandVAO();
  564. }
  565. else
  566. {
  567. setupVBO();
  568. }
  569. }
  570. // OLD: was it self render ? cleanup
  571. else if( !oldBatch )
  572. {
  573. // copy current state to batch
  574. V3F_C4B_T2F_Quad *batchQuads = _batchNode->getTextureAtlas()->getQuads();
  575. V3F_C4B_T2F_Quad *quad = &(batchQuads[_atlasIndex] );
  576. memcpy( quad, _quads, _totalParticles * sizeof(_quads[0]) );
  577. CC_SAFE_FREE(_quads);
  578. CC_SAFE_FREE(_indices);
  579. glDeleteBuffers(2, &_buffersVBO[0]);
  580. memset(_buffersVBO, 0, sizeof(_buffersVBO));
  581. if (Configuration::getInstance()->supportsShareableVAO())
  582. {
  583. glDeleteVertexArrays(1, &_VAOname);
  584. GL::bindVAO(0);
  585. _VAOname = 0;
  586. }
  587. }
  588. }
  589. }
  590. ParticleSystemQuad * ParticleSystemQuad::create() {
  591. ParticleSystemQuad *particleSystemQuad = new (std::nothrow) ParticleSystemQuad();
  592. if (particleSystemQuad && particleSystemQuad->init())
  593. {
  594. particleSystemQuad->autorelease();
  595. return particleSystemQuad;
  596. }
  597. CC_SAFE_DELETE(particleSystemQuad);
  598. return nullptr;
  599. }
  600. std::string ParticleSystemQuad::getDescription() const
  601. {
  602. return StringUtils::format("<ParticleSystemQuad | Tag = %d, Total Particles = %d>", _tag, _totalParticles);
  603. }
  604. NS_CC_END