@Magogan
This is the shader code:
cbuffer ProjectionMatrices : register (b0)
{
matrix View;
matrix Projection;
}
cbuffer WorldMatrices : register (b1)
{
matrix World;
}
cbuffer CameraPosition : register (b5)
{
float3 EyePosition;
};
cbuffer DirectionalLight : register (b2)
{
float3 LightDirection;
float4 LightColor;
};
cbuffer LightingVariables : register (b4)
{
float4 AmbientLight;
};
cbuffer Material : register (b3)
{
float Ka, Kd, Ks, A;
};
struct TexturedLitVertex
{
float4 Pos : SV_POSITION;
float4 Color: COLOR;
float2 Uv: TEXCOORD;
};
float4 calcBlinnPhongLighting(float MaterialKa, float MaterialKd, float MaterialKs, float MaterialA, float4 LColor, float3 N, float3 L, float3 H)
{
float4 Ia = MaterialKa * AmbientLight;
float4 Id = MaterialKd * saturate(dot(N, L));
float4 Is = MaterialKs * pow(saturate(dot(N, H)), MaterialA);
float4 finalColor = Ia + (Id + Is) * LColor;
finalColor.a = 1.0f;
return finalColor;
}
TexturedLitVertex VS_DirectionalLight(float4 Pos : POSITION, float3 Normal : NORMAL, float2 Uv : TEXCOORD0)
{
TexturedLitVertex output = (TexturedLitVertex)0;
//transform to clip space
output.Pos = mul(Pos, World);
output.Pos = mul(output.Pos, View);
output.Pos = mul(output.Pos, Projection);
//pass on the texture coordinates
output.Uv = Uv;
float3 N = normalize(mul(Normal, (float3x3)World));
float3 V = normalize(EyePosition - (float3)Pos);
float3 H = normalize(-LightDirection + V);
//calculate lighting intesity and interpolate it as color
float4 LightColor2 = float4( 1.0f, 1.0f, 1.0f, 1.0f );
output.Color = calcBlinnPhongLighting(Ka, Kd, Ks, A, LightColor2, N, LightDirection, H);
return output;
}
float4 PS_DirectionalLight(TexturedLitVertex psInput) : SV_Target
{
return psInput.Color *txDiffuse.Sample(triLinearSampler, psInput.Uv);
}
Also just to note, this issue might stem from the constant buffers. I'm not sure I'm using them correctly, I have like 7 of them for each variable pretty much and one of the buffers doesn't really work. The LightColor parameter in the DirectionLight is always incorrect, that is why I just resorted to setting it manually in the code for now (the float4 LightColor2 parameter). Just in case I'll post my code for how I manage related constant buffers in my code
__declspec(align(16)) struct DirectionalLight
{
DirectX::XMFLOAT3 LightDirection;
DirectX::XMFLOAT4 LightColor;
};
__declspec(align(16)) struct ProjectionVariables
{
DirectX::XMMATRIX View;
DirectX::XMMATRIX Projection;
};
__declspec(align(16)) struct WorldMatrices
{
DirectX::XMMATRIX World;
};
__declspec(align(16)) struct LightVariables
{
DirectX::XMFLOAT4 AmbientLight;
};
__declspec(align(16)) struct CameraPosition
{
DirectX::XMFLOAT3 EyePosition;
};
__declspec(align(16)) struct Material
{
float Ka;
float Kd;
float Ks;
float A;
};
void InitAll(ID3D11Device* device)
{
//create buffers
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(ProjectionVariables);
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 0;
device->CreateBuffer(&bufferDesc, NULL, &ViewProjBuffer);
bufferDesc.ByteWidth = sizeof(WorldMatrices);
device->CreateBuffer(&bufferDesc, NULL, &WorldMatrixBuffer);
bufferDesc.ByteWidth = sizeof(DirectionalLight);
device->CreateBuffer(&bufferDesc, NULL, &DirectionalLightBuffer);
bufferDesc.ByteWidth = sizeof(LightVariables);
device->CreateBuffer(&bufferDesc, NULL, &LightVariablesBuffer);
bufferDesc.ByteWidth = sizeof(CameraPosition);
device->CreateBuffer(&bufferDesc, NULL, &CameraPositionBuffer);
bufferDesc.ByteWidth = sizeof(Material);
device->CreateBuffer(&bufferDesc, NULL, &MaterialBuffer);
//bind all the buffers to a device
ID3D11DeviceContext* deviceContext;
device->GetImmediateContext(&deviceContext);
deviceContext->VSSetConstantBuffers(0, 1, &ViewProjBuffer);
deviceContext->PSSetConstantBuffers(0, 1, &ViewProjBuffer);
deviceContext->VSSetConstantBuffers(1, 1, &WorldMatrixBuffer);
deviceContext->PSSetConstantBuffers(1, 1, &WorldMatrixBuffer);
deviceContext->VSSetConstantBuffers(2, 1, &DirectionalLightBuffer);
deviceContext->PSSetConstantBuffers(2, 1, &DirectionalLightBuffer);
deviceContext->VSSetConstantBuffers(3, 1, &MaterialBuffer);
deviceContext->PSSetConstantBuffers(3, 1, &MaterialBuffer);
deviceContext->VSSetConstantBuffers(4, 1, &LightVariablesBuffer);
deviceContext->PSSetConstantBuffers(4, 1, &LightVariablesBuffer);
deviceContext->VSSetConstantBuffers(5, 1, &CameraPositionBuffer);
deviceContext->PSSetConstantBuffers(5, 1, &CameraPositionBuffer);
}
//then I update them in code
ConstantBuffers::WorldMatrices worldBuffer;
worldBuffer.World = XMMatrixIdentity();
mDeviceContext->UpdateSubresource(ConstantBuffers::WorldMatrixBuffer, 0, NULL, &worldBuffer, 0, 0);
ConstantBuffers::Material material;
material.Ka = 0.0f;
material.Kd = 1.0f;
material.Ks = 0.0f;
material.A = 0.0f;
mDeviceContext->UpdateSubresource(ConstantBuffers::MaterialBuffer, 0, NULL, &material, 0, 0);
ConstantBuffers::ProjectionVariables projectionMatrices;
projectionMatrices.View = XMMatrixTranspose(mCharacterController->GetCamera().GetViewMatrix());
projectionMatrices.Projection = XMMatrixTranspose(mCharacterController->GetCamera().GetProjectionMatrix());
mDeviceContext->UpdateSubresource(ConstantBuffers::ViewProjBuffer, 0, NULL, &projectionMatrices, 0, 0);
ConstantBuffers::CameraPosition eyePos;
eyePos.EyePosition = mCharacterController->GetCamera().GetPosition();
mDeviceContext->UpdateSubresource(ConstantBuffers::CameraPositionBuffer, 0, NULL, &eyePos, 0, 0);
ConstantBuffers::DirectionalLight dirLight;
XMVECTOR ldir = { -0.577f, 0.577f, -0.577f };
ldir = XMVector3Normalize(ldir);
XMStoreFloat3(&dirLight.LightDirection, ldir);
dirLight.LightColor = XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f );
mDeviceContext->UpdateSubresource(ConstantBuffers::DirectionalLightBuffer, 0, NULL, &dirLight, 0, 0);
ConstantBuffers::LightVariables lightVars;
lightVars.AmbientLight = { 1.0f, 1.0f, 1.0f, 1.0f };
mDeviceContext->UpdateSubresource(ConstantBuffers::LightVariablesBuffer, 0, NULL, &lightVars, 0, 0);