zoukankan      html  css  js  c++  java
  • 精灵灰化

    //-----------------------------------------------------------------
    //@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__
    #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 
     
                precision mediump float; 
     
                #endif 
     
                uniform sampler2D u_texture; 
     
                varying vec2 v_texCoord; 
     
                varying vec4 v_fragmentColor; 
     
                void main(void) 
     
                { 
     
                // Convert to greyscale using NTSC weightings 
     
                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); 
     
                }";
    
            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);
    }
  • 相关阅读:
    HTTP协议图解
    .NET 发布网站步骤
    使用php在服务器端生成图文验证码
    SQLServer复习文档1(with C#)
    理解 JavaScript 原型 / 原型链
    浅谈瀑布流
    懒加载
    jQuery ajax
    jQuery 动画效果 与 动画队列
    jQuery 事件
  • 原文地址:https://www.cnblogs.com/newlist/p/3532836.html
Copyright © 2011-2022 走看看