Merging phase 1 of the BGE Harmony branch:
[blender.git] / source / blender / blenkernel / intern / lamp.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/lamp.c
29  *  \ingroup bke
30  */
31
32 #include <stdlib.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_lamp_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_texture_types.h"
40
41 #include "BLI_listbase.h"
42 #include "BLI_math.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_colortools.h"
47 #include "BKE_icons.h"
48 #include "BKE_global.h"
49 #include "BKE_lamp.h"
50 #include "BKE_library.h"
51 #include "BKE_main.h"
52 #include "BKE_node.h"
53
54 void *add_lamp(const char *name)
55 {
56         Lamp *la;
57         
58         la=  alloc_libblock(&G.main->lamp, ID_LA, name);
59         
60         la->r= la->g= la->b= la->k= 1.0f;
61         la->haint= la->energy= 1.0f;
62         la->dist= 25.0f;
63         la->spotsize= 45.0f;
64         la->spotblend= 0.15f;
65         la->att2= 1.0f;
66         la->mode= LA_SHAD_BUF;
67         la->bufsize= 512;
68         la->clipsta= 0.5f;
69         la->clipend= 40.0f;
70         la->shadspotsize= 45.0f;
71         la->samp= 3;
72         la->bias= 1.0f;
73         la->soft= 3.0f;
74         la->compressthresh= 0.05f;
75         la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 
76         la->area_size=la->area_sizey=la->area_sizez= 1.0f; 
77         la->buffers= 1;
78         la->buftype= LA_SHADBUF_HALFWAY;
79         la->ray_samp_method = LA_SAMP_HALTON;
80         la->adapt_thresh = 0.001f;
81         la->preview=NULL;
82         la->falloff_type = LA_FALLOFF_INVSQUARE;
83         la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
84         la->sun_effect_type = 0;
85         la->horizon_brightness = 1.0;
86         la->spread = 1.0;
87         la->sun_brightness = 1.0;
88         la->sun_size = 1.0;
89         la->backscattered_light = 1.0f;
90         la->atm_turbidity = 2.0f;
91         la->atm_inscattering_factor = 1.0f;
92         la->atm_extinction_factor = 1.0f;
93         la->atm_distance_factor = 1.0f;
94         la->sun_intensity = 1.0f;
95         la->skyblendtype= MA_RAMP_ADD;
96         la->skyblendfac= 1.0f;
97         la->sky_colorspace= BLI_XYZ_CIE;
98         la->sky_exposure= 1.0f;
99         la->shadow_frustum_size= 10.0f;
100         
101         curvemapping_initialize(la->curfalloff);
102         return la;
103 }
104
105 Lamp *copy_lamp(Lamp *la)
106 {
107         Lamp *lan;
108         int a;
109         
110         lan= copy_libblock(&la->id);
111
112         for (a=0; a<MAX_MTEX; a++) {
113                 if (lan->mtex[a]) {
114                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
115                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
116                         id_us_plus((ID *)lan->mtex[a]->tex);
117                 }
118         }
119         
120         lan->curfalloff = curvemapping_copy(la->curfalloff);
121
122         if (la->nodetree)
123                 lan->nodetree= ntreeCopyTree(la->nodetree);
124         
125         if (la->preview)
126                 lan->preview = BKE_previewimg_copy(la->preview);
127         
128         return lan;
129 }
130
131 Lamp *localize_lamp(Lamp *la)
132 {
133         Lamp *lan;
134         int a;
135         
136         lan= copy_libblock(&la->id);
137         BLI_remlink(&G.main->lamp, lan);
138
139         for (a=0; a<MAX_MTEX; a++) {
140                 if (lan->mtex[a]) {
141                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_lamp");
142                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
143                         /* free lamp decrements */
144                         id_us_plus((ID *)lan->mtex[a]->tex);
145                 }
146         }
147         
148         lan->curfalloff = curvemapping_copy(la->curfalloff);
149
150         if (la->nodetree)
151                 lan->nodetree= ntreeLocalize(la->nodetree);
152         
153         lan->preview= NULL;
154         
155         return lan;
156 }
157
158 void make_local_lamp(Lamp *la)
159 {
160         Main *bmain= G.main;
161         Object *ob;
162         int is_local= FALSE, is_lib= FALSE;
163
164         /* - only lib users: do nothing
165          * - only local users: set flag
166          * - mixed: make copy
167          */
168         
169         if (la->id.lib==NULL) return;
170         if (la->id.us==1) {
171                 id_clear_lib_data(bmain, &la->id);
172                 return;
173         }
174         
175         ob= bmain->object.first;
176         while (ob) {
177                 if (ob->data==la) {
178                         if (ob->id.lib) is_lib= TRUE;
179                         else is_local= TRUE;
180                 }
181                 ob= ob->id.next;
182         }
183         
184         if (is_local && is_lib == FALSE) {
185                 id_clear_lib_data(bmain, &la->id);
186         }
187         else if (is_local && is_lib) {
188                 Lamp *la_new= copy_lamp(la);
189                 la_new->id.us= 0;
190
191                 /* Remap paths of new ID using old library as base. */
192                 BKE_id_lib_local_paths(bmain, la->id.lib, &la_new->id);
193
194                 ob= bmain->object.first;
195                 while (ob) {
196                         if (ob->data==la) {
197                                 
198                                 if (ob->id.lib==NULL) {
199                                         ob->data= la_new;
200                                         la_new->id.us++;
201                                         la->id.us--;
202                                 }
203                         }
204                         ob= ob->id.next;
205                 }
206         }
207 }
208
209 void free_lamp(Lamp *la)
210 {
211         MTex *mtex;
212         int a;
213
214         for (a=0; a<MAX_MTEX; a++) {
215                 mtex= la->mtex[a];
216                 if (mtex && mtex->tex) mtex->tex->id.us--;
217                 if (mtex) MEM_freeN(mtex);
218         }
219         
220         BKE_free_animdata((ID *)la);
221
222         curvemapping_free(la->curfalloff);
223
224         /* is no lib link block, but lamp extension */
225         if (la->nodetree) {
226                 ntreeFreeTree(la->nodetree);
227                 MEM_freeN(la->nodetree);
228         }
229         
230         BKE_previewimg_free(&la->preview);
231         BKE_icon_delete(&la->id);
232         la->id.icon_id = 0;
233 }
234