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

Wierd flashing when rendering simple diffuse lighting

Last post 6/6/2009 11:39 PM by Jman. 3 replies.
  • 6/5/2009 5:42 PM

    Wierd flashing when rendering simple diffuse lighting

    I have been trying to get a very simple pixel shader going that just applies diffuse lighting ( N dot L ) for a single directional light source. My pixel shader code is as follows:

    cbuffer cb0 : register(b0)
    {
       float4 vCameraPos;
       float4 vGlobalAmbient;
    };

    cbuffer cb1 : register(b1)
    {
       float4 vAmbient;
       float4 vDiffuse;
       float4 vSpecular;
       int bDiffuseMap;
       int bGlossMap;
       int bNormalMap;
       int bBumpMap;
    };

    cbuffer cb2 : register(b2)
    {
       float3 vLightColor;
       float3 vLightDir;
       int nLightType;
       float SomeOtherFloat;
    };

    struct PS_INPUT
    {
       float4 vPosProj : SV_POSITION;
       float2 vTexUV   : TEXCOORD0;
       float3 vNormWorld : TEXCOORD1;
       float3 vPosWorld  : TEXCOORD2;
    };

    // entry point
    float4 main( PS_INPUT vert ) : SV_Target
    {
       float4 color;
       color.a = vAmbient.a;

       float3 lightVec = normalize( vLightDir );
       float3 vectorToLight = normalize( vert.vPosWorld - lightVec );
       float fDiffuse = saturate( dot( vert.vNormWorld, vectorToLight ) );

       color.rgb = vLightColor * vAmbient.rgb;
       color.rgb += vDiffuse.rgb * fDiffuse * vLightColor;

       return color;
    }

    When I run this shader in my engine, the model it is rendering flashes between ambient-only and diffuse-lit very rapidly. I have determined that the problem is in the shader code, not my engine, and it is the statement that computes the fDiffuse variable. When I omit that line, and substitute a constant value for fDiffuse, the flashing goes away, but I am left with plain old ambient lighting.

    Has anyone else seen this happening? Am I doing something wrong? plz help!
  • 6/5/2009 6:02 PM In reply to

    Re: Wierd flashing when rendering simple diffuse lighting

    Check that you properly set the VertexDeclaration on the GraphicsDevice
    Regards,
    Louis Ingenthron
    Fortis Venaliter
    Lead Developer of FV ProductionsFV Productions
  • 6/5/2009 6:14 PM In reply to

    Re: Wierd flashing when rendering simple diffuse lighting

    I'm writing the whole thing in C++, using the Direct3D10 SDK. I'm not using the XNA Game Studio.
  • 6/6/2009 11:39 PM In reply to

    Re: Wierd flashing when rendering simple diffuse lighting

    I figured it out. I wasn't following the packing rules for some of my constant buffers.
Page 1 of 1 (4 items) Previous Next