Add barrel distortion shader

This commit is contained in:
jgk
2021-07-09 14:36:49 +02:00
parent 5301b6d0bd
commit fd388e1fdf
12 changed files with 192 additions and 8 deletions
+18
View File
@@ -0,0 +1,18 @@
#version 430 core
in vec2 cenPos;
in vec2 texPos;
in vec2 texDist;
in float factor;
out vec4 fColor;
layout (binding = 1) uniform sampler2D screenTexture;
float f ( float x) {
return max (1, 1 / (2 * x + 0.5)) ;
// return min (1, x) ;
}
void main()
{
vec4 tColor = vec4(texture(screenTexture , texPos) );
float t = distance(cenPos,texDist);
fColor = vec4(tColor.xy,t,tColor.w);
//, cenPos + f(distance(cenPos,texDist)) * (texPos - cenPos))) ;
}
+48
View File
@@ -0,0 +1,48 @@
#version 430 core
layout (points) in;
layout (triangle_strip, max_vertices = 4) out;
in vec2 vRad1[];
in vec2 vRad2[];
in float vFactor[];
out vec2 cenPos;
out vec2 texPos;
out vec2 texDist;
out float factor;
//const float root = 1 / sqrt(2);
const float root = 1;
vec2 cpos = gl_in[0].gl_Position.xy;
vec2 rad1 = vRad1[0];
vec2 rad2 = vRad2[0];
vec2 g(float x,float y)
{ return vec2
//(x,y);
(dot(rad1-cpos,vec2(x,y)-cpos)
,dot(rad2-cpos,vec2(x,y)-cpos)
);
}
void main() {
float factor = vFactor[0];
cenPos = cpos;
texPos = vec2(1,1);
//texDist = g(root,root);
texDist = vec2(root,root);
gl_Position = vec4 (1,1,0,1);
//gl_Position = vec4 (cenPos,0,1);
EmitVertex();
texPos = vec2(1,0);
//texDist = g(root,-root);
texDist = vec2(root,-root);
gl_Position = vec4 (1,-1,0,1);
EmitVertex();
texPos = vec2(0,1);
//texDist = g(-root,root);
texDist = vec2(-root,root);
gl_Position = vec4 (-1,1,0,1);
EmitVertex();
texPos = vec2(0,0);
//texDist = g(-root,-root);
texDist = vec2(-root,-root);
gl_Position = vec4 (-1,-1,0,1);
EmitVertex();
EndPrimitive();
}
+17
View File
@@ -0,0 +1,17 @@
#version 430 core
layout (location = 0) in vec2 pos;
layout (location = 1) in vec2 rad1;
layout (location = 2) in vec2 rad2;
layout (location = 3) in float factor;
layout (std140, binding = 0) uniform TheMat { mat4 theMat; } ;
out vec2 vRad1;
out vec2 vRad2;
out float vFactor;
void main()
{
gl_Position = theMat * vec4(pos,0,1);
//gl_Position = vec4(1,1,0,1);
vRad1 = (theMat * vec4(rad1,0,1)).xy;
vRad2 = (theMat * vec4(rad2,0,1)).xy;
vFactor = factor;
}
+57
View File
@@ -0,0 +1,57 @@
#version 430 core
in vec2 vTexPos;
out vec4 fColor;
uniform vec2 winSize;
uniform sampler2D screenTexture;
const float hOff = 2.0 / 600;
const float vOff = 2.0 / 600;
const float frac = 1.0 / 9;
const vec2 off[9] = vec2[]
( vec2( vOff,hOff )
, vec2( vOff,0.0 )
, vec2( vOff,-hOff )
, vec2( 0.0,hOff )
, vec2( 0.0,0.0 )
, vec2( 0.0,-hOff )
, vec2( -vOff,hOff )
, vec2( -vOff,0.0 )
, vec2( -vOff,-hOff )
);
const vec2 fiveOff[5] = vec2[]
( vec2( vOff,0.0 )
, vec2( 0.0,hOff )
, vec2( 0.0,0.0 )
, vec2( 0.0,-hOff )
, vec2( -vOff,0.0 )
);
vec4 combinef (vec4 ac, vec4 bc)
//{ return vec4 (max(ac.x,bc.x),max(ac.y,bc.y),max(ac.z,bc.z),ac.w + bc.w -
// ac.w*bc.w);
//{ return vec4 (max(ac.x,bc.x),max(ac.y,bc.y),max(ac.z,bc.z),ac.w + bc.w);
{ return vec4 (ac.rgb + bc.rgb * bc.a , ac.a + bc.a );
}
void main()
{
vec4 sampleTex[9];
for(int i=0; i<9; i++)
{
sampleTex[i] = vec4(texture(screenTexture, vTexPos + off[i]));
}
vec4 col = vec4(0,0,0,0);
for (int i = 0; i < 9; i++)
col = combinef(col,sampleTex[i]);
//col += sampleTex[i] * frac;
float alph = 0.0;
for (int i = 0; i < 9; i++)
alph += sampleTex[i].a;
alph = min(sampleTex[4].a, alph/9.0);
//
//fColor = col;
fColor = vec4(col.rgb/col.a,alph);
//if (col.a == 0.0)
//{ fColor = vec4(0,0,0,0); }
//else
//{ fColor = vec4( col.r/col.a , col.g/col.a , col.b/col.a, col.a/9.0); }
}
+9
View File
@@ -0,0 +1,9 @@
#version 430 core
layout (location = 0) in vec2 pos;
layout (location = 1) in vec2 texPos;
out vec2 vTexPos;
void main()
{
gl_Position = vec4(pos,0,1);
vTexPos = texPos;
}