summaryrefslogtreecommitdiffstats
path: root/res/effectlib/calculateRoughness.glsllib
blob: 13ca82347e12828a52b2802f98817676c8d87a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
float calculateRoughness( in vec3 N, in float roughnessU, in float roughnessV, in vec3 tangentU )
{
  float roughness = roughnessU;
  if ( abs(roughnessU - roughnessV) > 0.00001)
  {
    // determine major and minor radii a and b, and the vector along the major axis
    float a = roughnessU;
    float b = roughnessV;

    // we need the angle between the major axis and the projection of viewDir on the tangential plane
    // the major axis is the orthonormalization of tangentU with respect to N
    // the projection of viewDir is the orthonormalization of viewDir with respect to N
    // as both vectors would be calculated by orthonormalize, we can as well leave the second cross
    // product in those calculations away, as they don't change the angular relation.
    vec3 minorAxis = normalize( cross( tangentU, N ) );   // crossing this with N would give the major axis
                                                          // which is equivalent to orthonormalizing tangentU with respect to N
    if ( roughnessU < roughnessV )
    {
      a = roughnessV;
      b = roughnessU;
      minorAxis = cross( N, minorAxis );
    }

    vec3 po = normalize( cross( viewDir, N ) );
    float cosPhi = dot( po, minorAxis );

    // determine the polar coordinate of viewDir, take that radius as the roughness
    float excentricitySquare = 1.0f - square( b / a );
    roughness = b / sqrt( 1.0f - excentricitySquare * square( cosPhi ) );
  }
  return( roughness );
}