Blender V5.0
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_defaults.h"
18#include "DNA_light_types.h"
19#include "DNA_node_types.h"
20#include "DNA_scene_types.h"
21
22#include "BLI_math_base.hh"
23#include "BLI_math_matrix.hh"
25#include "BLI_utildefines.h"
26
27#include "BKE_icons.h"
28#include "BKE_idtype.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_lib_query.hh"
31#include "BKE_light.h"
32#include "BKE_node.hh"
33#include "BKE_preview_image.hh"
34
35#include "BLT_translation.hh"
36
37#include "DEG_depsgraph.hh"
38
40
41#include "BLO_read_write.hh"
42
43static void light_init_data(ID *id)
44{
45 Light *la = (Light *)id;
47
49}
50
61static void light_copy_data(Main *bmain,
62 std::optional<Library *> owner_library,
63 ID *id_dst,
64 const ID *id_src,
65 const int flag)
66{
67 Light *la_dst = (Light *)id_dst;
68 const Light *la_src = (const Light *)id_src;
69
70 const bool is_localized = (flag & LIB_ID_CREATE_LOCAL) != 0;
71 /* We always need allocation of our private ID data.
72 * User reference-counting is also handled by calling code,
73 * so the duplication calls for embedded data should _never_ handle it from here. */
74 const int flag_embedded_id_data = (flag & ~LIB_ID_CREATE_NO_ALLOCATE) |
76
77 if (la_src->nodetree) {
78 if (is_localized) {
79 la_dst->nodetree = blender::bke::node_tree_localize(la_src->nodetree, &la_dst->id);
80 }
81 else {
83 owner_library,
84 &la_src->nodetree->id,
85 &la_dst->id,
86 reinterpret_cast<ID **>(&la_dst->nodetree),
87 flag_embedded_id_data);
88 }
89 }
90
91 if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
92 BKE_previewimg_id_copy(&la_dst->id, &la_src->id);
93 }
94 else {
95 la_dst->preview = nullptr;
96 }
97}
98
99static void light_free_data(ID *id)
100{
101 Light *la = (Light *)id;
102
103 /* is no lib link block, but light extension */
104 if (la->nodetree) {
106 MEM_freeN(la->nodetree);
107 la->nodetree = nullptr;
108 }
109
112 la->id.icon_id = 0;
113}
114
116{
117 Light *lamp = reinterpret_cast<Light *>(id);
118
119 if (lamp->nodetree) {
120 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
123 }
124}
125
127{
128 Light *la = (Light *)id;
129
130 fn.single(&la->r);
131}
132
133static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
134{
135 Light *la = (Light *)id;
136
137 /* Forward compatibility for energy. */
138 la->energy_deprecated = la->energy * exp2f(la->exposure);
139 if (la->type == LA_AREA) {
140 la->energy_deprecated /= M_PI_4;
141 }
142
143 /* write LibData */
144 BLO_write_id_struct(writer, Light, id_address, &la->id);
145 BKE_id_blend_write(writer, &la->id);
146
147 /* Node-tree is integral part of lights, no libdata. */
148 if (la->nodetree) {
149 BLO_Write_IDBuffer temp_embedded_id_buffer{la->nodetree->id, writer};
150 BLO_write_struct_at_address(writer, bNodeTree, la->nodetree, temp_embedded_id_buffer.get());
152 writer, reinterpret_cast<bNodeTree *>(temp_embedded_id_buffer.get()));
153 }
154
156}
157
158static void light_blend_read_data(BlendDataReader *reader, ID *id)
159{
160 Light *la = (Light *)id;
161
162 BLO_read_struct(reader, PreviewImage, &la->preview);
164}
165
167 /*id_code*/ Light::id_type,
168 /*id_filter*/ FILTER_ID_LA,
169 /*dependencies_id_types*/ FILTER_ID_TE,
170 /*main_listbase_index*/ INDEX_ID_LA,
171 /*struct_size*/ sizeof(Light),
172 /*name*/ "Light",
173 /*name_plural*/ N_("lights"),
174 /*translation_context*/ BLT_I18NCONTEXT_ID_LIGHT,
176 /*asset_type_info*/ nullptr,
177
178 /*init_data*/ light_init_data,
179 /*copy_data*/ light_copy_data,
180 /*free_data*/ light_free_data,
181 /*make_local*/ nullptr,
182 /*foreach_id*/ light_foreach_id,
183 /*foreach_cache*/ nullptr,
184 /*foreach_path*/ nullptr,
185 /*foreach_working_space_color*/ light_foreach_working_space_color,
186 /*owner_pointer_get*/ nullptr,
187
188 /*blend_write*/ light_blend_write,
189 /*blend_read_data*/ light_blend_read_data,
190 /*blend_read_after_liblink*/ nullptr,
191
192 /*blend_read_undo_preserve*/ nullptr,
193
194 /*lib_override_apply_post*/ nullptr,
195};
196
197Light *BKE_light_add(Main *bmain, const char *name)
198{
199 Light *la;
200
201 la = BKE_id_new<Light>(bmain, name);
202
203 return la;
204}
205
206void BKE_light_eval(Depsgraph *depsgraph, Light *la)
207{
208 DEG_debug_print_eval(depsgraph, __func__, la->id.name, la);
209}
210
211float BKE_light_power(const Light &light)
212{
213 return light.energy * exp2f(light.exposure);
214}
215
217{
218 blender::float3 color(&light.r);
219
220 if (light.mode & LA_USE_TEMPERATURE) {
221 float temperature_color[4];
223 color *= blender::float3(temperature_color);
224 }
225
226 return color;
227}
228
229float BKE_light_area(const Light &light, const blender::float4x4 &object_to_world)
230{
231 /* Make illumination power constant. */
232 switch (light.type) {
233 case LA_AREA: {
234 /* Rectangle area. */
235 const blender::float3x3 scalemat = object_to_world.view<3, 3>();
236 const blender::float3 scale = blender::math::to_scale(scalemat);
237
238 const float size_x = light.area_size * scale.x;
239 const float size_y = (ELEM(light.area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE) ?
240 light.area_sizey :
241 light.area_size) *
242 scale.y;
243
244 float area = size_x * size_y;
245 /* Scale for smaller area of the ellipse compared to the surrounding rectangle. */
247 area *= float(M_PI / 4.0f);
248 }
249 return area;
250 }
251 case LA_LOCAL:
252 case LA_SPOT: {
253 /* Sphere area. For legacy reasons object scale is not taken into account
254 * here, even though logically it should be. */
255 const float radius = light.radius;
256 return (radius > 0.0f) ? float(4.0f * M_PI) * blender::math::square(radius) : 4.0f;
257 }
258 case LA_SUN: {
259 /* Sun disk area. */
260 const float angle = light.sun_angle / 2.0f;
261 return (angle > 0.0f) ? float(M_PI) * blender::math::square(sinf(angle)) : 1.0f;
262 }
263 }
264
266 return 1.0f;
267}
void BKE_icon_id_delete(struct ID *id)
Definition icons.cc:437
IDTypeInfo IDType_ID_LA
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:47
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:675
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1514
@ LIB_ID_CREATE_NO_ALLOCATE
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_USER_REFCOUNT
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2631
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
void BKE_library_foreach_ID_embedded(LibraryForeachIDData *data, ID **id_pp)
Definition lib_query.cc:172
General operations, lookup, etc. for blender lights.
void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
void BKE_previewimg_free(PreviewImage **prv)
void BKE_previewimg_blend_read(BlendDataReader *reader, PreviewImage *prv)
void BKE_previewimg_id_copy(ID *new_id, const ID *old_id)
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define M_PI
#define M_PI_4
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLO_write_struct_at_address(writer, struct_name, address, data_ptr)
#define BLT_I18NCONTEXT_ID_LIGHT
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
#define FILTER_ID_LA
Definition DNA_ID.h:1205
#define FILTER_ID_TE
Definition DNA_ID.h:1220
@ INDEX_ID_LA
Definition DNA_ID.h:1332
#define DNA_struct_default_get(struct_name)
@ LA_AREA
@ LA_LOCAL
@ LA_SPOT
@ LA_SUN
@ LA_USE_TEMPERATURE
@ LA_AREA_ELLIPSE
@ LA_AREA_RECT
@ LA_AREA_DISK
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
void IMB_colormanagement_blackbody_temperature_to_rgb(float r_dest[4], float value)
Read Guarded memory(de)allocation.
static void light_foreach_id(ID *id, LibraryForeachIDData *data)
blender::float3 BKE_light_color(const Light &light)
void BKE_light_eval(Depsgraph *depsgraph, Light *la)
static void light_blend_read_data(BlendDataReader *reader, ID *id)
static void light_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
static void light_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
float BKE_light_area(const Light &light, const blender::float4x4 &object_to_world)
static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
static void light_free_data(ID *id)
float BKE_light_power(const Light &light)
Light * BKE_light_add(Main *bmain, const char *name)
static void light_init_data(ID *id)
BMesh const char void * data
BPy_StructRNA * depsgraph
nullptr float
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:1140
bNodeTree * node_tree_localize(bNodeTree *ntree, std::optional< ID * > new_owner_id)
Definition node.cc:4586
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:4462
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
T square(const T &a)
MatBase< float, 4, 4 > float4x4
MatBase< float, 3, 3 > float3x3
VecBase< float, 3 > float3
const char * name
#define sinf
const blender::FunctionRef< void(float rgb[3])> single
Definition DNA_ID.h:414
char name[258]
Definition DNA_ID.h:432
int icon_id
Definition DNA_ID.h:444
float sun_angle
float energy
struct PreviewImage * preview
float temperature
float area_sizey
short area_shape
float exposure
struct bNodeTree * nodetree
float radius
float area_size
short type
const MatView< T, ViewNumCol, ViewNumRow, NumCol, NumRow, SrcStartCol, SrcStartRow, Alignment > view() const
#define N_(msgid)
uint8_t flag
Definition wm_window.cc:145