Cocos2dx - CCSprite使用 shader生成自己想要的颜色的图片

前端之家收集整理的这篇文章主要介绍了Cocos2dx - CCSprite使用 shader生成自己想要的颜色的图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

分类cocos2dx 6914人阅读 评论(1) 收藏 举报
  1. classBYGraySprite:publicCCSprite{
  2. public:
  3. BYGraySprite();
  4. virtual~BYGraySprite();
  5. staticBYGraySprite*create(constchar*pszFileName);
  6. boolinitWithTexture(CCTexture2D*pTexture,constCCRect&tRect);
  7. virtualvoiddraw();
  8. };
copy
    #include"BYGraySprite.h"
  1. BYGraySprite::BYGraySprite(){
  2. }
  3. BYGraySprite::~BYGraySprite(){
  4. BYGraySprite*BYGraySprite::create(char*pszFileName){
  5. BYGraySprite*graySprite=newBYGraySprite;
  6. if(graySprite&&graySprite->initWithFile(pszFileName)){
  7. graySprite->autorelease();
  8. returngraySprite;
  9. }else{
  10. CC_SAFE_RELEASE(graySprite);
  11. returnNULL;
  12. }
  13. boolBYGraySprite::initWithTexture(CCTexture2D*pTexture,153); font-weight:bold; background-color:inherit">constCCRect&tRect){
  14. do{
  15. CC_BREAK_IF(!CCSprite::initWithTexture(pTexture,tRect));
  16. GLchar*pszFragSource=
  17. "#ifdefGL_ES\n\
  18. precisionmediumpfloat;\n\
  19. #endif\n\
  20. uniformsampler2Du_texture;\n\
  21. varyingvec2v_texCoord;\n\
  22. varyingvec4v_fragmentColor;\n\
  23. voidmain(void)\n\
  24. {\n\
  25. //ConverttogreyscaleusingNTSCweightings\n\
  26. floatgrey=dot(texture2D(u_texture,v_texCoord).rgba,vec4(0.5,0.0,0.7));\n\
  27. gl_FragColor=vec4(grey,0.0);\n\
  28. }";
  29. CCGLProgram*pProgram=newCCGLProgram();
  30. pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert,pszFragSource);
  31. this->setShaderProgram(pProgram);
  32. pProgram->release();
  33. CHECK_GL_ERROR_DEBUG();
  34. this->getShaderProgram()->addAttribute(kCCAttributeNamePosition,kCCVertexAttrib_Position);
  35. this->getShaderProgram()->addAttribute(kCCAttributeNameColor,kCCVertexAttrib_Color);
  36. this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord,kCCVertexAttrib_TexCoords);
  37. CHECK_GL_ERROR_DEBUG();
  38. this->getShaderProgram()->link();
  39. this->getShaderProgram()->updateUniforms();
  40. returntrue;
  41. while(0);
  42. false;
  43. voidBYGraySprite::draw(){
  44. ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
  45. ccGLBlendFunc(m_sBlendFunc.src,m_sBlendFunc.dst);
  46. this->getShaderProgram()->use();
  47. this->getShaderProgram()->setUniformForModelViewProjectionMatrix();
  48. ccGLBindTexture2D(this->getTexture()->getName());
  49. #definekQuadSizesizeof(m_sQuad.bl)
  50. longoffset=(long)&m_sQuad;
  51. //vertex
  52. intdiff=offsetof(ccV3F_C4B_T2F,vertices);
  53. glVertexAttribPointer(kCCVertexAttrib_Position,3,GL_FLOAT,GL_FALSE,kQuadSize,(void*)(offset+diff));
  54. //texCoods
  55. diff=offsetof(ccV3F_C4B_T2F,texCoords);
  56. glVertexAttribPointer(kCCVertexAttrib_TexCoords,2,0); background-color:inherit">//color
  57. glVertexAttribPointer(kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,GL_TRUE,248)"> glDrawArrays(GL_TRIANGLE_STRIP,4);
  58. CC_INCREMENT_GL_DRAWS(1);
  59. }

使用

copy
    BYGraySprite*graySprite=BYGraySprite::create("boss.png");
  1. graySprite->setPosition(ccp(480,320));
  2. addChild(graySprite);
原文链接:https://www.f2er.com/cocos2dx/345276.html

猜你在找的Cocos2d-x相关文章