CCClippingNode.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2012 Pierre-David Bélanger
  3. * Copyright (c) 2012 cocos2d-x.org
  4. * Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. *
  6. * cocos2d-x: http://www.cocos2d-x.org
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "2d/CCClippingNode.h"
  28. #include "2d/CCDrawingPrimitives.h"
  29. #include "renderer/CCGLProgramCache.h"
  30. #include "renderer/ccGLStateCache.h"
  31. #include "renderer/CCRenderer.h"
  32. #include "renderer/CCRenderState.h"
  33. #include "base/CCDirector.h"
  34. #include "base/CCStencilStateManager.h"
  35. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
  36. #define CC_CLIPPING_NODE_OPENGLES 0
  37. #else
  38. #define CC_CLIPPING_NODE_OPENGLES 1
  39. #endif
  40. NS_CC_BEGIN
  41. #if CC_CLIPPING_NODE_OPENGLES
  42. static void setProgram(Node *n, GLProgram *p)
  43. {
  44. n->setGLProgram(p);
  45. auto& children = n->getChildren();
  46. for(const auto &child : children) {
  47. setProgram(child, p);
  48. }
  49. }
  50. #endif
  51. ClippingNode::ClippingNode()
  52. : _stencil(nullptr)
  53. , _stencilStateManager(new StencilStateManager())
  54. , _originStencilProgram(nullptr)
  55. {
  56. }
  57. ClippingNode::~ClippingNode()
  58. {
  59. if (_stencil)
  60. {
  61. _stencil->stopAllActions();
  62. _stencil->release();
  63. }
  64. CC_SAFE_DELETE(_stencilStateManager);
  65. }
  66. ClippingNode* ClippingNode::create()
  67. {
  68. ClippingNode *ret = new (std::nothrow) ClippingNode();
  69. if (ret && ret->init())
  70. {
  71. ret->autorelease();
  72. }
  73. else
  74. {
  75. CC_SAFE_DELETE(ret);
  76. }
  77. return ret;
  78. }
  79. ClippingNode* ClippingNode::create(Node *pStencil)
  80. {
  81. ClippingNode *ret = new (std::nothrow) ClippingNode();
  82. if (ret && ret->init(pStencil))
  83. {
  84. ret->autorelease();
  85. }
  86. else
  87. {
  88. CC_SAFE_DELETE(ret);
  89. }
  90. return ret;
  91. }
  92. bool ClippingNode::init()
  93. {
  94. return init(nullptr);
  95. }
  96. bool ClippingNode::init(Node *stencil)
  97. {
  98. setStencil(stencil);
  99. return true;
  100. }
  101. void ClippingNode::onEnter()
  102. {
  103. #if CC_ENABLE_SCRIPT_BINDING
  104. if (_scriptType == kScriptTypeJavascript)
  105. {
  106. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  107. return;
  108. }
  109. #endif
  110. Node::onEnter();
  111. if (_stencil != nullptr)
  112. {
  113. _stencil->onEnter();
  114. }
  115. else
  116. {
  117. CCLOG("ClippingNode warning: _stencil is nil.");
  118. }
  119. }
  120. void ClippingNode::onEnterTransitionDidFinish()
  121. {
  122. #if CC_ENABLE_SCRIPT_BINDING
  123. if (_scriptType == kScriptTypeJavascript)
  124. {
  125. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnterTransitionDidFinish))
  126. return;
  127. }
  128. #endif
  129. Node::onEnterTransitionDidFinish();
  130. if (_stencil != nullptr)
  131. {
  132. _stencil->onEnterTransitionDidFinish();
  133. }
  134. }
  135. void ClippingNode::onExitTransitionDidStart()
  136. {
  137. #if CC_ENABLE_SCRIPT_BINDING
  138. if (_scriptType == kScriptTypeJavascript)
  139. {
  140. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExitTransitionDidStart))
  141. return;
  142. }
  143. #endif
  144. if (_stencil != nullptr)
  145. {
  146. _stencil->onExitTransitionDidStart();
  147. }
  148. Node::onExitTransitionDidStart();
  149. }
  150. void ClippingNode::onExit()
  151. {
  152. #if CC_ENABLE_SCRIPT_BINDING
  153. if (_scriptType == kScriptTypeJavascript)
  154. {
  155. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
  156. return;
  157. }
  158. #endif
  159. if (_stencil != nullptr)
  160. {
  161. _stencil->onExit();
  162. }
  163. Node::onExit();
  164. }
  165. void ClippingNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  166. {
  167. if (!_visible || !hasContent())
  168. return;
  169. uint32_t flags = processParentFlags(parentTransform, parentFlags);
  170. // IMPORTANT:
  171. // To ease the migration to v3.0, we still support the Mat4 stack,
  172. // but it is deprecated and your code should not rely on it
  173. Director* director = Director::getInstance();
  174. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  175. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  176. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  177. //Add group command
  178. _groupCommand.init(_globalZOrder);
  179. renderer->addCommand(&_groupCommand);
  180. renderer->pushGroup(_groupCommand.getRenderQueueID());
  181. _beforeVisitCmd.init(_globalZOrder);
  182. _beforeVisitCmd.func = CC_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilStateManager);
  183. renderer->addCommand(&_beforeVisitCmd);
  184. auto alphaThreshold = this->getAlphaThreshold();
  185. if (alphaThreshold < 1)
  186. {
  187. #if CC_CLIPPING_NODE_OPENGLES
  188. // since glAlphaTest do not exists in OES, use a shader that writes
  189. // pixel only if greater than an alpha threshold
  190. GLProgram *program = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
  191. GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
  192. // set our alphaThreshold
  193. program->use();
  194. program->setUniformLocationWith1f(alphaValueLocation, alphaThreshold);
  195. // we need to recursively apply this shader to all the nodes in the stencil node
  196. // FIXME: we should have a way to apply shader to all nodes without having to do this
  197. setProgram(_stencil, program);
  198. #endif
  199. }
  200. _stencil->visit(renderer, _modelViewTransform, flags);
  201. _afterDrawStencilCmd.init(_globalZOrder);
  202. _afterDrawStencilCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilStateManager);
  203. renderer->addCommand(&_afterDrawStencilCmd);
  204. int i = 0;
  205. bool visibleByCamera = isVisitableByVisitingCamera();
  206. if(!_children.empty())
  207. {
  208. sortAllChildren();
  209. // draw children zOrder < 0
  210. for(auto size = _children.size(); i < size; ++i)
  211. {
  212. auto node = _children.at(i);
  213. if ( node && node->getLocalZOrder() < 0 )
  214. node->visit(renderer, _modelViewTransform, flags);
  215. else
  216. break;
  217. }
  218. // self draw
  219. if (visibleByCamera)
  220. this->draw(renderer, _modelViewTransform, flags);
  221. for(auto it=_children.cbegin()+i, itCend = _children.cend(); it != itCend; ++it)
  222. (*it)->visit(renderer, _modelViewTransform, flags);
  223. }
  224. else if (visibleByCamera)
  225. {
  226. this->draw(renderer, _modelViewTransform, flags);
  227. }
  228. _afterVisitCmd.init(_globalZOrder);
  229. _afterVisitCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilStateManager);
  230. renderer->addCommand(&_afterVisitCmd);
  231. renderer->popGroup();
  232. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  233. }
  234. void ClippingNode::setCameraMask(unsigned short mask, bool applyChildren)
  235. {
  236. Node::setCameraMask(mask, applyChildren);
  237. if (_stencil)
  238. _stencil->setCameraMask(mask, applyChildren);
  239. }
  240. Node* ClippingNode::getStencil() const
  241. {
  242. return _stencil;
  243. }
  244. void ClippingNode::setStencil(Node *stencil)
  245. {
  246. //early out if the stencil is already set
  247. if (_stencil == stencil)
  248. return;
  249. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  250. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  251. if (sEngine)
  252. {
  253. if (_stencil)
  254. sEngine->releaseScriptObject(this, _stencil);
  255. if (stencil)
  256. sEngine->retainScriptObject(this, stencil);
  257. }
  258. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  259. //cleanup current stencil
  260. if(_stencil != nullptr && _stencil->isRunning())
  261. {
  262. _stencil->onExitTransitionDidStart();
  263. _stencil->onExit();
  264. }
  265. CC_SAFE_RELEASE_NULL(_stencil);
  266. //initialise new stencil
  267. _stencil = stencil;
  268. CC_SAFE_RETAIN(_stencil);
  269. if(_stencil != nullptr && this->isRunning())
  270. {
  271. _stencil->onEnter();
  272. if(this->_isTransitionFinished)
  273. {
  274. _stencil->onEnterTransitionDidFinish();
  275. }
  276. }
  277. if (_stencil != nullptr)
  278. _originStencilProgram = _stencil->getGLProgram();
  279. }
  280. bool ClippingNode::hasContent() const
  281. {
  282. return _children.size() > 0;
  283. }
  284. GLfloat ClippingNode::getAlphaThreshold() const
  285. {
  286. return _stencilStateManager->getAlphaThreshold();
  287. }
  288. void ClippingNode::setAlphaThreshold(GLfloat alphaThreshold)
  289. {
  290. #if CC_CLIPPING_NODE_OPENGLES
  291. if (alphaThreshold == 1 && alphaThreshold != _stencilStateManager->getAlphaThreshold())
  292. {
  293. // should reset program used by _stencil
  294. if (_stencil)
  295. setProgram(_stencil, _originStencilProgram);
  296. }
  297. #endif
  298. _stencilStateManager->setAlphaThreshold(alphaThreshold);
  299. }
  300. bool ClippingNode::isInverted() const
  301. {
  302. return _stencilStateManager->isInverted();
  303. }
  304. void ClippingNode::setInverted(bool inverted)
  305. {
  306. _stencilStateManager->setInverted(inverted);
  307. }
  308. NS_CC_END