I am experimenting with the shader exported with my scene. Specifically the ability to render spotlights on the pixel level.
I have two spotlights on a vehicle.
Here is the problem. The spotlights illuminate the scene as expected when the vehicle is facing one direction.
However, when the car is turned around 180 degrees, spotlights don't show.
The spotlights gradually strengthen and weaken as the car circles around.
Maybe somebody with shader expertise can explain this, because shaders are a complete mystery to me. My guess is that it's probably something really simple. I just can see it.
Thanks for the help.
Here is the ".program code that was exported from 3ds max via easy ogre exporter...
Code: Select all
vertex_program exo_explorer_pb_terrain_alpine_valley_1vsAmbGEN0 cg
{
source pb_terrain_alpine_valley_1.cg
profiles vs_1_1 arbvp1
entry_point exo_explorer_pb_terrain_alpine_valley_1vsAmbGEN0
default_params
{
param_named_auto wvpMat worldviewproj_matrix
}
}
fragment_program exo_explorer_pb_terrain_alpine_valley_1fpAmbGENDIFF0 cg
{
source pb_terrain_alpine_valley_1.cg
profiles ps_2_x arbfp1
entry_point exo_explorer_pb_terrain_alpine_valley_1fpAmbGENDIFF0
default_params
{
param_named_auto ambient ambient_light_colour
param_named_auto matAmb surface_ambient_colour
param_named_auto matEmissive surface_emissive_colour
}
}
vertex_program exo_explorer_pb_terrain_alpine_valley_1vsLightGENNORM01 cg
{
source pb_terrain_alpine_valley_1.cg
profiles vs_1_1 arbvp1
entry_point exo_explorer_pb_terrain_alpine_valley_1vsLightGENNORM01
default_params
{
param_named_auto wMat world_matrix
param_named_auto wvpMat worldviewproj_matrix
param_named_auto spotlightDir light_direction_object_space 0
}
}
fragment_program exo_explorer_pb_terrain_alpine_valley_1fpLightGENDIFF0NORM1 cg
{
source pb_terrain_alpine_valley_1.cg
profiles ps_2_x arbfp1
entry_point exo_explorer_pb_terrain_alpine_valley_1fpLightGENDIFF0NORM1
default_params
{
param_named_auto lightDif0 light_diffuse_colour 0
param_named_auto lightSpec0 light_specular_colour 0
param_named_auto camPos camera_position
param_named_auto matShininess surface_shininess
param_named_auto matDif surface_diffuse_colour
param_named_auto matSpec surface_specular_colour
param_named_auto lightPos0 light_position 0
param_named_auto lightAtt0 light_attenuation 0
param_named_auto spotlightParams spotlight_params 0
param_named_auto iTWMat inverse_transpose_world_matrix
param_named normalMul float 1
}
}
Code: Select all
void exo_explorer_pb_terrain_alpine_valley_1vsAmbGEN0(float4 position : POSITION,
float3 normal : NORMAL,
float2 uv0 : TEXCOORD0,
out float4 oPos: POSITION,
out float2 oUv0 : TEXCOORD0,
uniform float4x4 wvpMat
)
{
oPos = mul(wvpMat, position);
oUv0 = uv0;
}
float4 exo_explorer_pb_terrain_alpine_valley_1fpAmbGENDIFF0(float4 position : POSITION,
float2 uv0 : TEXCOORD0,
uniform float3 ambient,
uniform float4 matAmb,
uniform float4 matEmissive,
uniform sampler2D diffuseMap : register(s0)): COLOR0
{
float4 diffuseTex = tex2D(diffuseMap, uv0);
float4 retColor = max(matEmissive, float4(ambient, 1) * matAmb);
retColor *= diffuseTex;
return retColor;
}
void exo_explorer_pb_terrain_alpine_valley_1vsLightGENNORM01(float4 position : POSITION,
float3 normal : NORMAL,
float3 tangent : TANGENT,
float2 uv0 : TEXCOORD0,
float2 uv1 : TEXCOORD1,
out float4 oPos : POSITION,
out float3 oNorm : TEXCOORD0,
out float3 oTang : TEXCOORD1,
out float3 oBinormal : TEXCOORD2,
out float3 oSpDir : TEXCOORD3,
out float4 oWp : TEXCOORD4,
out float2 oUv0 : TEXCOORD5,
out float2 oUv1 : TEXCOORD6,
uniform float4x4 wMat,
uniform float4x4 wvpMat,
uniform float4 spotlightDir)
{
oWp = mul(wMat, position);
oPos = mul(wvpMat, position);
oUv0 = uv0;
oUv1 = uv1;
oTang = tangent;
oBinormal = cross(tangent, normal);
oNorm = normal;
oSpDir = mul(wMat, spotlightDir).xyz;
}
float4 exo_explorer_pb_terrain_alpine_valley_1fpLightGENDIFF0NORM1(float4 position : POSITION,
float3 norm : TEXCOORD0,
float3 tangent : TEXCOORD1,
float3 binormal : TEXCOORD2,
float3 spDir : TEXCOORD3,
float4 wp : TEXCOORD4,
float2 uv0 : TEXCOORD5,
float2 uv1 : TEXCOORD6,
uniform float3 lightDif0,
uniform float4 lightPos0,
uniform float4 lightAtt0,
uniform float3 lightSpec0,
uniform float4 matDif,
uniform float4 matSpec,
uniform float matShininess,
uniform float3 camPos,
uniform float4 invSMSize,
uniform float4 spotlightParams,
uniform float4x4 iTWMat,
uniform float normalMul,
uniform sampler2D diffuseMap : register(s0),
uniform sampler2D normalMap : register(s1)): COLOR0
{
float3 ld0 = normalize(lightPos0.xyz - (lightPos0.w * wp.xyz));
// attenuation
half lightDist = length(lightPos0.xyz - wp.xyz) / (lightAtt0.r / lightAtt0.r);
half la = 1;
if(lightAtt0.a > 0.0)
{
half ila = lightDist * lightDist; // quadratic falloff
la = 1.0 / (lightAtt0.g + lightAtt0.b * lightDist + lightAtt0.a * ila);
}
float3 normalTex = tex2D(normalMap, uv1);
tangent *= normalMul;
binormal *= normalMul;
float3x3 tbn = float3x3(tangent, binormal, norm);
float3 normal = mul(transpose(tbn), (normalTex.xyz -0.5) * 2); // to object space
normal = normalize(mul((float3x3)iTWMat, normal));
float3 diffuse = max(dot(normal, ld0), 0);
// calculate the spotlight effect
float spot = (spotlightParams.x == 1 && spotlightParams.y == 0 && spotlightParams.z == 0 && spotlightParams.w == 1 ? 1 : // if so, then it's not a spot light
saturate((dot(normalize(-spDir), ld0) - spotlightParams.y) / (spotlightParams.x - spotlightParams.y)));
float3 camDir = normalize(camPos - wp.xyz);
float3 halfVec = normalize(ld0 + camDir);
float3 specular = pow(max(dot(normal, halfVec), 0), matShininess);
float4 diffuseTex = tex2D(diffuseMap, uv0);
float3 diffuseContrib = diffuse * lightDif0 * matDif.rgb;
diffuseContrib *= diffuseTex.rgb;
float3 specularContrib = specular * lightSpec0 * matSpec.rgb;
float3 light0C = (diffuseContrib + specularContrib) * la * spot;
float alpha = matDif.a;
alpha *= diffuseTex.a;
return float4(light0C, alpha);
}