I am getting an error somewhere in my DirectionalShadow() function. It works fine on a GEForce 8800 but fails to link on SM 3.0 hardware. The error seems to be at or involve line 156:
smcoord = (lmat0 * vec4(VertexPosition.x,VertexPosition.y,VertexPosition.z,1.0)).xyz;
The error:
Error: Failed to link shader program.
Fragment info
-------------
(50) : warning C7050: "$temp19" might be used before being initialized
(100) : warning C7050: "$temp22" might be used before being initialized
Internal error: assembly compile error for fragment shader at offset 14123:
-- error message --
line 320, column 15: error: variable not valid as a source register
line 325, column 15: error: variable not valid as a source register
-- internal assembly text --
!!ARBfp1.0
OPTION NV_fragment_program2;
OPTION ARB_fragment_program_shadow;
# cgc version 2.0.0018, build date Dec 5 2007
# command line args:
#vendor NVIDIA Corporation
#version 2.0.1.18
#profile fp40
#program main
#semantic texture0
#semantic texture1
#semantic texture8
#semantic texture9
#semantic texture10
#semantic texture11
#semantic texture12
#semantic CameraPosition
#semantic LightMatrix
#semantic IgnoreNormals
#semantic gl_LightSource : state.light
#semantic gl_Fog : state.fog
#var sampler2D texture0 : : texunit 0 : -1 : 1
#var sampler2D texture1 : : texunit 1 : -1 : 1
#var sampler2DSHADOW texture8 : : texunit 2 : -1 : 1
#var sampler2DSHADOW texture9 : : : -1 : 0
#var sampler2DSHADOW texture10 : : : -1 : 0
#var sampler2DSHADOW texture11 : : : -1 : 0
#var sampler2DSHADOW texture12 : : : -1 : 0
#var float3 VertexPosition : $vin.TEX0 : TEX0 : -1 : 1
#var float3 T : $vin.TEX1 : TEX1 : -1 : 1
#var float3 B : $vin.TEX2 : TEX2 : -1 : 1
#var float3 N : $vin.TEX3 : TEX3 : -1 : 1
#var float2 texCoord0 : $vin.TEX4 : TEX4 : -1 : 1
#var float2 texCoord1 : : : -1 : 0
#var float3 cubeCoord : : : -1 : 0
#var float4 ModelVertex : $vin.TEX5 : TEX5 : -1 : 1
#var float3 CameraPosition : : c[4] : -1 : 1
#var float4x4 LightMatrix[0] : : c[0], 4 : -1 : 1
#var float4x4 LightMatrix[1] : : , 4 : -1 : 0
#var float4x4 LightMatrix[2] : : , 4 : -1 : 0
#var float4x4 LightMatrix[3] : : , 4 : -1 : 0
#var float IgnoreNormals : : c[5] : -1 : 1
#var float4 gl_Color : $vin.COLOR : COL0 : -1 : 1
#var float4 gl_FragColor : $vout.COLOR : COL : -1 : 1
#var float4 gl_LightSource[0].ambient : state.light[0].ambient : c[6] : -1 : 1
#var float4 gl_LightSource[0].diffuse : state.light[0].diffuse : c[7] : -1 : 1
#var float4 gl_LightSource[0].specular : state.light[0].specular : : -1 : 0
#var float4 gl_LightSource[0].position : state.light[0].position : c[8] : -1 : 1
#var float4 gl_LightSource[0].halfVector : state.light[0].half : : -1 : 0
#var float3 gl_LightSource[0].spotDirection : state.light[0].spot.direction : c[9] : -1 : 1
#var float gl_LightSource[0].spotExponent : state.light[0].attenuation.w : c[10] : -1 : 0
#var float gl_LightSource[0].spotCutoff : NONE : c[11] : -1 : 1
#var float gl_LightSource[0].spotCosCutoff : state.light[0].spot.direction.w : c[9] : -1 : 0
#var float gl_LightSource[0].constantAttenuation : state.light[0].attenuation.x : : -1 : 0
#var float gl_LightSource[0].linearAttenuation : state.light[0].attenuation.y : c[10] : -1 : 1
#var float gl_LightSource[0].quadraticAttenuation : state.light[0].attenuation.z : c[10] : -1 : 1
#var float4 gl_LightSource[1].ambient : state.light[1].ambient : : -1 : 0
#var float4 gl_LightSource[1].diffuse : state.light[1].diffuse : : -1 : 0
#var float4 gl_LightSource[1].specular : state.light[1].specular : : -1 : 0
#var float4 gl_LightSource[1].position : state.light[1].position : : -1 : 0
#var float4 gl_LightSource[1].halfVector : state.light[1].half : : -1 : 0
#var float3 gl_LightSource[1].spotDirection : state.light[1].spot.direction : : -1 : 0
#var float gl_LightSource[1].spotExponent : state.light[1].attenuation.w : : -1 : 0
#var float gl_LightSource[1].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[1].spotCosCutoff : state.light[1].spot.direction.w : : -1 : 0
#var float gl_LightSource[1].constantAttenuation : state.light[1].attenuation.x : : -1 : 0
#var float gl_LightSource[1].linearAttenuation : state.light[1].attenuation.y : : -1 : 0
#var float gl_LightSource[1].quadraticAttenuation : state.light[1].attenuation.z : : -1 : 0
#var float4 gl_LightSource[2].ambient : state.light[2].ambient : : -1 : 0
#var float4 gl_LightSource[2].diffuse : state.light[2].diffuse : : -1 : 0
#var float4 gl_LightSource[2].specular : state.light[2].specular : : -1 : 0
#var float4 gl_LightSource[2].position : state.light[2].position : : -1 : 0
#var float4 gl_LightSource[2].halfVector : state.light[2].half : : -1 : 0
#var float3 gl_LightSource[2].spotDirection : state.light[2].spot.direction : : -1 : 0
#var float gl_LightSource[2].spotExponent : state.light[2].attenuation.w : : -1 : 0
#var float gl_LightSource[2].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[2].spotCosCutoff : state.light[2].spot.direction.w : : -1 : 0
#var float gl_LightSource[2].constantAttenuation : state.light[2].attenuation.x : : -1 : 0
#var float gl_LightSource[2].linearAttenuation : state.light[2].attenuation.y : : -1 : 0
#var float gl_LightSource[2].quadraticAttenuation : state.light[2].attenuation.z : : -1 : 0
#var float4 gl_LightSource[3].ambient : state.light[3].ambient : : -1 : 0
#var float4 gl_LightSource[3].diffuse : state.light[3].diffuse : : -1 : 0
#var float4 gl_LightSource[3].specular : state.light[3].specular : : -1 : 0
#var float4 gl_LightSource[3].position : state.light[3].position : : -1 : 0
#var float4 gl_LightSource[3].halfVector : state.light[3].half : : -1 : 0
#var float3 gl_LightSource[3].spotDirection : state.light[3].spot.direction : : -1 : 0
#var float gl_LightSource[3].spotExponent : state.light[3].attenuation.w : : -1 : 0
#var float gl_LightSource[3].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[3].spotCosCutoff : state.light[3].spot.direction.w : : -1 : 0
#var float gl_LightSource[3].constantAttenuation : state.light[3].attenuation.x : : -1 : 0
#var float gl_LightSource[3].linearAttenuation : state.light[3].attenuation.y : : -1 : 0
#var float gl_LightSource[3].quadraticAttenuation : state.light[3].attenuation.z : : -1 : 0
#var float4 gl_LightSource[4].ambient : state.light[4].ambient : : -1 : 0
#var float4 gl_LightSource[4].diffuse : state.light[4].diffuse : : -1 : 0
#var float4 gl_LightSource[4].specular : state.light[4].specular : : -1 : 0
#var float4 gl_LightSource[4].position : state.light[4].position : : -1 : 0
#var float4 gl_LightSource[4].halfVector : state.light[4].half : : -1 : 0
#var float3 gl_LightSource[4].spotDirection : state.light[4].spot.direction : : -1 : 0
#var float gl_LightSource[4].spotExponent : state.light[4].attenuation.w : : -1 : 0
#var float gl_LightSource[4].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[4].spotCosCutoff : state.light[4].spot.direction.w : : -1 : 0
#var float gl_LightSource[4].constantAttenuation : state.light[4].attenuation.x : : -1 : 0
#var float gl_LightSource[4].linearAttenuation : state.light[4].attenuation.y : : -1 : 0
#var float gl_LightSource[4].quadraticAttenuation : state.light[4].attenuation.z : : -1 : 0
#var float4 gl_LightSource[5].ambient : state.light[5].ambient : : -1 : 0
#var float4 gl_LightSource[5].diffuse : state.light[5].diffuse : : -1 : 0
#var float4 gl_LightSource[5].specular : state.light[5].specular : : -1 : 0
#var float4 gl_LightSource[5].position : state.light[5].position : : -1 : 0
#var float4 gl_LightSource[5].halfVector : state.light[5].half : : -1 : 0
#var float3 gl_LightSource[5].spotDirection : state.light[5].spot.direction : : -1 : 0
#var float gl_LightSource[5].spotExponent : state.light[5].attenuation.w : : -1 : 0
#var float gl_LightSource[5].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[5].spotCosCutoff : state.light[5].spot.direction.w : : -1 : 0
#var float gl_LightSource[5].constantAttenuation : state.light[5].attenuation.x : : -1 : 0
#var float gl_LightSource[5].linearAttenuation : state.light[5].attenuation.y : : -1 : 0
#var float gl_LightSource[5].quadraticAttenuation : state.light[5].attenuation.z : : -1 : 0
#var float4 gl_LightSource[6].ambient : state.light[6].ambient : : -1 : 0
#var float4 gl_LightSource[6].diffuse : state.light[6].diffuse : : -1 : 0
#var float4 gl_LightSource[6].specular : state.light[6].specular : : -1 : 0
#var float4 gl_LightSource[6].position : state.light[6].position : : -1 : 0
#var float4 gl_LightSource[6].halfVector : state.light[6].half : : -1 : 0
#var float3 gl_LightSource[6].spotDirection : state.light[6].spot.direction : : -1 : 0
#var float gl_LightSource[6].spotExponent : state.light[6].attenuation.w : : -1 : 0
#var float gl_LightSource[6].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[6].spotCosCutoff : state.light[6].spot.direction.w : : -1 : 0
#var float gl_LightSource[6].constantAttenuation : state.light[6].attenuation.x : : -1 : 0
#var float gl_LightSource[6].linearAttenuation : state.light[6].attenuation.y : : -1 : 0
#var float gl_LightSource[6].quadraticAttenuation : state.light[6].attenuation.z : : -1 : 0
#var float4 gl_LightSource[7].ambient : state.light[7].ambient : : -1 : 0
#var float4 gl_LightSource[7].diffuse : state.light[7].diffuse : : -1 : 0
#var float4 gl_LightSource[7].specular : state.light[7].specular : : -1 : 0
#var float4 gl_LightSource[7].position : state.light[7].position : : -1 : 0
#var float4 gl_LightSource[7].halfVector : state.light[7].half : : -1 : 0
#var float3 gl_LightSource[7].spotDirection : state.light[7].spot.direction : : -1 : 0
#var float gl_LightSource[7].spotExponent : state.light[7].attenuation.w : : -1 : 0
#var float gl_LightSource[7].spotCutoff : NONE : : -1 : 0
#var float gl_LightSource[7].spotCosCutoff : state.light[7].spot.direction.w : : -1 : 0
#var float gl_LightSource[7].constantAttenuation : state.light[7].attenuation.x : : -1 : 0
#var float gl_LightSource[7].linearAttenuation : state.light[7].attenuation.y : : -1 : 0
#var float gl_LightSource[7].quadraticAttenuation : state.light[7].attenuation.z : : -1 : 0
#var float4 gl_Fog.color : state.fog.color : c[12] : -1 : 1
#var float gl_Fog.density : state.fog.params.x : : -1 : 0
#var float gl_Fog.start : state.fog.params.y : : -1 : 0
#var float gl_Fog.end : state.fog.params.z : c[13] : -1 : 1
#var float gl_Fog.scale : state.fog.params.w : c[13] : -1 : 0
#var float gl_FogFragCoord : $vin.FOGC : FOGC : -1 : 1
#const
The shader:
#define LW_MAX_PASS_SIZE 0
#define LW_MAXLIGHTS 8
#define LW_DIRECTIONALLIGHT0 0
#define LW_DIRECTIONALLIGHTSAMPLER0 texture8
#define LW_LIGHTMATRIXARRAYSIZE 4
#define LW_LIGHTQUALITY1
#define LW_DIFFUSE texture0
#define LW_BUMPMAP texture1
#ifdef LW_DIFFUSE
uniform sampler2D LW_DIFFUSE;
#endif
#ifdef LW_BUMPMAP
uniform sampler2D LW_BUMPMAP;
#endif
#ifdef LW_GLOWMAP
uniform sampler2D LW_GLOWMAP;
#endif
#ifdef LW_CUBEMAP
uniform samplerCube LW_CUBEMAP;
#endif
//uniform sampler2D texture4;
//uniform sampler2D texture5;
//uniform sampler2D texture6;
//uniform sampler2D texture7;
uniform sampler2DShadow texture8;
uniform sampler2DShadow texture9;
uniform sampler2DShadow texture10;
uniform sampler2DShadow texture11;
uniform sampler2DShadow texture12;
varying vec3 VertexPosition;
varying vec3 T,B,N;
varying vec2 texCoord0;
varying vec2 texCoord1;
varying vec3 cubeCoord;
varying vec4 ModelVertex;
uniform vec3 CameraPosition;
uniform mat4 LightMatrix[ LW_LIGHTMATRIXARRAYSIZE ];
uniform float IgnoreNormals;
vec4 light(in int i, in vec3 normal, in vec3 position, in float shininess ) {
float intensity = 0.0;
float attenuation;
vec4 color;
// Directional light
attenuation = clamp( dot( normal, gl_LightSource[i].position.xyz ),0.0,1.0) * (1.0 - IgnoreNormals) + IgnoreNormals;
intensity += attenuation * (1.0 - gl_LightSource[i].position.w );
// Point/spot light
vec3 dp = ( gl_LightSource[i].position.xyz - position ) * gl_LightSource[i].position.w + gl_LightSource[i].position.xyz * ( 1.0 - gl_LightSource[i].position.w );
float d = length(dp);
d = max(d,0.0001);
attenuation = clamp( dot(normal,normalize(dp) ), 0.0, 1.0) * (1.0 - IgnoreNormals) + IgnoreNormals;
// Spot attenuation
float spotDot = dot( normalize(-dp), gl_LightSource[i].spotDirection);
if (spotDot < gl_LightSource[i].spotCosCutoff) {
attenuation = 0.0;
}
float spotAttenuation = min( 1.0 - (spotDot - gl_LightSource[i].spotExponent) / (gl_LightSource[i].spotCosCutoff - gl_LightSource[i].spotExponent) ,1.0);
if (gl_LightSource[i].spotCutoff==180.0) {
spotAttenuation=1.0;
}
attenuation *= spotAttenuation;
intensity += ( clamp(1.0-d / gl_LightSource[i].linearAttenuation,0.0,1.0) * attenuation ) * gl_LightSource[i].position.w;
if (intensity==0.0) {
return vec4(0.0);
}
// Diffuse
color = gl_LightSource[i].diffuse * intensity;
// Specular
if (shininess>0.0) {
vec3 halfvec = normalize( normalize(dp) + normalize(-position) );
vec4 specular = vec4( pow(max(0.0, dot(halfvec,normal)),8.0) * intensity ) * 4.0 * shininess;
#ifdef LW_SPECULARCOLOR
specular *= LW_SPECULARCOLOR;
#endif
specular *= gl_LightSource[i].diffuse;
color += specular;
}
return color;
}
vec4 DirectionalShadow( in int i, in mat4 lmat0, in mat4 lmat1, in mat4 lmat2, in mat4 lmat3, in sampler2DShadow shadowmap, in float shadowoffset ) {
//vec4 DirectionalShadow( in int i, in int lightmatindex, in sampler2DShadow shadowmap, in float shadowoffset ) {
int lightindex=-1;
vec4 shadowrange=vec4(8.0,20.0,40.0,160.0);
vec4 shadowcolor;
vec3 smcoord;
float d = length(VertexPosition-CameraPosition);
if (d<shadowrange.w) {
lightindex=3;
//lightindex=lightmatindex+3;
}
if (d<shadowrange.z) {
lightindex=2;
//lightindex=lightmatindex+2;
}
if (d<shadowrange.y) {
lightindex=1;
//lightindex=lightmatindex+1;
}
if (d<shadowrange.x) {
lightindex=0;
//lightindex=lightmatindex+0;
}
if (lightindex==-1) {
return vec4(1.0,1.0,1.0,1.0);
}
//smcoord = (LightMatrix[lightindex] * vec4(VertexPosition.x,VertexPosition.y,VertexPosition.z,1.0)).xyz;
//smcoord.x += 0.5;
//smcoord.y += 0.5;
//smcoord.x *= 0.5;
//smcoord.y *= 0.5;
mat4 lightmat=mat4(0.0);
if (lightindex==0) {
lightmat=lmat0;
}
//if (lightindex==lightmatindex+1) {
if (lightindex==1) {
lightmat=lmat1;
smcoord.x += 0.5;
}
if (lightindex==2) {
//if (lightindex==lightmatindex+2) {
lightmat=lmat2;
smcoord.y += 0.5;
}
if (lightindex==3) {
//if (lightindex==lightmatindex+3) {
lightmat=lmat3;
smcoord.x += 0.5;
smcoord.y += 0.5;
}
smcoord = (lmat0 * vec4(VertexPosition.x,VertexPosition.y,VertexPosition.z,1.0)).xyz;
smcoord.x += 0.5;
smcoord.y += 0.5;
smcoord.x *= 0.5;
smcoord.y *= 0.5;
smcoord.z -= shadowoffset;
float ps = 1.0 / gl_LightSource[i].quadraticAttenuation;// shadowmap resolution
//=======================================================================
// Shadowmap lookups
//=======================================================================
#ifdef LW_LIGHTQUALITY1
shadowcolor = shadow2D(shadowmap,smcoord);
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y+ps,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y-ps,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y+ps,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y+ps,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y-ps,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y-ps,smcoord.z));
shadowcolor /= 9.0;
#endif
#ifdef LW_LIGHTQUALITY2
shadowcolor = shadow2D(shadowmap,smcoord);
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y+ps,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y-ps,smcoord.z));
shadowcolor /= 5.0;
#endif
#ifdef LW_LIGHTQUALITY3
shadowcolor = shadow2D(shadowmap,smcoord);
#endif
//=======================================================================
//
//=======================================================================
//=======================================================================
//Fade last stage
//=======================================================================
float fade=(shadowrange.w-shadowrange.z)*0.25;
if (d>(shadowrange.w-fade)) {
shadowcolor = clamp( shadowcolor + (d - (shadowrange.w-fade))/fade,0.0,1.0);
}
//=======================================================================
//
//=======================================================================
shadowcolor.a=1.0;
return shadowcolor;
}
vec4 PointShadow( in int i, in int LightMatrixIndex, in sampler2DShadow shadowmap ) {
vec4 shadowcolor;
vec3 worldcoord;
vec3 smcoord = vec3(0.0);
float d;
float shadowMapResolution = gl_LightSource[i].quadraticAttenuation;
worldcoord = ( VertexPosition - LightMatrix[LightMatrixIndex][3].xyz );
int mainaxis;
if (abs(worldcoord.x)>abs(worldcoord.y)) {
if (abs(worldcoord.x)>abs(worldcoord.z)) {
mainaxis = 1;
}
else {
mainaxis = 3;
}
}
else {
if (abs(worldcoord.y)>abs(worldcoord.z)) {
mainaxis = 2;
}
else {
mainaxis = 3;
}
}
if (mainaxis==1) {
if (worldcoord.x>0.0) { mainaxis=4; }
}
if (mainaxis==2) {
if (worldcoord.y>0.0) { mainaxis=5; }
}
if (mainaxis==3) {
if (worldcoord.z>0.0) { mainaxis=6; }
}
float zFar = gl_LightSource[i].linearAttenuation;
float zNear = 0.1;
float a = zFar / ( zFar - zNear );
float b = zFar * zNear / ( zNear - zFar );
float ps = 1.0 / shadowMapResolution;
if (mainaxis==1) {
smcoord.z = -worldcoord.x;
smcoord.x = clamp( (worldcoord.z/smcoord.z*0.5+0.5) / 4.0, ps*0.5, 0.25 - ps*0.5) + 0.00;
smcoord.y = clamp( (worldcoord.y/smcoord.z*0.5+0.5) / 2.0, ps, 0.5 - ps);
}
if (mainaxis==2) {
smcoord.z = -worldcoord.y;
smcoord.x = clamp( (worldcoord.x/smcoord.z*0.5+0.5) / 4.0, ps*0.5, 0.25 - ps*0.5) + 0.00;
smcoord.y = clamp( (worldcoord.z/smcoord.z*0.5+0.5) / 2.0, ps, 0.5 - ps) + 0.50;
}
if (mainaxis==3) {
smcoord.z = -worldcoord.z;
smcoord.x = clamp( (-worldcoord.x/smcoord.z*0.5+0.5) / 4.0, ps*0.5, 0.25 - ps*0.5) + 0.75;
smcoord.y = clamp( (worldcoord.y/smcoord.z*0.5+0.5) / 2.0, ps, 0.5 - ps);
}
if (mainaxis==4) {
smcoord.z = worldcoord.x;
smcoord.x = clamp( (-worldcoord.z/smcoord.z*0.5+0.5) / 4.0, ps*0.5, 0.25 - ps*0.5) + 0.50;
smcoord.y = clamp( (worldcoord.y/smcoord.z*0.5+0.5) / 2.0, ps, 0.5 - ps);
}
if (mainaxis==5) {
smcoord.z = worldcoord.y;
smcoord.x = clamp( (worldcoord.x/smcoord.z*0.5+0.5) / 4.0, ps*0.5, 0.25 - ps*0.5) + 0.25;
smcoord.y = clamp( (-worldcoord.z/smcoord.z*0.5+0.5) / 2.0, ps, 0.5 - ps) + 0.50;
}
if (mainaxis==6) {
smcoord.z = worldcoord.z;
smcoord.x = clamp( (worldcoord.x/smcoord.z*0.5+0.5) / 4.0, ps*0.5, 0.25 - ps*0.5) + 0.25;
smcoord.y = clamp( (worldcoord.y/smcoord.z*0.5+0.5) / 2.0, ps, 0.5 - ps);
}
smcoord.z = a + b / smcoord.z;
shadowcolor = shadow2D(shadowmap,smcoord);
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y+ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y-ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y+ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y+ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y-ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y-ps*0.5,smcoord.z));
shadowcolor /= 9.0;
return shadowcolor;
}
vec4 SpotShadow( in int i, in int LightMatrixIndex, in sampler2DShadow shadowmap ) {
vec4 shadowcolor;
vec3 worldcoord;
vec3 smcoord = vec3(0.0);
float d;
float zFar = gl_LightSource[i].linearAttenuation;
float zNear = 0.1;
float a = zFar / ( zFar - zNear );
float b = zFar * zNear / ( zNear - zFar );
float ps = 1.0 / gl_LightSource[i].quadraticAttenuation; // shadowmap resolution
worldcoord = ( LightMatrix[LightMatrixIndex] * vec4(VertexPosition.xyz,1.0) ).xyz;
smcoord.z = worldcoord.z;
smcoord.x = worldcoord.x/smcoord.z*0.5+0.5;
smcoord.y = worldcoord.y/smcoord.z*0.5+0.5;
smcoord.z = a + b / smcoord.z;
shadowcolor = shadow2D(shadowmap,smcoord);
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y+ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x,smcoord.y-ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y+ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y+ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x-ps,smcoord.y-ps*0.5,smcoord.z));
shadowcolor += shadow2D(shadowmap,vec3(smcoord.x+ps,smcoord.y-ps*0.5,smcoord.z));
return shadowcolor / 9.0;
}
void main(void) {
float alpha = 1.0;
float shininess;
float dirshadowoffset = 0.0;
vec4 AmbientLight = gl_LightSource[0].ambient;
gl_FragColor = gl_Color;
vec4 lightcolor = vec4(0.0,0.0,0.0,1.0);
#ifdef LW_DIFFUSE
vec4 diffuse = texture2D(LW_DIFFUSE,texCoord0);
alpha = diffuse.a;
gl_FragColor *= diffuse;
#endif
vec3 normal;
#ifdef LW_BUMPMAP
vec4 bumpcolor = texture2D(LW_BUMPMAP,texCoord0);
normal = normalize(bumpcolor.xyz - 0.5);
normal = T * normal.x + B * normal.y + N * normal.z;
shininess = bumpcolor.a;
#else
normal = N;
shininess = 0.0;
#endif
vec4 l;
int lightMatrixIndex = 0;
#ifdef LW_DIRECTIONALLIGHT0
l = light( LW_DIRECTIONALLIGHT0, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= DirectionalShadow( LW_DIRECTIONALLIGHT0, LightMatrix[0], LightMatrix[1], LightMatrix[2], LightMatrix[3], LW_DIRECTIONALLIGHTSAMPLER0, dirshadowoffset );
// l *= DirectionalShadow( LW_DIRECTIONALLIGHT0, lightMatrixIndex, LW_DIRECTIONALLIGHTSAMPLER0, dirshadowoffset );
}
lightMatrixIndex += 4;
lightcolor += l;
#endif
#ifdef LW_DIRECTIONALLIGHT1
l = light( LW_DIRECTIONALLIGHT1, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= DirectionalShadow( LW_DIRECTIONALLIGHT1, lightMatrixIndex, LW_DIRECTIONALLIGHTSAMPLER1, dirshadowoffset );
}
lightMatrixIndex += 4;
lightcolor += l;
#endif
#ifdef LW_DIRECTIONALLIGHT2
l = light( LW_DIRECTIONALLIGHT2, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= DirectionalShadow( LW_DIRECTIONALLIGHT2, lightMatrixIndex, LW_DIRECTIONALLIGHTSAMPLER2, dirshadowoffset );
}
lightMatrixIndex += 4;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT0
l = light( LW_POINTLIGHT0, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER0 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT1
l = light( LW_POINTLIGHT1, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l = PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER1 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT2
l = light( LW_POINTLIGHT2, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER2 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT3
l = light( LW_POINTLIGHT3, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER3 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT4
l = light( LW_POINTLIGHT4, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER4 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT5
l = light( LW_POINTLIGHT5, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER5 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT6
l = light( LW_POINTLIGHT6, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER6 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_POINTLIGHT7
l = light( LW_POINTLIGHT7, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= PointShadow( LW_POINTLIGHT0, lightMatrixIndex, LW_POINTLIGHTSAMPLER7 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT0
l = light( LW_SPOTLIGHT0, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT0, lightMatrixIndex, LW_SPOTLIGHTSAMPLER0 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT1
l = light( LW_SPOTLIGHT1, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT1, lightMatrixIndex, LW_SPOTLIGHTSAMPLER1 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT2
l = light( LW_SPOTLIGHT2, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT2, lightMatrixIndex, LW_SPOTLIGHTSAMPLER2 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT3
l = light( LW_SPOTLIGHT3, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT3, lightMatrixIndex, LW_SPOTLIGHTSAMPLER3 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT4
l = light( LW_SPOTLIGHT4, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT4, lightMatrixIndex, LW_SPOTLIGHTSAMPLER4 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT5
l = light( LW_SPOTLIGHT5, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT5, lightMatrixIndex, LW_SPOTLIGHTSAMPLER5 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT6
l = light( LW_SPOTLIGHT6, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT6, lightMatrixIndex, LW_SPOTLIGHTSAMPLER6 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_SPOTLIGHT7
l = light( LW_SPOTLIGHT7, normal, ModelVertex.xyz, shininess );
if (length(l.xyz)>0.003) {
l *= SpotShadow( LW_SPOTLIGHT7, lightMatrixIndex, LW_SPOTLIGHTSAMPLER7 );
}
lightMatrixIndex += 1;
lightcolor += l;
#endif
#ifdef LW_NOLIGHTING
lightcolor=vec4(1.0,1.0,1.0,1.0);
#endif
#ifdef LW_LIGHTMAP
gl_FragColor = gl_FragColor * texture2D(LW_LIGHTMAP,texCoord1) * 2.0;
#endif
vec4 finalcolor;
finalcolor = gl_FragColor * lightcolor;
#ifndef LW_NOLIGHTING
finalcolor += gl_FragColor * AmbientLight;
#endif
gl_FragColor = finalcolor;
#ifdef LW_GLOWMAP
gl_FragColor += texture2D( LW_GLOWMAP ,texCoord0);
#endif
#ifdef LW_CUBEMAP
gl_FragColor = gl_FragColor * alpha + textureCube(texture3,cubeCoord) * (1.0 - alpha);
#endif
#ifndef LW_NOLIGHTING
float fogeffect = clamp( 1.0 - (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale , 0.0, 1.0 ) * gl_Fog.color.w;
gl_FragColor = gl_FragColor * (1.0 - fogeffect) + gl_Fog.color * fogeffect;
#endif
gl_FragColor.a = gl_Color.a * alpha;
}