Code refactoring: split lamp functions from object.c into new lamp.c.
[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         
100         curvemapping_initialize(la->curfalloff);
101         return la;
102 }
103
104 Lamp *copy_lamp(Lamp *la)
105 {
106         Lamp *lan;
107         int a;
108         
109         lan= copy_libblock(la);
110
111         for(a=0; a<MAX_MTEX; a++) {
112                 if(lan->mtex[a]) {
113                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex");
114                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
115                         id_us_plus((ID *)lan->mtex[a]->tex);
116                 }
117         }
118         
119         lan->curfalloff = curvemapping_copy(la->curfalloff);
120
121         if(la->nodetree)
122                 lan->nodetree= ntreeCopyTree(la->nodetree);
123         
124         if(la->preview)
125                 lan->preview = BKE_previewimg_copy(la->preview);
126         
127         return lan;
128 }
129
130 Lamp *localize_lamp(Lamp *la)
131 {
132         Lamp *lan;
133         int a;
134         
135         lan= copy_libblock(la);
136         BLI_remlink(&G.main->lamp, lan);
137
138         for(a=0; a<MAX_MTEX; a++) {
139                 if(lan->mtex[a]) {
140                         lan->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_lamp");
141                         memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex));
142                         /* free lamp decrements */
143                         id_us_plus((ID *)lan->mtex[a]->tex);
144                 }
145         }
146         
147         lan->curfalloff = curvemapping_copy(la->curfalloff);
148
149         if(la->nodetree)
150                 lan->nodetree= ntreeLocalize(la->nodetree);
151         
152         lan->preview= NULL;
153         
154         return lan;
155 }
156
157 void make_local_lamp(Lamp *la)
158 {
159         Main *bmain= G.main;
160         Object *ob;
161         int is_local= FALSE, is_lib= FALSE;
162
163         /* - only lib users: do nothing
164                 * - only local users: set flag
165                 * - mixed: make copy
166                 */
167         
168         if(la->id.lib==NULL) return;
169         if(la->id.us==1) {
170                 id_clear_lib_data(bmain, &la->id);
171                 return;
172         }
173         
174         ob= bmain->object.first;
175         while(ob) {
176                 if(ob->data==la) {
177                         if(ob->id.lib) is_lib= TRUE;
178                         else is_local= TRUE;
179                 }
180                 ob= ob->id.next;
181         }
182         
183         if(is_local && is_lib == FALSE) {
184                 id_clear_lib_data(bmain, &la->id);
185         }
186         else if(is_local && is_lib) {
187                 Lamp *lan= copy_lamp(la);
188                 lan->id.us= 0;
189
190                 /* Remap paths of new ID using old library as base. */
191                 BKE_id_lib_local_paths(bmain, &lan->id);
192
193                 ob= bmain->object.first;
194                 while(ob) {
195                         if(ob->data==la) {
196                                 
197                                 if(ob->id.lib==NULL) {
198                                         ob->data= lan;
199                                         lan->id.us++;
200                                         la->id.us--;
201                                 }
202                         }
203                         ob= ob->id.next;
204                 }
205         }
206 }
207
208 void free_lamp(Lamp *la)
209 {
210         MTex *mtex;
211         int a;
212
213         for(a=0; a<MAX_MTEX; a++) {
214                 mtex= la->mtex[a];
215                 if(mtex && mtex->tex) mtex->tex->id.us--;
216                 if(mtex) MEM_freeN(mtex);
217         }
218         
219         BKE_free_animdata((ID *)la);
220
221         curvemapping_free(la->curfalloff);
222
223         /* is no lib link block, but lamp extension */
224         if(la->nodetree) {
225                 ntreeFreeTree(la->nodetree);
226                 MEM_freeN(la->nodetree);
227         }
228         
229         BKE_previewimg_free(&la->preview);
230         BKE_icon_delete(&la->id);
231         la->id.icon_id = 0;
232 }
233