1 /// ============================================================================
3 Copyright (C) 2004 Robert Beckebans <trebor_7@users.sourceforge.net>
4 Please see the file "CONTRIBUTORS" for a list of contributors
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /// ============================================================================
23 uniform sampler2D u_diffusemap;
24 uniform sampler2D u_bumpmap;
25 uniform sampler2D u_specularmap;
26 uniform sampler2D u_attenuationmap_xy;
27 uniform sampler2D u_attenuationmap_z;
28 uniform vec3 u_view_origin;
29 uniform vec3 u_light_origin;
30 uniform vec3 u_light_color;
31 uniform float u_bump_scale;
32 uniform float u_specular_exponent;
34 varying vec3 var_vertex;
35 varying vec4 var_tex_diffuse_bump;
36 varying vec2 var_tex_specular;
37 varying vec4 var_tex_atten_xy_z;
38 varying mat3 var_mat_os2ts;
42 // compute view direction in tangent space
43 vec3 V = normalize(var_mat_os2ts * (u_view_origin - var_vertex));
45 // compute light direction in tangent space
46 vec3 L = normalize(var_mat_os2ts * (u_light_origin - var_vertex));
48 // compute half angle in tangent space
49 vec3 H = normalize(L + V);
51 // compute normal in tangent space from bumpmap
52 vec3 N = 2.0 * (texture2D(u_bumpmap, var_tex_diffuse_bump.pq).xyz - 0.5);
56 // compute the diffuse term
57 vec4 diffuse = texture2D(u_diffusemap, var_tex_diffuse_bump.st);
58 diffuse.rgb *= u_light_color * clamp(dot(N, L), 0.0, 1.0);
60 // compute the specular term
61 vec3 specular = texture2D(u_specularmap, var_tex_specular).rgb * u_light_color * pow(clamp(dot(N, H), 0.0, 1.0), u_specular_exponent);
63 // compute attenuation
64 vec3 attenuation_xy = texture2DProj(u_attenuationmap_xy, vec3(var_tex_atten_xy_z.x, var_tex_atten_xy_z.y, var_tex_atten_xy_z.w)).rgb;
65 vec3 attenuation_z = texture2D(u_attenuationmap_z, vec2(var_tex_atten_xy_z.z, 0)).rgb;
67 // compute final color
68 gl_FragColor.rgba = diffuse;
69 gl_FragColor.rgb += specular;
70 gl_FragColor.rgb *= attenuation_xy;
71 gl_FragColor.rgb *= attenuation_z;