1
0

CCTextureAtlas.cpp 20 KB

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