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