1
\$\begingroup\$

In OpenGL, a fragment shader goes through each pixel, right? So is it possible (within the shader itself) to get what pixel it is processing and color each specific pixel?

\$\endgroup\$

3 Answers 3

3
\$\begingroup\$

There is, in the fragment shader, a variable called gl_FragCoord. This contains the coordinates of the currently executing fragment shader.

Note that (by default) a pixel at pixel coordinates (x,y) will have gl_FragCoordinates of (x+0.5,y+0.5)

\$\endgroup\$
1
\$\begingroup\$

Not too sure what you mean, but you can get the current pixel coordinate in the fragment shader by using gl_FragCoord! This is considerably easier than interpolation, as mentioned in the other answer.

Here are more details: https://www.opengl.org/sdk/docs/man/html/gl_FragCoord.xhtml

\$\endgroup\$
2
  • \$\begingroup\$ interpolation is done automatically for shader so it is basically just about reading the varying set by vertex attribute. But of course my idea of using uv coordinates was based on misunderstood question where I tough we were talking about multiple objects that don't fill whole screen. \$\endgroup\$ Commented Nov 19, 2015 at 13:27
  • \$\begingroup\$ I thought you meant something like drawing a full screen quad and interpolating that :) Can be useful, but not for many setups! \$\endgroup\$ Commented Nov 19, 2015 at 20:26
0
\$\begingroup\$

you can attach u and v coordinates for each vertex and then fragment shader interpolates those u and v coordinates for each pixel. UV coordinates are used to calculate where to sample from textures but they can be also used directly for math.

\$\endgroup\$

You must log in to answer this question.