Cocos2d-x 灰白图生成

//
//  BYGraySprite.h
//  SanGuo_Lua
//
//  Created by liliang on 13-6-26.
//
//

#ifndef __SanGuo_Lua__BYGraySprite__
#define __SanGuo_Lua__BYGraySprite__

#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();
    
    static BYGraySprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
    static BYGraySprite* createWithSpriteFrameName(const char *pszSpriteFrameName);
    
    virtual bool initWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
};

#endif /* defined(__SanGuo_Lua__BYGraySprite__) */


//
//  BYGraySprite.cpp
//  SanGuo_Lua
//
//  Created by liliang on 13-6-26.
//
//

#include "BYGraySprite.h"

BYGraySprite::BYGraySprite(){
    
}

BYGraySprite::~BYGraySprite(){
    
}

BYGraySprite* BYGraySprite::create( const char* pszFileName ){
    BYGraySprite* graySprite = new BYGraySprite;
    if (graySprite && graySprite->initWithFile(pszFileName)){
        graySprite->autorelease();
        return graySprite;
    }else{
        CC_SAFE_RELEASE(graySprite);
        return NULL;
    }
}
BYGraySprite* BYGraySprite::createWithSpriteFrame(CCSpriteFrame *pSpriteFrame)
{
    BYGraySprite *pobSprite = new BYGraySprite();
    if (pSpriteFrame && pobSprite && pobSprite->initWithSpriteFrame(pSpriteFrame))
    {
        pobSprite->autorelease();
        return pobSprite;
    }
    CC_SAFE_DELETE(pobSprite);
    return NULL;
}
BYGraySprite* BYGraySprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
{
    CCSpriteFrame *pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(pszSpriteFrameName);
    
#if COCOS2D_DEBUG > 0
    char msg[256] = {0};
    sprintf(msg, "Invalid spriteFrameName: %s", pszSpriteFrameName);
    CCAssert(pFrame != NULL, msg);
#endif
    
    return createWithSpriteFrame(pFrame);
}
bool BYGraySprite::initWithSpriteFrame(CCSpriteFrame *pSpriteFrame)
{
    CCAssert(pSpriteFrame != NULL, "");
    
    bool bRet = initWithTexture(pSpriteFrame->getTexture(), pSpriteFrame->getRect());
    setDisplayFrame(pSpriteFrame);
    
    return bRet;
}

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 \
        float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114)); \n \
        gl_FragColor = vec4(grey, grey, grey, 1.0); \n \
        }";
        
        CCGLProgram* pProgram = new CCGLProgram();
        pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
        this->setShaderProgram(pProgram);
        pProgram->release();
        CHECK_GL_ERROR_DEBUG();
        
        this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
        this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_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 );
    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
    
    this->getShaderProgram()->use();
    this->getShaderProgram()->setUniformsForBuiltins();//setUniformForModelViewProjectionMatrix();
    
    ccGLBindTexture2D( this->getTexture()->getName() );
    
#define kQuadSize sizeof(m_sQuad.bl)
    long offset = (long)&m_sQuad;
    
    // vertex
    int diff = offsetof( ccV3F_C4B_T2F, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
    
    // texCoods
    diff = offsetof( ccV3F_C4B_T2F, texCoords);
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
    
    // color
    diff = offsetof( ccV3F_C4B_T2F, colors);
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    CC_INCREMENT_GL_DRAWS(1);
}

调用:

#include "BYGraySprite.h"

BYGraySprite* graySprite = BYGraySprite::createWithSpriteFrameName("b_pt_001.png");//BYGraySprite::create("Icon.png");

graySprite->setPosition( ccp(100,100) );

this->addChild(graySprite);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值