float4x4 WorldViewProjection; // World * View * Projection matrix
float4x4 WorldView; // World * View
float4x4 Proj; // Projection matrix
float4 Ambient; // Ambient light color
float4 ShadowColor; // Shadow volume color (for visualization)???not working
float4 LightColor; // Color of the light
float4 MatColor; // Color of the material
float4 LightView; // Position of light in view space
float FarClip=100.0f; // Z of far clip plane
texture Texture; // Texture for scene rendering
float LIGHT_FALLOFF = 6.0f;
sampler textureSampler = sampler_state
{
Texture = <Texture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
//////////////////////////////////////////////////////////////
//Structures
//////////////////////////////////////////////////////////////
struct AmbientA2V
{
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
};
struct AmbientV2P
{
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
};
struct PointLightA2V
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord0 : TEXCOORD0;
};
struct PointLightV2P
{
float4 Position : POSITION;
float3 LightDir : TEXCOORD0;
float3 ViewNormal : TEXCOORD1;
float2 TexCoord0 : TEXCOORD2;
float4 Diffuse : TEXCOORD3;
};
struct ShadowVolumeA2V
{
float4 Position : POSITION;
float3 Normal : NORMAL;
};
struct ShadowVolumeV2P
{
float4 Position : POSITION;
};
//////////////////////////////////////////////////////////////
//Vertex Shaders
//////////////////////////////////////////////////////////////
void AmbientVS( in AmbientA2V IN, out AmbientV2P OUT )
{
//Transform the position from object space to homogeneous projection space
OUT.Position = mul( IN.Position, WorldViewProjection );
//Copy the texture coordinate through
OUT.TexCoord0 = IN.TexCoord0;
}
void PointLightVS( in PointLightA2V IN, out PointLightV2P OUT )
{
// Transform the position from view space to homogeneous projection space
OUT.Position = mul( IN.Position, WorldViewProjection );
// Compute view space position
OUT.LightDir = LightView - mul( IN.Position, WorldView );
// Compute world space normal
OUT.ViewNormal = normalize( mul( IN.Normal, (float3x3)WorldView ) );
// Modulate material with light to obtain diffuse
OUT.Diffuse = MatColor * LightColor;
// Copy the texture coordinate through
OUT.TexCoord0 = IN.TexCoord0;
}
void ShadowVolumeVS( in ShadowVolumeA2V IN, out ShadowVolumeV2P OUT )
{
//Compute view space normal
IN.Normal = mul( IN.Normal, (float3x3)WorldView );
//Obtain view space position
float4 PosView = mul( IN.Position, WorldView );
//Light-to-vertex vector in view space
float3 LightVecView = PosView - LightView;
//Extrude the vertex away from light if it's facing away from the light.
if( dot( IN.Normal, -LightVecView ) < 0.0f )
{
if( PosView.z > LightView.z )
PosView.xyz += LightVecView * ( FarClip - PosView.z ) / LightVecView.z;
else
PosView = float4( LightVecView, 0.0f );
//Transform the position from view space to homogeneous projection space
OUT.Position = mul( PosView, Proj );
} else
OUT.Position = mul( IN.Position, WorldViewProjection );
}
//////////////////////////////////////////////////////////////
//Pixel Shaders
//////////////////////////////////////////////////////////////
float4 AmbientPS( in AmbientV2P IN ) : COLOR0
{
// Lookup mesh texture and modulate it with material and ambient amount
return Ambient * tex2D( textureSampler, IN.TexCoord0 );
}
float4 PointLightPS( in PointLightV2P IN ) : COLOR0
{
// Pixel to light vector
float LenSq = dot( IN.LightDir, IN.LightDir );
IN.LightDir = normalize( IN.LightDir );
float Attn = min(( LIGHT_FALLOFF * LIGHT_FALLOFF ) / LenSq, 1.0f);
// Compute lighting amount
float4 I = saturate( dot( normalize( IN.ViewNormal ), IN.LightDir ) ) * IN.Diffuse * Attn;
// Lookup mesh texture and modulate it with diffuse
return float4( tex2D( textureSampler, IN.TexCoord0 ).xyz, 1.0f ) * I;
}
float4 ShadowVolumePS() : COLOR0
{
return float4(0.1f,0.1f,0.1f,0.9f);
}
//////////////////////////////////////////////////////////////
//Techniques
//////////////////////////////////////////////////////////////
technique AmbientL
{
pass P0
{
VertexShader = compile vs_3_0 AmbientVS();
PixelShader = compile ps_3_0 AmbientPS();
StencilEnable = false;
ZFunc = LessEqual;
}
}
technique PointLight
{
pass P0
{
VertexShader = compile vs_3_0 PointLightVS();
PixelShader = compile ps_3_0 PointLightPS();
ZEnable = true;
ZFunc = LessEqual;
StencilEnable = true;
AlphaBlendEnable = true;
BlendOp = Add;
SrcBlend = One;
DestBlend = One;
StencilRef = 1;
StencilFunc = Greater;
StencilPass = Keep;
}
}
technique ShadowVolume2Sided
{
pass P0
{
VertexShader = compile vs_3_0 ShadowVolumeVS();
PixelShader = compile ps_3_0 ShadowVolumePS();
CullMode = None;
// Disable writing to the frame buffer
AlphaBlendEnable = true;
SrcBlend = Zero;
DestBlend = One;
// Disable writing to depth buffer
ZWriteEnable = false;
ZFunc = Less;
// Setup stencil states
TwoSidedStencilMode = true;
StencilEnable = true;
StencilRef = 1;
StencilMask = 0xFFFFFFFF;
StencilWriteMask = 0xFFFFFFFF;
Ccw_StencilFunc = Always;
Ccw_StencilZFail = Incr;
Ccw_StencilPass = Keep;
StencilFunc = Always;
StencilZFail = Decr;
StencilPass = Keep;
}
}
And my working wierd code
//Sending matrixes
float4x4 WorldViewProj;
float4x4 WorldView;
float4x4 Proj;
//ambient lighting
float4 Ambient=float4(0.1f,0.1f,0.1f,1.0f);
Texture2D Tex;//our texture
//point lighting
float4 LightColor=float4(1.0f,1.0f,1.0f,1.0f); // Color of the light
float4 MatColor=float4(1.0f,1.0f,1.0f,1.0f); // Color of the material
float4 LightView; // Position of light in view space
float FarClip=100.0f; // Z of far clip plane
float LIGHT_FALLOFF = 60.0f;
//tex sampler
SamplerState TexSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
//ambient lighting functions and structs
struct AmbientA2V
{
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD;
};
struct AmbientV2P
{
float4 Position : SV_POSITION;
float2 TexCoord0 : TEXCOORD0;
};
void AmbientVS( in AmbientA2V IN,out AmbientV2P OUT )
{
OUT.Position = mul( IN.Position, WorldViewProj);
OUT.TexCoord0 = IN.TexCoord0;
}
float4 AmbientPS( in AmbientV2P IN ) : SV_Target
{
return Ambient*Tex.Sample(TexSampler,IN.TexCoord0);
}
//point lighting functions and structs
struct PointLightA2V
{
float4 Position : POSITION;
float4 Normal : NORMAL;
float2 TexCoord0 : TEXCOORD;
};
struct PointLightV2P
{
float4 Position : SV_POSITION;
float3 LightDir: TEXCOORD0;
float3 ViewNormal : TEXCOORD1;
float2 TexCoord0 : TEXCOORD2;
float4 Diffuse : TEXCOORD3;
};
void PointLightVS( in PointLightA2V IN, out PointLightV2P OUT )
{
OUT.Position = mul( IN.Position, WorldViewProj );
OUT.LightDir = LightView - mul( IN.Position, WorldView );
OUT.ViewNormal = normalize( mul( IN.Normal, (float3x3)WorldView ) );
OUT.Diffuse = LightColor;
OUT.TexCoord0 = IN.TexCoord0;
}
float4 PointLightPS( in PointLightV2P IN ) : SV_Target
{
float LenSq = dot( IN.LightDir, IN.LightDir );
IN.LightDir = normalize( IN.LightDir );
float Attn = min(( LIGHT_FALLOFF * LIGHT_FALLOFF ) / LenSq, 1.0f);
float4 I = saturate( dot( normalize( IN.ViewNormal ), IN.LightDir ) ) * IN.Diffuse * Attn;
return float4(Tex.Sample(TexSampler,IN.TexCoord0).xyz,1.0f)*I;
}
DepthStencilState AmbientStencil
{
StencilEnable = FALSE;
DepthFunc = LESS;
};
BlendState PointLBlending
{
BlendEnable[0] = TRUE;
BlendOp = ADD;
SrcBlendAlpha = ONE;
DestBlendAlpha = ONE;
};
DepthStencilState PointLStencil
{
DepthEnable = TRUE;
DepthFunc = LESS;
StencilEnable = TRUE;
FrontFaceStencilFunc=GREATER;
BackFaceStencilFunc=GREATER;
FrontFaceStencilPass=KEEP;
BackFaceStencilPass=KEEP;
};
technique10 AmbientLight
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, AmbientVS() ) );
SetGeometryShader(NULL);
SetPixelShader( CompileShader( ps_4_0, AmbientPS() ) );
SetDepthStencilState( AmbientStencil, 0 );
}
}
technique10 PointLight
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, PointLightVS() ) );
SetGeometryShader(NULL);
SetPixelShader( CompileShader( ps_4_0, PointLightPS() ) );
SetDepthStencilState( PointLStencil, 0 );
SetBlendState( PointLBlending, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );//0xFFFFFFFF
}
}
Sorry if that too much, i just can understand what the hell is wrong with it.p.s. Sorry for my english
and here is the screenshot

p.s.s.position of light setting like this(got it from original source)
D3DXVECTOR4 LightPos=D3DXVECTOR4(Sphere1->GetPos().x,10,Sphere1->GetPos().z,1); D3DXVec4Transform( &LightPos, &LightPos, &cam->GetView() );











