Advertisement

Diffuse Lighting stuck on just 1 side of cube?

Started by May 26, 2015 12:33 PM
1 comment, last by okay_then 9 years, 7 months ago

So im trying to learn HLSL. This is the result

vertex_lighting.jpg

What happend is that the light part on the box is also rotating together with the box. The light should be in fixed position.

This is my shader file


float4x4 world;
float4x4 view;
float4x4 projection;




float4 lightColor;
float3 lightDirection;






float4 ambientColor;






struct VertexShaderOutput
{
float4 Position : POSITION;
float4 Color : COLOR0;
};




struct PixelShaderInput
{
float4 Color: COLOR0;
};






VertexShaderOutput DiffuseLighting(
float3 position : POSITION,
float3 normal : NORMAL)
{


VertexShaderOutput output;


//generate the world-view-proj matrix
float4x4 wvp = mul(mul(world, view), projection);


//transform the input position to the output
output.Position = mul(view, world);


float3 worldNormal = mul(normal, world);




float diffuseIntensity = saturate(dot(-lightDirection, worldNormal));




float4 diffuseColor = lightColor * diffuseIntensity;






output.Color = diffuseColor + ambientColor;
diffuseColor.a = 1.0;




//return the output structure
return output;
}


float4 SimplePixelShader(PixelShaderInput input) : COLOR
{
return input.Color;
}




technique VertexLighting
{


pass P0
{
//set the VertexShader state to the vertex shader function
VertexShader = compile vs_2_0 DiffuseLighting();


//set the PixelShader state to the pixel shader function          
PixelShader = compile ps_2_0 SimplePixelShader();
}
}

and this is just what I did on Draw method



 foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {


                    
                    part.Effect = effect;
                    effect.Parameters["world"].SetValue(world * mesh.ParentBone.Transform);
                    effect.Parameters["view"].SetValue(view);
                    effect.Parameters["projection"].SetValue(projection);
                    effect.Parameters["lightDirection"].SetValue(new Vector3(1, 0, 0));
                    effect.Parameters["lightColor"].SetValue(Color.Green.ToVector4());
                    effect.Parameters["ambientColor"].SetValue(Color.DarkSlateGray.ToVector4());


                }


                for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
                {
                    effect.CurrentTechnique.Passes[i].Apply();
                    mesh.Draw();
                }


              
            }

Anyone ideas whats wrong?

Transform the light direction by view (aka camera) projection. Technically, i think you only want the rotation component, for a directional light.

Also, your position calculation looks funny to me. Shouldn't that be a wvp transform?

Disclaimer - i dont know hlsl, only glsl.
Advertisement



float4x4 world;
float4x4 view;
float4x4 projection;


float4 lightColor;
float3 lightDirection;



float4 ambientColor;



struct VertexShaderOutput
{
    float4 Position : POSITION;
    float4 Color : COLOR0;
};


struct PixelShaderInput
{
    float4 Color: COLOR0;
};



VertexShaderOutput DiffuseLighting(
    float3 position : POSITION,
    float3 normal : NORMAL)
{

    VertexShaderOutput output;

    //generate the world-view-proj matrix
    float4x4 wvp = mul(mul(world, view), projection);

    //transform the input position to the output
    output.Position = mul(float4(position, 1.0), wvp);

    float3 worldNormal = mul(normal, world);


    float diffuseIntensity = saturate(dot(-lightDirection, worldNormal));


    float4 diffuseColor = lightColor * diffuseIntensity;



    output.Color = diffuseColor + ambientColor;
    diffuseColor.a = 1.0;


    //return the output structure
    return output;
}

float4 SimplePixelShader(PixelShaderInput input) : COLOR
{
    return input.Color;
}


technique VertexLighting
{

    pass P0
    {
        //set the VertexShader state to the vertex shader function
        VertexShader = compile vs_2_0 DiffuseLighting();

        //set the PixelShader state to the pixel shader function          
        PixelShader = compile ps_2_0 SimplePixelShader();
    }
}

Here

This topic is closed to new replies.

Advertisement