// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef FRESNEL_GLSLLIB
#define FRESNEL_GLSLLIB 1

float qt_schlick(float value)
{
    float n = 1.0 - value;
    float n2 = n * n;
    return n2 * n2 * n;
}

vec3 qt_F0_ior(float ior, float metalness, vec3 baseColor)
{
    float f0 = ((ior - 1.0) * (ior - 1.0)) / ((ior + 1.0) * (ior + 1.0));
    return (vec3(f0) * (1.0 - metalness)) + (baseColor * metalness);
}

vec3 qt_principledMaterialFresnel(in vec3 N, in vec3 viewDir, in vec3 f0, in float roughness, in float fresnelPower)
{
    float nDotV = clamp(dot(N, viewDir), 0.0, 1.0);
    vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - nDotV, fresnelPower);
    return F;
}

#endif
