我想将 ccsprite 分成多个部分。为此,我正在使用 Cocos2dx。我正在做的是我对 CCsprite 类进行子类化,然后执行以下代码
cocos2d::CCRenderTexture *rt = new cocos2d::CCRenderTexture();
rt->initWithWidthAndHeight(sprite->getContentSize().width, sprite->getContentSize().height, cocos2d::kCCTexture2DPixelFormat_RGB565);
rt->begin();
CCSprite *s2 = CCSprite::createWithTexture(sprite->getTexture(), sprite->getTextureRect());
s2->setPosition(ccp(sprite->getContentSize().width/2,sprite->getContentSize().height/2));
s2->visit();
rt->end();
shatteredSpriteObj->setTexture(rt->getSprite()->getTexture());
//shatteredSpriteObj->createWithTexture(rt->getSprite()->getTexture());
cocos2d::CCRect spriteRect = sprite->getTextureRect();
spriteRect.origin.x = 0;
spriteRect.origin.y = 0;
spriteRect.size.height = sprite->getContentSize().height;
spriteRect.size.width = sprite->getContentSize().width;
shatteredSpriteObj->setTextureRect(spriteRect);
float wid = sprite->getContentSize().width;
float hgt = sprite->getContentSize().height;
float pieceXsize = wid/(float)piecesX;
float pieceYsize = hgt/(float)piecesY;
float texWid = wid/shatteredSpriteObj->getTexture()->getPixelsWide();
float texHgt = hgt/shatteredSpriteObj->getTexture()->getPixelsHigh();
//cocos2d::ccColor4B color4 = {255,4,255, _realOpacity };
cocos2d::ccColor4B color4 = {_realColor.r, _realColor.g, _realColor.b, _realOpacity };
TriangleColors triColor4 = { color4, color4, color4 };
//cocos2d::CCPoint *ptArray[piecesX+1][piecesY+1];
std::vector<std::vector<cocos2d::CCPoint> > ptArray;
for (int x=0; x<=piecesX; x++) {
std::vector<cocos2d::CCPoint> row;
for (int y=0; y<=piecesY; y++) {
cocos2d::CCPoint pt = ccp((x*pieceXsize), (y*pieceYsize));
if (x>0 && x<piecesX && y>0 && y<piecesY) {
pt = ccpAdd(pt, ccp(roundf(randf(0.0, pieceXsize*0.45)), roundf(randf(0.0, pieceYsize*0.45))));
}
row.push_back(pt);
}
ptArray.push_back(row);
}
numVertices = 0;
for (int x=0; x<piecesX; x++) {
for (int y=0; y<piecesY; y++) {
if (numVertices>=SHATTER_VERTEX_MAX) {
CCLOG("NeedABiggerArray!");
return;
}
vdelta[numVertices] = ccp(randf(0.0, speedVar), randf(0.0, speedVar));
adelta[numVertices] = randf(0.0, rotVar);
colorArray[numVertices] = triColor4;
centerPt[numVertices] = ccp((x*pieceXsize)+(pieceXsize*0.3), (y*pieceYsize)+(pieceYsize*0.3));
vertices[numVertices] = tri(ptArray.at(x).at(y),
ptArray.at(x+1).at(y),
ptArray.at(x).at(y+1));
texCoords[numVertices++] = tri(ccp((ptArray.at(x).at(y).x/wid)*texWid, (ptArray.at(x).at(y).y/hgt)*texHgt),
ccp((ptArray.at(x+1).at(y).x/wid)*texWid, (ptArray.at(x+1).at(y).y/hgt)*texHgt),
ccp((ptArray.at(x).at(y+1).x/wid)*texWid, (ptArray.at(x).at(y+1).y/hgt)*texHgt));
vdelta[numVertices] = ccp(randf(0.0, speedVar), randf(0.0, speedVar));
adelta[numVertices] = randf(0.0, rotVar);
colorArray[numVertices] = triColor4;
vertices[numVertices] = tri(ptArray.at(x+1).at(y),
ptArray.at(x+1).at(y+1),
ptArray.at(x).at(y+1));
texCoords[numVertices++] = tri(ccp((ptArray.at(x+1).at(y).x/wid)*texWid, (ptArray.at(x+1).at(y).y/hgt)*texHgt),
ccp((ptArray.at(x+1).at(y+1).x/wid)*texWid, (ptArray.at(x+1).at(y+1).y/hgt)*texHgt),
ccp((ptArray.at(x).at(y+1).x/wid)*texWid, (ptArray.at(x).at(y+1).y/hgt)*texHgt));
}
}
然后覆盖CCSprite的draw方法
void CustomClass::draw(void){
CC_NODE_DRAW_SETUP();
cocos2d::ccGLEnableVertexAttribs(cocos2d::kCCVertexAttribFlag_Position | cocos2d::kCCVertexAttribFlag_Color);
glBindTexture(GL_TEXTURE_2D, shatteredSpriteObj->getTexture()->getName());
glVertexAttribPointer(cocos2d::kCCVertexAttrib_Position, 2, GL_FLOAT, GL_TRUE, 0, vertices);
glVertexAttribPointer(cocos2d::kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_TRUE, 0, texCoords);
glVertexAttribPointer(cocos2d::kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,0,GL_TRUE,colorArray);
//glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLES, 0, numVertices*3);
}
但问题是 Sprite 正在变成白色并分裂。我知道这是因为给出了这个 glVertexAttribPointer(cocos2d::kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,0,GL_TRUE,colorArray);
我想知道如何破坏图像本身。