HLSL/XNA Ambient light texture mixed up with multi pass lighting

Posted by Manu-EPITA on Stack Overflow See other posts from Stack Overflow or by Manu-EPITA
Published on 2013-10-19T21:43:33Z Indexed on 2013/10/22 21:54 UTC
Read the original article Hit count: 257

Filed under:
|
|
|
|

I've been having some troubles lately with lighting. I have found a source on google which is working pretty good on the example. However, when I try to implement it to my current project, I am getting some very weird bugs. The main one is that my textures are "mixed up" when I only activate the ambient light, which means that a model gets the texture of another one you can see it there.

I am using the same effect for every meshes of my models. I guess this could be the problem, but I don't really know how to "reset" an effect for a new model. Is it possible?

Here is my shader:

float4x4 WVP;

float4x4 WVP;
float3x3 World;

float3 Ke;
float3 Ka;
float3 Kd;
float3 Ks;
float specularPower;

float3 globalAmbient;
float3 lightColor;

float3 eyePosition;
float3 lightDirection;
float3 lightPosition;
float spotPower;

texture2D Texture;
sampler2D texSampler = sampler_state
{
    Texture = <Texture>;
    MinFilter = anisotropic;
    MagFilter = anisotropic;
    MipFilter = linear;
    MaxAnisotropy = 16;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 Texture  : TEXCOORD0;
    float3 Normal   : NORMAL0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 Texture  : TEXCOORD0;
    float3 PositionO: TEXCOORD1;
    float3 Normal   : NORMAL0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    output.Position = mul(input.Position, WVP);

    output.Normal = input.Normal;

    output.PositionO = input.Position.xyz;

    output.Texture = input.Texture;

    return output;
}

float4 PSAmbient(VertexShaderOutput input) : COLOR0
{
    return float4(Ka*globalAmbient + Ke,1) * tex2D(texSampler,input.Texture);
}

float4 PSDirectionalLight(VertexShaderOutput input) : COLOR0
{
    //Difuze
    float3 L = normalize(-lightDirection);
    float diffuseLight = max(dot(input.Normal,L), 0);
    float3 diffuse = Kd*lightColor*diffuseLight;

    //Specular
    float3 V = normalize(eyePosition - input.PositionO);
    float3 H = normalize(L + V);
    float specularLight = pow(max(dot(input.Normal,H),0),specularPower);
    if(diffuseLight<=0) specularLight=0;
    float3 specular = Ks * lightColor * specularLight;

    //sum all light components
    float3 light = diffuse + specular;

    return float4(light,1) * tex2D(texSampler,input.Texture);
}

technique MultiPassLight
{
    pass Ambient
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PSAmbient();
    }
    pass Directional
    {
        PixelShader = compile ps_3_0 PSDirectionalLight();
    }
}

And here is how I actually apply my effects:

public void ApplyLights(ModelMesh mesh, Matrix world, 
    Texture2D modelTexture, Camera camera, Effect effect, 
    GraphicsDevice graphicsDevice)
{
    graphicsDevice.BlendState = BlendState.Opaque;

    effect.CurrentTechnique.Passes["Ambient"].Apply();

    foreach (ModelMeshPart part in mesh.MeshParts)
    {
        graphicsDevice.SetVertexBuffer(part.VertexBuffer);
        graphicsDevice.Indices = part.IndexBuffer;

        // Texturing
        graphicsDevice.BlendState = BlendState.AlphaBlend;
        if (modelTexture != null)
        {
            effect.Parameters["Texture"].SetValue(
                modelTexture
            );
        }

        graphicsDevice.DrawIndexedPrimitives(
            PrimitiveType.TriangleList,
            part.VertexOffset,
            0,
            part.NumVertices,
            part.StartIndex,
            part.PrimitiveCount
        );

        // Applying our shader to all the mesh parts
        effect.Parameters["WVP"].SetValue(
            world *
            camera.View *
            camera.Projection
        );
        effect.Parameters["World"].SetValue(world);
        effect.Parameters["eyePosition"].SetValue(
            camera.Position
        );


        graphicsDevice.BlendState = BlendState.Additive;

        // Drawing lights
        foreach (DirectionalLight light in DirectionalLights)
        {
            effect.Parameters["lightColor"].SetValue(light.Color.ToVector3());
            effect.Parameters["lightDirection"].SetValue(light.Direction);

            // Applying changes and drawing them
            effect.CurrentTechnique.Passes["Directional"].Apply();
            graphicsDevice.DrawIndexedPrimitives(
                PrimitiveType.TriangleList, 
                part.VertexOffset, 
                0, 
                part.NumVertices, 
                part.StartIndex, 
                part.PrimitiveCount
            ); 
        }
    }

I am also applying this when loading the effect:

effect.Parameters["lightColor"].SetValue(Color.White.ToVector3());
effect.Parameters["globalAmbient"].SetValue(Color.White.ToVector3());
effect.Parameters["Ke"].SetValue(0.0f);
effect.Parameters["Ka"].SetValue(0.01f);
effect.Parameters["Kd"].SetValue(1.0f);
effect.Parameters["Ks"].SetValue(0.3f);
effect.Parameters["specularPower"].SetValue(100);

Thank you very much

UPDATE: I tried to load an effect for each model when drawing, but it doesn't seem to have changed anything. I suppose it is because XNA detects that the effect has already been loaded before and doesn't want to load a new one. Any idea why?

© Stack Overflow or respective owner

Related posts about c#

Related posts about models