Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pixel information inside a fragment shader?

Tags:

opengl

glsl

In my fragment shader I can load a texture, then do this:

uniform sampler2D tex;  void main(void) {    vec4 color = texture2D(tex, gl_TexCoord[0].st);    gl_FragColor = color; } 

That sets the current pixel to color value of texture. I can modify these, etc and it works well.

But a few questions. How do I tell "which" pixel I am? For example, say I want to set pixel 100,100 (x,y) to red. Everything else to black. How do I do a :

"if currentSelf.Position() == (100,100); then color=red; else color=black?"

?

I know how to set colors, but how do I get "my" location?

Secondly, how do I get values from a neighbor pixel?

I tried this:

vec4 nextColor = texture2D(tex, gl_TexCoord[1].st); 

But not clear what it is returning? if I'm pixel 100,100; how do I get the values from 101,100 or 100,101?

like image 284
user697111 Avatar asked Mar 19 '12 23:03

user697111


People also ask

Is fragment shader a pixel shader?

A fragment shader is the same as pixel shader. One main difference is that a vertex shader can manipulate the attributes of vertices. which are the corner points of your polygons. The fragment shader on the other hand takes care of how the pixels between the vertices look.

What is the input & output data of the fragment shader?

The output of a fragment shader is a depth value, a possible stencil value (unmodified by the fragment shader), and zero or more color values to be potentially written to the buffers in the current framebuffers. Fragment shaders take a single fragment as input and produce a single fragment as output.

How does a vertex shader pass data to a fragment shader?

A vertex shader receives its input data from a vertex attribute. But how does a fragment shader gets its data? Data is passed from shader to shader by using the in and out keywords. You create an output shader variable by using the out keyword.

What is the purpose of a fragment shader?

Fragment shaders Fragment (or texture) shaders define RGBA (red, green, blue, alpha) colors for each pixel being processed — a single fragment shader is called once per pixel. The purpose of the fragment shader is to set up the gl_FragColor variable. gl_FragColor is a built-in GLSL variable like gl_Position .


2 Answers

How do I tell "which" pixel I am?

You're not a pixel. You're a fragment. There's a reason that OpenGL calls them "Fragment shaders"; it's because they aren't pixels yet. Indeed, not only may they never become pixels (via discard or depth tests or whatever), thanks to multisampling, multiple fragments can combine to form a single pixel.

If you want to tell where your fragment shader is in window-space, use gl_FragCoord. Fragment positions are floating-point values, not integers, so you have to test with a range instead of a single "100, 100" value.

Secondly, how do I get values from a neighbor pixel?

If you're talking about the neighboring framebuffer pixel, you don't. Fragment shaders cannot arbitrarily read from the framebuffer, either in their own position or in a neighboring one.

If you're talking about accessing a neighboring texel from the one you accessed, then that's just a matter of biasing the texture coordinate you pass to texture2D. You have to get the size of the texture (since you're not using GLSL 1.30 or above, you have to manually pass this in), invert the size and either add or subtract these sizes from the S and T component of the texture coordinate.

like image 119
Nicol Bolas Avatar answered Oct 24 '22 14:10

Nicol Bolas


Easy peasy.

Just compute the size of a pixel based on resolution. Then look up +1 and -1.

vec2 onePixel = vec2(1.0, 1.0) / u_textureSize; gl_FragColor = (    texture2D(u_image, v_texCoord) +    texture2D(u_image, v_texCoord + vec2(onePixel.x, 0.0)) +    texture2D(u_image, v_texCoord + vec2(-onePixel.x, 0.0))) / 3.0; 

There's a good example here

like image 26
newshorts Avatar answered Oct 24 '22 14:10

newshorts