Shader/Filter: Thinks Origin is Bottom Left

I’m playing around with Filters and every time I use shaders in a new enviroment I run through a series of simple shaders to get a feel for how the engine works with them.
While trying to do a simple vignette shader I ran into a slight problem, the shader seems to think that the origin is at the bottom left. When I call length with (texCoord/resolution) I get the black fading oval, but it starts at the bottom left.

Funny thing though, when I use distance(vec2(0.5, 0.5), texCoord) * 1.0; it works fine.

Here’s the code for the shader:

uniform sampler2D m_Texture;
uniform vec2 resolution;
varying vec2 texCoord;

void main() {

    vec3 color = texture2D(m_Texture, texCoord).rgb;

    //not working code
    float len = length(texCoord / resolution) * vec2(0.5);

    //working code
    vec2 o = vec2(0.5, 0.5);
    float disVar= distance(vec2(0.5, 0.5), texCoord) * 1.0;
    vec3 col = texture2D(m_Texture, texCoord).rgb * (1.0 - disVar * disVar);

    //gl_FragColor = vec4(col.rgb, 1.0);
    gl_FragColor = vec4(vec3(len), 1.0);

I’d like to know why this isn’t working because I need the first implementation to do some other stuff.
I’m rather new to GLSL if you haven’t noticed.

Ah excuse me I posted wrong code.
Replace line 10 with

    float len = length(texCoord) * vec2(0.5);

and you’ll have fade in the bottom left as I was saying. the original gives me only white. Which is even stranger…

The bottom left is 0,0 in OpenGL by default… so it’s probably no surprise that it’s the origin.

I suppose the library I was using before sets the origin in the middle by default.
How could I shift the effect to the middle?

@Slyth said: I suppose the library I was using before sets the origin in the middle by default. How could I shift the effect to the middle?

The middle is 0.5, 0.5

Projection space also called clip space is from -1 to 1 and the origin 0,0 in in the middle of the screen.

Texture space however goes from 0 to 1 and the origin is the bottom left of the texture. It has an additional signification when using filter because the texture of the scene is applied to a full screen quad. so basically yo have a texture on screen so bottom left is your 0,0.

To go from one to the other you have to go value * 0.5 + 0.5 (proj → screen) or value * 2.0 -1.0 for the other way around.

Not sure what you’re trying to achieve, but if you want a fade value you probably want something that varies from 0 to 1 but you want the effect centered in the screen.
So line 10 should be something like this

float len = abs(texCoord * vec2(2.0) - vec2(1.0));
1 Like

Ah that got it to work, thanks alot nehon :slight_smile: