1
0

CCMotionStreak.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /****************************************************************************
  2. Copyright (c) 2011 ForzeField Studios S.L.
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "2d/CCMotionStreak.h"
  23. #include "math/CCVertex.h"
  24. #include "base/CCDirector.h"
  25. #include "renderer/CCTextureCache.h"
  26. #include "renderer/ccGLStateCache.h"
  27. #include "renderer/CCTexture2D.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "renderer/CCGLProgramState.h"
  30. NS_CC_BEGIN
  31. MotionStreak::MotionStreak()
  32. : _fastMode(false)
  33. , _startingPositionInitialized(false)
  34. , _texture(nullptr)
  35. , _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
  36. , _stroke(0.0f)
  37. , _fadeDelta(0.0f)
  38. , _minSeg(0.0f)
  39. , _maxPoints(0)
  40. , _nuPoints(0)
  41. , _previousNuPoints(0)
  42. , _pointVertexes(nullptr)
  43. , _pointState(nullptr)
  44. , _vertices(nullptr)
  45. , _colorPointer(nullptr)
  46. , _texCoords(nullptr)
  47. {
  48. }
  49. MotionStreak::~MotionStreak()
  50. {
  51. CC_SAFE_RELEASE(_texture);
  52. CC_SAFE_FREE(_pointState);
  53. CC_SAFE_FREE(_pointVertexes);
  54. CC_SAFE_FREE(_vertices);
  55. CC_SAFE_FREE(_colorPointer);
  56. CC_SAFE_FREE(_texCoords);
  57. }
  58. MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
  59. {
  60. MotionStreak *ret = new (std::nothrow) MotionStreak();
  61. if (ret && ret->initWithFade(fade, minSeg, stroke, color, path))
  62. {
  63. ret->autorelease();
  64. return ret;
  65. }
  66. CC_SAFE_DELETE(ret);
  67. return nullptr;
  68. }
  69. MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
  70. {
  71. MotionStreak *ret = new (std::nothrow) MotionStreak();
  72. if (ret && ret->initWithFade(fade, minSeg, stroke, color, texture))
  73. {
  74. ret->autorelease();
  75. return ret;
  76. }
  77. CC_SAFE_DELETE(ret);
  78. return nullptr;
  79. }
  80. bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
  81. {
  82. CCASSERT(!path.empty(), "Invalid filename");
  83. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(path);
  84. return initWithFade(fade, minSeg, stroke, color, texture);
  85. }
  86. bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
  87. {
  88. Node::setPosition(Vec2::ZERO);
  89. setAnchorPoint(Vec2::ZERO);
  90. setIgnoreAnchorPointForPosition(true);
  91. _startingPositionInitialized = false;
  92. _positionR.setZero();
  93. _fastMode = true;
  94. _minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg;
  95. _minSeg *= _minSeg;
  96. _stroke = stroke;
  97. _fadeDelta = 1.0f/fade;
  98. double fps = 1/Director::getInstance()->getAnimationInterval();
  99. _maxPoints = (int)(fade*fps)+2;
  100. _nuPoints = 0;
  101. _pointState = (float *)malloc(sizeof(float) * _maxPoints);
  102. _pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
  103. _vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2);
  104. _texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2);
  105. _colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4);
  106. // Set blend mode
  107. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  108. // shader state
  109. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texture));
  110. setTexture(texture);
  111. setColor(color);
  112. scheduleUpdate();
  113. return true;
  114. }
  115. void MotionStreak::setPosition(const Vec2& position)
  116. {
  117. if (!_startingPositionInitialized) {
  118. _startingPositionInitialized = true;
  119. }
  120. _positionR = position;
  121. }
  122. void MotionStreak::setPosition(float x, float y)
  123. {
  124. if (!_startingPositionInitialized) {
  125. _startingPositionInitialized = true;
  126. }
  127. _positionR.x = x;
  128. _positionR.y = y;
  129. }
  130. const Vec2& MotionStreak::getPosition() const
  131. {
  132. return _positionR;
  133. }
  134. void MotionStreak::getPosition(float* x, float* y) const
  135. {
  136. *x = _positionR.x;
  137. *y = _positionR.y;
  138. }
  139. float MotionStreak::getPositionX() const
  140. {
  141. return _positionR.x;
  142. }
  143. Vec3 MotionStreak::getPosition3D() const
  144. {
  145. return Vec3(_positionR.x, _positionR.y, getPositionZ());
  146. }
  147. void MotionStreak::setPositionX(float x)
  148. {
  149. if (!_startingPositionInitialized) {
  150. _startingPositionInitialized = true;
  151. }
  152. _positionR.x = x;
  153. }
  154. float MotionStreak::getPositionY() const
  155. {
  156. return _positionR.y;
  157. }
  158. void MotionStreak::setPositionY(float y)
  159. {
  160. if (!_startingPositionInitialized) {
  161. _startingPositionInitialized = true;
  162. }
  163. _positionR.y = y;
  164. }
  165. void MotionStreak::tintWithColor(const Color3B& colors)
  166. {
  167. setColor(colors);
  168. // Fast assignation
  169. for(unsigned int i = 0; i<_nuPoints*2; i++)
  170. {
  171. *((Color3B*) (_colorPointer+i*4)) = colors;
  172. }
  173. }
  174. Texture2D* MotionStreak::getTexture(void) const
  175. {
  176. return _texture;
  177. }
  178. void MotionStreak::setTexture(Texture2D *texture)
  179. {
  180. if (_texture != texture)
  181. {
  182. CC_SAFE_RETAIN(texture);
  183. CC_SAFE_RELEASE(_texture);
  184. _texture = texture;
  185. }
  186. }
  187. void MotionStreak::setBlendFunc(const BlendFunc &blendFunc)
  188. {
  189. _blendFunc = blendFunc;
  190. }
  191. const BlendFunc& MotionStreak::getBlendFunc(void) const
  192. {
  193. return _blendFunc;
  194. }
  195. void MotionStreak::setOpacity(GLubyte /*opacity*/)
  196. {
  197. CCASSERT(false, "Set opacity no supported");
  198. }
  199. GLubyte MotionStreak::getOpacity(void) const
  200. {
  201. CCASSERT(false, "Opacity no supported");
  202. return 0;
  203. }
  204. void MotionStreak::setOpacityModifyRGB(bool /*bValue*/)
  205. {
  206. }
  207. bool MotionStreak::isOpacityModifyRGB(void) const
  208. {
  209. return false;
  210. }
  211. void MotionStreak::update(float delta)
  212. {
  213. if (!_startingPositionInitialized)
  214. {
  215. return;
  216. }
  217. delta *= _fadeDelta;
  218. unsigned int newIdx, newIdx2, i, i2;
  219. unsigned int mov = 0;
  220. // Update current points
  221. for(i = 0; i<_nuPoints; i++)
  222. {
  223. _pointState[i]-=delta;
  224. if(_pointState[i] <= 0)
  225. mov++;
  226. else
  227. {
  228. newIdx = i-mov;
  229. if(mov>0)
  230. {
  231. // Move data
  232. _pointState[newIdx] = _pointState[i];
  233. // Move point
  234. _pointVertexes[newIdx] = _pointVertexes[i];
  235. // Move vertices
  236. i2 = i*2;
  237. newIdx2 = newIdx*2;
  238. _vertices[newIdx2] = _vertices[i2];
  239. _vertices[newIdx2+1] = _vertices[i2+1];
  240. // Move color
  241. i2 *= 4;
  242. newIdx2 *= 4;
  243. _colorPointer[newIdx2+0] = _colorPointer[i2+0];
  244. _colorPointer[newIdx2+1] = _colorPointer[i2+1];
  245. _colorPointer[newIdx2+2] = _colorPointer[i2+2];
  246. _colorPointer[newIdx2+4] = _colorPointer[i2+4];
  247. _colorPointer[newIdx2+5] = _colorPointer[i2+5];
  248. _colorPointer[newIdx2+6] = _colorPointer[i2+6];
  249. }else
  250. newIdx2 = newIdx*8;
  251. const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
  252. _colorPointer[newIdx2+3] = op;
  253. _colorPointer[newIdx2+7] = op;
  254. }
  255. }
  256. _nuPoints-=mov;
  257. // Append new point
  258. bool appendNewPoint = true;
  259. if(_nuPoints >= _maxPoints)
  260. {
  261. appendNewPoint = false;
  262. }
  263. else if(_nuPoints>0)
  264. {
  265. bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
  266. bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
  267. if(a1 || a2)
  268. {
  269. appendNewPoint = false;
  270. }
  271. }
  272. if(appendNewPoint)
  273. {
  274. _pointVertexes[_nuPoints] = _positionR;
  275. _pointState[_nuPoints] = 1.0f;
  276. // Color assignment
  277. const unsigned int offset = _nuPoints*8;
  278. *((Color3B*)(_colorPointer + offset)) = _displayedColor;
  279. *((Color3B*)(_colorPointer + offset+4)) = _displayedColor;
  280. // Opacity
  281. _colorPointer[offset+3] = 255;
  282. _colorPointer[offset+7] = 255;
  283. // Generate polygon
  284. if(_nuPoints > 0 && _fastMode )
  285. {
  286. if(_nuPoints > 1)
  287. {
  288. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
  289. }
  290. else
  291. {
  292. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
  293. }
  294. }
  295. _nuPoints ++;
  296. }
  297. if( ! _fastMode )
  298. {
  299. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
  300. }
  301. // Updated Tex Coords only if they are different than previous step
  302. if( _nuPoints && _previousNuPoints != _nuPoints ) {
  303. float texDelta = 1.0f / _nuPoints;
  304. for( i=0; i < _nuPoints; i++ ) {
  305. _texCoords[i*2] = Tex2F(0, texDelta*i);
  306. _texCoords[i*2+1] = Tex2F(1, texDelta*i);
  307. }
  308. _previousNuPoints = _nuPoints;
  309. }
  310. }
  311. void MotionStreak::reset()
  312. {
  313. _nuPoints = 0;
  314. }
  315. void MotionStreak::onDraw(const Mat4 &transform, uint32_t /*flags*/)
  316. {
  317. getGLProgram()->use();
  318. getGLProgram()->setUniformsForBuiltins(transform);
  319. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
  320. GL::blendFunc( _blendFunc.src, _blendFunc.dst );
  321. GL::bindTexture2D( _texture );
  322. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
  323. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
  324. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
  325. glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nuPoints*2);
  326. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _nuPoints*2);
  327. }
  328. void MotionStreak::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  329. {
  330. if(_nuPoints <= 1)
  331. return;
  332. _customCommand.init(_globalZOrder, transform, flags);
  333. _customCommand.func = CC_CALLBACK_0(MotionStreak::onDraw, this, transform, flags);
  334. renderer->addCommand(&_customCommand);
  335. }
  336. NS_CC_END