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"
|
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
|
|
|
|
|
|
|
|
static GLint g_sStencilBits = -1;
|
2013-12-24 14:08:57 +08:00
|
|
|
// store the current stencil layer (position in the stencil buffer),
|
|
|
|
// this will allow nesting up to n ClippingNode,
|
|
|
|
// where n is the number of bits of the stencil buffer.
|
2013-12-24 14:53:49 +08:00
|
|
|
static GLint s_layer = -1;
|
2012-11-08 17:55:25 +08:00
|
|
|
|
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)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _alphaThreshold(0.0f)
|
|
|
|
, _inverted(false)
|
2013-12-24 14:08:57 +08:00
|
|
|
, _currentStencilEnabled(GL_FALSE)
|
|
|
|
, _currentStencilWriteMask(~0)
|
|
|
|
, _currentStencilFunc(GL_ALWAYS)
|
|
|
|
, _currentStencilRef(0)
|
|
|
|
, _currentStencilValueMask(~0)
|
|
|
|
, _currentStencilFail(GL_KEEP)
|
|
|
|
, _currentStencilPassDepthFail(GL_KEEP)
|
|
|
|
, _currentStencilPassDepthPass(GL_KEEP)
|
|
|
|
, _currentDepthWriteMask(GL_TRUE)
|
|
|
|
, _currentAlphaTestEnabled(GL_FALSE)
|
|
|
|
, _currentAlphaTestFunc(GL_ALWAYS)
|
|
|
|
, _currentAlphaTestRef(1)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
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();
|
|
|
|
}
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_stencil);
|
2013-12-09 14:00:10 +08:00
|
|
|
_stencil = stencil;
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RETAIN(_stencil);
|
2012-11-08 17:55:25 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_alphaThreshold = 1;
|
|
|
|
_inverted = false;
|
2012-11-08 17:55:25 +08:00
|
|
|
// get (only once) the number of bits of the stencil buffer
|
|
|
|
static bool once = true;
|
|
|
|
if (once)
|
|
|
|
{
|
|
|
|
glGetIntegerv(GL_STENCIL_BITS, &g_sStencilBits);
|
|
|
|
if (g_sStencilBits <= 0)
|
|
|
|
{
|
|
|
|
CCLOG("Stencil buffer is not enabled.");
|
|
|
|
}
|
|
|
|
once = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
void ClippingNode::drawFullScreenQuadClearStencil()
|
|
|
|
{
|
2014-04-02 22:48:22 +08:00
|
|
|
Director* director = Director::getInstance();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
|
|
|
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2013-07-10 15:33:43 +08:00
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
Vec2 vertices[] = {
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2(-1.0f, -1.0f),
|
|
|
|
Vec2(1.0f, -1.0f),
|
|
|
|
Vec2(1.0f, 1.0f),
|
|
|
|
Vec2(-1.0f, 1.0f)
|
2014-09-11 15:39:56 +08:00
|
|
|
};
|
2013-07-10 15:33:43 +08:00
|
|
|
|
2014-09-12 07:33:34 +08:00
|
|
|
auto glProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_U_COLOR);
|
2014-09-11 15:39:56 +08:00
|
|
|
|
2014-09-12 07:33:34 +08:00
|
|
|
int colorLocation = glProgram->getUniformLocation("u_color");
|
2014-09-11 15:39:56 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
Color4F color(1, 1, 1, 1);
|
|
|
|
|
2014-09-12 07:33:34 +08:00
|
|
|
glProgram->use();
|
|
|
|
glProgram->setUniformsForBuiltins();
|
|
|
|
glProgram->setUniformLocationWith4fv(colorLocation, (GLfloat*) &color.r, 1);
|
2015-01-29 18:06:20 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2014-09-11 15:39:56 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
|
2014-09-16 14:50:00 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
2014-09-11 15:39:56 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
2013-07-10 15:33:43 +08:00
|
|
|
|
2014-04-02 22:48:22 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
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();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
|
|
|
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);
|
2013-12-26 21:19:12 +08:00
|
|
|
_beforeVisitCmd.func = CC_CALLBACK_0(ClippingNode::onBeforeVisit, this);
|
|
|
|
renderer->addCommand(&_beforeVisitCmd);
|
2014-01-02 16:19:23 +08:00
|
|
|
if (_alphaThreshold < 1)
|
|
|
|
{
|
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();
|
|
|
|
program->setUniformLocationWith1f(alphaValueLocation, _alphaThreshold);
|
|
|
|
// 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);
|
2013-12-26 21:19:12 +08:00
|
|
|
_afterDrawStencilCmd.func = CC_CALLBACK_0(ClippingNode::onAfterDrawStencil, this);
|
|
|
|
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);
|
2013-12-26 21:19:12 +08:00
|
|
|
_afterVisitCmd.func = CC_CALLBACK_0(ClippingNode::onAfterVisit, this);
|
|
|
|
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)
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
return _alphaThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClippingNode::setAlphaThreshold(GLfloat alphaThreshold)
|
|
|
|
{
|
|
|
|
_alphaThreshold = alphaThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClippingNode::isInverted() const
|
|
|
|
{
|
|
|
|
return _inverted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClippingNode::setInverted(bool inverted)
|
|
|
|
{
|
|
|
|
_inverted = inverted;
|
|
|
|
}
|
|
|
|
|
2013-12-24 14:53:49 +08:00
|
|
|
void ClippingNode::onBeforeVisit()
|
2013-12-24 14:08:57 +08:00
|
|
|
{
|
2012-11-08 17:55:25 +08:00
|
|
|
///////////////////////////////////
|
|
|
|
// INIT
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// increment the current layer
|
2013-12-24 14:53:49 +08:00
|
|
|
s_layer++;
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// mask of the current layer (ie: for layer 3: 00000100)
|
2013-12-24 14:53:49 +08:00
|
|
|
GLint mask_layer = 0x1 << s_layer;
|
2012-11-08 17:55:25 +08:00
|
|
|
// mask of all layers less than the current (ie: for layer 3: 00000011)
|
|
|
|
GLint mask_layer_l = mask_layer - 1;
|
|
|
|
// mask of all layers less than or equal to the current (ie: for layer 3: 00000111)
|
2013-12-24 14:08:57 +08:00
|
|
|
_mask_layer_le = mask_layer | mask_layer_l;
|
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// manually save the stencil state
|
2013-12-24 14:08:57 +08:00
|
|
|
|
|
|
|
_currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST);
|
|
|
|
glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)&_currentStencilWriteMask);
|
|
|
|
glGetIntegerv(GL_STENCIL_FUNC, (GLint *)&_currentStencilFunc);
|
|
|
|
glGetIntegerv(GL_STENCIL_REF, &_currentStencilRef);
|
|
|
|
glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)&_currentStencilValueMask);
|
|
|
|
glGetIntegerv(GL_STENCIL_FAIL, (GLint *)&_currentStencilFail);
|
|
|
|
glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)&_currentStencilPassDepthFail);
|
|
|
|
glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)&_currentStencilPassDepthPass);
|
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// enable stencil use
|
|
|
|
glEnable(GL_STENCIL_TEST);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilTest(true);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// check for OpenGL error while enabling stencil test
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// all bits on the stencil buffer are readonly, except the current layer bit,
|
|
|
|
// this means that operation like glClear or glStencilOp will be masked with this value
|
|
|
|
glStencilMask(mask_layer);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilWrite(mask_layer);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// manually save the depth test state
|
2013-12-24 14:08:57 +08:00
|
|
|
|
|
|
|
glGetBooleanv(GL_DEPTH_WRITEMASK, &_currentDepthWriteMask);
|
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// disable depth test while drawing the stencil
|
|
|
|
//glDisable(GL_DEPTH_TEST);
|
|
|
|
// disable update to the depth buffer while drawing the stencil,
|
|
|
|
// as the stencil is not meant to be rendered in the real scene,
|
|
|
|
// it should never prevent something else to be drawn,
|
|
|
|
// only disabling depth buffer update should do
|
|
|
|
glDepthMask(GL_FALSE);
|
2015-06-13 06:37:29 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setDepthWrite(false);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
///////////////////////////////////
|
|
|
|
// CLEAR STENCIL BUFFER
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// manually clear the stencil buffer by drawing a fullscreen rectangle on it
|
|
|
|
// setup the stencil test func like this:
|
|
|
|
// for each pixel in the fullscreen rectangle
|
|
|
|
// never draw it into the frame buffer
|
|
|
|
// if not in inverted mode: set the current layer value to 0 in the stencil buffer
|
|
|
|
// if in inverted mode: set the current layer value to 1 in the stencil buffer
|
|
|
|
glStencilFunc(GL_NEVER, mask_layer, mask_layer);
|
2013-06-15 14:03:30 +08:00
|
|
|
glStencilOp(!_inverted ? GL_ZERO : GL_REPLACE, GL_KEEP, GL_KEEP);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// draw a fullscreen solid rectangle to clear the stencil buffer
|
2014-05-15 01:07:09 +08:00
|
|
|
//ccDrawSolidRect(Vec2::ZERO, ccpFromSize([[Director sharedDirector] winSize]), Color4F(1, 1, 1, 1));
|
2013-07-10 15:33:43 +08:00
|
|
|
drawFullScreenQuadClearStencil();
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
///////////////////////////////////
|
|
|
|
// DRAW CLIPPING STENCIL
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// setup the stencil test func like this:
|
|
|
|
// for each pixel in the stencil node
|
|
|
|
// never draw it into the frame buffer
|
|
|
|
// if not in inverted mode: set the current layer value to 1 in the stencil buffer
|
|
|
|
// if in inverted mode: set the current layer value to 0 in the stencil buffer
|
|
|
|
glStencilFunc(GL_NEVER, mask_layer, mask_layer);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilFunction(RenderState::STENCIL_NEVER, mask_layer, mask_layer);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glStencilOp(!_inverted ? GL_REPLACE : GL_ZERO, GL_KEEP, GL_KEEP);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilOperation(
|
|
|
|
// !_inverted ? RenderState::STENCIL_OP_REPLACE : RenderState::STENCIL_OP_ZERO,
|
|
|
|
// RenderState::STENCIL_OP_KEEP,
|
|
|
|
// RenderState::STENCIL_OP_KEEP);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// enable alpha test only if the alpha threshold < 1,
|
|
|
|
// indeed if alpha threshold == 1, every pixel will be drawn anyways
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_alphaThreshold < 1) {
|
2015-05-01 17:36:19 +08:00
|
|
|
#if !CC_CLIPPING_NODE_OPENGLES
|
2012-11-08 17:55:25 +08:00
|
|
|
// manually save the alpha test state
|
2013-12-24 14:08:57 +08:00
|
|
|
_currentAlphaTestEnabled = glIsEnabled(GL_ALPHA_TEST);
|
|
|
|
glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint *)&_currentAlphaTestFunc);
|
|
|
|
glGetFloatv(GL_ALPHA_TEST_REF, &_currentAlphaTestRef);
|
2012-11-08 17:55:25 +08:00
|
|
|
// enable alpha testing
|
|
|
|
glEnable(GL_ALPHA_TEST);
|
|
|
|
// check for OpenGL error while enabling alpha test
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
// pixel will be drawn only if greater than an alpha threshold
|
2013-06-15 14:03:30 +08:00
|
|
|
glAlphaFunc(GL_GREATER, _alphaThreshold);
|
2012-11-08 17:55:25 +08:00
|
|
|
#endif
|
|
|
|
}
|
2013-12-24 14:08:57 +08:00
|
|
|
|
|
|
|
//Draw _stencil
|
|
|
|
}
|
|
|
|
|
2013-12-24 14:53:49 +08:00
|
|
|
void ClippingNode::onAfterDrawStencil()
|
2013-12-24 14:08:57 +08:00
|
|
|
{
|
2012-11-08 17:55:25 +08:00
|
|
|
// restore alpha test state
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_alphaThreshold < 1)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
2015-05-01 17:36:19 +08:00
|
|
|
#if CC_CLIPPING_NODE_OPENGLES
|
|
|
|
// FIXME: we need to find a way to restore the shaders of the stencil node and its childs
|
|
|
|
#else
|
2012-11-08 17:55:25 +08:00
|
|
|
// manually restore the alpha test state
|
2013-12-24 14:08:57 +08:00
|
|
|
glAlphaFunc(_currentAlphaTestFunc, _currentAlphaTestRef);
|
|
|
|
if (!_currentAlphaTestEnabled)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
|
|
|
glDisable(GL_ALPHA_TEST);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// restore the depth test state
|
2013-12-24 14:08:57 +08:00
|
|
|
glDepthMask(_currentDepthWriteMask);
|
2015-07-07 23:01:24 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setDepthWrite(_currentDepthWriteMask != 0);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
//if (currentDepthTestEnabled) {
|
|
|
|
// glEnable(GL_DEPTH_TEST);
|
|
|
|
//}
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
///////////////////////////////////
|
|
|
|
// DRAW CONTENT
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// setup the stencil test func like this:
|
|
|
|
// for each pixel of this node and its childs
|
|
|
|
// if all layers less than or equals to the current are set to 1 in the stencil buffer
|
|
|
|
// draw the pixel and keep the current layer in the stencil buffer
|
|
|
|
// else
|
|
|
|
// do not draw the pixel but keep the current layer in the stencil buffer
|
2013-12-24 14:08:57 +08:00
|
|
|
glStencilFunc(GL_EQUAL, _mask_layer_le, _mask_layer_le);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilFunction(RenderState::STENCIL_EQUAL, _mask_layer_le, _mask_layer_le);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilOperation(RenderState::STENCIL_OP_KEEP, RenderState::STENCIL_OP_KEEP, RenderState::STENCIL_OP_KEEP);
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// draw (according to the stencil test func) this node and its childs
|
2013-12-24 14:08:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-24 14:53:49 +08:00
|
|
|
void ClippingNode::onAfterVisit()
|
2013-12-24 14:08:57 +08:00
|
|
|
{
|
2012-11-08 17:55:25 +08:00
|
|
|
///////////////////////////////////
|
|
|
|
// CLEANUP
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// manually restore the stencil state
|
2013-12-24 14:08:57 +08:00
|
|
|
glStencilFunc(_currentStencilFunc, _currentStencilRef, _currentStencilValueMask);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilFunction((RenderState::StencilFunction)_currentStencilFunc, _currentStencilRef, _currentStencilValueMask);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2013-12-24 14:08:57 +08:00
|
|
|
glStencilOp(_currentStencilFail, _currentStencilPassDepthFail, _currentStencilPassDepthPass);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilOperation((RenderState::StencilOperation)_currentStencilFail,
|
|
|
|
// (RenderState::StencilOperation)_currentStencilPassDepthFail,
|
|
|
|
// (RenderState::StencilOperation)_currentStencilPassDepthPass);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2013-12-24 14:08:57 +08:00
|
|
|
glStencilMask(_currentStencilWriteMask);
|
|
|
|
if (!_currentStencilEnabled)
|
2012-11-08 17:55:25 +08:00
|
|
|
{
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
2015-07-01 14:06:37 +08:00
|
|
|
// RenderState::StateBlock::_defaultState->setStencilTest(false);
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
2013-12-24 14:08:57 +08:00
|
|
|
|
2012-11-08 17:55:25 +08:00
|
|
|
// we are done using this layer, decrement
|
2013-12-24 14:53:49 +08:00
|
|
|
s_layer--;
|
2012-11-08 17:55:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|