Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL Fragment Position

Tags:

glsl

Sorry I will rewrite the question hoping to be clear.

In my .cpp code I create a list of quads, a few of them have a flag, in the pixel shader I check if this flag is set or not, if the flag is not set, the quad gets colored in red for example, if the flag is set, I want to decide the color of every single pixel, so if I need to colour half of the flagged quad in red and the other half in blue I can simply do something like :

if coordinate in quad < something color = red
else colour = blue; 

In this way I can get half of the quad colored in blue and another half colored in red, or I can decide where to put the red color or where to put the blue one.

Imagine I've got a quad 50x50 pixels

[frag]

if(quad.flag == 1)
  {    
    if(Pixel_coordinate.x<25 ) gl_fragColor = vec4(1.0, 0.0, 0.0, 1.0);
    else gl_fragColor = vec4(0.0, 1.0, 0.0, 1.0);
  }
else
  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);

In this case I would expect that a quad with the flag set will get two colors per face. I hope I have been more specific now.

thanks.

Just to add something I can't use any texture.

Ok i do this now :

Every quad has 4 textures coordinated (0,0), (0,1), (1,1), (1,0);

I enable the texture coordinates using :

glTexCoordPointer(2, GL_SHORT, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 7));

[vert]

varying vec2 texCoord;

main()
{
    texCoord = gl_MultiTexCoord0.xy;
} 

[frag]

varying vec2 texCoord;

main()
{
    float x1 = texCoord.s;
    float x2 = texCoord.t;

    gl_FragColor = vec4(x1, x2, 0.0, 1.0);
}

I get always the yellow color so x1 =1 and x2 = 1 almost always and some quad is yellow/green.

I would expect that the texture coordinates change in the fragment shader and so I should get a gradient, am I wrong?

like image 330
user1583007 Avatar asked Aug 24 '12 14:08

user1583007


People also ask

Can you tell where the coordinate 0.0 0.0 is in our canvas?

Can you tell where the coordinate (0.0, 0.0)is in our canvas? - the left side below. It is the end of the basic structure of the shader. Now, let's write the shader code in earnest.

What is a fragment in GLSL?

A Fragment Shader is the Shader stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value. The fragment shader is the OpenGL pipeline stage after a primitive is rasterized. For each sample of the pixels covered by a primitive, a "fragment" is generated.

What is GL position?

gl_Position is the predefined variable which is available only in the vertex shader program. It contains the vertex position. In the above code, the coordinates attribute is passed in the form of a vector. As vertex shader is a per-vertex operation, the gl_position value is calculated for each vertex.

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.


2 Answers

If you want to know the coordinate within the quad, you need to calculate it yourself. In order to that, you'll need to create a new interpolant (call it something like vec2 quadCoord), and set it appropriately for each vertex, which means you'll likely also need to add it as an attribute and pass it through your vertex shader. eg:

// in the vertex shader
attribute vec2 quadCoordIn;
varying vec2 quadCoord;

main() {
    quadCoord = quadCoordIn;
              :

You'll need to feed in this attribute in your drawing code when drawing your quads. For each quad, the vertexes will have likely have quadCoordIn values of (0,0), (0,1), (1,1) and (1,0) -- you could use some other coordinate system if you prefer, but this is the easiest.

Then, in your fragment program, you can access quadCoord.xy to determine where in the quad you are.

like image 183
Chris Dodd Avatar answered Jan 03 '23 11:01

Chris Dodd


In addition to Chris Dodd's answer, you can also access the screen-space coordinate (in pixels, though actually pixel centers and thus ?.5) of the currently processed fragment through the special fragment shader variable gl_FragCoord:

gl_FragColor = (gl_FragCoord.x<25.0) ? vec4(1.0, 0.0, 0.0, 1.0) : vec4(0.0, 1.0, 0.0, 1.0);

But this gives you the position of the fragment in screen-space and thus relative to the lower left corner of you viewport. If you actually need to know the position inside the individual quad (which makes more sense if you want to actually color each quad half-by-half, since the "half-cut" would otherwise vary with the quad's position), then Chris Dodd's answer is the correct approach.

like image 38
Christian Rau Avatar answered Jan 03 '23 12:01

Christian Rau