Initial revision
[blender.git] / source / blender / blenkernel / intern / texture.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <math.h>
37
38 #ifdef WIN32
39 #include "BLI_winstuff.h"
40 #endif
41
42 #include "MEM_guardedalloc.h"
43
44 #include "PIL_dynlib.h"
45
46 #include "MTC_matrixops.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "BLI_rand.h"
51
52 #include "DNA_texture_types.h"
53 #include "DNA_key_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_image_types.h"
59
60 #include "IMB_imbuf_types.h"
61 #include "IMB_imbuf.h"
62
63 #include "BKE_osa_types.h"
64 #include "BKE_plugin_types.h"
65
66 #include "BKE_bad_level_calls.h"
67 #include "BKE_utildefines.h"
68
69 #include "BKE_global.h"
70 #include "BKE_main.h"
71
72 #include "BKE_library.h"
73 #include "BKE_image.h"
74 #include "BKE_texture.h"
75 #include "BKE_key.h"
76 #include "BKE_ipo.h"
77
78
79 /* These vars form the texture channel */
80 float Tin, Tr, Tg, Tb, Ta, Txtra;
81 extern int Talpha;
82
83
84 /* ------------------------------------------------------------------------- */
85
86 /* Alle support voor 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
140                                         info_func(info);
141
142                                         pit->doit= (int(*)(void)) info->tex_doit;
143                                         pit->callback= (void(*)(unsigned short)) info->callback;
144                                         pit->stypes= info->stypes;
145                                         pit->vars= info->nvars;
146                                         pit->pname= info->name;
147                                         pit->stnames= info->snames;
148                                         pit->varstr= info->varstr;
149                                         pit->result= info->result;
150                                         pit->cfra= info->cfra;
151                                         if (info->init) info->init();
152                                 }
153                                 MEM_freeN(info);
154                         } else {
155                                 printf ("Plugin returned unrecognized version number\n");
156                                 return;
157                         }
158                 }
159         }
160 }
161
162 /* ------------------------------------------------------------------------- */
163
164 PluginTex *add_plugin_tex(char *str)
165 {
166         PluginTex *pit;
167         VarStruct *varstr;
168         int a;
169         
170         pit= MEM_callocN(sizeof(PluginTex), "plugintex");
171         
172         strcpy(pit->name, str);
173         open_plugin_tex(pit);
174         
175         if(pit->doit==0) {
176                 if(pit->handle==0) error("no plugin: %s", str);
177                 else error("in plugin: %s", str);
178                 MEM_freeN(pit);
179                 return NULL;
180         }
181         
182         varstr= pit->varstr;
183         for(a=0; a<pit->vars; a++, varstr++) {
184                 if( (varstr->type & FLO)==FLO)
185                         pit->data[a]= varstr->def;
186                 else if( (varstr->type & INT)==INT)
187                         *((int *)(pit->data+a))= (int) varstr->def;
188         }
189
190         return pit;
191 }
192
193 /* ------------------------------------------------------------------------- */
194
195 void free_plugin_tex(PluginTex *pit)
196 {
197         if(pit==0) return;
198                 
199         /* geen PIL_dynlib_close: dezelfde plugin kan meerdere keren geopend zijn: 1 handle */
200         MEM_freeN(pit); 
201 }
202
203 /* ****************** COLORBAND ******************* */
204
205 ColorBand *add_colorband()
206 {
207         ColorBand *coba;
208         int a;
209         
210         coba= MEM_callocN( sizeof(ColorBand), "colorband");
211         
212         coba->data[0].r= 0.0;
213         coba->data[0].g= 0.0;
214         coba->data[0].b= 0.0;
215         coba->data[0].a= 0.0;
216         coba->data[0].pos= 0.0;
217
218         coba->data[1].r= 0.0;
219         coba->data[1].g= 1.0;
220         coba->data[1].b= 1.0;
221         coba->data[1].a= 1.0;
222         coba->data[1].pos= 1.0;
223         
224         for(a=2; a<MAXCOLORBAND; a++) {
225                 coba->data[a].r= 0.5;
226                 coba->data[a].g= 0.5;
227                 coba->data[a].b= 0.5;
228                 coba->data[a].a= 1.0;
229                 coba->data[a].pos= 0.5;
230         }
231         
232         coba->tot= 2;
233         
234         return coba;
235 }
236
237 /* ------------------------------------------------------------------------- */
238
239 int do_colorband(ColorBand *coba)
240 {
241         CBData *cbd1, *cbd2, *cbd0, *cbd3;
242         float fac, mfac, t[4];
243         int a;
244         
245         if(coba->tot==0) return 0;
246         Talpha= 1;
247         
248         cbd1= coba->data;
249         
250         if(Tin <= cbd1->pos) {  /* helemaal links */
251                 Tr= cbd1->r;
252                 Tg= cbd1->g;
253                 Tb= cbd1->b;
254                 Ta= cbd1->a;
255         }
256         else {
257                 /* we zoeken de eerste pos > Tin */
258         
259                 for(a=0; a<coba->tot; a++, cbd1++) if(cbd1->pos >= Tin) break;
260                         
261                 if(a==coba->tot) {      /* helemaal rechts */
262                         cbd1--;
263                         Tr= cbd1->r;
264                         Tg= cbd1->g;
265                         Tb= cbd1->b;
266                         Ta= cbd1->a;
267                 }
268                 else {
269                         cbd2= cbd1-1;
270                         fac= (Tin-cbd1->pos)/(cbd2->pos-cbd1->pos);
271                         
272                         if(coba->ipotype==2) {
273                                 /* ipo van r naar l: 3 2 1 0 */
274                                 
275                                 if(a>=coba->tot-1) cbd0= cbd1;
276                                 else cbd0= cbd1+1;
277                                 if(a<2) cbd3= cbd2;
278                                 else cbd3= cbd2-1;
279                                 
280                                 set_four_ipo(fac, t, KEY_BSPLINE);
281
282                                 Tr= t[3]*cbd3->r +t[2]*cbd2->r +t[1]*cbd1->r +t[0]*cbd0->r;
283                                 Tg= t[3]*cbd3->g +t[2]*cbd2->g +t[1]*cbd1->g +t[0]*cbd0->g;
284                                 Tb= t[3]*cbd3->b +t[2]*cbd2->b +t[1]*cbd1->b +t[0]*cbd0->b;
285                                 Ta= t[3]*cbd3->a +t[2]*cbd2->a +t[1]*cbd1->a +t[0]*cbd0->a;
286                                 CLAMP(Tr, 0.0, 1.0);
287                                 CLAMP(Tg, 0.0, 1.0);
288                                 CLAMP(Tb, 0.0, 1.0);
289                                 CLAMP(Ta, 0.0, 1.0);
290                         }
291                         else {
292                         
293                                 if(coba->ipotype==1) {  /* EASE */
294                                         mfac= fac*fac;
295                                         fac= 3.0f*mfac-2.0f*mfac*fac;
296                                 }
297                                 mfac= 1.0f-fac;
298                                 
299                                 Tr= mfac*cbd1->r + fac*cbd2->r;
300                                 Tg= mfac*cbd1->g + fac*cbd2->g;
301                                 Tb= mfac*cbd1->b + fac*cbd2->b;
302                                 Ta= mfac*cbd1->a + fac*cbd2->a;
303                         }
304                 }
305         }
306         return 1;       /* OK */
307 }
308
309 /* ******************* TEX ************************ */
310
311 void free_texture(Tex *tex)
312 {
313         free_plugin_tex(tex->plugin);
314         if(tex->coba) MEM_freeN(tex->coba);
315         if(tex->env) RE_free_envmap(tex->env);
316 }
317
318 /* ------------------------------------------------------------------------- */
319
320 void default_tex(Tex *tex)
321 {
322         tex->stype= 0;
323         tex->imaflag= TEX_INTERPOL+TEX_MIPMAP;
324         tex->extend= TEX_REPEAT;
325         tex->cropxmin= tex->cropymin= 0.0;
326         tex->cropxmax= tex->cropymax= 1.0;
327         tex->xrepeat= tex->yrepeat= 1;
328         tex->fie_ima= 2;
329         tex->sfra= 1;
330         tex->frames= 0;
331         tex->offset= 0;
332         tex->noisesize= 0.25;
333         tex->noisedepth= 2;
334         tex->turbul= 5.0;
335         tex->bright= 1.0;
336         tex->contrast= tex->filtersize= 1.0;
337         tex->rfac= 1.0;
338         tex->gfac= 1.0;
339         tex->bfac= 1.0;
340
341 }
342
343 /* ------------------------------------------------------------------------- */
344
345 Tex *add_texture(char *name)
346 {
347         Tex *tex;
348
349         tex= alloc_libblock(&G.main->tex, ID_TE, name);
350         
351         default_tex(tex);
352         
353         return tex;
354 }
355
356 /* ------------------------------------------------------------------------- */
357
358 void default_mtex(MTex *mtex)
359 {
360         mtex->texco= TEXCO_ORCO;
361         mtex->mapto= MAP_COL;
362         mtex->object= 0;
363         mtex->projx= PROJ_X;
364         mtex->projy= PROJ_Y;
365         mtex->projz= PROJ_Z;
366         mtex->mapping= MTEX_FLAT;
367         mtex->ofs[0]= 0.0;
368         mtex->ofs[1]= 0.0;
369         mtex->ofs[2]= 0.0;
370         mtex->size[0]= 1.0;
371         mtex->size[1]= 1.0;
372         mtex->size[2]= 1.0;
373         mtex->tex= 0;
374         mtex->texflag= 0;
375         mtex->colormodel= 0;
376         mtex->r= 1.0;
377         mtex->g= 0.0;
378         mtex->b= 1.0;
379         mtex->k= 1.0;
380         mtex->def_var= 1.0;
381         mtex->blendtype= MTEX_BLEND;
382         mtex->colfac= 1.0;
383         mtex->norfac= 0.5;
384         mtex->varfac= 1.0;
385 }
386
387
388 /* ------------------------------------------------------------------------- */
389
390 MTex *add_mtex()
391 {
392         MTex *mtex;
393         
394         mtex= MEM_callocN(sizeof(MTex), "add_mtex");
395         
396         default_mtex(mtex);
397         
398         return mtex;
399 }
400
401 /* ------------------------------------------------------------------------- */
402
403 Tex *copy_texture(Tex *tex)
404 {
405         Tex *texn;
406         
407         texn= copy_libblock(tex);
408         if(texn->type==TEX_IMAGE) id_us_plus((ID *)texn->ima);
409         else texn->ima= 0;
410         
411         if(texn->plugin) {
412                 texn->plugin= MEM_dupallocN(texn->plugin);
413                 open_plugin_tex(texn->plugin);
414         }
415         
416         if(texn->coba) texn->coba= MEM_dupallocN(texn->coba);
417         if(texn->env) texn->env= RE_copy_envmap(texn->env);
418         
419         return texn;
420 }
421
422 /* ------------------------------------------------------------------------- */
423
424 void make_local_texture(Tex *tex)
425 {
426         Tex *texn;
427         Material *ma;
428         World *wrld;
429         Lamp *la;
430         int a, local=0, lib=0;
431         
432         /* - zijn er alleen lib users: niet doen
433          * - zijn er alleen locale users: flag zetten
434          * - mixed: copy
435          */
436         
437         if(tex->id.lib==0) return;
438
439         /* speciaal geval: ima altijd meteen lokaal */
440         if(tex->ima) {
441                 tex->ima->id.lib= 0;
442                 tex->ima->id.flag= LIB_LOCAL;
443                 new_id(0, (ID *)tex->ima, 0);
444         }
445
446         if(tex->id.us==1) {
447                 tex->id.lib= 0;
448                 tex->id.flag= LIB_LOCAL;
449                 new_id(0, (ID *)tex, 0);
450
451                 return;
452         }
453         
454         ma= G.main->mat.first;
455         while(ma) {
456                 for(a=0; a<8; a++) {
457                         if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
458                                 if(ma->id.lib) lib= 1;
459                                 else local= 1;
460                         }
461                 }
462                 ma= ma->id.next;
463         }
464         la= G.main->lamp.first;
465         while(la) {
466                 for(a=0; a<8; a++) {
467                         if(la->mtex[a] && la->mtex[a]->tex==tex) {
468                                 if(la->id.lib) lib= 1;
469                                 else local= 1;
470                         }
471                 }
472                 la= la->id.next;
473         }
474         wrld= G.main->world.first;
475         while(wrld) {
476                 for(a=0; a<8; a++) {
477                         if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
478                                 if(wrld->id.lib) lib= 1;
479                                 else local= 1;
480                         }
481                 }
482                 wrld= wrld->id.next;
483         }
484         
485         if(local && lib==0) {
486                 tex->id.lib= 0;
487                 tex->id.flag= LIB_LOCAL;
488                 new_id(0, (ID *)tex, 0);
489         }
490         else if(local && lib) {
491                 texn= copy_texture(tex);
492                 texn->id.us= 0;
493                 
494                 ma= G.main->mat.first;
495                 while(ma) {
496                         for(a=0; a<8; a++) {
497                                 if(ma->mtex[a] && ma->mtex[a]->tex==tex) {
498                                         if(ma->id.lib==0) {
499                                                 ma->mtex[a]->tex= texn;
500                                                 texn->id.us++;
501                                                 tex->id.us--;
502                                         }
503                                 }
504                         }
505                         ma= ma->id.next;
506                 }
507                 la= G.main->lamp.first;
508                 while(la) {
509                         for(a=0; a<8; a++) {
510                                 if(la->mtex[a] && la->mtex[a]->tex==tex) {
511                                         if(la->id.lib==0) {
512                                                 la->mtex[a]->tex= texn;
513                                                 texn->id.us++;
514                                                 tex->id.us--;
515                                         }
516                                 }
517                         }
518                         la= la->id.next;
519                 }
520                 wrld= G.main->world.first;
521                 while(wrld) {
522                         for(a=0; a<8; a++) {
523                                 if(wrld->mtex[a] && wrld->mtex[a]->tex==tex) {
524                                         if(wrld->id.lib==0) {
525                                                 wrld->mtex[a]->tex= texn;
526                                                 texn->id.us++;
527                                                 tex->id.us--;
528                                         }
529                                 }
530                         }
531                         wrld= wrld->id.next;
532                 }
533
534         }
535 }
536
537 /* ------------------------------------------------------------------------- */
538
539 void autotexname(Tex *tex)
540 {
541 /*      extern char texstr[15][8];       *//* buttons.c, already in bad lev calls*/
542         Image *ima;
543         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
544         
545         if(tex) {
546                 if(tex->type==TEX_IMAGE) {
547                         ima= tex->ima;
548                         if(ima) {
549                                 strcpy(di, ima->name);
550                                 BLI_splitdirstring(di, fi);
551                                 strcpy(di, "I.");
552                                 strcat(di, fi);
553                                 new_id(&G.main->tex, (ID *)tex, di);
554                         }
555                         else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
556                 }
557                 else if(tex->type==TEX_PLUGIN && tex->plugin) new_id(&G.main->tex, (ID *)tex, tex->plugin->pname);
558                 else new_id(&G.main->tex, (ID *)tex, texstr[tex->type]);
559         }
560 }
561
562 /* ------------------------------------------------------------------------- */
563
564 void init_render_texture(Tex *tex)
565 {
566         Image *ima;
567         int imanr;
568         unsigned short numlen;
569         char name[256], head[FILE_MAXFILE], tail[FILE_MAXFILE];
570
571         /* is also used as signal */
572         tex->nor= 0;
573
574         /* imap testen */
575         if(tex->frames && tex->ima && tex->ima->name) { /* frames */
576                 strcpy(name, tex->ima->name);
577                 
578                 imanr= calcimanr(G.scene->r.cfra, tex);
579                 
580                 if(tex->imaflag & TEX_ANIM5) {
581                         if(tex->ima->lastframe != imanr) {
582                                 if(tex->ima->ibuf) IMB_freeImBuf(tex->ima->ibuf);
583                                 tex->ima->ibuf= 0;
584                                 tex->ima->lastframe= imanr;
585                         }
586                 }
587                 else {
588                                 /* voor patch field-ima rendering */
589                         tex->ima->lastframe= imanr;
590                         
591                         BLI_stringdec(name, head, tail, &numlen);
592                         BLI_stringenc(name, head, tail, numlen, imanr);
593         
594                         ima= add_image(name);
595
596                         if(ima) {
597                                 ima->flag |= IMA_FROMANIM;
598                                 
599                                 if(tex->ima) tex->ima->id.us--;
600                                 tex->ima= ima;
601                                 
602                                 ima->ok= 1;
603                         }
604                 }
605         }
606         if(tex->imaflag & (TEX_ANTIALI+TEX_ANTISCALE)) {
607                 if(tex->ima && tex->ima->lastquality<R.osa) {
608                         if(tex->ima->ibuf) IMB_freeImBuf(tex->ima->ibuf);
609                         tex->ima->ibuf= 0;
610                 }
611         }
612         
613         if(tex->type==TEX_PLUGIN) {
614                 if(tex->plugin && tex->plugin->doit) {
615                                 if(tex->plugin->cfra) {
616                                         *(tex->plugin->cfra)= frame_to_float(G.scene->r.cfra); 
617                                 }
618                 }
619         }
620         else if(tex->type==TEX_ENVMAP) {
621                 /* just in case */
622                 tex->imaflag= TEX_INTERPOL | TEX_MIPMAP;
623                 tex->extend= TEX_CLIP;
624                 
625                 if(tex->env) {
626                         /* temporal solution: layer 21 is to indicate an anvmap object */
627                         tex->env->notlay |= (1<<20);
628                         if(tex->env->object) tex->env->object->lay |= (1<<20);
629                         
630                         if(R.flag & R_RENDERING) {
631                                 if(tex->env->stype==ENV_ANIM) RE_free_envmapdata(tex->env);
632                         }
633                 }
634         }
635 }
636
637 /* ------------------------------------------------------------------------- */
638
639 void init_render_textures()
640 {
641         Tex *tex;
642         
643         tex= G.main->tex.first;
644         while(tex) {
645                 if(tex->id.us) init_render_texture(tex);
646                 tex= tex->id.next;
647         }
648         
649         free_unused_animimages();
650 }
651
652 /* ------------------------------------------------------------------------- */
653
654 void end_render_texture(Tex *tex)
655 {
656         
657
658 }
659
660 /* ------------------------------------------------------------------------- */
661
662 void end_render_textures()
663 {
664         Tex *tex;
665         
666         tex= G.main->tex.first;
667         while(tex) {
668                 if(tex->id.us) end_render_texture(tex);
669                 tex= tex->id.next;
670         }
671         
672 }
673
674
675 /* ************************** */
676
677 int clouds(Tex *tex, float *texvec)
678 {
679         float (*turbfunc)(float, float, float, float, int);
680
681         if(tex->noisetype==TEX_NOISESOFT) turbfunc= BLI_turbulence;
682         else turbfunc= BLI_turbulence1;
683         
684         Tin= turbfunc(tex->noisesize, texvec[0], texvec[1], texvec[2], tex->noisedepth);
685
686         if(tex->stype==1) {
687
688                 Tr= Tin;
689                 Tg= turbfunc(tex->noisesize, texvec[1], texvec[0], texvec[2], tex->noisedepth);
690
691                 Tb= turbfunc(tex->noisesize,texvec[1],texvec[2],texvec[0], tex->noisedepth);
692                 
693                 BRICONRGB;
694                 Ta= 1.0;
695                 
696                 return 1;
697         }
698         
699         BRICON;
700
701         if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
702                 
703         return 0;
704 }
705
706 /* ------------------------------------------------------------------------- */
707
708 int blend(Tex *tex, float *texvec)
709 {
710         float x, y, t;
711         
712         if(tex->flag & TEX_FLIPBLEND) {
713                 x= texvec[1];
714                 y= texvec[0];
715         }
716         else {
717                 x= texvec[0];
718                 y= texvec[1];
719         }
720         
721         if(tex->stype==0) {     /* lin */
722                 Tin= (1.0+x)/2.0;
723         }
724         else if(tex->stype==1) {        /* quad */
725                 Tin= (1.0+x)/2.0;
726                 if(Tin<0.0) Tin= 0.0;
727                 else Tin*= Tin;
728         }
729         else if(tex->stype==2) {        /* ease */
730                 Tin= (1.0+x)/2.0;
731                 if(Tin<=.0) Tin= 0.0;
732                 else if(Tin>=1.0) Tin= 1.0;
733                 else {
734                         t= Tin*Tin;
735                         Tin= (3.0*t-2.0*t*Tin);
736                 }
737         }
738         else if(tex->stype==3) { /* diag */
739                 Tin= (2.0+x+y)/4.0;
740         }
741         else {  /* sphere */
742                 Tin= 1.0-sqrt(x*x+      y*y+texvec[2]*texvec[2]);
743                 if(Tin<0.0) Tin= 0.0;
744                 if(tex->stype==5) Tin*= Tin;  /* halo */
745         }
746         
747         BRICON;
748         if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
749
750         return 0;
751 }
752
753 /* ------------------------------------------------------------------------- */
754
755 int wood(Tex *tex, float *texvec)
756 {
757         float (*noisefunc)(float, float, float, float);
758
759         if(tex->noisetype==TEX_NOISESOFT) noisefunc= BLI_hnoise;
760         else noisefunc= BLI_hnoisep;
761
762         
763         if(tex->stype==0) {
764                 Tin= 0.5+0.5*sin( (texvec[0]+texvec[1]+texvec[2])*10.0 );
765         }
766         else if(tex->stype==1) {
767                 Tin= 0.5+0.5*sin( sqrt(texvec[0]*texvec[0]+texvec[1]*texvec[1]+texvec[2]*texvec[2])*20.0 );
768         }
769         else if(tex->stype==2) {
770                 Tin= noisefunc(tex->noisesize, texvec[0], texvec[1], texvec[2]);
771                 Tin= 0.5+ 0.5*sin(tex->turbul*Tin+(texvec[0]+texvec[1]+texvec[2])*10.0);
772         }
773         else if(tex->stype==3) {
774                 Tin= noisefunc(tex->noisesize, texvec[0], texvec[1], texvec[2]);
775                 Tin= 0.5+ 0.5*sin(tex->turbul*Tin+(sqrt(texvec[0]*texvec[0]+texvec[1]*texvec[1]+texvec[2]*texvec[2]))*20.0);
776         }
777         
778         
779         BRICON;
780         if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
781         
782         return 0;
783 }
784
785 /* ------------------------------------------------------------------------- */
786
787 int marble(Tex *tex, float *texvec)
788 {
789         float n;
790         float (*turbfunc)(float, float, float, float, int);
791
792         if(tex->noisetype==TEX_NOISESOFT) turbfunc= BLI_turbulence;
793         else turbfunc= BLI_turbulence1;
794         
795         n= 5.0*(texvec[0]+texvec[1]+texvec[2]);
796
797         Tin = 0.5+0.5*sin(n+tex->turbul*turbfunc(tex->noisesize, texvec[0],texvec[1],texvec[2], tex->noisedepth));
798
799         switch (tex->stype) {
800         case 1:
801                 Tin= sqrt(Tin);
802                 break;
803         case 2:
804                 Tin= sqrt(Tin);
805                 Tin= sqrt(Tin);
806                 break;
807         }
808         
809         BRICON;
810         if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
811         
812         return 0;
813 }
814
815 /* ------------------------------------------------------------------------- */
816
817 int magic(Tex *tex, float *texvec)
818 {
819         float x, y, z, turb=1.0;
820         int n;
821
822         n= tex->noisedepth;
823         turb= tex->turbul/5.0;
824         
825         x=  sin( ( texvec[0]+texvec[1]+texvec[2])*5.0 );
826         y=  cos( (-texvec[0]+texvec[1]-texvec[2])*5.0 );
827         z= -cos( (-texvec[0]-texvec[1]+texvec[2])*5.0 );
828         if(n>0) {
829                 x*= turb; 
830                 y*= turb; 
831                 z*= turb;
832                 y= -cos(x-y+z);
833                 y*= turb;
834                 if(n>1) {
835                         x= cos(x-y-z);
836                         x*= turb;
837                         if(n>2) {
838                                 z= sin(-x-y-z);
839                                 z*= turb;
840                                 if(n>3) {
841                                         x= -cos(-x+y-z);
842                                         x*= turb;
843                                         if(n>4) {
844                                                 y= -sin(-x+y+z);
845                                                 y*= turb;
846                                                 if(n>5) {
847                                                         y= -cos(-x+y+z);
848                                                         y*= turb;
849                                                         if(n>6) {
850                                                                 x= cos(x+y+z);
851                                                                 x*= turb;
852                                                                 if(n>7) {
853                                                                         z= sin(x+y-z);
854                                                                         z*= turb;
855                                                                         if(n>8) {
856                                                                                 x= -cos(-x-y+z);
857                                                                                 x*= turb;
858                                                                                 if(n>9) {
859                                                                                         y= -sin(x-y+z);
860                                                                                         y*= turb;
861                                                                                 }
862                                                                         }
863                                                                 }
864                                                         }
865                                                 }
866                                         }
867                                 }
868                         }
869                 }
870         }
871
872         if(turb!=0.0) {
873                 turb*= 2.0;
874                 x/= turb; 
875                 y/= turb; 
876                 z/= turb;
877         }
878         Tr= 0.5-x;
879         Tg= 0.5-y;
880         Tb= 0.5-z;
881
882         BRICONRGB;
883         Ta= 1.0;
884         
885         return 1;
886 }
887
888 /* ------------------------------------------------------------------------- */
889
890 int stucci(Tex *tex, float *texvec)
891 {
892         float b2, vec[3];
893         float ofs;
894         float (*noisefunc)(float, float, float, float);
895
896         if(tex->nor == NULL) return 0;
897
898         if(tex->noisetype==TEX_NOISESOFT) noisefunc= BLI_hnoise;
899         else noisefunc= BLI_hnoisep;
900
901         ofs= tex->turbul/200.0;
902         
903         b2= noisefunc(tex->noisesize, texvec[0], texvec[1], texvec[2]);
904         if(tex->stype) ofs*=(b2*b2);
905         vec[0]= b2-noisefunc(tex->noisesize, texvec[0]+ofs, texvec[1], texvec[2]);
906         vec[1]= b2-noisefunc(tex->noisesize, texvec[0], texvec[1]+ofs, texvec[2]);
907         vec[2]= b2-noisefunc(tex->noisesize, texvec[0], texvec[1], texvec[2]+ofs);
908
909         if(tex->stype==1) {
910                 tex->nor[0]= vec[0];
911                 tex->nor[1]= vec[1];
912                 tex->nor[2]= vec[2];
913         }
914         else {
915                 tex->nor[0]= -vec[0];
916                 tex->nor[1]= -vec[1];
917                 tex->nor[2]= -vec[2];
918         }
919         
920         return 2;
921 }
922
923 /* ------------------------------------------------------------------------- */
924
925 int texnoise(Tex *tex)
926 {
927         float div=3.0;
928         int val, ran, loop;
929         
930         ran= BLI_rand();
931         val= (ran & 3);
932         
933         loop= tex->noisedepth;
934         while(loop--) {
935                 ran= (ran>>2);
936                 val*= (ran & 3);
937                 div*= 3.0;
938         }
939         
940         Tin= ((float)val)/div;;
941
942         BRICON;
943         if(tex->flag & TEX_COLORBAND)  return do_colorband(tex->coba);
944         
945         return 0;
946 }
947
948 /* ------------------------------------------------------------------------- */
949
950 int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt)
951 {
952         PluginTex *pit;
953         int rgbnor=0;
954         
955         Tin= 0.0;
956
957         pit= tex->plugin;
958         if(pit && pit->doit) {
959                 VECCOPY(pit->result+5, R.vn); 
960
961                 if(R.osatex) rgbnor= ((TexDoit)pit->doit)(tex->stype, pit->data, texvec, dxt, dyt);
962                 else rgbnor= ((TexDoit)pit->doit)(tex->stype, pit->data, texvec, 0, 0);
963
964                 Tin= pit->result[0];
965
966                 if(rgbnor & TEX_NOR) {
967                         if(tex->nor) {
968                                 VECCOPY(tex->nor, pit->result+5);
969                         }
970                 }
971                 
972                 if(rgbnor & TEX_RGB) {
973                         Tr= pit->result[1];
974                         Tg= pit->result[2];
975                         Tb= pit->result[3];
976                         Ta= pit->result[4];
977
978                         BRICONRGB;
979                 }
980                 
981                 BRICON;
982                 if(tex->flag & TEX_COLORBAND)  rgbnor |= do_colorband(tex->coba);
983         }
984
985         return rgbnor;
986 }
987
988 /* *************** PROJEKTIES ******************* */
989
990 void tubemap(float x, float y, float z, float *adr1, float *adr2)
991 {
992         float len;
993
994         *adr2 = (z + 1.0) / 2.0;
995
996         len= sqrt(x*x+y*y);
997         if(len>0) {
998                 *adr1 = (1.0 - (atan2(x/len,y/len) / M_PI)) / 2.0;
999         }
1000 }
1001
1002 /* ------------------------------------------------------------------------- */
1003
1004 void spheremap(float x, float y, float z, float *adr1, float *adr2)
1005 {
1006         float len;
1007
1008         len= sqrt(x*x+y*y+z*z);
1009         if(len>0.0) {
1010                 
1011                 if(x==0.0 && y==0.0) *adr1= 0.0;        /* anders domain error */
1012                 else *adr1 = (1.0 - atan2(x,y)/M_PI )/2.0;
1013                 
1014                 z/=len;
1015                 *adr2 = 1.0- saacos(z)/M_PI;
1016         }
1017 }
1018
1019 /* ------------------------------------------------------------------------- */
1020
1021 static int cubemap_glob(MTex *mtex, float x, float y, float z, float *adr1, float *adr2)
1022 {
1023         float x1, y1, z1, nor[3];
1024         int ret;
1025         
1026         VECCOPY(nor, R.vn);
1027         MTC_Mat4Mul3Vecfl(R.viewinv, nor);
1028         
1029         x1= fabs(nor[0]);
1030         y1= fabs(nor[1]);
1031         z1= fabs(nor[2]);
1032         
1033         if(z1>=x1 && z1>=y1) {
1034                 *adr1 = (x + 1.0) / 2.0;
1035                 *adr2 = (y + 1.0) / 2.0;
1036                 ret= 0;
1037         }
1038         else if(y1>=x1 && y1>=z1) {
1039                 *adr1 = (x + 1.0) / 2.0;
1040                 *adr2 = (z + 1.0) / 2.0;
1041                 ret= 1;
1042         }
1043         else {
1044                 *adr1 = (y + 1.0) / 2.0;
1045                 *adr2 = (z + 1.0) / 2.0;
1046                 ret= 2;         
1047         }
1048         return ret;
1049 }
1050
1051 /* ------------------------------------------------------------------------- */
1052
1053 static int cubemap(MTex *mtex, float x, float y, float z, float *adr1, float *adr2)
1054 {
1055         int proj[4], ret= 0;
1056         
1057         if(R.vlr && R.vlr->mface) {
1058                 int index;
1059                 /* the mtex->proj{xyz} have type char. maybe this should be wider? */
1060                 /* casting to int ensures that the index type is right.            */
1061                 index = (int) mtex->projx;
1062                 proj[index]= ME_PROJXY;
1063
1064                 index = (int) mtex->projy;
1065                 proj[index]= ME_PROJXZ;
1066
1067                 index = (int) mtex->projz;
1068                 proj[index]= ME_PROJYZ;
1069                 
1070                 if(R.vlr->mface->puno & proj[1]) {
1071                         *adr1 = (x + 1.0) / 2.0;
1072                         *adr2 = (y + 1.0) / 2.0;        
1073                 }
1074                 else if(R.vlr->mface->puno & proj[2]) {
1075                         *adr1 = (x + 1.0) / 2.0;
1076                         *adr2 = (z + 1.0) / 2.0;
1077                         ret= 1;
1078                 }
1079                 else {
1080                         *adr1 = (y + 1.0) / 2.0;
1081                         *adr2 = (z + 1.0) / 2.0;
1082                         ret= 2;
1083                 }               
1084         } else
1085                 return cubemap_glob(mtex, x, y, z, adr1, adr2);
1086
1087         return ret;
1088 }
1089
1090 /* ------------------------------------------------------------------------- */
1091
1092 static int cubemap_ob(MTex *mtex, float x, float y, float z, float *adr1, float *adr2)
1093 {
1094         float x1, y1, z1, nor[3];
1095         int ret;
1096         
1097         VECCOPY(nor, R.vn);
1098         if(mtex->object) MTC_Mat4Mul3Vecfl(mtex->object->imat, nor);
1099         
1100         x1= fabs(nor[0]);
1101         y1= fabs(nor[1]);
1102         z1= fabs(nor[2]);
1103         
1104         if(z1>=x1 && z1>=y1) {
1105                 *adr1 = (x + 1.0) / 2.0;
1106                 *adr2 = (y + 1.0) / 2.0;
1107                 ret= 0;
1108         }
1109         else if(y1>=x1 && y1>=z1) {
1110                 *adr1 = (x + 1.0) / 2.0;
1111                 *adr2 = (z + 1.0) / 2.0;
1112                 ret= 1;
1113         }
1114         else {
1115                 *adr1 = (y + 1.0) / 2.0;
1116                 *adr2 = (z + 1.0) / 2.0;
1117                 ret= 2;         
1118         }
1119         return ret;
1120 }
1121
1122 /* ------------------------------------------------------------------------- */
1123
1124 void do_2d_mapping(MTex *mtex, float *t, float *dxt, float *dyt)
1125 {
1126         Tex *tex;
1127         float fx, fy, fac1, area[8];
1128         int ok, proj, areaflag= 0, wrap;
1129         
1130         wrap= mtex->mapping;
1131         tex= mtex->tex;
1132
1133         if(R.osa==0) {
1134                 
1135                 if(wrap==MTEX_FLAT) {
1136                         fx = (t[0] + 1.0) / 2.0;
1137                         fy = (t[1] + 1.0) / 2.0;
1138                 }
1139                 else if(wrap==MTEX_TUBE) tubemap(t[0], t[1], t[2], &fx, &fy);
1140                 else if(wrap==MTEX_SPHERE) spheremap(t[0], t[1], t[2], &fx, &fy);
1141                 else {
1142                         if(mtex->texco==TEXCO_OBJECT) cubemap_ob(mtex, t[0], t[1], t[2], &fx, &fy);
1143                         else if(mtex->texco==TEXCO_GLOB) cubemap_glob(mtex, t[0], t[1], t[2], &fx, &fy);
1144                         else cubemap(mtex, t[0], t[1], t[2], &fx, &fy);
1145                 }
1146                 
1147                 /* repeat */
1148                 if(tex->xrepeat>1) {
1149                         fx *= tex->xrepeat;
1150                         if(fx>1.0) fx -= (int)(fx);
1151                         else if(fx<0.0) fx+= 1-(int)(fx);
1152                 }
1153                 if(tex->yrepeat>1) {
1154                         fy *= tex->yrepeat;
1155                         if(fy>1.0) fy -= (int)(fy);
1156                         else if(fy<0.0) fy+= 1-(int)(fy);
1157                 }
1158                 
1159                 /* crop */
1160                 if(tex->cropxmin!=0.0 || tex->cropxmax!=1.0) {
1161                         fac1= tex->cropxmax - tex->cropxmin;
1162                         fx= tex->cropxmin+ fx*fac1;
1163                 }
1164                 if(tex->cropymin!=0.0 || tex->cropymax!=1.0) {
1165                         fac1= tex->cropymax - tex->cropymin;
1166                         fy= tex->cropymin+ fy*fac1;
1167                 }
1168
1169                 t[0]= fx;
1170                 t[1]= fy;
1171         }
1172         else {
1173                 
1174                 if(wrap==MTEX_FLAT) {
1175                         fx= (t[0] + 1.0) / 2.0;
1176                         fy= (t[1] + 1.0) / 2.0;
1177                         dxt[0]/= 2.0; 
1178                         dxt[1]/= 2.0;
1179                         dyt[0]/= 2.0; 
1180                         dyt[1]/= 2.0;
1181                 }
1182                 else if ELEM(wrap, MTEX_TUBE, MTEX_SPHERE) {
1183                         /* uitzondering: de naad achter (y<0.0) */
1184                         ok= 1;
1185                         if(t[1]<=0.0) {
1186                                 fx= t[0]+dxt[0];
1187                                 fy= t[0]+dyt[0];
1188                                 if(fx>=0.0 && fy>=0.0 && t[0]>=0.0);
1189                                 else if(fx<=0.0 && fy<=0.0 && t[0]<=0.0);
1190                                 else ok= 0;
1191                         }
1192                         if(ok) {
1193                                 if(wrap==MTEX_TUBE) {
1194                                         tubemap(t[0], t[1], t[2], area, area+1);
1195                                         tubemap(t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2], area+2, area+3);
1196                                         tubemap(t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2], area+4, area+5);
1197                                 }
1198                                 else { 
1199                                         spheremap(t[0], t[1], t[2],area,area+1);
1200                                         spheremap(t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2], area+2, area+3);
1201                                         spheremap(t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2], area+4, area+5);
1202                                 }
1203                                 areaflag= 1;
1204                         }
1205                         else {
1206                                 if(wrap==MTEX_TUBE) tubemap(t[0], t[1], t[2], &fx, &fy);
1207                                 else spheremap(t[0], t[1], t[2], &fx, &fy);
1208                                 dxt[0]/= 2.0; 
1209                                 dxt[1]/= 2.0;
1210                                 dyt[0]/= 2.0; 
1211                                 dyt[1]/= 2.0;
1212                         }
1213                 }
1214                 else {
1215
1216                         if(mtex->texco==TEXCO_OBJECT) proj = cubemap_ob(mtex, t[0], t[1], t[2], &fx, &fy);
1217                         else if (mtex->texco==TEXCO_GLOB) proj = cubemap_glob(mtex, t[0], t[1], t[2], &fx, &fy);
1218                         else proj = cubemap(mtex, t[0], t[1], t[2], &fx, &fy);
1219
1220                         if(proj==1) {
1221                                 dxt[1]= dxt[2];
1222                                 dyt[1]= dyt[2];
1223                         }
1224                         else if(proj==2) {
1225                                 dxt[0]= dxt[1];
1226                                 dyt[0]= dyt[1];
1227                                 dxt[1]= dxt[2];
1228                                 dyt[1]= dyt[2];
1229                         }
1230                         dxt[0]/= 2.0; 
1231                         dxt[1]/= 2.0;
1232                         dyt[0]/= 2.0; 
1233                         dyt[1]/= 2.0;
1234                 }
1235                 
1236                 /* als area dan dxt[] en dyt[] opnieuw berekenen */
1237                 if(areaflag) {
1238                         fx= area[0]; 
1239                         fy= area[1];
1240                         dxt[0]= area[2]-fx;
1241                         dxt[1]= area[3]-fy;
1242                         dyt[0]= area[4]-fx;
1243                         dyt[1]= area[5]-fy;
1244                 }
1245                 
1246                 /* repeat */
1247                 if(tex->xrepeat>1) {
1248                         fx *= tex->xrepeat;
1249                         dxt[0]*= tex->xrepeat;
1250                         dyt[0]*= tex->xrepeat;
1251                         if(fx>1.0) fx -= (int)(fx);
1252                         else if(fx<0.0) fx+= 1-(int)(fx);
1253                 }
1254                 if(tex->yrepeat>1) {
1255                         fy *= tex->yrepeat;
1256                         dxt[1]*= tex->yrepeat;
1257                         dyt[1]*= tex->yrepeat;
1258                         if(fy>1.0) fy -= (int)(fy);
1259                         else if(fy<0.0) fy+= 1-(int)(fy);
1260                 }
1261                 
1262                 /* crop */
1263                 if(tex->cropxmin!=0.0 || tex->cropxmax!=1.0) {
1264                         fac1= tex->cropxmax - tex->cropxmin;
1265                         fx= tex->cropxmin+ fx*fac1;
1266                         dxt[0]*= fac1;
1267                         dyt[0]*= fac1;
1268                 }
1269                 if(tex->cropymin!=0.0 || tex->cropymax!=1.0) {
1270                         fac1= tex->cropymax - tex->cropymin;
1271                         fy= tex->cropymin+ fy*fac1;
1272                         dxt[1]*= fac1;
1273                         dyt[1]*= fac1;
1274                 }
1275                 
1276                 t[0]= fx;
1277                 t[1]= fy;
1278
1279         }
1280 }
1281
1282
1283 /* ************************************** */
1284
1285 int multitex(Tex *tex, float *texvec, float *dxt, float *dyt)
1286 {
1287
1288
1289         switch(tex->type) {
1290         
1291         case 0:
1292                 Tin= 0.0;
1293                 return 0;
1294         case TEX_CLOUDS:
1295                 return clouds(tex, texvec); 
1296         case TEX_WOOD:
1297                 return wood(tex, texvec); 
1298         case TEX_MARBLE:
1299                 return marble(tex, texvec); 
1300         case TEX_MAGIC:
1301                 return magic(tex, texvec); 
1302         case TEX_BLEND:
1303                 return blend(tex, texvec);
1304         case TEX_STUCCI:
1305                 Tin= 0.0;
1306                 return stucci(tex, texvec); 
1307         case TEX_NOISE:
1308                 return texnoise(tex); 
1309         case TEX_IMAGE:
1310                 if(R.osatex) return imagewraposa(tex, texvec, dxt, dyt); 
1311                 else return imagewrap(tex, texvec); 
1312                 break;
1313         case TEX_PLUGIN:
1314                 return plugintex(tex, texvec, dxt, dyt);
1315                 break;
1316         case TEX_ENVMAP:
1317                 return RE_envmaptex(tex, texvec, dxt, dyt);
1318                 break;
1319         }
1320         return 0;
1321 }
1322
1323 /* ------------------------------------------------------------------------- */
1324
1325 void do_material_tex()
1326 {
1327         Object *ob;
1328         Material *mat_col, *mat_colspec, *mat_colmir, *mat_ref;
1329         Material *mat_spec, *mat_har, *mat_emit, *mat_alpha;
1330         MTex *mtex;
1331         Tex *tex;
1332         float *co = NULL, *dx = NULL, *dy = NULL, fact, 
1333                 facm, factt, facmm, facmul = 0.0, stencilTin=1.0;
1334         float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3];
1335         int tex_nr, rgbnor= 0;
1336         
1337         /* hier flag testen of er wel tex is */
1338         
1339         mat_col=mat_colspec=mat_colmir=mat_ref=mat_spec=mat_har=mat_emit=mat_alpha= R.mat;
1340         
1341         tex_nr= 0;
1342         if(R.mat->septex) tex_nr= R.mat->texact;
1343         
1344         for(; tex_nr<8; tex_nr++) {
1345                 if(R.mat->mtex[tex_nr]) {
1346                         mtex= R.mat->mtex[tex_nr];
1347                         
1348                         tex= mtex->tex;
1349                         if(tex==0) continue;
1350                         
1351                         /* welke coords */
1352                         if(mtex->texco==TEXCO_ORCO) {
1353                                 co= R.lo; dx= O.dxlo; dy= O.dylo;
1354                         }
1355                         else if(mtex->texco==TEXCO_STICKY) {
1356                                 co= R.sticky; dx= O.dxsticky; dy= O.dysticky;
1357                         }
1358                         else if(mtex->texco==TEXCO_OBJECT) {
1359                                 ob= mtex->object;
1360                                 if(ob) {
1361                                         co= tempvec;
1362                                         dx= dxt;
1363                                         dy= dyt;
1364                                         VECCOPY(tempvec, R.co);
1365                                         MTC_Mat4MulVecfl(ob->imat, tempvec);
1366                                         if(R.osatex) {
1367                                                 VECCOPY(dxt, O.dxco);
1368                                                 VECCOPY(dyt, O.dyco);
1369                                                 MTC_Mat4Mul3Vecfl(ob->imat, dxt);
1370                                                 MTC_Mat4Mul3Vecfl(ob->imat, dyt);
1371                                         }
1372                                 }
1373                                 else {
1374                                         /* als object niet bestaat geen orco's gebruiken (zijn niet geinitialiseerd */
1375                                         co= R.co;
1376                                         dx= O.dxco; dy= O.dyco;
1377                                 }
1378                         }
1379                         else if(mtex->texco==TEXCO_REFL) {
1380                                 co= R.ref; dx= O.dxref; dy= O.dyref;
1381                         }
1382                         else if(mtex->texco==TEXCO_NORM) {
1383                                 co= R.orn; dx= O.dxno; dy= O.dyno;
1384                         }
1385                         else if(mtex->texco==TEXCO_GLOB) {
1386                                 co= R.gl; dx= O.dxco; dy= O.dyco;
1387                         }
1388                         else if(mtex->texco==TEXCO_UV) {
1389                                 co= R.uv; dx= O.dxuv; dy= O.dyuv;
1390                         }
1391                         else if(mtex->texco==TEXCO_WINDOW) {
1392                                 co= R.winco; dx= O.dxwin; dy= O.dywin;
1393                         }
1394                         
1395                         /* de pointer defines if bumping happens */
1396                         if(mtex->mapto & MAP_NORM) {
1397                                 tex->nor= norvec;
1398                                 norvec[0]= norvec[1]= norvec[2]= 0.0;
1399                         }
1400                         else tex->nor= 0;
1401                         
1402                         if(tex->type==TEX_IMAGE) {
1403                                 
1404                                 /* nieuw: eerst coords verwisselen, dan map, dan trans/scale */
1405                                 
1406                                 /* placement */
1407                                 if(mtex->projx) texvec[0]= co[mtex->projx-1];
1408                                 else texvec[0]= 0.0;
1409                                 if(mtex->projy) texvec[1]= co[mtex->projy-1];
1410                                 else texvec[1]= 0.0;
1411                                 if(mtex->projz) texvec[2]= co[mtex->projz-1];
1412                                 else texvec[2]= 0.0;
1413
1414                                 if(R.osatex) {
1415
1416                                         if(mtex->projx) {
1417                                                 dxt[0]= dx[mtex->projx-1];
1418                                                 dyt[0]= dy[mtex->projx-1];
1419                                         }
1420                                         else dxt[0]= 0.0;
1421                                         if(mtex->projy) {
1422                                                 dxt[1]= dx[mtex->projy-1];
1423                                                 dyt[1]= dy[mtex->projy-1];
1424                                         }
1425                                         else dxt[1]= 0.0;
1426                                         if(mtex->projx) {
1427                                                 dxt[2]= dx[mtex->projz-1];
1428                                                 dyt[2]= dy[mtex->projz-1];
1429                                         }
1430                                         else dxt[2]= 0.0;
1431                                 }
1432
1433                                 do_2d_mapping(mtex, texvec, dxt, dyt);
1434                                 
1435                                 /* translate en scale */
1436                                 texvec[0]= mtex->size[0]*(texvec[0]-0.5) +mtex->ofs[0]+0.5;
1437                                 texvec[1]= mtex->size[1]*(texvec[1]-0.5) +mtex->ofs[1]+0.5;
1438                                 if(R.osatex) {
1439                                         dxt[0]= mtex->size[0]*dxt[0];
1440                                         dxt[1]= mtex->size[1]*dxt[1];
1441                                         dyt[0]= mtex->size[0]*dyt[0];
1442                                         dyt[1]= mtex->size[1]*dyt[1];
1443                                 }
1444                         }
1445                         else {
1446
1447                                 /* placement */
1448                                 if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
1449                                 else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
1450                                 
1451                                 if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
1452                                 else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
1453                                 
1454                                 if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
1455                                 else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
1456                                 
1457                                 if(R.osatex) {
1458                                         if(mtex->projx) {
1459                                                 dxt[0]= mtex->size[0]*dx[mtex->projx-1];
1460                                                 dyt[0]= mtex->size[0]*dy[mtex->projx-1];
1461                                         }
1462                                         else dxt[0]= 0.0;
1463                                         if(mtex->projy) {
1464                                                 dxt[1]= mtex->size[1]*dx[mtex->projy-1];
1465                                                 dyt[1]= mtex->size[1]*dy[mtex->projy-1];
1466                                         }
1467                                         else dxt[1]= 0.0;
1468                                         if(mtex->projx) {
1469                                                 dxt[2]= mtex->size[2]*dx[mtex->projz-1];
1470                                                 dyt[2]= mtex->size[2]*dy[mtex->projz-1];
1471                                         }
1472                                         else dxt[2]= 0.0;
1473                                 }
1474                         }
1475
1476                         rgbnor= multitex(tex, texvec, dxt, dyt);
1477                         
1478                         /* texture output */
1479                         
1480                         if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1481                                 Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1482                                 rgbnor-= 1;
1483                         }
1484                         if(mtex->texflag & MTEX_NEGATIVE) {
1485                                 if(rgbnor & TEX_RGB) {
1486                                         Tr= 1.0-Tr;
1487                                         Tg= 1.0-Tg;
1488                                         Tb= 1.0-Tb;
1489                                 }
1490                                 Tin= 1.0-Tin;
1491                         }
1492                         if(mtex->texflag & MTEX_STENCIL) {
1493                                 if(rgbnor & TEX_RGB) {
1494                                         fact= Ta;
1495                                         Ta*= stencilTin;
1496                                         stencilTin*= fact;
1497                                 }
1498                                 else {
1499                                         fact= Tin;
1500                                         Tin*= stencilTin;
1501                                         stencilTin*= fact;
1502                                 }
1503                         }
1504                         else {
1505                                 if(rgbnor & TEX_RGB) Ta*= stencilTin;
1506                                 else Tin*= stencilTin;
1507                         }
1508
1509                         if(tex->nor && (rgbnor & TEX_NOR)==0) {
1510                                 /* make our own normal */
1511                                 if(rgbnor & TEX_RGB) {
1512                                         tex->nor[0]= Tr;
1513                                         tex->nor[1]= Tg;
1514                                         tex->nor[2]= Tb;
1515                                 }
1516                                 else {
1517                                         float co= 0.5*cos(Tin-0.5);
1518                                         float si= 0.5*sin(Tin-0.5);
1519                                         float f1, f2;
1520                                         
1521                                         f1= R.vn[0];
1522                                         f2= R.vn[1];
1523                                         tex->nor[0]= f1*co+f2*si;
1524                                         tex->nor[1]= f2*co-f1*si;
1525                                         f1= R.vn[1];
1526                                         f2= R.vn[2];
1527                                         tex->nor[1]= f1*co+f2*si;
1528                                         tex->nor[2]= f2*co-f1*si;
1529                                 }
1530                         }
1531
1532                         
1533                         /* mapping */
1534                         if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
1535                                 
1536                                 if((rgbnor & TEX_RGB)==0) {
1537                                         Tr= mtex->r;
1538                                         Tg= mtex->g;
1539                                         Tb= mtex->b;
1540                                 }
1541                                 else if(mtex->mapto & MAP_ALPHA) {
1542                                         if(mtex->texflag & MTEX_ALPHAMIX) Tin= Ta;
1543                                         else Tin= stencilTin;
1544                                 }
1545                                 else Tin= Ta;
1546
1547                                 fact= Tin*mtex->colfac;
1548                                 facm= 1.0-fact;
1549                                 if(mtex->blendtype==MTEX_MUL) facm= 1.0-mtex->colfac;
1550                                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
1551
1552                                 if(mtex->mapto & MAP_COL) {
1553                                         if(mtex->blendtype==MTEX_BLEND) {
1554                                                 R.matren->r= (fact*Tr + facm*mat_col->r);
1555                                                 R.matren->g= (fact*Tg + facm*mat_col->g);
1556                                                 R.matren->b= (fact*Tb + facm*mat_col->b);
1557                                         }
1558                                         else if(mtex->blendtype==MTEX_MUL) {
1559                                                 R.matren->r= (facm+fact*Tr)*mat_col->r;
1560                                                 R.matren->g= (facm+fact*Tg)*mat_col->g;
1561                                                 R.matren->b= (facm+fact*Tb)*mat_col->b;
1562                                         }
1563                                         else {
1564                                                 R.matren->r= (fact*Tr + mat_col->r);
1565                                                 R.matren->g= (fact*Tg + mat_col->g);
1566                                                 R.matren->b= (fact*Tb + mat_col->b);
1567                                         }
1568                                         mat_col= R.matren;
1569                                 }
1570                                 if(mtex->mapto & MAP_COLSPEC) {
1571                                         if(mtex->blendtype==MTEX_BLEND) {
1572                                                 R.matren->specr= (fact*Tr + facm*mat_colspec->specr);
1573                                                 R.matren->specg= (fact*Tg + facm*mat_colspec->specg);
1574                                                 R.matren->specb= (fact*Tb + facm*mat_colspec->specb);
1575                                         }
1576                                         else if(mtex->blendtype==MTEX_MUL) {
1577                                                 R.matren->specr= (facm+fact*Tr)*mat_colspec->specr;
1578                                                 R.matren->specg= (facm+fact*Tg)*mat_colspec->specg;
1579                                                 R.matren->specb= (facm+fact*Tb)*mat_colspec->specb;
1580                                         }
1581                                         else {
1582                                                 R.matren->specr= (fact*Tr + mat_colspec->specr);
1583                                                 R.matren->specg= (fact*Tg + mat_colspec->specg);
1584                                                 R.matren->specb= (fact*Tb + mat_colspec->specb);
1585                                         }
1586                                         mat_colspec= R.matren;
1587                                 }
1588                                 if(mtex->mapto & MAP_COLMIR) {
1589                                         if(mtex->blendtype==MTEX_BLEND) {
1590                                                 R.refcol[0]= fact + facm*R.refcol[0];
1591                                                 
1592                                                 R.refcol[1]= fact*Tr + facm*R.refcol[1];
1593                                                 R.refcol[2]= fact*Tg + facm*R.refcol[2];
1594                                                 R.refcol[3]= fact*Tb + facm*R.refcol[3];
1595                                         }
1596                                         else if(mtex->blendtype==MTEX_MUL) {
1597                                                 R.matren->mirr= (facm+fact*Tr)*mat_colmir->mirr;
1598                                                 R.matren->mirg= (facm+fact*Tg)*mat_colmir->mirg;
1599                                                 R.matren->mirb= (facm+fact*Tb)*mat_colmir->mirb;
1600                                         }
1601                                         else {
1602                                                 R.matren->mirr= (fact*Tr + mat_colmir->mirr);
1603                                                 R.matren->mirg= (fact*Tg + mat_colmir->mirg);
1604                                                 R.matren->mirb= (fact*Tb + mat_colmir->mirb);
1605                                         }
1606                                         mat_colmir= R.matren;
1607                                 }
1608                         }
1609                         if( (mtex->mapto & MAP_NORM) ) {
1610                                 if(tex->nor) {
1611                                         
1612                                         if(mtex->maptoneg & MAP_NORM) tex->norfac= -mtex->norfac;
1613                                         else tex->norfac= mtex->norfac;
1614
1615                                         R.vn[0]+= tex->norfac*tex->nor[0];
1616                                         R.vn[1]+= tex->norfac*tex->nor[1];
1617                                         R.vn[2]+= tex->norfac*tex->nor[2];
1618                                         
1619                                         Normalise(R.vn);
1620                                         
1621                                         /* hierdoor wordt de bump aan de volgende texture doorgegeven */
1622                                         R.orn[0]= R.vn[0];
1623                                         R.orn[1]= -R.vn[1];
1624                                         R.orn[2]= R.vn[2];
1625                                         
1626                                         /* reflection vector */
1627                                         RE_calc_R_ref();
1628                                 }
1629                         }
1630
1631                         if(mtex->mapto & MAP_VARS) {
1632                                 if(rgbnor & TEX_RGB) {
1633                                         if(Talpha) Tin= Ta;
1634                                         else Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1635                                 }
1636                                 
1637                                 fact= Tin*mtex->varfac;
1638                                 facm= 1.0-fact;
1639                                 if(mtex->blendtype==MTEX_MUL) facmul= 1.0-mtex->varfac;
1640                                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
1641
1642                                 if(mtex->mapto & MAP_REF) {
1643                                         if(mtex->maptoneg & MAP_REF) {factt= facm; facmm= fact;}
1644                                         else {factt= fact; facmm= facm;}
1645                                         
1646                                         if(mtex->blendtype==MTEX_BLEND)
1647                                                 R.matren->ref= factt*mtex->def_var+ facmm*mat_ref->ref;
1648                                         else if(mtex->blendtype==MTEX_MUL)
1649                                                 R.matren->ref= (facmul+factt)*mat_ref->ref;
1650                                         else {
1651                                                 R.matren->ref= factt+mat_ref->ref;
1652                                                 if(R.matren->ref<0.0) R.matren->ref= 0.0;
1653                                         }
1654                                         mat_ref= R.matren;
1655                                 }
1656                                 if(mtex->mapto & MAP_SPEC) {
1657                                         if(mtex->maptoneg & MAP_SPEC) {factt= facm; facmm= fact;}
1658                                         else {factt= fact; facmm= facm;}
1659                                         
1660                                         if(mtex->blendtype==MTEX_BLEND)
1661                                                 R.matren->spec= factt*mtex->def_var+ facmm*mat_spec->spec;
1662                                         else if(mtex->blendtype==MTEX_MUL)
1663                                                 R.matren->spec= (facmul+factt)*mat_spec->spec;
1664                                         else {
1665                                                 R.matren->spec= factt+mat_spec->spec;
1666                                                 if(R.matren->spec<0.0) R.matren->spec= 0.0;
1667                                         }
1668                                         mat_spec= R.matren;
1669                                 }
1670                                 if(mtex->mapto & MAP_EMIT) {
1671                                         if(mtex->maptoneg & MAP_EMIT) {factt= facm; facmm= fact;}
1672                                         else {factt= fact; facmm= facm;}
1673                                         
1674                                         if(mtex->blendtype==MTEX_BLEND)
1675                                                 R.matren->emit= factt*mtex->def_var+ facmm*mat_emit->emit;
1676                                         else if(mtex->blendtype==MTEX_MUL)
1677                                                 R.matren->emit= (facmul+factt)*mat_emit->emit;
1678                                         else {
1679                                                 R.matren->emit= factt+mat_emit->emit;
1680                                                 if(R.matren->emit<0.0) R.matren->emit= 0.0;
1681                                         }
1682                                         mat_emit= R.matren;
1683                                 }
1684                                 if(mtex->mapto & MAP_ALPHA) {
1685                                         if(mtex->maptoneg & MAP_ALPHA) {factt= facm; facmm= fact;}
1686                                         else {factt= fact; facmm= facm;}
1687                                         
1688                                         if(mtex->blendtype==MTEX_BLEND)
1689                                                 R.matren->alpha= factt*mtex->def_var+ facmm*mat_alpha->alpha;
1690                                         else if(mtex->blendtype==MTEX_MUL)
1691                                                 R.matren->alpha= (facmul+factt)*mat_alpha->alpha;
1692                                         else {
1693                                                 R.matren->alpha= factt+mat_alpha->alpha;
1694                                                 if(R.matren->alpha<0.0) R.matren->alpha= 0.0;
1695                                                 else if(R.matren->alpha>1.0) R.matren->alpha= 1.0;
1696                                         }
1697                                         mat_alpha= R.matren;
1698                                 }
1699                                 if(mtex->mapto & MAP_HAR) {
1700                                         if(mtex->maptoneg & MAP_HAR) {factt= facm; facmm= fact;}
1701                                         else {factt= fact; facmm= facm;}
1702                                         
1703                                         if(mtex->blendtype==MTEX_BLEND) {
1704                                                 R.matren->har= 128.0*factt*mtex->def_var+ facmm*mat_har->har;
1705                                         } else if(mtex->blendtype==MTEX_MUL) {
1706                                                 R.matren->har= (facmul+factt)*mat_har->har;
1707                                         } else {
1708                                                 R.matren->har= 128.0*factt+mat_har->har;
1709                                                 if(R.matren->har<1) R.matren->har= 1;
1710                                         }
1711                                         mat_har= R.matren;
1712                                 }
1713                         }
1714                 }
1715                 
1716                 if(R.mat->septex) break;
1717         }
1718 }
1719
1720 /* ------------------------------------------------------------------------- */
1721
1722 void do_halo_tex(HaloRen *har, float xn, float yn, float *colf)
1723 {
1724         MTex *mtex;
1725         float texvec[3], dxt[3], dyt[3], fact, facm, dx;
1726         int rgb;
1727         
1728         mtex= har->mat->mtex[0];
1729         if(mtex->tex==0) return;
1730         /* no normal mapping */
1731         mtex->tex->nor= 0;
1732                 
1733         texvec[0]= xn/har->rad;
1734         texvec[1]= yn/har->rad;
1735         texvec[2]= 0.0;
1736         
1737         R.osatex= (har->mat->texco & TEXCO_OSA);
1738
1739         /* placement */
1740         if(mtex->projx) texvec[0]= mtex->size[0]*(texvec[mtex->projx-1]+mtex->ofs[0]);
1741         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
1742         
1743         if(mtex->projy) texvec[1]= mtex->size[1]*(texvec[mtex->projy-1]+mtex->ofs[1]);
1744         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
1745         
1746         if(mtex->projz) texvec[2]= mtex->size[2]*(texvec[mtex->projz-1]+mtex->ofs[2]);
1747         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
1748         
1749         if(R.osatex) {
1750         
1751                 dx= 1.0/har->rad;
1752         
1753                 if(mtex->projx) {
1754                         dxt[0]= mtex->size[0]*dx;
1755                         dyt[0]= mtex->size[0]*dx;
1756                 }
1757                 else dxt[0]= 0.0;
1758                 if(mtex->projy) {
1759                         dxt[1]= mtex->size[1]*dx;
1760                         dyt[1]= mtex->size[1]*dx;
1761                 }
1762                 else dxt[1]= 0.0;
1763                 if(mtex->projz) {
1764                         dxt[2]= 0.0;
1765                         dyt[2]= 0.0;
1766                 }
1767                 else dxt[2]= 0.0;
1768
1769         }
1770
1771
1772         if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, dxt, dyt);
1773         
1774         rgb= multitex(mtex->tex, texvec, dxt, dyt);
1775
1776         /* texture uitgang */
1777         if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
1778                 Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1779                 rgb= 0;
1780         }
1781         if(mtex->texflag & MTEX_NEGATIVE) {
1782                 if(rgb) {
1783                         Tr= 1.0-Tr;
1784                         Tg= 1.0-Tg;
1785                         Tb= 1.0-Tb;
1786                 }
1787                 else Tin= 1.0-Tin;
1788         }
1789
1790         /* mapping */
1791         if(mtex->mapto & MAP_COL) {
1792                 
1793                 if(rgb==0) {
1794                         Tr= mtex->r;
1795                         Tg= mtex->g;
1796                         Tb= mtex->b;
1797                 }
1798                 else if(mtex->mapto & MAP_ALPHA) {
1799                         if(mtex->texflag & MTEX_ALPHAMIX) Tin= Ta; 
1800                         else Tin= 1.0;
1801                 }
1802                 else Tin= Ta;
1803
1804                 fact= Tin*mtex->colfac;
1805                 facm= 1.0-fact;
1806                 
1807                 if(mtex->blendtype==MTEX_MUL) {
1808                         facm= 1.0-mtex->colfac;
1809                 }
1810                 else fact*= 256;
1811
1812                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
1813
1814                 if(mtex->blendtype==MTEX_BLEND) {
1815                         colf[0]= (fact*Tr + facm*har->r);
1816                         colf[1]= (fact*Tg + facm*har->g);
1817                         colf[2]= (fact*Tb + facm*har->b);
1818                 }
1819                 else if(mtex->blendtype==MTEX_MUL) {
1820                         colf[0]= (facm+fact*Tr)*har->r;
1821                         colf[1]= (facm+fact*Tg)*har->g;
1822                         colf[2]= (facm+fact*Tb)*har->b;
1823                 }
1824                 else {
1825                         colf[0]= (fact*Tr + har->r);
1826                         colf[1]= (fact*Tg + har->g);
1827                         colf[2]= (fact*Tb + har->b);
1828                         
1829                         CLAMP(colf[0], 0.0, 1.0);
1830                         CLAMP(colf[1], 0.0, 1.0);
1831                         CLAMP(colf[2], 0.0, 1.0);
1832                 }
1833         }
1834         if(mtex->mapto & MAP_ALPHA) {
1835                 if(rgb) {
1836                         if(Talpha) Tin= Ta;
1837                         else Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1838                 }
1839                                 
1840                 colf[3]*= Tin;
1841         }
1842
1843         R.osatex= 0;
1844 }
1845
1846 /* ------------------------------------------------------------------------- */
1847
1848 void do_sky_tex()
1849 {
1850         World *wrld_hor, *wrld_zen;
1851         MTex *mtex;
1852         float *co, fact, facm, factt, facmm, facmul = 0.0, stencilTin=1.0;
1853         float tempvec[3], texvec[3], dxt[3], dyt[3];
1854         int tex_nr, rgb= 0, ok;
1855         
1856         
1857         /* hier flag testen of er wel tex is */
1858         
1859         wrld_hor= wrld_zen= G.scene->world;
1860
1861         /* The 6 here is rather arbitrary, it seems. */
1862         for(tex_nr=0; tex_nr<6; tex_nr++) {
1863                 if(R.wrld.mtex[tex_nr]) {
1864                         mtex= R.wrld.mtex[tex_nr];
1865                         
1866                         if(mtex->tex==0) continue;
1867                         /* if(mtex->mapto==0) continue; */
1868                         
1869                         /* welke coords */
1870                         co= R.lo;
1871                         
1872                         /* Grab the mapping settings for this texture */
1873                         if(mtex->texco==TEXCO_OBJECT) {
1874                                 Object *ob= mtex->object;
1875                                 if(ob) {
1876                                         VECCOPY(tempvec, R.lo);
1877                                         MTC_Mat4MulVecfl(ob->imat, tempvec);
1878                                         co= tempvec;
1879                                 }
1880                         }
1881                         
1882                         /* placement */                 
1883                         if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
1884                         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
1885                         
1886                         if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
1887                         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
1888                         
1889                         if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
1890                         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
1891                         
1892                         /* texture */
1893                         if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, dxt, dyt);
1894                         
1895                         rgb= multitex(mtex->tex, texvec, dxt, dyt);
1896                         
1897                         /* texture uitgang */
1898                         if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
1899                                 Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1900                                 rgb= 0;
1901                         }
1902                         if(mtex->texflag & MTEX_NEGATIVE) {
1903                                 if(rgb) {
1904                                         Tr= 1.0-Tr;
1905                                         Tg= 1.0-Tg;
1906                                         Tb= 1.0-Tb;
1907                                 }
1908                                 else Tin= 1.0-Tin;
1909                         }
1910                         if(mtex->texflag & MTEX_STENCIL) {
1911                                 if(rgb) {
1912                                         
1913                                 }
1914                                 else {
1915                                         fact= Tin;
1916                                         Tin*= stencilTin;
1917                                         stencilTin*= fact;
1918                                 }
1919                         }
1920                         else {
1921                                 if(rgb) ;
1922                                 else Tin*= stencilTin;
1923                         }
1924                         
1925                         /* colour mapping */
1926                         if(mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
1927                                 
1928                                 if(rgb==0) {
1929                                         Tr= mtex->r;
1930                                         Tg= mtex->g;
1931                                         Tb= mtex->b;
1932                                 }
1933                                 else Tin= 1.0;
1934
1935                                 fact= Tin*mtex->colfac;
1936                                 facm= 1.0-fact;
1937                                 if(mtex->blendtype==MTEX_MUL) facm= 1.0-mtex->colfac;
1938                                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
1939
1940                                 if(mtex->mapto & WOMAP_HORIZ) {
1941                                         if(mtex->blendtype==MTEX_BLEND) {
1942                                                 R.wrld.horr= (fact*Tr + facm*wrld_hor->horr);
1943                                                 R.wrld.horg= (fact*Tg + facm*wrld_hor->horg);
1944                                                 R.wrld.horb= (fact*Tb + facm*wrld_hor->horb);
1945                                         }
1946                                         else if(mtex->blendtype==MTEX_MUL) {
1947                                                 R.wrld.horr= (facm+fact*Tr)*wrld_hor->horr;
1948                                                 R.wrld.horg= (facm+fact*Tg)*wrld_hor->horg;
1949                                                 R.wrld.horb= (facm+fact*Tb)*wrld_hor->horb;
1950                                         }
1951                                         else {
1952                                                 R.wrld.horr= (fact*Tr + wrld_hor->horr);
1953                                                 R.wrld.horg= (fact*Tg + wrld_hor->horg);
1954                                                 R.wrld.horb= (fact*Tb + wrld_hor->horb);
1955                                         }
1956                                         wrld_hor= &R.wrld;
1957                                 }
1958                                 if(mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
1959                                         ok= 0;
1960                                         if(R.wrld.skytype & WO_SKYREAL) {
1961                                                 if((R.wrld.skytype & WO_ZENUP)) {
1962                                                         if(mtex->mapto & WOMAP_ZENUP) ok= 1;
1963                                                 }
1964                                                 else if(mtex->mapto & WOMAP_ZENDOWN) ok= 1;
1965                                         }
1966                                         else ok= 1;
1967                                         
1968                                         if(ok) {
1969                                         
1970                                                 if(mtex->blendtype==MTEX_BLEND) {
1971                                                         R.wrld.zenr= (fact*Tr + facm*wrld_zen->zenr);
1972                                                         R.wrld.zeng= (fact*Tg + facm*wrld_zen->zeng);
1973                                                         R.wrld.zenb= (fact*Tb + facm*wrld_zen->zenb);
1974                                                 }
1975                                                 else if(mtex->blendtype==MTEX_MUL) {
1976                                                         R.wrld.zenr= (facm+fact*Tr)*wrld_zen->zenr;
1977                                                         R.wrld.zeng= (facm+fact*Tg)*wrld_zen->zeng;
1978                                                         R.wrld.zenb= (facm+fact*Tb)*wrld_zen->zenb;
1979                                                 }
1980                                                 else {
1981                                                         R.wrld.zenr= (fact*Tr + wrld_zen->zenr);
1982                                                         R.wrld.zeng= (fact*Tg + wrld_zen->zeng);
1983                                                         R.wrld.zenb= (fact*Tb + wrld_zen->zenb);
1984                                                 }
1985                                                 wrld_zen= &R.wrld;
1986                                         }
1987                                         else {
1988                                                 /* anders blijft zenRGB hangen */
1989                                                 R.wrld.zenr= wrld_zen->zenr;
1990                                                 R.wrld.zeng= wrld_zen->zeng;
1991                                                 R.wrld.zenb= wrld_zen->zenb;
1992                                         }
1993                                 }
1994                         }
1995                         if(mtex->mapto & WOMAP_BLEND) {
1996                                 if(rgb) Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
1997                                 
1998                                 fact= Tin*mtex->varfac;
1999                                 facm= 1.0-fact;
2000                                 if(mtex->blendtype==MTEX_MUL) facmul= 1.0-mtex->varfac;
2001                                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
2002
2003                                 factt= fact; facmm= facm;
2004                                 
2005                                 if(mtex->blendtype==MTEX_BLEND)
2006                                         R.inprz= factt*mtex->def_var+ facmm*R.inprz;
2007                                 else if(mtex->blendtype==MTEX_MUL)
2008                                         R.inprz= (facmul+factt)*R.inprz;
2009                                 else {
2010                                         R.inprz= factt+R.inprz;
2011                                 }
2012                         }
2013                 }
2014         }
2015 }
2016
2017 /* ------------------------------------------------------------------------- */
2018 /* explicit lampren stuff should be factored out! or rather, the
2019    texturing stuff might need to go...*/
2020 void do_lamp_tex(LampRen *la, float *lavec)
2021 {
2022         Object *ob;
2023         LampRen *la_col;
2024         MTex *mtex;
2025         Tex *tex;
2026         float *co = NULL, *dx = NULL, *dy = NULL, fact, facm, stencilTin=1.0;
2027         float texvec[3], dxt[3], dyt[3], tempvec[3];
2028         int tex_nr, rgb= 0;
2029         
2030         /* hier flag testen of er wel tex is */
2031         
2032         la_col= la->org;
2033         
2034         tex_nr= 0;
2035         
2036         for(; tex_nr<6; tex_nr++) {
2037                 
2038                 if(la->mtex[tex_nr]) {
2039                         mtex= la->mtex[tex_nr];
2040                         
2041                         tex= mtex->tex;
2042                         if(tex==0) continue;
2043                         
2044                         /* welke coords */
2045                         if(mtex->texco==TEXCO_OBJECT) {
2046                                 ob= mtex->object;
2047                                 if(ob) {
2048                                         co= tempvec;
2049                                         dx= dxt;
2050                                         dy= dyt;
2051                                         VECCOPY(tempvec, R.co);
2052                                         MTC_Mat4MulVecfl(ob->imat, tempvec);
2053                                         if(R.osatex) {
2054                                                 VECCOPY(dxt, O.dxco);
2055                                                 VECCOPY(dyt, O.dyco);
2056                                                 MTC_Mat4Mul3Vecfl(ob->imat, dxt);
2057                                                 MTC_Mat4Mul3Vecfl(ob->imat, dyt);
2058                                         }
2059                                 }
2060                                 else {
2061                                         co= R.co;
2062                                         dx= O.dxco; dy= O.dyco;
2063                                 }
2064                         }
2065                         else if(mtex->texco==TEXCO_GLOB) {
2066                                 co= R.gl; dx= O.dxco; dy= O.dyco;
2067                                 VECCOPY(R.gl, R.co);
2068                                 MTC_Mat4MulVecfl(R.viewinv, R.gl);
2069                         }
2070                         else if(mtex->texco==TEXCO_VIEW) {
2071                                 
2072                                 VECCOPY(tempvec, lavec);
2073                                 MTC_Mat3MulVecfl(la->imat, tempvec);
2074                                 
2075                                 tempvec[0]*= la->spottexfac;
2076                                 tempvec[1]*= la->spottexfac;
2077                                 co= tempvec; 
2078                                 
2079                                 dx= dxt; dy= dyt;       
2080                                 if(R.osatex) {
2081                                         VECCOPY(dxt, O.dxlv);
2082                                         VECCOPY(dyt, O.dylv);
2083                                         /* need some matrix conversion here? la->imat is a [3][3]  matrix!!! **/
2084                                         MTC_Mat3MulVecfl(la->imat, dxt);
2085                                         MTC_Mat3MulVecfl(la->imat, dyt);
2086                                         
2087                                         VecMulf(dxt, la->spottexfac);
2088                                         VecMulf(dyt, la->spottexfac);
2089                                 }
2090                         }
2091                         
2092                         
2093                         /* placement */
2094                         if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2095                         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2096                         
2097                         if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
2098                         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2099                         
2100                         if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
2101                         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2102                         
2103                         if(R.osatex) {
2104                                 if(mtex->projx) {
2105                                         dxt[0]= mtex->size[0]*dx[mtex->projx-1];
2106                                         dyt[0]= mtex->size[0]*dy[mtex->projx-1];
2107                                 }
2108                                 else dxt[0]= 0.0;
2109                                 if(mtex->projy) {
2110                                         dxt[1]= mtex->size[1]*dx[mtex->projy-1];
2111                                         dyt[1]= mtex->size[1]*dy[mtex->projy-1];
2112                                 }
2113                                 else dxt[1]= 0.0;
2114                                 if(mtex->projx) {
2115                                         dxt[2]= mtex->size[2]*dx[mtex->projz-1];
2116                                         dyt[2]= mtex->size[2]*dy[mtex->projz-1];
2117                                 }
2118                                 else dxt[2]= 0.0;
2119                         }
2120                         
2121                         /* texture */
2122                         if(tex->type==TEX_IMAGE) {
2123                                 do_2d_mapping(mtex, texvec, dxt, dyt);
2124                                 
2125                                 if(mtex->mapto & MAP_NORM) {
2126                                         /* de pointer bepaalt of er gebumpt wordt */
2127                                         tex->nor= R.vn;
2128                                         if(mtex->maptoneg & MAP_NORM) tex->norfac= -mtex->norfac;
2129                                         else tex->norfac= mtex->norfac;
2130                                 }
2131                                 else tex->nor= 0;
2132                         }
2133                         
2134                         rgb= multitex(tex, texvec, dxt, dyt);
2135                         
2136                         
2137                         
2138                         /* texture uitgang */
2139                         if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2140                                 Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
2141                                 rgb= 0;
2142                         }
2143                         if(mtex->texflag & MTEX_NEGATIVE) {
2144                                 if(rgb) {
2145                                         Tr= 1.0-Tr;
2146                                         Tg= 1.0-Tg;
2147                                         Tb= 1.0-Tb;
2148                                 }
2149                                 else Tin= 1.0-Tin;
2150                         }
2151                         if(mtex->texflag & MTEX_STENCIL) {
2152                                 if(rgb) {
2153                                         fact= Ta;
2154                                         Ta*= stencilTin;
2155                                         stencilTin*= fact;
2156                                 }
2157                                 else {
2158                                         fact= Tin;
2159                                         Tin*= stencilTin;
2160                                         stencilTin*= fact;
2161                                 }
2162                         }
2163                         else {
2164                                 if(rgb) Ta*= stencilTin;
2165                                 else Tin*= stencilTin;
2166                         }
2167                         
2168                         /* mapping */
2169                         if(mtex->mapto & LAMAP_COL) {
2170                                 
2171                                 if(rgb==0) {
2172                                         Tr= mtex->r;
2173                                         Tg= mtex->g;
2174                                         Tb= mtex->b;
2175                                 }
2176                                 else if(mtex->mapto & MAP_ALPHA) {
2177                                         if(mtex->texflag & MTEX_ALPHAMIX) Tin= Ta;
2178                                         else Tin= stencilTin;
2179                                 }
2180                                 else Tin= Ta;
2181
2182                                 Tr*= la->energy;
2183                                 Tg*= la->energy;
2184                                 Tb*= la->energy;
2185
2186                                 fact= Tin*mtex->colfac;
2187                                 facm= 1.0-fact;
2188                                 if(mtex->blendtype==MTEX_MUL) facm= 1.0-mtex->colfac;
2189                                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
2190
2191                                 if(mtex->blendtype==MTEX_BLEND) {
2192                                         la->r= (fact*Tr + facm*la_col->r);
2193                                         la->g= (fact*Tg + facm*la_col->g);
2194                                         la->b= (fact*Tb + facm*la_col->b);
2195                                 }
2196                                 else if(mtex->blendtype==MTEX_MUL) {
2197                                         la->r= (facm+fact*Tr)*la_col->r;
2198                                         la->g= (facm+fact*Tg)*la_col->g;
2199                                         la->b= (facm+fact*Tb)*la_col->b;
2200                                 }
2201                                 else {
2202                                         la->r= (fact*Tr + la_col->r);
2203                                         la->g= (fact*Tg + la_col->g);
2204                                         la->b= (fact*Tb + la_col->b);
2205                                 }
2206                                 la_col= la; /* Is it just me or is this a useless statement? */
2207                         }
2208                         
2209                 }
2210         }
2211 }
2212
2213 /* ------------------------------------------------------------------------- */
2214
2215 void externtex(MTex *mtex, float *vec)
2216 {
2217         Tex *tex;
2218         float dxt[3], dyt[3], texvec[3];
2219         int rgb;
2220         
2221         tex= mtex->tex;
2222         if(tex==0) return;
2223         
2224         R.osatex= 0;
2225         R.vlr= 0;
2226         
2227         /* placement */
2228         if(mtex->projx) texvec[0]= mtex->size[0]*(vec[mtex->projx-1]+mtex->ofs[0]);
2229         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2230         
2231         if(mtex->projy) texvec[1]= mtex->size[1]*(vec[mtex->projy-1]+mtex->ofs[1]);
2232         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2233         
2234         if(mtex->projz) texvec[2]= mtex->size[2]*(vec[mtex->projz-1]+mtex->ofs[2]);
2235         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2236         
2237         /* texture */
2238         if(tex->type==TEX_IMAGE) {
2239                 do_2d_mapping(mtex, texvec, dxt, dyt);
2240                 
2241                 if(mtex->mapto & MAP_NORM) {
2242                         /* de pointer bepaalt of er gebumpt wordt */
2243                         tex->nor= R.vn;
2244                         if(mtex->maptoneg & MAP_NORM) tex->norfac= -mtex->norfac;
2245                         else tex->norfac= mtex->norfac;
2246                 }
2247                 else tex->nor= 0;
2248         }
2249         
2250         rgb= multitex(tex, texvec, dxt, dyt);
2251         if(rgb) {
2252                 Tin= (0.35*Tr+0.45*Tg+0.2*Tb);
2253         }
2254         else {
2255                 Tr= mtex->r;
2256                 Tg= mtex->g;
2257                 Tb= mtex->b;
2258         }
2259 }
2260
2261 /* ------------------------------------------------------------------------- */
2262
2263 void externtexcol(MTex *mtex, float *orco, char *col)
2264 {
2265         int temp;
2266         float b1;
2267
2268         if(mtex->tex==0) return;
2269         
2270         externtex(mtex, orco);
2271
2272         b1= 1.0-Tin;
2273
2274         temp= 255*(Tin*Tr)+b1*col[0];
2275         if(temp>255) col[0]= 255; else col[0]= temp;
2276         temp= 255*(Tin*Tg)+b1*col[1];
2277         if(temp>255) col[1]= 255; else col[1]= temp;
2278         temp= 255*(Tin*Tb)+b1*col[2];
2279         if(temp>255) col[2]= 255; else col[2]= temp;
2280         
2281 }
2282
2283 /* ------------------------------------------------------------------------- */
2284
2285 void render_realtime_texture()
2286 {
2287         static Tex tex;
2288         static int firsttime= 1;
2289         float texvec[2], dx[2], dy[2];
2290         
2291         if(firsttime) {
2292                 default_tex(&tex);
2293                 tex.type= TEX_IMAGE;
2294                 firsttime= 0;
2295         }
2296         
2297         tex.ima = R.vlr->tface->tpage;
2298         if(tex.ima) {
2299                 
2300                 texvec[0]= 0.5+0.5*R.uv[0];
2301                 texvec[1]= 0.5+0.5*R.uv[1];
2302                 if(R.osatex) {
2303                         dx[0]= 0.5*O.dxuv[0];
2304                         dx[1]= 0.5*O.dxuv[1];
2305                         dy[0]= 0.5*O.dyuv[0];
2306                         dy[1]= 0.5*O.dyuv[1];
2307                 }
2308                 
2309                 if(R.osatex) imagewraposa(&tex, texvec, dx, dy); 
2310                 else imagewrap(&tex, texvec); 
2311                 
2312                 R.vcol[0]*= Tr;
2313                 R.vcol[1]*= Tg;
2314                 R.vcol[2]*= Tb;
2315         }
2316         
2317         
2318 }
2319
2320 /* eof */