Sign in to follow this  
Sixten

ShadowMap Problem

Recommended Posts

HIya all.. Im working on a 3DEngine.. and im working with Pixel Shader now to create lighting and shadows.. I am using abit of code from the SDK Sample.. Im allso using the FX file from that Sample to have something to build on.. Now when i run my game.. the lighting is Black and when i walk towards the light it turns away from the camera.. Im using a camera object for the lighting view and one for the actual view of the screen.. it all work greate.. it renders the spot light but its black.. Have i got it all wrong ?? That this dose only render the shadows and not the lighting ?????

    //First pass render the shadows

    D3DXMATRIXA16 mLightView;
    mLightView = *Light.GetViewMatrix();
    g_pEffect->SetMatrix( "g_mProj", &g_mShadowProj);
	// Freely moveable light. Get light parameter
    // from the light camera.
    D3DXVECTOR3 v = *Light.GetEyePt();
    D3DXVECTOR4 v4;
    D3DXVec3Transform( &v4, &v, &mLightView );
    g_pEffect->SetVector( "g_vLightPos", &v4 );
    *(D3DXVECTOR3*)&v4 = *Light.GetWorldAhead();
    v4.w = 0.0f;  // Set w 0 so that the translation part doesn't come to play
    D3DXVec4Transform( &v4, &v4, &mLightView);  // Direction in view space
    D3DXVec3Normalize( (D3DXVECTOR3*)&v4, (D3DXVECTOR3*)&v4 );
    g_pEffect->SetVector( "g_vLightDir", &v4 );

    Mesh.Render(g_pEffect, true, &mLightView);

    //Secound Pass Render Objects

    const D3DXMATRIX *pmView = Camera1.GetViewMatrix();
    g_pEffect->SetTexture( "g_txShadow", g_pShadowMap );

    D3DXMATRIXA16 mViewToLightProj;
    mViewToLightProj = *pmView;
    D3DXMatrixInverse( &mViewToLightProj, NULL, &mViewToLightProj );
    D3DXMatrixMultiply( &mViewToLightProj, &mViewToLightProj, &mLightView );
    D3DXMatrixMultiply( &mViewToLightProj, &mViewToLightProj, &g_mShadowProj );
    g_pEffect->SetMatrix( "g_mViewToLightProj", &mViewToLightProj );

    g_pEffect->SetMatrix( "g_mProj", Camera1.GetProjMatrix());
    v = *Light.GetEyePt();
    D3DXVECTOR4 v42;
    D3DXVec3Transform( &v42, &v, pmView );
    g_pEffect->SetVector( "g_vLightPos", &v42 );
    *(D3DXVECTOR3*)&v42 = *Light.GetWorldAhead();
    v4.w = 0.0f;  // Set w 0 so that the translation part doesn't come to play
    D3DXVec4Transform( &v42, &v42, pmView );  // Direction in view space
    D3DXVec3Normalize( (D3DXVECTOR3*)&v42, (D3DXVECTOR3*)&v42 );
    g_pEffect->SetVector( "g_vLightDir", &v42 );

    Mesh.Render(g_pEffect, false, pmView);

The Mesh.Render
VOID Mesh::Render(ID3DXEffect* g_pEffect, bool bRenderShadow, const D3DXMATRIX *pmView)

    if( bRenderShadow )
        g_pEffect->SetTechnique( "RenderShadow" );

	if( !bRenderShadow )
            g_pEffect->SetTechnique( "RenderScene" );

	D3DXMATRIXA16 mWorldView = Meshes.WorldMatrix;
    D3DXMatrixMultiply( &mWorldView, &mWorldView, pmView );
    g_pEffect->SetMatrix( "g_mWorldView", &mWorldView );

	UINT cPass;
    g_pEffect->Begin( &cPass, 0 );
    for( UINT p = 0; p < cPass; ++p )
    {
		g_pEffect->BeginPass( p );
		for( DWORD i=0; i<Meshes.NumMaterials; i++ )
		{
			//D3DDevice->SetMaterial( &Meshes.Materials[i] );
			//D3DDevice->SetTexture( 0, Meshes.Textures[i] );

			//Meshes.Mesh->DrawSubset( i );

			D3DXVECTOR4 vDif( Meshes.Materials[i].Diffuse.r,
                              Meshes.Materials[i].Diffuse.g,
                              Meshes.Materials[i].Diffuse.b,
                              Meshes.Materials[i].Diffuse.a );
           g_pEffect->SetVector( "g_vMaterial", &vDif );
		   g_pEffect->SetTexture( "g_txScene", Meshes.Textures[i] );
           g_pEffect->CommitChanges();
           Meshes.Mesh->DrawSubset( i );
		}
		g_pEffect->EndPass();
	}
	g_pEffect->End();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this