views:

1777

answers:

3

Ok, in my glsl fragment shader I want to be able to calculate the distance of the fragment from a particular line in space.

The result of this is that I am first trying to use a varying vec2 set in my vertex shader to mirror what ends up in gl_FragCoord:

varying vec2 fake_frag_coord;
//in vertex shader:
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
fake_frag_coord=(gl_ModelViewProjectionMatrix * gl_Vertex).xy;

Now in the fragment shader I expect

gl_FragCoord.xy==fake_frag_coord

But it does not. What operation does the pipeline do on gl_Position to turn it into gl_FragCoord that I am neglecting to do on fake_frag_coord?

Thanks.

+3  A: 

gl_FragCoord is screen coordinates, so you'd need to have information about the screen size and such to produce it based on the Position. The fake_frag_coord you've produced will correspond to a point in the screen in normalized device coordinates I think (-1 to 1). So if you were to multiply by half the screen's size in a particular dimension, and then add that size, you'd get actual screen pixels.

Jay Kominek
You are not answering his question.
karx11erx
I'd be fascinated to hear what you think the question is, considering that the asker accepted this answer.
Jay Kominek
I was wrong about you being wrong, but right about using gl_ModelViewProjectionMatrix * gl_Vertex potentially introducing compute errors.
karx11erx
A: 

The problem you have is that gl_ModelViewProjectionMatrix * gl_Vertex computed in a shader is not always what the fixed function pipeline yields. To get identical results, do fake_frag_coord = ftransform().

Edit:

And then scale it with your screen dimensions.

karx11erx
ftransform produces the position, not the fragcoord. The question isn't asking how to get the position.
Jay Kominek
My answer wasn't completely wrong. You need to scale fake_frag_coord with your screen dimensions, but you may get different results depending on whether you use fake_frag_coord = gl_ModelViewProjectionMatrix * gl_Vertex or = ftransform().
karx11erx
A: 

varying vec2 fake_frag_coord; //in vertex shader:

fake_frag_coord=(gl_ModelViewMatrix * gl_Vertex).xy

medjebbar