--- /dev/null
+#pragma once\r
+\r
+static std::string get_math_glsl()\r
+{\r
+ static std::string glsl = \r
+ "/* "\r
+ "** Photoshop & misc math "\r
+ "** Blending modes, RGB/HSL/Contrast/Desaturate, levels control "\r
+ "** "\r
+ "** Romain Dura | Romz "\r
+ "** Blog: http://blog.mouaif.org "\r
+ "** Post: http://blog.mouaif.org/?p=94 "\r
+ "*/ "\r
+ " "\r
+ " "\r
+ "/* "\r
+ "** Desaturation "\r
+ "*/ "\r
+ " "\r
+ "vec4 Desaturate(vec3 color, float Desaturation) "\r
+ "{ "\r
+ " vec3 grayXfer = vec3(0.3, 0.59, 0.11); "\r
+ " vec3 gray = vec3(dot(grayXfer, color)); "\r
+ " return vec4(mix(color, gray, Desaturation), 1.0); "\r
+ "} "\r
+ " "\r
+ " "\r
+ "/* "\r
+ "** Hue, saturation, luminance "\r
+ "*/ "\r
+ " "\r
+ "vec3 RGBToHSL(vec3 color) "\r
+ "{ "\r
+ " vec3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part) "\r
+ " "\r
+ " float fmin = min(min(color.r, color.g), color.b); //Min. value of RGB "\r
+ " float fmax = max(max(color.r, color.g), color.b); //Max. value of RGB "\r
+ " float delta = fmax - fmin; //Delta RGB value "\r
+ " "\r
+ " hsl.z = (fmax + fmin) / 2.0; // Luminance "\r
+ " "\r
+ " if (delta == 0.0) //This is a gray, no chroma... "\r
+ " { "\r
+ " hsl.x = 0.0; // Hue "\r
+ " hsl.y = 0.0; // Saturation "\r
+ " } "\r
+ " else //Chromatic data... "\r
+ " { "\r
+ " if (hsl.z < 0.5) "\r
+ " hsl.y = delta / (fmax + fmin); // Saturation "\r
+ " else "\r
+ " hsl.y = delta / (2.0 - fmax - fmin); // Saturation "\r
+ " "\r
+ " float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta; "\r
+ " float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta; "\r
+ " float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta; "\r
+ " "\r
+ " if (color.r == fmax ) "\r
+ " hsl.x = deltaB - deltaG; // Hue "\r
+ " else if (color.g == fmax) "\r
+ " hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue "\r
+ " else if (color.b == fmax) "\r
+ " hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue "\r
+ " "\r
+ " if (hsl.x < 0.0) "\r
+ " hsl.x += 1.0; // Hue "\r
+ " else if (hsl.x > 1.0) "\r
+ " hsl.x -= 1.0; // Hue "\r
+ " } "\r
+ " "\r
+ " return hsl; "\r
+ "} "\r
+ " "\r
+ "float HueToRGB(float f1, float f2, float hue) "\r
+ "{ "\r
+ " if (hue < 0.0) "\r
+ " hue += 1.0; "\r
+ " else if (hue > 1.0) "\r
+ " hue -= 1.0; "\r
+ " float res; "\r
+ " if ((6.0 * hue) < 1.0) "\r
+ " res = f1 + (f2 - f1) * 6.0 * hue; "\r
+ " else if ((2.0 * hue) < 1.0) "\r
+ " res = f2; "\r
+ " else if ((3.0 * hue) < 2.0) "\r
+ " res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; "\r
+ " else "\r
+ " res = f1; "\r
+ " return res; "\r
+ "} "\r
+ " "\r
+ "vec3 HSLToRGB(vec3 hsl) "\r
+ "{ "\r
+ " vec3 rgb; "\r
+ " "\r
+ " if (hsl.y == 0.0) "\r
+ " rgb = vec3(hsl.z); // Luminance "\r
+ " else "\r
+ " { "\r
+ " float f2; "\r
+ " "\r
+ " if (hsl.z < 0.5) "\r
+ " f2 = hsl.z * (1.0 + hsl.y); "\r
+ " else "\r
+ " f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); "\r
+ " "\r
+ " float f1 = 2.0 * hsl.z - f2; "\r
+ " "\r
+ " rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); "\r
+ " rgb.g = HueToRGB(f1, f2, hsl.x); "\r
+ " rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0)); "\r
+ " } "\r
+ " "\r
+ " return rgb; "\r
+ "} "\r
+ " "\r
+ " "\r
+ "/* "\r
+ "** Contrast, saturation, brightness "\r
+ "** Code of this function is from TGM's shader pack "\r
+ "** http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057 "\r
+ "*/ "\r
+ " "\r
+ "// For all settings: 1.0 = 100% 0.5=50% 1.5 = 150% "\r
+ "vec3 ContrastSaturationBrightness(vec3 color, float brt, float sat, float con) "\r
+ "{ "\r
+ " // Increase or decrease theese values to adjust r, g and b color channels seperately "\r
+ " const float AvgLumR = 0.5; "\r
+ " const float AvgLumG = 0.5; "\r
+ " const float AvgLumB = 0.5; "\r
+ " "\r
+ " const vec3 LumCoeff = vec3(0.2125, 0.7154, 0.0721); "\r
+ " "\r
+ " vec3 AvgLumin = vec3(AvgLumR, AvgLumG, AvgLumB); "\r
+ " vec3 brtColor = color * brt; "\r
+ " vec3 intensity = vec3(dot(brtColor, LumCoeff)); "\r
+ " vec3 satColor = mix(intensity, brtColor, sat); "\r
+ " vec3 conColor = mix(AvgLumin, satColor, con); "\r
+ " return conColor; "\r
+ "} "\r
+ " "\r
+ " "\r
+ "/* "\r
+ "** Float blending modes "\r
+ "** Adapted from here: http://www.nathanm.com/photoshop-blending-math/ "\r
+ "** But I modified the HardMix (wrong condition), Overlay, SoftLight, ColorDodge, ColorBurn, VividLight, PinLight (inverted layers) ones to have correct results "\r
+ "*/ "\r
+ " "\r
+ "#define BlendLinearDodgef BlendAddf "\r
+ "#define BlendLinearBurnf BlendSubstractf "\r
+ "#define BlendAddf(base, blend) min(base + blend, 1.0) "\r
+ "#define BlendSubstractf(base, blend) max(base + blend - 1.0, 0.0) "\r
+ "#define BlendLightenf(base, blend) max(blend, base) "\r
+ "#define BlendDarkenf(base, blend) min(blend, base) "\r
+ "#define BlendLinearLightf(base, blend) (blend < 0.5 ? BlendLinearBurnf(base, (2.0 * blend)) : BlendLinearDodgef(base, (2.0 * (blend - 0.5)))) "\r
+ "#define BlendScreenf(base, blend) (1.0 - ((1.0 - base) * (1.0 - blend))) "\r
+ "#define BlendOverlayf(base, blend) (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend))) "\r
+ "#define BlendSoftLightf(base, blend) ((blend < 0.5) ? (2.0 * base * blend + base * base * (1.0 - 2.0 * blend)) : (sqrt(base) * (2.0 * blend - 1.0) + 2.0 * base * (1.0 - blend))) "\r
+ "#define BlendColorDodgef(base, blend) ((blend == 1.0) ? blend : min(base / (1.0 - blend), 1.0)) "\r
+ "#define BlendColorBurnf(base, blend) ((blend == 0.0) ? blend : max((1.0 - ((1.0 - base) / blend)), 0.0)) "\r
+ "#define BlendVividLightf(base, blend) ((blend < 0.5) ? BlendColorBurnf(base, (2.0 * blend)) : BlendColorDodgef(base, (2.0 * (blend - 0.5)))) "\r
+ "#define BlendPinLightf(base, blend) ((blend < 0.5) ? BlendDarkenf(base, (2.0 * blend)) : BlendLightenf(base, (2.0 *(blend - 0.5)))) "\r
+ "#define BlendHardMixf(base, blend) ((BlendVividLightf(base, blend) < 0.5) ? 0.0 : 1.0) "\r
+ "#define BlendReflectf(base, blend) ((blend == 1.0) ? blend : min(base * base / (1.0 - blend), 1.0)) "\r
+ " "\r
+ " "\r
+ "/* "\r
+ "** Vector3 blending modes "\r
+ "*/ "\r
+ " "\r
+ "// Component wise blending "\r
+ "#define Blend(base, blend, funcf) vec3(funcf(base.r, blend.r), funcf(base.g, blend.g), funcf(base.b, blend.b)) "\r
+ " "\r
+ "#define BlendNormal(base, blend) (blend) "\r
+ "#define BlendLighten BlendLightenf "\r
+ "#define BlendDarken BlendDarkenf "\r
+ "#define BlendMultiply(base, blend) (base * blend) "\r
+ "#define BlendAverage(base, blend) ((base + blend) / 2.0) "\r
+ "#define BlendAdd(base, blend) min(base + blend, vec3(1.0)) "\r
+ "#define BlendSubstract(base, blend) max(base + blend - vec3(1.0), vec3(0.0)) "\r
+ "#define BlendDifference(base, blend) abs(base - blend) "\r
+ "#define BlendNegation(base, blend) (vec3(1.0) - abs(vec3(1.0) - base - blend)) "\r
+ "#define BlendExclusion(base, blend) (base + blend - 2.0 * base * blend) "\r
+ "#define BlendScreen(base, blend) Blend(base, blend, BlendScreenf) "\r
+ "#define BlendOverlay(base, blend) Blend(base, blend, BlendOverlayf) "\r
+ "#define BlendSoftLight(base, blend) Blend(base, blend, BlendSoftLightf) "\r
+ "#define BlendHardLight(base, blend) BlendOverlay(blend, base) "\r
+ "#define BlendColorDodge(base, blend) Blend(base, blend, BlendColorDodgef) "\r
+ "#define BlendColorBurn(base, blend) Blend(base, blend, BlendColorBurnf) "\r
+ "#define BlendLinearDodge BlendAdd "\r
+ "#define BlendLinearBurn BlendSubstract "\r
+ "// Linear Light is another contrast-increasing mode "\r
+ "// If the blend color is darker than midgray, Linear Light darkens the image by decreasing the brightness. If the blend color is lighter than midgray, the result is a brighter image due to increased brightness."\r
+ "#define BlendLinearLight(base, blend) Blend(base, blend, BlendLinearLightf) "\r
+ "#define BlendVividLight(base, blend) Blend(base, blend, BlendVividLightf) "\r
+ "#define BlendPinLight(base, blend) Blend(base, blend, BlendPinLightf) "\r
+ "#define BlendHardMix(base, blend) Blend(base, blend, BlendHardMixf) "\r
+ "#define BlendReflect(base, blend) Blend(base, blend, BlendReflectf) "\r
+ "#define BlendGlow(base, blend) BlendReflect(blend, base) "\r
+ "#define BlendPhoenix(base, blend) (min(base, blend) - max(base, blend) + vec3(1.0)) "\r
+ "#define BlendOpacity(base, blend, F, O) (F(base, blend) * O + blend * (1.0 - O)) "\r
+ " "\r
+ " "\r
+ "// Hue Blend mode creates the result color by combining the luminance and saturation of the base color with the hue of the blend color. "\r
+ "vec3 BlendHue(vec3 base, vec3 blend) "\r
+ "{ "\r
+ " vec3 baseHSL = RGBToHSL(base); "\r
+ " return HSLToRGB(vec3(RGBToHSL(blend).r, baseHSL.g, baseHSL.b)); "\r
+ "} "\r
+ " "\r
+ "// Saturation Blend mode creates the result color by combining the luminance and hue of the base color with the saturation of the blend color. "\r
+ "vec3 BlendSaturation(vec3 base, vec3 blend) "\r
+ "{ "\r
+ " vec3 baseHSL = RGBToHSL(base); "\r
+ " return HSLToRGB(vec3(baseHSL.r, RGBToHSL(blend).g, baseHSL.b)); "\r
+ "} "\r
+ " "\r
+ "// Color Mode keeps the brightness of the base color and applies both the hue and saturation of the blend color. "\r
+ "vec3 BlendColor(vec3 base, vec3 blend) "\r
+ "{ "\r
+ " vec3 blendHSL = RGBToHSL(blend); "\r
+ " return HSLToRGB(vec3(blendHSL.r, blendHSL.g, RGBToHSL(base).b)); "\r
+ "} "\r
+ " "\r
+ "// Luminosity Blend mode creates the result color by combining the hue and saturation of the base color with the luminance of the blend color. "\r
+ "vec3 BlendLuminosity(vec3 base, vec3 blend) "\r
+ "{ "\r
+ " vec3 baseHSL = RGBToHSL(base); "\r
+ " return HSLToRGB(vec3(baseHSL.r, baseHSL.g, RGBToHSL(blend).b)); "\r
+ "} "\r
+ " "\r
+ " "\r
+ "/* "\r
+ "** Gamma correction "\r
+ "** Details: http://blog.mouaif.org/2009/01/22/photoshop-gamma-correction-shader/ "\r
+ "*/ "\r
+ " "\r
+ "#define GammaCorrection(color, gamma) pow(color, 1.0 / gamma) "\r
+ " "\r
+ "/* "\r
+ "** Levels control (input (+gamma), output) "\r
+ "** Details: http://blog.mouaif.org/2009/01/28/levels-control-shader/ "\r
+ "*/ "\r
+ " "\r
+ "#define LevelsControlInputRange(color, minInput, maxInput) min(max(color - vec3(minInput), vec3(0.0)) / (vec3(maxInput) - vec3(minInput)), vec3(1.0)) "\r
+ "#define LevelsControlInput(color, minInput, gamma, maxInput) GammaCorrection(LevelsControlInputRange(color, minInput, maxInput), gamma) "\r
+ "#define LevelsControlOutputRange(color, minOutput, maxOutput) mix(vec3(minOutput), vec3(maxOutput), color) "\r
+ "#define LevelsControl(color, minInput, gamma, maxInput, minOutput, maxOutput) LevelsControlOutputRange(LevelsControlInput(color, minInput, gamma, maxInput), minOutput, maxOutput) ";\r
+ \r
+ return glsl;\r
+}
\ No newline at end of file