Ivan Safrin 13 лет назад
Родитель
Сommit
95ef3cc459

+ 2 - 0
Core/Contents/Include/PolyImage.h

@@ -237,6 +237,8 @@ namespace Polycode {
 			* @return Pointer to raw image data.
 			*/						
 			char *getPixels();
+			
+			void premultiplyAlpha();
 		
 			static const int IMAGE_RGB = 0;
 			static const int IMAGE_RGBA = 1;

+ 2 - 0
Core/Contents/Include/PolyMaterialManager.h

@@ -77,6 +77,8 @@ namespace Polycode {
 			Shader *setShaderFromXMLNode(TiXmlNode *node);
 			Shader *createShaderFromXMLNode(TiXmlNode *node);
 		
+			bool premultiplyAlphaOnLoad;
+		
 		private:
 			std::vector<Texture*> textures;
 			std::vector<Material*> materials;

+ 2 - 1
Core/Contents/Include/PolyRenderer.h

@@ -223,7 +223,8 @@ namespace Polycode {
 		static const int BLEND_MODE_NORMAL = 0;
 		static const int BLEND_MODE_LIGHTEN = 1;
 		static const int BLEND_MODE_COLOR = 2;
-		
+		static const int BLEND_MODE_PREMULTIPLIED = 3;
+				
 		static const int FOG_LINEAR = 0;
 		static const int FOG_EXP = 1;
 		static const int FOG_EXP2 = 2;

+ 3 - 0
Core/Contents/Source/PolyGLRenderer.cpp

@@ -399,6 +399,9 @@ void OpenGLRenderer::setBlendingMode(int blendingMode) {
 		case BLEND_MODE_COLOR:
 				glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
 		break;
+		case BLEND_MODE_PREMULTIPLIED:
+			glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+		break;
 		default:
 			glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 		break;

+ 12 - 1
Core/Contents/Source/PolyImage.cpp

@@ -644,7 +644,18 @@ bool Image::saveImage(const String &fileName) {
 	return savePNG(fileName);
 }
 
-
+void Image::premultiplyAlpha() {
+	for(int x=0; x < width; x++) {
+		for(int y=0; y < height; y++) {
+			unsigned int *imageData32 = (unsigned int*)imageData;	
+			Color col =  Color(imageData32[x+(y*width)]);
+			col.r *= col.a;
+			col.g *= col.a;			
+			col.b *= col.a;						
+			imageData32[x+(y*width)] = col.getUint();
+		}
+	}
+}
 
 bool Image::savePNG(const String &fileName) {
 

+ 4 - 1
Core/Contents/Source/PolyMaterialManager.cpp

@@ -35,7 +35,7 @@ using namespace Polycode;
 using std::vector;
 
 MaterialManager::MaterialManager() {
-	
+	premultiplyAlphaOnLoad = false;
 }
 
 MaterialManager::~MaterialManager() {
@@ -93,6 +93,9 @@ Texture *MaterialManager::createTextureFromFile(const String& fileName, bool cla
 	
 	Image *image = new Image(fileName);
 	if(image->isLoaded()) {
+		if(premultiplyAlphaOnLoad) {
+			image->premultiplyAlpha();
+		}
 		newTexture = createTexture(image->getWidth(), image->getHeight(), image->getPixels(), clamp, createMipmaps);
 	} else {
 		Logger::log("Error loading image, using default texture.\n");