Hello all,
I've been trying to get some basic lighting to work with my application. So I've have had limited success due to my lack of knowledge of HLSL and thought I would bring my shader here
The goal was to get basic diffuse and ambient light working with a simple Quad
these structures sit at the very top of the Quad.cpp file right after the PreProcessor directives
struct Light
{
XMFLOAT3 dir;
XMFLOAT4 ambient;
XMFLOAT4 diffuse;
float pad;
};
Light light;
struct cbPerFrame
{
Light light;
};
cbPerFrame constBuffer;
the code for the lighting shader is as follows:
Texture2D colorMap : register( t0 );
SamplerState colorSampler : register( s0 );
cbuffer cbChangesEveryFrame : register( b0 )
{
matrix worldMatrix;
};
cbuffer cbNeverChanges : register( b1 )
{
matrix viewMatrix;
};
cbuffer cbChangeOnResize : register( b2 )
{
matrix projMatrix;
};
struct Light
{
float3 dir;
float4 ambient;
float4 diffuse;
};
cbuffer cbPerFrame
{
Light light;
};
struct VS_Input
{
float4 pos : POSITION;
float2 tex0 : TEXCOORD0;
float3 norm : NORMAL;
};
struct PS_Input
{
float4 pos : SV_POSITION;
float2 tex0 : TEXCOORD0;
float3 norm : NORMAL;
};
PS_Input VS_Main( VS_Input vertex )
{
PS_Input vsOut = ( PS_Input )0;
vsOut.pos = mul( vertex.pos, worldMatrix );
vsOut.pos = mul( vsOut.pos, viewMatrix );
vsOut.pos = mul( vsOut.pos, projMatrix );
vsOut.norm = mul( vertex.norm, worldMatrix);
vsOut.tex0 = vertex.tex0;
vsOut.norm = normalize(vsOut.norm);
return vsOut;
}
float4 PS_Main( PS_Input input ) : SV_TARGET
{
input.norm = normalize(input.norm);
float4 textureColor = colorMap.Sample(colorSampler, input.tex0);
float lightIntensity = saturate(dot(input.norm, light.dir));
float4 color = saturate(light.diffuse * lightIntensity);
color = color * textureColor;
return color;
}
One of the Constant Buffers as you see is cbPerFrame which contains the Light Structure member
This is my input Layout Description, its called after the shaders have been compiled in Quad.cpp.
D3D11_INPUT_ELEMENT_DESC inputE[] =
{
{"POSITION", NULL, DXGI_FORMAT_R32G32B32_FLOAT, NULL, NULL, D3D11_INPUT_PER_VERTEX_DATA, NULL},
{"TEXCOORD", NULL, DXGI_FORMAT_R32G32_FLOAT, NULL, 12, D3D11_INPUT_PER_VERTEX_DATA, NULL},
{"NORMAL", NULL, DXGI_FORMAT_R32G32B32_FLOAT, NULL, 20, D3D11_INPUT_PER_VERTEX_DATA, NULL},
};
This code Is called after the shaders have been compiled during runtime and the inputLayout is filled out
/*light.dir = XMFLOAT3(0.0f, 0.0f, 1.0f);
light.ambient = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
light.diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
*/
This is the code in my Render Function thats suppose to pass the light structure that I filled out after initalizing the shaders to the constBuffer.
I pass to the constant buffer inside the .fx file with the updateSubresource and the PSSetConstantBuffers call
constBuffer.light = light;
devcon->UpdateSubresource(cbPerFrameBuffer, 0, 0, &constBuffer, 0, 0);
devcon->PSSetConstantBuffers(0, 1, &cbPerFrameBuffer);
So I'm just wondering what am I overlooking? I figured there wouldn't be any easy way to ask this without posting 100+ lines of code. But if anyone could point out something thats off in the blocks of code that I have provided it would be greatly appreciated!
-Marcus