Wrong lightning with normalmap-shader

Good Evening :slight_smile:

I am dontknow and I am new to GLSL. I use a normalmap-shader together with OpenB3D in my project. But somehow the objects I applied the shader on get enlightened even if the light source is far away(see attachment).
The light source is a pointlight and somehow it works with the floor, but the walls are definitely wrong.

Vertex shader:

// shared vars between fragment and vertex shader
varying vec3 lightVec; 
varying vec3 eyeVec;   
varying vec2 texCoord; 
varying vec3 vTangent; 
                     

void main(void) {
  gl_Position = ftransform();                       
  texCoord = gl_MultiTexCoord0.xy;                  
  vec3 n = normalize(gl_NormalMatrix * gl_Normal);  
  vec3 c1 = cross( gl_Normal, vec3(0.0, 0.0, 1.0) );
  vec3 c2 = cross( gl_Normal, vec3(0.0, 1.0, 0.0) );
  if( length(c1)>length(c2) ) {
    vTangent = c1;
  }
  else
  {
    vTangent = c2;
  }
  vTangent = -normalize(vTangent);
  vec3 t = normalize(gl_NormalMatrix * vTangent);
  vec3 b = normalize(cross(n, t));
  b = -b;
  vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
  vec3 lv = vec3(gl_LightSource[0].position.x,gl_LightSource[0].position.y,gl_LightSource[0].position.z);
  vec3 tmpVec = lv - vVertex; 
  lightVec.x = dot(tmpVec, t);
  lightVec.y = dot(tmpVec, b);
  lightVec.z = dot(tmpVec, n);
  tmpVec   = -vVertex;          
  eyeVec.x = dot(tmpVec, t);  
  eyeVec.y = dot(tmpVec, b);  
  eyeVec.z = dot(tmpVec, n);  
}

Fragment shader:

// shared vars between fragment and vertex shader
varying vec3 lightVec;
varying vec3 eyeVec;  
varying vec2 texCoord;
// shared vars between fragment shader and OpenB3D+
uniform sampler2D tex; 
uniform sampler2D norm;

void main (void) {
  float distSqr = dot(lightVec, lightVec);
  vec3 lVec = lightVec * inversesqrt(distSqr);
  vec3 vVec = normalize(eyeVec);
  vec4 base = texture2D(tex, texCoord);
  vec3 bump = normalize( texture2D(norm, texCoord).xyz * 2.0 - 1.0);
  vec4 vAmbient = gl_LightSource[0].ambient;
  float diffuse = max( dot(lVec, bump), 0.0 );
  vec4 vDiffuse = gl_LightSource[0].diffuse * diffuse;    
  vec4 vSpecular = gl_LightSource[0].specular;
  gl_FragColor = ( vAmbient*base + vDiffuse*base );
}

I hope you can help me :slight_smile:

Your shader doesnโ€™t consider the distance between the light and the surface; [var]lVec[/var] always has unit length.

How can I make it considering the distance?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.