I've implemented a directional lighting shader, and it works if the object if still, however, when rotated the light moves in an odd way. I keep thinking it's the normals, but I can't spot exactly what's wrong. I've attempted to make a GIF of my program below:
Snippet of my code setting the shader parameters:
...
D3DXMATRIX rotMat, posMat, worldMat;
D3DXMatrixRotationYawPitchRoll(&rotMat, rot.y, rot.x, rot.z);
D3DXMatrixTranslation(&posMat, pos.x, pos.y, pos.z);
D3DXMatrixTranspose(&worldMat, &(rotMat*posMat));
D3DXMatrixTranspose(¶m.viewMat, ¶m.viewMat);
D3DXMatrixTranspose(¶m.projMat, ¶m.projMat);
if(FAILED(devcon->Map(matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource)))
{
loadedShaders = false;
return false;
}
mbptr = (MatrixBuffer*)mappedResource.pData;
mbptr->world = worldMat;
mbptr->view = param.viewMat;
mbptr->proj = param.projMat;
devcon->Unmap(matrixBuffer, 0);
devcon->VSSetConstantBuffers(0, 1, &matrixBuffer);
...
PixelInput LightVS(VertexInput input)
{
PixelInput output;
input.pos.w = 1.0f;
output.pos = mul(input.pos, mWorld);
output.pos = mul(output.pos, mView);
output.pos = mul(output.pos, mProj);
output.tex = input.tex;
output.norm = mul(input.norm, (float3x3)mWorld);
output.norm = normalize(output.norm);
return output;
}
float4 LightPS(PixelInput input) : SV_TARGET
{
float lightIntensity = saturate(dot(input.norm, lightDir));
float4 col = ambientCol;
if(lightIntensity > 0.0f) col += diffuseCol * lightIntensity;
return saturate(col) * shaderTexture.Sample(sampleType, input.tex);
}
Thanks for any help.