Sign in to follow this  
Icebone1000

My(?) first transform, light and texture shader

Recommended Posts

Icebone1000    1958
Hey, can someone check my first basic shader..it looks its working ok, but my lights are inverted in some way..I have those lights : g_vLiteDir[0] = XMVectorSet( -0.577f, -0.577f, -0.577f, 1.0f ); g_vLiteDir[1] = XMVectorSet( 0.0f, 0.0f, -1.0f, 1.0f ); But the illuminations comes from the botom and front...? Why are those inverted? heres the shader, I just mixed it from the first fx tutorials from the dx10 sdk

//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------

cbuffer cbChangers{
	matrix mWorld;	//space transformations-WORLD
	float4 vLiteDir[2];//LIGHT DIRECTION
};
cbuffer cbConstants{
	matrix mView; //space transformations-VIEW
	matrix mProjection;//space transformations-PROJECTION
	float4 vLiteColor[2];//LIGHT COLOR
};


//resources
Texture2D tex2D;
SamplerState samLinear{
	Filter = MIN_MAG_MIP_LINEAR;
	AddressU = Wrap;
    AddressV = Wrap;
};




struct VS_INPUT{
	float4 Pos : POSITION;
	float3 Norm : NORMAL;
	float2 Tex : TEXCOORD;
};

struct PS_INPUT{
	float4 Pos : SV_POSITION;
	float3 Norm : NORMAL;
	float2 Tex : TEXCOORD;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input_p )
{
	PS_INPUT vs_output = (PS_INPUT)0;

	vs_output.Pos = mul( input_p.Pos, mWorld );
	vs_output.Pos = mul( vs_output.Pos, mView );
	vs_output.Pos = mul( vs_output.Pos, mProjection );

	vs_output.Norm = mul( input_p.Norm, mWorld );

	vs_output.Tex = input_p.Tex;

	return vs_output;//SV_POSITION + TEXCOORD
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( PS_INPUT input_p ) : SV_TARGET
{

	float4 finalcolor = 0;

	//normal.lite for the 2 lites:(saturate keeps on the range 0-1)
	for( int i = 0; i<2; i++ ){
		finalcolor += saturate( dot( (float3)vLiteDir[i], input_p.Norm ) * vLiteColor[i] );
	}
	finalcolor.a = 1;//alpha

	finalcolor *= tex2D.Sample( samLinear, input_p.Tex );


	return finalcolor;//SV_TARGET
}

//--------------------------------------------------------------------------------------
technique11 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}




Share this post


Link to post
Share on other sites
MJP    19753
When you do N (dot) L, your L vector should point from the surface to the light. If you're providing your shader with the direction your light is facing, you'll need to negate that vector to get the results you expect.

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