CCMotionStreak.cpp 11 KB

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