This is my last post on GLSL. To tell the truth after all these post on GLSL I do not fully understand it. Either way here is bump mapping using GLSL. To make more sense of bump mapping here is an except from my project.

Bump mapping is an alternative to normal mapping in that it uses a grayscale height map instead of an RGB map. The height map determines how high something is. However, we won’t be actually moving any vertices. Like normal mapping, we will only change the normals.

As you may recall, the normal is perpendicular to the surface underneath. Another way to put it is that the normal is perpendicular to the tangent of the surface. Hence, we need to first calculate two tangents. To calculate a tangent, we need to know the height value just above and below (or to the left and to the right) of the current position. We can offset the texture coordinates in four different ways and use these offsets in four calls to texture2D. Note, for some maps, you may need to fiddle with this offset value to get something right (….this is probably why normal mapping is more prevalent in industry….). Once you have the two tangents, simply cross them and normalize to get the new normal.

## Vert Shader

uniform sampler2D my_color_texture; varying vec2 texture_coord; varying vec3 normal; varying vec3 vertex_to_light; varying vec3 vertex_to_camera; void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; normal = normalize(gl_NormalMatrix * gl_Normal); vec4 vertex_modelview = gl_ModelViewMatrix * gl_Vertex; vertex_to_light = gl_LightSource[0].position.xyz - vertex_modelview.xyz; vertex_to_camera = -vertex_modelview.xyz; texture_coord = vec2 (gl_MultiTexCoord0); }

## Frag Shader

uniform sampler2D my_color_texture; varying vec2 texture_coord; varying vec3 normal; varying vec3 vertex_to_light; varying vec3 vertex_to_camera; const float specularpower = 25.0; const float off = 0.005; const float falloff = 70.0; void main() { vec4 diffuse_color = vec4(0.0, 0.5, 1.0, 1.0); vec4 specular_color = vec4(1.0, 1.0, 1.0, 1.0); vec3 n_normal = normalize(normal); vec3 n_vertex_to_light = normalize(vertex_to_light); float diffuse = clamp (dot (n_normal, n_vertex_to_light), 0.0, 1.0); vec3 n_vertex_to_camera = normalize(vertex_to_camera); vec3 ref = reflect(-n_vertex_to_light, n_normal); float specular = clamp(dot (n_vertex_to_camera, ref), 0.0, 1.0); specular = pow(specular, specularpower); gl_FragColor.xyz = diffuse * diffuse_color.rgb + specular * specular_color.rgb; gl_FragColor.a = 1.0; }