1 // Copyright 2009-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include "light.h"
5 #include "../math/sampling.h"
6 #include "../math/linearspace.h"
7
8 namespace embree {
9
10 struct DirectionalLight
11 {
12 Light super; //!< inherited light fields
13
14 LinearSpace3fa frame; //!< coordinate frame, with vz == direction *towards* the light source
15 Vec3fa radiance; //!< RGB color and intensity of light
16 float cosAngle; //!< Angular limit of the cone light in an easier to use form: cosine of the half angle in radians
17 float pdf; //!< Probability to sample a direction to the light
18 };
19
20 // for very small cones treat as singular light, because float precision is not good enough
21 #define COS_ANGLE_MAX 0.99999988f
22
23
24 // Implementation
25 //////////////////////////////////////////////////////////////////////////////
26
DirectionalLight_sample(const Light * super,const DifferentialGeometry & dg,const Vec2f & s)27 Light_SampleRes DirectionalLight_sample(const Light* super,
28 const DifferentialGeometry& dg,
29 const Vec2f& s)
30 {
31 const DirectionalLight* self = (DirectionalLight*)super;
32 Light_SampleRes res;
33
34 res.dir = self->frame.vz;
35 res.dist = inf;
36 res.pdf = self->pdf;
37
38 if (self->cosAngle < COS_ANGLE_MAX)
39 res.dir = self->frame * uniformSampleCone(self->cosAngle, s);
40
41 res.weight = self->radiance; // *pdf/pdf cancel
42
43 return res;
44 }
45
DirectionalLight_eval(const Light * super,const DifferentialGeometry &,const Vec3fa & dir)46 Light_EvalRes DirectionalLight_eval(const Light* super,
47 const DifferentialGeometry&,
48 const Vec3fa& dir)
49 {
50 DirectionalLight* self = (DirectionalLight*)super;
51 Light_EvalRes res;
52 res.dist = inf;
53
54 if (self->cosAngle < COS_ANGLE_MAX && dot(self->frame.vz, dir) > self->cosAngle) {
55 res.value = self->radiance * self->pdf;
56 res.pdf = self->pdf;
57 } else {
58 res.value = Vec3fa(0.f);
59 res.pdf = 0.f;
60 }
61
62 return res;
63 }
64
65
66 // Exports (called from C++)
67 //////////////////////////////////////////////////////////////////////////////
68
69 //! Set the parameters of an ispc-side DirectionalLight object
DirectionalLight_set(void * super,const Vec3fa & direction,const Vec3fa & radiance,float cosAngle)70 extern "C" void DirectionalLight_set(void* super,
71 const Vec3fa& direction,
72 const Vec3fa& radiance,
73 float cosAngle)
74 {
75 DirectionalLight* self = (DirectionalLight*)super;
76 self->frame = frame(direction);
77 self->radiance = radiance;
78 self->cosAngle = cosAngle;
79 self->pdf = cosAngle < COS_ANGLE_MAX ? uniformSampleConePDF(cosAngle) : inf;
80 }
81
82 //! Create an ispc-side DirectionalLight object
DirectionalLight_create()83 extern "C" void* DirectionalLight_create()
84 {
85 DirectionalLight* self = (DirectionalLight*) alignedMalloc(sizeof(DirectionalLight),16);
86 Light_Constructor(&self->super);
87 self->super.sample = DirectionalLight_sample;
88 self->super.eval = DirectionalLight_eval;
89
90 DirectionalLight_set(self, Vec3fa(0.f, 0.f, 1.f), Vec3fa(1.f), 1.f);
91 return self;
92 }
93
94 } // namespace embree
95