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