Add localize_lamp and localize_world functions as already existed for materials
[blender.git] / source / blender / blenkernel / intern / world.c
1
2 /*  world.c
3  * 
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 /** \file blender/blenkernel/intern/world.c
34  *  \ingroup bke
35  */
36
37
38 #include <string.h>
39 #include <math.h>
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_world_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_texture_types.h"
45
46 #include "BLI_utildefines.h"
47
48 #include "BKE_world.h"
49 #include "BKE_library.h"
50 #include "BKE_animsys.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_icons.h"
54
55 void free_world(World *wrld)
56 {
57         MTex *mtex;
58         int a;
59         
60         for(a=0; a<MAX_MTEX; a++) {
61                 mtex= wrld->mtex[a];
62                 if(mtex && mtex->tex) mtex->tex->id.us--;
63                 if(mtex) MEM_freeN(mtex);
64         }
65         BKE_previewimg_free(&wrld->preview);
66
67         BKE_free_animdata((ID *)wrld);
68
69         BKE_icon_delete((struct ID*)wrld);
70         wrld->id.icon_id = 0;
71 }
72
73
74 World *add_world(const char *name)
75 {
76         Main *bmain= G.main;
77         World *wrld;
78
79         wrld= alloc_libblock(&bmain->world, ID_WO, name);
80         
81         wrld->horr= 0.05f;
82         wrld->horg= 0.05f;
83         wrld->horb= 0.05f;
84         wrld->zenr= 0.01f;
85         wrld->zeng= 0.01f;
86         wrld->zenb= 0.01f;
87         wrld->skytype= 0;
88         wrld->stardist= 15.0f;
89         wrld->starsize= 2.0f;
90         
91         wrld->exp= 0.0f;
92         wrld->exposure=wrld->range= 1.0f;
93
94         wrld->aodist= 10.0f;
95         wrld->aosamp= 5;
96         wrld->aoenergy= 1.0f;
97         wrld->ao_env_energy= 1.0f;
98         wrld->ao_indirect_energy= 1.0f;
99         wrld->ao_indirect_bounces= 1;
100         wrld->aobias= 0.05f;
101         wrld->ao_samp_method = WO_AOSAMP_HAMMERSLEY;    
102         wrld->ao_approx_error= 0.25f;
103         
104         wrld->preview = NULL;
105         wrld->miststa = 5.0f;
106         wrld->mistdist = 25.0f;
107
108         return wrld;
109 }
110
111 World *copy_world(World *wrld)
112 {
113         World *wrldn;
114         int a;
115         
116         wrldn= copy_libblock(wrld);
117         
118         for(a=0; a<MAX_MTEX; a++) {
119                 if(wrld->mtex[a]) {
120                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "copy_world");
121                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
122                         id_us_plus((ID *)wrldn->mtex[a]->tex);
123                 }
124         }
125         
126         if(wrld->preview)
127                 wrldn->preview = BKE_previewimg_copy(wrld->preview);
128
129         return wrldn;
130 }
131
132 World *localize_world(World *wrld)
133 {
134         World *wrldn;
135         int a;
136         
137         wrldn= copy_libblock(wrld);
138         BLI_remlink(&G.main->world, wrldn);
139         
140         for(a=0; a<MAX_MTEX; a++) {
141                 if(wrld->mtex[a]) {
142                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_world");
143                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
144                         /* free world decrements */
145                         id_us_plus((ID *)wrldn->mtex[a]->tex);
146                 }
147         }
148
149         wrldn->preview= NULL;
150         
151         return wrldn;
152 }
153
154 void make_local_world(World *wrld)
155 {
156         Main *bmain= G.main;
157         Scene *sce;
158         int local=0, lib=0;
159
160         /* - only lib users: do nothing
161                 * - only local users: set flag
162                 * - mixed: make copy
163                 */
164         
165         if(wrld->id.lib==NULL) return;
166         if(wrld->id.us==1) {
167                 wrld->id.lib= NULL;
168                 wrld->id.flag= LIB_LOCAL;
169                 new_id(NULL, (ID *)wrld, NULL);
170                 return;
171         }
172         
173         for(sce= bmain->scene.first; sce && ELEM(0, lib, local); sce= sce->id.next) {
174                 if(sce->world == wrld) {
175                         if(sce->id.lib) lib= 1;
176                         else local= 1;
177                 }
178         }
179
180         if(local && lib==0) {
181                 wrld->id.lib= NULL;
182                 wrld->id.flag= LIB_LOCAL;
183                 new_id(NULL, (ID *)wrld, NULL);
184         }
185         else if(local && lib) {
186                 World *wrldn= copy_world(wrld);
187                 wrldn->id.us= 0;
188                 
189                 for(sce= bmain->scene.first; sce; sce= sce->id.next) {
190                         if(sce->world == wrld) {
191                                 if(sce->id.lib==NULL) {
192                                         sce->world= wrldn;
193                                         wrldn->id.us++;
194                                         wrld->id.us--;
195                                 }
196                         }
197                 }
198         }
199 }