copying texture point density was using inline dupalloc rather then BKE_copy_pointden...
[blender-staging.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 /** \file blender/blenkernel/intern/texture.c
33  *  \ingroup bke
34  */
35
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <math.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "PIL_dynlib.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_math.h"
48 #include "BLI_kdopbvh.h"
49 #include "BLI_utildefines.h"
50
51 #include "DNA_key_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_world_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_color_types.h"
59 #include "DNA_particle_types.h"
60
61 #include "IMB_imbuf.h"
62
63 #include "BKE_plugin_types.h"
64 #include "BKE_utildefines.h"
65 #include "BKE_global.h"
66 #include "BKE_main.h"
67
68 #include "BKE_library.h"
69 #include "BKE_image.h"
70 #include "BKE_material.h"
71 #include "BKE_texture.h"
72 #include "BKE_key.h"
73 #include "BKE_icons.h"
74 #include "BKE_node.h"
75 #include "BKE_animsys.h"
76 #include "BKE_colortools.h"
77
78 /* ------------------------------------------------------------------------- */
79
80 /* All support for plugin textures: */
81 int test_dlerr(const char *name, const char *symbol)
82 {
83         char *err;
84         
85         err= PIL_dynlib_get_error_as_string(NULL);
86         if(err) {
87                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
88                 return 1;
89         }
90         
91         return 0;
92 }
93
94 /* ------------------------------------------------------------------------- */
95
96 void open_plugin_tex(PluginTex *pit)
97 {
98         int (*version)(void);
99         
100         /* init all the happy variables */
101         pit->doit= NULL;
102         pit->pname= NULL;
103         pit->stnames= NULL;
104         pit->varstr= NULL;
105         pit->result= NULL;
106         pit->cfra= NULL;
107         pit->version= 0;
108         pit->instance_init= NULL;
109         
110         /* clear the error list */
111         PIL_dynlib_get_error_as_string(NULL);
112
113         /* no PIL_dynlib_close! multiple opened plugins... */
114         /* if(pit->handle) PIL_dynlib_close(pit->handle); */
115         /* pit->handle= 0; */
116
117         /* open the needed object */
118         pit->handle= PIL_dynlib_open(pit->name);
119         if(test_dlerr(pit->name, pit->name)) return;
120
121         if (pit->handle != NULL) {
122                 /* find the address of the version function */
123                 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
124                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
125                 
126                 if (version != NULL) {
127                         pit->version= version();
128                         if( pit->version >= 2 && pit->version <=6) {
129                                 int (*info_func)(PluginInfo *);
130                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
131
132                                 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
133                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
134                                         info->instance_init = NULL;
135
136                                         info_func(info);
137
138                                         pit->doit= (int(*)(void)) info->tex_doit;
139                                         pit->callback= (void(*)(unsigned short)) info->callback;
140                                         pit->stypes= info->stypes;
141                                         pit->vars= info->nvars;
142                                         pit->pname= info->name;
143                                         pit->stnames= info->snames;
144                                         pit->varstr= info->varstr;
145                                         pit->result= info->result;
146                                         pit->cfra= info->cfra;
147                                         pit->instance_init = info->instance_init;
148                                         if (info->init) info->init();
149                                 }
150                                 MEM_freeN(info);
151                         } else {
152                                 printf ("Plugin returned unrecognized version number\n");
153                                 return;
154                         }
155                 }
156         }
157 }
158
159 /* ------------------------------------------------------------------------- */
160
161 /* very badlevel define to bypass linking with BIF_interface.h */
162 #define INT     96
163 #define FLO     128
164
165 PluginTex *add_plugin_tex(char *str)
166 {
167         PluginTex *pit;
168         VarStruct *varstr;
169         int a;
170         
171         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
172         
173         strcpy(pit->name, str);
174         open_plugin_tex(pit);
175         
176         if(pit->doit==NULL) {
177                 if(pit->handle==NULL) {;} //XXX error("no plugin: %s", str);
178                 else {;} //XXX error("in plugin: %s", str);
179                 MEM_freeN(pit);
180                 return NULL;
181         }
182         
183         varstr= pit->varstr;
184         for(a=0; a<pit->vars; a++, varstr++) {
185                 if( (varstr->type & FLO)==FLO)
186                         pit->data[a]= varstr->def;
187                 else if( (varstr->type & INT)==INT)
188                         *((int *)(pit->data+a))= (int) varstr->def;
189         }
190
191         if (pit->instance_init)
192                 pit->instance_init((void *) pit->data);
193
194         return pit;
195 }
196
197 /* ------------------------------------------------------------------------- */
198
199 void free_plugin_tex(PluginTex *pit)
200 {
201         if(pit==NULL) return;
202                 
203         /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
204         MEM_freeN(pit); 
205 }
206
207 /* ****************** Mapping ******************* */
208
209 TexMapping *add_mapping(void)
210 {
211         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
212         
213         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
214         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
215         unit_m4(texmap->mat);
216         
217         return texmap;
218 }
219
220 void init_mapping(TexMapping *texmap)
221 {
222         float eul[3], smat[3][3], rmat[3][3], mat[3][3];
223         
224         size_to_mat3( smat,texmap->size);
225         
226         eul[0]= DEG2RADF(texmap->rot[0]);
227         eul[1]= DEG2RADF(texmap->rot[1]);
228         eul[2]= DEG2RADF(texmap->rot[2]);
229         eul_to_mat3( rmat,eul);
230         
231         mul_m3_m3m3(mat, rmat, smat);
232         
233         copy_m4_m3(texmap->mat, mat);
234         VECCOPY(texmap->mat[3], texmap->loc);
235
236 }
237
238 /* ****************** COLORBAND ******************* */
239
240 void init_colorband(ColorBand *coba, int rangetype)
241 {
242         int a;
243         
244         coba->data[0].pos= 0.0;
245         coba->data[1].pos= 1.0;
246         
247         if(rangetype==0) {
248                 coba->data[0].r= 0.0;
249                 coba->data[0].g= 0.0;
250                 coba->data[0].b= 0.0;
251                 coba->data[0].a= 0.0;
252                 
253                 coba->data[1].r= 1.0;
254                 coba->data[1].g= 1.0;
255                 coba->data[1].b= 1.0;
256                 coba->data[1].a= 1.0;
257         }
258         else {
259                 coba->data[0].r= 0.0;
260                 coba->data[0].g= 0.0;
261                 coba->data[0].b= 0.0;
262                 coba->data[0].a= 1.0;
263                 
264                 coba->data[1].r= 1.0;
265                 coba->data[1].g= 1.0;
266                 coba->data[1].b= 1.0;
267                 coba->data[1].a= 1.0;
268         }
269         
270         for(a=2; a<MAXCOLORBAND; a++) {
271                 coba->data[a].r= 0.5;
272                 coba->data[a].g= 0.5;
273                 coba->data[a].b= 0.5;
274                 coba->data[a].a= 1.0;
275                 coba->data[a].pos= 0.5;
276         }
277         
278         coba->tot= 2;
279         
280 }
281
282 ColorBand *add_colorband(int rangetype)
283 {
284         ColorBand *coba;
285         
286         coba= MEM_callocN( sizeof(ColorBand), "colorband");
287         init_colorband(coba, rangetype);
288         
289         return coba;
290 }
291
292 /* ------------------------------------------------------------------------- */
293
294 int do_colorband(ColorBand *coba, float in, float out[4])
295 {
296         CBData *cbd1, *cbd2, *cbd0, *cbd3;
297         float fac, mfac, t[4];
298         int a;
299         
300         if(coba==NULL || coba->tot==0) return 0;
301         
302         cbd1= coba->data;
303         if(coba->tot==1) {
304                 out[0]= cbd1->r;
305                 out[1]= cbd1->g;
306                 out[2]= cbd1->b;
307                 out[3]= cbd1->a;
308         }
309         else {
310                 if(in <= cbd1->pos && coba->ipotype<2) {
311                         out[0]= cbd1->r;
312                         out[1]= cbd1->g;
313                         out[2]= cbd1->b;
314                         out[3]= cbd1->a;
315                 }
316                 else {
317                         CBData left, right;
318                         
319                         /* we're looking for first pos > in */
320                         for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
321                                 
322                         if(a==coba->tot) {
323                                 cbd2= cbd1-1;
324                                 right= *cbd2;
325                                 right.pos= 1.0f;
326                                 cbd1= &right;
327                         }
328                         else if(a==0) {
329                                 left= *cbd1;
330                                 left.pos= 0.0f;
331                                 cbd2= &left;
332                         }
333                         else cbd2= cbd1-1;
334                         
335                         if(in >= cbd1->pos && coba->ipotype<2) {
336                                 out[0]= cbd1->r;
337                                 out[1]= cbd1->g;
338                                 out[2]= cbd1->b;
339                                 out[3]= cbd1->a;
340                         }
341                         else {
342                 
343                                 if(cbd2->pos!=cbd1->pos)
344                                         fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
345                                 else {
346                                         /* was setting to 0.0 in 2.56 & previous, but this
347                                          * is incorrect for the last element, see [#26732] */
348                                         fac= (a != coba->tot) ? 0.0f : 1.0f;
349                                 }
350                                 
351                                 if (coba->ipotype==4) {
352                                         /* constant */
353                                         out[0]= cbd2->r;
354                                         out[1]= cbd2->g;
355                                         out[2]= cbd2->b;
356                                         out[3]= cbd2->a;
357                                         return 1;
358                                 }
359                                 
360                                 if(coba->ipotype>=2) {
361                                         /* ipo from right to left: 3 2 1 0 */
362                                         
363                                         if(a>=coba->tot-1) cbd0= cbd1;
364                                         else cbd0= cbd1+1;
365                                         if(a<2) cbd3= cbd2;
366                                         else cbd3= cbd2-1;
367                                         
368                                         CLAMP(fac, 0.0f, 1.0f);
369                                         
370                                         if(coba->ipotype==3)
371                                                 key_curve_position_weights(fac, t, KEY_CARDINAL);
372                                         else
373                                                 key_curve_position_weights(fac, t, KEY_BSPLINE);
374
375                                         out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
376                                         out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
377                                         out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
378                                         out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
379                                         CLAMP(out[0], 0.0f, 1.0f);
380                                         CLAMP(out[1], 0.0f, 1.0f);
381                                         CLAMP(out[2], 0.0f, 1.0f);
382                                         CLAMP(out[3], 0.0f, 1.0f);
383                                 }
384                                 else {
385                                 
386                                         if(coba->ipotype==1) {  /* EASE */
387                                                 mfac= fac*fac;
388                                                 fac= 3.0f*mfac-2.0f*mfac*fac;
389                                         }
390                                         mfac= 1.0f-fac;
391                                         
392                                         out[0]= mfac*cbd1->r + fac*cbd2->r;
393                                         out[1]= mfac*cbd1->g + fac*cbd2->g;
394                                         out[2]= mfac*cbd1->b + fac*cbd2->b;
395                                         out[3]= mfac*cbd1->a + fac*cbd2->a;
396                                 }
397                         }
398                 }
399         }
400         return 1;       /* OK */
401 }
402
403 void colorband_table_RGBA(ColorBand *coba, float **array, int *size)
404 {
405         int a;
406         
407         *size = CM_TABLE+1;
408         *array = MEM_callocN(sizeof(float)*(*size)*4, "ColorBand");
409
410         for(a=0; a<*size; a++)
411                 do_colorband(coba, (float)a/(float)CM_TABLE, &(*array)[a*4]);
412 }
413
414 int vergcband(const void *a1, const void *a2)
415 {
416         const CBData *x1=a1, *x2=a2;
417
418         if( x1->pos > x2->pos ) return 1;
419         else if( x1->pos < x2->pos) return -1;
420         return 0;
421 }
422
423 CBData *colorband_element_add(struct ColorBand *coba, float position)
424 {
425         int a;
426
427         if(coba->tot==MAXCOLORBAND) {
428                 return NULL;
429         }
430         else if(coba->tot > 0) {
431                 CBData *xnew;
432                 float col[4];
433
434                 do_colorband(coba, position, col);
435
436                 xnew = &coba->data[coba->tot];
437                 xnew->pos = position;
438
439                 xnew->r = col[0];
440                 xnew->g = col[1];
441                 xnew->b = col[2];
442                 xnew->a = col[3];
443         }
444
445         coba->tot++;
446         coba->cur = coba->tot-1;
447
448         for(a = 0; a < coba->tot; a++)
449                 coba->data[a].cur = a;
450
451         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
452
453         for(a = 0; a < coba->tot; a++) {
454                 if(coba->data[a].cur == coba->cur) {
455                         coba->cur = a;
456                         break;
457                 }
458         }
459
460         return coba->data + coba->cur;
461 }
462
463 int colorband_element_remove(struct ColorBand *coba, int index)
464 {
465         int a;
466
467         if(coba->tot < 2)
468                 return 0;
469
470         if(index < 0 || index >= coba->tot)
471                 return 0;
472
473         for(a = index; a < coba->tot; a++) {
474                 coba->data[a] = coba->data[a + 1];
475         }
476         if(coba->cur) coba->cur--;
477         coba->tot--;
478         return 1;
479 }
480
481 /* ******************* TEX ************************ */
482
483 void free_texture(Tex *tex)
484 {
485         free_plugin_tex(tex->plugin);
486         
487         if(tex->coba) MEM_freeN(tex->coba);
488         if(tex->env) BKE_free_envmap(tex->env);
489         if(tex->pd) BKE_free_pointdensity(tex->pd);
490         if(tex->vd) BKE_free_voxeldata(tex->vd);
491         BKE_free_animdata((struct ID *)tex);
492         
493         BKE_previewimg_free(&tex->preview);
494         BKE_icon_delete((struct ID*)tex);
495         tex->id.icon_id = 0;
496         
497         if(tex->nodetree) {
498                 ntreeFreeTree(tex->nodetree);
499                 MEM_freeN(tex->nodetree);
500         }
501 }
502
503 /* ------------------------------------------------------------------------- */
504
505 void default_tex(Tex *tex)
506 {
507         PluginTex *pit;
508         VarStruct *varstr;
509         int a;
510
511         tex->type= TEX_CLOUDS;
512         tex->stype= 0;
513         tex->flag= TEX_CHECKER_ODD;
514         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
515         tex->extend= TEX_REPEAT;
516         tex->cropxmin= tex->cropymin= 0.0;
517         tex->cropxmax= tex->cropymax= 1.0;
518         tex->texfilter = TXF_EWA;
519         tex->afmax = 8;
520         tex->xrepeat= tex->yrepeat= 1;
521         tex->fie_ima= 2;
522         tex->sfra= 1;
523         tex->frames= 0;
524         tex->offset= 0;
525         tex->noisesize= 0.25;
526         tex->noisedepth= 2;
527         tex->turbul= 5.0;
528         tex->nabla= 0.025;      // also in do_versions
529         tex->bright= 1.0;
530         tex->contrast= 1.0;
531         tex->saturation= 1.0;
532         tex->filtersize= 1.0;
533         tex->rfac= 1.0;
534         tex->gfac= 1.0;
535         tex->bfac= 1.0;
536         /* newnoise: init. */
537         tex->noisebasis = 0;
538         tex->noisebasis2 = 0;
539         /* musgrave */
540         tex->mg_H = 1.0;
541         tex->mg_lacunarity = 2.0;
542         tex->mg_octaves = 2.0;
543         tex->mg_offset = 1.0;
544         tex->mg_gain = 1.0;
545         tex->ns_outscale = 1.0;
546         /* distnoise */
547         tex->dist_amount = 1.0;
548         /* voronoi */
549         tex->vn_w1 = 1.0;
550         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
551         tex->vn_mexp = 2.5;
552         tex->vn_distm = 0;
553         tex->vn_coltype = 0;
554
555         if (tex->env) {
556                 tex->env->stype=ENV_ANIM;
557                 tex->env->clipsta=0.1;
558                 tex->env->clipend=100;
559                 tex->env->cuberes=600;
560                 tex->env->depth=0;
561         }
562
563         if (tex->pd) {
564                 tex->pd->radius = 0.3f;
565                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
566         }
567         
568         if (tex->vd) {
569                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
570                 tex->vd->interp_type=TEX_VD_LINEAR;
571                 tex->vd->file_format=TEX_VD_SMOKE;
572         }
573         pit = tex->plugin;
574         if (pit) {
575                 varstr= pit->varstr;
576                 if(varstr) {
577                         for(a=0; a<pit->vars; a++, varstr++) {
578                                 pit->data[a] = varstr->def;
579                         }
580                 }
581         }
582         
583         tex->iuser.fie_ima= 2;
584         tex->iuser.ok= 1;
585         tex->iuser.frames= 100;
586         tex->iuser.sfra= 1;
587         
588         tex->preview = NULL;
589 }
590
591 void tex_set_type(Tex *tex, int type)
592 {
593         switch(type) {
594                         
595                 case TEX_VOXELDATA:
596                         if (tex->vd == NULL)
597                                 tex->vd = BKE_add_voxeldata();
598                         break;
599                 case TEX_POINTDENSITY:
600                         if (tex->pd == NULL)
601                                 tex->pd = BKE_add_pointdensity();
602                         break;
603                 case TEX_ENVMAP:
604                         if (tex->env == NULL)
605                                 tex->env = BKE_add_envmap();
606                         break;
607         }
608         
609         tex->type = type;
610 }
611
612 /* ------------------------------------------------------------------------- */
613
614 Tex *add_texture(const char *name)
615 {
616         Main *bmain= G.main;
617         Tex *tex;
618
619         tex= alloc_libblock(&bmain->tex, ID_TE, name);
620         
621         default_tex(tex);
622         
623         return tex;
624 }
625
626 /* ------------------------------------------------------------------------- */
627
628 void default_mtex(MTex *mtex)
629 {
630         mtex->texco= TEXCO_ORCO;
631         mtex->mapto= MAP_COL;
632         mtex->object= NULL;
633         mtex->projx= PROJ_X;
634         mtex->projy= PROJ_Y;
635         mtex->projz= PROJ_Z;
636         mtex->mapping= MTEX_FLAT;
637         mtex->ofs[0]= 0.0;
638         mtex->ofs[1]= 0.0;
639         mtex->ofs[2]= 0.0;
640         mtex->size[0]= 1.0;
641         mtex->size[1]= 1.0;
642         mtex->size[2]= 1.0;
643         mtex->tex= NULL;
644         mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
645         mtex->colormodel= 0;
646         mtex->r= 1.0;
647         mtex->g= 0.0;
648         mtex->b= 1.0;
649         mtex->k= 1.0;
650         mtex->def_var= 1.0;
651         mtex->blendtype= MTEX_BLEND;
652         mtex->colfac= 1.0;
653         mtex->norfac= 1.0;
654         mtex->varfac= 1.0;
655         mtex->dispfac=0.2;
656         mtex->colspecfac= 1.0f;
657         mtex->mirrfac= 1.0f;
658         mtex->alphafac= 1.0f;
659         mtex->difffac= 1.0f;
660         mtex->specfac= 1.0f;
661         mtex->emitfac= 1.0f;
662         mtex->hardfac= 1.0f;
663         mtex->raymirrfac= 1.0f;
664         mtex->translfac= 1.0f;
665         mtex->ambfac= 1.0f;
666         mtex->colemitfac= 1.0f;
667         mtex->colreflfac= 1.0f;
668         mtex->coltransfac= 1.0f;
669         mtex->densfac= 1.0f;
670         mtex->scatterfac= 1.0f;
671         mtex->reflfac= 1.0f;
672         mtex->shadowfac= 1.0f;
673         mtex->zenupfac= 1.0f;
674         mtex->zendownfac= 1.0f;
675         mtex->blendfac= 1.0f;
676         mtex->timefac= 1.0f;
677         mtex->lengthfac= 1.0f;
678         mtex->clumpfac= 1.0f;
679         mtex->kinkfac= 1.0f;
680         mtex->roughfac= 1.0f;
681         mtex->padensfac= 1.0f;
682         mtex->lifefac= 1.0f;
683         mtex->sizefac= 1.0f;
684         mtex->ivelfac= 1.0f;
685         mtex->dampfac= 1.0f;
686         mtex->gravityfac= 1.0f;
687         mtex->fieldfac= 1.0f;
688         mtex->normapspace= MTEX_NSPACE_TANGENT;
689 }
690
691
692 /* ------------------------------------------------------------------------- */
693
694 MTex *add_mtex(void)
695 {
696         MTex *mtex;
697         
698         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
699         
700         default_mtex(mtex);
701         
702         return mtex;
703 }
704
705 /* slot -1 for first free ID */
706 MTex *add_mtex_id(ID *id, int slot)
707 {
708         MTex **mtex_ar;
709         short act;
710
711         give_active_mtex(id, &mtex_ar, &act);
712
713         if(mtex_ar==NULL) {
714                 return NULL;
715         }
716         
717         if(slot==-1) {
718                 /* find first free */
719                 int i;          
720                 for (i= 0; i < MAX_MTEX; i++) {
721                         if (!mtex_ar[i]) {
722                                 slot= i;
723                                 break;
724                         }
725                 }
726                 if(slot == -1) {
727                         return NULL;
728                 }
729         }
730         else {
731                 /* make sure slot is valid */
732                 if(slot < 0 || slot >= MAX_MTEX) {
733                         return NULL;
734                 }
735         }
736
737         if (mtex_ar[slot]) {
738                 id_us_min((ID *)mtex_ar[slot]->tex);
739                 MEM_freeN(mtex_ar[slot]);
740                 mtex_ar[slot]= NULL;
741         }
742
743         mtex_ar[slot]= add_mtex();
744
745         return mtex_ar[slot];
746 }
747
748 /* ------------------------------------------------------------------------- */
749
750 Tex *copy_texture(Tex *tex)
751 {
752         Tex *texn;
753         
754         texn= copy_libblock(tex);
755         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
756         else texn->ima= NULL;
757         
758         if(texn->plugin) {
759                 texn->plugin= MEM_dupallocN(texn->plugin);
760                 open_plugin_tex(texn->plugin);
761         }
762         
763         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
764         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
765         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
766         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
767         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
768
769         if(tex->nodetree) {
770                 ntreeEndExecTree(tex->nodetree);
771                 texn->nodetree= ntreeCopyTree(tex->nodetree); 
772         }
773         
774         return texn;
775 }
776
777 /* texture copy without adding to main dbase */
778 Tex *localize_texture(Tex *tex)
779 {
780         Tex *texn;
781         
782         texn= copy_libblock(tex);
783         BLI_remlink(&G.main->tex, texn);
784         
785         /* image texture: free_texture also doesn't decrease */
786         
787         if(texn->plugin) {
788                 texn->plugin= MEM_dupallocN(texn->plugin);
789                 open_plugin_tex(texn->plugin);
790         }
791         
792         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
793         if(texn->env) {
794                 texn->env= BKE_copy_envmap(texn->env);
795                 id_us_min(&texn->env->ima->id);
796         }
797         if(texn->pd) texn->pd= BKE_copy_pointdensity(texn->pd);
798         if(texn->vd) {
799                 texn->vd= MEM_dupallocN(texn->vd);
800                 if(texn->vd->dataset)
801                         texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
802         }
803         
804         texn->preview = NULL;
805         
806         if(tex->nodetree) {
807                 texn->nodetree= ntreeLocalize(tex->nodetree);
808         }
809         
810         return texn;
811 }
812
813
814 /* ------------------------------------------------------------------------- */
815
816 void make_local_texture(Tex *tex)
817 {
818         Main *bmain= G.main;
819         Tex *texn;
820         Material *ma;
821         World *wrld;
822         Lamp *la;
823         Brush *br;
824         ParticleSettings *pa;
825         int a, local=0, lib=0;
826
827         /* - only lib users: do nothing
828                 * - only local users: set flag
829                 * - mixed: make copy
830                 */
831         
832         if(tex->id.lib==NULL) return;
833
834         /* special case: ima always local immediately */
835         if(tex->ima) {
836                 tex->ima->id.lib= NULL;
837                 tex->ima->id.flag= LIB_LOCAL;
838                 new_id(&bmain->image, (ID *)tex->ima, NULL);
839         }
840
841         if(tex->id.us==1) {
842                 tex->id.lib= NULL;
843                 tex->id.flag= LIB_LOCAL;
844                 new_id(&bmain->tex, (ID *)tex, NULL);
845
846                 return;
847         }
848         
849         ma= bmain->mat.first;
850         while(ma) {
851                 for(a=0; a<MAX_MTEX; a++) {
852                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
853                                 if(ma->id.lib) lib= 1;
854                                 else local= 1;
855                         }
856                 }
857                 ma= ma->id.next;
858         }
859         la= bmain->lamp.first;
860         while(la) {
861                 for(a=0; a<MAX_MTEX; a++) {
862                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
863                                 if(la->id.lib) lib= 1;
864                                 else local= 1;
865                         }
866                 }
867                 la= la->id.next;
868         }
869         wrld= bmain->world.first;
870         while(wrld) {
871                 for(a=0; a<MAX_MTEX; a++) {
872                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
873                                 if(wrld->id.lib) lib= 1;
874                                 else local= 1;
875                         }
876                 }
877                 wrld= wrld->id.next;
878         }
879         br= bmain->brush.first;
880         while(br) {
881                 if(br->mtex.tex==tex) {
882                         if(br->id.lib) lib= 1;
883                         else local= 1;
884                 }
885                 br= br->id.next;
886         }
887         pa= bmain->particle.first;
888         while(pa) {
889                 for(a=0; a<MAX_MTEX; a++) {
890                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
891                                 if(pa->id.lib) lib= 1;
892                                 else local= 1;
893                         }
894                 }
895                 pa= pa->id.next;
896         }
897         
898         if(local && lib==0) {
899                 tex->id.lib= NULL;
900                 tex->id.flag= LIB_LOCAL;
901                 new_id(&bmain->tex, (ID *)tex, NULL);
902         }
903         else if(local && lib) {
904                 texn= copy_texture(tex);
905                 texn->id.us= 0;
906                 
907                 ma= bmain->mat.first;
908                 while(ma) {
909                         for(a=0; a<MAX_MTEX; a++) {
910                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
911                                         if(ma->id.lib==NULL) {
912                                                 ma->mtex[a]->tex= texn;
913                                                 texn->id.us++;
914                                                 tex->id.us--;
915                                         }
916                                 }
917                         }
918                         ma= ma->id.next;
919                 }
920                 la= bmain->lamp.first;
921                 while(la) {
922                         for(a=0; a<MAX_MTEX; a++) {
923                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
924                                         if(la->id.lib==NULL) {
925                                                 la->mtex[a]->tex= texn;
926                                                 texn->id.us++;
927                                                 tex->id.us--;
928                                         }
929                                 }
930                         }
931                         la= la->id.next;
932                 }
933                 wrld= bmain->world.first;
934                 while(wrld) {
935                         for(a=0; a<MAX_MTEX; a++) {
936                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
937                                         if(wrld->id.lib==NULL) {
938                                                 wrld->mtex[a]->tex= texn;
939                                                 texn->id.us++;
940                                                 tex->id.us--;
941                                         }
942                                 }
943                         }
944                         wrld= wrld->id.next;
945                 }
946                 br= bmain->brush.first;
947                 while(br) {
948                         if(br->mtex.tex==tex) {
949                                 if(br->id.lib==NULL) {
950                                         br->mtex.tex= texn;
951                                         texn->id.us++;
952                                         tex->id.us--;
953                                 }
954                         }
955                         br= br->id.next;
956                 }
957                 pa= bmain->particle.first;
958                 while(pa) {
959                         for(a=0; a<MAX_MTEX; a++) {
960                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
961                                         if(pa->id.lib==NULL) {
962                                                 pa->mtex[a]->tex= texn;
963                                                 texn->id.us++;
964                                                 tex->id.us--;
965                                         }
966                                 }
967                         }
968                         pa= pa->id.next;
969                 }
970         }
971 }
972
973 /* ------------------------------------------------------------------------- */
974
975 void autotexname(Tex *tex)
976 {
977         Main *bmain= G.main;
978         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
979                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
980                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
981         Image *ima;
982         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
983         
984         if(tex) {
985                 if(tex->use_nodes) {
986                         new_id(&bmain->tex, (ID *)tex, "Noddy");
987                 }
988                 else
989                 if(tex->type==TEX_IMAGE) {
990                         ima= tex->ima;
991                         if(ima) {
992                                 strcpy(di, ima->name);
993                                 BLI_splitdirstring(di, fi);
994                                 strcpy(di, "I.");
995                                 strcat(di, fi);
996                                 new_id(&bmain->tex, (ID *)tex, di);
997                         }
998                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
999                 }
1000                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1001                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1002         }
1003 }
1004
1005 /* ------------------------------------------------------------------------- */
1006
1007 Tex *give_current_object_texture(Object *ob)
1008 {
1009         Material *ma;
1010         Tex *tex= NULL;
1011         
1012         if(ob==NULL) return NULL;
1013         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1014         
1015         if(ob->type==OB_LAMP) {
1016                 tex= give_current_lamp_texture(ob->data);
1017         } else {
1018                 ma= give_current_material(ob, ob->actcol);
1019                 tex= give_current_material_texture(ma);
1020         }
1021         
1022         return tex;
1023 }
1024
1025 Tex *give_current_lamp_texture(Lamp *la)
1026 {
1027         MTex *mtex= NULL;
1028         Tex *tex= NULL;
1029
1030         if(la) {
1031                 mtex= la->mtex[(int)(la->texact)];
1032                 if(mtex) tex= mtex->tex;
1033         }
1034
1035         return tex;
1036 }
1037
1038 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1039 {
1040         int act= la->texact;
1041
1042         if(la->mtex[act] && la->mtex[act]->tex)
1043                 id_us_min(&la->mtex[act]->tex->id);
1044
1045         if(newtex) {
1046                 if(!la->mtex[act]) {
1047                         la->mtex[act]= add_mtex();
1048                         la->mtex[act]->texco= TEXCO_GLOB;
1049                 }
1050                 
1051                 la->mtex[act]->tex= newtex;
1052                 id_us_plus(&newtex->id);
1053         }
1054         else if(la->mtex[act]) {
1055                 MEM_freeN(la->mtex[act]);
1056                 la->mtex[act]= NULL;
1057         }
1058 }
1059
1060 bNode *give_current_material_texture_node(Material *ma)
1061 {
1062         if(ma && ma->use_nodes && ma->nodetree)
1063                 return nodeGetActiveID(ma->nodetree, ID_TE);
1064         
1065         return NULL;
1066 }
1067
1068 Tex *give_current_material_texture(Material *ma)
1069 {
1070         MTex *mtex= NULL;
1071         Tex *tex= NULL;
1072         bNode *node;
1073         
1074         if(ma && ma->use_nodes && ma->nodetree) {
1075                 /* first check texture, then material, this works together
1076                    with a hack that clears the active ID flag for textures on
1077                    making a material node active */
1078                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1079
1080                 if(node) {
1081                         tex= (Tex *)node->id;
1082                         ma= NULL;
1083                 }
1084                 else {
1085                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1086                         if(node) {
1087                                 ma= (Material*)node->id;
1088                                 if(ma) {
1089                                         mtex= ma->mtex[(int)(ma->texact)];
1090                                         if(mtex) tex= mtex->tex;
1091                                 }
1092                         }
1093                 }
1094                 return tex;
1095         }
1096
1097         if(ma) {
1098                 mtex= ma->mtex[(int)(ma->texact)];
1099                 if(mtex) tex= mtex->tex;
1100         }
1101         
1102         return tex;
1103 }
1104
1105 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1106 {
1107         switch(GS(id->name)) {
1108         case ID_MA:
1109                 *mtex_ar=               ((Material *)id)->mtex;
1110                 if(act) *act=   (((Material *)id)->texact);
1111                 break;
1112         case ID_WO:
1113                 *mtex_ar=               ((World *)id)->mtex;
1114                 if(act) *act=   (((World *)id)->texact);
1115                 break;
1116         case ID_LA:
1117                 *mtex_ar=               ((Lamp *)id)->mtex;
1118                 if(act) *act=   (((Lamp *)id)->texact);
1119                 break;
1120         case ID_PA:
1121                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1122                 if(act) *act=   (((ParticleSettings *)id)->texact);
1123                 break;
1124         default:
1125                 *mtex_ar = NULL;
1126                 if(act) *act=   0;
1127                 return FALSE;
1128         }
1129
1130         return TRUE;
1131 }
1132
1133 void set_active_mtex(ID *id, short act)
1134 {
1135         if(act<0)                               act= 0;
1136         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1137
1138         switch(GS(id->name)) {
1139         case ID_MA:
1140                 ((Material *)id)->texact= act;
1141                 break;
1142         case ID_WO:
1143                 ((World *)id)->texact= act;
1144                 break;
1145         case ID_LA:
1146                 ((Lamp *)id)->texact= act;
1147                 break;
1148         case ID_PA:
1149                 ((ParticleSettings *)id)->texact= act;
1150                 break;
1151         }
1152 }
1153
1154 void set_current_material_texture(Material *ma, Tex *newtex)
1155 {
1156         Tex *tex= NULL;
1157         bNode *node;
1158         
1159         if(ma && ma->use_nodes && ma->nodetree) {
1160                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1161
1162                 if(node) {
1163                         tex= (Tex *)node->id;
1164                         id_us_min(&tex->id);
1165                         node->id= &newtex->id;
1166                         id_us_plus(&newtex->id);
1167                         ma= NULL;
1168                 }
1169                 else {
1170                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1171                         if(node)
1172                                 ma= (Material*)node->id;
1173                 }
1174         }
1175         if(ma) {
1176                 int act= (int)ma->texact;
1177
1178                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1179                 id_us_min(&tex->id);
1180
1181                 if(newtex) {
1182                         if(!ma->mtex[act])
1183                                 ma->mtex[act]= add_mtex();
1184                         
1185                         ma->mtex[act]->tex= newtex;
1186                         id_us_plus(&newtex->id);
1187                 }
1188                 else if(ma->mtex[act]) {
1189                         MEM_freeN(ma->mtex[act]);
1190                         ma->mtex[act]= NULL;
1191                 }
1192         }
1193 }
1194
1195 Tex *give_current_world_texture(World *world)
1196 {
1197         MTex *mtex= NULL;
1198         Tex *tex= NULL;
1199         
1200         if(!world) return NULL;
1201         
1202         mtex= world->mtex[(int)(world->texact)];
1203         if(mtex) tex= mtex->tex;
1204         
1205         return tex;
1206 }
1207
1208 void set_current_world_texture(World *wo, Tex *newtex)
1209 {
1210         int act= wo->texact;
1211
1212         if(wo->mtex[act] && wo->mtex[act]->tex)
1213                 id_us_min(&wo->mtex[act]->tex->id);
1214
1215         if(newtex) {
1216                 if(!wo->mtex[act]) {
1217                         wo->mtex[act]= add_mtex();
1218                         wo->mtex[act]->texco= TEXCO_VIEW;
1219                 }
1220                 
1221                 wo->mtex[act]->tex= newtex;
1222                 id_us_plus(&newtex->id);
1223         }
1224         else if(wo->mtex[act]) {
1225                 MEM_freeN(wo->mtex[act]);
1226                 wo->mtex[act]= NULL;
1227         }
1228 }
1229
1230 Tex *give_current_brush_texture(Brush *br)
1231 {
1232         return br->mtex.tex;
1233 }
1234
1235 void set_current_brush_texture(Brush *br, Tex *newtex)
1236 {
1237         if(br->mtex.tex)
1238                 id_us_min(&br->mtex.tex->id);
1239
1240         if(newtex) {
1241                 br->mtex.tex= newtex;
1242                 id_us_plus(&newtex->id);
1243         }
1244 }
1245
1246 Tex *give_current_particle_texture(ParticleSettings *part)
1247 {
1248         MTex *mtex= NULL;
1249         Tex *tex= NULL;
1250         
1251         if(!part) return NULL;
1252         
1253         mtex= part->mtex[(int)(part->texact)];
1254         if(mtex) tex= mtex->tex;
1255         
1256         return tex;
1257 }
1258
1259 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1260 {
1261         int act= part->texact;
1262
1263         if(part->mtex[act] && part->mtex[act]->tex)
1264                 id_us_min(&part->mtex[act]->tex->id);
1265
1266         if(newtex) {
1267                 if(!part->mtex[act]) {
1268                         part->mtex[act]= add_mtex();
1269                         part->mtex[act]->texco= TEXCO_ORCO;
1270                         part->mtex[act]->blendtype= MTEX_MUL;
1271                 }
1272                 
1273                 part->mtex[act]->tex= newtex;
1274                 id_us_plus(&newtex->id);
1275         }
1276         else if(part->mtex[act]) {
1277                 MEM_freeN(part->mtex[act]);
1278                 part->mtex[act]= NULL;
1279         }
1280 }
1281
1282 /* ------------------------------------------------------------------------- */
1283
1284 EnvMap *BKE_add_envmap(void)
1285 {
1286         EnvMap *env;
1287         
1288         env= MEM_callocN(sizeof(EnvMap), "envmap");
1289         env->type= ENV_CUBE;
1290         env->stype= ENV_ANIM;
1291         env->clipsta= 0.1;
1292         env->clipend= 100.0;
1293         env->cuberes= 600;
1294         env->viewscale = 0.5;
1295         
1296         return env;
1297
1298
1299 /* ------------------------------------------------------------------------- */
1300
1301 EnvMap *BKE_copy_envmap(EnvMap *env)
1302 {
1303         EnvMap *envn;
1304         int a;
1305         
1306         envn= MEM_dupallocN(env);
1307         envn->ok= 0;
1308         for(a=0; a<6; a++) envn->cube[a]= NULL;
1309         if(envn->ima) id_us_plus((ID *)envn->ima);
1310         
1311         return envn;
1312 }
1313
1314 /* ------------------------------------------------------------------------- */
1315
1316 void BKE_free_envmapdata(EnvMap *env)
1317 {
1318         unsigned int part;
1319         
1320         for(part=0; part<6; part++) {
1321                 if(env->cube[part])
1322                         IMB_freeImBuf(env->cube[part]);
1323                 env->cube[part]= NULL;
1324         }
1325         env->ok= 0;
1326 }
1327
1328 /* ------------------------------------------------------------------------- */
1329
1330 void BKE_free_envmap(EnvMap *env)
1331 {
1332         
1333         BKE_free_envmapdata(env);
1334         MEM_freeN(env);
1335         
1336 }
1337
1338 /* ------------------------------------------------------------------------- */
1339
1340 PointDensity *BKE_add_pointdensity(void)
1341 {
1342         PointDensity *pd;
1343         
1344         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1345         pd->flag = 0;
1346         pd->radius = 0.3f;
1347         pd->falloff_type = TEX_PD_FALLOFF_STD;
1348         pd->falloff_softness = 2.0;
1349         pd->source = TEX_PD_PSYS;
1350         pd->point_tree = NULL;
1351         pd->point_data = NULL;
1352         pd->noise_size = 0.5f;
1353         pd->noise_depth = 1;
1354         pd->noise_fac = 1.0f;
1355         pd->noise_influence = TEX_PD_NOISE_STATIC;
1356         pd->coba = add_colorband(1);
1357         pd->speed_scale = 1.0f;
1358         pd->totpoints = 0;
1359         pd->object = NULL;
1360         pd->psys = 0;
1361         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1362         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1363
1364         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1365         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1366         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1367         curvemapping_changed(pd->falloff_curve, 0);
1368
1369         return pd;
1370
1371
1372 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1373 {
1374         PointDensity *pdn;
1375
1376         pdn= MEM_dupallocN(pd);
1377         pdn->point_tree = NULL;
1378         pdn->point_data = NULL;
1379         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1380         pdn->falloff_curve = curvemapping_copy(pdn->falloff_curve); /* can be NULL */
1381         return pdn;
1382 }
1383
1384 void BKE_free_pointdensitydata(PointDensity *pd)
1385 {
1386         if (pd->point_tree) {
1387                 BLI_bvhtree_free(pd->point_tree);
1388                 pd->point_tree = NULL;
1389         }
1390         if (pd->point_data) {
1391                 MEM_freeN(pd->point_data);
1392                 pd->point_data = NULL;
1393         }
1394         if(pd->coba) {
1395                 MEM_freeN(pd->coba);
1396                 pd->coba = NULL;
1397         }
1398
1399         curvemapping_free(pd->falloff_curve); /* can be NULL */
1400 }
1401
1402 void BKE_free_pointdensity(PointDensity *pd)
1403 {
1404         BKE_free_pointdensitydata(pd);
1405         MEM_freeN(pd);
1406 }
1407
1408
1409 void BKE_free_voxeldatadata(struct VoxelData *vd)
1410 {
1411         if (vd->dataset) {
1412                 MEM_freeN(vd->dataset);
1413                 vd->dataset = NULL;
1414         }
1415
1416 }
1417  
1418 void BKE_free_voxeldata(struct VoxelData *vd)
1419 {
1420         BKE_free_voxeldatadata(vd);
1421         MEM_freeN(vd);
1422 }
1423  
1424 struct VoxelData *BKE_add_voxeldata(void)
1425 {
1426         VoxelData *vd;
1427
1428         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1429         vd->dataset = NULL;
1430         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1431         vd->interp_type= TEX_VD_LINEAR;
1432         vd->file_format= TEX_VD_SMOKE;
1433         vd->int_multiplier = 1.0;
1434         vd->extend = TEX_CLIP;
1435         vd->object = NULL;
1436         vd->cachedframe = -1;
1437         vd->ok = 0;
1438         
1439         return vd;
1440  }
1441  
1442 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1443 {
1444         VoxelData *vdn;
1445
1446         vdn= MEM_dupallocN(vd); 
1447         vdn->dataset = NULL;
1448
1449         return vdn;
1450 }
1451
1452
1453 /* ------------------------------------------------------------------------- */
1454 int BKE_texture_dependsOnTime(const struct Tex *texture)
1455 {
1456         if(texture->plugin) {
1457                 // assume all plugins depend on time
1458                 return 1;
1459         } 
1460         else if(        texture->ima && 
1461                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1462                 return 1;
1463         }
1464         else if(texture->adt) {
1465                 // assume anything in adt means the texture is animated
1466                 return 1;
1467         }
1468         return 0;
1469 }
1470
1471 /* ------------------------------------------------------------------------- */