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