Sign in to follow this  

GLSL: Get position of current pixel?

This topic is 4160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I saw this one. But I'm needing this for a globe of light. I'm trying to calculate a point light in that way. So I'm sure that I need to calculate the current pixel location, like is done in the vertex shader with gl_Vertex...

Share this post


Link to post
Share on other sites
Here is a link to the lighthouse3d again, but now about per pixel lightning.

http://www.lighthouse3d.com/opengl/glsl/index.php?dirlightpix

All in all i don'T uderstand your problem at all. But this seems the most possible solution for you.

Share this post


Link to post
Share on other sites
For point lights, the standard approach is the compute the distance from the light per-vertex, then pass it on to the PS, letting it get interpolated along the way.

You can do more or less the same thing with the entire position rather than just a radial distance, but computing the distance per pixel is a complete waste of time.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also keep in mind that for big traingles the difference will be noticeable because distance doesn't vary linearly, and you should really be computing the distance per pixel.But for most well tessellated model it should be fine.

Share this post


Link to post
Share on other sites
So I'm trying to do somthing like this:

void main()
{
vec4 Idiff;
if ( distance(gl_FragCoord, gl_LightSource[0].position) < 300.0 )
Idiff = vec4(1.0, 1.0, 0.0, 1.0);
else
Idiff = vec4(0.0, 0.0, 1.0, 1.0);

vec4 pixelFinalColor = Idiff;
gl_FragColor = pixelFinalColor;
}


Result: [ http://geocities.yahoo.com.br/megaspeedlinux/und/screens/sp9.jpg ]
-Not worried about ambient and specular for now. Just trying to get things to work.
And this is. Just trying to calculate the actual distance from the current pixel and the light position. If minor than 300.0(this must br fixed to 3.0, since this calculation is getting the distance in pixels...) the area is "lighted", if not, take the other color. Is possible?

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
For point lights, the standard approach is the compute the distance from the light per-vertex, then pass it on to the PS, letting it get interpolated along the way.

You can do more or less the same thing with the entire position rather than just a radial distance, but computing the distance per pixel is a complete waste of time.


I wouldn't exactly call it a "waste of time". It's far more accurate. Radial distance is just a scalar, it will not get interpolated correctly with low tesselation. A point light lighting a big flat quad would result in a completely incorrect image(assuming the distance is used for attenuation); because the distance calculated in all the vertices would be big, the whole surface will get very little light.

Tsumuji: All you have to do is calculate in the VS the vertex-to-light vector, and pass it to the PS. There, you calculate the length of interpolated vector, and that's the distance between the light position and the pixel position.

Share this post


Link to post
Share on other sites
I think I got it:


code:

pov = gl_ModelViewMatrix * gl_Vertex;

void main()
{
//vec3 dist = vec3(3.0, 3.0, 3.0);
//vec3 distC = gl_LightSource[0].position.xyz - ecPosition;
vec3 L = normalize(gl_LightSource[0].position.xyz - ecPosition);
//vec4 pixelFinalColor = gl_FrontLightProduct[0].diffuse;
//vec4 pixelFinalColor = gl_FrontLightProduct[0].ambient;
//vec4 pixelFinalColor = gl_FrontLightProduct[0].specular;
//vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(normal,L), 0.0);
vec4 Idiff = vec4(0.0, 0.0, 1.0, 1.0);
if ( distance(pov, gl_LightSource[0].position) < 1.0 )//1.0 -> raio de ação da luz
Idiff = vec4(1.0, 1.0, 0.0, 1.0);
vec4 Iamb = gl_FrontLightProduct[0].ambient;

vec4 pixelFinalColor = Idiff;
vec4 texcolor = texture2D(tex,gl_TexCoord[0].st);

gl_FragColor = pixelFinalColor /** texcolor*/;
}





pov get the vertex position(pov = gl_ModelViewMatrix * gl_Vertex;) and passes it for fragment shader. Then I do distance(pov, gl_LightSource[0].position) to get the length.

Share this post


Link to post
Share on other sites

This topic is 4160 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this