CCClippingNode(Clip修剪 可以实现新手引导遮罩、滚动字幕。。渲染child时仅渲染模板透明度值大于透明度阀值的像素点.默认透明度阀值 =1)

本文主要是介绍CCClippingNode(Clip修剪 可以实现新手引导遮罩、滚动字幕。。渲染child时仅渲染模板透明度值大于透明度阀值的像素点.默认透明度阀值 =1),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

http://blog.csdn.net/univcore/article/details/42149725

         Cocos2d-x开发--使用CCRenderTexture将矩形图片裁剪成圆形显示         

  CCNode* m_pStencil;//模板   简单的模板可以用CCDrawNode 、 CCLayerColor来做 。 复杂的可以通过ccsprite 使用图片做


cocos2d-x利用CCClippingNode实现滚动字幕 - 白白手游专栏(...

要实现文字在一定区域里滚动,首先用CCClippingNode做出一个剪裁区域,在这个区域内文字可以显示,出了这个区域之后文字就看不到。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool TestLayer::init()
{
     CCSize size = CCDirector::sharedDirector()->getVisibleSize();
       
     //创建要显示的文字
     text = CCLabelTTF::create( "text\nbaibai\n白白\nbaibai\nhaha\nhaha\nahha\n" , "" , 30 );
     text->setPosition(ccp( 100 , - 120 ));
     
     //绘制裁剪区域
     CCDrawNode* shap = CCDrawNode::create();
     CCPoint point[ 4 ] = {ccp( 0 , 0 ), ccp( 200 , 0 ), ccp( 200 , 200 ), ccp( 0 , 200 )};
     shap->drawPolygon(point, 4 , ccc4f( 355 , 255 , 255 , 255 ), 2 , ccc4f( 255 , 255 , 255 , 255 ));
     CCClippingNode* cliper = CCClippingNode::create();
     cliper->setStencil(shap);
     cliper->setAnchorPoint(ccp(. 5 , . 5 ));
     cliper->setPosition(ccp( 100 , 20 ));
     addChild(cliper);
     //把要滚动的文字加入到裁剪区域
     cliper->addChild(text);
     
     //文字滚动,超出范围后从新开始
     schedule(schedule_selector(CTestLayer::rollText));
     return true ;
}
void TestLayer::rollText( float )
{
     text->getPositionY()> 300 ? text->setPositionY(- 150 ) : text->setPositionY(text->getPositionY()+ 2 );
}
cocos2d-x CCClippingNode遮罩实现手电筒效果         ( 通过移动模板实现

CCClippingNode遮罩解析 - shangguanyingqi的专栏 - 博客频道 - ...


#ifndef __MISCNODE_CCCLIPPING_NODE_H__

#define __MISCNODE_CCCLIPPING_NODE_H__


#include "base_nodes/CCNode.h"

#include "CCGL.h"  


NS_CC_BEGIN


/** CCClippingNode is a subclass of CCNode.

 It draws its content (childs) clipped using a stencil.  //绘制child时会先通过模板裁剪

 The stencil is an other CCNode that will not be drawn. //模板也是一个ccnode 但不会被渲染

 The clipping is done using the alpha part of the stencil (adjusted(调整) with an alphaThreshold).// 模板只是提供他的alpha 以实现裁剪的效果

 */

class CC_DLL CCClippingNode : public CCNode

{

protected:


// CCClippingNode 在渲染child时 仅渲染模板透明度值 大于 透明度阀值的 像素点 默认透明度阀值 =1 

    CCNode* m_pStencil;//模板   简单的模板可以用CCDrawNode 、 CCLayerColor来做 。 复杂的可以通过ccsprite 使用图片做

    GLfloat m_fAlphaThreshold;  //Alpha 透明度    Threshold 阀值工具

    bool    m_bInverted; //倒转的 反向的

        

private:

    CCClippingNode(); // 私有化构造函数 子类就不能new出此类了 这个类是不准备被派生了  

public:

    static CCClippingNode*create();  //无模板创建

    static CCClippingNode*create(CCNode *pStencil);//通过模板创建

    

    CCNode*getStencil() const;  //set get 模板

    voidsetStencil(CCNode *pStencil);

    

    GLfloatgetAlphaThreshold() const;   //set get AlphaThreshold

    voidsetAlphaThreshold(GLfloat fAlphaThreshold);

    

    /** Inverted. If this is set to YES,

     the stencil is inverted, so the content is drawn where the stencil is NOT drawn.

     This default to NO.

     */

    bool isInverted() const;

    voidsetInverted(bool bInverted);

    virtual ~CCClippingNode();

    virtual bool init();

    virtual boolinit(CCNode *pStencil);   

{

    CC_SAFE_RELEASE(m_pStencil);

    m_pStencil = pStencil;

    CC_SAFE_RETAIN(m_pStencil);

    

    m_fAlphaThreshold = 1;

    m_bInverted = false;

    // 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;

}

    virtual void onEnter();

    virtual void onEnterTransitionDidFinish();

    virtual void onExitTransitionDidStart();

    virtual void onExit();

    virtual void visit();

};


void CCClippingNode::visit()

{

    // if stencil buffer disabled

    if (g_sStencilBits < 1)

    {

        // draw everything, as if there where no stencil

        CCNode::visit();

        return;

    }

    

    // return fast (draw nothing, or draw everything if in inverted mode) if:

    // - nil stencil node

    // - or stencil node invisible:

    if (!m_pStencil || !m_pStencil->isVisible())

    {

        if (m_bInverted)

        {

            // draw everything

            CCNode::visit();

        }

        return;

    }

    

    // store the current stencil layer (position in the stencil buffer),

    // this will allow nesting up to n CCClippingNode,

    // where n is the number of bits of the stencil buffer.

    static GLint layer = -1;

    

    // all the _stencilBits are in use?

    if (layer + 1 == g_sStencilBits)

    {

        // warn once

        static bool once = true;

        if (once)

        {

            char warning[200] = {0};

            snprintf(warning, sizeof(warning), "Nesting more than %d stencils is not supported. Everything will be drawn without stencil for this node and its childs.", g_sStencilBits);

            CCLOG("%s", warning);

            

            once = false;

        }

        // draw everything, as if there where no stencil

        CCNode::visit();

        return;

    }

    

    ///

    // INIT

    

    // increment the current layer

    layer++;

    

    // mask of the current layer (ie: for layer 3: 00000100)

    GLint mask_layer = 0x1 << layer;

    // 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)

    GLint mask_layer_le = mask_layer | mask_layer_l;

    

    // manually save the stencil state

    GLboolean currentStencilEnabled = GL_FALSE;

    GLuint currentStencilWriteMask = ~0;

    GLenum currentStencilFunc = GL_ALWAYS;

    GLint currentStencilRef = 0;

    GLuint currentStencilValueMask = ~0;

    GLenum currentStencilFail = GL_KEEP;

    GLenum currentStencilPassDepthFail = GL_KEEP;

    GLenum currentStencilPassDepthPass = GL_KEEP;

    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);

    

    // enable stencil use

    glEnable(GL_STENCIL_TEST);

    // check for OpenGL error while enabling stencil test

    CHECK_GL_ERROR_DEBUG();

    

    // 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);

    

    // manually save the depth test state

    //GLboolean currentDepthTestEnabled = GL_TRUE;

    GLboolean currentDepthWriteMask = GL_TRUE;

    //currentDepthTestEnabled = glIsEnabled(GL_DEPTH_TEST);

    glGetBooleanv(GL_DEPTH_WRITEMASK, &currentDepthWriteMask);

    

    // 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);

    

    ///

    // CLEAR STENCIL BUFFER

    

    // 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);

    glStencilOp(!m_bInverted ? GL_ZERO : GL_REPLACE, GL_KEEP, GL_KEEP);

    

    // draw a fullscreen solid rectangle to clear the stencil buffer

    //ccDrawSolidRect(CCPointZero, ccpFromSize([[CCDirector sharedDirector] winSize]), ccc4f(1, 1, 1, 1));

    ccDrawSolidRect(CCPointZero, ccpFromSize(CCDirector::sharedDirector()->getWinSize()), ccc4f(1, 1, 1, 1));

    

    ///

    // DRAW CLIPPING STENCIL

    

    // 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);

    glStencilOp(!m_bInverted ? GL_REPLACE : GL_ZERO, GL_KEEP, GL_KEEP);

    

    // enable alpha test only if the alpha threshold < 1,

    // indeed if alpha threshold == 1, every pixel will be drawn anyways

#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)

    GLboolean currentAlphaTestEnabled = GL_FALSE;

    GLenum currentAlphaTestFunc = GL_ALWAYS;

    GLclampf currentAlphaTestRef = 1;

#endif

    if (m_fAlphaThreshold < 1) {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)

        // manually save the alpha test state

        currentAlphaTestEnabled = glIsEnabled(GL_ALPHA_TEST);

        glGetIntegerv(GL_ALPHA_TEST_FUNC, (GLint *)&currentAlphaTestFunc);

        glGetFloatv(GL_ALPHA_TEST_REF, &currentAlphaTestRef);

        // 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

        glAlphaFunc(GL_GREATER, m_fAlphaThreshold);

#else

        // since glAlphaTest do not exists in OES, use a shader that writes

        // pixel only if greater than an alpha threshold

        CCGLProgram *program = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest);

        GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), kCCUniformAlphaTestValue);

        // set our alphaThreshold

        program->setUniformLocationWith1f(alphaValueLocation, m_fAlphaThreshold);

        // we need to recursively apply this shader to all the nodes in the stencil node

        // XXX: we should have a way to apply shader to all nodes without having to do this

        setProgram(m_pStencil, program);

       

