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