cocos2d-x ccsprite 使用 shader生成自己想要的颜色的图片

来源:互联网 发布:淘宝怎么写评论 编辑:程序博客网 时间:2024/06/04 23:20
[cpp] view plaincopy
  1. class BYGraySprite : public CCSprite{  
  2.       
  3. public:  
  4.     BYGraySprite();  
  5.     virtual ~BYGraySprite();  
  6.     static BYGraySprite* create(const char* pszFileName);  
  7.     bool initWithTexture(CCTexture2D* pTexture, const CCRect& tRect);  
  8.     virtual void draw();  
  9. };  
[cpp] view plaincopy
  1. #include "BYGraySprite.h"  
  2. BYGraySprite::BYGraySprite(){  
  3.       
  4. }  
  5.   
  6. BYGraySprite::~BYGraySprite(){  
  7.       
  8. }  
  9.   
  10. BYGraySprite* BYGraySprite::create( const char* pszFileName ){  
  11.     BYGraySprite* graySprite = new BYGraySprite;  
  12.     if (graySprite && graySprite->initWithFile(pszFileName)){  
  13.         graySprite->autorelease();  
  14.         return graySprite;  
  15.     }else{  
  16.         CC_SAFE_RELEASE(graySprite);  
  17.         return NULL;  
  18.     }  
  19. }  
  20.   
  21. bool BYGraySprite::initWithTexture(CCTexture2D* pTexture, const CCRect& tRect ){  
  22.     do{  
  23.         CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, tRect));  
  24.           
  25.         GLchar* pszFragSource =  
  26.         "#ifdef GL_ES \n \  
  27.         precision mediump float; \n \  
  28.         #endif \n \  
  29.         uniform sampler2D u_texture; \n \  
  30.         varying vec2 v_texCoord; \n \  
  31.         varying vec4 v_fragmentColor; \n \  
  32.         void main(void) \n \  
  33.         { \n \  
  34.         // Convert to greyscale using NTSC weightings \n \  
  35.         float grey = dot(texture2D(u_texture, v_texCoord).rgba, vec4(0.5, 0.0, 0.0,0.7)); \n \  
  36.         gl_FragColor = vec4(grey, 0.0, 0.0, 0.0); \n \  
  37.         }";  
  38.           
  39.         CCGLProgram* pProgram = new CCGLProgram();  
  40.         pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);  
  41.         this->setShaderProgram(pProgram);  
  42.         pProgram->release();  
  43.         CHECK_GL_ERROR_DEBUG();  
  44.           
  45.         this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);  
  46.         this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);  
  47.         this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);  
  48.         CHECK_GL_ERROR_DEBUG();  
  49.           
  50.         this->getShaderProgram()->link();  
  51.         CHECK_GL_ERROR_DEBUG();  
  52.           
  53.         this->getShaderProgram()->updateUniforms();  
  54.         CHECK_GL_ERROR_DEBUG();  
  55.           
  56.         return true;  
  57.     } while (0);  
  58.     return false;  
  59. }  
  60.   
  61. void BYGraySprite::draw(){  
  62.     ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );  
  63.     ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );  
  64.       
  65.     this->getShaderProgram()->use();  
  66.     this->getShaderProgram()->setUniformForModelViewProjectionMatrix();  
  67.       
  68.     ccGLBindTexture2D( this->getTexture()->getName() );  
  69.       
  70. #define kQuadSize sizeof(m_sQuad.bl)  
  71.     long offset = (long)&m_sQuad;  
  72.       
  73.     // vertex  
  74.     int diff = offsetof( ccV3F_C4B_T2F, vertices);  
  75.     glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));  
  76.       
  77.     // texCoods  
  78.     diff = offsetof( ccV3F_C4B_T2F, texCoords);  
  79.     glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));  
  80.       
  81.     // color  
  82.     diff = offsetof( ccV3F_C4B_T2F, colors);  
  83.     glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));  
  84.     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
  85.     CC_INCREMENT_GL_DRAWS(1);  
  86. }  

使用

[cpp] view plaincopy
  1. BYGraySprite* graySprite = BYGraySprite::create("boss.png");  
  2. graySprite->setPosition(ccp(480,320) );  
  3. addChild(graySprite);  
0 0