17
votes

Pixel based lighting is a common issue in many OpenGL applications, as the standard OpenGL lighting has very poor quality.

I want to use a GLSL program to have per-pixel based lighting in my OpenGL program instead of per-vertex. Just Diffuse lighting, but with fog, texture and texture-alpha at least.

I started with this shader:

texture.vert:

varying vec3 position;
varying vec3 normal; 

void main(void) 
{
  gl_Position       = gl_ModelViewProjectionMatrix * gl_Vertex;
  gl_FrontColor     = gl_Color;
  gl_TexCoord[0]    = gl_MultiTexCoord0; 
  normal        = normalize(gl_NormalMatrix * gl_Normal);
  position      = vec3(gl_ModelViewMatrix * gl_Vertex);
}

texture.frag:

uniform sampler2D Texture0;
uniform int ActiveLights;

varying vec3 position;
varying vec3 normal; 

void main(void) 
{
  vec3 lightDir;
  float  attenFactor;
  vec3 eyeDir           = normalize(-position); // camera is at (0,0,0) in ModelView space
  vec4 lightAmbientDiffuse  = vec4(0.0,0.0,0.0,0.0);
  vec4 lightSpecular        = vec4(0.0,0.0,0.0,0.0);    

  // iterate all lights
  for (int i=0; i<ActiveLights; ++i)
  {
    // attenuation and light direction
    if (gl_LightSource[i].position.w != 0.0)
    {
        // positional light source
        float dist  = distance(gl_LightSource[i].position.xyz, position);
        attenFactor = 1.0/( gl_LightSource[i].constantAttenuation + 
                    gl_LightSource[i].linearAttenuation * dist +
                    gl_LightSource[i].quadraticAttenuation * dist * dist );
        lightDir    = normalize(gl_LightSource[i].position.xyz - position);
    }       
    else 
    {           
        // directional light source         
        attenFactor = 1.0;          
        lightDir    = gl_LightSource[i].position.xyz;       
    }       
    // ambient + diffuse        
    lightAmbientDiffuse     += gl_FrontLightProduct[i].ambient*attenFactor;     
    lightAmbientDiffuse     += gl_FrontLightProduct[i].diffuse * max(dot(normal, lightDir), 0.0) * attenFactor; 
    // specular     
    vec3 r      = normalize(reflect(-lightDir, normal));
    lightSpecular   += gl_FrontLightProduct[i].specular * 
                  pow(max(dot(r, eyeDir), 0.0), gl_FrontMaterial.shininess) *
                  attenFactor;  
  }     
  // compute final color    
  vec4 texColor = gl_Color * texture2D(Texture0, gl_TexCoord[0].xy);    
  gl_FragColor  = texColor * (gl_FrontLightModelProduct.sceneColor + lightAmbientDiffuse) + lightSpecular;

  float fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;    // Intensität berechnen 
  fog       = clamp(fog, 0.0, 1.0);                 // Beschneiden 
  gl_FragColor  = mix(gl_Fog.color, gl_FragColor, fog);         // Nebelfarbe einmischen 
}

Comments are german because it's a german site where this code was posted, sorry.

But all this shader does is make everything very dark. No lighting effects at all - yet the shader codes compile. If I only use GL_LIGHT0 in the fragment shader, then it seems to work, but only reasonable for camera facing polygons and my floor polygon is just extremely dark. Also quads with RGBA textures show no sign of transparency. I use standard glRotate/Translate for the Modelview matrix, and glVertex/Normal for my polygons. OpenGL lighting works fine apart from the fact that it looks ugly on very large surfaces. I triple checked my normals, they are fine.

Is there something wrong in the above code? OR Tell me why there is no generic lighting Shader for this actual task (point based light with distance falloff: a candle if you will) - shouldn't there be just one correct way to do this? I don't want bump/normal/parallax/toon/blur/whatever effects. I just want my lighting to perform better with larger polygons.

All Tutorials I found are only useful for lighting a single object when the camera is at 0,0,0 facing orthogonal to the object. The above is the only one found that at least looks like the thing I want to do.

1
The obvious first: have you bound a sampler, are your texture coords and normals correct, does it show the geometry if you set gl_FragColor to a fixed colour? With shaders I find it easier to get my head around them by using and adding little features one at a time, in the absence of being able to step into and trace through them :p.Robinson
Sampler is TEXTURE_2D and it is bound the rendered polys. Texture coords are correct, since normal openGL lighting performs flawless. Normals are perfect (as already stated). Yes it shows the Geometry when setting gl_FragColor to a fixed colour or just to texColor.Rock
GLSL doesn't have generic functions or template metaprogramming, though there is a compiler that can convert C++ templates to GLSL functions.Anderson Green

1 Answers

12
votes

I would strongly suggest you to read this article to see how the standard ADS lightning is done within GLSL.That is GL 4.0 but not a problem to adjust to your version:

Also you operate in the view (camera) space so DON"T negate the eyes vector :

    vec3 eyeDir  = normalize(-position); 

I had pretty similar issues to yours because I also negated the eye vector forgetting that it is transformed into the view space.Your diffuse and specular calculations seem to be wrong too in the current scenario.In your place I wouldn't use data from the fixed pipeline at all ,otherwise what is the point doing it in a shader? Here is the method to calculate diffuse and specular in the per fragment ADS point lightning:

void ads( int lightIndex,out vec3 ambAndDiff, out vec3 spec )
{


vec3 s =  vec3(lights[lightIndex].Position -  posOut) ;
vec3 v = normalize( posOut.xyz );
vec3 n = normalize(normOut); 
vec3 h = normalize(v+s) ;// half vector (read in the web on what it is )


    vec3 diffuse =  ((Ka+ lights[lightIndex].Ld) * Kd *  max( 0.0,dot(n, v) )) ;
     spec =  Ks *  pow( max(0.0, dot(n,h) ), Shininess ) ;  


 ambAndDiff =   diffuse ;

/// Ka-material ambient factor
/// Kd-material diffuse factor
/// Ks-material specular factor.
/// lights[lightIndex].Ld-lights diffuse factor;you may also add La and Ls if you want to have even more control of the light shading.
}

Also I wouldn't suggest you using the attenuation equation you have here,it is hard to control.If you want to add light radius based attenuation there is this nice blog post: