How to get pixel information inside a fragment shader?

Posted by user697111 on Stack Overflow See other posts from Stack Overflow or by user697111
Published on 2012-03-19T23:25:37Z Indexed on 2012/03/19 23:30 UTC
Read the original article Hit count: 275

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about opengl

Related posts about glsl