#include "include/shadow_mainlit.hlsli"

// cbuffer CBScene : register(b0)
// {
//     row_major float4x4 view;
//     row_major float4x4 proj;
//     float4 cameraPos;
//     float4 lightDirection;
//     float4 cameraColor;
// }

cbuffer CBPerMat : register(b1){
    float4 cbColor;
}

// cbuffer CBPerObj : register(b2)
// {
//     row_major float4x4 world;
// }

struct VSInput
{
    float3 position : POSITION;
    float3 normal : NORMAL;
    //    float3 tangent    : TANGENT;
    float2 texcoord0 : TEXCOORD0;
    //    float2 texcoord1  : TEXCOORD1;
    //    float4 color      : COLOR0;
    //    int4 boneIndex    : BLENDINDICES0;
    //    float4 boneWeight : BLENDWEIGHT0;
};

struct PSInput
{
    float4 pos : SV_POSITION;
    float3 worldPos : MPROP0;
    float4 shadowPos : MPROP1;
    float2 uv : MPROP2;
    float3 worldNrm : MPROP3;
};

PSInput VSMain(VSInput i)
{
    PSInput o;

    float4 pos = float4(i.position, 1.0f);
    pos = mul(pos, cbWorld);
    o.worldPos = pos.xyz;
    o.shadowPos = mul(pos, cbLightViewProjection);
    pos = mul(pos, cbView);
    pos = mul(pos, cbProj);
    o.pos = pos;

    o.uv = i.texcoord0;

    float4 _normal = float4(i.normal, 0.0f);
    _normal = mul(_normal, cbWorld);
    o.worldNrm = _normal.xyz;

    return o;
}

half4 PSMain(PSInput i) : SV_TARGET
{
    half3 color = half3(0.8f, 0.8f, 0.9f);

    i.worldNrm = normalize(i.worldNrm);
    float intlit = saturate(-dot(i.worldNrm, cbMainLightDirection.xyz));
    intlit *= CalculateMainLightShadow(i.shadowPos, i.worldNrm);

    intlit = 0.5f * (intlit + 1.0f);


    half3 col = color * intlit;

    return half4(col.xyz, 1.0f);
}
