From 1b0eb2797def67c67001a00269ec79ce54716504 Mon Sep 17 00:00:00 2001
From: Jonas Kulla <Nyocurio@gmail.com>
Date: Fri, 31 Jan 2014 10:08:05 +0100
Subject: [PATCH] BltShader: Correct algorithm as provided my /cremno

The color calculation is now actually 99% correct!
Fixes #14.
---
 shader/bitmapBlit.frag | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/shader/bitmapBlit.frag b/shader/bitmapBlit.frag
index 1913c5b..18be173 100644
--- a/shader/bitmapBlit.frag
+++ b/shader/bitmapBlit.frag
@@ -20,18 +20,14 @@ void main()
 
 	vec4 resFrag;
 
-	float ab = opacity;
-	float as = srcFrag.a;
-	float ad = dstFrag.a;
+	float co1 = srcFrag.a * opacity;
+	float co2 = dstFrag.a * (1.0 - co1);
+	resFrag.a = co1 + co2;
 
-	float at = ab*as;
-	resFrag.a = at + ad - ad*at;
-
-	// Sigh...
-	if (ad == 0.0)
+	if (resFrag.a == 0.0)
 		resFrag.rgb = srcFrag.rgb;
 	else
-		resFrag.rgb = as*srcFrag.rgb + (1.0-at) * ad * dstFrag.rgb;
+		resFrag.rgb = (co1*srcFrag.rgb + co2*dstFrag.rgb) / resFrag.a;
 
 	gl_FragColor = resFrag;
 }