Fix for bug #8555: geometry node front/bake was broken.
[plumiferos.git] / source / blender / blenkernel / intern / texture.c
blobbe13b87c88fec6e4cb4d7644880fa61c006381dc
1 /* texture.c
4 * $Id: texture.c 10536 2007-04-16 10:55:59Z ton $
6 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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.
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.
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.
25 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 * All rights reserved.
28 * The Original Code is: all of this file.
30 * Contributor(s): none yet.
32 * ***** END GPL/BL DUAL LICENSE BLOCK *****
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
40 #include "MEM_guardedalloc.h"
42 #include "PIL_dynlib.h"
44 #include "MTC_matrixops.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_arithb.h"
48 #include "BLI_rand.h"
50 #include "DNA_texture_types.h"
51 #include "DNA_key_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_image_types.h"
56 #include "DNA_world_types.h"
57 #include "DNA_brush_types.h"
58 #include "DNA_node_types.h"
60 #include "IMB_imbuf_types.h"
61 #include "IMB_imbuf.h"
63 #include "BKE_plugin_types.h"
65 #include "BKE_bad_level_calls.h"
66 #include "BKE_utildefines.h"
68 #include "BKE_global.h"
69 #include "BKE_main.h"
71 #include "BKE_library.h"
72 #include "BKE_image.h"
73 #include "BKE_material.h"
74 #include "BKE_texture.h"
75 #include "BKE_key.h"
76 #include "BKE_icons.h"
77 #include "BKE_ipo.h"
78 #include "BKE_brush.h"
79 #include "BKE_node.h"
82 /* ------------------------------------------------------------------------- */
84 /* All support for plugin textures: */
85 int test_dlerr(const char *name, const char *symbol)
87 char *err;
89 err= PIL_dynlib_get_error_as_string(NULL);
90 if(err) {
91 printf("var1: %s, var2: %s, var3: %s\n", name, symbol, err);
92 return 1;
95 return 0;
98 /* ------------------------------------------------------------------------- */
100 void open_plugin_tex(PluginTex *pit)
102 int (*version)(void);
104 /* init all the happy variables */
105 pit->doit= 0;
106 pit->pname= 0;
107 pit->stnames= 0;
108 pit->varstr= 0;
109 pit->result= 0;
110 pit->cfra= 0;
111 pit->version= 0;
113 /* clear the error list */
114 PIL_dynlib_get_error_as_string(NULL);
116 /* no PIL_dynlib_close! multiple opened plugins... */
117 /* if(pit->handle) PIL_dynlib_close(pit->handle); */
118 /* pit->handle= 0; */
120 /* open the needed object */
121 pit->handle= PIL_dynlib_open(pit->name);
122 if(test_dlerr(pit->name, pit->name)) return;
124 if (pit->handle != 0) {
125 /* find the address of the version function */
126 version= (int (*)(void)) PIL_dynlib_find_symbol(pit->handle, "plugin_tex_getversion");
127 if (test_dlerr(pit->name, "plugin_tex_getversion")) return;
129 if (version != 0) {
130 pit->version= version();
131 if (pit->version>=2 && pit->version<=5) {
132 int (*info_func)(PluginInfo *);
133 PluginInfo *info= (PluginInfo*) MEM_mallocN(sizeof(PluginInfo), "plugin_info");
135 info_func= (int (*)(PluginInfo *))PIL_dynlib_find_symbol(pit->handle, "plugin_getinfo");
136 if (!test_dlerr(pit->name, "plugin_getinfo")) {
137 info->instance_init = NULL;
139 info_func(info);
141 pit->doit= (int(*)(void)) info->tex_doit;
142 pit->callback= (void(*)(unsigned short)) info->callback;
143 pit->stypes= info->stypes;
144 pit->vars= info->nvars;
145 pit->pname= info->name;
146 pit->stnames= info->snames;
147 pit->varstr= info->varstr;
148 pit->result= info->result;
149 pit->cfra= info->cfra;
150 pit->instance_init = info->instance_init;
151 if (info->init) info->init();
153 MEM_freeN(info);
154 } else {
155 printf ("Plugin returned unrecognized version number\n");
156 return;
162 /* ------------------------------------------------------------------------- */
164 /* very badlevel define to bypass linking with BIF_interface.h */
165 #define INT 96
166 #define FLO 128
168 PluginTex *add_plugin_tex(char *str)
170 PluginTex *pit;
171 VarStruct *varstr;
172 int a;
174 pit= MEM_callocN(sizeof(PluginTex), "plugintex");
176 strcpy(pit->name, str);
177 open_plugin_tex(pit);
179 if(pit->doit==0) {
180 if(pit->handle==0) error("no plugin: %s", str);
181 else error("in plugin: %s", str);
182 MEM_freeN(pit);
183 return NULL;
186 varstr= pit->varstr;
187 for(a=0; a<pit->vars; a++, varstr++) {
188 if( (varstr->type & FLO)==FLO)
189 pit->data[a]= varstr->def;
190 else if( (varstr->type & INT)==INT)
191 *((int *)(pit->data+a))= (int) varstr->def;
194 if (pit->instance_init)
195 pit->instance_init((void *) pit->data);
197 return pit;
200 /* ------------------------------------------------------------------------- */
202 void free_plugin_tex(PluginTex *pit)
204 if(pit==0) return;
206 /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
207 MEM_freeN(pit);
210 /* ****************** Mapping ******************* */
212 TexMapping *add_mapping(void)
214 TexMapping *texmap= MEM_callocN(sizeof(TexMapping), "Tex map");
216 texmap->size[0]= texmap->size[1]= texmap->size[2]= 1.0f;
217 texmap->max[0]= texmap->max[1]= texmap->max[2]= 1.0f;
218 Mat4One(texmap->mat);
220 return texmap;
223 void init_mapping(TexMapping *texmap)
225 float eul[3], smat[3][3], rmat[3][3], mat[3][3];
227 SizeToMat3(texmap->size, smat);
229 eul[0]= (M_PI/180.0f)*texmap->rot[0];
230 eul[1]= (M_PI/180.0f)*texmap->rot[1];
231 eul[2]= (M_PI/180.0f)*texmap->rot[2];
232 EulToMat3(eul, rmat);
234 Mat3MulMat3(mat, rmat, smat);
236 Mat4CpyMat3(texmap->mat, mat);
237 VECCOPY(texmap->mat[3], texmap->loc);
241 /* ****************** COLORBAND ******************* */
243 ColorBand *add_colorband(int rangetype)
245 ColorBand *coba;
246 int a;
248 coba= MEM_callocN( sizeof(ColorBand), "colorband");
250 coba->data[0].pos= 0.0;
251 coba->data[1].pos= 1.0;
253 if(rangetype==0) {
254 coba->data[0].r= 0.0;
255 coba->data[0].g= 0.0;
256 coba->data[0].b= 0.0;
257 coba->data[0].a= 0.0;
259 coba->data[1].r= 0.0;
260 coba->data[1].g= 1.0;
261 coba->data[1].b= 1.0;
262 coba->data[1].a= 1.0;
264 else {
265 coba->data[0].r= 0.0;
266 coba->data[0].g= 0.0;
267 coba->data[0].b= 0.0;
268 coba->data[0].a= 1.0;
270 coba->data[1].r= 1.0;
271 coba->data[1].g= 1.0;
272 coba->data[1].b= 1.0;
273 coba->data[1].a= 1.0;
276 for(a=2; a<MAXCOLORBAND; a++) {
277 coba->data[a].r= 0.5;
278 coba->data[a].g= 0.5;
279 coba->data[a].b= 0.5;
280 coba->data[a].a= 1.0;
281 coba->data[a].pos= 0.5;
284 coba->tot= 2;
286 return coba;
289 /* ------------------------------------------------------------------------- */
291 int do_colorband(ColorBand *coba, float in, float out[4])
293 CBData *cbd1, *cbd2, *cbd0, *cbd3;
294 float fac, mfac, t[4];
295 int a;
297 if(coba==NULL || coba->tot==0) return 0;
299 cbd1= coba->data;
300 if(coba->tot==1) {
301 out[0]= cbd1->r;
302 out[1]= cbd1->g;
303 out[2]= cbd1->b;
304 out[3]= cbd1->a;
306 else {
307 if(in <= cbd1->pos && coba->ipotype<2) {
308 out[0]= cbd1->r;
309 out[1]= cbd1->g;
310 out[2]= cbd1->b;
311 out[3]= cbd1->a;
313 else {
314 CBData left, right;
316 /* we're looking for first pos > in */
317 for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos > in) break;
319 if(a==coba->tot) {
320 cbd2= cbd1-1;
321 right= *cbd2;
322 right.pos= 1.0f;
323 cbd1= &right;
325 else if(a==0) {
326 left= *cbd1;
327 left.pos= 0.0f;
328 cbd2= &left;
330 else cbd2= cbd1-1;
332 if(in >= cbd1->pos && coba->ipotype<2) {
333 out[0]= cbd1->r;
334 out[1]= cbd1->g;
335 out[2]= cbd1->b;
336 out[3]= cbd1->a;
338 else {
340 if(cbd2->pos!=cbd1->pos)
341 fac= (in-cbd1->pos)/(cbd2->pos-cbd1->pos);
342 else
343 fac= 0.0f;
345 if(coba->ipotype>=2) {
346 /* ipo from right to left: 3 2 1 0 */
348 if(a>=coba->tot-1) cbd0= cbd1;
349 else cbd0= cbd1+1;
350 if(a<2) cbd3= cbd2;
351 else cbd3= cbd2-1;
353 CLAMP(fac, 0.0f, 1.0f);
355 if(coba->ipotype==3)
356 set_four_ipo(fac, t, KEY_CARDINAL);
357 else
358 set_four_ipo(fac, t, KEY_BSPLINE);
360 out[0]= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
361 out[1]= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
362 out[2]= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
363 out[3]= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
364 CLAMP(out[0], 0.0, 1.0);
365 CLAMP(out[1], 0.0, 1.0);
366 CLAMP(out[2], 0.0, 1.0);
367 CLAMP(out[3], 0.0, 1.0);
369 else {
371 if(coba->ipotype==1) { /* EASE */
372 mfac= fac*fac;
373 fac= 3.0f*mfac-2.0f*mfac*fac;
375 mfac= 1.0f-fac;
377 out[0]= mfac*cbd1->r + fac*cbd2->r;
378 out[1]= mfac*cbd1->g + fac*cbd2->g;
379 out[2]= mfac*cbd1->b + fac*cbd2->b;
380 out[3]= mfac*cbd1->a + fac*cbd2->a;
385 return 1; /* OK */
388 /* ******************* TEX ************************ */
390 void free_texture(Tex *tex)
392 free_plugin_tex(tex->plugin);
393 if(tex->coba) MEM_freeN(tex->coba);
394 if(tex->env) BKE_free_envmap(tex->env);
395 BKE_icon_delete((struct ID*)tex);
396 tex->id.icon_id = 0;
399 /* ------------------------------------------------------------------------- */
401 void default_tex(Tex *tex)
403 PluginTex *pit;
404 VarStruct *varstr;
405 int a;
407 tex->stype= 0;
408 tex->flag= TEX_CHECKER_ODD;
409 tex->imaflag= TEX_INTERPOL+TEX_MIPMAP+TEX_USEALPHA;
410 tex->extend= TEX_REPEAT;
411 tex->cropxmin= tex->cropymin= 0.0;
412 tex->cropxmax= tex->cropymax= 1.0;
413 tex->xrepeat= tex->yrepeat= 1;
414 tex->fie_ima= 2;
415 tex->sfra= 1;
416 tex->frames= 0;
417 tex->offset= 0;
418 tex->noisesize= 0.25;
419 tex->noisedepth= 2;
420 tex->turbul= 5.0;
421 tex->nabla= 0.025; // also in do_versions
422 tex->bright= 1.0;
423 tex->contrast= 1.0;
424 tex->filtersize= 1.0;
425 tex->rfac= 1.0;
426 tex->gfac= 1.0;
427 tex->bfac= 1.0;
428 /* newnoise: init. */
429 tex->noisebasis = 0;
430 tex->noisebasis2 = 0;
431 /* musgrave */
432 tex->mg_H = 1.0;
433 tex->mg_lacunarity = 2.0;
434 tex->mg_octaves = 2.0;
435 tex->mg_offset = 1.0;
436 tex->mg_gain = 1.0;
437 tex->ns_outscale = 1.0;
438 /* distnoise */
439 tex->dist_amount = 1.0;
440 /* voronoi */
441 tex->vn_w1 = 1.0;
442 tex->vn_w2 = tex->vn_w3 = tex->vn_w4 = 0.0;
443 tex->vn_mexp = 2.5;
444 tex->vn_distm = 0;
445 tex->vn_coltype = 0;
447 if (tex->env) {
448 tex->env->stype=ENV_STATIC;
449 tex->env->clipsta=0.1;
450 tex->env->clipend=100;
451 tex->env->cuberes=100;
452 tex->env->depth=0;
455 pit = tex->plugin;
456 if (pit) {
457 varstr= pit->varstr;
458 if(varstr) {
459 for(a=0; a<pit->vars; a++, varstr++) {
460 pit->data[a] = varstr->def;
465 tex->iuser.fie_ima= 2;
466 tex->iuser.ok= 1;
467 tex->iuser.frames= 100;
470 /* ------------------------------------------------------------------------- */
472 Tex *add_texture(char *name)
474 Tex *tex;
476 tex= alloc_libblock(&G.main->tex, ID_TE, name);
478 default_tex(tex);
480 return tex;
483 /* ------------------------------------------------------------------------- */
485 void default_mtex(MTex *mtex)
487 mtex->texco= TEXCO_ORCO;
488 mtex->mapto= MAP_COL;
489 mtex->object= 0;
490 mtex->projx= PROJ_X;
491 mtex->projy= PROJ_Y;
492 mtex->projz= PROJ_Z;
493 mtex->mapping= MTEX_FLAT;
494 mtex->ofs[0]= 0.0;
495 mtex->ofs[1]= 0.0;
496 mtex->ofs[2]= 0.0;
497 mtex->size[0]= 1.0;
498 mtex->size[1]= 1.0;
499 mtex->size[2]= 1.0;
500 mtex->tex= 0;
501 mtex->texflag= 0;
502 mtex->colormodel= 0;
503 mtex->r= 1.0;
504 mtex->g= 0.0;
505 mtex->b= 1.0;
506 mtex->k= 1.0;
507 mtex->def_var= 1.0;
508 mtex->blendtype= MTEX_BLEND;
509 mtex->colfac= 1.0;
510 mtex->norfac= 0.5;
511 mtex->varfac= 1.0;
512 mtex->dispfac=0.2;
516 /* ------------------------------------------------------------------------- */
518 MTex *add_mtex()
520 MTex *mtex;
522 mtex= MEM_callocN(sizeof(MTex), "add_mtex");
524 default_mtex(mtex);
526 return mtex;
529 /* ------------------------------------------------------------------------- */
531 Tex *copy_texture(Tex *tex)
533 Tex *texn;
535 texn= copy_libblock(tex);
536 if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
537 else texn->ima= 0;
539 if(texn->plugin) {
540 texn->plugin= MEM_dupallocN(texn->plugin);
541 open_plugin_tex(texn->plugin);
544 if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
545 if(texn->env) texn->env= BKE_copy_envmap(texn->env);
547 return texn;
550 /* ------------------------------------------------------------------------- */
552 void make_local_texture(Tex *tex)
554 Tex *texn;
555 Material *ma;
556 World *wrld;
557 Lamp *la;
558 Brush *br;
559 int a, local=0, lib=0;
561 /* - only lib users: do nothing
562 * - only local users: set flag
563 * - mixed: make copy
566 if(tex->id.lib==0) return;
568 /* special case: ima always local immediately */
569 if(tex->ima) {
570 tex->ima->id.lib= 0;
571 tex->ima->id.flag= LIB_LOCAL;
572 new_id(0, (ID *)tex->ima, 0);
575 if(tex->id.us==1) {
576 tex->id.lib= 0;
577 tex->id.flag= LIB_LOCAL;
578 new_id(0, (ID *)tex, 0);
580 return;
583 ma= G.main->mat.first;
584 while(ma) {
585 for(a=0; a<MAX_MTEX; a++) {
586 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
587 if(ma->id.lib) lib= 1;
588 else local= 1;
591 ma= ma->id.next;
593 la= G.main->lamp.first;
594 while(la) {
595 for(a=0; a<MAX_MTEX; a++) {
596 if(la->mtex[a] && la->mtex[a]->tex==tex) {
597 if(la->id.lib) lib= 1;
598 else local= 1;
601 la= la->id.next;
603 wrld= G.main->world.first;
604 while(wrld) {
605 for(a=0; a<MAX_MTEX; a++) {
606 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
607 if(wrld->id.lib) lib= 1;
608 else local= 1;
611 wrld= wrld->id.next;
613 br= G.main->brush.first;
614 while(br) {
615 for(a=0; a<MAX_MTEX; a++) {
616 if(br->mtex[a] && br->mtex[a]->tex==tex) {
617 if(br->id.lib) lib= 1;
618 else local= 1;
621 br= br->id.next;
624 if(local && lib==0) {
625 tex->id.lib= 0;
626 tex->id.flag= LIB_LOCAL;
627 new_id(0, (ID *)tex, 0);
629 else if(local && lib) {
630 texn= copy_texture(tex);
631 texn->id.us= 0;
633 ma= G.main->mat.first;
634 while(ma) {
635 for(a=0; a<MAX_MTEX; a++) {
636 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
637 if(ma->id.lib==0) {
638 ma->mtex[a]->tex= texn;
639 texn->id.us++;
640 tex->id.us--;
644 ma= ma->id.next;
646 la= G.main->lamp.first;
647 while(la) {
648 for(a=0; a<MAX_MTEX; a++) {
649 if(la->mtex[a] && la->mtex[a]->tex==tex) {
650 if(la->id.lib==0) {
651 la->mtex[a]->tex= texn;
652 texn->id.us++;
653 tex->id.us--;
657 la= la->id.next;
659 wrld= G.main->world.first;
660 while(wrld) {
661 for(a=0; a<MAX_MTEX; a++) {
662 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
663 if(wrld->id.lib==0) {
664 wrld->mtex[a]->tex= texn;
665 texn->id.us++;
666 tex->id.us--;
670 wrld= wrld->id.next;
672 br= G.main->brush.first;
673 while(br) {
674 for(a=0; a<MAX_MTEX; a++) {
675 if(br->mtex[a] && br->mtex[a]->tex==tex) {
676 if(br->id.lib==0) {
677 br->mtex[a]->tex= texn;
678 texn->id.us++;
679 tex->id.us--;
683 br= br->id.next;
688 /* ------------------------------------------------------------------------- */
690 void autotexname(Tex *tex)
692 /* extern char texstr[20][12]; *//* buttons.c, already in bad lev calls*/
693 Image *ima;
694 char di[FILE_MAXDIR], fi[FILE_MAXFILE];
696 if(tex) {
697 if(tex->type==TEX_IMAGE) {
698 ima= tex->ima;
699 if(ima) {
700 strcpy(di, ima->name);
701 BLI_splitdirstring(di, fi);
702 strcpy(di, "I.");
703 strcat(di, fi);
704 new_id(&G.main->tex, (ID *)tex, di);
706 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
708 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
709 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
713 /* ------------------------------------------------------------------------- */
715 Tex *give_current_texture(Object *ob, int act)
717 Material ***matarar, *ma;
718 Lamp *la = 0;
719 MTex *mtex = 0;
720 Tex *tex = 0;
721 bNode *node;
723 if(ob==0) return 0;
724 if(ob->totcol==0) return 0;
726 if(ob->type==OB_LAMP) {
727 la=(Lamp *)ob->data;
728 if(la) {
729 mtex= la->mtex[(int)(la->texact)];
730 if(mtex) tex= mtex->tex;
732 } else {
733 if(act>ob->totcol) act= ob->totcol;
734 else if(act==0) act= 1;
736 if( BTST(ob->colbits, act-1) ) { /* in object */
737 ma= ob->mat[act-1];
739 else { /* in data */
740 matarar= give_matarar(ob);
742 if(matarar && *matarar) ma= (*matarar)[act-1];
743 else ma= 0;
746 if(ma && ma->use_nodes && ma->nodetree) {
747 node= nodeGetActiveID(ma->nodetree, ID_TE);
749 if(node) {
750 tex= (Tex *)node->id;
751 ma= NULL;
753 else {
754 node= nodeGetActiveID(ma->nodetree, ID_MA);
755 if(node)
756 ma= (Material*)node->id;
759 if(ma) {
760 mtex= ma->mtex[(int)(ma->texact)];
761 if(mtex) tex= mtex->tex;
765 return tex;
769 /* ------------------------------------------------------------------------- */
771 EnvMap *BKE_add_envmap(void)
773 EnvMap *env;
775 env= MEM_callocN(sizeof(EnvMap), "envmap");
776 env->type= ENV_CUBE;
777 env->stype= ENV_STATIC;
778 env->clipsta= 0.1;
779 env->clipend= 100.0;
780 env->cuberes= 100;
782 return env;
785 /* ------------------------------------------------------------------------- */
787 EnvMap *BKE_copy_envmap(EnvMap *env)
789 EnvMap *envn;
790 int a;
792 envn= MEM_dupallocN(env);
793 envn->ok= 0;
794 for(a=0; a<6; a++) envn->cube[a]= NULL;
795 if(envn->ima) id_us_plus((ID *)envn->ima);
797 return envn;
800 /* ------------------------------------------------------------------------- */
802 void BKE_free_envmapdata(EnvMap *env)
804 unsigned int part;
806 for(part=0; part<6; part++) {
807 if(env->cube[part])
808 IMB_freeImBuf(env->cube[part]);
809 env->cube[part]= NULL;
811 env->ok= 0;
814 /* ------------------------------------------------------------------------- */
816 void BKE_free_envmap(EnvMap *env)
819 BKE_free_envmapdata(env);
820 MEM_freeN(env);
824 /* ------------------------------------------------------------------------- */