Blender V2.61 - r43446
|
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 00032 #include <stdlib.h> 00033 00034 #include "MEM_guardedalloc.h" 00035 00036 #include "DNA_lamp_types.h" 00037 #include "DNA_material_types.h" 00038 #include "DNA_object_types.h" 00039 #include "DNA_texture_types.h" 00040 00041 #include "BLI_listbase.h" 00042 #include "BLI_math.h" 00043 #include "BLI_utildefines.h" 00044 00045 #include "BKE_animsys.h" 00046 #include "BKE_colortools.h" 00047 #include "BKE_icons.h" 00048 #include "BKE_global.h" 00049 #include "BKE_lamp.h" 00050 #include "BKE_library.h" 00051 #include "BKE_main.h" 00052 #include "BKE_node.h" 00053 00054 void *add_lamp(const char *name) 00055 { 00056 Lamp *la; 00057 00058 la= alloc_libblock(&G.main->lamp, ID_LA, name); 00059 00060 la->r= la->g= la->b= la->k= 1.0f; 00061 la->haint= la->energy= 1.0f; 00062 la->dist= 25.0f; 00063 la->spotsize= 45.0f; 00064 la->spotblend= 0.15f; 00065 la->att2= 1.0f; 00066 la->mode= LA_SHAD_BUF; 00067 la->bufsize= 512; 00068 la->clipsta= 0.5f; 00069 la->clipend= 40.0f; 00070 la->shadspotsize= 45.0f; 00071 la->samp= 3; 00072 la->bias= 1.0f; 00073 la->soft= 3.0f; 00074 la->compressthresh= 0.05f; 00075 la->ray_samp= la->ray_sampy= la->ray_sampz= 1; 00076 la->area_size=la->area_sizey=la->area_sizez= 1.0f; 00077 la->buffers= 1; 00078 la->buftype= LA_SHADBUF_HALFWAY; 00079 la->ray_samp_method = LA_SAMP_HALTON; 00080 la->adapt_thresh = 0.001f; 00081 la->preview=NULL; 00082 la->falloff_type = LA_FALLOFF_INVSQUARE; 00083 la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f); 00084 la->sun_effect_type = 0; 00085 la->horizon_brightness = 1.0; 00086 la->spread = 1.0; 00087 la->sun_brightness = 1.0; 00088 la->sun_size = 1.0; 00089 la->backscattered_light = 1.0f; 00090 la->atm_turbidity = 2.0f; 00091 la->atm_inscattering_factor = 1.0f; 00092 la->atm_extinction_factor = 1.0f; 00093 la->atm_distance_factor = 1.0f; 00094 la->sun_intensity = 1.0f; 00095 la->skyblendtype= MA_RAMP_ADD; 00096 la->skyblendfac= 1.0f; 00097 la->sky_colorspace= BLI_XYZ_CIE; 00098 la->sky_exposure= 1.0f; 00099 00100 curvemapping_initialize(la->curfalloff); 00101 return la; 00102 } 00103 00104 Lamp *copy_lamp(Lamp *la) 00105 { 00106 Lamp *lan; 00107 int a; 00108 00109 lan= copy_libblock(&la->id); 00110 00111 for(a=0; a<MAX_MTEX; a++) { 00112 if(lan->mtex[a]) { 00113 lan->mtex[a]= MEM_mallocN(sizeof(MTex), "copylamptex"); 00114 memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex)); 00115 id_us_plus((ID *)lan->mtex[a]->tex); 00116 } 00117 } 00118 00119 lan->curfalloff = curvemapping_copy(la->curfalloff); 00120 00121 if(la->nodetree) 00122 lan->nodetree= ntreeCopyTree(la->nodetree); 00123 00124 if(la->preview) 00125 lan->preview = BKE_previewimg_copy(la->preview); 00126 00127 return lan; 00128 } 00129 00130 Lamp *localize_lamp(Lamp *la) 00131 { 00132 Lamp *lan; 00133 int a; 00134 00135 lan= copy_libblock(&la->id); 00136 BLI_remlink(&G.main->lamp, lan); 00137 00138 for(a=0; a<MAX_MTEX; a++) { 00139 if(lan->mtex[a]) { 00140 lan->mtex[a]= MEM_mallocN(sizeof(MTex), "localize_lamp"); 00141 memcpy(lan->mtex[a], la->mtex[a], sizeof(MTex)); 00142 /* free lamp decrements */ 00143 id_us_plus((ID *)lan->mtex[a]->tex); 00144 } 00145 } 00146 00147 lan->curfalloff = curvemapping_copy(la->curfalloff); 00148 00149 if(la->nodetree) 00150 lan->nodetree= ntreeLocalize(la->nodetree); 00151 00152 lan->preview= NULL; 00153 00154 return lan; 00155 } 00156 00157 void make_local_lamp(Lamp *la) 00158 { 00159 Main *bmain= G.main; 00160 Object *ob; 00161 int is_local= FALSE, is_lib= FALSE; 00162 00163 /* - only lib users: do nothing 00164 * - only local users: set flag 00165 * - mixed: make copy 00166 */ 00167 00168 if(la->id.lib==NULL) return; 00169 if(la->id.us==1) { 00170 id_clear_lib_data(bmain, &la->id); 00171 return; 00172 } 00173 00174 ob= bmain->object.first; 00175 while(ob) { 00176 if(ob->data==la) { 00177 if(ob->id.lib) is_lib= TRUE; 00178 else is_local= TRUE; 00179 } 00180 ob= ob->id.next; 00181 } 00182 00183 if(is_local && is_lib == FALSE) { 00184 id_clear_lib_data(bmain, &la->id); 00185 } 00186 else if(is_local && is_lib) { 00187 Lamp *la_new= copy_lamp(la); 00188 la_new->id.us= 0; 00189 00190 /* Remap paths of new ID using old library as base. */ 00191 BKE_id_lib_local_paths(bmain, la->id.lib, &la_new->id); 00192 00193 ob= bmain->object.first; 00194 while(ob) { 00195 if(ob->data==la) { 00196 00197 if(ob->id.lib==NULL) { 00198 ob->data= la_new; 00199 la_new->id.us++; 00200 la->id.us--; 00201 } 00202 } 00203 ob= ob->id.next; 00204 } 00205 } 00206 } 00207 00208 void free_lamp(Lamp *la) 00209 { 00210 MTex *mtex; 00211 int a; 00212 00213 for(a=0; a<MAX_MTEX; a++) { 00214 mtex= la->mtex[a]; 00215 if(mtex && mtex->tex) mtex->tex->id.us--; 00216 if(mtex) MEM_freeN(mtex); 00217 } 00218 00219 BKE_free_animdata((ID *)la); 00220 00221 curvemapping_free(la->curfalloff); 00222 00223 /* is no lib link block, but lamp extension */ 00224 if(la->nodetree) { 00225 ntreeFreeTree(la->nodetree); 00226 MEM_freeN(la->nodetree); 00227 } 00228 00229 BKE_previewimg_free(&la->preview); 00230 BKE_icon_delete(&la->id); 00231 la->id.icon_id = 0; 00232 } 00233