| 1 |
/// ============================================================================ |
| 2 |
/* |
| 3 |
Copyright (C) 2004 Robert Beckebans <[email protected]> |
| 4 |
Please see the file "AUTHORS" for a list of contributors |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU 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. |
| 10 |
|
| 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. |
| 14 |
|
| 15 |
See the GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU 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. |
| 20 |
*/ |
| 21 |
/// ============================================================================ |
| 22 |
|
| 23 |
#include "utils.cg" |
| 24 |
|
| 25 |
struct cg_vertex2fragment |
| 26 |
{ |
| 27 |
float4 position : TEXCOORD0; |
| 28 |
float4 tex_diffuse_bump : TEXCOORD1; |
| 29 |
float4 tex_specular : TEXCOORD2; |
| 30 |
float4 tex_atten_xy_z : TEXCOORD3; |
| 31 |
|
| 32 |
float3 tangent : TEXCOORD4; |
| 33 |
float3 binormal : TEXCOORD5; |
| 34 |
float3 normal : TEXCOORD6; |
| 35 |
}; |
| 36 |
|
| 37 |
struct cg_fragment2final |
| 38 |
{ |
| 39 |
float4 color : COLOR; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
cg_fragment2final main(cg_vertex2fragment IN, |
| 44 |
uniform sampler2D diffusemap, |
| 45 |
uniform sampler2D bumpmap, |
| 46 |
uniform sampler2D specularmap, |
| 47 |
uniform sampler2D attenuationmap_xy, |
| 48 |
uniform sampler2D attenuationmap_z, |
| 49 |
uniform float3 view_origin, |
| 50 |
uniform float3 light_origin, |
| 51 |
uniform float3 light_color, |
| 52 |
uniform float bump_scale, |
| 53 |
uniform float specular_exponent) |
| 54 |
{ |
| 55 |
cg_fragment2final OUT; |
| 56 |
|
| 57 |
// construct object-space-to-tangent-space 3x3 matrix |
| 58 |
float3x3 rotation = float3x3(IN.tangent, IN.binormal, IN.normal); |
| 59 |
|
| 60 |
// compute view direction in tangent space |
| 61 |
float3 V = normalize(mul(rotation, view_origin - IN.position.xyz)); |
| 62 |
|
| 63 |
// compute light direction in tangent space |
| 64 |
float3 L = normalize(mul(rotation, (light_origin - IN.position.xyz))); |
| 65 |
|
| 66 |
// compute half angle in tangent space |
| 67 |
float3 H = normalize(L + V); |
| 68 |
|
| 69 |
// compute normal in tangent space from bumpmap |
| 70 |
float3 T = CG_Expand(tex2D(bumpmap, IN.tex_diffuse_bump.zw).xyz); |
| 71 |
T.z *= bump_scale; |
| 72 |
float3 N = normalize(T); |
| 73 |
|
| 74 |
// compute the diffuse term |
| 75 |
float4 diffuse = tex2D(diffusemap, IN.tex_diffuse_bump.xy); |
| 76 |
diffuse.rgb *= light_color * saturate(dot(N, L)); |
| 77 |
|
| 78 |
// compute the specular term |
| 79 |
float3 specular = tex2D(specularmap, IN.tex_specular.xy).rgb * light_color * pow(saturate(dot(N, H)), specular_exponent); |
| 80 |
|
| 81 |
// compute attenuation |
| 82 |
float3 attenuation_xy = tex2Dproj(attenuationmap_xy, float3(IN.tex_atten_xy_z.x, IN.tex_atten_xy_z.y, IN.tex_atten_xy_z.w)).rgb; |
| 83 |
float3 attenuation_z = tex2D(attenuationmap_z, float2(IN.tex_atten_xy_z.z, 0)).rgb; |
| 84 |
|
| 85 |
// compute final color |
| 86 |
OUT.color.rgba = diffuse; |
| 87 |
OUT.color.rgb += specular; |
| 88 |
OUT.color.rgb *= attenuation_xy; |
| 89 |
OUT.color.rgb *= attenuation_z; |
| 90 |
|
| 91 |
return OUT; |
| 92 |
} |