- classBYGraySprite:publicCCSprite{
- public:
- BYGraySprite();
- virtual~BYGraySprite();
- staticBYGraySprite*create(constchar*pszFileName);
- boolinitWithTexture(CCTexture2D*pTexture,constCCRect&tRect);
- virtualvoiddraw();
- };
copy
- #include"BYGraySprite.h"
- BYGraySprite::BYGraySprite(){
- }
- BYGraySprite::~BYGraySprite(){
- BYGraySprite*BYGraySprite::create(char*pszFileName){
- BYGraySprite*graySprite=newBYGraySprite;
- if(graySprite&&graySprite->initWithFile(pszFileName)){
- graySprite->autorelease();
- returngraySprite;
- }else{
- CC_SAFE_RELEASE(graySprite);
- returnNULL;
- }
- boolBYGraySprite::initWithTexture(CCTexture2D*pTexture,153); font-weight:bold; background-color:inherit">constCCRect&tRect){
- do{
- CC_BREAK_IF(!CCSprite::initWithTexture(pTexture,tRect));
- GLchar*pszFragSource=
- "#ifdefGL_ES\n\
- precisionmediumpfloat;\n\
- #endif\n\
- uniformsampler2Du_texture;\n\
- varyingvec2v_texCoord;\n\
- varyingvec4v_fragmentColor;\n\
- voidmain(void)\n\
- {\n\
- //ConverttogreyscaleusingNTSCweightings\n\
- floatgrey=dot(texture2D(u_texture,v_texCoord).rgba,vec4(0.5,0.0,0.7));\n\
- gl_FragColor=vec4(grey,0.0);\n\
- }";
- CCGLProgram*pProgram=newCCGLProgram();
- pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert,pszFragSource);
- this->setShaderProgram(pProgram);
- pProgram->release();
- CHECK_GL_ERROR_DEBUG();
- this->getShaderProgram()->addAttribute(kCCAttributeNamePosition,kCCVertexAttrib_Position);
- this->getShaderProgram()->addAttribute(kCCAttributeNameColor,kCCVertexAttrib_Color);
- this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord,kCCVertexAttrib_TexCoords);
- CHECK_GL_ERROR_DEBUG();
- this->getShaderProgram()->link();
- this->getShaderProgram()->updateUniforms();
- returntrue;
- while(0);
- false;
- voidBYGraySprite::draw(){
- ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex);
- ccGLBlendFunc(m_sBlendFunc.src,m_sBlendFunc.dst);
- this->getShaderProgram()->use();
- this->getShaderProgram()->setUniformForModelViewProjectionMatrix();
- ccGLBindTexture2D(this->getTexture()->getName());
- #definekQuadSizesizeof(m_sQuad.bl)
- longoffset=(long)&m_sQuad;
- //vertex
- intdiff=offsetof(ccV3F_C4B_T2F,vertices);
- glVertexAttribPointer(kCCVertexAttrib_Position,3,GL_FLOAT,GL_FALSE,kQuadSize,(void*)(offset+diff));
- //texCoods
- diff=offsetof(ccV3F_C4B_T2F,texCoords);
- glVertexAttribPointer(kCCVertexAttrib_TexCoords,2,0); background-color:inherit">//color
- glVertexAttribPointer(kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,GL_TRUE,248)"> glDrawArrays(GL_TRIANGLE_STRIP,4);
- CC_INCREMENT_GL_DRAWS(1);
- }