GLSL fragment position

Sorry, I rewrote the question, hoping to be clear.

In my .cpp code I create a list of quads, some of which have a flag, in the pixel shader I check whether this flag is set or not, if the flag is not set, the square turns red for example, if the flag is set, I want to determine the color of each pixels, so if I need to color half of the marked square in red and the second half in blue, I can just do something like:

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

That way, I can get half the square painted in blue and the other half in red, or I can decide where to put the red color or where to put the blue.

Imagine I have a 50x50 pixel square

[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 expect the square with the flag set to get two colors on the face. Hopefully now I have been more specific.

thanks.

Just to add something, I cannot use texture.

Ok, I'm doing it now:

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

I resolve 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 always get yellow, so x1 = 1 and x2 = 1 almost always, and some ATVs are yellow / green.

I would expect the texture coordinates to change in the fragment shader, and so I have to get the gradient, right?

+6
source share
2 answers

If you want to know the coordinate in a square, you need to calculate it yourself. To do this, you will need to create a new interpolator (call it something like vec2 quadCoord ) and configure it for each vertex accordingly, which means that you 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; : 

When drawing your squares, you will need to pass this attribute to your drawing code. For each square, the vertices will have the values quadCoordIn (0,0), (0,1), (1,1) and (1,0) - you can use some other coordinate system if you want, but this is the easiest.

Then, in your snippet program, you can access quadCoord.xy to determine where you are squared.

+4
source

In addition to Chris Dodd’s answer, you can also access the coordinate of the screen space (in pixels, although actually by the centers of the pixels and, therefore ?.5 ) of the fragment currently processed through a special 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 the screen space and, therefore, relative to the lower left corner of the viewport. If you really need to know the position inside a separate ATV (which makes sense, if you want to actually color each square one and a half and a half, since the “half cut” will otherwise change depending on the quadrant), then Chris Dodd The answer is correct an approach.

+2
source

Source: https://habr.com/ru/post/923675/


All Articles