CCGrid.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /****************************************************************************
  2. Copyright (c) 2009 On-Core
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 "2d/CCGrid.h"
  24. #include "base/ccMacros.h"
  25. #include "base/ccUtils.h"
  26. #include "2d/CCNode.h"
  27. #include "2d/CCGrabber.h"
  28. #include "renderer/CCGLProgram.h"
  29. #include "renderer/CCGLProgramCache.h"
  30. #include "renderer/ccGLStateCache.h"
  31. #include "renderer/CCRenderer.h"
  32. #include "renderer/CCRenderState.h"
  33. #include "renderer/CCTexture2D.h"
  34. #include "platform/CCGL.h"
  35. #include "2d/CCCamera.h"
  36. NS_CC_BEGIN
  37. // implementation of GridBase
  38. GridBase* GridBase::create(const Size& gridSize)
  39. {
  40. GridBase *pGridBase = new (std::nothrow) GridBase();
  41. if (pGridBase)
  42. {
  43. if (pGridBase->initWithSize(gridSize))
  44. {
  45. pGridBase->autorelease();
  46. }
  47. else
  48. {
  49. CC_SAFE_RELEASE_NULL(pGridBase);
  50. }
  51. }
  52. return pGridBase;
  53. }
  54. GridBase* GridBase::create(const Size& gridSize, Texture2D *texture, bool flipped)
  55. {
  56. GridBase *pGridBase = new (std::nothrow) GridBase();
  57. if (pGridBase)
  58. {
  59. if (pGridBase->initWithSize(gridSize, texture, flipped))
  60. {
  61. pGridBase->autorelease();
  62. }
  63. else
  64. {
  65. CC_SAFE_RELEASE_NULL(pGridBase);
  66. }
  67. }
  68. return pGridBase;
  69. }
  70. bool GridBase::initWithSize(const Size& gridSize)
  71. {
  72. return initWithSize(gridSize, Rect::ZERO);
  73. }
  74. bool GridBase::initWithSize(const cocos2d::Size &gridSize, const cocos2d::Rect &rect)
  75. {
  76. Director *director = Director::getInstance();
  77. Size s = director->getWinSizeInPixels();
  78. auto POTWide = ccNextPOT((unsigned int)s.width);
  79. auto POTHigh = ccNextPOT((unsigned int)s.height);
  80. // we only use rgba8888
  81. Texture2D::PixelFormat format = Texture2D::PixelFormat::RGBA8888;
  82. auto dataLen = POTWide * POTHigh * 4;
  83. void *data = calloc(dataLen, 1);
  84. if (! data)
  85. {
  86. CCLOG("cocos2d: Grid: not enough memory.");
  87. this->release();
  88. return false;
  89. }
  90. Texture2D *texture = new (std::nothrow) Texture2D();
  91. if (! texture)
  92. {
  93. free(data);
  94. CCLOG("cocos2d: Grid: error creating texture");
  95. return false;
  96. }
  97. texture->initWithData(data, dataLen, format, POTWide, POTHigh, s);
  98. free(data);
  99. initWithSize(gridSize, texture, false, rect);
  100. texture->release();
  101. return true;
  102. }
  103. bool GridBase::initWithSize(const Size& gridSize, Texture2D *texture, bool flipped)
  104. {
  105. return initWithSize(gridSize, texture, flipped, Rect::ZERO);
  106. }
  107. bool GridBase::initWithSize(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect)
  108. {
  109. bool ret = true;
  110. _active = false;
  111. _reuseGrid = 0;
  112. _gridSize = gridSize;
  113. _texture = texture;
  114. CC_SAFE_RETAIN(_texture);
  115. _isTextureFlipped = flipped;
  116. if (rect.equals(Rect::ZERO)) {
  117. auto size = _texture->getContentSize();
  118. _gridRect.setRect(0, 0, size.width, size.height);
  119. }
  120. else{
  121. _gridRect = rect;
  122. }
  123. _step.x = _gridRect.size.width/_gridSize.width;
  124. _step.y = _gridRect.size.height/_gridSize.height;
  125. _grabber = new (std::nothrow) Grabber();
  126. if (_grabber)
  127. {
  128. _grabber->grab(_texture);
  129. }
  130. else
  131. {
  132. ret = false;
  133. }
  134. _shaderProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
  135. calculateVertexPoints();
  136. return ret;
  137. }
  138. GridBase::~GridBase(void)
  139. {
  140. CCLOGINFO("deallocing GridBase: %p", this);
  141. //TODO: ? why 2.0 comments this line: setActive(false);
  142. CC_SAFE_RELEASE(_texture);
  143. CC_SAFE_RELEASE(_grabber);
  144. }
  145. // properties
  146. void GridBase::setActive(bool active)
  147. {
  148. _active = active;
  149. if (! active)
  150. {
  151. Director *pDirector = Director::getInstance();
  152. Director::Projection proj = pDirector->getProjection();
  153. pDirector->setProjection(proj);
  154. }
  155. }
  156. void GridBase::setTextureFlipped(bool flipped)
  157. {
  158. if (_isTextureFlipped != flipped)
  159. {
  160. _isTextureFlipped = flipped;
  161. calculateVertexPoints();
  162. }
  163. }
  164. void GridBase::set2DProjection()
  165. {
  166. Director *director = Director::getInstance();
  167. Size size = director->getWinSizeInPixels();
  168. director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
  169. Mat4 orthoMatrix;
  170. Mat4::createOrthographicOffCenter(0, size.width, 0, size.height, -1, 1, &orthoMatrix);
  171. director->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix);
  172. director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  173. GL::setProjectionMatrixDirty();
  174. }
  175. void GridBase::setGridRect(const cocos2d::Rect &rect)
  176. {
  177. _gridRect = rect;
  178. }
  179. void GridBase::beforeDraw(void)
  180. {
  181. // save projection
  182. Director *director = Director::getInstance();
  183. _directorProjection = director->getProjection();
  184. // 2d projection
  185. // [director setProjection:Director::Projection::_2D];
  186. set2DProjection();
  187. Size size = director->getWinSizeInPixels();
  188. glViewport(0, 0, (GLsizei)(size.width), (GLsizei)(size.height) );
  189. _grabber->beforeRender(_texture);
  190. }
  191. void GridBase::afterDraw(cocos2d::Node * /*target*/)
  192. {
  193. _grabber->afterRender(_texture);
  194. // restore projection
  195. Director *director = Director::getInstance();
  196. director->setProjection(_directorProjection);
  197. director->setViewport();
  198. const auto& vp = Camera::getDefaultViewport();
  199. glViewport(vp._left, vp._bottom, vp._width, vp._height);
  200. // if (target->getCamera()->isDirty())
  201. // {
  202. // Vec2 offset = target->getAnchorPointInPoints();
  203. //
  204. // //
  205. // // FIXME: Camera should be applied in the AnchorPoint
  206. // //
  207. // kmGLTranslatef(offset.x, offset.y, 0);
  208. // target->getCamera()->locate();
  209. // kmGLTranslatef(-offset.x, -offset.y, 0);
  210. // }
  211. GL::bindTexture2D(_texture->getName());
  212. // restore projection for default FBO .fixed bug #543 #544
  213. //TODO: Director::getInstance()->setProjection(Director::getInstance()->getProjection());
  214. //TODO: Director::getInstance()->applyOrientation();
  215. beforeBlit();
  216. blit();
  217. afterBlit();
  218. }
  219. void GridBase::blit(void)
  220. {
  221. CCASSERT(0, "Subclass should implement it.");
  222. }
  223. void GridBase::reuse(void)
  224. {
  225. CCASSERT(0, "Subclass should implement it!");
  226. }
  227. void GridBase::calculateVertexPoints(void)
  228. {
  229. CCASSERT(0, "Subclass should implement it.");
  230. }
  231. // implementation of Grid3D
  232. Grid3D* Grid3D::create(const Size& gridSize)
  233. {
  234. Grid3D *ret= new (std::nothrow) Grid3D();
  235. if (ret)
  236. {
  237. if (ret->initWithSize(gridSize))
  238. {
  239. ret->autorelease();
  240. }
  241. else
  242. {
  243. delete ret;
  244. ret = nullptr;
  245. }
  246. }
  247. return ret;
  248. }
  249. Grid3D* Grid3D::create(const Size& gridSize, const Rect& rect)
  250. {
  251. Grid3D *ret= new (std::nothrow) Grid3D();
  252. if (ret)
  253. {
  254. if (ret->initWithSize(gridSize, rect))
  255. {
  256. ret->autorelease();
  257. }
  258. else
  259. {
  260. delete ret;
  261. ret = nullptr;
  262. }
  263. }
  264. return ret;
  265. }
  266. Grid3D* Grid3D::create(const Size& gridSize, Texture2D *texture, bool flipped)
  267. {
  268. Grid3D *ret= new (std::nothrow) Grid3D();
  269. if (ret)
  270. {
  271. if (ret->initWithSize(gridSize, texture, flipped))
  272. {
  273. ret->autorelease();
  274. }
  275. else
  276. {
  277. delete ret;
  278. ret = nullptr;
  279. }
  280. }
  281. return ret;
  282. }
  283. Grid3D* Grid3D::create(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect)
  284. {
  285. Grid3D *ret= new (std::nothrow) Grid3D();
  286. if (ret)
  287. {
  288. if (ret->initWithSize(gridSize, texture, flipped, rect))
  289. {
  290. ret->autorelease();
  291. }
  292. else
  293. {
  294. delete ret;
  295. ret = nullptr;
  296. }
  297. }
  298. return ret;
  299. }
  300. Grid3D::Grid3D()
  301. : _texCoordinates(nullptr)
  302. , _vertices(nullptr)
  303. , _originalVertices(nullptr)
  304. , _indices(nullptr)
  305. , _needDepthTestForBlit(false)
  306. {
  307. }
  308. Grid3D::~Grid3D(void)
  309. {
  310. CC_SAFE_FREE(_texCoordinates);
  311. CC_SAFE_FREE(_vertices);
  312. CC_SAFE_FREE(_indices);
  313. CC_SAFE_FREE(_originalVertices);
  314. }
  315. void Grid3D::beforeBlit()
  316. {
  317. if(_needDepthTestForBlit)
  318. {
  319. _oldDepthTestValue = glIsEnabled(GL_DEPTH_TEST) != GL_FALSE;
  320. GLboolean depthWriteMask;
  321. glGetBooleanv(GL_DEPTH_WRITEMASK, &depthWriteMask);
  322. _oldDepthWriteValue = depthWriteMask != GL_FALSE;
  323. CHECK_GL_ERROR_DEBUG();
  324. glEnable(GL_DEPTH_TEST);
  325. RenderState::StateBlock::_defaultState->setDepthTest(true);
  326. glDepthMask(true);
  327. RenderState::StateBlock::_defaultState->setDepthWrite(true);
  328. }
  329. }
  330. void Grid3D::afterBlit()
  331. {
  332. if(_needDepthTestForBlit)
  333. {
  334. if(_oldDepthTestValue)
  335. glEnable(GL_DEPTH_TEST);
  336. else
  337. glDisable(GL_DEPTH_TEST);
  338. RenderState::StateBlock::_defaultState->setDepthTest(_oldDepthTestValue);
  339. glDepthMask(_oldDepthWriteValue);
  340. RenderState::StateBlock::_defaultState->setDepthWrite(_oldDepthWriteValue);
  341. }
  342. }
  343. void Grid3D::blit(void)
  344. {
  345. int n = _gridSize.width * _gridSize.height;
  346. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
  347. _shaderProgram->use();
  348. _shaderProgram->setUniformsForBuiltins();
  349. //
  350. // Attributes
  351. //
  352. // position
  353. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
  354. // texCoords
  355. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
  356. glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, _indices);
  357. }
  358. void Grid3D::calculateVertexPoints(void)
  359. {
  360. float width = (float)_texture->getPixelsWide();
  361. float height = (float)_texture->getPixelsHigh();
  362. float imageH = _texture->getContentSizeInPixels().height;
  363. int x, y, i;
  364. CC_SAFE_FREE(_vertices);
  365. CC_SAFE_FREE(_originalVertices);
  366. CC_SAFE_FREE(_texCoordinates);
  367. CC_SAFE_FREE(_indices);
  368. unsigned int numOfPoints = (_gridSize.width+1) * (_gridSize.height+1);
  369. _vertices = malloc(numOfPoints * sizeof(Vec3));
  370. _originalVertices = malloc(numOfPoints * sizeof(Vec3));
  371. _texCoordinates = malloc(numOfPoints * sizeof(Vec2));
  372. _indices = (GLushort*)malloc(_gridSize.width * _gridSize.height * sizeof(GLushort) * 6);
  373. GLfloat *vertArray = (GLfloat*)_vertices;
  374. GLfloat *texArray = (GLfloat*)_texCoordinates;
  375. GLushort *idxArray = _indices;
  376. for (x = 0; x < _gridSize.width; ++x)
  377. {
  378. for (y = 0; y < _gridSize.height; ++y)
  379. {
  380. int idx = (y * _gridSize.width) + x;
  381. GLfloat x1 = x * _step.x + _gridRect.origin.x;
  382. GLfloat x2 = x1 + _step.x;
  383. GLfloat y1 = y * _step.y + _gridRect.origin.y;
  384. GLfloat y2= y1 + _step.y;
  385. GLushort a = (GLushort)(x * (_gridSize.height + 1) + y);
  386. GLushort b = (GLushort)((x + 1) * (_gridSize.height + 1) + y);
  387. GLushort c = (GLushort)((x + 1) * (_gridSize.height + 1) + (y + 1));
  388. GLushort d = (GLushort)(x * (_gridSize.height + 1) + (y + 1));
  389. GLushort tempidx[6] = {a, b, d, b, c, d};
  390. memcpy(&idxArray[6*idx], tempidx, 6*sizeof(GLushort));
  391. int l1[4] = {a*3, b*3, c*3, d*3};
  392. Vec3 e(x1, y1, 0);
  393. Vec3 f(x2, y1, 0);
  394. Vec3 g(x2, y2, 0);
  395. Vec3 h(x1, y2, 0);
  396. Vec3 l2[4] = {e, f, g, h};
  397. int tex1[4] = {a*2, b*2, c*2, d*2};
  398. Vec2 Tex2F[4] = {Vec2(x1, y1), Vec2(x2, y1), Vec2(x2, y2), Vec2(x1, y2)};
  399. for (i = 0; i < 4; ++i)
  400. {
  401. vertArray[l1[i]] = l2[i].x;
  402. vertArray[l1[i] + 1] = l2[i].y;
  403. vertArray[l1[i] + 2] = l2[i].z;
  404. texArray[tex1[i]] = Tex2F[i].x / width;
  405. if (_isTextureFlipped)
  406. {
  407. texArray[tex1[i] + 1] = (imageH - Tex2F[i].y) / height;
  408. }
  409. else
  410. {
  411. texArray[tex1[i] + 1] = Tex2F[i].y / height;
  412. }
  413. }
  414. }
  415. }
  416. memcpy(_originalVertices, _vertices, (_gridSize.width+1) * (_gridSize.height+1) * sizeof(Vec3));
  417. }
  418. Vec3 Grid3D::getVertex(const Vec2& pos) const
  419. {
  420. CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
  421. int index = (pos.x * (_gridSize.height+1) + pos.y) * 3;
  422. float *vertArray = (float*)_vertices;
  423. Vec3 vert(vertArray[index], vertArray[index+1], vertArray[index+2]);
  424. return vert;
  425. }
  426. Vec3 Grid3D::getOriginalVertex(const Vec2& pos) const
  427. {
  428. CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
  429. int index = (pos.x * (_gridSize.height+1) + pos.y) * 3;
  430. float *vertArray = (float*)_originalVertices;
  431. Vec3 vert(vertArray[index], vertArray[index+1], vertArray[index+2]);
  432. return vert;
  433. }
  434. void Grid3D::setVertex(const Vec2& pos, const Vec3& vertex)
  435. {
  436. CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
  437. int index = (pos.x * (_gridSize.height + 1) + pos.y) * 3;
  438. float *vertArray = (float*)_vertices;
  439. vertArray[index] = vertex.x;
  440. vertArray[index+1] = vertex.y;
  441. vertArray[index+2] = vertex.z;
  442. }
  443. void Grid3D::reuse(void)
  444. {
  445. if (_reuseGrid > 0)
  446. {
  447. memcpy(_originalVertices, _vertices, (_gridSize.width+1) * (_gridSize.height+1) * sizeof(Vec3));
  448. --_reuseGrid;
  449. }
  450. }
  451. // implementation of TiledGrid3D
  452. TiledGrid3D::TiledGrid3D()
  453. : _texCoordinates(nullptr)
  454. , _vertices(nullptr)
  455. , _originalVertices(nullptr)
  456. , _indices(nullptr)
  457. {
  458. }
  459. TiledGrid3D::~TiledGrid3D(void)
  460. {
  461. CC_SAFE_FREE(_texCoordinates);
  462. CC_SAFE_FREE(_vertices);
  463. CC_SAFE_FREE(_originalVertices);
  464. CC_SAFE_FREE(_indices);
  465. }
  466. TiledGrid3D* TiledGrid3D::create(const Size& gridSize)
  467. {
  468. TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
  469. if (ret)
  470. {
  471. if (ret->initWithSize(gridSize))
  472. {
  473. ret->autorelease();
  474. }
  475. else
  476. {
  477. delete ret;
  478. ret = nullptr;
  479. }
  480. }
  481. return ret;
  482. }
  483. TiledGrid3D* TiledGrid3D::create(const Size& gridSize, const Rect& rect)
  484. {
  485. TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
  486. if (ret)
  487. {
  488. if (ret->initWithSize(gridSize, rect))
  489. {
  490. ret->autorelease();
  491. }
  492. else
  493. {
  494. delete ret;
  495. ret = nullptr;
  496. }
  497. }
  498. return ret;
  499. }
  500. TiledGrid3D* TiledGrid3D::create(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect)
  501. {
  502. TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
  503. if (ret)
  504. {
  505. if (ret->initWithSize(gridSize, texture, flipped, rect))
  506. {
  507. ret->autorelease();
  508. }
  509. else
  510. {
  511. delete ret;
  512. ret = nullptr;
  513. }
  514. }
  515. return ret;
  516. }
  517. TiledGrid3D* TiledGrid3D::create(const Size& gridSize, Texture2D *texture, bool flipped)
  518. {
  519. TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
  520. if (ret)
  521. {
  522. if (ret->initWithSize(gridSize, texture, flipped))
  523. {
  524. ret->autorelease();
  525. }
  526. else
  527. {
  528. delete ret;
  529. ret = nullptr;
  530. }
  531. }
  532. return ret;
  533. }
  534. void TiledGrid3D::blit(void)
  535. {
  536. int n = _gridSize.width * _gridSize.height;
  537. _shaderProgram->use();
  538. _shaderProgram->setUniformsForBuiltins();
  539. //
  540. // Attributes
  541. //
  542. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
  543. // position
  544. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
  545. // texCoords
  546. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
  547. glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, _indices);
  548. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,n*6);
  549. }
  550. void TiledGrid3D::calculateVertexPoints(void)
  551. {
  552. float width = (float)_texture->getPixelsWide();
  553. float height = (float)_texture->getPixelsHigh();
  554. float imageH = _texture->getContentSizeInPixels().height;
  555. int numQuads = _gridSize.width * _gridSize.height;
  556. CC_SAFE_FREE(_vertices);
  557. CC_SAFE_FREE(_originalVertices);
  558. CC_SAFE_FREE(_texCoordinates);
  559. CC_SAFE_FREE(_indices);
  560. _vertices = malloc(numQuads*4*sizeof(Vec3));
  561. _originalVertices = malloc(numQuads*4*sizeof(Vec3));
  562. _texCoordinates = malloc(numQuads*4*sizeof(Vec2));
  563. _indices = (GLushort*)malloc(numQuads*6*sizeof(GLushort));
  564. GLfloat *vertArray = (GLfloat*)_vertices;
  565. GLfloat *texArray = (GLfloat*)_texCoordinates;
  566. GLushort *idxArray = _indices;
  567. int x, y;
  568. for( x = 0; x < _gridSize.width; x++ )
  569. {
  570. for( y = 0; y < _gridSize.height; y++ )
  571. {
  572. float x1 = x * _step.x + _gridRect.origin.x;
  573. float x2 = x1 + _step.x;
  574. float y1 = y * _step.y + _gridRect.origin.y;
  575. float y2 = y1 + _step.y;
  576. *vertArray++ = x1;
  577. *vertArray++ = y1;
  578. *vertArray++ = 0;
  579. *vertArray++ = x2;
  580. *vertArray++ = y1;
  581. *vertArray++ = 0;
  582. *vertArray++ = x1;
  583. *vertArray++ = y2;
  584. *vertArray++ = 0;
  585. *vertArray++ = x2;
  586. *vertArray++ = y2;
  587. *vertArray++ = 0;
  588. float newY1 = y1;
  589. float newY2 = y2;
  590. if (_isTextureFlipped)
  591. {
  592. newY1 = imageH - y1;
  593. newY2 = imageH - y2;
  594. }
  595. *texArray++ = x1 / width;
  596. *texArray++ = newY1 / height;
  597. *texArray++ = x2 / width;
  598. *texArray++ = newY1 / height;
  599. *texArray++ = x1 / width;
  600. *texArray++ = newY2 / height;
  601. *texArray++ = x2 / width;
  602. *texArray++ = newY2 / height;
  603. }
  604. }
  605. for (x = 0; x < numQuads; x++)
  606. {
  607. idxArray[x*6+0] = (GLushort)(x * 4 + 0);
  608. idxArray[x*6+1] = (GLushort)(x * 4 + 1);
  609. idxArray[x*6+2] = (GLushort)(x * 4 + 2);
  610. idxArray[x*6+3] = (GLushort)(x * 4 + 1);
  611. idxArray[x*6+4] = (GLushort)(x * 4 + 2);
  612. idxArray[x*6+5] = (GLushort)(x * 4 + 3);
  613. }
  614. memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(GLfloat));
  615. }
  616. void TiledGrid3D::setTile(const Vec2& pos, const Quad3& coords)
  617. {
  618. CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
  619. int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
  620. float *vertArray = (float*)_vertices;
  621. memcpy(&vertArray[idx], &coords, sizeof(Quad3));
  622. }
  623. Quad3 TiledGrid3D::getOriginalTile(const Vec2& pos) const
  624. {
  625. CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
  626. int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
  627. float *vertArray = (float*)_originalVertices;
  628. Quad3 ret;
  629. memcpy(&ret, &vertArray[idx], sizeof(Quad3));
  630. return ret;
  631. }
  632. Quad3 TiledGrid3D::getTile(const Vec2& pos) const
  633. {
  634. CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
  635. int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
  636. float *vertArray = (float*)_vertices;
  637. Quad3 ret;
  638. memcpy(&ret, &vertArray[idx], sizeof(Quad3));
  639. return ret;
  640. }
  641. void TiledGrid3D::reuse(void)
  642. {
  643. if (_reuseGrid > 0)
  644. {
  645. int numQuads = _gridSize.width * _gridSize.height;
  646. memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(GLfloat));
  647. --_reuseGrid;
  648. }
  649. }
  650. NS_CC_END