1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#include "light.isph"
5#include "../math/sampling.isph"
6#include "../math/linearspace.isph"
7
8struct AmbientLight
9{
10  Light super;      //!< inherited light fields
11
12  Vec3f radiance;   //!< RGB color and intensity of light
13};
14
15
16// Implementation
17//////////////////////////////////////////////////////////////////////////////
18
19// XXX importance sampling is only done into the positive hemisphere
20// ==> poor support for translucent materials
21Light_SampleRes AmbientLight_sample(const uniform Light* uniform super,
22                                    const DifferentialGeometry& dg,
23                                    const Vec2f& s)
24{
25  uniform AmbientLight* uniform self = (uniform AmbientLight* uniform)super;
26  Light_SampleRes res;
27
28  const Vec3f localDir = cosineSampleHemisphere(s);
29  res.dir = frame(dg.Ns) * localDir;
30  res.pdf = cosineSampleHemispherePDF(localDir);
31  res.dist = inf;
32  res.weight = self->radiance * rcp(res.pdf);
33
34  return res;
35}
36
37Light_EvalRes AmbientLight_eval(const uniform Light* uniform super,
38                                const DifferentialGeometry& dg,
39                                const Vec3f& dir)
40{
41  uniform AmbientLight* uniform self = (uniform AmbientLight* uniform)super;
42  Light_EvalRes res;
43
44  res.value = self->radiance;
45  res.dist = inf;
46  res.pdf = cosineSampleHemispherePDF(max(dot(dg.Ns, dir), 0.f));
47
48  return res;
49}
50
51
52void AmbientLight_Constructor(uniform AmbientLight* uniform self,
53                              const uniform Vec3f& radiance)
54{
55  Light_Constructor(&self->super);
56  self->radiance = radiance;
57  self->super.sample = AmbientLight_sample;
58  self->super.eval = AmbientLight_eval;
59}
60
61
62// Exports (called from C++)
63//////////////////////////////////////////////////////////////////////////////
64
65//! Create an ispc-side AmbientLight object
66export void *uniform AmbientLight_create()
67{
68  uniform AmbientLight* uniform self = uniform new uniform AmbientLight;
69  AmbientLight_Constructor(self, make_Vec3f(1.f));
70  return self;
71}
72
73//! Set the parameters of an ispc-side AmbientLight object
74export void AmbientLight_set(void* uniform super,
75                             const uniform Vec3f& radiance)
76{
77  uniform AmbientLight* uniform self = (uniform AmbientLight* uniform)super;
78  self->radiance = radiance;
79}
80