Blender V5.3
blenkernel/intern/light.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <optional>
11
12#include "MEM_guardedalloc.h"
13
14/* Allow using deprecated functionality for .blend file I/O. */
15#define DNA_DEPRECATED_ALLOW
16
17#include "DNA_light_types.h"
18#include "DNA_node_types.h"
19#include "DNA_scene_types.h"
20
21#include "BLI_math_base.hh"
22#include "BLI_math_matrix.hh"
24#include "BLI_utildefines.hh"
25
26#include "BKE_icons.hh"
27#include "BKE_idtype.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_lib_query.hh"
30#include "BKE_light.h"
31#include "BKE_node.hh"
32#include "BKE_preview_image.hh"
33
34#include "BLT_translation.hh"
35
36#include "DEG_depsgraph.hh"
37
39
40#include "BLO_read_write.hh"
41
42#include "NOD_defaults.hh"
43
44namespace blender {
45
46static void light_init_data(ID *id)
47{
48 Light *la = id_cast<Light *>(id);
50}
51
62static void light_copy_data(Main *bmain,
63 std::optional<Library *> owner_library,
64 ID *id_dst,
65 const ID *id_src,
66 const int flag)
67{
68 Light *la_dst = id_cast<Light *>(id_dst);
69 const Light *la_src = id_cast<const Light *>(id_src);
70
71 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
72 /* We always need allocation of our private ID data.
73 * User reference-counting is also handled by calling code,
74 * so the duplication calls for embedded data should _never_ handle it from here. */
75 const int flag_embedded_id_data = (flag & ~LIB_ID_CREATE_NO_ALLOCATE) |
77
78 if (la_src->nodetree) {
79 if (is_localized) {
80 la_dst->nodetree = bke::node_tree_localize(la_src->nodetree, &la_dst->id);
81 }
82 else {
84 owner_library,
85 &la_src->nodetree->id,
86 &la_dst->id,
87 reinterpret_cast<ID **>(&la_dst->nodetree),
88 flag_embedded_id_data);
89 }
90 }
91
92 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
93 BKE_previewimg_id_copy(&la_dst->id, &la_src->id);
94 }
95 else {
96 la_dst->preview = nullptr;
97 }
98}
99
100static void light_free_data(ID *id)
101{
102 Light *la = id_cast<Light *>(id);
103
104 /* is no lib link block, but light extension */
105 if (la->nodetree) {
107 MEM_delete(la->nodetree);
108 la->nodetree = nullptr;
109 }
110
111 BKE_previewimg_id_free(&la->id);
112 BKE_icon_id_delete(&la->id);
113 la->id.icon_id = 0;
114}
115
117{
118 Light *lamp = reinterpret_cast<Light *>(id);
119
120 if (lamp->nodetree) {
121 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
123 data, BKE_library_foreach_ID_embedded(data, (ID **)&lamp->nodetree));
124 }
125}
126
128{
129 Light *la = id_cast<Light *>(id);
130
131 fn.single(&la->r);
132}
133
134static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
135{
136 Light *la = id_cast<Light *>(id);
137
138 /* Forward compatibility for energy. */
139 la->energy_deprecated = la->energy * exp2f(la->exposure);
140 if (la->type == LA_AREA) {
141 la->energy_deprecated /= M_PI_4;
142 }
143
144 /* Forward compatibility for Use Nodes. */
145 la->use_nodes = true;
146
147 /* write LibData */
148 writer->write_id_struct(id_address, la);
149 BKE_id_blend_write(writer, &la->id);
150
151 /* Node-tree is integral part of lights, no libdata. */
152 if (la->nodetree) {
153 BLO_Write_IDBuffer temp_embedded_id_buffer{la->nodetree->id, writer};
154 bNodeTree *temp_ntree = reinterpret_cast<bNodeTree *>(temp_embedded_id_buffer.get());
155 writer->write_embedded_id_struct(la->nodetree, temp_ntree);
156 bke::node_tree_blend_write(writer, temp_ntree);
157 }
158
159 BKE_previewimg_blend_write(writer, la->preview);
160}
161
162static void light_blend_read_data(BlendDataReader *reader, ID *id)
163{
164 Light *la = id_cast<Light *>(id);
165
166 BLO_read_struct(reader, PreviewImage, &la->preview);
167 BKE_previewimg_blend_read(reader, la->preview);
168}
169
171 .id_code = Light::id_type,
172 .id_filter = FILTER_ID_LA,
173 .dependencies_id_types = FILTER_ID_TE,
174 .main_listbase_index = INDEX_ID_LA,
175 .struct_size = sizeof(Light),
176 .name = "Light",
177 .name_plural = N_("lights"),
178 .translation_context = BLT_I18NCONTEXT_ID_LIGHT,
180 .asset_type_info = nullptr,
181
182 .init_data = light_init_data,
183 .copy_data = light_copy_data,
184 .free_data = light_free_data,
185 .make_local = nullptr,
186 .foreach_id = light_foreach_id,
187 .foreach_cache = nullptr,
188 .foreach_path = nullptr,
189 .foreach_working_space_color = light_foreach_working_space_color,
190 .foreach_asset_weak_reference = nullptr,
191 .owner_pointer_get = nullptr,
192
193 .blend_write = light_blend_write,
194 .blend_read_data = light_blend_read_data,
195 .blend_read_after_liblink = nullptr,
196
197 .blend_read_undo_preserve = nullptr,
198
199 .lib_override_apply_post = nullptr,
200};
201
202Light *BKE_light_add(Main *bmain, const char *name)
203{
204 Light *la;
205
206 la = BKE_id_new<Light>(bmain, name);
207
208 nodes::node_tree_shader_default(nullptr, bmain, &la->id);
209
210 return la;
211}
212
213void BKE_light_eval(Depsgraph *depsgraph, Light *la)
214{
215 DEG_debug_print_eval(depsgraph, __func__, la->id.name, la);
216}
217
218float BKE_light_power(const Light &light)
219{
220 return light.energy * exp2f(light.exposure);
221}
222
224{
225 float3 color(&light.r);
226
227 if (light.mode & LA_USE_TEMPERATURE) {
228 float temperature_color[4];
229 IMB_colormanagement_blackbody_temperature_to_rgb(temperature_color, light.temperature);
230 color *= float3(temperature_color);
231 }
232
233 return color;
234}
235
236float BKE_light_area(const Light &light, const float4x4 &object_to_world)
237{
238 /* Make illumination power constant. */
239 switch (light.type) {
240 case LA_AREA: {
241 /* Rectangle area. */
242 const float3x3 scalemat = object_to_world.view<3, 3>();
243 const float3 scale = math::to_scale(scalemat);
244
245 const float size_x = light.area_size * scale.x;
246 const float size_y = (ELEM(light.area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE) ?
247 light.area_sizey :
248 light.area_size) *
249 scale.y;
250
251 float area = size_x * size_y;
252 /* Scale for smaller area of the ellipse compared to the surrounding rectangle. */
253 if (ELEM(light.area_shape, LA_AREA_DISK, LA_AREA_ELLIPSE)) {
254 area *= float(M_PI / 4.0f);
255 }
256 return area;
257 }
258 case LA_LOCAL:
259 case LA_SPOT: {
260 /* Sphere area. For legacy reasons object scale is not taken into account
261 * here, even though logically it should be. */
262 const float radius = light.radius;
263 return (radius > 0.0f) ? float(4.0f * M_PI) * math::square(radius) : 4.0f;
264 }
265 case LA_SUN: {
266 /* Sun disk area. */
267 const float angle = light.sun_angle / 2.0f;
268 return (angle > 0.0f) ? float(M_PI) * math::square(sinf(angle)) : 1.0f;
269 }
270 }
271
273 return 1.0f;
274}
275
276} // namespace blender
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
General operations, lookup, etc. for blender lights.
#define M_PI
#define M_PI_4
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_LIGHT
#define FILTER_ID_LA
Definition DNA_ID.h:1207
#define FILTER_ID_TE
Definition DNA_ID.h:1222
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
float3x3 scale(float3x3 mat, float2 scale)
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:1361
bNodeTree * node_tree_localize(bNodeTree *ntree, std::optional< ID * > new_owner_id)
Definition node.cc:5355
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:5231
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
T square(const T &a)
void node_tree_shader_default(const bContext *C, Main *bmain, ID *id)
BMesh Iterators.
static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
void BKE_icon_id_delete(ID *id)
Definition icons.cc:524
void BKE_previewimg_id_free(ID *id)
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
static void light_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
float BKE_light_power(const Light &light)
MatBase< float, 4, 4 > float4x4
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_ALLOCATE
BMesh const char void * data
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:176
void IMB_colormanagement_blackbody_temperature_to_rgb(float r_dest[4], float value)
MatBase< float, 3, 3 > float3x3
static void light_init_data(ID *id)
static void light_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
static void light_free_data(ID *id)
IDTypeInfo IDType_ID_LA
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
static void light_foreach_id(ID *id, LibraryForeachIDData *data)
float3 BKE_light_color(const Light &light)
Light * BKE_light_add(Main *bmain, const char *name) ATTR_WARN_UNUSED_RESULT
@ INDEX_ID_LA
Definition DNA_ID.h:1334
static void light_blend_read_data(BlendDataReader *reader, ID *id)
float BKE_light_area(const Light &light, const float4x4 &object_to_world)
VecBase< float, 3 > float3
void BKE_light_eval(Depsgraph *depsgraph, Light *la)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
#define sinf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void write_embedded_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
#define N_(msgid)