merge with/from trunk at r35190
[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 #include "DNA_particle_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= NULL;
97         pit->pname= NULL;
98         pit->stnames= NULL;
99         pit->varstr= NULL;
100         pit->result= NULL;
101         pit->cfra= NULL;
102         pit->version= 0;
103         pit->instance_init= NULL;
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 != NULL) {
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 != NULL) {
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==NULL) {
172                 if(pit->handle==NULL) {;} //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==NULL) 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 int vergcband(const void *a1, const void *a2)
407 {
408         const CBData *x1=a1, *x2=a2;
409
410         if( x1->pos > x2->pos ) return 1;
411         else if( x1->pos < x2->pos) return -1;
412         return 0;
413 }
414
415 CBData *colorband_element_add(struct ColorBand *coba, float position)
416 {
417         int a;
418
419         if(coba->tot==MAXCOLORBAND) {
420                 return NULL;
421         }
422         else if(coba->tot > 0) {
423                 CBData *xnew;
424                 float col[4];
425
426                 do_colorband(coba, position, col);
427
428                 xnew = &coba->data[coba->tot];
429                 xnew->pos = position;
430
431                 xnew->r = col[0];
432                 xnew->g = col[1];
433                 xnew->b = col[2];
434                 xnew->a = col[3];
435         }
436
437         coba->tot++;
438         coba->cur = coba->tot-1;
439
440         for(a = 0; a < coba->tot; a++)
441                 coba->data[a].cur = a;
442
443         qsort(coba->data, coba->tot, sizeof(CBData), vergcband);
444
445         for(a = 0; a < coba->tot; a++) {
446                 if(coba->data[a].cur == coba->cur) {
447                         coba->cur = a;
448                         break;
449                 }
450         }
451
452         return coba->data + coba->cur;
453 }
454
455 int colorband_element_remove(struct ColorBand *coba, int index)
456 {
457         int a;
458
459         if(coba->tot < 2)
460                 return 0;
461
462         if(index < 0 || index >= coba->tot)
463                 return 0;
464
465         for(a = index; a < coba->tot; a++) {
466                 coba->data[a] = coba->data[a + 1];
467         }
468         if(coba->cur) coba->cur--;
469         coba->tot--;
470         return 1;
471 }
472
473 /* ******************* TEX ************************ */
474
475 void free_texture(Tex *tex)
476 {
477         free_plugin_tex(tex->plugin);
478         if(tex->coba) MEM_freeN(tex->coba);
479         if(tex->env) BKE_free_envmap(tex->env);
480         if(tex->pd) BKE_free_pointdensity(tex->pd);
481         if(tex->vd) BKE_free_voxeldata(tex->vd);
482         BKE_free_animdata((struct ID *)tex);
483         BKE_previewimg_free(&tex->preview);
484         BKE_icon_delete((struct ID*)tex);
485         tex->id.icon_id = 0;
486         
487         if(tex->nodetree) {
488                 ntreeFreeTree(tex->nodetree);
489                 MEM_freeN(tex->nodetree);
490         }
491 }
492
493 /* ------------------------------------------------------------------------- */
494
495 void default_tex(Tex *tex)
496 {
497         PluginTex *pit;
498         VarStruct *varstr;
499         int a;
500
501         tex->type= TEX_CLOUDS;
502         tex->stype= 0;
503         tex->flag= TEX_CHECKER_ODD;
504         tex->imaflag= TEX_INTERPOL|TEX_MIPMAP|TEX_USEALPHA;
505         tex->extend= TEX_REPEAT;
506         tex->cropxmin= tex->cropymin= 0.0;
507         tex->cropxmax= tex->cropymax= 1.0;
508         tex->texfilter = TXF_EWA;
509         tex->afmax = 8;
510         tex->xrepeat= tex->yrepeat= 1;
511         tex->fie_ima= 2;
512         tex->sfra= 1;
513         tex->frames= 0;
514         tex->offset= 0;
515         tex->noisesize= 0.25;
516         tex->noisedepth= 2;
517         tex->turbul= 5.0;
518         tex->nabla= 0.025;      // also in do_versions
519         tex->bright= 1.0;
520         tex->contrast= 1.0;
521         tex->saturation= 1.0;
522         tex->filtersize= 1.0;
523         tex->rfac= 1.0;
524         tex->gfac= 1.0;
525         tex->bfac= 1.0;
526         /* newnoise: init. */
527         tex->noisebasis = 0;
528         tex->noisebasis2 = 0;
529         /* musgrave */
530         tex->mg_H = 1.0;
531         tex->mg_lacunarity = 2.0;
532         tex->mg_octaves = 2.0;
533         tex->mg_offset = 1.0;
534         tex->mg_gain = 1.0;
535         tex->ns_outscale = 1.0;
536         /* distnoise */
537         tex->dist_amount = 1.0;
538         /* voronoi */
539         tex->vn_w1 = 1.0;
540         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
541         tex->vn_mexp = 2.5;
542         tex->vn_distm = 0;
543         tex->vn_coltype = 0;
544
545         if (tex->env) {
546                 tex->env->stype=ENV_ANIM;
547                 tex->env->clipsta=0.1;
548                 tex->env->clipend=100;
549                 tex->env->cuberes=600;
550                 tex->env->depth=0;
551         }
552
553         if (tex->pd) {
554                 tex->pd->radius = 0.3f;
555                 tex->pd->falloff_type = TEX_PD_FALLOFF_STD;
556         }
557         
558         if (tex->vd) {
559                 tex->vd->resol[0] = tex->vd->resol[1] = tex->vd->resol[2] = 0;
560                 tex->vd->interp_type=TEX_VD_LINEAR;
561                 tex->vd->file_format=TEX_VD_SMOKE;
562         }
563         pit = tex->plugin;
564         if (pit) {
565                 varstr= pit->varstr;
566                 if(varstr) {
567                         for(a=0; a<pit->vars; a++, varstr++) {
568                                 pit->data[a] = varstr->def;
569                         }
570                 }
571         }
572         
573         tex->iuser.fie_ima= 2;
574         tex->iuser.ok= 1;
575         tex->iuser.frames= 100;
576         tex->iuser.sfra= 1;
577         
578         tex->preview = NULL;
579 }
580
581 void tex_set_type(Tex *tex, int type)
582 {
583         switch(type) {
584                         
585                 case TEX_VOXELDATA:
586                         if (tex->vd == NULL)
587                                 tex->vd = BKE_add_voxeldata();
588                         break;
589                 case TEX_POINTDENSITY:
590                         if (tex->pd == NULL)
591                                 tex->pd = BKE_add_pointdensity();
592                         break;
593                 case TEX_ENVMAP:
594                         if (tex->env == NULL)
595                                 tex->env = BKE_add_envmap();
596                         break;
597         }
598         
599         tex->type = type;
600 }
601
602 /* ------------------------------------------------------------------------- */
603
604 Tex *add_texture(const char *name)
605 {
606         Main *bmain= G.main;
607         Tex *tex;
608
609         tex= alloc_libblock(&bmain->tex, ID_TE, name);
610         
611         default_tex(tex);
612         
613         return tex;
614 }
615
616 /* ------------------------------------------------------------------------- */
617
618 void default_mtex(MTex *mtex)
619 {
620         mtex->texco= TEXCO_ORCO;
621         mtex->mapto= MAP_COL;
622         mtex->object= NULL;
623         mtex->projx= PROJ_X;
624         mtex->projy= PROJ_Y;
625         mtex->projz= PROJ_Z;
626         mtex->mapping= MTEX_FLAT;
627         mtex->ofs[0]= 0.0;
628         mtex->ofs[1]= 0.0;
629         mtex->ofs[2]= 0.0;
630         mtex->size[0]= 1.0;
631         mtex->size[1]= 1.0;
632         mtex->size[2]= 1.0;
633         mtex->tex= NULL;
634         mtex->texflag= MTEX_3TAP_BUMP | MTEX_BUMP_OBJECTSPACE;
635         mtex->colormodel= 0;
636         mtex->r= 1.0;
637         mtex->g= 0.0;
638         mtex->b= 1.0;
639         mtex->k= 1.0;
640         mtex->def_var= 1.0;
641         mtex->blendtype= MTEX_BLEND;
642         mtex->colfac= 1.0;
643         mtex->norfac= 1.0;
644         mtex->varfac= 1.0;
645         mtex->dispfac=0.2;
646         mtex->colspecfac= 1.0f;
647         mtex->mirrfac= 1.0f;
648         mtex->alphafac= 1.0f;
649         mtex->difffac= 1.0f;
650         mtex->specfac= 1.0f;
651         mtex->emitfac= 1.0f;
652         mtex->hardfac= 1.0f;
653         mtex->raymirrfac= 1.0f;
654         mtex->translfac= 1.0f;
655         mtex->ambfac= 1.0f;
656         mtex->colemitfac= 1.0f;
657         mtex->colreflfac= 1.0f;
658         mtex->coltransfac= 1.0f;
659         mtex->densfac= 1.0f;
660         mtex->scatterfac= 1.0f;
661         mtex->reflfac= 1.0f;
662         mtex->shadowfac= 1.0f;
663         mtex->zenupfac= 1.0f;
664         mtex->zendownfac= 1.0f;
665         mtex->blendfac= 1.0f;
666         mtex->timefac= 1.0f;
667         mtex->lengthfac= 1.0f;
668         mtex->clumpfac= 1.0f;
669         mtex->kinkfac= 1.0f;
670         mtex->roughfac= 1.0f;
671         mtex->padensfac= 1.0f;
672         mtex->lifefac= 1.0f;
673         mtex->sizefac= 1.0f;
674         mtex->ivelfac= 1.0f;
675         mtex->dampfac= 1.0f;
676         mtex->gravityfac= 1.0f;
677         mtex->fieldfac= 1.0f;
678         mtex->normapspace= MTEX_NSPACE_TANGENT;
679 }
680
681
682 /* ------------------------------------------------------------------------- */
683
684 MTex *add_mtex(void)
685 {
686         MTex *mtex;
687         
688         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
689         
690         default_mtex(mtex);
691         
692         return mtex;
693 }
694
695 /* slot -1 for first free ID */
696 MTex *add_mtex_id(ID *id, int slot)
697 {
698         MTex **mtex_ar;
699         short act;
700
701         give_active_mtex(id, &mtex_ar, &act);
702
703         if(mtex_ar==NULL) {
704                 return NULL;
705         }
706         
707         if(slot==-1) {
708                 /* find first free */
709                 int i;          
710                 for (i= 0; i < MAX_MTEX; i++) {
711                         if (!mtex_ar[i]) {
712                                 slot= i;
713                                 break;
714                         }
715                 }
716                 if(slot == -1) {
717                         return NULL;
718                 }
719         }
720         else {
721                 /* make sure slot is valid */
722                 if(slot < 0 || slot >= MAX_MTEX) {
723                         return NULL;
724                 }
725         }
726
727         if (mtex_ar[slot]) {
728                 id_us_min((ID *)mtex_ar[slot]->tex);
729                 MEM_freeN(mtex_ar[slot]);
730                 mtex_ar[slot]= NULL;
731         }
732
733         mtex_ar[slot]= add_mtex();
734
735         return mtex_ar[slot];
736 }
737
738 /* ------------------------------------------------------------------------- */
739
740 Tex *copy_texture(Tex *tex)
741 {
742         Tex *texn;
743         
744         texn= copy_libblock(tex);
745         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
746         else texn->ima= NULL;
747         
748 #if 0 // XXX old animation system
749         id_us_plus((ID *)texn->ipo);
750 #endif // XXX old animation system
751         
752         if(texn->plugin) {
753                 texn->plugin= MEM_dupallocN(texn->plugin);
754                 open_plugin_tex(texn->plugin);
755         }
756         
757         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
758         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
759         if(texn->pd) texn->pd= MEM_dupallocN(texn->pd);
760         if(texn->vd) texn->vd= MEM_dupallocN(texn->vd);
761         
762         if(tex->preview) texn->preview = BKE_previewimg_copy(tex->preview);
763
764         if(tex->nodetree) {
765                 ntreeEndExecTree(tex->nodetree);
766                 texn->nodetree= ntreeCopyTree(tex->nodetree); /* 0 == full new tree */
767         }
768         
769         return texn;
770 }
771
772 /* ------------------------------------------------------------------------- */
773
774 void make_local_texture(Tex *tex)
775 {
776         Main *bmain= G.main;
777         Tex *texn;
778         Material *ma;
779         World *wrld;
780         Lamp *la;
781         Brush *br;
782         ParticleSettings *pa;
783         int a, local=0, lib=0;
784
785         /* - only lib users: do nothing
786                 * - only local users: set flag
787                 * - mixed: make copy
788                 */
789         
790         if(tex->id.lib==NULL) return;
791
792         /* special case: ima always local immediately */
793         if(tex->ima) {
794                 tex->ima->id.lib= NULL;
795                 tex->ima->id.flag= LIB_LOCAL;
796                 new_id(NULL, (ID *)tex->ima, NULL);
797         }
798
799         if(tex->id.us==1) {
800                 tex->id.lib= NULL;
801                 tex->id.flag= LIB_LOCAL;
802                 new_id(NULL, (ID *)tex, NULL);
803
804                 return;
805         }
806         
807         ma= bmain->mat.first;
808         while(ma) {
809                 for(a=0; a<MAX_MTEX; a++) {
810                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
811                                 if(ma->id.lib) lib= 1;
812                                 else local= 1;
813                         }
814                 }
815                 ma= ma->id.next;
816         }
817         la= bmain->lamp.first;
818         while(la) {
819                 for(a=0; a<MAX_MTEX; a++) {
820                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
821                                 if(la->id.lib) lib= 1;
822                                 else local= 1;
823                         }
824                 }
825                 la= la->id.next;
826         }
827         wrld= bmain->world.first;
828         while(wrld) {
829                 for(a=0; a<MAX_MTEX; a++) {
830                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
831                                 if(wrld->id.lib) lib= 1;
832                                 else local= 1;
833                         }
834                 }
835                 wrld= wrld->id.next;
836         }
837         br= bmain->brush.first;
838         while(br) {
839                 if(br->mtex.tex==tex) {
840                         if(br->id.lib) lib= 1;
841                         else local= 1;
842                 }
843                 br= br->id.next;
844         }
845         pa= bmain->particle.first;
846         while(pa) {
847                 for(a=0; a<MAX_MTEX; a++) {
848                         if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
849                                 if(pa->id.lib) lib= 1;
850                                 else local= 1;
851                         }
852                 }
853                 pa= pa->id.next;
854         }
855         
856         if(local && lib==0) {
857                 tex->id.lib= NULL;
858                 tex->id.flag= LIB_LOCAL;
859                 new_id(NULL, (ID *)tex, NULL);
860         }
861         else if(local && lib) {
862                 texn= copy_texture(tex);
863                 texn->id.us= 0;
864                 
865                 ma= bmain->mat.first;
866                 while(ma) {
867                         for(a=0; a<MAX_MTEX; a++) {
868                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
869                                         if(ma->id.lib==NULL) {
870                                                 ma->mtex[a]->tex= texn;
871                                                 texn->id.us++;
872                                                 tex->id.us--;
873                                         }
874                                 }
875                         }
876                         ma= ma->id.next;
877                 }
878                 la= bmain->lamp.first;
879                 while(la) {
880                         for(a=0; a<MAX_MTEX; a++) {
881                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
882                                         if(la->id.lib==NULL) {
883                                                 la->mtex[a]->tex= texn;
884                                                 texn->id.us++;
885                                                 tex->id.us--;
886                                         }
887                                 }
888                         }
889                         la= la->id.next;
890                 }
891                 wrld= bmain->world.first;
892                 while(wrld) {
893                         for(a=0; a<MAX_MTEX; a++) {
894                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
895                                         if(wrld->id.lib==NULL) {
896                                                 wrld->mtex[a]->tex= texn;
897                                                 texn->id.us++;
898                                                 tex->id.us--;
899                                         }
900                                 }
901                         }
902                         wrld= wrld->id.next;
903                 }
904                 br= bmain->brush.first;
905                 while(br) {
906                         if(br->mtex.tex==tex) {
907                                 if(br->id.lib==NULL) {
908                                         br->mtex.tex= texn;
909                                         texn->id.us++;
910                                         tex->id.us--;
911                                 }
912                         }
913                         br= br->id.next;
914                 }
915                 pa= bmain->particle.first;
916                 while(pa) {
917                         for(a=0; a<MAX_MTEX; a++) {
918                                 if(pa->mtex[a] && pa->mtex[a]->tex==tex) {
919                                         if(pa->id.lib==NULL) {
920                                                 pa->mtex[a]->tex= texn;
921                                                 texn->id.us++;
922                                                 tex->id.us--;
923                                         }
924                                 }
925                         }
926                         pa= pa->id.next;
927                 }
928         }
929 }
930
931 /* ------------------------------------------------------------------------- */
932
933 void autotexname(Tex *tex)
934 {
935         Main *bmain= G.main;
936         char texstr[20][15]= {"None"  , "Clouds" , "Wood", "Marble", "Magic"  , "Blend",
937                 "Stucci", "Noise"  , "Image", "Plugin", "EnvMap" , "Musgrave",
938                 "Voronoi", "DistNoise", "Point Density", "Voxel Data", "", "", "", ""};
939         Image *ima;
940         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
941         
942         if(tex) {
943                 if(tex->use_nodes) {
944                         new_id(&bmain->tex, (ID *)tex, "Noddy");
945                 }
946                 else
947                 if(tex->type==TEX_IMAGE) {
948                         ima= tex->ima;
949                         if(ima) {
950                                 strcpy(di, ima->name);
951                                 BLI_splitdirstring(di, fi);
952                                 strcpy(di, "I.");
953                                 strcat(di, fi);
954                                 new_id(&bmain->tex, (ID *)tex, di);
955                         }
956                         else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
957                 }
958                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&bmain->tex, (ID *)tex, tex->plugin->pname);
959                 else new_id(&bmain->tex, (ID *)tex, texstr[tex->type]);
960         }
961 }
962
963 /* ------------------------------------------------------------------------- */
964
965 Tex *give_current_object_texture(Object *ob)
966 {
967         Material *ma;
968         Tex *tex= NULL;
969         
970         if(ob==NULL) return NULL;
971         if(ob->totcol==0 && !(ob->type==OB_LAMP)) return NULL;
972         
973         if(ob->type==OB_LAMP) {
974                 tex= give_current_lamp_texture(ob->data);
975         } else {
976                 ma= give_current_material(ob, ob->actcol);
977                 tex= give_current_material_texture(ma);
978         }
979         
980         return tex;
981 }
982
983 Tex *give_current_lamp_texture(Lamp *la)
984 {
985         MTex *mtex= NULL;
986         Tex *tex= NULL;
987
988         if(la) {
989                 mtex= la->mtex[(int)(la->texact)];
990                 if(mtex) tex= mtex->tex;
991         }
992
993         return tex;
994 }
995
996 void set_current_lamp_texture(Lamp *la, Tex *newtex)
997 {
998         int act= la->texact;
999
1000         if(la->mtex[act] && la->mtex[act]->tex)
1001                 id_us_min(&la->mtex[act]->tex->id);
1002
1003         if(newtex) {
1004                 if(!la->mtex[act]) {
1005                         la->mtex[act]= add_mtex();
1006                         la->mtex[act]->texco= TEXCO_GLOB;
1007                 }
1008                 
1009                 la->mtex[act]->tex= newtex;
1010                 id_us_plus(&newtex->id);
1011         }
1012         else if(la->mtex[act]) {
1013                 MEM_freeN(la->mtex[act]);
1014                 la->mtex[act]= NULL;
1015         }
1016 }
1017
1018 bNode *give_current_material_texture_node(Material *ma)
1019 {
1020         if(ma && ma->use_nodes && ma->nodetree)
1021                 return nodeGetActiveID(ma->nodetree, ID_TE);
1022         
1023         return NULL;
1024 }
1025
1026 Tex *give_current_material_texture(Material *ma)
1027 {
1028         MTex *mtex= NULL;
1029         Tex *tex= NULL;
1030         bNode *node;
1031         
1032         if(ma && ma->use_nodes && ma->nodetree) {
1033                 /* first check texture, then material, this works together
1034                    with a hack that clears the active ID flag for textures on
1035                    making a material node active */
1036                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1037
1038                 if(node) {
1039                         tex= (Tex *)node->id;
1040                         ma= NULL;
1041                 }
1042                 else {
1043                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1044                         if(node) {
1045                                 ma= (Material*)node->id;
1046                                 if(ma) {
1047                                         mtex= ma->mtex[(int)(ma->texact)];
1048                                         if(mtex) tex= mtex->tex;
1049                                 }
1050                         }
1051                 }
1052                 return tex;
1053         }
1054
1055         if(ma) {
1056                 mtex= ma->mtex[(int)(ma->texact)];
1057                 if(mtex) tex= mtex->tex;
1058         }
1059         
1060         return tex;
1061 }
1062
1063 int give_active_mtex(ID *id, MTex ***mtex_ar, short *act)
1064 {
1065         switch(GS(id->name)) {
1066         case ID_MA:
1067                 *mtex_ar=               ((Material *)id)->mtex;
1068                 if(act) *act=   (((Material *)id)->texact);
1069                 break;
1070         case ID_WO:
1071                 *mtex_ar=               ((World *)id)->mtex;
1072                 if(act) *act=   (((World *)id)->texact);
1073                 break;
1074         case ID_LA:
1075                 *mtex_ar=               ((Lamp *)id)->mtex;
1076                 if(act) *act=   (((Lamp *)id)->texact);
1077                 break;
1078         case ID_PA:
1079                 *mtex_ar=               ((ParticleSettings *)id)->mtex;
1080                 if(act) *act=   (((ParticleSettings *)id)->texact);
1081                 break;
1082         default:
1083                 *mtex_ar = NULL;
1084                 if(act) *act=   0;
1085                 return FALSE;
1086         }
1087
1088         return TRUE;
1089 }
1090
1091 void set_active_mtex(ID *id, short act)
1092 {
1093         if(act<0)                               act= 0;
1094         else if(act>=MAX_MTEX)  act= MAX_MTEX-1;
1095
1096         switch(GS(id->name)) {
1097         case ID_MA:
1098                 ((Material *)id)->texact= act;
1099                 break;
1100         case ID_WO:
1101                 ((World *)id)->texact= act;
1102                 break;
1103         case ID_LA:
1104                 ((Lamp *)id)->texact= act;
1105                 break;
1106         case ID_PA:
1107                 ((ParticleSettings *)id)->texact= act;
1108                 break;
1109         }
1110 }
1111
1112 void set_current_material_texture(Material *ma, Tex *newtex)
1113 {
1114         Tex *tex= NULL;
1115         bNode *node;
1116         
1117         if(ma && ma->use_nodes && ma->nodetree) {
1118                 node= nodeGetActiveID(ma->nodetree, ID_TE);
1119
1120                 if(node) {
1121                         tex= (Tex *)node->id;
1122                         id_us_min(&tex->id);
1123                         node->id= &newtex->id;
1124                         id_us_plus(&newtex->id);
1125                         ma= NULL;
1126                 }
1127                 else {
1128                         node= nodeGetActiveID(ma->nodetree, ID_MA);
1129                         if(node)
1130                                 ma= (Material*)node->id;
1131                 }
1132         }
1133         if(ma) {
1134                 int act= (int)ma->texact;
1135
1136                 tex= (ma->mtex[act])? ma->mtex[act]->tex: NULL;
1137                 id_us_min(&tex->id);
1138
1139                 if(newtex) {
1140                         if(!ma->mtex[act])
1141                                 ma->mtex[act]= add_mtex();
1142                         
1143                         ma->mtex[act]->tex= newtex;
1144                         id_us_plus(&newtex->id);
1145                 }
1146                 else if(ma->mtex[act]) {
1147                         MEM_freeN(ma->mtex[act]);
1148                         ma->mtex[act]= NULL;
1149                 }
1150         }
1151 }
1152
1153 Tex *give_current_world_texture(World *world)
1154 {
1155         MTex *mtex= NULL;
1156         Tex *tex= NULL;
1157         
1158         if(!world) return NULL;
1159         
1160         mtex= world->mtex[(int)(world->texact)];
1161         if(mtex) tex= mtex->tex;
1162         
1163         return tex;
1164 }
1165
1166 void set_current_world_texture(World *wo, Tex *newtex)
1167 {
1168         int act= wo->texact;
1169
1170         if(wo->mtex[act] && wo->mtex[act]->tex)
1171                 id_us_min(&wo->mtex[act]->tex->id);
1172
1173         if(newtex) {
1174                 if(!wo->mtex[act]) {
1175                         wo->mtex[act]= add_mtex();
1176                         wo->mtex[act]->texco= TEXCO_VIEW;
1177                 }
1178                 
1179                 wo->mtex[act]->tex= newtex;
1180                 id_us_plus(&newtex->id);
1181         }
1182         else if(wo->mtex[act]) {
1183                 MEM_freeN(wo->mtex[act]);
1184                 wo->mtex[act]= NULL;
1185         }
1186 }
1187
1188 Tex *give_current_brush_texture(Brush *br)
1189 {
1190         return br->mtex.tex;
1191 }
1192
1193 void set_current_brush_texture(Brush *br, Tex *newtex)
1194 {
1195         if(br->mtex.tex)
1196                 id_us_min(&br->mtex.tex->id);
1197
1198         if(newtex) {
1199                 br->mtex.tex= newtex;
1200                 id_us_plus(&newtex->id);
1201         }
1202 }
1203
1204 Tex *give_current_particle_texture(ParticleSettings *part)
1205 {
1206         MTex *mtex= NULL;
1207         Tex *tex= NULL;
1208         
1209         if(!part) return NULL;
1210         
1211         mtex= part->mtex[(int)(part->texact)];
1212         if(mtex) tex= mtex->tex;
1213         
1214         return tex;
1215 }
1216
1217 void set_current_particle_texture(ParticleSettings *part, Tex *newtex)
1218 {
1219         int act= part->texact;
1220
1221         if(part->mtex[act] && part->mtex[act]->tex)
1222                 id_us_min(&part->mtex[act]->tex->id);
1223
1224         if(newtex) {
1225                 if(!part->mtex[act]) {
1226                         part->mtex[act]= add_mtex();
1227                         part->mtex[act]->texco= TEXCO_ORCO;
1228                         part->mtex[act]->blendtype= MTEX_MUL;
1229                 }
1230                 
1231                 part->mtex[act]->tex= newtex;
1232                 id_us_plus(&newtex->id);
1233         }
1234         else if(part->mtex[act]) {
1235                 MEM_freeN(part->mtex[act]);
1236                 part->mtex[act]= NULL;
1237         }
1238 }
1239
1240 /* ------------------------------------------------------------------------- */
1241
1242 EnvMap *BKE_add_envmap(void)
1243 {
1244         EnvMap *env;
1245         
1246         env= MEM_callocN(sizeof(EnvMap), "envmap");
1247         env->type= ENV_CUBE;
1248         env->stype= ENV_ANIM;
1249         env->clipsta= 0.1;
1250         env->clipend= 100.0;
1251         env->cuberes= 600;
1252         env->viewscale = 0.5;
1253         
1254         return env;
1255
1256
1257 /* ------------------------------------------------------------------------- */
1258
1259 EnvMap *BKE_copy_envmap(EnvMap *env)
1260 {
1261         EnvMap *envn;
1262         int a;
1263         
1264         envn= MEM_dupallocN(env);
1265         envn->ok= 0;
1266         for(a=0; a<6; a++) envn->cube[a]= NULL;
1267         if(envn->ima) id_us_plus((ID *)envn->ima);
1268         
1269         return envn;
1270 }
1271
1272 /* ------------------------------------------------------------------------- */
1273
1274 void BKE_free_envmapdata(EnvMap *env)
1275 {
1276         unsigned int part;
1277         
1278         for(part=0; part<6; part++) {
1279                 if(env->cube[part])
1280                         IMB_freeImBuf(env->cube[part]);
1281                 env->cube[part]= NULL;
1282         }
1283         env->ok= 0;
1284 }
1285
1286 /* ------------------------------------------------------------------------- */
1287
1288 void BKE_free_envmap(EnvMap *env)
1289 {
1290         
1291         BKE_free_envmapdata(env);
1292         MEM_freeN(env);
1293         
1294 }
1295
1296 /* ------------------------------------------------------------------------- */
1297
1298 PointDensity *BKE_add_pointdensity(void)
1299 {
1300         PointDensity *pd;
1301         
1302         pd= MEM_callocN(sizeof(PointDensity), "pointdensity");
1303         pd->flag = 0;
1304         pd->radius = 0.3f;
1305         pd->falloff_type = TEX_PD_FALLOFF_STD;
1306         pd->falloff_softness = 2.0;
1307         pd->source = TEX_PD_PSYS;
1308         pd->point_tree = NULL;
1309         pd->point_data = NULL;
1310         pd->noise_size = 0.5f;
1311         pd->noise_depth = 1;
1312         pd->noise_fac = 1.0f;
1313         pd->noise_influence = TEX_PD_NOISE_STATIC;
1314         pd->coba = add_colorband(1);
1315         pd->speed_scale = 1.0f;
1316         pd->totpoints = 0;
1317         pd->object = NULL;
1318         pd->psys = 0;
1319         pd->psys_cache_space= TEX_PD_WORLDSPACE;
1320         return pd;
1321
1322
1323 PointDensity *BKE_copy_pointdensity(PointDensity *pd)
1324 {
1325         PointDensity *pdn;
1326
1327         pdn= MEM_dupallocN(pd);
1328         pdn->point_tree = NULL;
1329         pdn->point_data = NULL;
1330         if(pdn->coba) pdn->coba= MEM_dupallocN(pdn->coba);
1331         
1332         return pdn;
1333 }
1334
1335 void BKE_free_pointdensitydata(PointDensity *pd)
1336 {
1337         if (pd->point_tree) {
1338                 BLI_bvhtree_free(pd->point_tree);
1339                 pd->point_tree = NULL;
1340         }
1341         if (pd->point_data) {
1342                 MEM_freeN(pd->point_data);
1343                 pd->point_data = NULL;
1344         }
1345         if(pd->coba) MEM_freeN(pd->coba);
1346 }
1347
1348 void BKE_free_pointdensity(PointDensity *pd)
1349 {
1350         BKE_free_pointdensitydata(pd);
1351         MEM_freeN(pd);
1352 }
1353
1354
1355 void BKE_free_voxeldatadata(struct VoxelData *vd)
1356 {
1357         if (vd->dataset) {
1358                 MEM_freeN(vd->dataset);
1359                 vd->dataset = NULL;
1360         }
1361
1362 }
1363  
1364 void BKE_free_voxeldata(struct VoxelData *vd)
1365 {
1366         BKE_free_voxeldatadata(vd);
1367         MEM_freeN(vd);
1368 }
1369  
1370 struct VoxelData *BKE_add_voxeldata(void)
1371 {
1372         VoxelData *vd;
1373
1374         vd= MEM_callocN(sizeof(struct VoxelData), "voxeldata");
1375         vd->dataset = NULL;
1376         vd->resol[0] = vd->resol[1] = vd->resol[2] = 1;
1377         vd->interp_type= TEX_VD_LINEAR;
1378         vd->file_format= TEX_VD_SMOKE;
1379         vd->int_multiplier = 1.0;
1380         vd->extend = TEX_CLIP;
1381         vd->object = NULL;
1382         vd->cachedframe = -1;
1383         vd->ok = 0;
1384         
1385         return vd;
1386  }
1387  
1388 struct VoxelData *BKE_copy_voxeldata(struct VoxelData *vd)
1389 {
1390         VoxelData *vdn;
1391
1392         vdn= MEM_dupallocN(vd); 
1393         vdn->dataset = NULL;
1394
1395         return vdn;
1396 }
1397
1398
1399 /* ------------------------------------------------------------------------- */
1400 int BKE_texture_dependsOnTime(const struct Tex *texture)
1401 {
1402         if(texture->plugin) {
1403                 // assume all plugins depend on time
1404                 return 1;
1405         } 
1406         else if(        texture->ima && 
1407                         ELEM(texture->ima->source, IMA_SRC_SEQUENCE, IMA_SRC_MOVIE)) {
1408                 return 1;
1409         }
1410         else if(texture->adt) {
1411                 // assume anything in adt means the texture is animated
1412                 return 1;
1413         }
1414         return 0;
1415 }
1416
1417 /* ------------------------------------------------------------------------- */