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