07a83da3d8c8aa2d985d4c154673ae39ac3d2417
[blender.git] / source / blender / blenkernel / intern / world.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/world.c
29  *  \ingroup bke
30  */
31
32
33 #include <string.h>
34 #include <math.h>
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_world_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_texture_types.h"
40
41 #include "BLI_listbase.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_bpath.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_global.h"
47 #include "BKE_icons.h"
48 #include "BKE_library.h"
49 #include "BKE_library.h"
50 #include "BKE_main.h"
51 #include "BKE_node.h"
52 #include "BKE_world.h"
53
54 void free_world(World *wrld)
55 {
56         MTex *mtex;
57         int a;
58         
59         for (a=0; a<MAX_MTEX; a++) {
60                 mtex= wrld->mtex[a];
61                 if (mtex && mtex->tex) mtex->tex->id.us--;
62                 if (mtex) MEM_freeN(mtex);
63         }
64         BKE_previewimg_free(&wrld->preview);
65
66         BKE_free_animdata((ID *)wrld);
67
68         /* is no lib link block, but world extension */
69         if (wrld->nodetree) {
70                 ntreeFreeTree(wrld->nodetree);
71                 MEM_freeN(wrld->nodetree);
72         }
73
74         BKE_icon_delete((struct ID*)wrld);
75         wrld->id.icon_id = 0;
76 }
77
78
79 World *add_world(const char *name)
80 {
81         Main *bmain= G.main;
82         World *wrld;
83
84         wrld= alloc_libblock(&bmain->world, ID_WO, name);
85         
86         wrld->horr= 0.05f;
87         wrld->horg= 0.05f;
88         wrld->horb= 0.05f;
89         wrld->zenr= 0.01f;
90         wrld->zeng= 0.01f;
91         wrld->zenb= 0.01f;
92         wrld->skytype= 0;
93         wrld->stardist= 15.0f;
94         wrld->starsize= 2.0f;
95         
96         wrld->exp= 0.0f;
97         wrld->exposure=wrld->range= 1.0f;
98
99         wrld->aodist= 10.0f;
100         wrld->aosamp= 5;
101         wrld->aoenergy= 1.0f;
102         wrld->ao_env_energy= 1.0f;
103         wrld->ao_indirect_energy= 1.0f;
104         wrld->ao_indirect_bounces= 1;
105         wrld->aobias= 0.05f;
106         wrld->ao_samp_method = WO_AOSAMP_HAMMERSLEY;    
107         wrld->ao_approx_error= 0.25f;
108         
109         wrld->preview = NULL;
110         wrld->miststa = 5.0f;
111         wrld->mistdist = 25.0f;
112
113         return wrld;
114 }
115
116 World *copy_world(World *wrld)
117 {
118         World *wrldn;
119         int a;
120         
121         wrldn= copy_libblock(&wrld->id);
122         
123         for (a=0; a<MAX_MTEX; a++) {
124                 if (wrld->mtex[a]) {
125                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "copy_world");
126                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
127                         id_us_plus((ID *)wrldn->mtex[a]->tex);
128                 }
129         }
130
131         if (wrld->nodetree)
132                 wrldn->nodetree= ntreeCopyTree(wrld->nodetree);
133         
134         if (wrld->preview)
135                 wrldn->preview = BKE_previewimg_copy(wrld->preview);
136
137         return wrldn;
138 }
139
140 World *localize_world(World *wrld)
141 {
142         World *wrldn;
143         int a;
144         
145         wrldn= copy_libblock(&wrld->id);
146         BLI_remlink(&G.main->world, wrldn);
147         
148         for (a=0; a<MAX_MTEX; a++) {
149                 if (wrld->mtex[a]) {
150                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_world");
151                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
152                         /* free world decrements */
153                         id_us_plus((ID *)wrldn->mtex[a]->tex);
154                 }
155         }
156
157         if (wrld->nodetree)
158                 wrldn->nodetree= ntreeLocalize(wrld->nodetree);
159         
160         wrldn->preview= NULL;
161         
162         return wrldn;
163 }
164
165 void make_local_world(World *wrld)
166 {
167         Main *bmain= G.main;
168         Scene *sce;
169         int is_local= FALSE, is_lib= FALSE;
170
171         /* - only lib users: do nothing
172          * - only local users: set flag
173          * - mixed: make copy
174          */
175         
176         if (wrld->id.lib==NULL) return;
177         if (wrld->id.us==1) {
178                 id_clear_lib_data(bmain, &wrld->id);
179                 return;
180         }
181         
182         for (sce= bmain->scene.first; sce && ELEM(FALSE, is_lib, is_local); sce= sce->id.next) {
183                 if (sce->world == wrld) {
184                         if (sce->id.lib) is_lib= TRUE;
185                         else is_local= TRUE;
186                 }
187         }
188
189         if (is_local && is_lib==FALSE) {
190                 id_clear_lib_data(bmain, &wrld->id);
191         }
192         else if (is_local && is_lib) {
193                 World *wrld_new= copy_world(wrld);
194                 wrld_new->id.us= 0;
195
196                 /* Remap paths of new ID using old library as base. */
197                 BKE_id_lib_local_paths(bmain, wrld->id.lib, &wrld_new->id);
198
199                 for (sce= bmain->scene.first; sce; sce= sce->id.next) {
200                         if (sce->world == wrld) {
201                                 if (sce->id.lib==NULL) {
202                                         sce->world= wrld_new;
203                                         wrld_new->id.us++;
204                                         wrld->id.us--;
205                                 }
206                         }
207                 }
208         }
209 }