From a4bd912dc168e19ce2e1b234bb0110d9728932f2 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 12 May 2013 14:22:12 +0200 Subject: [PATCH] Fixed premultiplied images (the default). --- spine-cocos2d-iphone/src/spine/CCSkeleton.h | 3 ++- spine-cocos2d-iphone/src/spine/CCSkeleton.m | 18 ++++++++++++++++-- .../src/spine/spine-cocos2d-iphone.h | 2 +- .../src/spine/spine-cocos2d-iphone.m | 10 ++++++++-- spine-cocos2dx/src/spine/CCSkeleton.cpp | 16 +++++++++++++++- spine-cocos2dx/src/spine/CCSkeleton.h | 3 +++ spine-cocos2dx/src/spine/spine-cocos2dx.cpp | 10 ++++++++-- spine-cocos2dx/src/spine/spine-cocos2dx.h | 2 +- 8 files changed, 54 insertions(+), 10 deletions(-) diff --git a/spine-cocos2d-iphone/src/spine/CCSkeleton.h b/spine-cocos2d-iphone/src/spine/CCSkeleton.h index 1ae782cb8..c67135d6a 100644 --- a/spine-cocos2d-iphone/src/spine/CCSkeleton.h +++ b/spine-cocos2d-iphone/src/spine/CCSkeleton.h @@ -35,8 +35,9 @@ Draws a skeleton. float _timeScale; bool _debugSlots; bool _debugBones; + bool _premultipliedAlpha; - ccBlendFunc _blendFunc; + ccBlendFunc _blendFunc; bool _ownsSkeletonData; Atlas* _atlas; } diff --git a/spine-cocos2d-iphone/src/spine/CCSkeleton.m b/spine-cocos2d-iphone/src/spine/CCSkeleton.m index 9f52676cc..3b257fc59 100644 --- a/spine-cocos2d-iphone/src/spine/CCSkeleton.m +++ b/spine-cocos2d-iphone/src/spine/CCSkeleton.m @@ -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 diff --git a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h index bf9bd62c0..099383d86 100644 --- a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h +++ b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.h @@ -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 } diff --git a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m index a614f6287..60bdf39e7 100644 --- a/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m +++ b/spine-cocos2d-iphone/src/spine/spine-cocos2d-iphone.m @@ -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; diff --git a/spine-cocos2dx/src/spine/CCSkeleton.cpp b/spine-cocos2dx/src/spine/CCSkeleton.cpp index 7e1d3f66d..b82726fa0 100644 --- a/spine-cocos2dx/src/spine/CCSkeleton.cpp +++ b/spine-cocos2dx/src/spine/CCSkeleton.cpp @@ -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; +} + } diff --git a/spine-cocos2dx/src/spine/CCSkeleton.h b/spine-cocos2dx/src/spine/CCSkeleton.h index 7b316f825..ee8b6b88c 100644 --- a/spine-cocos2dx/src/spine/CCSkeleton.h +++ b/spine-cocos2dx/src/spine/CCSkeleton.h @@ -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 (); diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp index b6638294c..7337eec5c 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.cpp +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.cpp @@ -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; diff --git a/spine-cocos2dx/src/spine/spine-cocos2dx.h b/spine-cocos2dx/src/spine/spine-cocos2dx.h index 44e5f3367..f102877db 100644 --- a/spine-cocos2dx/src/spine/spine-cocos2dx.h +++ b/spine-cocos2dx/src/spine/spine-cocos2dx.h @@ -31,6 +31,6 @@ #include #include -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_ */