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