=trunk=
[blender.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= MEM_dupallocN(texn->pd);
766         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
767         
768         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
769
770         if(tex->nodetree) {
771                 ntreeEndExecTree(tex->nodetree);
772                 texn->nodetree= ntreeCopyTree(tex->nodetree); 
773         }
774         
775         return texn;
776 }
777
778 /* texture copy without adding to main dbase */
779 Tex *localize_texture(Tex *tex)
780 {
781         Tex *texn;
782         
783         texn= copy_libblock(tex);
784         BLI_remlink(&G.main->tex, texn);
785         
786         /* image texture: free_texture also doesn't decrease */
787         
788         if(texn->plugin) {
789                 texn->plugin= MEM_dupallocN(texn->plugin);
790                 open_plugin_tex(texn->plugin);
791         }
792         
793         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
794         if(texn->env) {
795                 texn->env= BKE_copy_envmap(texn->env);
796                 id_us_min(&texn->env->ima->id);
797         }
798         if(texn->pd) {
799                 texn->pd= MEM_dupallocN(texn->pd);
800                 if(texn->pd->coba) {
801                         texn->pd->point_tree = NULL;
802                         texn->pd->coba= MEM_dupallocN(texn->pd->coba);
803                 }
804
805         }
806         if(texn->vd) {
807                 texn->vd= MEM_dupallocN(texn->vd);
808                 if(texn->vd->dataset)
809                         texn->vd->dataset= MEM_dupallocN(texn->vd->dataset);
810         }
811         
812         texn->preview = NULL;
813         
814         if(tex->nodetree) {
815                 texn->nodetree= ntreeLocalize(tex->nodetree);
816         }
817         
818         return texn;
819 }
820
821
822 /* ------------------------------------------------------------------------- */
823
824 void make_local_texture(Tex *tex)
825 {
826         Main *bmain= G.main;
827         Tex *texn;
828         Material *ma;
829         World *wrld;
830         Lamp *la;
831         Brush *br;
832         ParticleSettings *pa;
833         int a, local=0, lib=0;
834
835         /* - only lib users: do nothing
836                 * - only local users: set flag
837                 * - mixed: make copy
838                 */
839         
840         if(tex->id.lib==NULL) return;
841
842         /* special case: ima always local immediately */
843         if(tex->ima) {
844                 tex->ima->id.lib= NULL;
845                 tex->ima->id.flag= LIB_LOCAL;
846                 new_id(&bmain->image, (ID *)tex->ima, NULL);
847         }
848
849         if(tex->id.us==1) {
850                 tex->id.lib= NULL;
851                 tex->id.flag= LIB_LOCAL;
852                 new_id(&bmain->tex, (ID *)tex, NULL);
853
854                 return;
855         }
856         
857         ma= bmain->mat.first;
858         while(ma) {
859                 for(a=0; a<MAX_MTEX; a++) {
860                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
861                                 if(ma->id.lib) lib= 1;
862                                 else local= 1;
863                         }
864                 }
865                 ma= ma->id.next;
866         }
867         la= bmain->lamp.first;
868         while(la) {
869                 for(a=0; a<MAX_MTEX; a++) {
870                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
871                                 if(la->id.lib) lib= 1;
872                                 else local= 1;
873                         }
874                 }
875                 la= la->id.next;
876         }
877         wrld= bmain->world.first;
878         while(wrld) {
879                 for(a=0; a<MAX_MTEX; a++) {
880                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
881                                 if(wrld->id.lib) lib= 1;
882                                 else local= 1;
883                         }
884                 }
885                 wrld= wrld->id.next;
886         }
887         br= bmain->brush.first;
888         while(br) {
889                 if(br->mtex.tex==tex) {
890                         if(br->id.lib) lib= 1;
891                         else local= 1;
892                 }
893                 br= br->id.next;
894         }
895         pa= bmain->particle.first;
896         while(pa) {
897                 for(a=0; a<MAX_MTEX; a++) {
898                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
899                                 if(pa->id.lib) lib= 1;
900                                 else local= 1;
901                         }
902                 }
903                 pa= pa->id.next;
904         }
905         
906         if(local && lib==0) {
907                 tex->id.lib= NULL;
908                 tex->id.flag= LIB_LOCAL;
909                 new_id(&bmain->tex, (ID *)tex, NULL);
910         }
911         else if(local && lib) {
912                 texn= copy_texture(tex);
913                 texn->id.us= 0;
914                 
915                 ma= bmain->mat.first;
916                 while(ma) {
917                         for(a=0; a<MAX_MTEX; a++) {
918                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
919                                         if(ma->id.lib==NULL) {
920                                                 ma->mtex[a]->tex= texn;
921                                                 texn->id.us++;
922                                                 tex->id.us--;
923                                         }
924                                 }
925                         }
926                         ma= ma->id.next;
927                 }
928                 la= bmain->lamp.first;
929                 while(la) {
930                         for(a=0; a<MAX_MTEX; a++) {
931                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
932                                         if(la->id.lib==NULL) {
933                                                 la->mtex[a]->tex= texn;
934                                                 texn->id.us++;
935                                                 tex->id.us--;
936                                         }
937                                 }
938                         }
939                         la= la->id.next;
940                 }
941                 wrld= bmain->world.first;
942                 while(wrld) {
943                         for(a=0; a<MAX_MTEX; a++) {
944                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
945                                         if(wrld->id.lib==NULL) {
946                                                 wrld->mtex[a]->tex= texn;
947                                                 texn->id.us++;
948                                                 tex->id.us--;
949                                         }
950                                 }
951                         }
952                         wrld= wrld->id.next;
953                 }
954                 br= bmain->brush.first;
955                 while(br) {
956                         if(br->mtex.tex==tex) {
957                                 if(br->id.lib==NULL) {
958                                         br->mtex.tex= texn;
959                                         texn->id.us++;
960                                         tex->id.us--;
961                                 }
962                         }
963                         br= br->id.next;
964                 }
965                 pa= bmain->particle.first;
966                 while(pa) {
967                         for(a=0; a<MAX_MTEX; a++) {
968                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
969                                         if(pa->id.lib==NULL) {
970                                                 pa->mtex[a]->tex= texn;
971                                                 texn->id.us++;
972                                                 tex->id.us--;
973                                         }
974                                 }
975                         }
976                         pa= pa->id.next;
977                 }
978         }
979 }
980
981 /* ------------------------------------------------------------------------- */
982
983 void autotexname(Tex *tex)
984 {
985         Main *bmain= G.main;
986         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
987                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
988                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
989         Image *ima;
990         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
991         
992         if(tex) {
993                 if(tex->use_nodes) {
994                         new_id(&bmain->tex, (ID *)tex, "Noddy");
995                 }
996                 else
997                 if(tex->type==TEX_IMAGE) {
998                         ima= tex->ima;
999                         if(ima) {
1000                                 strcpy(di, ima->name);
1001                                 BLI_splitdirstring(di, fi);
1002                                 strcpy(di, "I.");
1003                                 strcat(di, fi);
1004                                 new_id(&bmain->tex, (ID *)tex, di);
1005                         }
1006                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1007                 }
1008                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
1009                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
1010         }
1011 }
1012
1013 /* ------------------------------------------------------------------------- */
1014
1015 Tex *give_current_object_texture(Object *ob)
1016 {
1017         Material *ma;
1018         Tex *tex= NULL;
1019         
1020         if(ob==NULL) return NULL;
1021         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
1022         
1023         if(ob->type==OB_LAMP) {
1024                 tex= give_current_lamp_texture(ob->data);
1025         } else {
1026                 ma= give_current_material(ob, ob->actcol);
1027                 tex= give_current_material_texture(ma);
1028         }
1029         
1030         return tex;
1031 }
1032
1033 Tex *give_current_lamp_texture(Lamp *la)
1034 {
1035         MTex *mtex= NULL;
1036         Tex *tex= NULL;
1037
1038         if(la) {
1039                 mtex= la->mtex[(int)(la->texact)];
1040                 if(mtex) tex= mtex->tex;
1041         }
1042
1043         return tex;
1044 }
1045
1046 void set_current_lamp_texture(Lamp *la, Tex *newtex)
1047 {
1048         int act= la->texact;
1049
1050         if(la->mtex[act] && la->mtex[act]->tex)
1051                 id_us_min(&la->mtex[act]->tex->id);
1052
1053         if(newtex) {
1054                 if(!la->mtex[act]) {
1055                         la->mtex[act]= add_mtex();
1056                         la->mtex[act]->texco= TEXCO_GLOB;
1057                 }
1058                 
1059                 la->mtex[act]->tex= newtex;
1060                 id_us_plus(&newtex->id);
1061         }
1062         else if(la->mtex[act]) {
1063                 MEM_freeN(la->mtex[act]);
1064                 la->mtex[act]= NULL;
1065         }
1066 }
1067
1068 bNode *give_current_material_texture_node(Material *ma)
1069 {
1070         if(ma && ma->use_nodes && ma->nodetree)
1071                 return nodeGetActiveID(ma->nodetree, ID_TE);
1072         
1073         return NULL;
1074 }
1075
1076 Tex *give_current_material_texture(Material *ma)
1077 {
1078         MTex *mtex= NULL;
1079         Tex *tex= NULL;
1080         bNode *node;
1081         
1082         if(ma && ma->use_nodes && ma->nodetree) {
1083                 /* first check texture, then material, this works together
1084                    with a hack that clears the active ID flag for textures on
1085                    making a material node active */
1086                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1087
1088                 if(node) {
1089                         tex= (Tex *)node->id;
1090                         ma= NULL;
1091                 }
1092                 else {
1093                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1094                         if(node) {
1095                                 ma= (Material*)node->id;
1096                                 if(ma) {
1097                                         mtex= ma->mtex[(int)(ma->texact)];
1098                                         if(mtex) tex= mtex->tex;
1099                                 }
1100                         }
1101                 }
1102                 return tex;
1103         }
1104
1105         if(ma) {
1106                 mtex= ma->mtex[(int)(ma->texact)];
1107                 if(mtex) tex= mtex->tex;
1108         }
1109         
1110         return tex;
1111 }
1112
1113 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1114 {
1115         switch(GS(id->name)) {
1116         case ID_MA:
1117                 *mtex_ar=               ((Material *)id)->mtex;
1118                 if(act) *act=   (((Material *)id)->texact);
1119                 break;
1120         case ID_WO:
1121                 *mtex_ar=               ((World *)id)->mtex;
1122                 if(act) *act=   (((World *)id)->texact);
1123                 break;
1124         case ID_LA:
1125                 *mtex_ar=               ((Lamp *)id)->mtex;
1126                 if(act) *act=   (((Lamp *)id)->texact);
1127                 break;
1128         case ID_PA:
1129                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1130                 if(act) *act=   (((ParticleSettings *)id)->texact);
1131                 break;
1132         default:
1133                 *mtex_ar = NULL;
1134                 if(act) *act=   0;
1135                 return FALSE;
1136         }
1137
1138         return TRUE;
1139 }
1140
1141 void set_active_mtex(ID *id, short act)
1142 {
1143         if(act<0)                               act= 0;
1144         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1145
1146         switch(GS(id->name)) {
1147         case ID_MA:
1148                 ((Material *)id)->texact= act;
1149                 break;
1150         case ID_WO:
1151                 ((World *)id)->texact= act;
1152                 break;
1153         case ID_LA:
1154                 ((Lamp *)id)->texact= act;
1155                 break;
1156         case ID_PA:
1157                 ((ParticleSettings *)id)->texact= act;
1158                 break;
1159         }
1160 }
1161
1162 void set_current_material_texture(Material *ma, Tex *newtex)
1163 {
1164         Tex *tex= NULL;
1165         bNode *node;
1166         
1167         if(ma && ma->use_nodes && ma->nodetree) {
1168                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1169
1170                 if(node) {
1171                         tex= (Tex *)node->id;
1172                         id_us_min(&tex->id);
1173                         node->id= &newtex->id;
1174                         id_us_plus(&newtex->id);
1175                         ma= NULL;
1176                 }
1177                 else {
1178                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1179                         if(node)
1180                                 ma= (Material*)node->id;
1181                 }
1182         }
1183         if(ma) {
1184                 int act= (int)ma->texact;
1185
1186                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1187                 id_us_min(&tex->id);
1188
1189                 if(newtex) {
1190                         if(!ma->mtex[act])
1191                                 ma->mtex[act]= add_mtex();
1192                         
1193                         ma->mtex[act]->tex= newtex;
1194                         id_us_plus(&newtex->id);
1195                 }
1196                 else if(ma->mtex[act]) {
1197                         MEM_freeN(ma->mtex[act]);
1198                         ma->mtex[act]= NULL;
1199                 }
1200         }
1201 }
1202
1203 Tex *give_current_world_texture(World *world)
1204 {
1205         MTex *mtex= NULL;
1206         Tex *tex= NULL;
1207         
1208         if(!world) return NULL;
1209         
1210         mtex= world->mtex[(int)(world->texact)];
1211         if(mtex) tex= mtex->tex;
1212         
1213         return tex;
1214 }
1215
1216 void set_current_world_texture(World *wo, Tex *newtex)
1217 {
1218         int act= wo->texact;
1219
1220         if(wo->mtex[act] && wo->mtex[act]->tex)
1221                 id_us_min(&wo->mtex[act]->tex->id);
1222
1223         if(newtex) {
1224                 if(!wo->mtex[act]) {
1225                         wo->mtex[act]= add_mtex();
1226                         wo->mtex[act]->texco= TEXCO_VIEW;
1227                 }
1228                 
1229                 wo->mtex[act]->tex= newtex;
1230                 id_us_plus(&newtex->id);
1231         }
1232         else if(wo->mtex[act]) {
1233                 MEM_freeN(wo->mtex[act]);
1234                 wo->mtex[act]= NULL;
1235         }
1236 }
1237
1238 Tex *give_current_brush_texture(Brush *br)
1239 {
1240         return br->mtex.tex;
1241 }
1242
1243 void set_current_brush_texture(Brush *br, Tex *newtex)
1244 {
1245         if(br->mtex.tex)
1246                 id_us_min(&br->mtex.tex->id);
1247
1248         if(newtex) {
1249                 br->mtex.tex= newtex;
1250                 id_us_plus(&newtex->id);
1251         }
1252 }
1253
1254 Tex *give_current_particle_texture(ParticleSettings *part)
1255 {
1256         MTex *mtex= NULL;
1257         Tex *tex= NULL;
1258         
1259         if(!part) return NULL;
1260         
1261         mtex= part->mtex[(int)(part->texact)];
1262         if(mtex) tex= mtex->tex;
1263         
1264         return tex;
1265 }
1266
1267 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1268 {
1269         int act= part->texact;
1270
1271         if(part->mtex[act] && part->mtex[act]->tex)
1272                 id_us_min(&part->mtex[act]->tex->id);
1273
1274         if(newtex) {
1275                 if(!part->mtex[act]) {
1276                         part->mtex[act]= add_mtex();
1277                         part->mtex[act]->texco= TEXCO_ORCO;
1278                         part->mtex[act]->blendtype= MTEX_MUL;
1279                 }
1280                 
1281                 part->mtex[act]->tex= newtex;
1282                 id_us_plus(&newtex->id);
1283         }
1284         else if(part->mtex[act]) {
1285                 MEM_freeN(part->mtex[act]);
1286                 part->mtex[act]= NULL;
1287         }
1288 }
1289
1290 /* ------------------------------------------------------------------------- */
1291
1292 EnvMap *BKE_add_envmap(void)
1293 {
1294         EnvMap *env;
1295         
1296         env= MEM_callocN(sizeof(EnvMap), "envmap");
1297         env->type= ENV_CUBE;
1298         env->stype= ENV_ANIM;
1299         env->clipsta= 0.1;
1300         env->clipend= 100.0;
1301         env->cuberes= 600;
1302         env->viewscale = 0.5;
1303         
1304         return env;
1305
1306
1307 /* ------------------------------------------------------------------------- */
1308
1309 EnvMap *BKE_copy_envmap(EnvMap *env)
1310 {
1311         EnvMap *envn;
1312         int a;
1313         
1314         envn= MEM_dupallocN(env);
1315         envn->ok= 0;
1316         for(a=0; a<6; a++) envn->cube[a]= NULL;
1317         if(envn->ima) id_us_plus((ID *)envn->ima);
1318         
1319         return envn;
1320 }
1321
1322 /* ------------------------------------------------------------------------- */
1323
1324 void BKE_free_envmapdata(EnvMap *env)
1325 {
1326         unsigned int part;
1327         
1328         for(part=0; part<6; part++) {
1329                 if(env->cube[part])
1330                         IMB_freeImBuf(env->cube[part]);
1331                 env->cube[part]= NULL;
1332         }
1333         env->ok= 0;
1334 }
1335
1336 /* ------------------------------------------------------------------------- */
1337
1338 void BKE_free_envmap(EnvMap *env)
1339 {
1340         
1341         BKE_free_envmapdata(env);
1342         MEM_freeN(env);
1343         
1344 }
1345
1346 /* ------------------------------------------------------------------------- */
1347
1348 PointDensity *BKE_add_pointdensity(void)
1349 {
1350         PointDensity *pd;
1351         
1352         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1353         pd->flag = 0;
1354         pd->radius = 0.3f;
1355         pd->falloff_type = TEX_PD_FALLOFF_STD;
1356         pd->falloff_softness = 2.0;
1357         pd->source = TEX_PD_PSYS;
1358         pd->point_tree = NULL;
1359         pd->point_data = NULL;
1360         pd->noise_size = 0.5f;
1361         pd->noise_depth = 1;
1362         pd->noise_fac = 1.0f;
1363         pd->noise_influence = TEX_PD_NOISE_STATIC;
1364         pd->coba = add_colorband(1);
1365         pd->speed_scale = 1.0f;
1366         pd->totpoints = 0;
1367         pd->object = NULL;
1368         pd->psys = 0;
1369         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1370         pd->falloff_curve = curvemapping_add(1, 0, 0, 1, 1);
1371
1372         pd->falloff_curve->preset = CURVE_PRESET_LINE;
1373         pd->falloff_curve->cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
1374         curvemap_reset(pd->falloff_curve->cm, &pd->falloff_curve->clipr, pd->falloff_curve->preset, CURVEMAP_SLOPE_POSITIVE);
1375         curvemapping_changed(pd->falloff_curve, 0);
1376
1377         return pd;
1378
1379
1380 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1381 {
1382         PointDensity *pdn;
1383
1384         pdn= MEM_dupallocN(pd);
1385         pdn->point_tree = NULL;
1386         pdn->point_data = NULL;
1387         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1388         
1389         return pdn;
1390 }
1391
1392 void BKE_free_pointdensitydata(PointDensity *pd)
1393 {
1394         if (pd->point_tree) {
1395                 BLI_bvhtree_free(pd->point_tree);
1396                 pd->point_tree = NULL;
1397         }
1398         if (pd->point_data) {
1399                 MEM_freeN(pd->point_data);
1400                 pd->point_data = NULL;
1401         }
1402         if(pd->coba) {
1403                 MEM_freeN(pd->coba);
1404                 pd->coba = NULL;
1405         }
1406 }
1407
1408 void BKE_free_pointdensity(PointDensity *pd)
1409 {
1410         BKE_free_pointdensitydata(pd);
1411         MEM_freeN(pd);
1412 }
1413
1414
1415 void BKE_free_voxeldatadata(struct VoxelData *vd)
1416 {
1417         if (vd->dataset) {
1418                 MEM_freeN(vd->dataset);
1419                 vd->dataset = NULL;
1420         }
1421
1422 }
1423  
1424 void BKE_free_voxeldata(struct VoxelData *vd)
1425 {
1426         BKE_free_voxeldatadata(vd);
1427         MEM_freeN(vd);
1428 }
1429  
1430 struct VoxelData *BKE_add_voxeldata(void)
1431 {
1432         VoxelData *vd;
1433
1434         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1435         vd->dataset = NULL;
1436         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1437         vd->interp_type= TEX_VD_LINEAR;
1438         vd->file_format= TEX_VD_SMOKE;
1439         vd->int_multiplier = 1.0;
1440         vd->extend = TEX_CLIP;
1441         vd->object = NULL;
1442         vd->cachedframe = -1;
1443         vd->ok = 0;
1444         
1445         return vd;
1446  }
1447  
1448 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1449 {
1450         VoxelData *vdn;
1451
1452         vdn= MEM_dupallocN(vd); 
1453         vdn->dataset = NULL;
1454
1455         return vdn;
1456 }
1457
1458
1459 /* ------------------------------------------------------------------------- */
1460 int BKE_texture_dependsOnTime(const struct Tex *texture)
1461 {
1462         if(texture->plugin) {
1463                 // assume all plugins depend on time
1464                 return 1;
1465         } 
1466         else if(        texture->ima && 
1467                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1468                 return 1;
1469         }
1470         else if(texture->adt) {
1471                 // assume anything in adt means the texture is animated
1472                 return 1;
1473         }
1474         return 0;
1475 }
1476
1477 /* ------------------------------------------------------------------------- */