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