2012-11-21 15:22:54 +08:00
//
2014-08-28 01:54:15 +08:00
// ClippingNodeTest
2012-11-21 15:22:54 +08:00
//
//
// by Pierre-David Bélanger
//
# include "ClippingNodeTest.h"
# include "../testResource.h"
2014-04-30 08:37:36 +08:00
# include "renderer/CCRenderer.h"
2012-11-21 15:22:54 +08:00
2015-04-09 08:37:30 +08:00
USING_NS_CC ;
2012-11-21 15:22:54 +08:00
enum {
kTagTitleLabel = 1 ,
kTagSubtitleLabel = 2 ,
kTagStencilNode = 100 ,
kTagClipperNode = 101 ,
kTagContentNode = 102 ,
} ;
2015-04-03 14:31:03 +08:00
ClippingNodeTests : : ClippingNodeTests ( )
{
ADD_TEST_CASE ( ScrollViewDemo ) ;
ADD_TEST_CASE ( HoleDemo ) ;
ADD_TEST_CASE ( ShapeTest ) ;
ADD_TEST_CASE ( ShapeInvertedTest ) ;
ADD_TEST_CASE ( SpriteTest ) ;
ADD_TEST_CASE ( SpriteNoAlphaTest ) ;
ADD_TEST_CASE ( SpriteInvertedTest ) ;
ADD_TEST_CASE ( NestedTest ) ;
ADD_TEST_CASE ( RawStencilBufferTest ) ;
ADD_TEST_CASE ( RawStencilBufferTest2 ) ;
ADD_TEST_CASE ( RawStencilBufferTest3 ) ;
ADD_TEST_CASE ( RawStencilBufferTest4 ) ;
ADD_TEST_CASE ( RawStencilBufferTest5 ) ;
ADD_TEST_CASE ( RawStencilBufferTest6 ) ;
ADD_TEST_CASE ( ClippingToRenderTextureTest ) ;
ADD_TEST_CASE ( ClippingRectangleNodeTest ) ;
2012-11-21 15:22:54 +08:00
}
2014-03-26 14:54:50 +08:00
//// Demo examples start here
2012-11-21 15:22:54 +08:00
//@implementation BaseClippingNodeTest
bool BaseClippingNodeTest : : init ( )
{
2015-04-03 14:31:03 +08:00
if ( TestCase : : init ( ) ) {
2012-11-21 15:22:54 +08:00
2013-08-16 16:05:27 +08:00
auto background = Sprite : : create ( s_back3 ) ;
2014-05-15 01:07:09 +08:00
background - > setAnchorPoint ( Vec2 : : ZERO ) ;
background - > setPosition ( Vec2 : : ZERO ) ;
2012-11-21 15:22:54 +08:00
this - > addChild ( background , - 1 ) ;
this - > setup ( ) ;
return true ;
}
return false ;
}
BaseClippingNodeTest : : ~ BaseClippingNodeTest ( )
{
2013-11-07 21:48:39 +08:00
Director : : getInstance ( ) - > getTextureCache ( ) - > removeUnusedTextures ( ) ;
2012-11-21 15:22:54 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string BaseClippingNodeTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Clipping Demo " ;
}
void BaseClippingNodeTest : : setup ( )
{
}
2014-03-26 14:54:50 +08:00
// BasicTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string BasicTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Basic Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string BasicTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " " ;
}
void BasicTest : : setup ( )
{
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-11-21 15:22:54 +08:00
2013-08-16 16:05:27 +08:00
auto stencil = this - > stencil ( ) ;
2012-11-21 15:22:54 +08:00
stencil - > setTag ( kTagStencilNode ) ;
2014-08-28 11:41:18 +08:00
stencil - > setPosition ( 50 , 50 ) ;
2012-11-21 15:22:54 +08:00
2013-08-16 16:05:27 +08:00
auto clipper = this - > clipper ( ) ;
2012-11-21 15:22:54 +08:00
clipper - > setTag ( kTagClipperNode ) ;
2014-05-15 01:07:09 +08:00
clipper - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
2014-08-28 11:41:18 +08:00
clipper - > setPosition ( s . width / 2 - 50 , s . height / 2 - 50 ) ;
2012-11-21 15:22:54 +08:00
clipper - > setStencil ( stencil ) ;
this - > addChild ( clipper ) ;
2013-08-16 16:05:27 +08:00
auto content = this - > content ( ) ;
2014-08-28 11:41:18 +08:00
content - > setPosition ( 50 , 50 ) ;
2012-11-21 15:22:54 +08:00
clipper - > addChild ( content ) ;
}
2013-06-20 14:17:10 +08:00
Action * BasicTest : : actionRotate ( )
2012-11-21 15:22:54 +08:00
{
2013-06-20 14:17:10 +08:00
return RepeatForever : : create ( RotateBy : : create ( 1.0f , 90.0f ) ) ;
2012-11-21 15:22:54 +08:00
}
2013-06-20 14:17:10 +08:00
Action * BasicTest : : actionScale ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto scale = ScaleBy : : create ( 1.33f , 1.5f ) ;
2014-07-10 00:45:27 +08:00
return RepeatForever : : create ( Sequence : : create ( scale , scale - > reverse ( ) , nullptr ) ) ;
2012-11-21 15:22:54 +08:00
}
2013-06-20 14:17:10 +08:00
DrawNode * BasicTest : : shape ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto shape = DrawNode : : create ( ) ;
2014-05-15 01:07:09 +08:00
static Vec2 triangle [ 3 ] ;
triangle [ 0 ] = Vec2 ( - 100 , - 100 ) ;
triangle [ 1 ] = Vec2 ( 100 , - 100 ) ;
triangle [ 2 ] = Vec2 ( 0 , 100 ) ;
2012-11-21 15:22:54 +08:00
2013-07-07 21:08:14 +08:00
static Color4F green ( 0 , 1 , 0 , 1 ) ;
2012-11-21 15:22:54 +08:00
shape - > drawPolygon ( triangle , 3 , green , 0 , green ) ;
return shape ;
}
2013-06-20 14:17:10 +08:00
Sprite * BasicTest : : grossini ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto grossini = Sprite : : create ( s_pathGrossini ) ;
2012-11-21 15:22:54 +08:00
grossini - > setScale ( 1.5 ) ;
return grossini ;
}
2013-06-20 14:17:10 +08:00
Node * BasicTest : : stencil ( )
2012-11-21 15:22:54 +08:00
{
2014-07-10 00:45:27 +08:00
return nullptr ;
2012-11-21 15:22:54 +08:00
}
2013-06-20 14:17:10 +08:00
ClippingNode * BasicTest : : clipper ( )
2012-11-21 15:22:54 +08:00
{
2013-06-20 14:17:10 +08:00
return ClippingNode : : create ( ) ;
2012-11-21 15:22:54 +08:00
}
2013-06-20 14:17:10 +08:00
Node * BasicTest : : content ( )
2012-11-21 15:22:54 +08:00
{
2014-07-10 00:45:27 +08:00
return nullptr ;
2012-11-21 15:22:54 +08:00
}
2014-03-26 14:54:50 +08:00
// ShapeTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string ShapeTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Shape Basic Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string ShapeTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " A DrawNode as stencil and Sprite as content " ;
}
2013-06-20 14:17:10 +08:00
Node * ShapeTest : : stencil ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto node = this - > shape ( ) ;
2012-11-21 15:22:54 +08:00
node - > runAction ( this - > actionRotate ( ) ) ;
return node ;
}
2013-06-20 14:17:10 +08:00
Node * ShapeTest : : content ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto node = this - > grossini ( ) ;
2012-11-21 15:22:54 +08:00
node - > runAction ( this - > actionScale ( ) ) ;
return node ;
}
2014-03-26 14:54:50 +08:00
// ShapeInvertedTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string ShapeInvertedTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Shape Inverted Basic Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string ShapeInvertedTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " A DrawNode as stencil and Sprite as content, inverted " ;
}
2013-06-20 14:17:10 +08:00
ClippingNode * ShapeInvertedTest : : clipper ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto clipper = ShapeTest : : clipper ( ) ;
2012-11-21 15:22:54 +08:00
clipper - > setInverted ( true ) ;
return clipper ;
}
2014-03-26 14:54:50 +08:00
// SpriteTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string SpriteTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Sprite Basic Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string SpriteTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " A Sprite as stencil and DrawNode as content " ;
}
2013-06-20 14:17:10 +08:00
Node * SpriteTest : : stencil ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto node = this - > grossini ( ) ;
2012-11-21 15:22:54 +08:00
node - > runAction ( this - > actionRotate ( ) ) ;
return node ;
}
2013-06-20 14:17:10 +08:00
ClippingNode * SpriteTest : : clipper ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto clipper = BasicTest : : clipper ( ) ;
2012-11-21 15:22:54 +08:00
clipper - > setAlphaThreshold ( 0.05f ) ;
return clipper ;
}
2013-06-20 14:17:10 +08:00
Node * SpriteTest : : content ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto node = this - > shape ( ) ;
2012-11-21 15:22:54 +08:00
node - > runAction ( this - > actionScale ( ) ) ;
return node ;
}
2014-03-26 14:54:50 +08:00
// SpriteNoAlphaTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string SpriteNoAlphaTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Sprite No Alpha Basic Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string SpriteNoAlphaTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " A Sprite as stencil and DrawNode as content, no alpha " ;
}
2013-06-20 14:17:10 +08:00
ClippingNode * SpriteNoAlphaTest : : clipper ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto clipper = SpriteTest : : clipper ( ) ;
2012-11-21 15:22:54 +08:00
clipper - > setAlphaThreshold ( 1 ) ;
return clipper ;
}
2014-03-26 14:54:50 +08:00
// SpriteInvertedTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string SpriteInvertedTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Sprite Inverted Basic Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string SpriteInvertedTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " A Sprite as stencil and DrawNode as content, inverted " ;
}
2013-06-20 14:17:10 +08:00
ClippingNode * SpriteInvertedTest : : clipper ( )
2012-11-21 15:22:54 +08:00
{
2013-08-16 16:05:27 +08:00
auto clipper = SpriteTest : : clipper ( ) ;
2012-11-21 15:22:54 +08:00
clipper - > setAlphaThreshold ( 0.05f ) ;
clipper - > setInverted ( true ) ;
return clipper ;
}
2014-03-26 14:54:50 +08:00
// NestedTest
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string NestedTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Nested Test " ;
}
2013-12-19 05:52:10 +08:00
std : : string NestedTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " Nest 9 Clipping Nodes, max is usually 8 " ;
}
void NestedTest : : setup ( )
{
static int depth = 9 ;
2013-08-16 16:05:27 +08:00
Node * parent = this ;
2012-11-21 15:22:54 +08:00
for ( int i = 0 ; i < depth ; i + + ) {
int size = 225 - i * ( 225 / ( depth * 2 ) ) ;
2013-08-16 16:05:27 +08:00
auto clipper = ClippingNode : : create ( ) ;
2013-07-12 14:30:26 +08:00
clipper - > setContentSize ( Size ( size , size ) ) ;
2014-05-15 01:07:09 +08:00
clipper - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
2014-08-28 11:41:18 +08:00
clipper - > setPosition ( parent - > getContentSize ( ) . width / 2 , parent - > getContentSize ( ) . height / 2 ) ;
2012-11-21 15:22:54 +08:00
clipper - > setAlphaThreshold ( 0.05f ) ;
2013-06-20 14:17:10 +08:00
clipper - > runAction ( RepeatForever : : create ( RotateBy : : create ( i % 3 ? 1.33 : 1.66 , i % 2 ? 90 : - 90 ) ) ) ;
2012-11-21 15:22:54 +08:00
parent - > addChild ( clipper ) ;
2013-08-16 16:05:27 +08:00
auto stencil = Sprite : : create ( s_pathGrossini ) ;
2012-11-21 15:22:54 +08:00
stencil - > setScale ( 2.5 - ( i * ( 2.5 / depth ) ) ) ;
2014-05-15 01:07:09 +08:00
stencil - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
2014-08-28 11:41:18 +08:00
stencil - > setPosition ( clipper - > getContentSize ( ) . width / 2 , clipper - > getContentSize ( ) . height / 2 ) ;
2012-11-21 15:22:54 +08:00
stencil - > setVisible ( false ) ;
2013-06-20 14:17:10 +08:00
stencil - > runAction ( Sequence : : createWithTwoActions ( DelayTime : : create ( i ) , Show : : create ( ) ) ) ;
2012-11-21 15:22:54 +08:00
clipper - > setStencil ( stencil ) ;
clipper - > addChild ( stencil ) ;
parent = clipper ;
}
}
2014-03-26 14:54:50 +08:00
// HoleDemo
2012-11-21 15:22:54 +08:00
HoleDemo : : ~ HoleDemo ( )
{
2013-06-15 14:03:30 +08:00
CC_SAFE_RELEASE ( _outerClipper ) ;
CC_SAFE_RELEASE ( _holes ) ;
CC_SAFE_RELEASE ( _holesStencil ) ;
2012-11-21 15:22:54 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string HoleDemo : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Hole Demo " ;
}
2013-12-19 05:52:10 +08:00
std : : string HoleDemo : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " Touch/click to poke holes " ;
}
void HoleDemo : : setup ( )
{
2013-08-16 16:05:27 +08:00
auto target = Sprite : : create ( s_pathBlock ) ;
2014-05-15 01:07:09 +08:00
target - > setAnchorPoint ( Vec2 : : ZERO ) ;
2012-11-21 15:22:54 +08:00
target - > setScale ( 3 ) ;
2013-06-20 14:17:10 +08:00
_outerClipper = ClippingNode : : create ( ) ;
2013-06-15 14:03:30 +08:00
_outerClipper - > retain ( ) ;
2013-08-27 09:08:21 +08:00
AffineTransform tranform = AffineTransform : : IDENTITY ;
2013-06-20 14:17:10 +08:00
tranform = AffineTransformScale ( tranform , target - > getScale ( ) , target - > getScale ( ) ) ;
2012-11-21 15:22:54 +08:00
2013-06-20 14:17:10 +08:00
_outerClipper - > setContentSize ( SizeApplyAffineTransform ( target - > getContentSize ( ) , tranform ) ) ;
2014-05-15 01:07:09 +08:00
_outerClipper - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
_outerClipper - > setPosition ( Vec2 ( this - > getContentSize ( ) ) * 0.5f ) ;
2013-06-20 14:17:10 +08:00
_outerClipper - > runAction ( RepeatForever : : create ( RotateBy : : create ( 1 , 45 ) ) ) ;
2012-11-21 15:22:54 +08:00
2013-06-15 14:03:30 +08:00
_outerClipper - > setStencil ( target ) ;
2012-11-21 15:22:54 +08:00
2013-08-16 16:05:27 +08:00
auto holesClipper = ClippingNode : : create ( ) ;
2012-11-21 15:22:54 +08:00
holesClipper - > setInverted ( true ) ;
holesClipper - > setAlphaThreshold ( 0.05f ) ;
holesClipper - > addChild ( target ) ;
2013-06-20 14:17:10 +08:00
_holes = Node : : create ( ) ;
2013-06-15 14:03:30 +08:00
_holes - > retain ( ) ;
2012-11-21 15:22:54 +08:00
2013-06-15 14:03:30 +08:00
holesClipper - > addChild ( _holes ) ;
2012-11-21 15:22:54 +08:00
2013-06-20 14:17:10 +08:00
_holesStencil = Node : : create ( ) ;
2013-06-15 14:03:30 +08:00
_holesStencil - > retain ( ) ;
2012-11-21 15:22:54 +08:00
2013-06-15 14:03:30 +08:00
holesClipper - > setStencil ( _holesStencil ) ;
2012-11-21 15:22:54 +08:00
2013-06-15 14:03:30 +08:00
_outerClipper - > addChild ( holesClipper ) ;
2012-11-21 15:22:54 +08:00
2013-06-15 14:03:30 +08:00
this - > addChild ( _outerClipper ) ;
2013-10-12 14:02:01 +08:00
2013-10-23 11:27:24 +08:00
auto listener = EventListenerTouchAllAtOnce : : create ( ) ;
listener - > onTouchesBegan = CC_CALLBACK_2 ( HoleDemo : : onTouchesBegan , this ) ;
2013-10-26 15:04:01 +08:00
_eventDispatcher - > addEventListenerWithSceneGraphPriority ( listener , this ) ;
2012-11-21 15:22:54 +08:00
}
2014-05-15 01:07:09 +08:00
void HoleDemo : : pokeHoleAtPoint ( Vec2 point )
2012-11-21 15:22:54 +08:00
{
float scale = CCRANDOM_0_1 ( ) * 0.2 + 0.9 ;
float rotation = CCRANDOM_0_1 ( ) * 360 ;
2013-08-16 16:05:27 +08:00
auto hole = Sprite : : create ( " Images/hole_effect.png " ) ;
2012-11-21 15:22:54 +08:00
hole - > setPosition ( point ) ;
hole - > setRotation ( rotation ) ;
hole - > setScale ( scale ) ;
2013-06-15 14:03:30 +08:00
_holes - > addChild ( hole ) ;
2012-11-21 15:22:54 +08:00
2013-08-16 16:05:27 +08:00
auto holeStencil = Sprite : : create ( " Images/hole_stencil.png " ) ;
2012-11-21 15:22:54 +08:00
holeStencil - > setPosition ( point ) ;
holeStencil - > setRotation ( rotation ) ;
holeStencil - > setScale ( scale ) ;
2013-06-15 14:03:30 +08:00
_holesStencil - > addChild ( holeStencil ) ;
2012-11-21 15:22:54 +08:00
2013-06-20 14:17:10 +08:00
_outerClipper - > runAction ( Sequence : : createWithTwoActions ( ScaleBy : : create ( 0.05f , 0.95f ) ,
ScaleTo : : create ( 0.125f , 1 ) ) ) ;
2012-11-21 15:22:54 +08:00
}
2013-09-03 18:22:03 +08:00
void HoleDemo : : onTouchesBegan ( const std : : vector < Touch * > & touches , Event * event )
2012-11-21 15:22:54 +08:00
{
2013-09-03 18:22:03 +08:00
Touch * touch = ( Touch * ) touches [ 0 ] ;
2014-05-15 01:07:09 +08:00
Vec2 point = _outerClipper - > convertToNodeSpace ( Director : : getInstance ( ) - > convertToGL ( touch - > getLocationInView ( ) ) ) ;
2013-08-16 16:05:27 +08:00
auto rect = Rect ( 0 , 0 , _outerClipper - > getContentSize ( ) . width , _outerClipper - > getContentSize ( ) . height ) ;
2012-11-21 15:22:54 +08:00
if ( ! rect . containsPoint ( point ) ) return ;
this - > pokeHoleAtPoint ( point ) ;
}
2014-03-26 14:54:50 +08:00
// ScrollViewDemo
2012-11-21 15:22:54 +08:00
2013-12-19 05:52:10 +08:00
std : : string ScrollViewDemo : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Scroll View Demo " ;
}
2013-12-19 05:52:10 +08:00
std : : string ScrollViewDemo : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " Move/drag to scroll the content " ;
}
void ScrollViewDemo : : setup ( )
{
2013-08-16 16:05:27 +08:00
auto clipper = ClippingNode : : create ( ) ;
2012-11-21 15:22:54 +08:00
clipper - > setTag ( kTagClipperNode ) ;
2013-07-12 14:30:26 +08:00
clipper - > setContentSize ( Size ( 200 , 200 ) ) ;
2014-05-15 01:07:09 +08:00
clipper - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
2014-08-28 11:41:18 +08:00
clipper - > setPosition ( this - > getContentSize ( ) . width / 2 , this - > getContentSize ( ) . height / 2 ) ;
2013-06-20 14:17:10 +08:00
clipper - > runAction ( RepeatForever : : create ( RotateBy : : create ( 1 , 45 ) ) ) ;
2012-11-21 15:22:54 +08:00
this - > addChild ( clipper ) ;
2013-08-16 16:05:27 +08:00
auto stencil = DrawNode : : create ( ) ;
2014-05-15 01:07:09 +08:00
Vec2 rectangle [ 4 ] ;
rectangle [ 0 ] = Vec2 ( 0 , 0 ) ;
rectangle [ 1 ] = Vec2 ( clipper - > getContentSize ( ) . width , 0 ) ;
rectangle [ 2 ] = Vec2 ( clipper - > getContentSize ( ) . width , clipper - > getContentSize ( ) . height ) ;
rectangle [ 3 ] = Vec2 ( 0 , clipper - > getContentSize ( ) . height ) ;
2012-11-21 15:22:54 +08:00
2013-07-07 21:08:14 +08:00
Color4F white ( 1 , 1 , 1 , 1 ) ;
2012-11-21 15:22:54 +08:00
stencil - > drawPolygon ( rectangle , 4 , white , 1 , white ) ;
clipper - > setStencil ( stencil ) ;
2013-08-16 16:05:27 +08:00
auto content = Sprite : : create ( s_back2 ) ;
2012-11-21 15:22:54 +08:00
content - > setTag ( kTagContentNode ) ;
2014-05-15 01:07:09 +08:00
content - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
2014-08-28 11:41:18 +08:00
content - > setPosition ( clipper - > getContentSize ( ) . width / 2 , clipper - > getContentSize ( ) . height / 2 ) ;
2012-11-21 15:22:54 +08:00
clipper - > addChild ( content ) ;
2013-06-15 14:03:30 +08:00
_scrolling = false ;
2012-11-21 15:22:54 +08:00
2013-10-23 11:27:24 +08:00
auto listener = EventListenerTouchAllAtOnce : : create ( ) ;
listener - > onTouchesBegan = CC_CALLBACK_2 ( ScrollViewDemo : : onTouchesBegan , this ) ;
listener - > onTouchesMoved = CC_CALLBACK_2 ( ScrollViewDemo : : onTouchesMoved , this ) ;
listener - > onTouchesEnded = CC_CALLBACK_2 ( ScrollViewDemo : : onTouchesEnded , this ) ;
2013-10-26 15:04:01 +08:00
_eventDispatcher - > addEventListenerWithSceneGraphPriority ( listener , this ) ;
2012-11-21 15:22:54 +08:00
}
2013-09-03 18:22:03 +08:00
void ScrollViewDemo : : onTouchesBegan ( const std : : vector < Touch * > & touches , Event * event )
2012-11-21 15:22:54 +08:00
{
2013-09-03 18:22:03 +08:00
Touch * touch = touches [ 0 ] ;
2013-08-16 16:05:27 +08:00
auto clipper = this - > getChildByTag ( kTagClipperNode ) ;
2014-05-15 01:07:09 +08:00
Vec2 point = clipper - > convertToNodeSpace ( Director : : getInstance ( ) - > convertToGL ( touch - > getLocationInView ( ) ) ) ;
2013-08-16 16:05:27 +08:00
auto rect = Rect ( 0 , 0 , clipper - > getContentSize ( ) . width , clipper - > getContentSize ( ) . height ) ;
2013-06-15 14:03:30 +08:00
_scrolling = rect . containsPoint ( point ) ;
_lastPoint = point ;
2012-11-21 15:22:54 +08:00
}
2013-09-03 18:22:03 +08:00
void ScrollViewDemo : : onTouchesMoved ( const std : : vector < Touch * > & touches , Event * event )
2012-11-21 15:22:54 +08:00
{
2013-06-15 14:03:30 +08:00
if ( ! _scrolling ) return ;
2013-09-03 18:22:03 +08:00
Touch * touch = touches [ 0 ] ;
2013-08-16 16:05:27 +08:00
auto clipper = this - > getChildByTag ( kTagClipperNode ) ;
auto point = clipper - > convertToNodeSpace ( Director : : getInstance ( ) - > convertToGL ( touch - > getLocationInView ( ) ) ) ;
2014-05-15 01:07:09 +08:00
Vec2 diff = point - _lastPoint ;
2013-08-16 16:05:27 +08:00
auto content = clipper - > getChildByTag ( kTagContentNode ) ;
2013-07-11 16:38:58 +08:00
content - > setPosition ( content - > getPosition ( ) + diff ) ;
2013-06-15 14:03:30 +08:00
_lastPoint = point ;
2012-11-21 15:22:54 +08:00
}
2013-09-03 18:22:03 +08:00
void ScrollViewDemo : : onTouchesEnded ( const std : : vector < Touch * > & touches , Event * event )
2012-11-21 15:22:54 +08:00
{
2013-06-15 14:03:30 +08:00
if ( ! _scrolling ) return ;
_scrolling = false ;
2012-11-21 15:22:54 +08:00
}
2014-03-26 14:54:50 +08:00
// RawStencilBufferTests
2012-11-21 15:22:54 +08:00
//#if COCOS2D_DEBUG > 1
static GLint _stencilBits = - 1 ;
static const GLfloat _alphaThreshold = 0.05f ;
static const int _planeCount = 8 ;
2013-07-05 16:49:22 +08:00
static const Color4F _planeColor [ ] = {
2013-07-07 21:08:14 +08:00
Color4F ( 0 , 0 , 0 , 0.65f ) ,
Color4F ( 0.7f , 0 , 0 , 0.6f ) ,
Color4F ( 0 , 0.7f , 0 , 0.55f ) ,
Color4F ( 0 , 0 , 0.7f , 0.5f ) ,
Color4F ( 0.7f , 0.7f , 0 , 0.45f ) ,
Color4F ( 0 , 0.7f , 0.7f , 0.4f ) ,
Color4F ( 0.7f , 0 , 0.7f , 0.35f ) ,
Color4F ( 0.7f , 0.7f , 0.7f , 0.3f ) ,
2012-11-21 15:22:54 +08:00
} ;
RawStencilBufferTest : : ~ RawStencilBufferTest ( )
{
2013-12-30 15:26:01 +08:00
2012-11-21 15:22:54 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest : : title ( ) const
2012-11-21 15:22:54 +08:00
{
return " Raw Stencil Tests " ;
}
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " 1:Default " ;
}
void RawStencilBufferTest : : setup ( )
{
glGetIntegerv ( GL_STENCIL_BITS , & _stencilBits ) ;
if ( _stencilBits < 3 ) {
2013-06-20 14:17:10 +08:00
CCLOGWARN ( " Stencil must be enabled for the current GLView. " ) ;
2012-11-21 15:22:54 +08:00
}
2013-12-30 13:55:44 +08:00
for ( int i = 0 ; i < _planeCount ; + + i )
{
2013-12-30 15:26:01 +08:00
Sprite * sprite = Sprite : : create ( s_pathGrossini ) ;
2014-05-15 01:07:09 +08:00
sprite - > setAnchorPoint ( Vec2 ( 0.5 , 0 ) ) ;
2013-12-30 15:26:01 +08:00
sprite - > setScale ( 2.5f ) ;
_sprites . pushBack ( sprite ) ;
2014-04-22 10:58:42 +08:00
Sprite * sprite2 = Sprite : : create ( s_pathGrossini ) ;
2014-05-15 01:07:09 +08:00
sprite2 - > setAnchorPoint ( Vec2 ( 0.5 , 0 ) ) ;
2014-04-22 10:58:42 +08:00
sprite2 - > setScale ( 2.5f ) ;
_spritesStencil . pushBack ( sprite2 ) ;
2013-12-30 13:55:44 +08:00
}
2013-07-12 06:24:23 +08:00
Director : : getInstance ( ) - > setAlphaBlending ( true ) ;
2012-11-21 15:22:54 +08:00
}
2014-05-31 07:42:05 +08:00
void RawStencilBufferTest : : draw ( Renderer * renderer , const Mat4 & transform , uint32_t flags )
2012-11-21 15:22:54 +08:00
{
2014-05-15 01:07:09 +08:00
auto winPoint = Vec2 ( Director : : getInstance ( ) - > getWinSize ( ) ) ;
2012-11-21 15:22:54 +08:00
2013-08-16 16:05:27 +08:00
auto planeSize = winPoint * ( 1.0 / _planeCount ) ;
2012-11-21 15:22:54 +08:00
2013-12-27 14:42:45 +08:00
size_t neededCmdSize = _planeCount * 2 + 2 ;
if ( _renderCmds . size ( ) ! = neededCmdSize )
{
_renderCmds . resize ( neededCmdSize ) ;
}
auto iter = _renderCmds . begin ( ) ;
2013-12-26 15:30:13 +08:00
2014-01-19 03:35:27 +08:00
iter - > init ( _globalZOrder ) ;
2013-12-27 14:42:45 +08:00
iter - > func = CC_CALLBACK_0 ( RawStencilBufferTest : : onEnableStencil , this ) ;
renderer - > addCommand ( & ( * iter ) ) ;
+ + iter ;
2013-12-26 15:30:13 +08:00
2012-11-21 15:22:54 +08:00
for ( int i = 0 ; i < _planeCount ; i + + ) {
2013-08-16 16:05:27 +08:00
auto stencilPoint = planeSize * ( _planeCount - i ) ;
2012-11-21 15:22:54 +08:00
stencilPoint . x = winPoint . x ;
2013-08-16 16:05:27 +08:00
auto spritePoint = planeSize * i ;
2012-11-21 15:22:54 +08:00
spritePoint . x + = planeSize . x / 2 ;
spritePoint . y = 0 ;
2013-12-30 15:26:01 +08:00
_sprites . at ( i ) - > setPosition ( spritePoint ) ;
2014-04-22 10:58:42 +08:00
_spritesStencil . at ( i ) - > setPosition ( spritePoint ) ;
2014-01-19 03:35:27 +08:00
iter - > init ( _globalZOrder ) ;
2013-12-27 14:42:45 +08:00
iter - > func = CC_CALLBACK_0 ( RawStencilBufferTest : : onBeforeDrawClip , this , i , stencilPoint ) ;
renderer - > addCommand ( & ( * iter ) ) ;
+ + iter ;
2012-11-21 15:22:54 +08:00
2014-04-03 15:59:55 +08:00
Director * director = Director : : getInstance ( ) ;
CCASSERT ( nullptr ! = director , " Director is null when seting matrix stack " ) ;
director - > pushMatrix ( MATRIX_STACK_TYPE : : MATRIX_STACK_MODELVIEW ) ;
2014-03-01 03:20:53 +08:00
_modelViewTransform = this - > transform ( transform ) ;
2014-05-31 07:42:05 +08:00
_spritesStencil . at ( i ) - > visit ( renderer , _modelViewTransform , flags ) ;
2014-04-03 15:59:55 +08:00
director - > popMatrix ( MATRIX_STACK_TYPE : : MATRIX_STACK_MODELVIEW ) ;
2014-04-23 14:46:14 +08:00
2014-01-19 03:35:27 +08:00
iter - > init ( _globalZOrder ) ;
2013-12-27 14:42:45 +08:00
iter - > func = CC_CALLBACK_0 ( RawStencilBufferTest : : onBeforeDrawSprite , this , i , winPoint ) ;
renderer - > addCommand ( & ( * iter ) ) ;
+ + iter ;
2012-11-21 15:22:54 +08:00
2014-04-03 15:59:55 +08:00
director - > pushMatrix ( MATRIX_STACK_TYPE : : MATRIX_STACK_MODELVIEW ) ;
2014-03-01 03:20:53 +08:00
_modelViewTransform = this - > transform ( transform ) ;
2014-05-31 07:42:05 +08:00
_sprites . at ( i ) - > visit ( renderer , _modelViewTransform , flags ) ;
2014-04-03 15:59:55 +08:00
director - > popMatrix ( MATRIX_STACK_TYPE : : MATRIX_STACK_MODELVIEW ) ;
2012-11-21 15:22:54 +08:00
}
2014-01-19 03:35:27 +08:00
iter - > init ( _globalZOrder ) ;
2013-12-27 14:42:45 +08:00
iter - > func = CC_CALLBACK_0 ( RawStencilBufferTest : : onDisableStencil , this ) ;
renderer - > addCommand ( & ( * iter ) ) ;
2013-12-26 15:30:13 +08:00
}
void RawStencilBufferTest : : onEnableStencil ( )
{
glEnable ( GL_STENCIL_TEST ) ;
CHECK_GL_ERROR_DEBUG ( ) ;
}
void RawStencilBufferTest : : onDisableStencil ( )
{
2012-11-21 15:22:54 +08:00
glDisable ( GL_STENCIL_TEST ) ;
CHECK_GL_ERROR_DEBUG ( ) ;
}
2014-05-15 01:07:09 +08:00
void RawStencilBufferTest : : onBeforeDrawClip ( int planeIndex , const Vec2 & pt )
2013-12-26 15:30:13 +08:00
{
this - > setupStencilForClippingOnPlane ( planeIndex ) ;
CHECK_GL_ERROR_DEBUG ( ) ;
2014-09-11 15:39:56 +08:00
Vec2 vertices [ ] = {
Vec2 : : ZERO ,
Vec2 ( pt . x , 0 ) ,
pt ,
Vec2 ( 0 , pt . y )
} ;
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 ) ;
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-12-26 15:30:13 +08:00
}
2014-05-15 01:07:09 +08:00
void RawStencilBufferTest : : onBeforeDrawSprite ( int planeIndex , const Vec2 & pt )
2013-12-26 15:30:13 +08:00
{
this - > setupStencilForDrawingOnPlane ( planeIndex ) ;
CHECK_GL_ERROR_DEBUG ( ) ;
2014-09-16 14:50:00 +08:00
2014-09-11 15:39:56 +08:00
Vec2 vertices [ ] = {
2014-09-12 11:12:20 +08:00
Vec2 : : ZERO ,
Vec2 ( pt . x , 0 ) ,
pt ,
Vec2 ( 0 , pt . y )
2014-09-11 15:39:56 +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 = _planeColor [ planeIndex ] ;
2014-09-12 07:33:34 +08:00
glProgram - > use ( ) ;
glProgram - > setUniformsForBuiltins ( ) ;
glProgram - > setUniformLocationWith4fv ( colorLocation , ( GLfloat * ) & color . r , 1 ) ;
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-12-26 15:30:13 +08:00
}
2012-11-21 15:22:54 +08:00
void RawStencilBufferTest : : setupStencilForClippingOnPlane ( GLint plane )
{
GLint planeMask = 0x1 < < plane ;
glStencilMask ( planeMask ) ;
glClearStencil ( 0x0 ) ;
glClear ( GL_STENCIL_BUFFER_BIT ) ;
glFlush ( ) ;
glStencilFunc ( GL_NEVER , planeMask , planeMask ) ;
glStencilOp ( GL_REPLACE , GL_KEEP , GL_KEEP ) ;
}
void RawStencilBufferTest : : setupStencilForDrawingOnPlane ( GLint plane )
{
GLint planeMask = 0x1 < < plane ;
2013-07-05 11:59:46 +08:00
glStencilFunc ( GL_EQUAL , planeMask , planeMask ) ;
2012-11-21 15:22:54 +08:00
glStencilOp ( GL_KEEP , GL_KEEP , GL_KEEP ) ;
}
//@implementation RawStencilBufferTest2
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest2 : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " 2:DepthMask:FALSE " ;
}
void RawStencilBufferTest2 : : setupStencilForClippingOnPlane ( GLint plane )
{
RawStencilBufferTest : : setupStencilForClippingOnPlane ( plane ) ;
glDepthMask ( GL_FALSE ) ;
}
void RawStencilBufferTest2 : : setupStencilForDrawingOnPlane ( GLint plane )
{
glDepthMask ( GL_TRUE ) ;
RawStencilBufferTest : : setupStencilForDrawingOnPlane ( plane ) ;
}
//@implementation RawStencilBufferTest3
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest3 : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " 3:DepthTest:DISABLE,DepthMask:FALSE " ;
}
void RawStencilBufferTest3 : : setupStencilForClippingOnPlane ( GLint plane )
{
RawStencilBufferTest : : setupStencilForClippingOnPlane ( plane ) ;
glDisable ( GL_DEPTH_TEST ) ;
glDepthMask ( GL_FALSE ) ;
}
void RawStencilBufferTest3 : : setupStencilForDrawingOnPlane ( GLint plane )
{
glDepthMask ( GL_TRUE ) ;
2013-04-27 11:33:23 +08:00
//glEnable(GL_DEPTH_TEST);
2012-11-21 15:22:54 +08:00
RawStencilBufferTest : : setupStencilForDrawingOnPlane ( plane ) ;
}
2014-04-22 10:58:42 +08:00
void RawStencilBufferTestAlphaTest : : setup ( )
{
RawStencilBufferTest : : setup ( ) ;
2014-05-13 10:12:56 +08:00
auto programState = GLProgramState : : getOrCreateWithGLProgramName ( GLProgram : : SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV ) ;
2014-04-22 10:58:42 +08:00
for ( int i = 0 ; i < _planeCount ; + + i )
{
2014-05-10 09:39:25 +08:00
_spritesStencil . at ( i ) - > setGLProgramState ( programState ) ;
2014-04-22 10:58:42 +08:00
}
}
2012-11-21 15:22:54 +08:00
//@implementation RawStencilBufferTest4
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest4 : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " 4:DepthMask:FALSE,AlphaTest:ENABLE " ;
}
void RawStencilBufferTest4 : : setupStencilForClippingOnPlane ( GLint plane )
{
RawStencilBufferTest : : setupStencilForClippingOnPlane ( plane ) ;
glDepthMask ( GL_FALSE ) ;
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
glEnable ( GL_ALPHA_TEST ) ;
glAlphaFunc ( GL_GREATER , _alphaThreshold ) ;
# else
2014-05-10 12:29:24 +08:00
auto program = GLProgramCache : : getInstance ( ) - > getGLProgram ( GLProgram : : SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV ) ;
2013-07-25 17:48:22 +08:00
GLint alphaValueLocation = glGetUniformLocation ( program - > getProgram ( ) , GLProgram : : UNIFORM_NAME_ALPHA_TEST_VALUE ) ;
2014-04-22 10:58:42 +08:00
program - > use ( ) ;
2012-11-21 15:22:54 +08:00
program - > setUniformLocationWith1f ( alphaValueLocation , _alphaThreshold ) ;
# endif
}
void RawStencilBufferTest4 : : setupStencilForDrawingOnPlane ( GLint plane )
{
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
glDisable ( GL_ALPHA_TEST ) ;
# endif
glDepthMask ( GL_TRUE ) ;
RawStencilBufferTest : : setupStencilForDrawingOnPlane ( plane ) ;
}
//@implementation RawStencilBufferTest5
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest5 : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " 5:DepthTest:DISABLE,DepthMask:FALSE,AlphaTest:ENABLE " ;
}
void RawStencilBufferTest5 : : setupStencilForClippingOnPlane ( GLint plane )
{
RawStencilBufferTest : : setupStencilForClippingOnPlane ( plane ) ;
glDisable ( GL_DEPTH_TEST ) ;
glDepthMask ( GL_FALSE ) ;
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
glEnable ( GL_ALPHA_TEST ) ;
glAlphaFunc ( GL_GREATER , _alphaThreshold ) ;
# else
2014-05-10 12:29:24 +08:00
auto program = GLProgramCache : : getInstance ( ) - > getGLProgram ( GLProgram : : SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV ) ;
2013-07-25 17:48:22 +08:00
GLint alphaValueLocation = glGetUniformLocation ( program - > getProgram ( ) , GLProgram : : UNIFORM_NAME_ALPHA_TEST_VALUE ) ;
2014-04-22 10:58:42 +08:00
program - > use ( ) ;
2012-11-21 15:22:54 +08:00
program - > setUniformLocationWith1f ( alphaValueLocation , _alphaThreshold ) ;
# endif
}
void RawStencilBufferTest5 : : setupStencilForDrawingOnPlane ( GLint plane )
{
2012-11-21 16:28:31 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2012-11-21 15:22:54 +08:00
glDisable ( GL_ALPHA_TEST ) ;
# endif
glDepthMask ( GL_TRUE ) ;
2013-04-27 11:33:23 +08:00
//glEnable(GL_DEPTH_TEST);
2012-11-21 15:22:54 +08:00
RawStencilBufferTest : : setupStencilForDrawingOnPlane ( plane ) ;
}
//@implementation RawStencilBufferTest6
2013-12-19 05:52:10 +08:00
std : : string RawStencilBufferTest6 : : subtitle ( ) const
2012-11-21 15:22:54 +08:00
{
return " 6:ManualClear,AlphaTest:ENABLE " ;
}
void RawStencilBufferTest6 : : setup ( )
{
2014-04-22 10:58:42 +08:00
RawStencilBufferTestAlphaTest : : setup ( ) ;
2012-11-21 15:22:54 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2014-05-15 01:07:09 +08:00
auto winPoint = Vec2 ( Director : : getInstance ( ) - > getWinSize ( ) ) ;
2013-07-30 16:27:13 +08:00
//by default, glReadPixels will pack data with 4 bytes allignment
unsigned char bits [ 4 ] = { 0 , 0 , 0 , 0 } ;
2012-11-21 15:22:54 +08:00
glStencilMask ( ~ 0 ) ;
glClearStencil ( 0 ) ;
glClear ( GL_STENCIL_BUFFER_BIT ) ;
glFlush ( ) ;
glReadPixels ( 0 , 0 , 1 , 1 , GL_STENCIL_INDEX , GL_UNSIGNED_BYTE , & bits ) ;
2014-04-09 23:31:05 +08:00
auto clearToZeroLabel = Label : : createWithTTF ( String : : createWithFormat ( " 00=%02x " , bits [ 0 ] ) - > getCString ( ) , " fonts/arial.ttf " , 20 ) ;
2014-08-28 11:41:18 +08:00
clearToZeroLabel - > setPosition ( ( winPoint . x / 3 ) * 1 , winPoint . y - 10 ) ;
2012-11-21 15:22:54 +08:00
this - > addChild ( clearToZeroLabel ) ;
glStencilMask ( 0x0F ) ;
glClearStencil ( 0xAA ) ;
glClear ( GL_STENCIL_BUFFER_BIT ) ;
glFlush ( ) ;
glReadPixels ( 0 , 0 , 1 , 1 , GL_STENCIL_INDEX , GL_UNSIGNED_BYTE , & bits ) ;
2014-04-09 23:31:05 +08:00
auto clearToMaskLabel = Label : : createWithTTF ( String : : createWithFormat ( " 0a=%02x " , bits [ 0 ] ) - > getCString ( ) , " fonts/arial.ttf " , 20 ) ;
2014-08-28 11:41:18 +08:00
clearToMaskLabel - > setPosition ( ( winPoint . x / 3 ) * 2 , winPoint . y - 10 ) ;
2012-11-21 15:22:54 +08:00
this - > addChild ( clearToMaskLabel ) ;
# endif
glStencilMask ( ~ 0 ) ;
}
void RawStencilBufferTest6 : : setupStencilForClippingOnPlane ( GLint plane )
{
GLint planeMask = 0x1 < < plane ;
glStencilMask ( planeMask ) ;
glStencilFunc ( GL_NEVER , 0 , planeMask ) ;
glStencilOp ( GL_REPLACE , GL_KEEP , GL_KEEP ) ;
2014-09-11 15:39:56 +08:00
Vec2 pt = Director : : getInstance ( ) - > getWinSize ( ) ;
Vec2 vertices [ ] = {
2014-09-12 14:50:41 +08:00
Vec2 : : ZERO ,
Vec2 ( pt . x , 0 ) ,
pt ,
Vec2 ( 0 , pt . y )
2014-09-11 15:39:56 +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 ) ;
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 ) ;
2012-11-21 15:22:54 +08:00
glStencilFunc ( GL_NEVER , planeMask , planeMask ) ;
glStencilOp ( GL_REPLACE , GL_KEEP , GL_KEEP ) ;
glDisable ( GL_DEPTH_TEST ) ;
glDepthMask ( GL_FALSE ) ;
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
glEnable ( GL_ALPHA_TEST ) ;
glAlphaFunc ( GL_GREATER , _alphaThreshold ) ;
# else
2014-05-10 12:29:24 +08:00
auto program = GLProgramCache : : getInstance ( ) - > getGLProgram ( GLProgram : : SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV ) ;
2013-07-25 17:48:22 +08:00
GLint alphaValueLocation = glGetUniformLocation ( program - > getProgram ( ) , GLProgram : : UNIFORM_NAME_ALPHA_TEST_VALUE ) ;
2014-04-22 10:58:42 +08:00
program - > use ( ) ;
2012-11-21 15:22:54 +08:00
program - > setUniformLocationWith1f ( alphaValueLocation , _alphaThreshold ) ;
# endif
glFlush ( ) ;
}
void RawStencilBufferTest6 : : setupStencilForDrawingOnPlane ( GLint plane )
{
2012-11-21 16:28:31 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2012-11-21 15:22:54 +08:00
glDisable ( GL_ALPHA_TEST ) ;
# endif
glDepthMask ( GL_TRUE ) ;
2013-04-27 11:33:23 +08:00
//glEnable(GL_DEPTH_TEST);
2012-11-21 15:22:54 +08:00
RawStencilBufferTest : : setupStencilForDrawingOnPlane ( plane ) ;
glFlush ( ) ;
}
//#endif // COCOS2D_DEBUG > 1
2014-08-28 01:54:15 +08:00
//ClippingToRenderTextureTest
std : : string ClippingToRenderTextureTest : : title ( ) const
{
return " Clipping to RenderTexture " ;
}
std : : string ClippingToRenderTextureTest : : subtitle ( ) const
{
return " Both should look the same " ;
}
void ClippingToRenderTextureTest : : setup ( )
{
auto button = MenuItemFont : : create ( " Reproduce bug " , [ & ] ( Ref * sender ) {
2014-10-17 18:48:18 +08:00
std : : vector < Node * > nodes ;
2014-08-28 01:54:15 +08:00
enumerateChildren ( " remove me [0-9] " , [ & ] ( Node * node ) {
2014-10-17 18:48:18 +08:00
nodes . push_back ( node ) ;
2014-08-28 01:54:15 +08:00
return false ;
2014-10-17 18:48:18 +08:00
} ) ;
for ( auto node : nodes )
{
this - > removeChild ( node ) ;
2014-08-28 01:54:15 +08:00
}
2014-10-17 18:48:18 +08:00
this - > reproduceBug ( ) ;
2014-08-28 01:54:15 +08:00
} ) ;
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
// create menu, it's an autorelease object
2014-10-09 09:55:33 +08:00
auto menu = Menu : : create ( button , nullptr ) ;
2014-08-28 01:54:15 +08:00
menu - > setPosition ( Point ( s . width / 2 , s . height / 2 ) ) ;
this - > addChild ( menu , 1 ) ;
expectedBehaviour ( ) ;
}
void ClippingToRenderTextureTest : : expectedBehaviour ( )
{
auto director = Director : : getInstance ( ) ;
Size visibleSize = director - > getVisibleSize ( ) ;
Point origin = director - > getVisibleOrigin ( ) ;
// add "HelloWorld" splash screen"
auto sprite = Sprite : : create ( " Images/grossini.png " ) ;
// position the sprite on the center of the screen
sprite - > setPosition ( Point ( visibleSize . width / 2 + origin . x , visibleSize . height / 2 + origin . y ) ) ;
// add the sprite as a child to this layer
this - > addChild ( sprite , 0 ) ;
sprite - > setName ( " remove me 0 " ) ;
// container node that will contain the clippingNode
auto container = Node : : create ( ) ;
this - > addChild ( container ) ;
container - > setName ( " remove me 1 " ) ;
auto stencil = DrawNode : : create ( ) ;
Point triangle [ 3 ] ;
triangle [ 0 ] = Point ( - 50 , - 50 ) ;
triangle [ 1 ] = Point ( 50 , - 50 ) ;
triangle [ 2 ] = Point ( 0 , 50 ) ;
Color4F green ( 0 , 1 , 0 , 1 ) ;
stencil - > drawPolygon ( triangle , 3 , green , 0 , green ) ;
auto clipper = ClippingNode : : create ( ) ;
clipper - > setAnchorPoint ( Point ( 0.5 , 0.5 ) ) ;
clipper - > setPosition ( Point ( visibleSize . width / 2 , visibleSize . height / 2 ) ) ;
clipper - > setStencil ( stencil ) ;
clipper - > setInverted ( true ) ;
container - > addChild ( clipper , 1 ) ;
auto img = DrawNode : : create ( ) ;
triangle [ 0 ] = Point ( - 200 , - 200 ) ;
triangle [ 1 ] = Point ( 200 , - 200 ) ;
triangle [ 2 ] = Point ( 0 , 200 ) ;
Color4F red ( 1 , 0 , 0 , 1 ) ;
img - > drawPolygon ( triangle , 3 , red , 0 , red ) ;
clipper - > addChild ( img ) ;
}
void ClippingToRenderTextureTest : : reproduceBug ( )
{
auto director = Director : : getInstance ( ) ;
Size visibleSize = director - > getVisibleSize ( ) ;
Point origin = director - > getVisibleOrigin ( ) ;
// add "HelloWorld" splash screen"
auto sprite = Sprite : : create ( " Images/grossini.png " ) ;
// position the sprite on the center of the screen
sprite - > setPosition ( Point ( visibleSize . width / 2 + origin . x , visibleSize . height / 2 + origin . y ) ) ;
// add the sprite as a child to this layer
this - > addChild ( sprite , 0 ) ;
// container node that will contain the clippingNode
auto container = Node : : create ( ) ;
container - > retain ( ) ;
auto stencil = DrawNode : : create ( ) ;
Point triangle [ 3 ] ;
triangle [ 0 ] = Point ( - 50 , - 50 ) ;
triangle [ 1 ] = Point ( 50 , - 50 ) ;
triangle [ 2 ] = Point ( 0 , 50 ) ;
Color4F green ( 0 , 1 , 0 , 1 ) ;
stencil - > drawPolygon ( triangle , 3 , green , 0 , green ) ;
auto clipper = ClippingNode : : create ( ) ;
clipper - > setAnchorPoint ( Point ( 0.5 , 0.5 ) ) ;
clipper - > setPosition ( Point ( visibleSize . width / 2 , visibleSize . height / 2 ) ) ;
clipper - > setStencil ( stencil ) ;
clipper - > setInverted ( true ) ;
container - > addChild ( clipper , 1 ) ;
auto img = DrawNode : : create ( ) ;
triangle [ 0 ] = Point ( - 200 , - 200 ) ;
triangle [ 1 ] = Point ( 200 , - 200 ) ;
triangle [ 2 ] = Point ( 0 , 200 ) ;
Color4F red ( 1 , 0 , 0 , 1 ) ;
img - > drawPolygon ( triangle , 3 , red , 0 , red ) ;
clipper - > addChild ( img ) ;
2014-12-03 14:17:42 +08:00
// container rendered on Texture the size of the screen and because Clipping node use stencil buffer so we need to
// create RenderTexture with depthStencil format parameter
RenderTexture * rt = RenderTexture : : create ( visibleSize . width , visibleSize . height , Texture2D : : PixelFormat : : RGBA8888 , GL_DEPTH24_STENCIL8 ) ;
2014-08-28 01:54:15 +08:00
rt - > setPosition ( visibleSize . width / 2 , visibleSize . height / 2 ) ;
this - > addChild ( rt ) ;
2014-12-03 14:17:42 +08:00
rt - > begin ( ) ;
2014-08-28 01:54:15 +08:00
container - > visit ( ) ;
rt - > end ( ) ;
}
2014-10-15 13:41:47 +08:00
// ClippingRectangleNodeDemo
std : : string ClippingRectangleNodeTest : : title ( ) const
{
return " ClippingRectangleNode Test " ;
}
std : : string ClippingRectangleNodeTest : : subtitle ( ) const
{
return " more effectively " ;
}
void ClippingRectangleNodeTest : : setup ( )
{
2015-07-17 11:56:23 +08:00
//Notice: The rectangle is base on screen coordinate, so ClippingRectangleNode's region doesn't support any transforms, such as setScale, setRotate and so on.
2014-10-15 13:41:47 +08:00
auto clipper = ClippingRectangleNode : : create ( ) ;
clipper - > setClippingRegion ( Rect ( this - > getContentSize ( ) . width / 2 - 100 , this - > getContentSize ( ) . height / 2 - 100 , 200 , 200 ) ) ;
clipper - > setTag ( kTagClipperNode ) ;
this - > addChild ( clipper ) ;
auto content = Sprite : : create ( s_back2 ) ;
content - > setTag ( kTagContentNode ) ;
content - > setAnchorPoint ( Vec2 ( 0.5 , 0.5 ) ) ;
2015-07-17 11:56:23 +08:00
clipper - > setPosition ( this - > getContentSize ( ) . width / 2 , this - > getContentSize ( ) . height / 2 ) ;
2014-10-15 13:41:47 +08:00
clipper - > addChild ( content ) ;
}