1 /* 2 Copyright (c) 2017-2018 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.graphics.filters.blur; 29 30 import derelict.opengl; 31 import dlib.math.vector; 32 import dagon.core.ownership; 33 import dagon.graphics.postproc; 34 import dagon.graphics.framebuffer; 35 import dagon.graphics.rc; 36 37 /* 38 * Gaussian blur implementation is based on code by Matt DesLauriers: 39 * https://github.com/Jam3/glsl-fast-gaussian-blur 40 */ 41 42 class PostFilterBlur: PostFilter 43 { 44 private string vs = " 45 #version 330 core 46 47 uniform mat4 modelViewMatrix; 48 uniform mat4 projectionMatrix; 49 50 uniform vec2 viewSize; 51 52 layout (location = 0) in vec2 va_Vertex; 53 layout (location = 1) in vec2 va_Texcoord; 54 55 out vec2 texCoord; 56 57 void main() 58 { 59 texCoord = va_Texcoord; 60 gl_Position = projectionMatrix * modelViewMatrix * vec4(va_Vertex * viewSize, 0.0, 1.0); 61 } 62 "; 63 64 private string fs = " 65 #version 330 core 66 67 uniform bool enabled; 68 69 uniform sampler2D fbColor; 70 uniform vec2 viewSize; 71 uniform vec2 direction; 72 73 in vec2 texCoord; 74 out vec4 frag_color; 75 76 vec4 blur9(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) 77 { 78 vec4 color = vec4(0.0); 79 vec2 off1 = vec2(1.3846153846) * direction; 80 vec2 off2 = vec2(3.2307692308) * direction; 81 color += texture2D(image, uv) * 0.2270270270; 82 color += texture2D(image, uv + (off1 / resolution)) * 0.3162162162; 83 color += texture2D(image, uv - (off1 / resolution)) * 0.3162162162; 84 color += texture2D(image, uv + (off2 / resolution)) * 0.0702702703; 85 color += texture2D(image, uv - (off2 / resolution)) * 0.0702702703; 86 return color; 87 } 88 89 const float weight[5] = float[](0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216); 90 91 void main() 92 { 93 vec2 fragCoord = gl_FragCoord.xy; 94 vec2 invScreenSize = 1.0 / viewSize; 95 96 vec3 color; 97 if (enabled) 98 { 99 color = blur9(fbColor, texCoord, viewSize, direction).rgb; 100 } 101 else 102 { 103 color = vec3(0.0, 0.0, 0.0); 104 } 105 106 color = clamp(color, vec3(0.0), vec3(1.0)); 107 108 frag_color = vec4(color, 1.0); 109 } 110 "; 111 112 override string vertexShader() 113 { 114 return vs; 115 } 116 117 override string fragmentShader() 118 { 119 return fs; 120 } 121 122 GLint directionLoc; 123 124 Vector2f direction; 125 float radius = 1.0f; 126 127 this(bool horizontal, Framebuffer inputBuffer, Framebuffer outputBuffer, Owner o) 128 { 129 super(inputBuffer, outputBuffer, o); 130 131 directionLoc = glGetUniformLocation(shaderProgram, "direction"); 132 133 if (horizontal) 134 direction = Vector2f(1.0f, 0.0f); 135 else 136 direction = Vector2f(0.0f, 1.0f); 137 } 138 139 override void bind(RenderingContext* rc) 140 { 141 super.bind(rc); 142 143 Vector2f dirScaled = direction * radius; 144 glUniform2fv(directionLoc, 1, dirScaled.arrayof.ptr); 145 } 146 }