精灵变灰效果

转自:http://blog.csdn.net/u012945598/article/details/19898609




BYGraySprite.h:

#include "cocoa/CCGeometry.h"

#include "cocos2d.h"

USING_NS_CC;


class BYGraySprite : public CCSprite{

    

public:

    BYGraySprite();

    virtual ~BYGraySprite();

    static BYGraySprite* create(const char* pszFileName);//创建一个变灰的精灵

    bool initWithTexture(CCTexture2D* pTexture, const CCRect& tRect);

    virtual void draw();

    

    void enableSprite(CCSprite* sp); //恢复正常颜色的方法

};


BYGraySprite.cpp:

#include "BYGraySprite.h"


BYGraySprite::BYGraySprite(){

    

}


BYGraySprite::~BYGraySprite(){

    

}


BYGraySpriteBYGraySprite::create( const char* pszFileName ){

    BYGraySprite* graySprite = new BYGraySprite;

    if (graySprite && graySprite->initWithFile(pszFileName)){

        graySprite->autorelease();

        return graySprite;

    }else{

        CC_SAFE_RELEASE(graySprite);

        return NULL;

    }

}


bool BYGraySprite::initWithTexture(CCTexture2D* pTexture, const CCRect& tRect ){

    do{

        CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, tRect));

        

        GLchar* pszFragSource =

        "#ifdef GL_ES \n \

        precision mediump float; \n \

        #endif \n \

        uniform sampler2D u_texture; \n \

        varying vec2 v_texCoord; \n \

        varying vec4 v_fragmentColor; \n \

        void main(void) \n \

        { \n \

        // Convert to greyscale using NTSC weightings \n \

      vec4 col = texture2D(u_texture, v_texCoord);  float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114));  gl_FragColor = vec4(grey, grey, grey, col.a);   \n \

        }";

        

        CCGLProgram* pProgram = new CCGLProgram();

        pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);

        this->setShaderProgram(pProgram);

        pProgram->release();

        CHECK_GL_ERROR_DEBUG();

        

        this->getShaderProgram()->addAttribute(kCCAttributeNamePositionkCCVertexAttrib_Position);

        this->getShaderProgram()->addAttribute(kCCAttributeNameColorkCCVertexAttrib_Color);

        this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoordkCCVertexAttrib_TexCoords);

        CHECK_GL_ERROR_DEBUG();

        

        this->getShaderProgram()->link();

        CHECK_GL_ERROR_DEBUG();

        

        this->getShaderProgram()->updateUniforms();

        CHECK_GL_ERROR_DEBUG();

        

        return true;

    } while (0);

    return false;

}


void BYGraySprite::draw(){

    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );

    ccGLBlendFuncm_sBlendFunc.srcm_sBlendFunc.dst );

    

    this->getShaderProgram()->use();

    this->getShaderProgram()->setUniformsForBuiltins();

    

    ccGLBindTexture2Dthis->getTexture()->getName() );

    

#define kQuadSize sizeof(m_sQuad.bl)

    long offset = (long)&m_sQuad;

    

    // vertex

    int diff = offsetofccV3F_C4B_T2Fvertices);

    glVertexAttribPointer(kCCVertexAttrib_Position3GL_FLOATGL_FALSEkQuadSize, (void*) (offset + diff));

    

    // texCoods

    diff = offsetofccV3F_C4B_T2FtexCoords);

    glVertexAttribPointer(kCCVertexAttrib_TexCoords2GL_FLOATGL_FALSEkQuadSize, (void*)(offset + diff));

    

    // color

    diff = offsetofccV3F_C4B_T2Fcolors);

    glVertexAttribPointer(kCCVertexAttrib_Color4GL_UNSIGNED_BYTEGL_TRUEkQuadSize, (void*)(offset + diff));

    glDrawArrays(GL_TRIANGLE_STRIP04);

    CC_INCREMENT_GL_DRAWS(1);

}


void BYGraySprite::enableSprite(CCSprite* sp)

{

    const GLchar* pszFragSource =

    "#ifdef GL_ES \n \

    precision mediump float; \n \

    #endif \n \

    uniform sampler2D u_texture; \n \

    varying vec2 v_texCoord; \n \

    varying vec4 v_fragmentColor; \n \

    void main(void) \n \

    { \n \

    // Convert to greyscale using NTSC weightings \n \

    vec4 col = texture2D(u_texture, v_texCoord); \n \

    gl_FragColor = vec4(col.r, col.g, col.b, col.a); \n \

    }";

    CCGLProgram* pProgram = new CCGLProgram();

    pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);

    sp->setShaderProgram(pProgram);

    pProgram->release();

    sp->getShaderProgram()->addAttribute(kCCAttributeNamePositionkCCVertexAttrib_Position);

    sp->getShaderProgram()->addAttribute(kCCAttributeNameColorkCCVertexAttrib_Color);

    sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoordkCCVertexAttrib_TexCoords);

    sp->getShaderProgram()->link();

    sp->getShaderProgram()->updateUniforms();

}


在HelloWorld.cpp中测试代码:

    BYGraySprite* graySprite = BYGraySprite::create("stone.png");

    graySprite->setPosition(ccp(480,400));

    //graySprite->enableSprite(graySprite); 恢复颜色的方法

    this->addChild(graySprite);

  

    CCSprite * sprite=CCSprite::create("stone.png");

    sprite->setPosition(ccp(graySprite->getPositionX(), graySprite->getPositionY()-200));

    this->addChild(sprite);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值