CCTextureAtlas.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 "renderer/CCTextureAtlas.h"
  25. #include <stdlib.h>
  26. #include "base/ccMacros.h"
  27. #include "base/ccUTF8.h"
  28. #include "base/CCEventType.h"
  29. #include "base/CCDirector.h"
  30. #include "base/CCConfiguration.h"
  31. #include "base/CCEventDispatcher.h"
  32. #include "base/CCEventListenerCustom.h"
  33. #include "renderer/CCTextureCache.h"
  34. #include "renderer/CCGLProgram.h"
  35. #include "renderer/ccGLStateCache.h"
  36. #include "renderer/CCRenderer.h"
  37. #include "renderer/CCTexture2D.h"
  38. #include "platform/CCGL.h"
  39. //According to some tests GL_TRIANGLE_STRIP is slower, MUCH slower. Probably I'm doing something very wrong
  40. // implementation TextureAtlas
  41. NS_CC_BEGIN
  42. TextureAtlas::TextureAtlas()
  43. :_indices(nullptr)
  44. ,_dirty(false)
  45. ,_texture(nullptr)
  46. ,_quads(nullptr)
  47. #if CC_ENABLE_CACHE_TEXTURE_DATA
  48. ,_rendererRecreatedListener(nullptr)
  49. #endif
  50. {}
  51. TextureAtlas::~TextureAtlas()
  52. {
  53. CCLOGINFO("deallocing TextureAtlas: %p", this);
  54. CC_SAFE_FREE(_quads);
  55. CC_SAFE_FREE(_indices);
  56. glDeleteBuffers(2, _buffersVBO);
  57. if (Configuration::getInstance()->supportsShareableVAO())
  58. {
  59. glDeleteVertexArrays(1, &_VAOname);
  60. GL::bindVAO(0);
  61. }
  62. CC_SAFE_RELEASE(_texture);
  63. #if CC_ENABLE_CACHE_TEXTURE_DATA
  64. Director::getInstance()->getEventDispatcher()->removeEventListener(_rendererRecreatedListener);
  65. #endif
  66. }
  67. ssize_t TextureAtlas::getTotalQuads() const
  68. {
  69. return _totalQuads;
  70. }
  71. ssize_t TextureAtlas::getCapacity() const
  72. {
  73. return _capacity;
  74. }
  75. Texture2D* TextureAtlas::getTexture() const
  76. {
  77. return _texture;
  78. }
  79. void TextureAtlas::setTexture(Texture2D * var)
  80. {
  81. CC_SAFE_RETAIN(var);
  82. CC_SAFE_RELEASE(_texture);
  83. _texture = var;
  84. }
  85. V3F_C4B_T2F_Quad* TextureAtlas::getQuads()
  86. {
  87. //if someone accesses the quads directly, presume that changes will be made
  88. _dirty = true;
  89. return _quads;
  90. }
  91. void TextureAtlas::setQuads(V3F_C4B_T2F_Quad* quads)
  92. {
  93. _quads = quads;
  94. }
  95. // TextureAtlas - alloc & init
  96. TextureAtlas * TextureAtlas::create(const std::string& file, ssize_t capacity)
  97. {
  98. TextureAtlas * textureAtlas = new (std::nothrow) TextureAtlas();
  99. if(textureAtlas && textureAtlas->initWithFile(file, capacity))
  100. {
  101. textureAtlas->autorelease();
  102. return textureAtlas;
  103. }
  104. CC_SAFE_DELETE(textureAtlas);
  105. return nullptr;
  106. }
  107. TextureAtlas * TextureAtlas::createWithTexture(Texture2D *texture, ssize_t capacity)
  108. {
  109. TextureAtlas * textureAtlas = new (std::nothrow) TextureAtlas();
  110. if (textureAtlas && textureAtlas->initWithTexture(texture, capacity))
  111. {
  112. textureAtlas->autorelease();
  113. return textureAtlas;
  114. }
  115. CC_SAFE_DELETE(textureAtlas);
  116. return nullptr;
  117. }
  118. bool TextureAtlas::initWithFile(const std::string& file, ssize_t capacity)
  119. {
  120. // retained in property
  121. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(file);
  122. if (texture)
  123. {
  124. return initWithTexture(texture, capacity);
  125. }
  126. else
  127. {
  128. CCLOG("cocos2d: Could not open file: %s", file.c_str());
  129. return false;
  130. }
  131. }
  132. bool TextureAtlas::initWithTexture(Texture2D *texture, ssize_t capacity)
  133. {
  134. CCASSERT(capacity>=0, "Capacity must be >= 0");
  135. // CCASSERT(texture != nullptr, "texture should not be null");
  136. _capacity = capacity;
  137. _totalQuads = 0;
  138. // retained in property
  139. this->_texture = texture;
  140. CC_SAFE_RETAIN(_texture);
  141. // Re-initialization is not allowed
  142. CCASSERT(_quads == nullptr && _indices == nullptr, "_quads and _indices should be nullptr.");
  143. _quads = (V3F_C4B_T2F_Quad*)malloc( _capacity * sizeof(V3F_C4B_T2F_Quad) );
  144. _indices = (GLushort *)malloc( _capacity * 6 * sizeof(GLushort) );
  145. if( ! ( _quads && _indices) && _capacity > 0)
  146. {
  147. //CCLOG("cocos2d: TextureAtlas: not enough memory");
  148. CC_SAFE_FREE(_quads);
  149. CC_SAFE_FREE(_indices);
  150. // release texture, should set it to null, because the destruction will
  151. // release it too. see cocos2d-x issue #484
  152. CC_SAFE_RELEASE_NULL(_texture);
  153. return false;
  154. }
  155. memset( _quads, 0, _capacity * sizeof(V3F_C4B_T2F_Quad) );
  156. memset( _indices, 0, _capacity * 6 * sizeof(GLushort) );
  157. #if CC_ENABLE_CACHE_TEXTURE_DATA
  158. /** listen the event that renderer was recreated on Android/WP8 */
  159. _rendererRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, CC_CALLBACK_1(TextureAtlas::listenRendererRecreated, this));
  160. Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_rendererRecreatedListener, -1);
  161. #endif
  162. this->setupIndices();
  163. if (Configuration::getInstance()->supportsShareableVAO())
  164. {
  165. setupVBOandVAO();
  166. }
  167. else
  168. {
  169. setupVBO();
  170. }
  171. _dirty = true;
  172. return true;
  173. }
  174. void TextureAtlas::listenRendererRecreated(EventCustom* /*event*/)
  175. {
  176. if (Configuration::getInstance()->supportsShareableVAO())
  177. {
  178. setupVBOandVAO();
  179. }
  180. else
  181. {
  182. setupVBO();
  183. }
  184. // set _dirty to true to force it rebinding buffer
  185. _dirty = true;
  186. }
  187. std::string TextureAtlas::getDescription() const
  188. {
  189. return StringUtils::format("<TextureAtlas | totalQuads = %d>", static_cast<int>(_totalQuads));
  190. }
  191. void TextureAtlas::setupIndices()
  192. {
  193. if (_capacity == 0)
  194. return;
  195. for( int i=0; i < _capacity; i++)
  196. {
  197. _indices[i*6+0] = i*4+0;
  198. _indices[i*6+1] = i*4+1;
  199. _indices[i*6+2] = i*4+2;
  200. // inverted index. issue #179
  201. _indices[i*6+3] = i*4+3;
  202. _indices[i*6+4] = i*4+2;
  203. _indices[i*6+5] = i*4+1;
  204. }
  205. }
  206. //TextureAtlas - VAO / VBO specific
  207. void TextureAtlas::setupVBOandVAO()
  208. {
  209. glGenVertexArrays(1, &_VAOname);
  210. GL::bindVAO(_VAOname);
  211. #define kQuadSize sizeof(_quads[0].bl)
  212. glGenBuffers(2, &_buffersVBO[0]);
  213. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  214. glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, _quads, GL_DYNAMIC_DRAW);
  215. // vertices
  216. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  217. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices));
  218. // colors
  219. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  220. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors));
  221. // tex coords
  222. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  223. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords));
  224. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
  225. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW);
  226. // Must unbind the VAO before changing the element buffer.
  227. GL::bindVAO(0);
  228. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  229. glBindBuffer(GL_ARRAY_BUFFER, 0);
  230. CHECK_GL_ERROR_DEBUG();
  231. }
  232. void TextureAtlas::setupVBO()
  233. {
  234. glGenBuffers(2, &_buffersVBO[0]);
  235. mapBuffers();
  236. }
  237. void TextureAtlas::mapBuffers()
  238. {
  239. // Avoid changing the element buffer for whatever VAO might be bound.
  240. GL::bindVAO(0);
  241. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  242. glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, _quads, GL_DYNAMIC_DRAW);
  243. glBindBuffer(GL_ARRAY_BUFFER, 0);
  244. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
  245. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _capacity * 6, _indices, GL_STATIC_DRAW);
  246. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  247. CHECK_GL_ERROR_DEBUG();
  248. }
  249. // TextureAtlas - Update, Insert, Move & Remove
  250. void TextureAtlas::updateQuad(V3F_C4B_T2F_Quad *quad, ssize_t index)
  251. {
  252. CCASSERT( index >= 0 && index < _capacity, "updateQuadWithTexture: Invalid index");
  253. _totalQuads = MAX( index+1, _totalQuads);
  254. _quads[index] = *quad;
  255. _dirty = true;
  256. }
  257. void TextureAtlas::insertQuad(V3F_C4B_T2F_Quad *quad, ssize_t index)
  258. {
  259. CCASSERT( index>=0 && index<_capacity, "insertQuadWithTexture: Invalid index");
  260. _totalQuads++;
  261. CCASSERT( _totalQuads <= _capacity, "invalid totalQuads");
  262. // issue #575. index can be > totalQuads
  263. auto remaining = (_totalQuads-1) - index;
  264. // last object doesn't need to be moved
  265. if( remaining > 0)
  266. {
  267. // texture coordinates
  268. memmove( &_quads[index+1],&_quads[index], sizeof(_quads[0]) * remaining );
  269. }
  270. _quads[index] = *quad;
  271. _dirty = true;
  272. }
  273. void TextureAtlas::insertQuads(V3F_C4B_T2F_Quad* quads, ssize_t index, ssize_t amount)
  274. {
  275. CCASSERT(index>=0 && amount>=0 && index+amount<=_capacity, "insertQuadWithTexture: Invalid index + amount");
  276. _totalQuads += amount;
  277. CCASSERT( _totalQuads <= _capacity, "invalid totalQuads");
  278. // issue #575. index can be > totalQuads
  279. auto remaining = (_totalQuads-1) - index - amount;
  280. // last object doesn't need to be moved
  281. if( remaining > 0)
  282. {
  283. // tex coordinates
  284. memmove( &_quads[index+amount],&_quads[index], sizeof(_quads[0]) * remaining );
  285. }
  286. auto max = index + amount;
  287. int j = 0;
  288. for (ssize_t i = index; i < max ; i++)
  289. {
  290. _quads[index] = quads[j];
  291. index++;
  292. j++;
  293. }
  294. _dirty = true;
  295. }
  296. void TextureAtlas::insertQuadFromIndex(ssize_t oldIndex, ssize_t newIndex)
  297. {
  298. CCASSERT( newIndex >= 0 && newIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
  299. CCASSERT( oldIndex >= 0 && oldIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
  300. if( oldIndex == newIndex )
  301. {
  302. return;
  303. }
  304. // because it is ambiguous in iphone, so we implement abs ourselves
  305. // unsigned int howMany = std::abs( oldIndex - newIndex);
  306. auto howMany = (oldIndex - newIndex) > 0 ? (oldIndex - newIndex) : (newIndex - oldIndex);
  307. auto dst = oldIndex;
  308. auto src = oldIndex + 1;
  309. if( oldIndex > newIndex)
  310. {
  311. dst = newIndex+1;
  312. src = newIndex;
  313. }
  314. // texture coordinates
  315. V3F_C4B_T2F_Quad quadsBackup = _quads[oldIndex];
  316. memmove( &_quads[dst],&_quads[src], sizeof(_quads[0]) * howMany );
  317. _quads[newIndex] = quadsBackup;
  318. _dirty = true;
  319. }
  320. void TextureAtlas::removeQuadAtIndex(ssize_t index)
  321. {
  322. CCASSERT( index>=0 && index<_totalQuads, "removeQuadAtIndex: Invalid index");
  323. auto remaining = (_totalQuads-1) - index;
  324. // last object doesn't need to be moved
  325. if( remaining )
  326. {
  327. // texture coordinates
  328. memmove( &_quads[index],&_quads[index+1], sizeof(_quads[0]) * remaining );
  329. }
  330. _totalQuads--;
  331. _dirty = true;
  332. }
  333. void TextureAtlas::removeQuadsAtIndex(ssize_t index, ssize_t amount)
  334. {
  335. CCASSERT(index>=0 && amount>=0 && index+amount<=_totalQuads, "removeQuadAtIndex: index + amount out of bounds");
  336. auto remaining = (_totalQuads) - (index + amount);
  337. _totalQuads -= amount;
  338. if ( remaining )
  339. {
  340. memmove( &_quads[index], &_quads[index+amount], sizeof(_quads[0]) * remaining );
  341. }
  342. _dirty = true;
  343. }
  344. void TextureAtlas::removeAllQuads()
  345. {
  346. _totalQuads = 0;
  347. }
  348. // TextureAtlas - Resize
  349. bool TextureAtlas::resizeCapacity(ssize_t newCapacity)
  350. {
  351. CCASSERT(newCapacity >= 0, "capacity >= 0");
  352. if (newCapacity == _capacity)
  353. {
  354. return true;
  355. }
  356. auto oldCapacity = _capacity;
  357. // update capacity and totalQuads
  358. _totalQuads = MIN(_totalQuads, newCapacity);
  359. _capacity = newCapacity;
  360. V3F_C4B_T2F_Quad* tmpQuads = nullptr;
  361. GLushort* tmpIndices = nullptr;
  362. // when calling initWithTexture(fileName, 0) on bada device, calloc(0, 1) will fail and return nullptr,
  363. // so here must judge whether _quads and _indices is nullptr.
  364. ssize_t _quads_size = sizeof(_quads[0]);
  365. ssize_t new_quads_size = _capacity * _quads_size;
  366. if (_quads == nullptr)
  367. {
  368. tmpQuads = (V3F_C4B_T2F_Quad*)malloc(new_quads_size);
  369. if (tmpQuads != nullptr)
  370. {
  371. memset(tmpQuads, 0, new_quads_size);
  372. }
  373. }
  374. else
  375. {
  376. tmpQuads = (V3F_C4B_T2F_Quad*)realloc(_quads, new_quads_size);
  377. if (tmpQuads != nullptr && _capacity > oldCapacity)
  378. {
  379. memset(tmpQuads + oldCapacity, 0, (_capacity - oldCapacity)*_quads_size);
  380. }
  381. _quads = nullptr;
  382. }
  383. ssize_t _indices_size = sizeof(_indices[0]);
  384. ssize_t new_size = _capacity * 6 * _indices_size;
  385. if (_indices == nullptr)
  386. {
  387. tmpIndices = (GLushort*)malloc(new_size);
  388. if (tmpIndices != nullptr)
  389. {
  390. memset(tmpIndices, 0, new_size);
  391. }
  392. }
  393. else
  394. {
  395. tmpIndices = (GLushort*)realloc(_indices, new_size);
  396. if (tmpIndices != nullptr && _capacity > oldCapacity)
  397. {
  398. memset(tmpIndices + oldCapacity, 0, (_capacity - oldCapacity) * 6 * _indices_size);
  399. }
  400. _indices = nullptr;
  401. }
  402. if (!(tmpQuads && tmpIndices)) {
  403. CCLOG("cocos2d: TextureAtlas: not enough memory");
  404. CC_SAFE_FREE(tmpQuads);
  405. CC_SAFE_FREE(tmpIndices);
  406. CC_SAFE_FREE(_quads);
  407. CC_SAFE_FREE(_indices);
  408. _capacity = _totalQuads = 0;
  409. return false;
  410. }
  411. _quads = tmpQuads;
  412. _indices = tmpIndices;
  413. setupIndices();
  414. mapBuffers();
  415. _dirty = true;
  416. return true;
  417. }
  418. void TextureAtlas::increaseTotalQuadsWith(ssize_t amount)
  419. {
  420. CCASSERT(amount>=0, "amount >= 0");
  421. _totalQuads += amount;
  422. }
  423. void TextureAtlas::moveQuadsFromIndex(ssize_t oldIndex, ssize_t amount, ssize_t newIndex)
  424. {
  425. CCASSERT(oldIndex>=0 && amount>=0 && newIndex>=0, "values must be >= 0");
  426. CCASSERT(newIndex + amount <= _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
  427. CCASSERT(oldIndex < _totalQuads, "insertQuadFromIndex:atIndex: Invalid index");
  428. if( oldIndex == newIndex )
  429. {
  430. return;
  431. }
  432. //create buffer
  433. size_t quadSize = sizeof(V3F_C4B_T2F_Quad);
  434. V3F_C4B_T2F_Quad* tempQuads = (V3F_C4B_T2F_Quad*)malloc( quadSize * amount);
  435. memcpy( tempQuads, &_quads[oldIndex], quadSize * amount );
  436. if (newIndex < oldIndex)
  437. {
  438. // move quads from newIndex to newIndex + amount to make room for buffer
  439. memmove( &_quads[newIndex], &_quads[newIndex+amount], (oldIndex-newIndex)*quadSize);
  440. }
  441. else
  442. {
  443. // move quads above back
  444. memmove( &_quads[oldIndex], &_quads[oldIndex+amount], (newIndex-oldIndex)*quadSize);
  445. }
  446. memcpy( &_quads[newIndex], tempQuads, amount*quadSize);
  447. free(tempQuads);
  448. _dirty = true;
  449. }
  450. void TextureAtlas::moveQuadsFromIndex(ssize_t index, ssize_t newIndex)
  451. {
  452. CCASSERT(index>=0 && newIndex>=0, "values must be >= 0");
  453. CCASSERT(newIndex + (_totalQuads - index) <= _capacity, "moveQuadsFromIndex move is out of bounds");
  454. memmove(_quads + newIndex,_quads + index, (_totalQuads - index) * sizeof(_quads[0]));
  455. }
  456. void TextureAtlas::fillWithEmptyQuadsFromIndex(ssize_t index, ssize_t amount)
  457. {
  458. CCASSERT(index>=0 && amount>=0, "values must be >= 0");
  459. V3F_C4B_T2F_Quad quad;
  460. memset(&quad, 0, sizeof(quad));
  461. auto to = index + amount;
  462. for (ssize_t i = index ; i < to ; i++)
  463. {
  464. _quads[i] = quad;
  465. }
  466. }
  467. // TextureAtlas - Drawing
  468. void TextureAtlas::drawQuads()
  469. {
  470. this->drawNumberOfQuads(_totalQuads, 0);
  471. }
  472. void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads)
  473. {
  474. CCASSERT(numberOfQuads>=0, "numberOfQuads must be >= 0");
  475. this->drawNumberOfQuads(numberOfQuads, 0);
  476. }
  477. void TextureAtlas::drawNumberOfQuads(ssize_t numberOfQuads, ssize_t start)
  478. {
  479. CCASSERT(numberOfQuads>=0 && start>=0, "numberOfQuads and start must be >= 0");
  480. if(!numberOfQuads)
  481. return;
  482. GL::bindTexture2D(_texture);
  483. auto conf = Configuration::getInstance();
  484. if (conf->supportsShareableVAO() && conf->supportsMapBuffer())
  485. {
  486. //
  487. // Using VBO and VAO
  488. //
  489. // FIXME:: update is done in draw... perhaps it should be done in a timer
  490. if (_dirty)
  491. {
  492. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  493. // option 1: subdata
  494. // glBufferSubData(GL_ARRAY_BUFFER, sizeof(_quads[0])*start, sizeof(_quads[0]) * n , &_quads[start] );
  495. // option 2: data
  496. // glBufferData(GL_ARRAY_BUFFER, sizeof(quads_[0]) * (n-start), &quads_[start], GL_DYNAMIC_DRAW);
  497. // option 3: orphaning + glMapBuffer
  498. glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, nullptr, GL_DYNAMIC_DRAW);
  499. void *buf = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
  500. memcpy(buf, _quads, sizeof(_quads[0])* _totalQuads);
  501. glUnmapBuffer(GL_ARRAY_BUFFER);
  502. glBindBuffer(GL_ARRAY_BUFFER, 0);
  503. _dirty = false;
  504. }
  505. GL::bindVAO(_VAOname);
  506. #if CC_REBIND_INDICES_BUFFER
  507. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
  508. #endif
  509. glDrawElements(GL_TRIANGLES, (GLsizei) numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])) );
  510. GL::bindVAO(0);
  511. #if CC_REBIND_INDICES_BUFFER
  512. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  513. #endif
  514. // glBindVertexArray(0);
  515. }
  516. else
  517. {
  518. //
  519. // Using VBO without VAO
  520. //
  521. #define kQuadSize sizeof(_quads[0].bl)
  522. glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
  523. // FIXME:: update is done in draw... perhaps it should be done in a timer
  524. if (_dirty)
  525. {
  526. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_quads[0]) * _totalQuads , &_quads[0] );
  527. _dirty = false;
  528. }
  529. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  530. // vertices
  531. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices));
  532. // colors
  533. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));
  534. // tex coords
  535. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
  536. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
  537. glDrawElements(GL_TRIANGLES, (GLsizei)numberOfQuads*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(_indices[0])));
  538. glBindBuffer(GL_ARRAY_BUFFER, 0);
  539. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  540. }
  541. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,numberOfQuads*6);
  542. CHECK_GL_ERROR_DEBUG();
  543. }
  544. NS_CC_END