首页 > 其他 > 详细

精灵灰化

时间:2014-01-25 14:15:26      阅读:399      评论:0      收藏:0      [点我收藏+]

 

bubuko.com,布布扣
//-----------------------------------------------------------------
//@file        gameui/Module/MdGraySprite.h
//@date        2013-11-07
//@desc        精灵灰化:shader
//@action    UI组件
//-----------------------------------------------------------------

#ifndef __GAMEUI_MODULE_MDGRAYSPRITE_H__
#define __GAMEUI_MODULE_MDGRAYSPRITE_H__

#include "cocos2d.h"

USING_NS_CC;

class MdGraySprite : public CCSprite
{
public:
    MdGraySprite();
    virtual ~MdGraySprite();

    static MdGraySprite* create(const char* pszFileName);
    static MdGraySprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
    virtual bool initWithTexture(CCTexture2D* pTexture, const CCRect& tRect);
    virtual void draw();
};


#endif // __GAMEUI_MODULE_MDGRAYSPRITE_H__
bubuko.com,布布扣
bubuko.com,布布扣
#include "MdGraySprite.h"
#include "cocoa/CCGeometry.h"


MdGraySprite::MdGraySprite()
{

}


MdGraySprite::~MdGraySprite()
{

}


MdGraySprite* MdGraySprite::create( const char* pszFileName )
{
    MdGraySprite* graySprite = new MdGraySprite();
    if (graySprite && graySprite->initWithFile(pszFileName))
    {
        graySprite->autorelease();
        return graySprite;
    }

    CC_SAFE_RELEASE(graySprite);
    return NULL;
}
MdGraySprite* MdGraySprite::createWithSpriteFrame(CCSpriteFrame *pSpriteFrame)
{
    MdGraySprite* graySprite = new MdGraySprite();
    if (graySprite && graySprite->initWithSpriteFrame(pSpriteFrame))
    {
        graySprite->autorelease();
        return graySprite;
    }
    CC_SAFE_RELEASE(graySprite);
    return NULL;
}

bool MdGraySprite::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); \n             float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n             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(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 MdGraySprite::draw()
{
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );


    this->getShaderProgram()->use();
    //this->getShaderProgram()->setUniformForModelViewProjectionMatrix();
    this->getShaderProgram()->setUniformsForBuiltins();

    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);
}
bubuko.com,布布扣

精灵灰化

原文:http://www.cnblogs.com/newlist/p/3532836.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!