Fixed premultiplied images (the default).

This commit is contained in:
NathanSweet 2013-05-12 14:22:12 +02:00
parent beeff7d752
commit a4bd912dc1
8 changed files with 54 additions and 10 deletions

View File

@ -35,8 +35,9 @@ Draws a skeleton.
float _timeScale;
bool _debugSlots;
bool _debugBones;
bool _premultipliedAlpha;
ccBlendFunc _blendFunc;
ccBlendFunc _blendFunc;
bool _ownsSkeletonData;
Atlas* _atlas;
}

View File

@ -58,6 +58,7 @@
_blendFunc.src = GL_ONE;
_blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
[self setOpacityModifyRGB:YES];
_timeScale = 1;
@ -132,6 +133,11 @@
_skeleton->g = color.g / (float)255;
_skeleton->b = color.b / (float)255;
_skeleton->a = self.opacity / (float)255;
if (_premultipliedAlpha) {
_skeleton->r *= _skeleton->a;
_skeleton->g *= _skeleton->a;
_skeleton->b *= _skeleton->a;
}
CCTextureAtlas* textureAtlas = 0;
ccV3F_C4B_T2F_Quad quad;
@ -153,7 +159,7 @@
textureAtlas = regionTextureAtlas;
if (textureAtlas.capacity == textureAtlas.totalQuads &&
![textureAtlas resizeCapacity:textureAtlas.capacity * 2]) return;
RegionAttachment_updateQuad(attachment, slot, &quad);
RegionAttachment_updateQuad(attachment, slot, &quad, _premultipliedAlpha);
[textureAtlas updateQuad:&quad atIndex:textureAtlas.totalQuads];
}
if (textureAtlas) {
@ -171,7 +177,7 @@
Slot* slot = _skeleton->slots[i];
if (!slot->attachment || slot->attachment->type != ATTACHMENT_REGION) continue;
RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
RegionAttachment_updateQuad(attachment, slot, &quad);
RegionAttachment_updateQuad(attachment, slot, &quad, _premultipliedAlpha);
points[0] = ccp(quad.bl.vertices.x, quad.bl.vertices.y);
points[1] = ccp(quad.br.vertices.x, quad.br.vertices.y);
points[2] = ccp(quad.tr.vertices.x, quad.tr.vertices.y);
@ -283,4 +289,12 @@
return _blendFunc;
}
- (void) setOpacityModifyRGB:(BOOL)value {
_premultipliedAlpha = value;
}
- (BOOL) doesOpacityModifyRGB {
return _premultipliedAlpha;
}
@end

View File

@ -33,7 +33,7 @@ namespace spine {
extern "C" {
#endif
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad);
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha);
#ifdef __cplusplus
}

View File

@ -46,14 +46,20 @@ char* _Util_readFile (const char* path, int* length) {
/**/
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad) {
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha) {
float vertices[8];
RegionAttachment_computeVertices(self, slot, vertices);
GLubyte r = slot->skeleton->r * slot->r * 255;
GLubyte g = slot->skeleton->g * slot->g * 255;
GLubyte b = slot->skeleton->b * slot->b * 255;
GLubyte a = slot->skeleton->a * slot->a * 255;
float normalizedAlpha = slot->skeleton->a * slot->a;
if (premultipliedAlpha) {
r *= normalizedAlpha;
g *= normalizedAlpha;
b *= normalizedAlpha;
}
GLubyte a = normalizedAlpha * 255;
quad->bl.colors.r = r;
quad->bl.colors.g = g;
quad->bl.colors.b = b;

View File

@ -57,6 +57,7 @@ void CCSkeleton::initialize () {
blendFunc.src = GL_ONE;
blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
setOpacityModifyRGB(true);
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
scheduleUpdate();
@ -124,6 +125,11 @@ void CCSkeleton::draw () {
skeleton->g = color.g / (float)255;
skeleton->b = color.b / (float)255;
skeleton->a = getOpacity() / (float)255;
if (premultipliedAlpha) {
skeleton->r *= skeleton->a;
skeleton->g *= skeleton->a;
skeleton->b *= skeleton->a;
}
CCTextureAtlas* textureAtlas = 0;
ccV3F_C4B_T2F_Quad quad;
@ -145,7 +151,7 @@ void CCSkeleton::draw () {
textureAtlas = regionTextureAtlas;
if (textureAtlas->getCapacity() == textureAtlas->getTotalQuads() &&
!textureAtlas->resizeCapacity(textureAtlas->getCapacity() * 2)) return;
RegionAttachment_updateQuad(attachment, slot, &quad);
RegionAttachment_updateQuad(attachment, slot, &quad, premultipliedAlpha);
textureAtlas->updateQuad(&quad, textureAtlas->getTotalQuads());
}
if (textureAtlas) {
@ -272,4 +278,12 @@ void CCSkeleton::setBlendFunc (ccBlendFunc blendFunc) {
this->blendFunc = blendFunc;
}
void CCSkeleton::setOpacityModifyRGB (bool value) {
premultipliedAlpha = value;
}
bool CCSkeleton::isOpacityModifyRGB () {
return premultipliedAlpha;
}
}

View File

@ -41,6 +41,7 @@ public:
float timeScale;
bool debugSlots;
bool debugBones;
bool premultipliedAlpha;
static CCSkeleton* createWithData (SkeletonData* skeletonData, bool ownsSkeletonData = false);
static CCSkeleton* createWithFile (const char* skeletonDataFile, Atlas* atlas, float scale = 1);
@ -80,6 +81,8 @@ public:
// --- CCBlendProtocol
CC_PROPERTY(cocos2d::ccBlendFunc, blendFunc, BlendFunc);
virtual void setOpacityModifyRGB (bool value);
virtual bool isOpacityModifyRGB ();
protected:
CCSkeleton ();

View File

@ -51,14 +51,20 @@ char* _Util_readFile (const char* path, int* length) {
/**/
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad) {
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, ccV3F_C4B_T2F_Quad* quad, bool premultipliedAlpha) {
float vertices[8];
RegionAttachment_computeVertices(self, slot, vertices);
GLubyte r = slot->skeleton->r * slot->r * 255;
GLubyte g = slot->skeleton->g * slot->g * 255;
GLubyte b = slot->skeleton->b * slot->b * 255;
GLubyte a = slot->skeleton->a * slot->a * 255;
float normalizedAlpha = slot->skeleton->a * slot->a;
if (premultipliedAlpha) {
r *= normalizedAlpha;
g *= normalizedAlpha;
b *= normalizedAlpha;
}
GLubyte a = normalizedAlpha * 255;
quad->bl.colors.r = r;
quad->bl.colors.g = g;
quad->bl.colors.b = b;

View File

@ -31,6 +31,6 @@
#include <spine/CCSkeleton.h>
#include <spine/CCSkeletonAnimation.h>
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, cocos2d::ccV3F_C4B_T2F_Quad* quad);
void RegionAttachment_updateQuad (RegionAttachment* self, Slot* slot, cocos2d::ccV3F_C4B_T2F_Quad* quad, bool premultiplied = false);
#endif /* SPINE_COCOS2DX_H_ */