Advertisement

Problems with directional light

Started by April 29, 2018 09:20 AM
1 comment, last by DividedByZero 6 years, 9 months ago

Hi guys,

I am having troubles implementing a directional light in HLSL.

I have been following this guide https://www.gamasutra.com/view/feature/131275/implementing_lighting_models_with_.php?page=2 but the quad is rendering pure black no matter where the light vector is pointing.

My quad is in the format of position, texcoord, & normal.

This is the shader so far,


cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
	float4x4 worldmat;
	float4x4 worldviewmat;
	float4x4 worldviewprojmat;
	float4 vecLightDir;
}

struct VS_OUTPUT
{
	float4 Pos : SV_POSITION;
	float3 Light : TEXCOORD0;
	float3 Norm : TEXCOORD1;
};

VS_OUTPUT vs_main(float4 position : POSITION, float2 texcoord : TEXCOORD, float3 normal : NORMAL)
{
	float4 newpos;
	newpos = mul(position, worldmat);
	newpos = mul(newpos, worldviewmat);
	newpos = mul(newpos, worldviewprojmat);
	//	return newpos;

	VS_OUTPUT Out = (VS_OUTPUT)0;
	Out.Pos = newpos;// mul(newpos, matWorldViewProj); // transform Position
	Out.Light = vecLightDir; // output light vector
	Out.Norm = normalize(mul(normal, worldmat)); // transform Normal and normalize it 

	return Out;
}

float4 ps_main(float3 Light: TEXCOORD0, float3 Norm : TEXCOORD1) : SV_TARGET
{
	float4 diffuse = { 1.0f, 0.0f, 0.0f, 1.0f };
	float4 ambient = { 0.1, 0.0, 0.0, 1.0 };


	//return float4(1.0f, 1.0f, 1.0f, 1.0f);
	return ambient + diffuse * saturate(dot(Light, Norm));
}

Any help would be truly appreciated as this is the only real area of DX11 that I really have difficulties in.

Thanks in advance.  :)

I have scrapped this method for another.

This topic is closed to new replies.

Advertisement