Blender V4.3
studiolight.cc File Reference
#include "BKE_studiolight.h"
#include "BKE_appdir.hh"
#include "BKE_icons.h"
#include "BLI_dynstr.h"
#include "BLI_fileops.h"
#include "BLI_fileops_types.h"
#include "BLI_linklist.h"
#include "BLI_listbase.h"
#include "BLI_math_color.h"
#include "BLI_math_vector.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "DNA_listBase.h"
#include "IMB_imbuf.hh"
#include "IMB_interp.hh"
#include "IMB_openexr.hh"
#include "GPU_texture.hh"
#include "MEM_guardedalloc.h"
#include <cstring>

Go to the source code of this file.

Classes

struct  MultilayerConvertContext
 

Macros

#define STUDIOLIGHT_PASSNAME_DIFFUSE   "diffuse"
 
#define STUDIOLIGHT_PASSNAME_SPECULAR   "specular"
 
#define ITER_PIXELS(type, src, channels, width, height)
 
#define ITER_PIXELS_END
 
#define IMB_SAFE_FREE(p)
 
#define GPU_TEXTURE_SAFE_FREE(p)
 
#define STUDIOLIGHT_DELETE_ICON(s)
 
#define STUDIOLIGHT_FILE_VERSION   1
 
#define READ_VAL(type, parser, id, val, lines)
 
#define READ_FVAL(id, val, lines)   READ_VAL(float, atof, id, val, lines)
 
#define READ_IVAL(id, val, lines)   READ_VAL(int, atoi, id, val, lines)
 
#define READ_VEC3(id, val, lines)
 
#define READ_SOLIDLIGHT(sl, i, lines)
 
#define WRITE_FVAL(str, id, val)   BLI_dynstr_appendf(str, id " %f\n", val)
 
#define WRITE_IVAL(str, id, val)   BLI_dynstr_appendf(str, id " %d\n", val)
 
#define WRITE_VEC3(str, id, val)
 
#define WRITE_SOLIDLIGHT(str, sl, i)
 
#define STUDIOLIGHT_DIAMETER   0.95f
 
#define RESCALE_COORD(x)   (x / STUDIOLIGHT_DIAMETER - (1.0f - STUDIOLIGHT_DIAMETER) / 2.0f)
 

Functions

static void studiolight_free_image_buffers (StudioLight *sl)
 
static void studiolight_free (StudioLight *sl)
 
static void studiolight_free_temp_resources (StudioLight *sl)
 
static StudioLightstudiolight_create (int flag)
 
static void studiolight_load_solid_light (StudioLight *sl)
 
static void studiolight_write_solid_light (StudioLight *sl)
 
static void direction_to_equirect (float r[2], const float dir[3])
 
static void * studiolight_multilayer_addview (void *, const char *)
 
static void * studiolight_multilayer_addlayer (void *base, const char *)
 
static floatstudiolight_multilayer_convert_pass (const ImBuf *ibuf, float *rect, const uint channels)
 
static void studiolight_multilayer_addpass (void *base, void *, const char *pass_name, float *rect, int num_channels, const char *, const char *)
 
static void studiolight_load_equirect_image (StudioLight *sl)
 
static void studiolight_create_equirect_radiance_gputexture (StudioLight *sl)
 
static void studiolight_create_matcap_gputexture (StudioLightImage *sli)
 
static void studiolight_create_matcap_diffuse_gputexture (StudioLight *sl)
 
static void studiolight_create_matcap_specular_gputexture (StudioLight *sl)
 
static float4 studiolight_calculate_radiance (const ImBuf *ibuf, const float direction[3])
 
BLI_INLINE float area_element (float x, float y)
 
static float brdf_approx (float spec_color, float roughness, float NV)
 
static float wrapped_lighting (float NL, float w)
 
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_lights_eval (StudioLight *sl, const float normal[3], float r_color[3])
 
static StudioLightstudiolight_add_file (const char *filepath, int flag)
 
static void studiolight_add_files_from_datafolder (const int folder_id, const char *subfolder, int flag)
 
static int studiolight_flag_cmp_order (const StudioLight *sl)
 
static int studiolight_cmp (const void *a, const void *b)
 
static uint alpha_circle_mask (float u, float v, float inner_edge, float outer_edge)
 
static void sphere_normal_from_uv (float normal[3], float u, float v)
 
static void studiolight_radiance_preview (uint *icon_buffer, StudioLight *sl)
 
