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