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