Homepage Forums RetroPie Project Video Output on RetroPie help with OpelGL / barrel distortion Reply To: help with OpelGL / barrel distortion

#106535
davej
Participant

I got bored and wrote a barrel distortion only shader.

It runs speedily on my original 256Mb model B, which matches a model A+ hardware wise, even on a 1080 screen.

It doesn’t do any sort of anti-aliasing so the results don’t look great by default – it has the image problems I described earlier. Because you are going to be using such a small screen however you can get a substantial image improvement by specifying that the input texture should have linear filtering rather than nearest. These two images show examples of the same screen with nearest and linear filtering.

[attachment file=”106536″]

[attachment file=”106537″]

I’ve tried adding it as an attachment but it doesn’t let me and code tags don’t seem to like it either so here it is – until the #endif:

#if defined(VERTEX)
uniform mediump mat4 MVPMatrix;
attribute mediump vec4 VertexCoord;
attribute mediump vec2 TexCoord;

varying mediump vec2 TEX0;

void main()
{
TEX0 = TexCoord;
gl_Position = MVPMatrix * VertexCoord;
}
#elif defined(FRAGMENT)
uniform sampler2D Texture;
uniform vec2 InputSize;
uniform vec2 TextureSize;
varying vec2 TEX0;

const mediump float BARREL_DISTORTION = 0.25;
const mediump float rescale = 1.0 – (0.25 * BARREL_DISTORTION);

void main()
{
vec2 scale = TextureSize / InputSize;
vec2 tex0 = TEX0 * scale;
vec2 texcoord = tex0 – vec2(0.5);
float rsq = texcoord.x * texcoord.x + texcoord.y * texcoord.y;
texcoord = texcoord + (texcoord * (BARREL_DISTORTION * rsq));
texcoord *= rescale;

if (abs(texcoord.x) > 0.5 || abs(texcoord.y) > 0.5)
gl_FragColor = vec4(0.0);
else
{
texcoord += vec2(0.5);
texcoord /= scale;
vec3 colour = texture2D(Texture, texcoord).rgb;

gl_FragColor = vec4(colour,1.0);
}
}
#endif