Blender V5.3
studiolight.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006-2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_studiolight.h"
10
11#include "BKE_appdir.hh"
12#include "BKE_icons.hh"
13
14#include "BLI_dynstr.hh"
15#include "BLI_fileops.hh"
16#include "BLI_fileops_types.hh"
17#include "BLI_linklist.hh"
18#include "BLI_listbase.hh"
19#include "BLI_math_color_c.hh"
20#include "BLI_math_vector_c.hh"
21#include "BLI_path_utils.hh"
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24
25#include "CLG_log.h"
26
27#include "DNA_color_types.h"
28#include "DNA_listBase.h"
29
31#include "IMB_imbuf.hh"
32#include "IMB_interp.hh"
33#include "IMB_openexr.hh"
34
35#include "GPU_texture.hh"
36
37#include "MEM_guardedalloc.h"
38
39#include <cstring>
40
41namespace blender {
42
43/* Statics */
44static ListBaseT<StudioLight> studiolights;
45static int last_studiolight_id = 0;
46#define STUDIOLIGHT_PASSNAME_DIFFUSE "diffuse"
47#define STUDIOLIGHT_PASSNAME_SPECULAR "specular"
48
49static const char *STUDIOLIGHT_LIGHTS_FOLDER = "studiolights" SEP_STR "studio" SEP_STR;
50static const char *STUDIOLIGHT_WORLD_FOLDER = "studiolights" SEP_STR "world" SEP_STR;
51static const char *STUDIOLIGHT_MATCAP_FOLDER = "studiolights" SEP_STR "matcap" SEP_STR;
52
53static const char *STUDIOLIGHT_WORLD_DEFAULT = "forest.exr";
54static const char *STUDIOLIGHT_MATCAP_DEFAULT = "basic_1.exr";
55
56/* ITER MACRO */
57
71#define ITER_PIXELS(type, src, channels, width, height) \
72 { \
73 float texel_size[2]; \
74 texel_size[0] = 1.0f / width; \
75 texel_size[1] = 1.0f / height; \
76 type(*pixel_)[channels] = (type(*)[channels])src; \
77 for (float y = 0.5 * texel_size[1]; y < 1.0; y += texel_size[1]) { \
78 for (float x = 0.5 * texel_size[0]; x < 1.0; x += texel_size[0], pixel_++) { \
79 type *pixel = *pixel_;
80
81#define ITER_PIXELS_END \
82 } \
83 } \
84 } \
85 ((void)0)
86
87/* FUNCTIONS */
88#define IMB_SAFE_FREE(p) \
89 do { \
90 if (p) { \
91 IMB_freeImBuf(p); \
92 p = nullptr; \
93 } \
94 } while (0)
95
96#define GPU_TEXTURE_SAFE_FREE(p) \
97 do { \
98 if (p) { \
99 GPU_texture_free(p); \
100 p = nullptr; \
101 } \
102 } while (0)
103
111
120
122{
123#define STUDIOLIGHT_DELETE_ICON(s) \
124 do { \
125 if (s != 0) { \
126 BKE_icon_delete(s); \
127 s = 0; \
128 } \
129 } while (0)
130
131 if (sl->free_function) {
133 }
138#undef STUDIOLIGHT_DELETE_ICON
139
142
143 MEM_SAFE_DELETE(sl);
144}
145
152{
153 const bool is_used_in_viewport = bool(sl->flag & (STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE |
156 if (is_used_in_viewport) {
157 return;
158 }
160}
161
163{
164 StudioLight *sl = MEM_new<StudioLight>(__func__);
165 sl->filepath[0] = 0x00;
166 sl->name[0] = 0x00;
167 sl->free_function = nullptr;
168 sl->flag = flag;
170 if (flag & STUDIOLIGHT_TYPE_STUDIO) {
172 }
173 else if (flag & STUDIOLIGHT_TYPE_MATCAP) {
177 }
178 else {
180 }
181
182 return sl;
183}
184
185#define STUDIOLIGHT_FILE_VERSION 1
186
187#define READ_VAL(type, parser, id, val, lines) \
188 do { \
189 for (LinkNode *line = lines; line; line = line->next) { \
190 char *val_str, *str = static_cast<char *>(line->link); \
191 if ((val_str = strstr(str, id " "))) { \
192 val_str += sizeof(id); /* Skip id + spacer. */ \
193 val = parser(val_str); \
194 } \
195 } \
196 } while (0)
197
198#define READ_FVAL(id, val, lines) READ_VAL(float, atof, id, val, lines)
199#define READ_IVAL(id, val, lines) READ_VAL(int, atoi, id, val, lines)
200
201#define READ_VEC3(id, val, lines) \
202 do { \
203 READ_FVAL(id ".x", val[0], lines); \
204 READ_FVAL(id ".y", val[1], lines); \
205 READ_FVAL(id ".z", val[2], lines); \
206 } while (0)
207
208#define READ_SOLIDLIGHT(sl, i, lines) \
209 do { \
210 READ_IVAL("light[" STRINGIFY(i) "].flag", sl[i].flag, lines); \
211 READ_FVAL("light[" STRINGIFY(i) "].smooth", sl[i].smooth, lines); \
212 READ_VEC3("light[" STRINGIFY(i) "].col", sl[i].col, lines); \
213 READ_VEC3("light[" STRINGIFY(i) "].spec", sl[i].spec, lines); \
214 READ_VEC3("light[" STRINGIFY(i) "].vec", sl[i].vec, lines); \
215 } while (0)
216
218{
220 if (lines) {
221 READ_VEC3("light_ambient", sl->light_ambient, lines);
222 READ_SOLIDLIGHT(sl->light, 0, lines);
223 READ_SOLIDLIGHT(sl->light, 1, lines);
224 READ_SOLIDLIGHT(sl->light, 2, lines);
225 READ_SOLIDLIGHT(sl->light, 3, lines);
226 }
227 BLI_file_free_lines(lines);
228}
229
230#undef READ_SOLIDLIGHT
231#undef READ_VEC3
232#undef READ_IVAL
233#undef READ_FVAL
234
235#define WRITE_FVAL(str, id, val) BLI_dynstr_appendf(str, id " %f\n", val)
236#define WRITE_IVAL(str, id, val) BLI_dynstr_appendf(str, id " %d\n", val)
237
238#define WRITE_VEC3(str, id, val) \
239 do { \
240 WRITE_FVAL(str, id ".x", val[0]); \
241 WRITE_FVAL(str, id ".y", val[1]); \
242 WRITE_FVAL(str, id ".z", val[2]); \
243 } while (0)
244
245#define WRITE_SOLIDLIGHT(str, sl, i) \
246 do { \
247 WRITE_IVAL(str, "light[" STRINGIFY(i) "].flag", sl[i].flag); \
248 WRITE_FVAL(str, "light[" STRINGIFY(i) "].smooth", sl[i].smooth); \
249 WRITE_VEC3(str, "light[" STRINGIFY(i) "].col", sl[i].col); \
250 WRITE_VEC3(str, "light[" STRINGIFY(i) "].spec", sl[i].spec); \
251 WRITE_VEC3(str, "light[" STRINGIFY(i) "].vec", sl[i].vec); \
252 } while (0)
253
255{
256 FILE *fp = BLI_fopen(sl->filepath, "wb");
257 if (fp) {
259
260 /* Very dumb ASCII format. One value per line separated by a space. */
262 WRITE_VEC3(str, "light_ambient", sl->light_ambient);
263 WRITE_SOLIDLIGHT(str, sl->light, 0);
264 WRITE_SOLIDLIGHT(str, sl->light, 1);
265 WRITE_SOLIDLIGHT(str, sl->light, 2);
266 WRITE_SOLIDLIGHT(str, sl->light, 3);
267
268 char *cstr = BLI_dynstr_get_cstring(str);
269
270 fwrite(cstr, BLI_dynstr_get_len(str), 1, fp);
271 fclose(fp);
272
273 MEM_delete(cstr);
275 }
276}
277
278#undef WRITE_SOLIDLIGHT
279#undef WRITE_VEC3
280#undef WRITE_IVAL
281#undef WRITE_FVAL
282
283static void direction_to_equirect(float r[2], const float dir[3])
284{
285 r[0] = (atan2f(dir[1], dir[0]) - M_PI) / -(M_PI * 2);
286 r[1] = (acosf(dir[2] / 1.0) - M_PI) / -M_PI;
287}
288
294 const char *pass_name,
295 const char *file_colorspace)
296{
298 for (ExrPassInfo &info : passes) {
299 if (info.pass != pass_name) {
300 continue;
301 }
302 MutableSpan<ExrPassInfo> single(&info, 1);
303 IMB_exr_read_passes(handle, single, file_colorspace, false);
304 ImBuf *pass_ibuf = info.ibuf;
305 if (pass_ibuf == nullptr || pass_ibuf->channels == 4) {
306 return pass_ibuf;
307 }
308 /* Expand to 4 channels for matcaps. */
309 ImBuf *rgba_ibuf = IMB_allocImBuf(pass_ibuf->x, pass_ibuf->y, ImBufFlags::FloatData);
310 if (rgba_ibuf) {
312 pass_ibuf->float_data(),
313 pass_ibuf->channels,
314 pass_ibuf->x,
315 pass_ibuf->y);
316 }
317 IMB_freeImBuf(pass_ibuf);
318 return rgba_ibuf;
319 }
320 return nullptr;
321}
322
324{
326 ColorManagedColorspaceSettings colorspace_settings{};
328 sl->filepath, ImBufFlags::MultiLayer | ImBufFlags::AlphaIgnore, &colorspace_settings);
329 ImBuf *specular_ibuf = nullptr;
330 ImBuf *diffuse_ibuf = nullptr;
331 const bool failed = (ibuf == nullptr);
332
333 if (ibuf) {
334 ExrReadHandle *exr_handle = flag_is_set(ibuf->flags, ImBufFlags::MultiLayer) ?
336 nullptr;
337
338 if (exr_handle) {
339 /* The read file is a multi-layer OpenEXR file. This is currently only
340 * supported for MATCAPS where the first found 'diffuse' pass will be used
341 * for diffuse lighting and the first found 'specular' pass will be used
342 * for specular lighting. */
343 const char *file_colorspace = colorspace_settings.name;
344 diffuse_ibuf = studiolight_read_matcap_pass(
345 exr_handle, STUDIOLIGHT_PASSNAME_DIFFUSE, file_colorspace);
346 specular_ibuf = studiolight_read_matcap_pass(
347 exr_handle, STUDIOLIGHT_PASSNAME_SPECULAR, file_colorspace);
348
349 IMB_exr_close(exr_handle);
350 IMB_freeImBuf(ibuf);
351 ibuf = nullptr;
352 }
353 else if (!flag_is_set(ibuf->flags, ImBufFlags::MultiLayer)) {
354 /* read file is an single layer openexr file or the read file isn't
355 * an openexr file */
357 diffuse_ibuf = ibuf;
358 ibuf = nullptr;
359 }
360 else {
361 IMB_freeImBuf(ibuf);
362 ibuf = nullptr;
363 }
364 }
365
366 if (diffuse_ibuf == nullptr) {
367 /* Create 1x1 diffuse buffer, in case image failed to load or if there was
368 * only a specular pass in the multilayer file or no passes were found. */
369 const float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
370 const float magenta[4] = {1.0f, 0.0f, 1.0f, 1.0f};
371 diffuse_ibuf = IMB_allocFromBuffer(
372 nullptr, (failed || (specular_ibuf == nullptr)) ? magenta : black, 1, 1, 4);
373 }
374
375 if (sl->flag & STUDIOLIGHT_TYPE_MATCAP) {
376 sl->matcap_diffuse.ibuf = diffuse_ibuf;
377 sl->matcap_specular.ibuf = specular_ibuf;
378 if (specular_ibuf != nullptr) {
380 }
381 }
382 else {
383 sl->equirect_radiance_buffer = diffuse_ibuf;
384 if (specular_ibuf != nullptr) {
385 IMB_freeImBuf(specular_ibuf);
386 }
387 }
388 }
389
391}
392
394{
398
400 "studiolight_radiance",
401 ibuf->x,
402 ibuf->y,
403 1,
404 gpu::TextureFormat::SFLOAT_16_16_16_16,
406 ibuf->float_data());
408 GPU_texture_filter_mode(tex, true);
410 }
412}
413
415{
416 BLI_assert(sli->ibuf);
417 ImBuf *ibuf = sli->ibuf;
418 const size_t ibuf_pixel_count = IMB_get_pixel_count(ibuf);
419 float *gpu_matcap_3components = MEM_new_array_zeroed<float>(3 * ibuf_pixel_count, __func__);
420
421 const float (*offset4)[4] = reinterpret_cast<const float (*)[4]>(ibuf->float_data());
422 float (*offset3)[3] = reinterpret_cast<float (*)[3]>(gpu_matcap_3components);
423 for (size_t i = 0; i < ibuf_pixel_count; i++, offset4++, offset3++) {
424 copy_v3_v3(*offset3, *offset4);
425 }
426
427 sli->gputexture = GPU_texture_create_2d("matcap",
428 ibuf->x,
429 ibuf->y,
430 1,
431 gpu::TextureFormat::UFLOAT_11_11_10,
433 nullptr);
434 GPU_texture_update(sli->gputexture, GPU_DATA_FLOAT, gpu_matcap_3components);
435
436 MEM_SAFE_DELETE(gpu_matcap_3components);
437}
438
461
462static float4 studiolight_calculate_radiance(const ImBuf *ibuf, const float direction[3])
463{
464 float uv[2];
465 direction_to_equirect(uv, direction);
466 return imbuf::interpolate_nearest_border_fl(ibuf, uv[0] * ibuf->x, uv[1] * ibuf->y);
467}
468
469/*
470 * Spherical Harmonics
471 */
472BLI_INLINE float area_element(float x, float y)
473{
474 return atan2(x * y, sqrtf(x * x + y * y + 1));
475}
476
477static float brdf_approx(float spec_color, float roughness, float NV)
478{
479 /* Very rough approximation. We don't need it to be correct, just fast.
480 * Just simulate fresnel effect with roughness attenuation. */
481 float fresnel = exp2(-8.35f * NV) * (1.0f - roughness);
482 return spec_color * (1.0f - fresnel) + fresnel;
483}
484
485/* NL need to be unclamped. w in [0..1] range. */
486static float wrapped_lighting(float NL, float w)
487{
488 float w_1 = w + 1.0f;
489 return max_ff((NL + w) / (w_1 * w_1), 0.0f);
490}
491
492static float blinn_specular(const float L[3],
493 const float I[3],
494 const float N[3],
495 const float R[3],
496 float NL,
497 float roughness,
498 float wrap)
499{
500 float half_dir[3];
501 float wrapped_NL = dot_v3v3(L, R);
502 add_v3_v3v3(half_dir, L, I);
503 normalize_v3(half_dir);
504 float spec_angle = max_ff(dot_v3v3(half_dir, N), 0.0f);
505
506 float gloss = 1.0f - roughness;
507 /* Reduce gloss for smooth light. (simulate bigger light) */
508 gloss *= 1.0f - wrap;
509 float shininess = exp2(10.0f * gloss + 1.0f);
510
511 /* Pi is already divided in the light power.
512 * normalization_factor = (shininess + 8.0) / (8.0 * M_PI) */
513 float normalization_factor = shininess * 0.125f + 1.0f;
514 float spec_light = powf(spec_angle, shininess) * max_ff(NL, 0.0f) * normalization_factor;
515
516 /* Simulate Env. light. */
517 float w = wrap * (1.0 - roughness) + roughness;
518 float spec_env = wrapped_lighting(wrapped_NL, w);
519
520 float w2 = wrap * wrap;
521
522 return spec_light * (1.0 - w2) + spec_env * w2;
523}
524
525/* Keep in sync with the GLSL shader function `get_world_lighting()`. */
526static void studiolight_lights_eval(StudioLight *sl, const float normal[3], float r_color[3])
527{
528 float R[3], I[3] = {0.0f, 0.0f, 1.0f}, N[3] = {normal[0], normal[2], -normal[1]};
529 const float roughness = 0.5f;
530 const float diffuse_color = 0.8f;
531 const float specular_color = brdf_approx(0.05f, roughness, N[2]);
532 float diff_light[3], spec_light[3];
533
534 /* Ambient lighting */
535 copy_v3_v3(diff_light, sl->light_ambient);
536 copy_v3_v3(spec_light, sl->light_ambient);
537
538 reflect_v3_v3v3(R, I, N);
539 for (int i = 0; i < STUDIOLIGHT_MAX_LIGHT; i++) {
540 SolidLight *light = &sl->light[i];
541 if (light->flag) {
542 /* Diffuse lighting */
543 float NL = dot_v3v3(light->vec, N);
544 float diff = wrapped_lighting(NL, light->smooth);
545 madd_v3_v3fl(diff_light, light->col, diff);
546 /* Specular lighting */
547 float spec = blinn_specular(light->vec, I, N, R, NL, roughness, light->smooth);
548 madd_v3_v3fl(spec_light, light->spec, spec);
549 }
550 }
551
552 /* Multiply result by surface colors. */
553 mul_v3_fl(diff_light, diffuse_color * (1.0 - specular_color));
554 mul_v3_fl(spec_light, specular_color);
555
556 add_v3_v3v3(r_color, diff_light, spec_light);
557}
558
559static StudioLight *studiolight_add_file(const char *filepath, int flag)
560{
563
564 if ((((flag & STUDIOLIGHT_TYPE_STUDIO) != 0) && BLI_path_extension_check(filename, ".sl")) ||
566 {
568 const size_t name_len = STRNCPY_RLEN(sl->name, filename);
569 BLI_str_utf8_invalid_strip(sl->name, name_len);
570 STRNCPY(sl->filepath, filepath);
571
572 if ((flag & STUDIOLIGHT_TYPE_STUDIO) != 0) {
574 }
576 return sl;
577 }
578 return nullptr;
579}
580
581static void studiolight_add_files_from_datafolder(const int folder_id,
582 const char *subfolder,
583 int flag)
584{
585 const std::optional<std::string> folder = BKE_appdir_folder_id(folder_id, subfolder);
586 if (!folder) {
587 return;
588 }
589
590 direntry *dirs;
591 const uint dirs_num = BLI_filelist_dir_contents(folder->c_str(), &dirs);
592 int i;
593 for (i = 0; i < dirs_num; i++) {
594 if (dirs[i].type & S_IFREG) {
595 studiolight_add_file(dirs[i].path, flag);
596 }
597 }
598 BLI_filelist_free(dirs, dirs_num);
599 dirs = nullptr;
600}
601
603{
604 /* Internal studiolights before external studio lights */
606 return 1;
607 }
608 return 0;
609}
610
611static int studiolight_cmp(const void *a, const void *b)
612{
613 const StudioLight *sl1 = static_cast<const StudioLight *>(a);
614 const StudioLight *sl2 = static_cast<const StudioLight *>(b);
615
616 const int flagorder1 = studiolight_flag_cmp_order(sl1);
617 const int flagorder2 = studiolight_flag_cmp_order(sl2);
618
619 if (flagorder1 < flagorder2) {
620 return -1;
621 }
622 if (flagorder1 > flagorder2) {
623 return 1;
624 }
625
626 return BLI_strcasecmp(sl1->name, sl2->name);
627}
628
629/* icons */
630
631/* Takes normalized uvs as parameter (range from 0 to 1).
632 * inner_edge and outer_edge are distances (from the center)
633 * in uv space for the alpha mask falloff. */
634static uint alpha_circle_mask(float u, float v, float inner_edge, float outer_edge)
635{
636 /* Coords from center. */
637 const float co[2] = {u - 0.5f, v - 0.5f};
638 float dist = len_v2(co);
639 float alpha = 1.0f + (inner_edge - dist) / (outer_edge - inner_edge);
640 uint mask = uint(floorf(255.0f * min_ff(max_ff(alpha, 0.0f), 1.0f)));
641 return mask << 24;
642}
643
644/* Percentage of the icon that the preview sphere covers. */
645#define STUDIOLIGHT_DIAMETER 0.95f
646/* Rescale coord around (0.5, 0.5) by STUDIOLIGHT_DIAMETER. */
647#define RESCALE_COORD(x) (x / STUDIOLIGHT_DIAMETER - (1.0f - STUDIOLIGHT_DIAMETER) / 2.0f)
648
649/* Remaps normalized UV [0..1] to a sphere normal around (0.5, 0.5) */
650static void sphere_normal_from_uv(float normal[3], float u, float v)
651{
652 normal[0] = u * 2.0f - 1.0f;
653 normal[1] = v * 2.0f - 1.0f;
654 float dist = len_v2(normal);
655 normal[2] = sqrtf(1.0f - square_f(dist));
656}
657
658static void studiolight_radiance_preview(uint *icon_buffer, StudioLight *sl)
659{
661
663 float dy = RESCALE_COORD(y);
664 float dx = RESCALE_COORD(x);
665
666 uint alphamask = alpha_circle_mask(dx, dy, 0.5f - texel_size[0], 0.5f);
667 if (alphamask != 0) {
668 float normal[3], direction[3];
669 const float incoming[3] = {0.0f, 0.0f, -1.0f};
670 sphere_normal_from_uv(normal, dx, dy);
671 reflect_v3_v3v3(direction, incoming, normal);
672 /* We want to see horizon not poles. */
673 std::swap(direction[1], direction[2]);
674 direction[1] = -direction[1];
675
677
678 /* Buffer is in the working space. */
679 float srgb[3];
681 *pixel = rgb_to_cpack(srgb[0], srgb[1], srgb[2]) | alphamask;
682 }
683 else {
684 *pixel = 0x0;
685 }
686 }
688}
689
690static void studiolight_matcap_preview(uint *icon_buffer, StudioLight *sl, bool flipped)
691{
693
694 ImBuf *diffuse_buffer = sl->matcap_diffuse.ibuf;
695 ImBuf *specular_buffer = sl->matcap_specular.ibuf;
696
698 float dy = RESCALE_COORD(y);
699 float dx = RESCALE_COORD(x);
700 if (flipped) {
701 dx = 1.0f - dx;
702 }
703
704 float u = dx * diffuse_buffer->x - 1.0f;
705 float v = dy * diffuse_buffer->y - 1.0f;
707
708 if (specular_buffer) {
709 float4 specular = imbuf::interpolate_nearest_border_fl(specular_buffer, u, v);
710 add_v3_v3(color, specular);
711 }
712
713 uint alphamask = alpha_circle_mask(dx, dy, 0.5f - texel_size[0], 0.5f);
714
715 /* Buffers are in the working space. */
716 float srgb[3];
718 *pixel = rgb_to_cpack(srgb[0], srgb[1], srgb[2]) | alphamask;
719 }
721}
722
723static void studiolight_irradiance_preview(uint *icon_buffer, StudioLight *sl)
724{
726 float dy = RESCALE_COORD(y);
727 float dx = RESCALE_COORD(x);
728
729 uint alphamask = alpha_circle_mask(dx, dy, 0.5f - texel_size[0], 0.5f);
730 if (alphamask != 0) {
731 float normal[3], color[3];
732 sphere_normal_from_uv(normal, dx, dy);
733 /* We want to see horizon not poles. */
734 std::swap(normal[1], normal[2]);
735 normal[1] = -normal[1];
736
737 studiolight_lights_eval(sl, normal, color);
738
742 alphamask;
743 }
744 else {
745 *pixel = 0x0;
746 }
747 }
749}
750
751void BKE_studiolight_default(SolidLight lights[4], float light_ambient[3])
752{
753 copy_v3_fl3(light_ambient, 0.0, 0.0, 0.0);
754
755 lights[0].flag = 1;
756 lights[0].smooth = 0.526620f;
757 lights[0].col[0] = 0.033103f;
758 lights[0].col[1] = 0.033103f;
759 lights[0].col[2] = 0.033103f;
760 lights[0].spec[0] = 0.266761f;
761 lights[0].spec[1] = 0.266761f;
762 lights[0].spec[2] = 0.266761f;
763 lights[0].vec[0] = -0.352546f;
764 lights[0].vec[1] = 0.170931f;
765 lights[0].vec[2] = -0.920051f;
766
767 lights[1].flag = 1;
768 lights[1].smooth = 0.000000f;
769 lights[1].col[0] = 0.521083f;
770 lights[1].col[1] = 0.538226f;
771 lights[1].col[2] = 0.538226f;
772 lights[1].spec[0] = 0.599030f;
773 lights[1].spec[1] = 0.599030f;
774 lights[1].spec[2] = 0.599030f;
775 lights[1].vec[0] = -0.408163f;
776 lights[1].vec[1] = 0.346939f;
777 lights[1].vec[2] = 0.844415f;
778
779 lights[2].flag = 1;
780 lights[2].smooth = 0.478261f;
781 lights[2].col[0] = 0.038403f;
782 lights[2].col[1] = 0.034357f;
783 lights[2].col[2] = 0.049530f;
784 lights[2].spec[0] = 0.106102f;
785 lights[2].spec[1] = 0.125981f;
786 lights[2].spec[2] = 0.158523f;
787 lights[2].vec[0] = 0.521739f;
788 lights[2].vec[1] = 0.826087f;
789 lights[2].vec[2] = 0.212999f;
790
791 lights[3].flag = 1;
792 lights[3].smooth = 0.200000f;
793 lights[3].col[0] = 0.090838f;
794 lights[3].col[1] = 0.082080f;
795 lights[3].col[2] = 0.072255f;
796 lights[3].spec[0] = 0.106535f;
797 lights[3].spec[1] = 0.084771f;
798 lights[3].spec[2] = 0.066080f;
799 lights[3].vec[0] = 0.624519f;
800 lights[3].vec[1] = -0.562067f;
801 lights[3].vec[2] = -0.542269f;
802}
803
805{
806 /* Add default studio light */
809 STRNCPY(sl->name, "Default");
810
812
813 /* Go over the preset folder and add a studio-light for every image with its path. */
814 /* Also reserve icon space for it. */
833
834 /* sort studio lights on filename. */
836
838}
839
841{
842 while (StudioLight *sl = static_cast<StudioLight *>(BLI_pophead(&studiolights))) {
844 }
845}
846
848{
849 const char *default_name = "";
850
851 if (flag & STUDIOLIGHT_TYPE_WORLD) {
852 default_name = STUDIOLIGHT_WORLD_DEFAULT;
853 }
854 else if (flag & STUDIOLIGHT_TYPE_MATCAP) {
855 default_name = STUDIOLIGHT_MATCAP_DEFAULT;
856 }
857
858 for (StudioLight &sl : studiolights) {
859 if ((sl.flag & flag) && STREQ(sl.name, default_name)) {
860 return &sl;
861 }
862 }
863
864 for (StudioLight &sl : studiolights) {
865 if (sl.flag & flag) {
866 return &sl;
867 }
868 }
869 return nullptr;
870}
871
872StudioLight *BKE_studiolight_find(const char *name, int flag)
873{
874 for (StudioLight &sl : studiolights) {
875 if (STREQLEN(sl.name, name, FILE_MAXFILE)) {
876 if (sl.flag & flag) {
877 return &sl;
878 }
879
880 /* flags do not match, so use default */
881 return BKE_studiolight_find_default(flag);
882 }
883 }
884 /* When not found, use the default studio light */
885 return BKE_studiolight_find_default(flag);
886}
887
889{
890 for (StudioLight &sl : studiolights) {
891 if (sl.index == index) {
892 return &sl;
893 }
894 }
895 /* When not found, use the default studio light */
896 return BKE_studiolight_find_default(flag);
897}
898
899ListBaseT<StudioLight> &BKE_studiolight_listbase()
900{
901 return studiolights;
902}
903
904void BKE_studiolight_preview(uint *icon_buffer, StudioLight *sl, int icon_id_type)
905{
906 switch (icon_id_type) {
908 default: {
909 studiolight_radiance_preview(icon_buffer, sl);
910 break;
911 }
913 studiolight_irradiance_preview(icon_buffer, sl);
914 break;
915 }
917 studiolight_matcap_preview(icon_buffer, sl, false);
918 break;
919 }
921 studiolight_matcap_preview(icon_buffer, sl, true);
922 break;
923 }
924 }
926}
927
954
955/*
956 * Python API Functions.
957 */
958
960{
961 if (sl->flag & STUDIOLIGHT_USER_DEFINED) {
964 }
965}
966
967StudioLight *BKE_studiolight_load(const char *filepath, int type)
968{
970 return sl;
971}
972
974 const SolidLight light[4],
975 const float light_ambient[3])
976{
980
983 STRNCPY(sl->filepath, filepath);
984 const size_t name_len = STRNCPY_RLEN(sl->name, filename);
985 BLI_str_utf8_invalid_strip(sl->name, name_len);
986
987 memcpy(sl->light, light, sizeof(*light) * 4);
988 memcpy(sl->light_ambient, light_ambient, sizeof(*light_ambient) * 3);
989
991
993 return sl;
994}
995
997{
998 static StudioLight sl = {nullptr};
1000
1001 memcpy(sl.light, U.light_param, sizeof(*sl.light) * 4);
1002 memcpy(sl.light_ambient, U.light_ambient, sizeof(*sl.light_ambient) * 3);
1003
1004 return &sl;
1005}
1006
1012
1014 StudioLightFreeFunction *free_function,
1015 void *data)
1016{
1017 sl->free_function = free_function;
1019}
1020
1022{
1023 BLI_assert(sl != nullptr);
1024 if (sl->icon_id_radiance == icon_id) {
1025 sl->icon_id_radiance = 0;
1026 }
1027 if (sl->icon_id_irradiance == icon_id) {
1028 sl->icon_id_irradiance = 0;
1029 }
1030 if (sl->icon_id_matcap == icon_id) {
1031 sl->icon_id_matcap = 0;
1032 }
1033 if (sl->icon_id_matcap_flipped == icon_id) {
1034 sl->icon_id_matcap_flipped = 0;
1035 }
1036}
1037
1038} // namespace blender
#define STUDIOLIGHT_ICON_ID_TYPE_MATCAP
#define STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED
#define STUDIOLIGHT_ICON_ID_TYPE_RADIANCE
#define STUDIOLIGHT_ICON_SIZE
#define STUDIOLIGHT_ICON_ID_TYPE_IRRADIANCE
#define STUDIOLIGHT_MAX_LIGHT
A dynamically sized string ADT.
File and directory operations.
Some types for dealing with directories.
#define M_PI
#define FILE_MAXFILE
#define STRNCPY_RLEN(dst, src)
These structs are the foundation for all linked lists in the library system.
Read Blender memory allocation and freeing.
#define STREQLEN(a, b, n)
#define BLI_INLINE
#define STREQ(a, b)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define NL
#define powf(x, y)
#define acosf(x)
#define str(s)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define exp2
#define atan2
#define N
#define R
#define L
std::string filename(const std::string_view &str, size_t pos)
BLI_INLINE Texture * wrap(MTLTexture *texture)
float4 interpolate_nearest_border_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:29
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void BKE_studiolight_preview(uint *icon_buffer, StudioLight *sl, int icon_id_type)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static void studiolight_create_matcap_diffuse_gputexture(StudioLight *sl)
StudioLight * BKE_studiolight_create(const char *filepath, const SolidLight light[4], const float light_ambient[3])
@ STUDIOLIGHT_INTERNAL
@ STUDIOLIGHT_EXTERNAL_FILE
@ STUDIOLIGHT_USER_DEFINED
@ STUDIOLIGHT_TYPE_MATCAP
@ STUDIOLIGHT_MATCAP_SPECULAR_GPUTEXTURE
@ STUDIOLIGHT_TYPE_STUDIO
@ STUDIOLIGHT_MATCAP_DIFFUSE_GPUTEXTURE
@ STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE
@ STUDIOLIGHT_SPECULAR_HIGHLIGHT_PASS
@ STUDIOLIGHT_TYPE_WORLD
@ STUDIOLIGHT_EXTERNAL_IMAGE_LOADED
char * STRNCPY(char(&dst)[N], const char *src)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
static const char * STUDIOLIGHT_MATCAP_FOLDER
static StudioLight * studiolight_add_file(const char *filepath, int flag)
void BKE_studiolight_init()
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void studiolight_irradiance_preview(uint *icon_buffer, StudioLight *sl)
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
static StudioLight * studiolight_create(int flag)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
void BKE_studiolight_ensure_flag(StudioLight *sl, int flag)
static void studiolight_lights_eval(StudioLight *sl, const float normal[3], float r_color[3])
static void studiolight_add_files_from_datafolder(const int folder_id, const char *subfolder, int flag)
void BKE_studiolight_refresh()
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static int last_studiolight_id
void BKE_studiolight_free()
static void studiolight_load_solid_light(StudioLight *sl)
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
static void studiolight_create_equirect_radiance_gputexture(StudioLight *sl)
static const char * STUDIOLIGHT_MATCAP_DEFAULT
BLI_INLINE float area_element(float x, float y)
int BKE_icon_ensure_studio_light(StudioLight *sl, int id_type)
Definition icons.cc:664
void BKE_studiolight_unset_icon_id(StudioLight *sl, int icon_id)
ATTR_WARN_UNUSED_RESULT const BMVert * v
ExrReadHandle * IMB_exr_open_multilayer(const char *filepath)
unsigned int rgb_to_cpack(float r, float g, float b)
void BKE_studiolight_set_free_function(StudioLight *sl, StudioLightFreeFunction *free_function, void *data)
static ListBaseT< StudioLight > studiolights
static void studiolight_free_gpu_textures(StudioLight *sl)
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
void BKE_studiolight_default(SolidLight lights[4], float light_ambient[3])
MINLINE void mul_v3_fl(float r[3], float f)
struct StudioLight * BKE_studiolight_findindex(int index, int flag)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
struct StudioLight * BKE_studiolight_find(const char *name, int flag)
static float brdf_approx(float spec_color, float roughness, float NV)
VecBase< float, 4 > float4
struct LinkNode * BLI_file_read_as_lines(const char *filepath) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:591
static void studiolight_matcap_preview(uint *icon_buffer, StudioLight *sl, bool flipped)
void IMB_float_from_byte(ImBuf *ibuf)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
ImBuf * IMB_load_image_from_filepath(const char *filepath, ImBufFlags flags, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:201
std::optional< std::string > BKE_appdir_folder_id(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:762
static void studiolight_write_solid_light(StudioLight *sl)
void IMB_freeImBuf(ImBuf *ibuf)
ListBaseT< StudioLight > & BKE_studiolight_listbase()
@ GPU_TEXTURE_USAGE_SHADER_READ
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
static const char * STUDIOLIGHT_WORLD_DEFAULT
static void studiolight_free(StudioLight *sl)
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void IMB_buffer_float_rgba_from_float(float *dest, const float *src, int src_channels, int width, int height)
static void studiolight_radiance_preview(uint *icon_buffer, StudioLight *sl)
static void direction_to_equirect(float r[2], const float dir[3])
static float blinn_specular(const float L[3], const float I[3], const float N[3], const float R[3], float NL, float roughness, float wrap)
static void studiolight_create_matcap_specular_gputexture(StudioLight *sl)
static const char * STUDIOLIGHT_LIGHTS_FOLDER
void reflect_v3_v3v3(float out[3], const float v[3], const float normal[3])
void IMB_exr_close(ExrReadHandle *handle)
static int studiolight_cmp(const void *a, const void *b)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
static void studiolight_free_image_buffers(StudioLight *sl)
void BKE_studiolight_remove(StudioLight *sl)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
struct StudioLight * BKE_studiolight_find_default(int flag)
void StudioLightFreeFunction(struct StudioLight *, void *data)
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
StudioLight * BKE_studiolight_load(const char *filepath, int type)
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static void studiolight_free_temp_resources(StudioLight *sl)
bool BLI_path_extension_check_array(const char *path, const char **ext_array) ATTR_NONNULL(1
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
MINLINE float max_ff(float a, float b)
ImBuf * IMB_allocFromBuffer(const uint8_t *byte_buffer, const float *float_buffer, unsigned int w, unsigned int h, unsigned int channels)
static int studiolight_flag_cmp_order(const StudioLight *sl)
MINLINE float square_f(float a)
static void studiolight_load_equirect_image(StudioLight *sl)
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
BLI_INLINE void IMB_colormanagement_scene_linear_to_srgb_v3(float srgb[3], const float scene_linear[3])
@ BLENDER_SYSTEM_DATAFILES
@ BLENDER_USER_DATAFILES
static const char * STUDIOLIGHT_WORLD_FOLDER
int BLI_dynstr_get_len(const DynStr *ds) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_file_free_lines(struct LinkNode *lines)
Definition storage.cc:637
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
Vector< ExrPassInfo > IMB_exr_get_passes(ExrReadHandle *handle)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void studiolight_create_matcap_gputexture(StudioLightImage *sli)
static uint alpha_circle_mask(float u, float v, float inner_edge, float outer_edge)
int BLI_str_utf8_invalid_strip(char *str, size_t str_len) ATTR_NONNULL(1)
float linearrgb_to_srgb(float c)
void IMB_exr_read_passes(ExrReadHandle *handle, MutableSpan< ExrPassInfo > entries, const char *src_colorspace=nullptr, bool predivide=false)
static float wrapped_lighting(float NL, float w)
static ImBuf * studiolight_read_matcap_pass(ExrReadHandle *handle, const char *pass_name, const char *file_colorspace)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
const char * IMB_colormanagement_working_space_get()
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
StudioLight * BKE_studiolight_studio_edit_get()
static float4 studiolight_calculate_radiance(const ImBuf *ibuf, const float direction[3])
static void sphere_normal_from_uv(float normal[3], float u, float v)
#define I
#define floorf
#define sqrtf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79
StudioLightImage matcap_specular
StudioLightImage matcap_diffuse
std::string equirect_working_space
char name[FILE_MAXFILE]
gpu::Texture * equirect_radiance_gputexture
struct ImBuf * equirect_radiance_buffer
SolidLight light[STUDIOLIGHT_MAX_LIGHT]
StudioLightFreeFunction * free_function
char filepath[FILE_MAX]
#define STUDIOLIGHT_FILE_VERSION
#define WRITE_IVAL(str, id, val)
#define STUDIOLIGHT_PASSNAME_DIFFUSE
#define WRITE_SOLIDLIGHT(str, sl, i)
#define ITER_PIXELS(type, src, channels, width, height)
#define WRITE_VEC3(str, id, val)
#define ITER_PIXELS_END
#define STUDIOLIGHT_DELETE_ICON(s)
#define STUDIOLIGHT_PASSNAME_SPECULAR
#define READ_VEC3(id, val, lines)
#define IMB_SAFE_FREE(p)
#define RESCALE_COORD(x)
#define GPU_TEXTURE_SAFE_FREE(p)
#define READ_SOLIDLIGHT(sl, i, lines)
#define SEP_STR
Definition unit.cc:41