GLSL per pixel lighting with custom light type
- by Justin
Ok, I am having a big problem here. I just got into GLSL yesterday, so the code will be terrible, I'm sure.
Basically, I am attempting to make a light that can be passed into the fragment shader (for learning purposes). I have four input values: one for the position of the light, one for the color, one for the distance it can travel, and one for the intensity. I want to find the distance between the light and the fragment, then calculate the color from there. The code I have gives me a simply gorgeous ring of light that get's twisted and widened as the matrix is modified. I love the results, but it is not even close to what I am after.
I want the light to be moved with all of the vertices, so it is always in the same place in relation to the objects. I can easily take it from there, but getting that to work seems to be impossible with my current structure. Can somebody give me a few pointers (pun not intended)?
Vertex shader:
attribute vec4 position;
attribute vec4 color;
attribute vec2 textureCoordinates;
varying vec4 colorVarying;
varying vec2 texturePosition;
varying vec4 fposition;
varying vec4 lightPosition;
varying float lightDistance;
varying float lightIntensity;
varying vec4 lightColor;
void main() {
vec4 ECposition = gl_ModelViewMatrix * gl_Vertex;
vec3 tnorm = normalize(vec3 (gl_NormalMatrix * gl_Normal));
fposition = ftransform();
gl_Position = fposition;
gl_TexCoord[0] = gl_MultiTexCoord0;
fposition = ECposition;
lightPosition = vec4(0.0, 0.0, 5.0, 0.0) * gl_ModelViewMatrix * gl_Vertex;
lightDistance = 5.0;
lightIntensity = 1.0;
lightColor = vec4(0.2, 0.2, 0.2, 1.0);
}
Fragment shader:
varying vec4 colorVarying;
varying vec2 texturePosition;
varying vec4 fposition;
varying vec4 lightPosition;
varying float lightDistance;
varying float lightIntensity;
varying vec4 lightColor;
uniform sampler2D texture;
void main() {
float l_distance = sqrt((gl_FragCoord.x * lightPosition.x) + (gl_FragCoord.y * lightPosition.y) + (gl_FragCoord.z * lightPosition.z));
float l_value = lightIntensity / (l_distance / lightDistance);
vec4 l_color = vec4(l_value * lightColor.r, l_value * lightColor.g, l_value * lightColor.b, l_value * lightColor.a);
vec4 color;
color = texture2D(texture, gl_TexCoord[0].st);
gl_FragColor = l_color * color;
//gl_FragColor = fposition;
}