#endif

    }

    

    // draw the stencil node as if it was one of our child

    // (according to the stencil test func/op and alpha (or alpha shader) test)

    kmGLPushMatrix();

    transform();

    m_pStencil->visit();

    kmGLPopMatrix();

    

    // restore alpha test state

    if (m_fAlphaThreshold < 1)

    {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)

        // manually restore the alpha test state

        glAlphaFunc(currentAlphaTestFunc, currentAlphaTestRef);

        if (!currentAlphaTestEnabled)

        {

            glDisable(GL_ALPHA_TEST);

        }

#else

// XXX: we need to find a way to restore the shaders of the stencil node and its childs

#endif

    }

    

    // restore the depth test state

    glDepthMask(currentDepthWriteMask);

    //if (currentDepthTestEnabled) {

    //    glEnable(GL_DEPTH_TEST);

    //}

    

    ///

    // DRAW CONTENT

    

    // 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

    glStencilFunc(GL_EQUAL, mask_layer_le, mask_layer_le);

    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

    

    // draw (according to the stencil test func) this node and its childs

    CCNode::visit();

    

    ///

    // CLEANUP

    

    // manually restore the stencil state

    glStencilFunc(currentStencilFunc, currentStencilRef, currentStencilValueMask);

    glStencilOp(currentStencilFail, currentStencilPassDepthFail, currentStencilPassDepthPass);

    glStencilMask(currentStencilWriteMask);

    if (!currentStencilEnabled)

    {

        glDisable(GL_STENCIL_TEST);

    }

    

    // we are done using this layer, decrement

    layer--;

}



这篇关于CCClippingNode(Clip修剪 可以实现新手引导遮罩、滚动字幕。。渲染child时仅渲染模板透明度值大于透明度阀值的像素点.默认透明度阀值 =1)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/661607

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

禁止平板,iPad长按弹出默认菜单事件

通过监控按下抬起时间差来禁止弹出事件,把以下代码写在要禁止的页面的页面加载事件里面即可     var date;document.addEventListener('touchstart', event => {date = new Date().getTime();});document.addEventListener('touchend', event => {if (new

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n