码迷,mamicode.com
首页 > 其他好文 > 详细

灰色图片

时间:2016-06-25 09:32:25      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

第一种实现

  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. }




灰色图片

标签:

原文地址:http://www.cnblogs.com/nfking/p/5615763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!