CCMotionStreak3D.cpp 11 KB

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