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