1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006-2007 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bke
24  *
25  * Studio lighting for the 3dview
26  */
27 
28 #include "BLI_sys_types.h"
29 
30 #include "BLI_path_util.h"
31 
32 #include "DNA_userdef_types.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 struct ImBuf;
39 
40 /*
41  * These defines are the indexes in the StudioLight.diffuse_light
42  * X_POS means the light that is traveling towards the positive X
43  * So Light direction.
44  */
45 #define STUDIOLIGHT_X_POS 0
46 #define STUDIOLIGHT_X_NEG 1
47 #define STUDIOLIGHT_Y_POS 2
48 #define STUDIOLIGHT_Y_NEG 3
49 #define STUDIOLIGHT_Z_POS 4
50 #define STUDIOLIGHT_Z_NEG 5
51 
52 #define STUDIOLIGHT_ICON_ID_TYPE_RADIANCE (1 << 0)
53 #define STUDIOLIGHT_ICON_ID_TYPE_IRRADIANCE (1 << 1)
54 #define STUDIOLIGHT_ICON_ID_TYPE_MATCAP (1 << 2)
55 #define STUDIOLIGHT_ICON_ID_TYPE_MATCAP_FLIPPED (1 << 3)
56 
57 #define STUDIOLIGHT_MAX_LIGHT 4
58 
59 #define STUDIOLIGHT_ICON_SIZE 96
60 
61 /* Only 1 - 5 is supported */
62 #define STUDIOLIGHT_SH_BANDS 2
63 
64 #define STUDIOLIGHT_SH_COEFS_LEN (STUDIOLIGHT_SH_BANDS * STUDIOLIGHT_SH_BANDS)
65 
66 #if STUDIOLIGHT_SH_BANDS > 3
67 /* Bypass L3 */
68 #  define STUDIOLIGHT_SH_EFFECTIVE_COEFS_LEN (STUDIOLIGHT_SH_COEFS_LEN - 7)
69 #else
70 #  define STUDIOLIGHT_SH_EFFECTIVE_COEFS_LEN STUDIOLIGHT_SH_COEFS_LEN
71 #endif
72 
73 struct GPUTexture;
74 struct StudioLight;
75 
76 /* StudioLight.flag */
77 enum StudioLightFlag {
78   STUDIOLIGHT_SPHERICAL_HARMONICS_COEFFICIENTS_CALCULATED = (1 << 0),
79   /*  STUDIOLIGHT_LIGHT_DIRECTION_CALCULATED                  = (1 << 1), */
80   STUDIOLIGHT_INTERNAL = (1 << 2),
81   STUDIOLIGHT_EXTERNAL_FILE = (1 << 3),
82   STUDIOLIGHT_TYPE_STUDIO = (1 << 4),
83   STUDIOLIGHT_TYPE_WORLD = (1 << 5),
84   STUDIOLIGHT_TYPE_MATCAP = (1 << 6),
85   STUDIOLIGHT_EXTERNAL_IMAGE_LOADED = (1 << 7),
86   STUDIOLIGHT_EQUIRECT_IRRADIANCE_IMAGE_CALCULATED = (1 << 8),
87   STUDIOLIGHT_EQUIRECT_RADIANCE_GPUTEXTURE = (1 << 9),
88   STUDIOLIGHT_EQUIRECT_IRRADIANCE_GPUTEXTURE = (1 << 10),
89   STUDIOLIGHT_RADIANCE_BUFFERS_CALCULATED = (1 << 11),
90   STUDIOLIGHT_USER_DEFINED = (1 << 12),
91   STUDIOLIGHT_UI_EXPANDED = (1 << 13),
92 
93   STUDIOLIGHT_MATCAP_DIFFUSE_GPUTEXTURE = (1 << 14),
94   STUDIOLIGHT_MATCAP_SPECULAR_GPUTEXTURE = (1 << 15),
95   /* Is set for studio lights and matcaps with specular highlight pass. */
96   STUDIOLIGHT_SPECULAR_HIGHLIGHT_PASS = (1 << 16),
97 };
98 
99 #define STUDIOLIGHT_FLAG_ALL (STUDIOLIGHT_INTERNAL | STUDIOLIGHT_EXTERNAL_FILE)
100 #define STUDIOLIGHT_FLAG_ORIENTATIONS \
101   (STUDIOLIGHT_TYPE_STUDIO | STUDIOLIGHT_TYPE_WORLD | STUDIOLIGHT_TYPE_MATCAP)
102 #define STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE (STUDIOLIGHT_TYPE_WORLD)
103 #define STUDIOLIGHT_ORIENTATIONS_SOLID (STUDIOLIGHT_INTERNAL | STUDIOLIGHT_TYPE_STUDIO)
104 
105 typedef void StudioLightFreeFunction(struct StudioLight *, void *data);
106 
107 typedef struct StudioLightImage {
108   struct ImBuf *ibuf;
109   struct GPUTexture *gputexture;
110 } StudioLightImage;
111 
112 typedef struct StudioLight {
113   struct StudioLight *next, *prev;
114 
115   int index;
116   int flag;
117   char name[FILE_MAXFILE];
118   char path[FILE_MAX];
119   char *path_irr_cache;
120   char *path_sh_cache;
121   int icon_id_irradiance;
122   int icon_id_radiance;
123   int icon_id_matcap;
124   int icon_id_matcap_flipped;
125   float spherical_harmonics_coefs[STUDIOLIGHT_SH_EFFECTIVE_COEFS_LEN][3];
126   float light_direction[3];
127   StudioLightImage matcap_diffuse;
128   StudioLightImage matcap_specular;
129   struct ImBuf *equirect_radiance_buffer;
130   struct ImBuf *equirect_irradiance_buffer;
131   struct ImBuf *radiance_cubemap_buffers[6];
132   struct GPUTexture *equirect_radiance_gputexture;
133   struct GPUTexture *equirect_irradiance_gputexture;
134   SolidLight light[STUDIOLIGHT_MAX_LIGHT];
135   float light_ambient[3];
136 
137   /*
138    * Free function to clean up the running icons previews (wmJob) the usage is in
139    * interface_icons. Please be aware that this was build to handle only one free function
140    * that cleans up all icons. just to keep the code simple.
141    */
142   StudioLightFreeFunction *free_function;
143   void *free_function_data;
144 } StudioLight;
145 
146 void BKE_studiolight_init(void);
147 void BKE_studiolight_free(void);
148 void BKE_studiolight_default(SolidLight lights[4], float light_ambient[4]);
149 struct StudioLight *BKE_studiolight_find(const char *name, int flag);
150 struct StudioLight *BKE_studiolight_findindex(int index, int flag);
151 struct StudioLight *BKE_studiolight_find_default(int flag);
152 void BKE_studiolight_preview(uint *icon_buffer, StudioLight *sl, int icon_id_type);
153 struct ListBase *BKE_studiolight_listbase(void);
154 void BKE_studiolight_ensure_flag(StudioLight *sl, int flag);
155 void BKE_studiolight_refresh(void);
156 StudioLight *BKE_studiolight_load(const char *path, int type);
157 StudioLight *BKE_studiolight_create(const char *path,
158                                     const SolidLight light[4],
159                                     const float light_ambient[3]);
160 StudioLight *BKE_studiolight_studio_edit_get(void);
161 void BKE_studiolight_remove(StudioLight *sl);
162 void BKE_studiolight_set_free_function(StudioLight *sl,
163                                        StudioLightFreeFunction *free_function,
164                                        void *data);
165 void BKE_studiolight_unset_icon_id(StudioLight *sl, int icon_id);
166 
167 #ifdef __cplusplus
168 }
169 #endif
170