Blender  V2.59
world.c
Go to the documentation of this file.
00001 
00002 /*  world.c
00003  * 
00004  * 
00005  * $Id: world.c 36777 2011-05-19 11:54:03Z blendix $
00006  *
00007  * ***** BEGIN GPL LICENSE BLOCK *****
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License
00011  * as published by the Free Software Foundation; either version 2
00012  * of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00022  *
00023  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00024  * All rights reserved.
00025  *
00026  * The Original Code is: all of this file.
00027  *
00028  * Contributor(s): none yet.
00029  *
00030  * ***** END GPL LICENSE BLOCK *****
00031  */
00032 
00038 #include <string.h>
00039 #include <math.h>
00040 #include "MEM_guardedalloc.h"
00041 
00042 #include "DNA_world_types.h"
00043 #include "DNA_scene_types.h"
00044 #include "DNA_texture_types.h"
00045 
00046 #include "BLI_listbase.h"
00047 #include "BLI_utildefines.h"
00048 
00049 #include "BKE_world.h"
00050 #include "BKE_library.h"
00051 #include "BKE_animsys.h"
00052 #include "BKE_global.h"
00053 #include "BKE_main.h"
00054 #include "BKE_icons.h"
00055 
00056 void free_world(World *wrld)
00057 {
00058         MTex *mtex;
00059         int a;
00060         
00061         for(a=0; a<MAX_MTEX; a++) {
00062                 mtex= wrld->mtex[a];
00063                 if(mtex && mtex->tex) mtex->tex->id.us--;
00064                 if(mtex) MEM_freeN(mtex);
00065         }
00066         BKE_previewimg_free(&wrld->preview);
00067 
00068         BKE_free_animdata((ID *)wrld);
00069 
00070         BKE_icon_delete((struct ID*)wrld);
00071         wrld->id.icon_id = 0;
00072 }
00073 
00074 
00075 World *add_world(const char *name)
00076 {
00077         Main *bmain= G.main;
00078         World *wrld;
00079 
00080         wrld= alloc_libblock(&bmain->world, ID_WO, name);
00081         
00082         wrld->horr= 0.05f;
00083         wrld->horg= 0.05f;
00084         wrld->horb= 0.05f;
00085         wrld->zenr= 0.01f;
00086         wrld->zeng= 0.01f;
00087         wrld->zenb= 0.01f;
00088         wrld->skytype= 0;
00089         wrld->stardist= 15.0f;
00090         wrld->starsize= 2.0f;
00091         
00092         wrld->exp= 0.0f;
00093         wrld->exposure=wrld->range= 1.0f;
00094 
00095         wrld->aodist= 10.0f;
00096         wrld->aosamp= 5;
00097         wrld->aoenergy= 1.0f;
00098         wrld->ao_env_energy= 1.0f;
00099         wrld->ao_indirect_energy= 1.0f;
00100         wrld->ao_indirect_bounces= 1;
00101         wrld->aobias= 0.05f;
00102         wrld->ao_samp_method = WO_AOSAMP_HAMMERSLEY;    
00103         wrld->ao_approx_error= 0.25f;
00104         
00105         wrld->preview = NULL;
00106         wrld->miststa = 5.0f;
00107         wrld->mistdist = 25.0f;
00108 
00109         return wrld;
00110 }
00111 
00112 World *copy_world(World *wrld)
00113 {
00114         World *wrldn;
00115         int a;
00116         
00117         wrldn= copy_libblock(wrld);
00118         
00119         for(a=0; a<MAX_MTEX; a++) {
00120                 if(wrld->mtex[a]) {
00121                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "copy_world");
00122                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
00123                         id_us_plus((ID *)wrldn->mtex[a]->tex);
00124                 }
00125         }
00126         
00127         if(wrld->preview)
00128                 wrldn->preview = BKE_previewimg_copy(wrld->preview);
00129 
00130         return wrldn;
00131 }
00132 
00133 World *localize_world(World *wrld)
00134 {
00135         World *wrldn;
00136         int a;
00137         
00138         wrldn= copy_libblock(wrld);
00139         BLI_remlink(&G.main->world, wrldn);
00140         
00141         for(a=0; a<MAX_MTEX; a++) {
00142                 if(wrld->mtex[a]) {
00143                         wrldn->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_world");
00144                         memcpy(wrldn->mtex[a], wrld->mtex[a], sizeof(MTex));
00145                         /* free world decrements */
00146                         id_us_plus((ID *)wrldn->mtex[a]->tex);
00147                 }
00148         }
00149 
00150         wrldn->preview= NULL;
00151         
00152         return wrldn;
00153 }
00154 
00155 void make_local_world(World *wrld)
00156 {
00157         Main *bmain= G.main;
00158         Scene *sce;
00159         int local=0, lib=0;
00160 
00161         /* - only lib users: do nothing
00162                 * - only local users: set flag
00163                 * - mixed: make copy
00164                 */
00165         
00166         if(wrld->id.lib==NULL) return;
00167         if(wrld->id.us==1) {
00168                 wrld->id.lib= NULL;
00169                 wrld->id.flag= LIB_LOCAL;
00170                 new_id(NULL, (ID *)wrld, NULL);
00171                 return;
00172         }
00173         
00174         for(sce= bmain->scene.first; sce && ELEM(0, lib, local); sce= sce->id.next) {
00175                 if(sce->world == wrld) {
00176                         if(sce->id.lib) lib= 1;
00177                         else local= 1;
00178                 }
00179         }
00180 
00181         if(local && lib==0) {
00182                 wrld->id.lib= NULL;
00183                 wrld->id.flag= LIB_LOCAL;
00184                 new_id(NULL, (ID *)wrld, NULL);
00185         }
00186         else if(local && lib) {
00187                 World *wrldn= copy_world(wrld);
00188                 wrldn->id.us= 0;
00189                 
00190                 for(sce= bmain->scene.first; sce; sce= sce->id.next) {
00191                         if(sce->world == wrld) {
00192                                 if(sce->id.lib==NULL) {
00193                                         sce->world= wrldn;
00194                                         wrldn->id.us++;
00195                                         wrld->id.us--;
00196                                 }
00197                         }
00198                 }
00199         }
00200 }