GLSL: Get position of current pixel?

Started by
10 comments, last by Tsumuji 17 years, 9 months ago
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.
Advertisement
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.

This topic is closed to new replies.

Advertisement