zoukankan      html  css  js  c++  java
  • [zz]cc灰白图生成

    游戏中人物死掉后要把人物头像变成灰白图,很多游戏也是这样处理,问题来了,怎么将CCsprite生成的图变成灰白呢?

     

    看了下实现,基本有了办法。CCSprite是在initWithTexture的时候渲染的贴图,如果在这里面设置一个一个灰度Shader,也许可以将图片改成灰白色。

     

    GL Shader脚本代码:



    1. #ifdef GL_ES  
    2. precision mediump float;  
    3. #endif  
    4. uniform sampler2D u_texture;  
    5. varying vec2 v_texCoord;  
    6. varying vec4 v_fragmentColor;  
    7. void main(void)  
    8. {  
    9. // Convert to greyscale using NTSC weightings  
    10. float alpha = texture2D(u_texture, v_texCoord).a;  
    11. float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114));  
    12. gl_FragColor = vec4(grey, grey, grey, alpha);  
    13. }  




    这个主要目的是将RGB值转换为YUV值,Y就是灰度,所以我们取出里面的Y值来实现我们的灰白图。

     

    Google了下,vec3(0.299, 0.587, 0.114) 是RGB转YUV的参数值。

     

    计算完了之后根据原来的纹理alpha值,输出我们处理后的颜色到gl_FragColor,就可以让shader去渲染黑白图。

     

    里面主要是在光栅化后的片段进行颜色处理,最后输出的是像素,它不会产生额外纹理,所以不会卡。

     

    注意:GLSL1.0的可能有些函数不支持,会crash。不过目前基本可以不考虑。

     

    下面就是我写好的类,加入自己的工程就可以了。

     

    头文件

    1. //  
    2. // BYGraySprite.h  
    3. // Demo  
    4. //  
    5. // Created by Yanghui Liu on 12-11-2.  
    6. // Copyright (c) 2012年 BoyoJoy. All rights reserved.  
    7. //  
    8.   
    9.   
    10. #ifndef Demo_BYGraySprite_h  
    11. #define Demo_BYGraySprite_h  
    12.   
    13.   
    14. #include "cocoa/CCGeometry.h"  
    15. #include "cocos2d.h"  
    16. USING_NS_CC;  
    17.   
    18.   
    19. class BYGraySprite : public CCSprite{  
    20.   
    21.   
    22. public:  
    23.  BYGraySprite();  
    24.  virtual ~BYGraySprite();  
    25.  static BYGraySprite* create(const char* pszFileName);  
    26.  bool initWithTexture(CCTexture2D* pTexture, const CCRect& tRect);  
    27.  virtual void draw();  
    28.   
    29.   
    30. };  
    31.   
    32.   
    33. #endif  



    cpp文件


    1. //  
    2. // BYGraySprite.cpp  
    3. // Demo  
    4. //  
    5. // Created by Yanghui Liu on 12-11-2.  
    6. // Copyright (c) 2012年 BoyoJoy. All rights reserved.  
    7. //  
    8.   
    9.   
    10. #include "BYGraySprite.h"  
    11.   
    12.   
    13. BYGraySprite::BYGraySprite(){  
    14.   
    15.   
    16. }  
    17.   
    18.   
    19. BYGraySprite::~BYGraySprite(){  
    20.   
    21.   
    22. }  
    23.   
    24.   
    25. BYGraySprite* BYGraySprite::create( const char* pszFileName ){  
    26.  BYGraySprite* graySprite = new BYGraySprite;  
    27.  if (graySprite && graySprite->initWithFile(pszFileName)){  
    28.  graySprite->autorelease();  
    29.  return graySprite;  
    30.  }else{  
    31.  CC_SAFE_RELEASE(graySprite);  
    32.  return NULL;  
    33.  }  
    34. }  
    35.   
    36.   
    37. bool BYGraySprite::initWithTexture(CCTexture2D* pTexture, const CCRect& tRect ){  
    38.  do{  
    39.  CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, tRect));  
    40.   
    41.   
    42.  GLchar* pszFragSource =  
    43.  "#ifdef GL_ES     
    44.  precision mediump float;     
    45.  #endif     
    46.  uniform sampler2D u_texture;     
    47.  varying vec2 v_texCoord;     
    48.  varying vec4 v_fragmentColor;     
    49.  void main(void)     
    50.  {     
    51.  // Convert to greyscale using NTSC weightings     
    52.  float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114));     
    53.  gl_FragColor = vec4(grey, grey, grey, 1.0);     
    54.  }";  
    55.   
    56.   
    57.  CCGLProgram* pProgram = new CCGLProgram();  
    58.  pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);  
    59.  this->setShaderProgram(pProgram);  
    60.  pProgram->release();  
    61.  CHECK_GL_ERROR_DEBUG();  
    62.   
    63.   
    64.  this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);  
    65.  this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);  
    66.  this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);  
    67.  CHECK_GL_ERROR_DEBUG();  
    68.   
    69.   
    70.  this->getShaderProgram()->link();  
    71.  CHECK_GL_ERROR_DEBUG();  
    72.   
    73.   
    74.  this->getShaderProgram()->updateUniforms();  
    75.  CHECK_GL_ERROR_DEBUG();  
    76.   
    77.   
    78.  return true;  
    79.  } while (0);  
    80.  return false;  
    81. }  
    82.   
    83.   
    84. void BYGraySprite::draw(){  
    85.  ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );  
    86.  ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );  
    87.   
    88.   
    89.  this->getShaderProgram()->use();  
    90.  this->getShaderProgram()->setUniformForModelViewProjectionMatrix();  
    91.   
    92.   
    93.  ccGLBindTexture2D( this->getTexture()->getName() );  
    94.   
    95.   
    96. #define kQuadSize sizeof(m_sQuad.bl)  
    97.  long offset = (long)&m_sQuad;  
    98.   
    99.   
    100.  // vertex  
    101.  int diff = offsetof( ccV3F_C4B_T2F, vertices);  
    102.  glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));  
    103.   
    104.   
    105.  // texCoods  
    106.  diff = offsetof( ccV3F_C4B_T2F, texCoords);  
    107.  glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));  
    108.   
    109.   
    110.  // color  
    111.  diff = offsetof( ccV3F_C4B_T2F, colors);  
    112.  glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));  
    113.  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
    114.  CC_INCREMENT_GL_DRAWS(1);  
    115. }  





    使用方法:

    1. BYGraySprite* graySprite = BYGraySprite::create("Icon.png");  
    2.  graySprite->setPosition( ccp(size.width/2, size.height/2) );  
    3.  this->addChild(graySprite);  

    二、之前alpha值是直接用的1,所以透明图会渲染成黑色。把原来纹理的alpha取出来并使用。就可以避免该问题。

    也就是将GL脚本代码中的

    float grey = dot(texture2D(u_texture, v_texCoord).rgb, vec3(0.299, 0.587, 0.114));

    gl_FragColor = vec4(grey, grey, grey, 1.0);

    改成:

    vec4 col = texture2D(u_texture, v_texCoord);

    float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114));

    gl_FragColor = vec4(grey, grey, grey, col.a);

  • 相关阅读:
    C语言I作业12—学期总结
    C语言I博客作业11
    C语言I博客作业10
    预习非数值数据的编码方式
    计算机作业
    C语言||作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
  • 原文地址:https://www.cnblogs.com/wishing/p/3493795.html
Copyright © 2011-2022 走看看