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