CCDrawNode.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  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 THE
  22. * SOFTWARE.
  23. */
  24. #include "2d/CCDrawNode.h"
  25. #include "base/CCEventType.h"
  26. #include "base/CCConfiguration.h"
  27. #include "renderer/CCRenderer.h"
  28. #include "renderer/ccGLStateCache.h"
  29. #include "renderer/CCGLProgramState.h"
  30. #include "renderer/CCGLProgramCache.h"
  31. #include "base/CCDirector.h"
  32. #include "base/CCEventListenerCustom.h"
  33. #include "base/CCEventDispatcher.h"
  34. #include "2d/CCActionCatmullRom.h"
  35. #include "platform/CCGL.h"
  36. NS_CC_BEGIN
  37. // Vec2 == CGPoint in 32-bits, but not in 64-bits (OS X)
  38. // that's why the "v2f" functions are needed
  39. static Vec2 v2fzero(0.0f,0.0f);
  40. static inline Vec2 v2f(float x, float y)
  41. {
  42. Vec2 ret(x, y);
  43. return ret;
  44. }
  45. static inline Vec2 v2fadd(const Vec2 &v0, const Vec2 &v1)
  46. {
  47. return v2f(v0.x+v1.x, v0.y+v1.y);
  48. }
  49. static inline Vec2 v2fsub(const Vec2 &v0, const Vec2 &v1)
  50. {
  51. return v2f(v0.x-v1.x, v0.y-v1.y);
  52. }
  53. static inline Vec2 v2fmult(const Vec2 &v, float s)
  54. {
  55. return v2f(v.x * s, v.y * s);
  56. }
  57. static inline Vec2 v2fperp(const Vec2 &p0)
  58. {
  59. return v2f(-p0.y, p0.x);
  60. }
  61. static inline Vec2 v2fneg(const Vec2 &p0)
  62. {
  63. return v2f(-p0.x, - p0.y);
  64. }
  65. static inline float v2fdot(const Vec2 &p0, const Vec2 &p1)
  66. {
  67. return p0.x * p1.x + p0.y * p1.y;
  68. }
  69. static inline Vec2 v2fnormalize(const Vec2 &p)
  70. {
  71. Vec2 r(p.x, p.y);
  72. r.normalize();
  73. return v2f(r.x, r.y);
  74. }
  75. static inline Vec2 __v2f(const Vec2 &v)
  76. {
  77. //#ifdef __LP64__
  78. return v2f(v.x, v.y);
  79. // #else
  80. // return * ((Vec2*) &v);
  81. // #endif
  82. }
  83. static inline Tex2F __t(const Vec2 &v)
  84. {
  85. return *(Tex2F*)&v;
  86. }
  87. // implementation of DrawNode
  88. DrawNode::DrawNode(GLfloat lineWidth)
  89. : _vao(0)
  90. , _vbo(0)
  91. , _vaoGLPoint(0)
  92. , _vboGLPoint(0)
  93. , _vaoGLLine(0)
  94. , _vboGLLine(0)
  95. , _bufferCapacity(0)
  96. , _bufferCount(0)
  97. , _buffer(nullptr)
  98. , _bufferCapacityGLPoint(0)
  99. , _bufferCountGLPoint(0)
  100. , _bufferGLPoint(nullptr)
  101. , _bufferCapacityGLLine(0)
  102. , _bufferCountGLLine(0)
  103. , _bufferGLLine(nullptr)
  104. , _dirty(false)
  105. , _dirtyGLPoint(false)
  106. , _dirtyGLLine(false)
  107. , _lineWidth(lineWidth)
  108. , _defaultLineWidth(lineWidth)
  109. {
  110. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  111. }
  112. DrawNode::~DrawNode()
  113. {
  114. free(_buffer);
  115. _buffer = nullptr;
  116. free(_bufferGLPoint);
  117. _bufferGLPoint = nullptr;
  118. free(_bufferGLLine);
  119. _bufferGLLine = nullptr;
  120. glDeleteBuffers(1, &_vbo);
  121. glDeleteBuffers(1, &_vboGLLine);
  122. glDeleteBuffers(1, &_vboGLPoint);
  123. _vbo = 0;
  124. _vboGLPoint = 0;
  125. _vboGLLine = 0;
  126. if (Configuration::getInstance()->supportsShareableVAO())
  127. {
  128. GL::bindVAO(0);
  129. glDeleteVertexArrays(1, &_vao);
  130. glDeleteVertexArrays(1, &_vaoGLLine);
  131. glDeleteVertexArrays(1, &_vaoGLPoint);
  132. _vao = _vaoGLLine = _vaoGLPoint = 0;
  133. }
  134. }
  135. DrawNode* DrawNode::create(GLfloat defaultLineWidth)
  136. {
  137. DrawNode* ret = new (std::nothrow) DrawNode(defaultLineWidth);
  138. if (ret && ret->init())
  139. {
  140. ret->autorelease();
  141. }
  142. else
  143. {
  144. CC_SAFE_DELETE(ret);
  145. }
  146. return ret;
  147. }
  148. void DrawNode::ensureCapacity(int count)
  149. {
  150. CCASSERT(count>=0, "capacity must be >= 0");
  151. if(_bufferCount + count > _bufferCapacity)
  152. {
  153. _bufferCapacity += MAX(_bufferCapacity, count);
  154. _buffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F));
  155. }
  156. }
  157. void DrawNode::ensureCapacityGLPoint(int count)
  158. {
  159. CCASSERT(count>=0, "capacity must be >= 0");
  160. if(_bufferCountGLPoint + count > _bufferCapacityGLPoint)
  161. {
  162. _bufferCapacityGLPoint += MAX(_bufferCapacityGLPoint, count);
  163. _bufferGLPoint = (V2F_C4B_T2F*)realloc(_bufferGLPoint, _bufferCapacityGLPoint*sizeof(V2F_C4B_T2F));
  164. }
  165. }
  166. void DrawNode::ensureCapacityGLLine(int count)
  167. {
  168. CCASSERT(count>=0, "capacity must be >= 0");
  169. if(_bufferCountGLLine + count > _bufferCapacityGLLine)
  170. {
  171. _bufferCapacityGLLine += MAX(_bufferCapacityGLLine, count);
  172. _bufferGLLine = (V2F_C4B_T2F*)realloc(_bufferGLLine, _bufferCapacityGLLine*sizeof(V2F_C4B_T2F));
  173. }
  174. }
  175. bool DrawNode::init()
  176. {
  177. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  178. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR));
  179. ensureCapacity(512);
  180. ensureCapacityGLPoint(64);
  181. ensureCapacityGLLine(256);
  182. if (Configuration::getInstance()->supportsShareableVAO())
  183. {
  184. glGenVertexArrays(1, &_vao);
  185. GL::bindVAO(_vao);
  186. glGenBuffers(1, &_vbo);
  187. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  188. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
  189. // vertex
  190. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  191. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  192. // color
  193. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  194. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  195. // texcoord
  196. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  197. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  198. glGenVertexArrays(1, &_vaoGLLine);
  199. GL::bindVAO(_vaoGLLine);
  200. glGenBuffers(1, &_vboGLLine);
  201. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  202. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
  203. // vertex
  204. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  205. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  206. // color
  207. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  208. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  209. // texcoord
  210. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  211. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  212. glGenVertexArrays(1, &_vaoGLPoint);
  213. GL::bindVAO(_vaoGLPoint);
  214. glGenBuffers(1, &_vboGLPoint);
  215. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  216. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
  217. // vertex
  218. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
  219. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  220. // color
  221. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
  222. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  223. // Texture coord as pointsize
  224. glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
  225. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  226. GL::bindVAO(0);
  227. glBindBuffer(GL_ARRAY_BUFFER, 0);
  228. }
  229. else
  230. {
  231. glGenBuffers(1, &_vbo);
  232. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  233. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
  234. glGenBuffers(1, &_vboGLLine);
  235. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  236. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
  237. glGenBuffers(1, &_vboGLPoint);
  238. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  239. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
  240. glBindBuffer(GL_ARRAY_BUFFER, 0);
  241. }
  242. CHECK_GL_ERROR_DEBUG();
  243. _dirty = true;
  244. _dirtyGLLine = true;
  245. _dirtyGLPoint = true;
  246. #if CC_ENABLE_CACHE_TEXTURE_DATA
  247. // Need to listen the event only when not use batchnode, because it will use VBO
  248. auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
  249. /** listen the event that renderer was recreated on Android/WP8 */
  250. this->init();
  251. });
  252. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  253. #endif
  254. return true;
  255. }
  256. void DrawNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  257. {
  258. if(_bufferCount)
  259. {
  260. _customCommand.init(_globalZOrder, transform, flags);
  261. _customCommand.func = CC_CALLBACK_0(DrawNode::onDraw, this, transform, flags);
  262. renderer->addCommand(&_customCommand);
  263. }
  264. if(_bufferCountGLPoint)
  265. {
  266. _customCommandGLPoint.init(_globalZOrder, transform, flags);
  267. _customCommandGLPoint.func = CC_CALLBACK_0(DrawNode::onDrawGLPoint, this, transform, flags);
  268. renderer->addCommand(&_customCommandGLPoint);
  269. }
  270. if(_bufferCountGLLine)
  271. {
  272. _customCommandGLLine.init(_globalZOrder, transform, flags);
  273. _customCommandGLLine.func = CC_CALLBACK_0(DrawNode::onDrawGLLine, this, transform, flags);
  274. renderer->addCommand(&_customCommandGLLine);
  275. }
  276. }
  277. void DrawNode::onDraw(const Mat4 &transform, uint32_t /*flags*/)
  278. {
  279. getGLProgramState()->apply(transform);
  280. auto glProgram = this->getGLProgram();
  281. glProgram->setUniformLocationWith1f(glProgram->getUniformLocation("u_alpha"), _displayedOpacity / 255.0);
  282. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  283. if (_dirty)
  284. {
  285. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  286. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacity, _buffer, GL_STREAM_DRAW);
  287. _dirty = false;
  288. }
  289. if (Configuration::getInstance()->supportsShareableVAO())
  290. {
  291. GL::bindVAO(_vao);
  292. }
  293. else
  294. {
  295. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  296. glBindBuffer(GL_ARRAY_BUFFER, _vbo);
  297. // vertex
  298. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  299. // color
  300. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  301. // texcoord
  302. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  303. }
  304. glDrawArrays(GL_TRIANGLES, 0, _bufferCount);
  305. glBindBuffer(GL_ARRAY_BUFFER, 0);
  306. if (Configuration::getInstance()->supportsShareableVAO())
  307. {
  308. GL::bindVAO(0);
  309. }
  310. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferCount);
  311. CHECK_GL_ERROR_DEBUG();
  312. }
  313. void DrawNode::onDrawGLLine(const Mat4 &transform, uint32_t /*flags*/)
  314. {
  315. auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_LENGTH_TEXTURE_COLOR);
  316. glProgram->use();
  317. glProgram->setUniformsForBuiltins(transform);
  318. glProgram->setUniformLocationWith1f(glProgram->getUniformLocation("u_alpha"), _displayedOpacity / 255.0);
  319. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  320. if (_dirtyGLLine)
  321. {
  322. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  323. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLLine, _bufferGLLine, GL_STREAM_DRAW);
  324. _dirtyGLLine = false;
  325. }
  326. if (Configuration::getInstance()->supportsShareableVAO())
  327. {
  328. GL::bindVAO(_vaoGLLine);
  329. }
  330. else
  331. {
  332. glBindBuffer(GL_ARRAY_BUFFER, _vboGLLine);
  333. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  334. // vertex
  335. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  336. // color
  337. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  338. // texcoord
  339. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  340. }
  341. glLineWidth(_lineWidth);
  342. glDrawArrays(GL_LINES, 0, _bufferCountGLLine);
  343. if (Configuration::getInstance()->supportsShareableVAO())
  344. {
  345. GL::bindVAO(0);
  346. }
  347. glBindBuffer(GL_ARRAY_BUFFER, 0);
  348. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLLine);
  349. CHECK_GL_ERROR_DEBUG();
  350. }
  351. void DrawNode::onDrawGLPoint(const Mat4 &transform, uint32_t /*flags*/)
  352. {
  353. auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR_TEXASPOINTSIZE);
  354. glProgram->use();
  355. glProgram->setUniformsForBuiltins(transform);
  356. glProgram->setUniformLocationWith1f(glProgram->getUniformLocation("u_alpha"), _displayedOpacity / 255.0);
  357. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  358. if (_dirtyGLPoint)
  359. {
  360. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  361. glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)*_bufferCapacityGLPoint, _bufferGLPoint, GL_STREAM_DRAW);
  362. _dirtyGLPoint = false;
  363. }
  364. if (Configuration::getInstance()->supportsShareableVAO())
  365. {
  366. GL::bindVAO(_vaoGLPoint);
  367. }
  368. else
  369. {
  370. glBindBuffer(GL_ARRAY_BUFFER, _vboGLPoint);
  371. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  372. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices));
  373. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors));
  374. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords));
  375. }
  376. glDrawArrays(GL_POINTS, 0, _bufferCountGLPoint);
  377. if (Configuration::getInstance()->supportsShareableVAO())
  378. {
  379. GL::bindVAO(0);
  380. }
  381. glBindBuffer(GL_ARRAY_BUFFER, 0);
  382. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,_bufferCountGLPoint);
  383. CHECK_GL_ERROR_DEBUG();
  384. }
  385. void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4F &color)
  386. {
  387. ensureCapacityGLPoint(1);
  388. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
  389. V2F_C4B_T2F a = {position, Color4B(color), Tex2F(pointSize,0)};
  390. *point = a;
  391. _bufferCountGLPoint += 1;
  392. _dirtyGLPoint = true;
  393. }
  394. void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const Color4F &color)
  395. {
  396. drawPoints(position, numberOfPoints, 1.0, color);
  397. }
  398. void DrawNode::drawPoints(const Vec2 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color)
  399. {
  400. ensureCapacityGLPoint(numberOfPoints);
  401. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLPoint + _bufferCountGLPoint);
  402. for(unsigned int i=0; i < numberOfPoints; i++,point++)
  403. {
  404. V2F_C4B_T2F a = {position[i], Color4B(color), Tex2F(pointSize,0)};
  405. *point = a;
  406. }
  407. _bufferCountGLPoint += numberOfPoints;
  408. _dirtyGLPoint = true;
  409. }
  410. void DrawNode::drawLine(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  411. {
  412. ensureCapacityGLLine(2);
  413. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
  414. V2F_C4B_T2F a = {origin, Color4B(color), Tex2F(0.0, 0.0)};
  415. V2F_C4B_T2F b = {destination, Color4B(color), Tex2F(0.0, 0.0)};
  416. *point = a;
  417. *(point+1) = b;
  418. _bufferCountGLLine += 2;
  419. _dirtyGLLine = true;
  420. }
  421. void DrawNode::drawRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  422. {
  423. drawLine(Vec2(origin.x, origin.y), Vec2(destination.x, origin.y), color);
  424. drawLine(Vec2(destination.x, origin.y), Vec2(destination.x, destination.y), color);
  425. drawLine(Vec2(destination.x, destination.y), Vec2(origin.x, destination.y), color);
  426. drawLine(Vec2(origin.x, destination.y), Vec2(origin.x, origin.y), color);
  427. }
  428. void DrawNode::drawPoly(const Vec2 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color)
  429. {
  430. unsigned int vertex_count;
  431. if(closePolygon)
  432. {
  433. vertex_count = 2 * numberOfPoints;
  434. ensureCapacityGLLine(vertex_count);
  435. }
  436. else
  437. {
  438. vertex_count = 2 * (numberOfPoints - 1);
  439. ensureCapacityGLLine(vertex_count);
  440. }
  441. V2F_C4B_T2F *point = (V2F_C4B_T2F*)(_bufferGLLine + _bufferCountGLLine);
  442. unsigned int i = 0;
  443. for(; i<numberOfPoints-1; i++)
  444. {
  445. V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
  446. V2F_C4B_T2F b = {poli[i+1], Color4B(color), Tex2F(0.0, 0.0)};
  447. *point = a;
  448. *(point+1) = b;
  449. point += 2;
  450. }
  451. if(closePolygon)
  452. {
  453. V2F_C4B_T2F a = {poli[i], Color4B(color), Tex2F(0.0, 0.0)};
  454. V2F_C4B_T2F b = {poli[0], Color4B(color), Tex2F(0.0, 0.0)};
  455. *point = a;
  456. *(point+1) = b;
  457. }
  458. _bufferCountGLLine += vertex_count;
  459. }
  460. void DrawNode::drawCircle(const Vec2& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY, const Color4F &color)
  461. {
  462. const float coef = 2.0f * (float)M_PI/segments;
  463. Vec2 *vertices = new (std::nothrow) Vec2[segments+2];
  464. if( ! vertices )
  465. return;
  466. for(unsigned int i = 0;i <= segments; i++) {
  467. float rads = i*coef;
  468. GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
  469. GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
  470. vertices[i].x = j;
  471. vertices[i].y = k;
  472. }
  473. if(drawLineToCenter)
  474. {
  475. vertices[segments+1].x = center.x;
  476. vertices[segments+1].y = center.y;
  477. drawPoly(vertices, segments+2, true, color);
  478. }
  479. else
  480. drawPoly(vertices, segments+1, true, color);
  481. CC_SAFE_DELETE_ARRAY(vertices);
  482. }
  483. void DrawNode::drawCircle(const Vec2 &center, float radius, float angle, unsigned int segments, bool drawLineToCenter, const Color4F &color)
  484. {
  485. drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color);
  486. }
  487. void DrawNode::drawQuadBezier(const Vec2 &origin, const Vec2 &control, const Vec2 &destination, unsigned int segments, const Color4F &color)
  488. {
  489. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  490. if( ! vertices )
  491. return;
  492. float t = 0.0f;
  493. for(unsigned int i = 0; i < segments; i++)
  494. {
  495. vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
  496. vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
  497. t += 1.0f / segments;
  498. }
  499. vertices[segments].x = destination.x;
  500. vertices[segments].y = destination.y;
  501. drawPoly(vertices, segments+1, false, color);
  502. CC_SAFE_DELETE_ARRAY(vertices);
  503. }
  504. void DrawNode::drawCubicBezier(const Vec2 &origin, const Vec2 &control1, const Vec2 &control2, const Vec2 &destination, unsigned int segments, const Color4F &color)
  505. {
  506. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  507. if( ! vertices )
  508. return;
  509. float t = 0;
  510. for (unsigned int i = 0; i < segments; i++)
  511. {
  512. vertices[i].x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
  513. vertices[i].y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
  514. t += 1.0f / segments;
  515. }
  516. vertices[segments].x = destination.x;
  517. vertices[segments].y = destination.y;
  518. drawPoly(vertices, segments+1, false, color);
  519. CC_SAFE_DELETE_ARRAY(vertices);
  520. }
  521. void DrawNode::drawCardinalSpline(PointArray *config, float tension, unsigned int segments, const Color4F &color)
  522. {
  523. Vec2* vertices = new (std::nothrow) Vec2[segments + 1];
  524. if( ! vertices )
  525. return;
  526. ssize_t p;
  527. float lt;
  528. float deltaT = 1.0f / config->count();
  529. for( unsigned int i=0; i < segments+1;i++) {
  530. float dt = (float)i / segments;
  531. // border
  532. if( dt == 1 ) {
  533. p = config->count() - 1;
  534. lt = 1;
  535. } else {
  536. p = dt / deltaT;
  537. lt = (dt - deltaT * (float)p) / deltaT;
  538. }
  539. // Interpolate
  540. Vec2 pp0 = config->getControlPointAtIndex(p-1);
  541. Vec2 pp1 = config->getControlPointAtIndex(p+0);
  542. Vec2 pp2 = config->getControlPointAtIndex(p+1);
  543. Vec2 pp3 = config->getControlPointAtIndex(p+2);
  544. Vec2 newPos = ccCardinalSplineAt( pp0, pp1, pp2, pp3, tension, lt);
  545. vertices[i].x = newPos.x;
  546. vertices[i].y = newPos.y;
  547. }
  548. drawPoly(vertices, segments+1, false, color);
  549. CC_SAFE_DELETE_ARRAY(vertices);
  550. }
  551. void DrawNode::drawCatmullRom(PointArray *points, unsigned int segments, const Color4F &color)
  552. {
  553. drawCardinalSpline( points, 0.5f, segments, color);
  554. }
  555. void DrawNode::drawDot(const Vec2 &pos, float radius, const Color4F &color)
  556. {
  557. unsigned int vertex_count = 2*3;
  558. ensureCapacity(vertex_count);
  559. V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), Color4B(color), Tex2F(-1.0, -1.0) };
  560. V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), Color4B(color), Tex2F(-1.0, 1.0) };
  561. V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), Color4B(color), Tex2F( 1.0, 1.0) };
  562. V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), Color4B(color), Tex2F( 1.0, -1.0) };
  563. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  564. V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
  565. V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
  566. triangles[0] = triangle0;
  567. triangles[1] = triangle1;
  568. _bufferCount += vertex_count;
  569. _dirty = true;
  570. }
  571. void DrawNode::drawRect(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Vec2& p4, const Color4F &color)
  572. {
  573. drawLine(Vec2(p1.x, p1.y), Vec2(p2.x, p2.y), color);
  574. drawLine(Vec2(p2.x, p2.y), Vec2(p3.x, p3.y), color);
  575. drawLine(Vec2(p3.x, p3.y), Vec2(p4.x, p4.y), color);
  576. drawLine(Vec2(p4.x, p4.y), Vec2(p1.x, p1.y), color);
  577. }
  578. void DrawNode::drawSegment(const Vec2 &from, const Vec2 &to, float radius, const Color4F &color)
  579. {
  580. unsigned int vertex_count = 6*3;
  581. ensureCapacity(vertex_count);
  582. Vec2 a = __v2f(from);
  583. Vec2 b = __v2f(to);
  584. Vec2 n = v2fnormalize(v2fperp(v2fsub(b, a)));
  585. Vec2 t = v2fperp(n);
  586. Vec2 nw = v2fmult(n, radius);
  587. Vec2 tw = v2fmult(t, radius);
  588. Vec2 v0 = v2fsub(b, v2fadd(nw, tw));
  589. Vec2 v1 = v2fadd(b, v2fsub(nw, tw));
  590. Vec2 v2 = v2fsub(b, nw);
  591. Vec2 v3 = v2fadd(b, nw);
  592. Vec2 v4 = v2fsub(a, nw);
  593. Vec2 v5 = v2fadd(a, nw);
  594. Vec2 v6 = v2fsub(a, v2fsub(nw, tw));
  595. Vec2 v7 = v2fadd(a, v2fadd(nw, tw));
  596. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  597. V2F_C4B_T2F_Triangle triangles0 = {
  598. {v0, Color4B(color), __t(v2fneg(v2fadd(n, t)))},
  599. {v1, Color4B(color), __t(v2fsub(n, t))},
  600. {v2, Color4B(color), __t(v2fneg(n))},
  601. };
  602. triangles[0] = triangles0;
  603. V2F_C4B_T2F_Triangle triangles1 = {
  604. {v3, Color4B(color), __t(n)},
  605. {v1, Color4B(color), __t(v2fsub(n, t))},
  606. {v2, Color4B(color), __t(v2fneg(n))},
  607. };
  608. triangles[1] = triangles1;
  609. V2F_C4B_T2F_Triangle triangles2 = {
  610. {v3, Color4B(color), __t(n)},
  611. {v4, Color4B(color), __t(v2fneg(n))},
  612. {v2, Color4B(color), __t(v2fneg(n))},
  613. };
  614. triangles[2] = triangles2;
  615. V2F_C4B_T2F_Triangle triangles3 = {
  616. {v3, Color4B(color), __t(n)},
  617. {v4, Color4B(color), __t(v2fneg(n))},
  618. {v5, Color4B(color), __t(n) },
  619. };
  620. triangles[3] = triangles3;
  621. V2F_C4B_T2F_Triangle triangles4 = {
  622. {v6, Color4B(color), __t(v2fsub(t, n))},
  623. {v4, Color4B(color), __t(v2fneg(n)) },
  624. {v5, Color4B(color), __t(n)},
  625. };
  626. triangles[4] = triangles4;
  627. V2F_C4B_T2F_Triangle triangles5 = {
  628. {v6, Color4B(color), __t(v2fsub(t, n))},
  629. {v7, Color4B(color), __t(v2fadd(n, t))},
  630. {v5, Color4B(color), __t(n)},
  631. };
  632. triangles[5] = triangles5;
  633. _bufferCount += vertex_count;
  634. _dirty = true;
  635. }
  636. void DrawNode::drawPolygon(const Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor)
  637. {
  638. CCASSERT(count >= 0, "invalid count value");
  639. bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f);
  640. auto triangle_count = outline ? (3*count - 2) : (count - 2);
  641. auto vertex_count = 3*triangle_count;
  642. ensureCapacity(vertex_count);
  643. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  644. V2F_C4B_T2F_Triangle *cursor = triangles;
  645. for (int i = 0; i < count-2; i++)
  646. {
  647. V2F_C4B_T2F_Triangle tmp = {
  648. {verts[0], Color4B(fillColor), __t(v2fzero)},
  649. {verts[i+1], Color4B(fillColor), __t(v2fzero)},
  650. {verts[i+2], Color4B(fillColor), __t(v2fzero)},
  651. };
  652. *cursor++ = tmp;
  653. }
  654. if(outline)
  655. {
  656. struct ExtrudeVerts {Vec2 offset, n;};
  657. struct ExtrudeVerts* extrude = (struct ExtrudeVerts*)malloc(sizeof(struct ExtrudeVerts)*count);
  658. memset(extrude, 0, sizeof(struct ExtrudeVerts)*count);
  659. for (int i = 0; i < count; i++)
  660. {
  661. Vec2 v0 = __v2f(verts[(i-1+count)%count]);
  662. Vec2 v1 = __v2f(verts[i]);
  663. Vec2 v2 = __v2f(verts[(i+1)%count]);
  664. Vec2 n1 = v2fnormalize(v2fperp(v2fsub(v1, v0)));
  665. Vec2 n2 = v2fnormalize(v2fperp(v2fsub(v2, v1)));
  666. Vec2 offset = v2fmult(v2fadd(n1, n2), 1.0f / (v2fdot(n1, n2) + 1.0f));
  667. struct ExtrudeVerts tmp = {offset, n2};
  668. extrude[i] = tmp;
  669. }
  670. for(int i = 0; i < count; i++)
  671. {
  672. int j = (i+1)%count;
  673. Vec2 v0 = __v2f(verts[i]);
  674. Vec2 v1 = __v2f(verts[j]);
  675. Vec2 n0 = extrude[i].n;
  676. Vec2 offset0 = extrude[i].offset;
  677. Vec2 offset1 = extrude[j].offset;
  678. Vec2 inner0 = v2fsub(v0, v2fmult(offset0, borderWidth));
  679. Vec2 inner1 = v2fsub(v1, v2fmult(offset1, borderWidth));
  680. Vec2 outer0 = v2fadd(v0, v2fmult(offset0, borderWidth));
  681. Vec2 outer1 = v2fadd(v1, v2fmult(offset1, borderWidth));
  682. V2F_C4B_T2F_Triangle tmp1 = {
  683. {inner0, Color4B(borderColor), __t(v2fneg(n0))},
  684. {inner1, Color4B(borderColor), __t(v2fneg(n0))},
  685. {outer1, Color4B(borderColor), __t(n0)}
  686. };
  687. *cursor++ = tmp1;
  688. V2F_C4B_T2F_Triangle tmp2 = {
  689. {inner0, Color4B(borderColor), __t(v2fneg(n0))},
  690. {outer0, Color4B(borderColor), __t(n0)},
  691. {outer1, Color4B(borderColor), __t(n0)}
  692. };
  693. *cursor++ = tmp2;
  694. }
  695. free(extrude);
  696. }
  697. _bufferCount += vertex_count;
  698. _dirty = true;
  699. }
  700. void DrawNode::drawSolidRect(const Vec2 &origin, const Vec2 &destination, const Color4F &color)
  701. {
  702. Vec2 vertices[] = {
  703. origin,
  704. Vec2(destination.x, origin.y),
  705. destination,
  706. Vec2(origin.x, destination.y)
  707. };
  708. drawSolidPoly(vertices, 4, color );
  709. }
  710. void DrawNode::drawSolidPoly(const Vec2 *poli, unsigned int numberOfPoints, const Color4F &color)
  711. {
  712. drawPolygon(poli, numberOfPoints, color, 0.0, Color4F(0.0, 0.0, 0.0, 0.0));
  713. }
  714. void DrawNode::drawSolidCircle(const Vec2& center, float radius, float angle, unsigned int segments, float scaleX, float scaleY, const Color4F &color)
  715. {
  716. const float coef = 2.0f * (float)M_PI/segments;
  717. Vec2 *vertices = new (std::nothrow) Vec2[segments];
  718. if( ! vertices )
  719. return;
  720. for(unsigned int i = 0;i < segments; i++)
  721. {
  722. float rads = i*coef;
  723. GLfloat j = radius * cosf(rads + angle) * scaleX + center.x;
  724. GLfloat k = radius * sinf(rads + angle) * scaleY + center.y;
  725. vertices[i].x = j;
  726. vertices[i].y = k;
  727. }
  728. drawSolidPoly(vertices, segments, color);
  729. CC_SAFE_DELETE_ARRAY(vertices);
  730. }
  731. void DrawNode::drawSolidCircle( const Vec2& center, float radius, float angle, unsigned int segments, const Color4F& color)
  732. {
  733. drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f, color);
  734. }
  735. void DrawNode::drawTriangle(const Vec2 &p1, const Vec2 &p2, const Vec2 &p3, const Color4F &color)
  736. {
  737. unsigned int vertex_count = 3;
  738. ensureCapacity(vertex_count);
  739. Color4B col = Color4B(color);
  740. V2F_C4B_T2F a = {Vec2(p1.x, p1.y), col, Tex2F(0.0, 0.0) };
  741. V2F_C4B_T2F b = {Vec2(p2.x, p2.y), col, Tex2F(0.0, 0.0) };
  742. V2F_C4B_T2F c = {Vec2(p3.x, p3.y), col, Tex2F(0.0, 0.0) };
  743. V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
  744. V2F_C4B_T2F_Triangle triangle = {a, b, c};
  745. triangles[0] = triangle;
  746. _bufferCount += vertex_count;
  747. _dirty = true;
  748. }
  749. void DrawNode::drawQuadraticBezier(const Vec2& from, const Vec2& control, const Vec2& to, unsigned int segments, const Color4F &color)
  750. {
  751. drawQuadBezier(from, control, to, segments, color);
  752. }
  753. void DrawNode::clear()
  754. {
  755. _bufferCount = 0;
  756. _dirty = true;
  757. _bufferCountGLLine = 0;
  758. _dirtyGLLine = true;
  759. _bufferCountGLPoint = 0;
  760. _dirtyGLPoint = true;
  761. _lineWidth = _defaultLineWidth;
  762. }
  763. const BlendFunc& DrawNode::getBlendFunc() const
  764. {
  765. return _blendFunc;
  766. }
  767. void DrawNode::setBlendFunc(const BlendFunc &blendFunc)
  768. {
  769. _blendFunc = blendFunc;
  770. }
  771. void DrawNode::setLineWidth(GLfloat lineWidth)
  772. {
  773. _lineWidth = lineWidth;
  774. }
  775. GLfloat DrawNode::getLineWidth()
  776. {
  777. return this->_lineWidth;
  778. }
  779. NS_CC_END