File: quad_light.cpp

package info (click to toggle)
embree 4.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100,656 kB
  • sloc: cpp: 228,918; xml: 40,944; ansic: 2,685; python: 812; sh: 635; makefile: 228; csh: 42
file content (106 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "light.h"

namespace embree {

struct QuadLight
{
  Light super;            //!< inherited light fields

  Vec3fa position;         //!< world-space corner position of the light
  Vec3fa edge1;            //!< vectors to adjacent corners
  Vec3fa edge2;            //!< vectors to adjacent corners
  Vec3fa radiance;         //!< RGB color and intensity of the QuadLight

  Vec3fa nnormal;          //!< negated normal, the direction that the QuadLight is not emitting; normalized
  float ppdf;             // probability to sample point on light = 1/area
};


// Implementation
//////////////////////////////////////////////////////////////////////////////

SYCL_EXTERNAL Light_SampleRes QuadLight_sample(const Light* super,
                                                              const DifferentialGeometry& dg,
                                                              const Vec2f& s)
{
  const QuadLight* self = (QuadLight*)super;
  Light_SampleRes res;

  // res position on light with density ppdf = 1/area
  // TODO: use solid angle sampling
  const Vec3fa pos = self->position + self->edge1 * s.x + self->edge2 * s.y;

  // extant light vector from the hit point
  const Vec3fa dir = pos - dg.P;
  const float dist = length(dir);

  // normalized light vector
  res.dir = dir / dist;
  res.dist = dist;

  // convert to pdf wrt. solid angle
  const float cosd = dot(self->nnormal, res.dir);
  res.pdf = self->ppdf * (dist * dist) / abs(cosd);

  // emit only to one side
  res.weight = cosd > 0.f ? self->radiance * rcp(res.pdf) : Vec3fa(0.f);

  return res;
}

SYCL_EXTERNAL Light_EvalRes QuadLight_eval(const Light* super,
                                                          const DifferentialGeometry& dg,
                                                          const Vec3fa& dir)
  
{
  Light_EvalRes res;
  res.value = Vec3fa(0,0,0);
  res.dist = inf;
  res.pdf = 0.f;
  return res;
}


// Exports (called from C++)
//////////////////////////////////////////////////////////////////////////////

//! Set the parameters of an ispc-side QuadLight object
extern "C" void QuadLight_set(void* super,
                          const Vec3fa& position,
                          const Vec3fa& edge2,
                          const Vec3fa& edge1,
                          const Vec3fa& radiance)
{
  QuadLight* self = (QuadLight*)super;
  self->position = position;
  self->edge1    = edge1;
  self->edge2    = edge2;
  self->radiance = radiance;

  const Vec3fa ndirection = cross(edge2, edge1);
  self->ppdf = rcp(length(ndirection));
  self->nnormal = ndirection * self->ppdf;
}

//! Create an ispc-side QuadLight object
extern "C" void* QuadLight_create()
{
  QuadLight* self = (QuadLight*) alignedUSMMalloc(sizeof(QuadLight),16);

  Light_Constructor(&self->super);
  //self->super.sample = GET_FUNCTION_POINTER(QuadLight_sample);
  self->super.type = LIGHT_QUAD;

  QuadLight_set(self,
                Vec3fa(0.f),
                Vec3fa(1.f, 0.f, 0.f),
                Vec3fa(0.f, 1.f, 0.f),
                Vec3fa(1.f));

  return self;
}

} // namespace embree