[Shader Library] Bump Mapping Shader with Multiple Lights (GLSL)



Shader Library - Bump mapping



More shaders are available here: Geeks3D Shader Library.

This is a bump mapping shader (Phong lighting) based on this tutorial.
This shader supports several lights (up to 8).

You can download a GeeXLab demo to see the shader in real time:
Download GLSL_Bump_Mapping_Multiple_Lights.zip

Unzip the archive somewhere and launch the demo with Start_Demo_ATI_NVIDIA.bat.

Some results:

  • Core 2 Duo E8400 / GeForce GTS 250 (Fw191.07) / WinXP SP2: 2050FPS (800×600) and 650 FPS (1920×1200)
  • AMD X2 3800+ / Radeon HD 4850 (Cat9.9) / WinXP SP2: 2480FPS (800×600) and 1150 FPS (1920×1080)

Shader Library - Bump mapping

Shader desription (for NVIDIA and ATI)

Language: OpenGL 2 – GLSL

Type: Lighting

Inputs:

  • glTangent4f (vec4): tangent space vector per vertex. glTangent4f is a vertex attribute and is provided by GeeXLab 3D engine.
  • colorMap (sampler2D): color texture on texture unit 0
  • normalMap (sampler2D): normal texture on texture unit 1

Ouputs: color buffer

Shader code:

[Vertex_Shader]
#define MAX_LIGHTS 8
#define NUM_LIGHTS 3
varying vec3 lightVec[MAX_LIGHTS]; 
varying vec3 viewVec;
attribute vec4 glTangent4f; 
void main(void)
{
  gl_Position = ftransform();
  gl_TexCoord[0] = gl_MultiTexCoord0;
  
  vec3 n = normalize(gl_NormalMatrix * gl_Normal);
  vec3 t = normalize(gl_NormalMatrix * glTangent4f.xyz);
  vec3 b = cross(n, t);
  
  vec3 v;
  vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
  int i;
  for (i=0; i<NUM_LIGHTS; ++i)
  {
    vec3 lVec = gl_LightSource[i].position.xyz - vVertex;
    v.x = dot(lVec, t);
    v.y = dot(lVec, b);
    v.z = dot(lVec, n);
    lightVec[i] = v;
  }
  
  vec3 vVec = -vVertex;
  v.x = dot(vVec, t);
  v.y = dot(vVec, b);
  v.z = dot(vVec, n);
  viewVec = v;
}

[Pixel_Shader]
#define MAX_LIGHTS 8
#define NUM_LIGHTS 3
varying vec3 lightVec[MAX_LIGHTS]; 
varying vec3 viewVec;
uniform sampler2D colorMap;
uniform sampler2D normalMap;
void main (void)
{
  vec2 uv = gl_TexCoord[0].st * 4.0;
  vec4 base = texture2D(colorMap, uv);
  vec4 final_color = vec4(0.2, 0.2, 0.2, 1.0) * base;
  vec3 vVec = normalize(viewVec);
  vec3 bump = 
     normalize(texture2D(normalMap, uv).xyz * 2.0 - 1.0);
  vec3 R = reflect(-vVec, bump);
  int i;
  for (i=0; i<NUM_LIGHTS; ++i)
  {	
    vec3 lVec = normalize(lightVec[i]);
    float diffuse = max(dot(lVec, bump), 0.0);
    vec4 vDiffuse = 
       gl_FrontLightProduct[i].diffuse * 
       diffuse * base;	
    final_color += vDiffuse;
  
    float specular = 
      pow(clamp(dot(R, lVec), 0.0, 1.0), 
            gl_FrontMaterial.shininess);
    vec4 vSpecular = 
      gl_FrontLightProduct[i].specular * 
      specular * diffuse;	
    final_color += vSpecular;
  }
  
  gl_FragColor = final_color;	
}


3 thoughts on “[Shader Library] Bump Mapping Shader with Multiple Lights (GLSL)”

  1. horacio

    Not work, my pc amd x2 and ati 4850 (ct 9.9).
    not found phyton26.ddl.
    When found this libriry???
    Thanks!!

  2. alain

    how can i implement the attribute vec4 glTangent4f;
    in a cpp file ?

Comments are closed.