/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
 *
 * SPDX-License-Identifier: Apache-2.0 */

#include "node_fresnel.h"
#include "stdcycles.h"

shader node_glossy_bsdf(color Color = 0.8,
                        string distribution = "ggx",
                        float Roughness = 0.2,
                        float Anisotropy = 0.0,
                        float Rotation = 0.0,
                        normal Normal = N,
                        normal Tangent = 0.0,
                        output closure color BSDF = 0)
{
  /* compute roughness */
  float roughness = Roughness * Roughness;
  float roughness_u, roughness_v;
  float aniso = clamp(Anisotropy, -0.99, 0.99);

  /* rotate tangent around normal */
  vector T = Tangent;

  if (abs(aniso) <= 1e-4) {
    roughness_u = roughness;
    roughness_v = roughness;
  }
  else {
    if (Rotation != 0.0)
      T = rotate(T, Rotation * M_2PI, point(0.0, 0.0, 0.0), Normal);

    if (aniso < 0.0) {
      roughness_u = roughness / (1.0 + aniso);
      roughness_v = roughness * (1.0 + aniso);
    }
    else {
      roughness_u = roughness * (1.0 - aniso);
      roughness_v = roughness / (1.0 - aniso);
    }
  }

  if (distribution == "Multiscatter GGX")
    BSDF = Color * microfacet_multi_ggx_aniso(Normal, T, roughness_u, roughness_v, Color);
  else
    BSDF = Color * microfacet(distribution, Normal, T, roughness_u, roughness_v, 0.0, 0);
}
