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