# GLSL: Get position of current pixel?

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

## Recommended Posts

There's a way to get the current pixel location in a GLSL shader? I'm needing this to calculate my point light...

##### Share on other sites
http://www.lighthouse3d.com/opengl/glsl/index.php?lights

Here is a tutorial on GLSL. There is plenty info on lights of different types

##### Share on other sites
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 on other sites
Put non-transformed vertex location as return argument of your vertex shader. That way youll get location in pixel shader for every pixel.

##### 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 on other sites
What position? World position? Transformed position?

##### Share on other sites
If by pixel you mean a fragment's position, then you can use gl_FragCoord (it's a vec4).

##### 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 on other sites
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 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?

1. 1
2. 2
3. 3
frob
20
4. 4
Rutin
19
5. 5

• 32
• 13
• 11
• 9
• 18
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007066

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!