=Texture Plugins=
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /* texture.c
2  *
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "MEM_guardedalloc.h"
45
46 #include "PIL_dynlib.h"
47
48 #include "MTC_matrixops.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52 #include "BLI_rand.h"
53
54 #include "DNA_texture_types.h"
55 #include "DNA_key_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_lamp_types.h"
58 #include "DNA_material_types.h"
59 #include "DNA_image_types.h"
60 #include "DNA_world_types.h"
61 #include "DNA_brush_types.h"
62
63 #include "IMB_imbuf_types.h"
64 #include "IMB_imbuf.h"
65
66 #include "BKE_plugin_types.h"
67
68 #include "BKE_bad_level_calls.h"
69 #include "BKE_utildefines.h"
70
71 #include "BKE_global.h"
72 #include "BKE_main.h"
73
74 #include "BKE_library.h"
75 #include "BKE_image.h"
76 #include "BKE_material.h"
77 #include "BKE_texture.h"
78 #include "BKE_key.h"
79 #include "BKE_icons.h"
80 #include "BKE_ipo.h"
81 #include "BKE_brush.h"
82
83
84 /* ------------------------------------------------------------------------- */
85
86 /* All support for plugin textures: */
87 int test_dlerr(const char *name,  const char *symbol)
88 {
89         char *err;
90         
91         err= PIL_dynlib_get_error_as_string(NULL);
92         if(err) {
93                 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
94                 return 1;
95         }
96         
97         return 0;
98 }
99
100 /* ------------------------------------------------------------------------- */
101
102 void open_plugin_tex(PluginTex *pit)
103 {
104         int (*version)(void);
105         
106         /* init all the happy variables */
107         pit->doit= 0;
108         pit->pname= 0;
109         pit->stnames= 0;
110         pit->varstr= 0;
111         pit->result= 0;
112         pit->cfra= 0;
113         pit->version= 0;
114         
115         /* clear the error list */
116         PIL_dynlib_get_error_as_string(NULL);
117
118         /* no PIL_dynlib_close! multiple opened plugins... */
119         /* if(pit->handle) PIL_dynlib_close(pit->handle); */
120         /* pit->handle= 0; */
121
122         /* open the needed object */
123         pit->handle= PIL_dynlib_open(pit->name);
124         if(test_dlerr(pit->name, pit->name)) return;
125
126         if (pit->handle != 0) {
127                 /* find the address of the version function */
128                 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
129                 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
130                 
131                 if (version != 0) {
132                         pit->version= version();
133                         if (pit->version==2 || pit->version==3) {
134                                 int (*info_func)(PluginInfo *);
135                                 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info"); 
136
137                                 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
138                                 if (!test_dlerr(pit->name, "plugin_getinfo")) {
139                                         info->instance_init = NULL;
140
141                                         info_func(info);
142
143                                         pit->doit= (int(*)(void)) info->tex_doit;
144                                         pit->callback= (void(*)(unsigned short)) info->callback;
145                                         pit->stypes= info->stypes;
146                                         pit->vars= info->nvars;
147                                         pit->pname= info->name;
148                                         pit->stnames= info->snames;
149                                         pit->varstr= info->varstr;
150                                         pit->result= info->result;
151                                         pit->cfra= info->cfra;
152                                         pit->instance_init = info->instance_init;
153                                         if (info->init) info->init();
154                                 }
155                                 MEM_freeN(info);
156                         } else {
157                                 printf ("Plugin returned unrecognized version number\n");
158                                 return;
159                         }
160                 }
161         }
162 }
163
164 /* ------------------------------------------------------------------------- */
165
166 /* very badlevel define to bypass linking with BIF_interface.h */
167 #define INT     96
168 #define FLO     128
169
170 PluginTex *add_plugin_tex(char *str)
171 {
172         PluginTex *pit;
173         VarStruct *varstr;
174         int a;
175         
176         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
177         
178         strcpy(pit->name, str);
179         open_plugin_tex(pit);
180         
181         if(pit->doit==0) {
182                 if(pit->handle==0) error("no plugin: %s", str);
183                 else error("in plugin: %s", str);
184                 MEM_freeN(pit);
185                 return NULL;
186         }
187         
188         varstr= pit->varstr;
189         for(a=0; a<pit->vars; a++, varstr++) {
190                 if( (varstr->type & FLO)==FLO)
191                         pit->data[a]= varstr->def;
192                 else if( (varstr->type & INT)==INT)
193                         *((int *)(pit->data+a))= (int) varstr->def;
194         }
195
196         if (pit->instance_init)
197                 pit->instance_init((void *) pit->data);
198
199         return pit;
200 }
201
202 /* ------------------------------------------------------------------------- */
203
204 void free_plugin_tex(PluginTex *pit)
205 {
206         if(pit==0) return;
207                 
208         /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
209         MEM_freeN(pit); 
210 }
211
212 /* ****************** Mapping ******************* */
213
214 TexMapping *add_mapping(void)
215 {
216         TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
217         
218         texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
219         texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
220         Mat4One(texmap->mat);
221         
222         return texmap;
223 }
224
225 void init_mapping(TexMapping *texmap)
226 {
227         float eul[3], smat[3][3], rmat[3][3], mat[3][3];
228         
229         SizeToMat3(texmap->size, smat);
230         
231         eul[0]= (M_PI/180.0f)*texmap->rot[0];
232         eul[1]= (M_PI/180.0f)*texmap->rot[1];
233         eul[2]= (M_PI/180.0f)*texmap->rot[2];
234         EulToMat3(eul, rmat);
235         
236         Mat3MulMat3(mat, rmat, smat);
237         
238         Mat4CpyMat3(texmap->mat, mat);
239         VECCOPY(texmap->mat[3], texmap->loc);
240
241 }
242
243 /* ****************** COLORBAND ******************* */
244
245 ColorBand *add_colorband(int rangetype)
246 {
247         ColorBand *coba;
248         int a;
249         
250         coba= MEM_callocN( sizeof(ColorBand), "colorband");
251         
252         coba->data[0].pos= 0.0;
253         coba->data[1].pos= 1.0;
254         
255         if(rangetype==0) {
256                 coba->data[0].r= 0.0;
257                 coba->data[0].g= 0.0;
258                 coba->data[0].b= 0.0;
259                 coba->data[0].a= 0.0;
260                 
261                 coba->data[1].r= 0.0;
262                 coba->data[1].g= 1.0;
263                 coba->data[1].b= 1.0;
264                 coba->data[1].a= 1.0;
265         }
266         else {
267                 coba->data[0].r= 0.0;
268                 coba->data[0].g= 0.0;
269                 coba->data[0].b= 0.0;
270                 coba->data[0].a= 1.0;
271                 
272                 coba->data[1].r= 1.0;
273                 coba->data[1].g= 1.0;
274                 coba->data[1].b= 1.0;
275                 coba->data[1].a= 1.0;
276         }
277         
278         for(a=2; a<MAXCOLORBAND; a++) {
279                 coba->data[a].r= 0.5;
280                 coba->data[a].g= 0.5;
281                 coba->data[a].b= 0.5;
282                 coba->data[a].a= 1.0;
283                 coba->data[a].pos= 0.5;
284         }
285         
286         coba->tot= 2;
287         
288         return coba;
289 }
290
291 /* ------------------------------------------------------------------------- */
292
293 int do_colorband(ColorBand *coba, float in, float out[4])
294 {
295         CBData *cbd1, *cbd2, *cbd0, *cbd3;
296         float fac, mfac, t[4];
297         int a;
298         
299         if(coba->tot==0) return 0;
300         
301         cbd1= coba->data;
302         if(coba->tot==1) {
303                 out[0]= cbd1->r;
304                 out[1]= cbd1->g;
305                 out[2]= cbd1->b;
306                 out[3]= cbd1->a;
307         }
308         else {
309                 if(in <= cbd1->pos && coba->ipotype<2) {
310                         out[0]= cbd1->r;
311                         out[1]= cbd1->g;
312                         out[2]= cbd1->b;
313                         out[3]= cbd1->a;
314                 }
315                 else {
316                         CBData left, right;
317                         
318                         /* we're looking for first pos > in */
319                         for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
320                                 
321                         if(a==coba->tot) {
322                                 cbd2= cbd1-1;
323                                 right= *cbd2;
324                                 right.pos= 1.0f;
325                                 cbd1= &right;
326                         }
327                         else if(a==0) {
328                                 left= *cbd1;
329                                 left.pos= 0.0f;
330                                 cbd2= &left;
331                         }
332                         else cbd2= cbd1-1;
333                         
334                         if(in >= cbd1->pos && coba->ipotype<2) {
335                                 out[0]= cbd1->r;
336                                 out[1]= cbd1->g;
337                                 out[2]= cbd1->b;
338                                 out[3]= cbd1->a;
339                         }
340                         else {
341                 
342                                 if(cbd2->pos!=cbd1->pos)
343                                         fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
344                                 else
345                                         fac= 0.0f;
346                                 
347                                 if(coba->ipotype>=2) {
348                                         /* ipo from right to left: 3 2 1 0 */
349                                         
350                                         if(a>=coba->tot-1) cbd0= cbd1;
351                                         else cbd0= cbd1+1;
352                                         if(a<2) cbd3= cbd2;
353                                         else cbd3= cbd2-1;
354                                         
355                                         CLAMP(fac, 0.0f, 1.0f);
356                                         
357                                         if(coba->ipotype==3)
358                                                 set_four_ipo(fac, t, KEY_CARDINAL);
359                                         else
360                                                 set_four_ipo(fac, t, KEY_BSPLINE);
361
362                                         out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
363                                         out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
364                                         out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
365                                         out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
366                                         CLAMP(out[0], 0.0, 1.0);
367                                         CLAMP(out[1], 0.0, 1.0);
368                                         CLAMP(out[2], 0.0, 1.0);
369                                         CLAMP(out[3], 0.0, 1.0);
370                                 }
371                                 else {
372                                 
373                                         if(coba->ipotype==1) {  /* EASE */
374                                                 mfac= fac*fac;
375                                                 fac= 3.0f*mfac-2.0f*mfac*fac;
376                                         }
377                                         mfac= 1.0f-fac;
378                                         
379                                         out[0]= mfac*cbd1->r + fac*cbd2->r;
380                                         out[1]= mfac*cbd1->g + fac*cbd2->g;
381                                         out[2]= mfac*cbd1->b + fac*cbd2->b;
382                                         out[3]= mfac*cbd1->a + fac*cbd2->a;
383                                 }
384                         }
385                 }
386         }
387         return 1;       /* OK */
388 }
389
390 /* ******************* TEX ************************ */
391
392 void free_texture(Tex *tex)
393 {
394         free_plugin_tex(tex->plugin);
395         if(tex->coba) MEM_freeN(tex->coba);
396         if(tex->env) BKE_free_envmap(tex->env);
397         BKE_icon_delete((struct ID*)tex);
398         tex->id.icon_id = 0;
399 }
400
401 /* ------------------------------------------------------------------------- */
402
403 void default_tex(Tex *tex)
404 {
405         PluginTex *pit;
406         VarStruct *varstr;
407         int a;
408
409         tex->stype= 0;
410         tex->flag= TEX_CHECKER_ODD;
411         tex->imaflag= TEX_INTERPOL+TEX_MIPMAP;
412         tex->extend= TEX_REPEAT;
413         tex->cropxmin= tex->cropymin= 0.0;
414         tex->cropxmax= tex->cropymax= 1.0;
415         tex->xrepeat= tex->yrepeat= 1;
416         tex->fie_ima= 2;
417         tex->sfra= 1;
418         tex->frames= 0;
419         tex->offset= 0;
420         tex->noisesize= 0.25;
421         tex->noisedepth= 2;
422         tex->turbul= 5.0;
423         tex->nabla= 0.025;      // also in do_versions
424         tex->bright= 1.0;
425         tex->contrast= tex->filtersize= 1.0;
426         tex->rfac= 1.0;
427         tex->gfac= 1.0;
428         tex->bfac= 1.0;
429         /* newnoise: init. */
430         tex->noisebasis = 0;
431         tex->noisebasis2 = 0;
432         /* musgrave */
433         tex->mg_H = 1.0;
434         tex->mg_lacunarity = 2.0;
435         tex->mg_octaves = 2.0;
436         tex->mg_offset = 1.0;
437         tex->mg_gain = 1.0;
438         tex->ns_outscale = 1.0;
439         /* distnoise */
440         tex->dist_amount = 1.0;
441         /* voronoi */
442         tex->vn_w1 = 1.0;
443         tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
444         tex->vn_mexp = 2.5;
445         tex->vn_distm = 0;
446         tex->vn_coltype = 0;
447
448         if (tex->env) {
449                 tex->env->stype=ENV_STATIC;
450                 tex->env->clipsta=0.1;
451                 tex->env->clipend=100;
452                 tex->env->cuberes=100;
453                 tex->env->depth=0;
454         }
455
456         pit = tex->plugin;
457         if (pit) {
458                         varstr= pit->varstr;
459                         if(varstr) {
460                                         for(a=0; a<pit->vars; a++, varstr++) {
461                                                 pit->data[a] = varstr->def;
462                                         }
463                         }
464         }
465 }
466
467 /* ------------------------------------------------------------------------- */
468
469 Tex *add_texture(char *name)
470 {
471         Tex *tex;
472
473         tex= alloc_libblock(&G.main->tex, ID_TE, name);
474         
475         default_tex(tex);
476         
477         return tex;
478 }
479
480 /* ------------------------------------------------------------------------- */
481
482 void default_mtex(MTex *mtex)
483 {
484         mtex->texco= TEXCO_ORCO;
485         mtex->mapto= MAP_COL;
486         mtex->object= 0;
487         mtex->projx= PROJ_X;
488         mtex->projy= PROJ_Y;
489         mtex->projz= PROJ_Z;
490         mtex->mapping= MTEX_FLAT;
491         mtex->ofs[0]= 0.0;
492         mtex->ofs[1]= 0.0;
493         mtex->ofs[2]= 0.0;
494         mtex->size[0]= 1.0;
495         mtex->size[1]= 1.0;
496         mtex->size[2]= 1.0;
497         mtex->tex= 0;
498         mtex->texflag= 0;
499         mtex->colormodel= 0;
500         mtex->r= 1.0;
501         mtex->g= 0.0;
502         mtex->b= 1.0;
503         mtex->k= 1.0;
504         mtex->def_var= 1.0;
505         mtex->blendtype= MTEX_BLEND;
506         mtex->colfac= 1.0;
507         mtex->norfac= 0.5;
508         mtex->varfac= 1.0;
509         mtex->dispfac=0.2;
510 }
511
512
513 /* ------------------------------------------------------------------------- */
514
515 MTex *add_mtex()
516 {
517         MTex *mtex;
518         
519         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
520         
521         default_mtex(mtex);
522         
523         return mtex;
524 }
525
526 /* ------------------------------------------------------------------------- */
527
528 Tex *copy_texture(Tex *tex)
529 {
530         Tex *texn;
531         
532         texn= copy_libblock(tex);
533         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
534         else texn->ima= 0;
535         
536         if(texn->plugin) {
537                 texn->plugin= MEM_dupallocN(texn->plugin);
538                 open_plugin_tex(texn->plugin);
539         }
540         
541         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
542         if(texn->env) texn->env= BKE_copy_envmap(texn->env);
543         
544         return texn;
545 }
546
547 /* ------------------------------------------------------------------------- */
548
549 void make_local_texture(Tex *tex)
550 {
551         Tex *texn;
552         Material *ma;
553         World *wrld;
554         Lamp *la;
555         Brush *br;
556         int a, local=0, lib=0;
557
558         /* - only lib users: do nothing
559             * - only local users: set flag
560             * - mixed: make copy
561             */
562         
563         if(tex->id.lib==0) return;
564
565         /* special case: ima always local immediately */
566         if(tex->ima) {
567                 tex->ima->id.lib= 0;
568                 tex->ima->id.flag= LIB_LOCAL;
569                 new_id(0, (ID *)tex->ima, 0);
570         }
571
572         if(tex->id.us==1) {
573                 tex->id.lib= 0;
574                 tex->id.flag= LIB_LOCAL;
575                 new_id(0, (ID *)tex, 0);
576
577                 return;
578         }
579         
580         ma= G.main->mat.first;
581         while(ma) {
582                 for(a=0; a<MAX_MTEX; a++) {
583                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
584                                 if(ma->id.lib) lib= 1;
585                                 else local= 1;
586                         }
587                 }
588                 ma= ma->id.next;
589         }
590         la= G.main->lamp.first;
591         while(la) {
592                 for(a=0; a<MAX_MTEX; a++) {
593                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
594                                 if(la->id.lib) lib= 1;
595                                 else local= 1;
596                         }
597                 }
598                 la= la->id.next;
599         }
600         wrld= G.main->world.first;
601         while(wrld) {
602                 for(a=0; a<MAX_MTEX; a++) {
603                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
604                                 if(wrld->id.lib) lib= 1;
605                                 else local= 1;
606                         }
607                 }
608                 wrld= wrld->id.next;
609         }
610         br= G.main->brush.first;
611         while(br) {
612                 for(a=0; a<MAX_MTEX; a++) {
613                         if(br->mtex[a] && br->mtex[a]->tex==tex) {
614                                 if(br->id.lib) lib= 1;
615                                 else local= 1;
616                         }
617                 }
618                 br= br->id.next;
619         }
620         
621         if(local && lib==0) {
622                 tex->id.lib= 0;
623                 tex->id.flag= LIB_LOCAL;
624                 new_id(0, (ID *)tex, 0);
625         }
626         else if(local && lib) {
627                 texn= copy_texture(tex);
628                 texn->id.us= 0;
629                 
630                 ma= G.main->mat.first;
631                 while(ma) {
632                         for(a=0; a<MAX_MTEX; a++) {
633                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
634                                         if(ma->id.lib==0) {
635                                                 ma->mtex[a]->tex= texn;
636                                                 texn->id.us++;
637                                                 tex->id.us--;
638                                         }
639                                 }
640                         }
641                         ma= ma->id.next;
642                 }
643                 la= G.main->lamp.first;
644                 while(la) {
645                         for(a=0; a<MAX_MTEX; a++) {
646                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
647                                         if(la->id.lib==0) {
648                                                 la->mtex[a]->tex= texn;
649                                                 texn->id.us++;
650                                                 tex->id.us--;
651                                         }
652                                 }
653                         }
654                         la= la->id.next;
655                 }
656                 wrld= G.main->world.first;
657                 while(wrld) {
658                         for(a=0; a<MAX_MTEX; a++) {
659                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
660                                         if(wrld->id.lib==0) {
661                                                 wrld->mtex[a]->tex= texn;
662                                                 texn->id.us++;
663                                                 tex->id.us--;
664                                         }
665                                 }
666                         }
667                         wrld= wrld->id.next;
668                 }
669                 br= G.main->brush.first;
670                 while(br) {
671                         for(a=0; a<MAX_MTEX; a++) {
672                                 if(br->mtex[a] && br->mtex[a]->tex==tex) {
673                                         if(br->id.lib==0) {
674                                                 br->mtex[a]->tex= texn;
675                                                 texn->id.us++;
676                                                 tex->id.us--;
677                                         }
678                                 }
679                         }
680                         br= br->id.next;
681                 }
682         }
683 }
684
685 /* ------------------------------------------------------------------------- */
686
687 void autotexname(Tex *tex)
688 {
689 /*      extern char texstr[20][12];      *//* buttons.c, already in bad lev calls*/
690         Image *ima;
691         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
692         
693         if(tex) {
694                 if(tex->type==TEX_IMAGE) {
695                         ima= tex->ima;
696                         if(ima) {
697                                 strcpy(di, ima->name);
698                                 BLI_splitdirstring(di, fi);
699                                 strcpy(di, "I.");
700                                 strcat(di, fi);
701                                 new_id(&G.main->tex, (ID *)tex, di);
702                         }
703                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
704                 }
705                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
706                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
707         }
708 }
709
710 /* ------------------------------------------------------------------------- */
711
712 Tex *give_current_texture(Object *ob, int act)
713 {
714         Material ***matarar, *ma;
715         Lamp *la = 0;
716         MTex *mtex = 0;
717         Tex *tex = 0;
718         
719         if(ob==0) return 0;
720         if(ob->totcol==0) return 0;
721         
722         if(ob->type==OB_LAMP) {
723                 la=(Lamp *)ob->data;
724                 if(la) {
725                         mtex= la->mtex[(int)(la->texact)];
726                         if(mtex) tex= mtex->tex;
727                 }
728                 else tex= 0;
729         } else {
730                 if(act>ob->totcol) act= ob->totcol;
731                 else if(act==0) act= 1;
732                 
733                 if( BTST(ob->colbits, act-1) ) {        /* in object */
734                         ma= ob->mat[act-1];
735                 }
736                 else {                                                          /* in data */
737                         matarar= give_matarar(ob);
738                         
739                         if(matarar && *matarar) ma= (*matarar)[act-1];
740                         else ma= 0;
741                         
742                 }
743                 if(ma) {
744                         mtex= ma->mtex[(int)(ma->texact)];
745                         if(mtex) tex= mtex->tex;
746                 }
747                 else tex= 0;
748         }
749         
750         return tex;
751 }
752
753
754 /* ------------------------------------------------------------------------- */
755
756 EnvMap *BKE_add_envmap(void)
757 {
758         EnvMap *env;
759         
760         env= MEM_callocN(sizeof(EnvMap), "envmap");
761         env->type= ENV_CUBE;
762         env->stype= ENV_STATIC;
763         env->clipsta= 0.1;
764         env->clipend= 100.0;
765         env->cuberes= 100;
766         
767         return env;
768
769
770 /* ------------------------------------------------------------------------- */
771
772 EnvMap *BKE_copy_envmap(EnvMap *env)
773 {
774         EnvMap *envn;
775         int a;
776         
777         envn= MEM_dupallocN(env);
778         envn->ok= 0;
779         for(a=0; a<6; a++) envn->cube[a]= 0;
780         if(envn->ima) id_us_plus((ID *)envn->ima);
781         
782         return envn;
783 }
784
785 /* ------------------------------------------------------------------------- */
786
787 void BKE_free_envmapdata(EnvMap *env)
788 {
789         Image *ima;
790         unsigned int a, part;
791         
792         for(part=0; part<6; part++) {
793                 ima= env->cube[part];
794                 if(ima) {
795                         if(ima->ibuf) IMB_freeImBuf(ima->ibuf);
796                         
797                         for(a=0; a<BLI_ARRAY_NELEMS(ima->mipmap); a++) {
798                                 if(ima->mipmap[a]) IMB_freeImBuf(ima->mipmap[a]);
799                         }
800                         MEM_freeN(ima);
801                         env->cube[part]= 0;
802                 }
803         }
804         env->ok= 0;
805 }
806
807 /* ------------------------------------------------------------------------- */
808
809 void BKE_free_envmap(EnvMap *env)
810 {
811         
812         BKE_free_envmapdata(env);
813         MEM_freeN(env);
814         
815 }
816
817 /* ------------------------------------------------------------------------- */