axmol/cocos/2d/CCClippingNode.h

197 lines
6.1 KiB
C
Raw Normal View History

2012-11-08 17:55:25 +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
*
* 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.
*
*/
#ifndef __MISCNODE_CCCLIPPING_NODE_H__
#define __MISCNODE_CCCLIPPING_NODE_H__
#include "2d/CCNode.h"
2014-09-10 07:50:02 +08:00
#include "platform/CCGL.h"
2014-04-30 08:37:36 +08:00
#include "renderer/CCGroupCommand.h"
#include "renderer/CCCustomCommand.h"
2012-11-08 17:55:25 +08:00
NS_CC_BEGIN
2015-03-21 17:06:26 +08:00
/**
* @addtogroup _2d
* @{
*/
/** ClippingNode is a subclass of Node.
2015-03-21 17:06:26 +08:00
* It draws its content (childs) clipped using a stencil.
* The stencil is an other Node that will not be drawn.
* The clipping is done using the alpha part of the stencil (adjusted with an alphaThreshold).
2012-11-08 17:55:25 +08:00
*/
class CC_DLL ClippingNode : public Node
2012-11-08 17:55:25 +08:00
{
public:
/** Creates and initializes a clipping node without a stencil.
2015-03-21 17:06:26 +08:00
*
* @return An autorelease ClippingNode.
2012-11-08 17:55:25 +08:00
*/
static ClippingNode* create();
2012-11-08 17:55:25 +08:00
/** Creates and initializes a clipping node with an other node as its stencil.
2015-03-21 17:06:26 +08:00
* The stencil node will be retained.
* @param stencil The stencil node.
2012-11-08 17:55:25 +08:00
*/
static ClippingNode* create(Node *stencil);
/** The Node to use as a stencil to do the clipping.
2015-03-21 17:06:26 +08:00
* The stencil node will be retained.
* This default to nil.
*
* @return The stencil node.
2012-11-08 17:55:25 +08:00
*/
Node* getStencil() const;
2015-03-21 17:06:26 +08:00
/** Set the Node to use as a stencil to do the clipping.
*
* @param stencil The Node to use as a stencil to do the clipping.
*/
void setStencil(Node *stencil);
/** If stencil has no childre it will not be drawn.
2015-03-21 17:06:26 +08:00
* If you have custom stencil-based node with stencil drawing mechanics other then children-based,
* then this method should return true every time you wish stencil to be visited.
* By default returns true if has any children attached.
*
* @return If you have custom stencil-based node with stencil drawing mechanics other then children-based,
* then this method should return true every time you wish stencil to be visited.
* By default returns true if has any children attached.
* @js NA
2015-03-21 17:06:26 +08:00
*/
virtual bool hasContent() const;
2012-11-08 17:55:25 +08:00
/** The alpha threshold.
2015-03-21 17:06:26 +08:00
* The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold.
* Should be a float between 0 and 1.
* This default to 1 (so alpha test is disabled).
*
* @return The alpha threshold value,Should be a float between 0 and 1.
2012-11-08 17:55:25 +08:00
*/
2012-11-09 12:08:18 +08:00
GLfloat getAlphaThreshold() const;
2015-03-21 17:06:26 +08:00
/** Set the alpha threshold.
*
* @param alphaThreshold The alpha threshold.
*/
void setAlphaThreshold(GLfloat alphaThreshold);
2012-11-08 17:55:25 +08:00
/** Inverted. If this is set to true,
2015-03-21 17:06:26 +08:00
* the stencil is inverted, so the content is drawn where the stencil is NOT drawn.
* This default to false.
*
* @return If the clippingNode is Inverted, it will be return true.
2012-11-08 17:55:25 +08:00
*/
2012-11-09 12:08:18 +08:00
bool isInverted() const;
2015-03-21 17:06:26 +08:00
/** Set the ClippingNode whether or not invert.
*
* @param inverted A bool Type,to set the ClippingNode whether or not invert.
*/
void setInverted(bool inverted);
// Overrides
/**
* @lua NA
*/
virtual void onEnter() override;
/**
* @lua NA
*/
virtual void onEnterTransitionDidFinish() override;
/**
* @lua NA
*/
virtual void onExitTransitionDidStart() override;
/**
* @lua NA
*/
virtual void onExit() override;
virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
2015-03-16 17:11:17 +08:00
virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
CC_CONSTRUCTOR_ACCESS:
ClippingNode();
/**
* @js NA
* @lua NA
*/
virtual ~ClippingNode();
/** Initializes a clipping node without a stencil.
*/
virtual bool init() override;
/** Initializes a clipping node with an other node as its stencil.
The stencil node will be retained, and its parent will be set to this clipping node.
*/
virtual bool init(Node *stencil);
protected:
/**draw fullscreen quad to clear stencil bits
*/
void drawFullScreenQuadClearStencil();
Node* _stencil;
GLfloat _alphaThreshold;
bool _inverted;
//renderData and callback
void onBeforeVisit();
void onAfterDrawStencil();
void onAfterVisit();
GLboolean _currentStencilEnabled;
GLuint _currentStencilWriteMask;
GLenum _currentStencilFunc;
GLint _currentStencilRef;
GLuint _currentStencilValueMask;
GLenum _currentStencilFail;
GLenum _currentStencilPassDepthFail;
GLenum _currentStencilPassDepthPass;
GLboolean _currentDepthWriteMask;
GLboolean _currentAlphaTestEnabled;
GLenum _currentAlphaTestFunc;
GLclampf _currentAlphaTestRef;
GLint _mask_layer_le;
GroupCommand _groupCommand;
CustomCommand _beforeVisitCmd;
CustomCommand _afterDrawStencilCmd;
CustomCommand _afterVisitCmd;
private:
CC_DISALLOW_COPY_AND_ASSIGN(ClippingNode);
2012-11-08 17:55:25 +08:00
};
2015-03-21 17:06:26 +08:00
/** @} */
2012-11-08 17:55:25 +08:00
NS_CC_END
#endif // __MISCNODE_CCCLIPPING_NODE_H__