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