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