// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "BSDF.ih"
#include "math/sampling.ih"

OSPRAY_BEGIN_ISPC_NAMESPACE

inline BSDF_EvalRes Lambert_eval(
    const varying BSDF *uniform self, const vec3f &, const vec3f &wi)
{
  BSDF_EvalRes res;
  float cosThetaI = max(dot(wi, getN(self)), 0.f);
  res.pdf = cosineSampleHemispherePDF(cosThetaI);
  res.value = self->albedo * (float)one_over_pi * cosThetaI;
  return res;
}

inline BSDF_SampleRes Lambert_sample(
    const varying BSDF *uniform self, const vec3f &, const vec2f &s, float)
{
  const vec3f localDir = cosineSampleHemisphere(s);
  BSDF_SampleRes res;
  res.wi = getFrame(self) * localDir;
  res.pdf = cosineSampleHemispherePDF(localDir);
  res.type = BSDF_DIFFUSE_REFLECTION;
  res.weight = self->albedo;
  return res;
}

inline void Lambert_Constructor(varying BSDF *uniform self,
    const varying linear3f *uniform frame,
    const varying vec3f &R)
{
  BSDF_Constructor(self, R, BSDF_DIFFUSE_REFLECTION, BSDF_TYPE_LAMBERT, frame);
}

OSPRAY_END_ISPC_NAMESPACE
