1/****************************************************************************
2**
3** Copyright (C) 2014 NVIDIA Corporation.
4** Copyright (C) 2019 The Qt Company Ltd.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of Qt 3D Studio.
8**
9** $QT_BEGIN_LICENSE:GPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3 or (at your option) any later version
21** approved by the KDE Free Qt Foundation. The licenses are as published by
22** the Free Software Foundation and appearing in the file LICENSE.GPL3
23** included in the packaging of this file. Please review the following
24** information to ensure the GNU General Public License requirements will
25** be met: https://www.gnu.org/licenses/gpl-3.0.html.
26**
27** $QT_END_LICENSE$
28**
29****************************************************************************/
30
31
32float simpleFresnel( in vec3 N, in float ior, float fresnelPower )
33{
34    float F = ((1.0-ior) * (1.0-ior)) / ((1.0+ior) * (1.0+ior));
35    float fresnelGlancingAngle = clamp( 1.0 - dot(viewDir, N), 0.0, 1.0 );
36    float ratio = F + (1.0 - F) * pow(fresnelGlancingAngle, fresnelPower);
37
38    return ratio;
39}
40
41// PKC : A variation on the simple Schlick approach that also handles the characteristic
42// "dip" that metals exhibit.  The dip is very mild or virtually nonexisting in dielectrics,
43// but shows up in metals because of their strong incident reflectivity, and also allows for
44// colored metals like gold or copper to show their Fresnel hue shift as well because certain
45// components will have different baseline responses.
46vec3 pseudoFresnel( in vec3 N, in vec3 ior, float fresnelPower )
47{
48    vec3 F0 = ((vec3(1.0)-ior) * (vec3(1.0)-ior)) / ((vec3(1.0)+ior) * (vec3(1.0)+ior));
49
50    float fresnelGlancingAngle = clamp( 1.0 - dot(viewDir, N), 0.0, 1.0 );
51
52    float fresCurve = pow(fresnelGlancingAngle, fresnelPower);
53    vec3 dipCurve = F0 * sin( 3.1415926535 * fresCurve ) * 0.31830988618;
54
55    vec3 retCol = (vec3(1.0) - F0) * fresCurve;
56    retCol += F0 - dipCurve;
57    return clamp(retCol, 0.0, 1.0);
58}
59
60// look here: http://en.wikipedia.org/wiki/Fresnel_equations
61float fresnel( in float eta1, in float eta2, in float cosTheta1 )
62{
63  float etaInv = eta1 / eta2;
64  float cosTheta2 = 1.0 - ( 1.0 - cosTheta1 * cosTheta1 ) * ( etaInv * etaInv );
65  if ( 0.0 <= cosTheta2 )
66  {
67    cosTheta2 = sqrt( cosTheta2 );
68    float n1t1 = eta1 * cosTheta1;
69    float n1t2 = eta1 * cosTheta2;
70    float n2t1 = eta2 * cosTheta1;
71    float n2t2 = eta2 * cosTheta2;
72    float rs = ( n1t1 - n2t2 ) / ( n1t1 + n2t2 );
73    float rp = ( n1t2 - n2t1 ) / ( n1t2 + n2t1 );
74    float f = 0.5 * ( rs * rs + rp * rp );
75    return( clamp( f, 0.0, 1.0 ) );
76  }
77  else
78  {
79    return( 1.0 );
80  }
81}
82
83// we assume, light rays run through air with ior == 1.0
84// with eta = eta2 / eta1, we have
85//  - when hitting a front face: eta2 == ior, eta1 == 1.0 => eta = ior
86//  - when hitting a back face : eta2 == 1.0, eta1 == ior => eta = 1.0 / ior
87vec3 fresnel( in vec3 N, in vec3 ior )
88{
89  float cosTheta1 = dot( N, viewDir );
90  if ( gl_FrontFacing )
91  {
92    return( vec3( fresnel( 1.0, ior[0], cosTheta1 ), fresnel( 1.0, ior[1], cosTheta1 ), fresnel( 1.0, ior[2], cosTheta1 ) ) );
93  }
94  else
95  {
96    return( vec3( fresnel( ior[0], 1.0, cosTheta1 ), fresnel( ior[1], 1.0, cosTheta1 ), fresnel( ior[2], 1.0, cosTheta1 ) ) );
97  }
98}
99
100vec4 fresnelLayer( in vec3 N, in vec3 ior, in float weight, in vec3 layercolor, in vec4 layer, in vec4 base, in float alpha )
101{
102  vec3 refl = reflect( -viewDir, N );
103  float reflWt = clamp( dot( surfNormal, refl ) + 1.0, 0.0, 1.0 );
104
105  vec3 fresColor = reflWt * weight * layercolor * pseudoFresnel( N, ior, 5.0 );
106
107  return( vec4( mix( base.rgb, layer.rgb, fresColor ), mix(alpha, 1.0, luminance(fresColor)) ) );
108}
109
110