分类:cocos2dx
2013-05-30 11:14
6914人阅读
收藏
举报
- classBYGraySprite:publicCCSprite{
-
- public:
- BYGraySprite();
- virtual~BYGraySprite();
- staticBYGraySprite*create(constchar*pszFileName);
- boolinitWithTexture(CCTexture2D*pTexture,constCCRect&tRect);
- virtualvoiddraw();
- };
#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_ESn
- precisionmediumpfloat;n
- #endifn
- uniformsampler2Du_texture;n
- varyingvec2v_texCoord;n
- varyingvec4v_fragmentColor;n
- voidmain(void)n
- {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;
-
- 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);
- }
使用
BYGraySprite*graySprite=BYGraySprite::create("boss.png");
- graySprite->setPosition(ccp(480,320));
- addChild(graySprite);
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|