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