static void studiolight_matcap_preview (uint *icon_buffer, StudioLight *sl, bool flipped)
 
static void studiolight_irradiance_preview (uint *icon_buffer, StudioLight *sl)
 
void BKE_studiolight_default (SolidLight lights[4], float light_ambient[3])
 
void BKE_studiolight_init ()
 
void BKE_studiolight_free ()
 
StudioLightBKE_studiolight_find_default (int flag)
 
StudioLightBKE_studiolight_find (const char *name, int flag)
 
StudioLightBKE_studiolight_findindex (int index, int flag)
 
ListBaseBKE_studiolight_listbase ()
 
void BKE_studiolight_preview (uint *icon_buffer, StudioLight *sl, int icon_id_type)
 
void BKE_studiolight_ensure_flag (StudioLight *sl, int flag)
 
void BKE_studiolight_remove (StudioLight *sl)
 
StudioLightBKE_studiolight_load (const char *filepath, int type)
 
StudioLightBKE_studiolight_create (const char *filepath, const SolidLight light[4], const float light_ambient[3])
 
StudioLightBKE_studiolight_studio_edit_get ()
 
void BKE_studiolight_refresh ()
 
void BKE_studiolight_set_free_function (StudioLight *sl, StudioLightFreeFunction *free_function, void *data)
 
void BKE_studiolight_unset_icon_id (StudioLight *sl, int icon_id)
 

Variables

static ListBase studiolights
 
static int last_studiolight_id = 0
 
static const char * STUDIOLIGHT_LIGHTS_FOLDER = "studiolights" SEP_STR "studio" SEP_STR
 
static const char * STUDIOLIGHT_WORLD_FOLDER = "studiolights" SEP_STR "world" SEP_STR
 
static const char * STUDIOLIGHT_MATCAP_FOLDER = "studiolights" SEP_STR "matcap" SEP_STR
 
static const char * STUDIOLIGHT_WORLD_DEFAULT = "forest.exr"
 
static const char * STUDIOLIGHT_MATCAP_DEFAULT = "basic_1.exr"
 

Macro Definition Documentation

◆ GPU_TEXTURE_SAFE_FREE

#define GPU_TEXTURE_SAFE_FREE ( p)
Value:
do { \
if (p) { \
GPU_texture_free(p); \
p = nullptr; \
} \
} while (0)

Definition at line 91 of file studiolight.cc.

Referenced by studiolight_free().

◆ IMB_SAFE_FREE

#define IMB_SAFE_FREE ( p)
Value:
do { \
if (p) { \
IMB_freeImBuf(p); \
p = nullptr; \
} \
} while (0)

Definition at line 83 of file studiolight.cc.

Referenced by studiolight_free_image_buffers().

◆ ITER_PIXELS

#define ITER_PIXELS ( type,
src,
channels,
width,
height )
Value:
{ \
float texel_size[2]; \
texel_size[0] = 1.0f / width; \
texel_size[1] = 1.0f / height; \
type(*pixel_)[channels] = (type(*)[channels])src; \
for (float y = 0.5 * texel_size[1]; y < 1.0; y += texel_size[1]) { \
for (float x = 0.5 * texel_size[0]; x < 1.0; x += texel_size[0], pixel_++) { \
type *pixel = *pixel_;
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a producing a negative Combine Generate a color from its and blue channels(Deprecated)") DefNode(ShaderNode

Iter on all pixel giving texel center position and pixel pointer.

Arguments type : type of src. src : source buffer. channels : number of channels per pixel.

Others x, y : normalized UV coordinate [0..1] of the current pixel center. texel_size[2] : UV size of a pixel in this texture. pixel[] : pointer to the current pixel.

Definition at line 66 of file studiolight.cc.

Referenced by studiolight_irradiance_preview(), studiolight_matcap_preview(), and studiolight_radiance_preview().

◆ ITER_PIXELS_END

#define ITER_PIXELS_END
Value:
} \
} \
} \
((void)0)

Definition at line 76 of file studiolight.cc.

Referenced by studiolight_irradiance_preview(), studiolight_matcap_preview(), and studiolight_radiance_preview().

◆ READ_FVAL

#define READ_FVAL ( id,
val,
lines )   READ_VAL(float, atof, id, val, lines)

Definition at line 186 of file studiolight.cc.

◆ READ_IVAL

#define READ_IVAL ( id,
val,
lines )   READ_VAL(int, atoi, id, val, lines)

Definition at line 187 of file studiolight.cc.

◆ READ_SOLIDLIGHT

