2012-11-08 17:55:25 +08:00
|
|
|
/*
|
2014-01-07 11:25:07 +08:00
|
|
|
* Copyright (c) 2012 Pierre-David Bélanger
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-11-08 17:55:25 +08:00
|
|
|
*
|
2014-01-07 11:25:07 +08:00
|
|
|
* cocos2d-x: http://www.cocos2d-x.org
|
2012-11-08 17:55:25 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCClippingNode.h"
|
|
|
|
#include "2d/CCDrawingPrimitives.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2014-09-11 17:55:47 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2015-06-13 06:03:48 +08:00
|
|
|
#include "renderer/CCRenderState.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2015-11-25 14:24:27 +08:00
|
|
|
#include "base/CCStencilStateManager.hpp"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2015-05-01 17:36:19 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
|
|
|
#define CC_CLIPPING_NODE_OPENGLES 0
|
|
|
|
#else
|
|
|
|
#define CC_CLIPPING_NODE_OPENGLES 1
|
|
|
|
#endif
|
2012-11-08 17:55:25 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-05-01 17:36:19 +08:00
|
|
|
#if CC_CLIPPING_NODE_OPENGLES
|
2013-06-20 14:13:12 +08:00
|
|
|
static void setProgram(Node *n, GLProgram *p)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2014-05-09 07:42:36 +08:00
|
|
|
n->setGLProgram(p);
|
2012-11-08 17:55:25 +08:00
|
|
|
|
2013-12-17 17:45:29 +08:00
|
|
|
auto& children = n->getChildren();
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &child : children) {
|
2013-11-28 16:02:03 +08:00
|
|
|
setProgram(child, p);
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
2015-05-01 17:36:19 +08:00
|
|
|
#endif
|
2012-11-08 17:55:25 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ClippingNode::ClippingNode()
|
2013-12-18 17:47:20 +08:00
|
|
|
: _stencil(nullptr)
|
2015-11-25 14:14:17 +08:00
|
|
|
,_stencilStateManager(new StencilStateManager())
|
2013-12-24 14:08:57 +08:00
|
|
|
{
|
|
|
|
}
|
2012-11-08 17:55:25 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ClippingNode::~ClippingNode()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2014-04-12 12:56:36 +08:00
|
|
|
if (_stencil)
|
|
|
|
{
|
|
|
|
_stencil->stopAllActions();
|
|
|
|
_stencil->release();
|
|
|
|
}
|
2015-12-14 10:40:09 +08:00
|
|
|
CC_SAFE_DELETE(_stencilStateManager);
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ClippingNode* ClippingNode::create()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ClippingNode *ret = new (std::nothrow) ClippingNode();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ClippingNode* ClippingNode::create(Node *pStencil)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ClippingNode *ret = new (std::nothrow) ClippingNode();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init(pStencil))
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ClippingNode::init()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return init(nullptr);
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-12-09 14:00:10 +08:00
|
|
|
bool ClippingNode::init(Node *stencil)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2015-12-17 21:55:47 +08:00
|
|
|
setStencil(stencil);
|
2012-11-08 17:55:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ClippingNode::onEnter()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2014-06-13 17:59:23 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2014-06-18 11:51:52 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
|
2014-06-13 17:59:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onEnter();
|
2013-12-09 14:00:10 +08:00
|
|
|
|
|
|
|
if (_stencil != nullptr)
|
|
|
|
{
|
|
|
|
_stencil->onEnter();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("ClippingNode warning: _stencil is nil.");
|
|
|
|
}
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ClippingNode::onEnterTransitionDidFinish()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2015-07-28 16:07:14 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2015-07-28 18:24:03 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnterTransitionDidFinish))
|
2015-07-28 16:07:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onEnterTransitionDidFinish();
|
2013-12-09 14:00:10 +08:00
|
|
|
|
|
|
|
if (_stencil != nullptr)
|
|
|
|
{
|
|
|
|
_stencil->onEnterTransitionDidFinish();
|
|
|
|
}
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ClippingNode::onExitTransitionDidStart()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2015-07-28 16:07:14 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2015-07-28 18:24:03 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExitTransitionDidStart))
|
2015-07-28 16:07:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-09 14:00:10 +08:00
|
|
|
if (_stencil != nullptr)
|
|
|
|
{
|
|
|
|
_stencil->onExitTransitionDidStart();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onExitTransitionDidStart();
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ClippingNode::onExit()
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2015-07-28 16:07:14 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
if (_scriptType == kScriptTypeJavascript)
|
|
|
|
{
|
2015-07-28 18:24:03 +08:00
|
|
|
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
|
2015-07-28 16:07:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-09 14:00:10 +08:00
|
|
|
if (_stencil != nullptr)
|
|
|
|
{
|
|
|
|
_stencil->onExit();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onExit();
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-10 15:33:43 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void ClippingNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2014-11-12 03:09:58 +08:00
|
|
|
if (!_visible || !hasContent())
|
2013-12-24 17:16:28 +08:00
|
|
|
return;
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
2014-03-01 03:20:53 +08:00
|
|
|
|
|
|
|
// IMPORTANT:
|
2014-05-15 01:07:09 +08:00
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
2014-03-01 03:20:53 +08:00
|
|
|
// but it is deprecated and your code should not rely on it
|
2014-04-02 22:48:22 +08:00
|
|
|
Director* director = Director::getInstance();
|
2015-09-22 16:08:23 +08:00
|
|
|
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
|
2014-04-02 22:48:22 +08:00
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2014-03-01 03:20:53 +08:00
|
|
|
|
2013-12-24 14:08:57 +08:00
|
|
|
//Add group command
|
2014-02-28 13:43:54 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_groupCommand.init(_globalZOrder);
|
2013-12-26 18:11:23 +08:00
|
|
|
renderer->addCommand(&_groupCommand);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2013-12-26 18:11:23 +08:00
|
|
|
renderer->pushGroup(_groupCommand.getRenderQueueID());
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_beforeVisitCmd.init(_globalZOrder);
|
2015-11-25 14:14:17 +08:00
|
|
|
_beforeVisitCmd.func = CC_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilStateManager);
|
2013-12-26 21:19:12 +08:00
|
|
|
renderer->addCommand(&_beforeVisitCmd);
|
2015-11-25 14:14:17 +08:00
|
|
|
|
2015-11-25 18:27:38 +08:00
|
|
|
auto alphaThreshold = this->getAlphaThreshold();
|
|
|
|
if (alphaThreshold < 1)
|
2014-01-02 16:19:23 +08:00
|
|
|
{
|
2015-05-01 17:36:19 +08:00
|
|
|
#if CC_CLIPPING_NODE_OPENGLES
|
2014-01-02 16:19:23 +08:00
|
|
|
// since glAlphaTest do not exists in OES, use a shader that writes
|
|
|
|
// pixel only if greater than an alpha threshold
|
2014-05-10 12:29:24 +08:00
|
|
|
GLProgram *program = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
|
2014-01-02 16:19:23 +08:00
|
|
|
GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
|
|
|
|
// set our alphaThreshold
|
|
|
|
program->use();
|
2015-11-25 18:27:38 +08:00
|
|
|
program->setUniformLocationWith1f(alphaValueLocation, alphaThreshold);
|
2014-01-02 16:19:23 +08:00
|
|
|
// we need to recursively apply this shader to all the nodes in the stencil node
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: we should have a way to apply shader to all nodes without having to do this
|
2014-01-02 16:19:23 +08:00
|
|
|
setProgram(_stencil, program);
|
|
|
|
|
|
|
|
#endif
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2014-01-02 16:19:23 +08:00
|
|
|
}
|
2014-05-31 07:42:05 +08:00
|
|
|
_stencil->visit(renderer, _modelViewTransform, flags);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_afterDrawStencilCmd.init(_globalZOrder);
|
2015-11-25 14:14:17 +08:00
|
|
|
_afterDrawStencilCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilStateManager);
|
2013-12-26 21:19:12 +08:00
|
|
|
renderer->addCommand(&_afterDrawStencilCmd);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2013-12-24 17:16:28 +08:00
|
|
|
int i = 0;
|
2014-08-14 10:26:37 +08:00
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
2013-12-24 17:16:28 +08:00
|
|
|
|
|
|
|
if(!_children.empty())
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for( ; i < _children.size(); i++ )
|
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
|
|
|
|
2014-01-20 15:03:30 +08:00
|
|
|
if ( node && node->getLocalZOrder() < 0 )
|
2014-05-31 07:42:05 +08:00
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
2013-12-24 17:16:28 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// self draw
|
2014-08-13 12:06:51 +08:00
|
|
|
if (visibleByCamera)
|
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
2013-12-24 17:16:28 +08:00
|
|
|
|
|
|
|
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
|
2014-05-31 07:42:05 +08:00
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
2013-12-24 17:16:28 +08:00
|
|
|
}
|
2014-08-13 12:06:51 +08:00
|
|
|
else if (visibleByCamera)
|
2013-12-24 17:16:28 +08:00
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
this->draw(renderer, _modelViewTransform, flags);
|
2013-12-24 17:16:28 +08:00
|
|
|
}
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_afterVisitCmd.init(_globalZOrder);
|
2015-11-25 14:14:17 +08:00
|
|
|
_afterVisitCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilStateManager);
|
2013-12-26 21:19:12 +08:00
|
|
|
renderer->addCommand(&_afterVisitCmd);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
|
|
|
renderer->popGroup();
|
2013-12-24 17:16:28 +08:00
|
|
|
|
2014-04-02 22:48:22 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2013-12-24 14:08:57 +08:00
|
|
|
}
|
|
|
|
|
2015-03-16 17:11:17 +08:00
|
|
|
void ClippingNode::setCameraMask(unsigned short mask, bool applyChildren)
|
|
|
|
{
|
|
|
|
Node::setCameraMask(mask, applyChildren);
|
|
|
|
|
|
|
|
if (_stencil)
|
|
|
|
_stencil->setCameraMask(mask, applyChildren);
|
|
|
|
}
|
|
|
|
|
2013-12-24 14:08:57 +08:00
|
|
|
Node* ClippingNode::getStencil() const
|
|
|
|
{
|
|
|
|
return _stencil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClippingNode::setStencil(Node *stencil)
|
|
|
|
{
|
2015-12-17 21:55:47 +08:00
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
if (_stencil)
|
|
|
|
sEngine->releaseScriptObject(this, _stencil);
|
|
|
|
if (stencil)
|
|
|
|
sEngine->retainScriptObject(this, stencil);
|
|
|
|
}
|
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2014-04-12 12:56:36 +08:00
|
|
|
CC_SAFE_RETAIN(stencil);
|
2013-12-24 14:08:57 +08:00
|
|
|
CC_SAFE_RELEASE(_stencil);
|
|
|
|
_stencil = stencil;
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:43:05 +08:00
|
|
|
bool ClippingNode::hasContent() const
|
2014-11-12 03:09:58 +08:00
|
|
|
{
|
|
|
|
return _children.size() > 0;
|
|
|
|
}
|
|
|
|
|
2013-12-24 14:08:57 +08:00
|
|
|
GLfloat ClippingNode::getAlphaThreshold() const
|
|
|
|
{
|
2015-11-25 14:14:17 +08:00
|
|
|
return _stencilStateManager->getAlphaThreshold();
|
2013-12-24 14:08:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClippingNode::setAlphaThreshold(GLfloat alphaThreshold)
|
|
|
|
{
|
2015-11-25 14:14:17 +08:00
|
|
|
_stencilStateManager->setAlphaThreshold(alphaThreshold);
|
2013-12-24 14:08:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ClippingNode::isInverted() const
|
|
|
|
{
|
2015-11-25 14:14:17 +08:00
|
|
|
return _stencilStateManager->isInverted();
|
2013-12-24 14:08:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClippingNode::setInverted(bool inverted)
|
|
|
|
{
|
2015-11-25 14:14:17 +08:00
|
|
|
_stencilStateManager->setInverted(inverted);
|
2013-12-24 14:08:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
NS_CC_END
|