Jump to content


Lighting+Ambient+Shadowing Converting shader from 3.0 to 4.0


  • You cannot reply to this topic
No replies to this topic

#1 Some0neNewDX10

    New Member

  • Members
  • Pip
  • 1 posts

Posted 23 April 2008 - 01:28 PM

Hi lately i started to learn DX10 and shader model 4.0, and tried to Stencil Shadows shader from 3.0 to 4.0. With few problems i converted ambient light and began convert point light. Here i got many problems. At start i just got black screen of nothingness and now i finally got lighting but it seems something is wrong with it, it's working only only at half of face(mean in one face lighting goes somehow only to one vertex from 2 ;) ) If somebody can help me it would be very appreciated and if somebody could convert shadows too it would be awesome, tx, below is original source


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
Posted Image

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() );






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users