Blender V5.3
sun.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
10
11#include "util/math_fast.h"
12
14
16 const ccl_global KernelLight *klight,
17 const float3 D)
18{
19 /* Map direction (x, y, z) to disk [-0.5, 0.5]^2:
20 * r^2 = (1 - z) / (1 - cos(klight->sun.angle))
21 * u_ = 0.5 * x * r / sin_angle(D, -klight->co)
22 * v_ = 0.5 * y * r / sin_angle(D, -klight->co) */
23 const float fac = klight->sun.half_inv_sin_half_angle / len(D - klight->co);
24
25 /* Get u axis and v axis. */
26 const Transform itfm = lamp_get_inverse_transform(kg, klight);
27 const float u_ = dot(D, make_float3(itfm.x)) * fac;
28 const float v_ = dot(D, make_float3(itfm.y)) * fac;
29
30 /* NOTE: Return barycentric coordinates in the same notation as Embree and OptiX. */
31 return make_float2(v_ + 0.5f, -u_ - v_);
32}
33
35 const float2 rand,
37{
38 float unused;
39 ls->Ng = sample_uniform_cone(
40 klight->co, klight->sun.one_minus_cosangle, rand, &unused, &ls->pdf);
41
42 ls->P = ls->Ng;
43 ls->D = -ls->Ng;
44 ls->t = FLT_MAX;
45
46 ls->eval_fac = klight->sun.eval_fac;
47
48 return true;
49}
50
51/* Special intersection check.
52 * Returns true if the sun_light_eval_from_intersection() for this light would return true.
53 *
54 * The intersection parameters t, u, v are optimized for the shadow ray towards a dedicated light:
55 * u = v = 0, t = FLT_MAX.
56 */
58 const ccl_private Ray *ccl_restrict ray,
59 ccl_private float *t)
60{
61 kernel_assert(klight->type == LIGHT_SUN);
62
63 if (klight->sun.angle == 0.0f) {
64 return false;
65 }
66
67 if (vector_angle(-klight->co, ray->D) > klight->sun.angle) {
68 return false;
69 }
70
71 *t = FLT_MAX;
72
73 return true;
74}
75
77 const float3 ray_D)
78{
79 if (klight->sun.angle == 0.0f) {
80 return LightEval{};
81 }
82
83 if (vector_angle(-klight->co, ray_D) > klight->sun.angle) {
84 return LightEval{};
85 }
86
87 return LightEval{klight->sun.eval_fac, klight->sun.pdf};
88}
89
90template<bool in_volume_segment>
92 const float theta_e,
93 const float t,
94 ccl_private float &cos_theta_u,
96 ccl_private float3 &point_to_centroid,
97 ccl_private float &theta_d)
98{
99 if (in_volume_segment) {
100 if (t == FLT_MAX) {
101 /* In world volumes, distant lights can contribute to the lighting of the volume with
102 * specific configurations of procedurally generated volumes. Use a ray length of 1.0 in this
103 * case to give the distant light some weight, but one that isn't too high for a typical
104 * world volume use case. */
105 theta_d = 1.0f;
106 }
107 else {
108 theta_d = t;
109 }
110 }
111
112 /* Treating it as a disk light 1 unit away */
113 cos_theta_u = fast_cosf(theta_e);
114
115 distance = make_float2(1.0f / cos_theta_u, 1.0f);
116
117 point_to_centroid = -centroid;
118
119 return true;
120}
121
#define D
#define kernel_assert(cond)
#define ccl_restrict
#define ccl_device_forceinline
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float3 sample_uniform_cone(float2 rand, float cos_angle)
#define FLT_MAX
float dot(VecOp< float, D >, VecOp< float, D >) RET
float distance(float, float) RET
ccl_device_inline Transform lamp_get_inverse_transform(KernelGlobals kg, const ccl_global KernelLight *klight)
@ LIGHT_SUN
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline float vector_angle(const float3 a, const float3 b)
Definition math_fast.h:339
ccl_device float fast_cosf(float x)
Definition math_fast.h:118
#define ccl_device
#define make_float2
float4 y
Definition transform.h:53
float4 x
Definition transform.h:53
ccl_device bool sun_light_intersect(const ccl_global KernelLight *klight, const ccl_private Ray *ccl_restrict ray, ccl_private float *t)
Definition sun.h:57
ccl_device_forceinline bool sun_light_tree_parameters(const float3 centroid, const float theta_e, const float t, ccl_private float &cos_theta_u, ccl_private float2 &distance, ccl_private float3 &point_to_centroid, ccl_private float &theta_d)
Definition sun.h:91
CCL_NAMESPACE_BEGIN ccl_device_inline float2 sun_light_uv(KernelGlobals kg, const ccl_global KernelLight *klight, const float3 D)
Definition sun.h:15
ccl_device LightEval sun_light_eval_from_intersection(const ccl_global KernelLight *klight, const float3 ray_D)
Definition sun.h:76
ccl_device_inline bool sun_light_sample(const ccl_global KernelLight *klight, const float2 rand, ccl_private LightSample *ls)
Definition sun.h:34