分享

cocos2dx 图片变灰及正常显示实现(lua可以调用)

 勤奋不止 2013-12-09

图片变灰, 采用shader就可以实现,  

有2中方法,

1,  像CCSprite一样create

2.  把CCSprite传进来, 并把图片要不要变灰的flag传进来, 具体实现看代码和后面的使用方法


-----------------------------------------

GraySprite.h

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
//
//  GraySprite.h
//  goddess
//
//  Created by rekoo on 13-7-23.
//
//
#ifndef __goddess__GraySprite__
#define __goddess__GraySprite__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class GraySprite : public CCSprite
{
public:
    GraySprite();
    ~GraySprite();
                                            
    bool initWithTexture(CCTexture2D* texture, const CCRect&  rect);
    void draw();
    void initProgram();
    void listenBackToForeground(CCObject *obj);
                                            
    static GraySprite* create(const char *pszFileName);
                                            
};
#endif /* defined(__goddess__GraySprite__) */


GraySprite.cpp

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
//  GraySprite.cpp
//  goddess
//
//  Created by rekoo on 13-7-23.
//
//
// 用shader创建灰度图, 用法跟sprite一样
//  lua用法: local sprite = GraySprite:create("pic.png")
#include "GraySprite.h"
GraySprite::GraySprite()
{
}
GraySprite::~GraySprite()
{
}
GraySprite* GraySprite::create(const char *pszFileName)
{
    GraySprite* pRet = new GraySprite();
    if (pRet && pRet->initWithFile(pszFileName))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
                                        
    return pRet;
}
void GraySprite::listenBackToForeground(CCObject *obj)
{
    setShaderProgram(NULL);
    initProgram();
}
bool GraySprite::initWithTexture(CCTexture2D* texture, const CCRect& rect)
{
    if( CCSprite::initWithTexture(texture, rect) )
    {
                                                    
        CCSize s = getTexture()->getContentSizeInPixels();
                                         
        this->initProgram();
                                            
        return true;
    }
                                        
    return false;
}
void GraySprite::initProgram()
{
//    GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
//                                                                       CCFileUtils::sharedFileUtils()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();
                                        
  const  GLchar * pfrag =    "#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 \
    float alpha = texture2D(u_texture, v_texCoord).a; \n \
    float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299,0.587,0.114)); \n \
    gl_FragColor = vec4(grey, grey, grey,alpha); \n \
    } ";
                                        
                                     
    CCGLProgram* pProgram = new CCGLProgram();
    pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pfrag);
    setShaderProgram(pProgram);
    pProgram->release();
                                        
    CHECK_GL_ERROR_DEBUG();
                                        
    getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
    getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
    getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
                                        
    CHECK_GL_ERROR_DEBUG();
                                        
    getShaderProgram()->link();
                                        
    CHECK_GL_ERROR_DEBUG();
                                        
    getShaderProgram()->updateUniforms();
                                        
}
void GraySprite::draw()
{
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
    ccBlendFunc blend = getBlendFunc();
    ccGLBlendFunc(blend.src, blend.dst);
                                        
    getShaderProgram()->use();
    getShaderProgram()->setUniformsForBuiltins();
                                        
                                        
    ccGLBindTexture2D( getTexture()->getName());
                                        
    //
    // Attributes
    //
#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);
}



--------------------------------

使用例子:

1.  GraySprite * gs = GraySprite::create("01.png");

剩下跟CCSprite一样, 设置位置, addChild等等

2. CCSprite * cs = CCSprite::create("02.png");

GraySprite::setGray(cs, 1);   //变灰

GraySprite::setGray(cs, 0);  //原色


-----------------------------

把GraySprte暴露出去, lua就可以调用, 很方便

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多