Blender V2.61 - r43446

world.c

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