#define READ_SOLIDLIGHT ( sl,
i,
lines )
Value:
do { \
READ_IVAL("light[" STRINGIFY(i) "].flag", sl[i].flag, lines); \
READ_FVAL("light[" STRINGIFY(i) "].smooth", sl[i].smooth, lines); \
READ_VEC3("light[" STRINGIFY(i) "].col", sl[i].col, lines); \
READ_VEC3("light[" STRINGIFY(i) "].spec", sl[i].spec, lines); \
READ_VEC3("light[" STRINGIFY(i) "].vec", sl[i].vec, lines); \
} while (0)
#define STRINGIFY(x)
uint col
uint8_t flag
Definition wm_window.cc:138

Definition at line 196 of file studiolight.cc.

Referenced by studiolight_load_solid_light().

◆ READ_VAL

#define READ_VAL ( type,
parser,
id,
val,
lines )
Value:
do { \
for (LinkNode *line = lines; line; line = line->next) { \
char *val_str, *str = static_cast<char *>(line->link); \
if ((val_str = strstr(str, id " "))) { \
val_str += sizeof(id); /* Skip id + spacer. */ \
val = parser(val_str); \
} \
} \
} while (0)
#define str(s)
struct LinkNode * next

Definition at line 175 of file studiolight.cc.

◆ READ_VEC3

#define READ_VEC3 ( id,
val,
lines )
Value:
do { \
READ_FVAL(id ".x", val[0], lines); \
READ_FVAL(id ".y", val[1], lines); \
READ_FVAL(id ".z", val[2], lines); \
} while (0)

Definition at line 189 of file studiolight.cc.

Referenced by studiolight_load_solid_light().

◆ RESCALE_COORD

#define RESCALE_COORD ( x)    (x / STUDIOLIGHT_DIAMETER - (1.0f - STUDIOLIGHT_DIAMETER) / 2.0f)

◆ STUDIOLIGHT_DELETE_ICON

#define STUDIOLIGHT_DELETE_ICON ( s)
Value:
do { \
if (s != 0) { \
BKE_icon_delete(s); \
s = 0; \
} \
} while (0)

Referenced by studiolight_free().

◆ STUDIOLIGHT_DIAMETER

#define STUDIOLIGHT_DIAMETER   0.95f

Definition at line 674 of file studiolight.cc.

◆ STUDIOLIGHT_FILE_VERSION

#define STUDIOLIGHT_FILE_VERSION   1

Definition at line 173 of file studiolight.cc.

Referenced by studiolight_write_solid_light().

◆ STUDIOLIGHT_PASSNAME_DIFFUSE

#define STUDIOLIGHT_PASSNAME_DIFFUSE   "diffuse"

Definition at line 41 of file studiolight.cc.

Referenced by studiolight_multilayer_addpass().

◆ STUDIOLIGHT_PASSNAME_SPECULAR

#define STUDIOLIGHT_PASSNAME_SPECULAR   "specular"

Definition at line 42 of file studiolight.cc.

Referenced by studiolight_multilayer_addpass().

◆ WRITE_FVAL

#define WRITE_FVAL ( str,
id,
val )   BLI_dynstr_appendf(str, id " %f\n", val)

Definition at line 223 of file studiolight.cc.

◆ WRITE_IVAL

#define WRITE_IVAL ( str,
id,
val )   BLI_dynstr_appendf(str, id " %d\n", val)

Definition at line 224 of file studiolight.cc.

Referenced by studiolight_write_solid_light().

◆ WRITE_SOLIDLIGHT

#define WRITE_SOLIDLIGHT ( str,
sl,
i )
Value:
do { \
WRITE_IVAL(str, "light[" STRINGIFY(i) "].flag", sl[i].flag); \
WRITE_FVAL(str, "light[" STRINGIFY(i) "].smooth", sl[i].smooth); \
WRITE_VEC3(str, "light[" STRINGIFY(i) "].col", sl[i].col); \
WRITE_VEC3(str, "light[" STRINGIFY(i) "].spec", sl[i].spec); \
WRITE_VEC3(str, "light[" STRINGIFY(i) "].vec", sl[i].vec); \
} while (0)

Definition at line 233 of file studiolight.cc.

Referenced by studiolight_write_solid_light().

◆ WRITE_VEC3

#define WRITE_VEC3 ( str,
id,
val )
Value:
do { \
WRITE_FVAL(str, id ".x", val[0]); \
WRITE_FVAL(str, id ".y", val[1]); \
WRITE_FVAL(str, id ".z", val[2]); \
} while (0)

