| 25 | ////////////////////////////////////////////////////////////////////////////// |
| 26 | |
| 27 | SYCL_EXTERNAL Light_SampleRes SpotLight_sample(const Light* super, |
| 28 | const DifferentialGeometry& dg, |
| 29 | const Vec2f& s) |
| 30 | { |
| 31 | const SpotLight* self = (SpotLight*)super; |
| 32 | Light_SampleRes res; |
| 33 | |
| 34 | // extant light vector from the hit point |
| 35 | res.dir = self->position - dg.P; |
| 36 | |
| 37 | if (self->radius > 0.0f) |
| 38 | res.dir = self->frame * uniformSampleDisk(self->radius, s) + res.dir; |
| 39 | |
| 40 | const float dist2 = dot(res.dir, res.dir); |
| 41 | const float invdist = rsqrt(dist2); |
| 42 | |
| 43 | // normalized light vector |
| 44 | res.dir = res.dir * invdist; |
| 45 | res.dist = dist2 * invdist; |
| 46 | |
| 47 | // cosine of the negated light direction and light vector. |
| 48 | const float cosAngle = -dot(self->frame.vz, res.dir); |
| 49 | const float angularAttenuation = clamp((cosAngle - self->cosAngleMax) * self->cosAngleScale); |
| 50 | |
| 51 | if (self->radius > 0.0f) |
| 52 | res.pdf = self->diskPdf * dist2 * abs(cosAngle); |
| 53 | else |
| 54 | res.pdf = inf; // we always take this res |
| 55 | |
| 56 | // convert from power to radiance by attenuating by distance^2; attenuate by angle |
| 57 | res.weight = self->power * (sqr(invdist) * angularAttenuation); |
| 58 | |
| 59 | return res; |
| 60 | } |
| 61 | |
| 62 | SYCL_EXTERNAL Light_EvalRes SpotLight_eval(const Light* super, |
| 63 | const DifferentialGeometry& dg, |