XNA Creators Club Online
Page 1 of 1 (1 items)
Sort Posts: Previous Next

Problem about PCSS shadow effect

Last post 08-19-2008 3:19 PM by nova cat. 0 replies.
  • 08-19-2008 3:19 PM

    Problem about PCSS shadow effect

    Hi all,

    I'm a newbie here and seeking some help on how to create shadow map effect properly.
    i've just integrated the shadow effect into my game, which is found in a shadow-mapping sample on msdn.
    Currently I'm trying the PCSS-Shadow effect (shadow_pscc.fx, downloaded from the nvidia offical site).I tried to replace the original shadow effect with the new one but failed. The position of the objects seems fine, but their shadows are not moving with them. Instead, they are fixed to the perspective of the light source. Probably my usage of shader techniques are totally wrong...

    the critical parameters of the effect are these lines:

     
    // transform object vertices to world-space:  
    float4x4 gWorldXf : World < string UIWidget="None"; >;  
    // transform object normals, tangents, & binormals to world-space:  
    float4x4 gWorldITXf : WorldInverseTranspose < string UIWidget="None"; >;  
    // transform object vertices to view space and project them in perspective:  
    float4x4 gWvpXf : WorldViewProjection < string UIWidget="None"; >;  
    // provide tranform from "view" or "eye" coords back to world-space:  
    float4x4 gViewIXf : ViewInverse < string UIWidget="None"; >;  
     
    texture gShadMap : RENDERCOLORTARGET <  
       float2 Dimensions = { SHADOW_SIZE, SHADOW_SIZE };  
       string Format = (SHADOW_FMT) ;  
       string UIWidget = "None";  
    >;  
    sampler2D gShadSampler = sampler_state {  
        texture = <gShadMap>;  
        AddressU = Clamp;  
        AddressV = Clamp;
    #if DIRECT3D_VERSION >= 0xa00  
        Filter = MIN_MAG_MIP_POINT;
    #else /* DIRECT3D_VERSION < 0xa00 */  
        MinFilter = Point;  
        MagFilter = Point;  
        MipFilter = Point;
    #endif /* DIRECT3D_VERSION */  
    };  
     
    texture gShadDepthTarget : RENDERDEPTHSTENCILTARGET <  
       float2 Dimensions = { SHADOW_SIZE, SHADOW_SIZE };  
       string format = "D24S8";  
       string UIWidget = "None";  
    >;  
     


    and the technique part is like this...

    technique Main <  
           string Script = "Pass=MakeShadow;" 
                   "Pass=UseShadow;";  
    > {  
           pass MakeShadow <  
                   string Script = "RenderColorTarget0=gShadMap;" 
                    "RenderDepthStencilTarget=gShadDepthTarget;" 
                    "RenderPort=SpotLight0;" 
                    "ClearSetColor=gShadowClearColor;" 
                    "ClearSetDepth=gClearDepth;" 
                    "Clear=Color;" 
                    "Clear=Depth;" 
                    "Draw=geometry;";  
           > {...}  
           pass UseShadow <  
                   string Script = "RenderColorTarget0=;" 
                       "RenderDepthStencilTarget=;" 
                       "RenderPort=;" 
                       "ClearSetColor=gClearColor;" 
                       "ClearSetDepth=gClearDepth;" 
                       "Clear=Color;" 
                       "Clear=Depth;" 
                       "Draw=geometry;";  
           > {...}  

     

    And here is my code fragments to draw my scene...

        protected override void Draw(GameTime gameTime)  
        {  
            map = CreateShadowMap(); //map is a texture2D object  
     
            MyEffect.Parameters["gShadMap"].SetValue(map);  
     
            GraphicsDevice.Clear(Color.CornflowerBlue);  
     
            //suppose the "Main" technique is for rendering shadows to the scene...  
            DrawScene(MyEffect.Techniques["Main"]);  
     
            base.Draw(gameTime);  
        }  
     
        private Texture2D CreateShadowMap()  
        {  
            GraphicsDevice.RenderState.DepthBufferFunction = CompareFunction.LessEqual;  
            GraphicsDevice.SetRenderTarget(0, shadowRenderTarget);  
            DepthStencilBuffer old = GraphicsDevice.DepthStencilBuffer;  
            GraphicsDevice.DepthStencilBuffer = shadowDepthBuffer;  
            GraphicsDevice.Clear(Color.Black);  
              
            //confused... shall I use the same technique here to generate shadow map?  
            DrawScene(MyEffect.Techniques["Main"]);  
     
            GraphicsDevice.SetRenderTarget(0, null);  
            GraphicsDevice.DepthStencilBuffer = old;  
            return shadowRenderTarget.GetTexture();  
        }  
     
        private void DrawScene(EffectTechnique technique)  
        {  
            //I guess the parameters are okay...  
            MyEffect.Parameters["gWorldXf"].SetValue(terrainWorld);  
            MyEffect.Parameters["gWvpXf"].SetValue(terrainWorld * cameraView * cameraProjection);  
            MyEffect.Parameters["gWorldITXf"].SetValue(Matrix.Transpose(Matrix.Invert(terrainWorld)));  
            MyEffect.CommitChanges();  
     
            //routines here...  
            foreach (ModelMesh mesh in terrain.Meshes)  
            {  
                foreach (Effect effect in mesh.Effects)  
                {  
                    effect.CurrentTechnique = technique;  
                    mesh.Draw();  
                }  
            }  
        } 

    Not very likely that I've set the matrix parameters wrong. And I guess the problem is that i shouldn't have used the "Main" technique twice, both making the shadow map and rendering the shadows. The two techniques are usually seperately defined. But this time I can't find any other techniques in the effect file. Any ideas?
    Or could you please show me some samples of PCSS effect for XNA?

    Many thanks!

Page 1 of 1 (1 items) Previous Next