Definition at line 226 of file studiolight.cc.

Referenced by studiolight_write_solid_light().

Function Documentation

◆ alpha_circle_mask()

static uint alpha_circle_mask ( float u,
float v,
float inner_edge,
float outer_edge )
static

◆ area_element()

BLI_INLINE float area_element ( float x,
float y )

Definition at line 502 of file studiolight.cc.

References sqrtf.

◆ BKE_studiolight_create()

◆ BKE_studiolight_default()

void BKE_studiolight_default ( SolidLight lights[4],
float light_ambient[3] )

◆ BKE_studiolight_ensure_flag()

◆ BKE_studiolight_find()

◆ BKE_studiolight_find_default()

◆ BKE_studiolight_findindex()

StudioLight * BKE_studiolight_findindex ( int index,
int flag )

Definition at line 919 of file studiolight.cc.

References BKE_studiolight_find_default(), flag, LISTBASE_FOREACH, and studiolights.

◆ BKE_studiolight_free()

void BKE_studiolight_free ( void )

Definition at line 871 of file studiolight.cc.

References BLI_pophead(), studiolight_free(), and studiolights.

Referenced by BKE_blender_free(), and BKE_studiolight_refresh().

◆ BKE_studiolight_init()

◆ BKE_studiolight_listbase()

ListBase * BKE_studiolight_listbase ( void )

Definition at line 930 of file studiolight.cc.

References studiolights.

◆ BKE_studiolight_load()

StudioLight * BKE_studiolight_load ( const char * filepath,
int type )

Definition at line 991 of file studiolight.cc.

References studiolight_add_file(), and STUDIOLIGHT_USER_DEFINED.

◆ BKE_studiolight_preview()

◆ BKE_studiolight_refresh()

void BKE_studiolight_refresh ( void )

Definition at line 1030 of file studiolight.cc.

References BKE_studiolight_free(), and BKE_studiolight_init().

◆ BKE_studiolight_remove()

void BKE_studiolight_remove ( StudioLight * sl)

◆ BKE_studiolight_set_free_function()

void BKE_studiolight_set_free_function ( StudioLight * sl,
StudioLightFreeFunction * free_function,
void * data )

◆ BKE_studiolight_studio_edit_get()

StudioLight * BKE_studiolight_studio_edit_get ( void )

Only useful for workbench while editing the user-preferences.

Definition at line 1019 of file studiolight.cc.

References StudioLight::flag, StudioLight::light, StudioLight::light_ambient, STUDIOLIGHT_SPECULAR_HIGHLIGHT_PASS, and STUDIOLIGHT_TYPE_STUDIO.

Referenced by blender::workbench::SceneResources::init().

◆ BKE_studiolight_unset_icon_id()

void BKE_studiolight_unset_icon_id ( StudioLight * sl,
int icon_id )

◆ blinn_specular()

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

Definition at line 522 of file studiolight.cc.

References add_v3_v3v3(), dot_v3v3(), I, L, max_ff(), N, NL, normalize_v3(), powf, R, w(), wrap(), and wrapped_lighting().

Referenced by studiolight_lights_eval().

◆ brdf_approx()

static float brdf_approx ( float spec_color,
float roughness,
float NV )
static

Definition at line 507 of file studiolight.cc.

Referenced by studiolight_lights_eval().

◆ direction_to_equirect()

static void direction_to_equirect ( float r[2],
const float dir[3] )
static

Definition at line 271 of file studiolight.cc.

References acosf, atan2f, and M_PI.

Referenced by studiolight_calculate_radiance().

◆ sphere_normal_from_uv()

static void sphere_normal_from_uv ( float normal[3],
float u,
float v )
static

Definition at line 679 of file studiolight.cc.

References len_v2(), sqrtf, square_f(), and v.

Referenced by studiolight_irradiance_preview(), and studiolight_radiance_preview().

◆ studiolight_add_file()

◆ studiolight_add_files_from_datafolder()

static void studiolight_add_files_from_datafolder ( const int folder_id,
const char * subfolder,
int flag )
static

◆ studiolight_calculate_radiance()

static float4 studiolight_calculate_radiance ( const ImBuf * ibuf,
const float direction[3] )
static

◆ studiolight_cmp()

static int studiolight_cmp ( const void * a,
const void * b )
static

Definition at line 640 of file studiolight.cc.

