灰色图片

第一种实现

 
 
  1. // 第一种
  2. void HelloWorld::spriteToGrey(Sprite* sprite_chess)
  3. {
  4. Point point = sprite_chess->getPosition();
  5. sprite_chess->setPosition(sprite_chess->getContentSize().width / 2, sprite_chess->getContentSize().height / 2);
  6. RenderTexture *render = RenderTexture::create(sprite_chess->getContentSize().width, sprite_chess->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
  7. render->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f);
  8. sprite_chess->visit();
  9. render->end();
  10. Director::getInstance()->getRenderer()->render();
  11. Image *finalImage = render->newImage();
  12. unsigned char *pData = finalImage->getData();
  13. int iIndex = 0;
  14. for (int i = 0; i < finalImage->getHeight(); i++)
  15. {
  16. for (int j = 0; j < finalImage->getWidth(); j++)
  17. {
  18. // gray
  19. int iBPos = iIndex;
  20. unsigned int iB = pData[iIndex];
  21. iIndex++;
  22. unsigned int iG = pData[iIndex];
  23. iIndex++;
  24. unsigned int iR = pData[iIndex];
  25. iIndex++;
  26. iIndex++;
  27. unsigned int iGray = 0.3 * iR + 0.6 * iG + 0.1 * iB;
  28. pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray;
  29. }
  30. }
  31. Texture2D *texture = new Texture2D;
  32. texture->initWithImage(finalImage);
  33. delete finalImage;
  34. sprite_chess->setTexture(texture);
  35. texture->release();
  36. sprite_chess->setPosition(point);
  37. }

第二种实现 - 第二种实现较好

 
 
  1. //实现Sprite或者ImageView类型的图片变灰(已修正位置偏移问题)
  2. void HelloWorld::spriteToGrey2(Node* tempSp)
  3. {
  4. ImageView* imgView = dynamic_cast<ImageView*>(tempSp);
  5. Sprite* imgView2 = dynamic_cast<Sprite*>(tempSp);
  6. Sprite* sp = nullptr;
  7. if (imgView) {
  8. Scale9Sprite* scale9sp = dynamic_cast<Scale9Sprite*>(imgView->getVirtualRenderer());
  9. if (scale9sp) {
  10. sp = scale9sp->getSprite();
  11. }
  12. }
  13. else if (imgView2) {
  14. sp = imgView2;
  15. }
  16. if (!sp) {
  17. return;
  18. }
  19. const GLchar* pszFragSource =
  20. "#ifdef GL_ES \n\
  21. precision mediump float; \n\
  22. #endif \n\
  23. varying vec2 v_texCoord; \n\
  24. varying vec4 v_fragmentColor; \n\
  25. void main(void) \n\
  26. { \n\
  27. // Convert to greyscale using NTSC weightings \n\
  28. vec4 col = v_fragmentColor * texture2D(CC_Texture0, v_texCoord); \n\
  29. float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n\
  30. gl_FragColor = vec4(grey, grey, grey, col.a); \n\
  31. }";
  32. GLProgram* pProgram = new GLProgram();
  33. pProgram->initWithByteArrays(ccPositionTextureColor_noMVP_vert, pszFragSource);
  34. sp->setGLProgram(pProgram);
  35. pProgram->release();
  36. CHECK_GL_ERROR_DEBUG();
  37. sp->getGLProgram()->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
  38. sp->getGLProgram()->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
  39. sp->getGLProgram()->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD);
  40. CHECK_GL_ERROR_DEBUG();
  41. sp->getGLProgram()->link();
  42. CHECK_GL_ERROR_DEBUG();
  43. sp->getGLProgram()->updateUniforms();
  44. CHECK_GL_ERROR_DEBUG();
  45. }




转载于:https://www.cnblogs.com/nfking/p/5615763.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值