GLSL Fragment Position

末鹿安然 提交于 2020-01-01 10:14:14

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12111325/glsl-fragment-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!