References b, BLI_strcasecmp(), StudioLight::name, and studiolight_flag_cmp_order().

Referenced by BKE_studiolight_init().

◆ studiolight_create()

◆ studiolight_create_equirect_radiance_gputexture()

◆ studiolight_create_matcap_diffuse_gputexture()

◆ studiolight_create_matcap_gputexture()

◆ studiolight_create_matcap_specular_gputexture()

◆ studiolight_flag_cmp_order()

static int studiolight_flag_cmp_order ( const StudioLight * sl)
static

Definition at line 631 of file studiolight.cc.

References StudioLight::flag, and STUDIOLIGHT_EXTERNAL_FILE.

Referenced by studiolight_cmp().

◆ studiolight_free()

◆ studiolight_free_image_buffers()

◆ studiolight_free_temp_resources()

static void studiolight_free_temp_resources ( StudioLight * sl)
static

Free temp resources when the studio light is only requested for icons.

Only keeps around resources for studio lights that have been used in any viewport.

Definition at line 139 of file studiolight.cc.

References StudioLight::flag, STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE, studiolight_free_image_buffers(), STUDIOLIGHT_MATCAP_DIFFUSE_GPUTEXTURE, and STUDIOLIGHT_MATCAP_SPECULAR_GPUTEXTURE.

Referenced by BKE_studiolight_preview().

◆ studiolight_irradiance_preview()

static void studiolight_irradiance_preview ( uint * icon_buffer,
StudioLight * sl )
static

◆ studiolight_lights_eval()

static void studiolight_lights_eval ( StudioLight * sl,
const float normal[3],
float r_color[3] )
static

◆ studiolight_load_equirect_image()

◆ studiolight_load_solid_light()

static void studiolight_load_solid_light ( StudioLight * sl)
static

◆ studiolight_matcap_preview()

◆ studiolight_multilayer_addlayer()

static void * studiolight_multilayer_addlayer ( void * base,
const char *  )
static

Definition at line 288 of file studiolight.cc.

Referenced by studiolight_load_equirect_image().

◆ studiolight_multilayer_addpass()

static void studiolight_multilayer_addpass ( void * base,
void * ,
const char * pass_name,
float * rect,
int num_channels,
const char * ,
const char *  )
static

◆ studiolight_multilayer_addview()

static void * studiolight_multilayer_addview ( void * ,
const char *  )
static

Definition at line 284 of file studiolight.cc.

Referenced by studiolight_load_equirect_image().

◆ studiolight_multilayer_convert_pass()

static float * studiolight_multilayer_convert_pass ( const ImBuf * ibuf,
float * rect,
const uint channels )
static

◆ studiolight_radiance_preview()

◆ studiolight_write_solid_light()

◆ wrapped_lighting()

static float wrapped_lighting ( float NL,
float w )
static

Definition at line 516 of file studiolight.cc.

References max_ff(), NL, and w().

Referenced by blinn_specular(), and studiolight_lights_eval().

Variable Documentation

◆ last_studiolight_id

int last_studiolight_id = 0
static

Definition at line 40 of file studiolight.cc.

Referenced by studiolight_create().

◆ STUDIOLIGHT_LIGHTS_FOLDER

const char* STUDIOLIGHT_LIGHTS_FOLDER = "studiolights" SEP_STR "studio" SEP_STR
static

Definition at line 44 of file studiolight.cc.

Referenced by BKE_studiolight_init().

◆ STUDIOLIGHT_MATCAP_DEFAULT

const char* STUDIOLIGHT_MATCAP_DEFAULT = "basic_1.exr"
static

Definition at line 49 of file studiolight.cc.

Referenced by BKE_studiolight_find_default().

◆ STUDIOLIGHT_MATCAP_FOLDER

const char* STUDIOLIGHT_MATCAP_FOLDER = "studiolights" SEP_STR "matcap" SEP_STR
static

Definition at line 46 of file studiolight.cc.

Referenced by BKE_studiolight_init().

◆ STUDIOLIGHT_WORLD_DEFAULT

const char* STUDIOLIGHT_WORLD_DEFAULT = "forest.exr"
static

Definition at line 48 of file studiolight.cc.

Referenced by BKE_studiolight_find_default().

◆ STUDIOLIGHT_WORLD_FOLDER

const char* STUDIOLIGHT_WORLD_FOLDER = "studiolights" SEP_STR "world" SEP_STR
static

Definition at line 45 of file studiolight.cc.

Referenced by BKE_studiolight_init().

◆ studiolights