Blender V4.5
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);
119
120 if (lamp->nodetree) {
121 /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
124 }
125
128 }
129}
130
131static void light_blend_write(BlendWriter *writer, ID *id, const void *id_address)
132{
133 Light *la = (Light *)id;
134
135 /* Forward compatibility for energy. */
136 la->energy_deprecated = la->energy * exp2f(la->exposure);
137 if (la->type == LA_AREA) {
138 la->energy_deprecated /= M_PI_4;
139 }
140
141 /* write LibData */
142 BLO_write_id_struct(writer, Light, id_address, &la->id);
143 BKE_id_blend_write(writer, &la->id);
144
145 /* Node-tree is integral part of lights, no libdata. */
146 if (la->nodetree) {
147 BLO_Write_IDBuffer temp_embedded_id_buffer{la->nodetree->id, writer};
148 BLO_write_struct_at_address(writer, bNodeTree, la->nodetree, temp_embedded_id_buffer.get());
150 writer, reinterpret_cast<bNodeTree *>(temp_embedded_id_buffer.get()));
151 }
152
154}
155
156static void light_blend_read_data(BlendDataReader *reader, ID *id)
157{
158 Light *la = (Light *)id;
159
160 BLO_read_struct(reader, PreviewImage, &la->preview);
162}
163
165 /*id_code*/ Light::id_type,
166 /*id_filter*/ FILTER_ID_LA,
167 /*dependencies_id_types*/ FILTER_ID_TE,
168 /*main_listbase_index*/ INDEX_ID_LA,
169 /*struct_size*/ sizeof(Light),
170 /*name*/ "Light",
171 /*name_plural*/ N_("lights"),
172 /*translation_context*/ BLT_I18NCONTEXT_ID_LIGHT,
174 /*asset_type_info*/ nullptr,
175
176 /*init_data*/ light_init_data,
177 /*copy_data*/ light_copy_data,
178 /*free_data*/ light_free_data,
179 /*make_local*/ nullptr,
180 /*foreach_id*/ light_foreach_id,
181 /*foreach_cache*/ nullptr,
182 /*foreach_path*/ nullptr,
183 /*owner_pointer_get*/ nullptr,
184
185 /*blend_write*/ light_blend_write,
186 /*blend_read_data*/ light_blend_read_data,
187 /*blend_read_after_liblink*/ nullptr,
188
189 /*blend_read_undo_preserve*/ nullptr,
190
191 /*lib_override_apply_post*/ nullptr,
192};
193
194Light *BKE_light_add(Main *bmain, const char *name)
195{
196 Light *la;
197
198 la = BKE_id_new<Light>(bmain, name);
199
200 return la;
201}
202
203void BKE_light_eval(Depsgraph *depsgraph, Light *la)
204{
205 DEG_debug_print_eval(depsgraph, __func__, la->id.name, la);
206}
207
208float BKE_light_power(const Light &light)
209{
210 return light.energy * exp2f(light.exposure);
211}
212
214{
215 blender::float3 color(&light.r);
216
217 if (light.mode & LA_USE_TEMPERATURE) {
218 float temperature_color[4];
220 color *= blender::float3(temperature_color);
221 }
222
223 return color;
224}
225
226float BKE_light_area(const Light &light, const blender::float4x4 &object_to_world)
227{
228 /* Make illumination power constant. */
229 switch (light.type) {
230 case LA_AREA: {
231 /* Rectangle area. */
232 const blender::float3x3 scalemat = object_to_world.view<3, 3>();
233 const blender::float3 scale = blender::math::to_scale(scalemat);
234
235 const float size_x = light.area_size * scale.x;
236 const float size_y = (ELEM(light.area_shape, LA_AREA_RECT, LA_AREA_ELLIPSE) ?
237 light.area_sizey :
238 light.area_size) *
239 scale.y;
240
241 float area = size_x * size_y;
242 /* Scale for smaller area of the ellipse compared to the surrounding rectangle. */
244 area *= float(M_PI / 4.0f);
245 }
246 return area;
247 }
248 case LA_LOCAL:
249 case LA_SPOT: {
250 /* Sphere area. For legacy reasons object scale is not taken into account
251 * here, even though logically it should be. */
252 const float radius = light.radius;
253 return (radius > 0.0f) ? float(4.0f * M_PI) * blender::math::square(radius) : 4.0f;
254 }
255 case LA_SUN: {
256 /* Sun disk area. */
257 const float angle = light.sun_angle / 2.0f;
258 return (angle > 0.0f) ? float(M_PI) * blender::math::square(sinf(angle)) : 1.0f;
259 }
260 }
261
263 return 1.0f;
264}
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:44
@ LIB_ID_CREATE_NO_ALLOCATE
@ LIB_ID_COPY_NO_PREVIEW
@ LIB_ID_CREATE_LOCAL
@ LIB_ID_CREATE_NO_USER_REFCOUNT
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:663
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1495
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2611
#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
@ IDWALK_CB_USER
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:129
@ IDWALK_DO_DEPRECATED_POINTERS
#define BKE_LIB_FOREACHID_PROCESS_ID_NOCHECK(data_, id_, cb_flag_)
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)
@ INDEX_ID_LA
Definition DNA_ID.h:1241
#define DNA_struct_default_get(struct_name)
@ LA_AREA_ELLIPSE
@ LA_AREA_RECT
@ LA_AREA_DISK
@ LA_USE_TEMPERATURE
@ LA_AREA
@ LA_LOCAL
@ LA_SPOT
@ LA_SUN
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)
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
#define sinf(x)
#define FILTER_ID_LA
#define FILTER_ID_TE
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_tree_blend_write(BlendWriter *writer, bNodeTree *ntree)
Definition node.cc:1464
bNodeTree * node_tree_localize(bNodeTree *ntree, std::optional< ID * > new_owner_id)
Definition node.cc:4858
void node_tree_free_embedded_tree(bNodeTree *ntree)
Definition node.cc:4724
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
Definition DNA_ID.h:404
int icon_id
Definition DNA_ID.h:426
char name[66]
Definition DNA_ID.h:415
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:139