Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL: gl_FragCoord issues

I am experimenting with GLSL for OpenGL ES 2.0. I have a quad and a texture I am rendering. I can successfully do it this way:

//VERTEX SHADER
attribute highp vec4 vertex;
attribute mediump vec2 coord0;

uniform mediump mat4 worldViewProjection;

varying mediump vec2 tc0;

void main()
{
    // Transforming The Vertex
    gl_Position = worldViewProjection * vertex;

    // Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
    tc0 = vec2(coord0);
}

//FRAGMENT SHADER
varying mediump vec2 tc0;

uniform sampler2D my_color_texture;

void main()
{
    gl_FragColor = texture2D(my_color_texture, tc0);
}

So far so good. However, I'd like to do some pixel-based filtering, e.g. Median. So, I'd like to work in pixel coordinates rather than in normalized (tc0) and then convert the result back to normalized coords. Therefore, I'd like to use gl_FragCoord instead of a uv attribute (tc0). But I don't know how to go back to normalized coords because I don't know the range of gl_FragCoords. Any idea how I could get it? I have got that far, using a fixed value for 'normalization', though it's not working perfectly as it is causing stretching and tiling (but at least is showing something):

//FRAGMENT SHADER
varying mediump vec2 tc0;

uniform sampler2D my_color_texture;

void main()
{
    gl_FragColor = texture2D(my_color_texture, vec2(gl_FragCoord) / vec2(256, 256));
}

So, the simple question is, what should I use in the place of vec2(256, 256) so that I could get the same result as if I were using the uv coords.

Thanks!

like image 858
Albus Dumbledore Avatar asked Feb 28 '11 16:02

Albus Dumbledore


People also ask

Is gl_FragCoord normalized?

gl_FragCoord is in screen coordinates, so to get normalized coords you need to divide by the viewport width and height. You can use a uniform variable to pass that information to the shader, since there is no built in variable for it.

Is gl_FragColor deprecated?

Yes, gl_FragColor is deprecated. You should use the following syntax: layout(location = 0) out vec4 diffuseColor; It is included in the GLSL 4.60 spec under the section 7.1.

What is the range of gl_FragCoord?

The actual result is: gl_FragCoord. xy is in the range [0.5, 1023.5], not [0.0, 1023.0]. From the spec: "By default, gl_FragCoord assumes a lower-left origin for window coordinates and assumes pixel centers are located at half-pixel coordinates.

What is sampler2D?

A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.


1 Answers

gl_FragCoord is in screen coordinates, so to get normalized coords you need to divide by the viewport width and height. You can use a uniform variable to pass that information to the shader, since there is no built in variable for it.

like image 77
Dr. Snoopy Avatar answered Sep 25 '22 15:09

Dr. Snoopy