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
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 *****
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"
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"
71 #include "BKE_library.h"
72 #include "BKE_image.h"
73 #include "BKE_material.h"
74 #include "BKE_texture.h"
76 #include "BKE_icons.h"
78 #include "BKE_brush.h"
82 /* ------------------------------------------------------------------------- */
84 /* All support for plugin textures: */
85 int test_dlerr(const char *name
, const char *symbol
)
89 err
= PIL_dynlib_get_error_as_string(NULL
);
91 printf("var1: %s, var2: %s, var3: %s\n", name
, symbol
, err
);
98 /* ------------------------------------------------------------------------- */
100 void open_plugin_tex(PluginTex
*pit
)
102 int (*version
)(void);
104 /* init all the happy variables */
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;
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
;
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();
155 printf ("Plugin returned unrecognized version number\n");
162 /* ------------------------------------------------------------------------- */
164 /* very badlevel define to bypass linking with BIF_interface.h */
168 PluginTex
*add_plugin_tex(char *str
)
174 pit
= MEM_callocN(sizeof(PluginTex
), "plugintex");
176 strcpy(pit
->name
, str
);
177 open_plugin_tex(pit
);
180 if(pit
->handle
==0) error("no plugin: %s", str
);
181 else error("in plugin: %s", str
);
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
);
200 /* ------------------------------------------------------------------------- */
202 void free_plugin_tex(PluginTex
*pit
)
206 /* no PIL_dynlib_close: same plugin can be opened multiple times, 1 handle */
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
);
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
)
248 coba
= MEM_callocN( sizeof(ColorBand
), "colorband");
250 coba
->data
[0].pos
= 0.0;
251 coba
->data
[1].pos
= 1.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;
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;
289 /* ------------------------------------------------------------------------- */
291 int do_colorband(ColorBand
*coba
, float in
, float out
[4])
293 CBData
*cbd1
, *cbd2
, *cbd0
, *cbd3
;
294 float fac
, mfac
, t
[4];
297 if(coba
==NULL
|| coba
->tot
==0) return 0;
307 if(in
<= cbd1
->pos
&& coba
->ipotype
<2) {
316 /* we're looking for first pos > in */
317 for(a
=0; a
<coba
->tot
; a
++, cbd1
++) if(cbd1
->pos
> in
) break;
332 if(in
>= cbd1
->pos
&& coba
->ipotype
<2) {
340 if(cbd2
->pos
!=cbd1
->pos
)
341 fac
= (in
-cbd1
->pos
)/(cbd2
->pos
-cbd1
->pos
);
345 if(coba
->ipotype
>=2) {
346 /* ipo from right to left: 3 2 1 0 */
348 if(a
>=coba
->tot
-1) cbd0
= cbd1
;
353 CLAMP(fac
, 0.0f
, 1.0f
);
356 set_four_ipo(fac
, t
, KEY_CARDINAL
);
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);
371 if(coba
->ipotype
==1) { /* EASE */
373 fac
= 3.0f
*mfac
-2.0f
*mfac
*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
;
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
);
399 /* ------------------------------------------------------------------------- */
401 void default_tex(Tex
*tex
)
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;
418 tex
->noisesize
= 0.25;
421 tex
->nabla
= 0.025; // also in do_versions
424 tex
->filtersize
= 1.0;
428 /* newnoise: init. */
430 tex
->noisebasis2
= 0;
433 tex
->mg_lacunarity
= 2.0;
434 tex
->mg_octaves
= 2.0;
435 tex
->mg_offset
= 1.0;
437 tex
->ns_outscale
= 1.0;
439 tex
->dist_amount
= 1.0;
442 tex
->vn_w2
= tex
->vn_w3
= tex
->vn_w4
= 0.0;
448 tex
->env
->stype
=ENV_STATIC
;
449 tex
->env
->clipsta
=0.1;
450 tex
->env
->clipend
=100;
451 tex
->env
->cuberes
=100;
459 for(a
=0; a
<pit
->vars
; a
++, varstr
++) {
460 pit
->data
[a
] = varstr
->def
;
465 tex
->iuser
.fie_ima
= 2;
467 tex
->iuser
.frames
= 100;
470 /* ------------------------------------------------------------------------- */
472 Tex
*add_texture(char *name
)
476 tex
= alloc_libblock(&G
.main
->tex
, ID_TE
, name
);
483 /* ------------------------------------------------------------------------- */
485 void default_mtex(MTex
*mtex
)
487 mtex
->texco
= TEXCO_ORCO
;
488 mtex
->mapto
= MAP_COL
;
493 mtex
->mapping
= MTEX_FLAT
;
508 mtex
->blendtype
= MTEX_BLEND
;
516 /* ------------------------------------------------------------------------- */
522 mtex
= MEM_callocN(sizeof(MTex
), "add_mtex");
529 /* ------------------------------------------------------------------------- */
531 Tex
*copy_texture(Tex
*tex
)
535 texn
= copy_libblock(tex
);
536 if(texn
->type
==TEX_IMAGE
) id_us_plus((ID
*)texn
->ima
);
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
);
550 /* ------------------------------------------------------------------------- */
552 void make_local_texture(Tex
*tex
)
559 int a
, local
=0, lib
=0;
561 /* - only lib users: do nothing
562 * - only local users: set flag
566 if(tex
->id
.lib
==0) return;
568 /* special case: ima always local immediately */
571 tex
->ima
->id
.flag
= LIB_LOCAL
;
572 new_id(0, (ID
*)tex
->ima
, 0);
577 tex
->id
.flag
= LIB_LOCAL
;
578 new_id(0, (ID
*)tex
, 0);
583 ma
= G
.main
->mat
.first
;
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;
593 la
= G
.main
->lamp
.first
;
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;
603 wrld
= G
.main
->world
.first
;
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;
613 br
= G
.main
->brush
.first
;
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;
624 if(local
&& 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
);
633 ma
= G
.main
->mat
.first
;
635 for(a
=0; a
<MAX_MTEX
; a
++) {
636 if(ma
->mtex
[a
] && ma
->mtex
[a
]->tex
==tex
) {
638 ma
->mtex
[a
]->tex
= texn
;
646 la
= G
.main
->lamp
.first
;
648 for(a
=0; a
<MAX_MTEX
; a
++) {
649 if(la
->mtex
[a
] && la
->mtex
[a
]->tex
==tex
) {
651 la
->mtex
[a
]->tex
= texn
;
659 wrld
= G
.main
->world
.first
;
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
;
672 br
= G
.main
->brush
.first
;
674 for(a
=0; a
<MAX_MTEX
; a
++) {
675 if(br
->mtex
[a
] && br
->mtex
[a
]->tex
==tex
) {
677 br
->mtex
[a
]->tex
= texn
;
688 /* ------------------------------------------------------------------------- */
690 void autotexname(Tex
*tex
)
692 /* extern char texstr[20][12]; *//* buttons.c, already in bad lev calls*/
694 char di
[FILE_MAXDIR
], fi
[FILE_MAXFILE
];
697 if(tex
->type
==TEX_IMAGE
) {
700 strcpy(di
, ima
->name
);
701 BLI_splitdirstring(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
;
724 if(ob
->totcol
==0) return 0;
726 if(ob
->type
==OB_LAMP
) {
729 mtex
= la
->mtex
[(int)(la
->texact
)];
730 if(mtex
) tex
= mtex
->tex
;
733 if(act
>ob
->totcol
) act
= ob
->totcol
;
734 else if(act
==0) act
= 1;
736 if( BTST(ob
->colbits
, act
-1) ) { /* in object */
740 matarar
= give_matarar(ob
);
742 if(matarar
&& *matarar
) ma
= (*matarar
)[act
-1];
746 if(ma
&& ma
->use_nodes
&& ma
->nodetree
) {
747 node
= nodeGetActiveID(ma
->nodetree
, ID_TE
);
750 tex
= (Tex
*)node
->id
;
754 node
= nodeGetActiveID(ma
->nodetree
, ID_MA
);
756 ma
= (Material
*)node
->id
;
760 mtex
= ma
->mtex
[(int)(ma
->texact
)];
761 if(mtex
) tex
= mtex
->tex
;
769 /* ------------------------------------------------------------------------- */
771 EnvMap
*BKE_add_envmap(void)
775 env
= MEM_callocN(sizeof(EnvMap
), "envmap");
777 env
->stype
= ENV_STATIC
;
785 /* ------------------------------------------------------------------------- */
787 EnvMap
*BKE_copy_envmap(EnvMap
*env
)
792 envn
= MEM_dupallocN(env
);
794 for(a
=0; a
<6; a
++) envn
->cube
[a
]= NULL
;
795 if(envn
->ima
) id_us_plus((ID
*)envn
->ima
);
800 /* ------------------------------------------------------------------------- */
802 void BKE_free_envmapdata(EnvMap
*env
)
806 for(part
=0; part
<6; part
++) {
808 IMB_freeImBuf(env
->cube
[part
]);
809 env
->cube
[part
]= NULL
;
814 /* ------------------------------------------------------------------------- */
816 void BKE_free_envmap(EnvMap
*env
)
819 BKE_free_envmapdata(env
);
824 /* ------------------------------------------------------------------------- */