CCParticleSystemQuad.cpp 21 KB

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