a4182637f0278fb7b2c5c9682918d0fb7c0c7d99
[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 int multitex_ext(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
1207 {
1208         
1209         if(tex==NULL) {
1210                 memset(texres, 0, sizeof(TexResult));
1211                 return 0;
1212         }
1213         
1214         /* Image requires 2d mapping conversion */
1215         if(tex->type==TEX_IMAGE) {
1216                 MTex mtex;
1217                 float texvec_l[3], dxt_l[3], dyt_l[3];
1218                 
1219                 mtex.mapping= MTEX_FLAT;
1220                 mtex.tex= tex;
1221                 mtex.object= NULL;
1222                 mtex.texco= TEXCO_ORCO;
1223                 
1224                 VECCOPY(texvec_l, texvec);
1225                 if(dxt && dyt) {
1226                         VECCOPY(dxt_l, dxt);
1227                         VECCOPY(dyt_l, dyt);
1228                 }
1229                 else {
1230                         dxt_l[0]= dxt_l[1]= dxt_l[2]= 0.0f;
1231                         dyt_l[0]= dyt_l[1]= dyt_l[2]= 0.0f;
1232                 }
1233                 
1234                 do_2d_mapping(&mtex, texvec_l, NULL, NULL, dxt_l, dyt_l);
1235
1236                 return multitex(tex, texvec_l, dxt_l, dyt_l, osatex, texres);
1237         }
1238         else
1239                 return multitex(tex, texvec, dxt, dyt, osatex, texres);
1240 }
1241
1242 /* ------------------------------------------------------------------------- */
1243
1244 /* in = destination, tex = texture, out = previous color */
1245 /* fact = texture strength, facg = button strength value */
1246 void texture_rgb_blend(float *in, float *tex, float *out, float fact, float facg, int blendtype)
1247 {
1248         float facm, col;
1249         
1250         switch(blendtype) {
1251         case MTEX_BLEND:
1252                 fact*= facg;
1253                 facm= 1.0-fact;
1254
1255                 in[0]= (fact*tex[0] + facm*out[0]);
1256                 in[1]= (fact*tex[1] + facm*out[1]);
1257                 in[2]= (fact*tex[2] + facm*out[2]);
1258                 break;
1259                 
1260         case MTEX_MUL:
1261                 fact*= facg;
1262                 facm= 1.0-facg;
1263                 in[0]= (facm+fact*tex[0])*out[0];
1264                 in[1]= (facm+fact*tex[1])*out[1];
1265                 in[2]= (facm+fact*tex[2])*out[2];
1266                 break;
1267
1268         case MTEX_SCREEN:
1269                 fact*= facg;
1270                 facm= 1.0-facg;
1271                 in[0]= 1.0 - (facm+fact*(1.0-tex[0])) * (1.0-out[0]);
1272                 in[1]= 1.0 - (facm+fact*(1.0-tex[1])) * (1.0-out[1]);
1273                 in[2]= 1.0 - (facm+fact*(1.0-tex[2])) * (1.0-out[2]);
1274                 break;
1275
1276         case MTEX_OVERLAY:
1277                 fact*= facg;
1278                 facm= 1.0-facg;
1279                 
1280                 if(out[0] < 0.5f)
1281                         in[0] = out[0] * (facm + 2.0f*fact*tex[0]);
1282                 else
1283                         in[0] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[0])) * (1.0 - out[0]);
1284                 if(out[1] < 0.5f)
1285                         in[1] = out[1] * (facm + 2.0f*fact*tex[1]);
1286                 else
1287                         in[1] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[1])) * (1.0 - out[1]);
1288                 if(out[2] < 0.5f)
1289                         in[2] = out[2] * (facm + 2.0f*fact*tex[2]);
1290                 else
1291                         in[2] = 1.0f - (facm + 2.0f*fact*(1.0 - tex[2])) * (1.0 - out[2]);
1292                 break;
1293                 
1294         case MTEX_SUB:
1295                 fact= -fact;
1296         case MTEX_ADD:
1297                 fact*= facg;
1298                 in[0]= (fact*tex[0] + out[0]);
1299                 in[1]= (fact*tex[1] + out[1]);
1300                 in[2]= (fact*tex[2] + out[2]);
1301                 break;
1302
1303         case MTEX_DIV:
1304                 fact*= facg;
1305                 facm= 1.0-fact;
1306                 
1307                 if(tex[0]!=0.0)
1308                         in[0]= facm*out[0] + fact*out[0]/tex[0];
1309                 if(tex[1]!=0.0)
1310                         in[1]= facm*out[1] + fact*out[1]/tex[1];
1311                 if(tex[2]!=0.0)
1312                         in[2]= facm*out[2] + fact*out[2]/tex[2];
1313
1314                 break;
1315
1316         case MTEX_DIFF:
1317                 fact*= facg;
1318                 facm= 1.0-fact;
1319                 in[0]= facm*out[0] + fact*fabs(tex[0]-out[0]);
1320                 in[1]= facm*out[1] + fact*fabs(tex[1]-out[1]);
1321                 in[2]= facm*out[2] + fact*fabs(tex[2]-out[2]);
1322                 break;
1323
1324         case MTEX_DARK:
1325                 fact*= facg;
1326                 facm= 1.0-fact;
1327                 
1328                 col= fact*tex[0];
1329                 if(col < out[0]) in[0]= col; else in[0]= out[0];
1330                 col= fact*tex[1];
1331                 if(col < out[1]) in[1]= col; else in[1]= out[1];
1332                 col= fact*tex[2];
1333                 if(col < out[2]) in[2]= col; else in[2]= out[2];
1334                 break;
1335
1336         case MTEX_LIGHT:
1337                 fact*= facg;
1338                 facm= 1.0-fact;
1339                 
1340                 col= fact*tex[0];
1341                 if(col > out[0]) in[0]= col; else in[0]= out[0];
1342                 col= fact*tex[1];
1343                 if(col > out[1]) in[1]= col; else in[1]= out[1];
1344                 col= fact*tex[2];
1345                 if(col > out[2]) in[2]= col; else in[2]= out[2];
1346                 break;
1347                 
1348         case MTEX_BLEND_HUE:
1349                 fact*= facg;
1350                 VECCOPY(in, out);
1351                 ramp_blend(MA_RAMP_HUE, in, in+1, in+2, fact, tex);
1352                 break;
1353         case MTEX_BLEND_SAT:
1354                 fact*= facg;
1355                 VECCOPY(in, out);
1356                 ramp_blend(MA_RAMP_SAT, in, in+1, in+2, fact, tex);
1357                 break;
1358         case MTEX_BLEND_VAL:
1359                 fact*= facg;
1360                 VECCOPY(in, out);
1361                 ramp_blend(MA_RAMP_VAL, in, in+1, in+2, fact, tex);
1362                 break;
1363         case MTEX_BLEND_COLOR:
1364                 fact*= facg;
1365                 VECCOPY(in, out);
1366                 ramp_blend(MA_RAMP_COLOR, in, in+1, in+2, fact, tex);
1367                 break;
1368         }
1369 }
1370
1371 float texture_value_blend(float tex, float out, float fact, float facg, int blendtype, int flip)
1372 {
1373         float in=0.0, facm, col;
1374         
1375         fact*= facg;
1376         facm= 1.0-fact;
1377         if(flip) SWAP(float, fact, facm);
1378
1379         switch(blendtype) {
1380         case MTEX_BLEND:
1381                 in= fact*tex + facm*out;
1382                 break;
1383
1384         case MTEX_MUL:
1385                 facm= 1.0-facg;
1386                 in= (facm+fact*tex)*out;
1387                 break;
1388
1389         case MTEX_SCREEN:
1390                 facm= 1.0-facg;
1391                 in= 1.0-(facm+fact*(1.0-tex))*(1.0-out);
1392                 break;
1393
1394         case MTEX_SUB:
1395                 fact= -fact;
1396         case MTEX_ADD:
1397                 in= fact*tex + out;
1398                 break;
1399
1400         case MTEX_DIV:
1401                 if(tex!=0.0)
1402                         in= facm*out + fact*out/tex;
1403                 break;
1404
1405         case MTEX_DIFF:
1406                 in= facm*out + fact*fabs(tex-out);
1407                 break;
1408
1409         case MTEX_DARK:
1410                 col= fact*tex;
1411                 if(col < out) in= col; else in= out;
1412                 break;
1413
1414         case MTEX_LIGHT:
1415                 col= fact*tex;
1416                 if(col > out) in= col; else in= out;
1417                 break;
1418         }
1419         
1420         return in;
1421 }
1422
1423
1424 void do_material_tex(ShadeInput *shi)
1425 {
1426         MTex *mtex;
1427         Tex *tex;
1428         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
1429         float *co = NULL, *dx = NULL, *dy = NULL;
1430         float fact, facm, factt, facmm, stencilTin=1.0;
1431         float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3], warpvec[3]={0.0f, 0.0f, 0.0f}, Tnor=1.0;
1432         int tex_nr, rgbnor= 0, warpdone=0;
1433
1434         if (R.r.scemode & R_NO_TEX) return;
1435         /* here: test flag if there's a tex (todo) */
1436         
1437         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
1438                 
1439                 /* separate tex switching */
1440                 if(shi->mat->septex & (1<<tex_nr)) continue;
1441                 
1442                 if(shi->mat->mtex[tex_nr]) {
1443                         mtex= shi->mat->mtex[tex_nr];
1444                         
1445                         tex= mtex->tex;
1446                         if(tex==0) continue;
1447
1448                         /* which coords */
1449                         if(mtex->texco==TEXCO_ORCO) {
1450                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
1451                                         co= shi->duplilo; dx= dxt; dy= dyt;
1452                                         dxt[0]= dxt[1]= dxt[2]= 0.0f;
1453                                         dyt[0]= dyt[1]= dyt[2]= 0.0f;
1454                                 }
1455                                 else {
1456                                         co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
1457                                 }
1458                         }
1459                         else if(mtex->texco==TEXCO_STICKY) {
1460                                 co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
1461                         }
1462                         else if(mtex->texco==TEXCO_OBJECT) {
1463                                 Object *ob= mtex->object;
1464                                 if(ob) {
1465                                         co= tempvec;
1466                                         dx= dxt;
1467                                         dy= dyt;
1468                                         VECCOPY(tempvec, shi->co);
1469                                         MTC_Mat4MulVecfl(ob->imat, tempvec);
1470                                         if(shi->osatex) {
1471                                                 VECCOPY(dxt, shi->dxco);
1472                                                 VECCOPY(dyt, shi->dyco);
1473                                                 MTC_Mat4Mul3Vecfl(ob->imat, dxt);
1474                                                 MTC_Mat4Mul3Vecfl(ob->imat, dyt);
1475                                         }
1476                                 }
1477                                 else {
1478                                         /* if object doesn't exist, do not use orcos (not initialized) */
1479                                         co= shi->co;
1480                                         dx= shi->dxco; dy= shi->dyco;
1481                                 }
1482                         }
1483                         else if(mtex->texco==TEXCO_REFL) {
1484                                 co= shi->ref; dx= shi->dxref; dy= shi->dyref;
1485                         }
1486                         else if(mtex->texco==TEXCO_NORM) {
1487                                 co= shi->orn; dx= shi->dxno; dy= shi->dyno;
1488                         }
1489                         else if(mtex->texco==TEXCO_TANGENT) {
1490                                 co= shi->tang; dx= shi->dxno; dy= shi->dyno;
1491                         }
1492                         else if(mtex->texco==TEXCO_GLOB) {
1493                                 co= shi->gl; dx= shi->dxco; dy= shi->dyco;
1494                         }
1495                         else if(mtex->texco==TEXCO_UV) {
1496                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
1497                                         co= shi->dupliuv; dx= dxt; dy= dyt;
1498                                         dxt[0]= dxt[1]= dxt[2]= 0.0f;
1499                                         dyt[0]= dyt[1]= dyt[2]= 0.0f;
1500                                 }
1501                                 else {
1502                                         ShadeInputUV *suv= &shi->uv[shi->actuv];
1503                                         int i;
1504
1505                                         if(mtex->uvname[0] != 0) {
1506                                                 for(i = 0; i < shi->totuv; i++) {
1507                                                         if(strcmp(shi->uv[i].name, mtex->uvname)==0) {
1508                                                                 suv= &shi->uv[i];
1509                                                                 break;
1510                                                         }
1511                                                 }
1512                                         }
1513
1514                                         co= suv->uv;
1515                                         dx= suv->dxuv;
1516                                         dy= suv->dyuv; 
1517                                 }
1518                         }
1519                         else if(mtex->texco==TEXCO_WINDOW) {
1520                                 co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
1521                         }
1522                         else if(mtex->texco==TEXCO_STRAND) {
1523                                 co= tempvec; dx= dxt; dy= dyt;
1524                                 co[0]= shi->strand;
1525                                 co[1]= co[2]= 0.0f;
1526                                 dx[0]= shi->dxstrand;
1527                                 dx[1]= dx[2]= 0.0f;
1528                                 dy[0]= shi->dystrand;
1529                                 dy[1]= dy[2]= 0.0f;
1530                         }
1531                         else if(mtex->texco==TEXCO_STRESS) {
1532                                 co= tempvec; dx= dxt; dy= dyt;
1533                                 co[0]= shi->stress;
1534                                 co[1]= co[2]= 0.0f;
1535                                 dx[0]= 0.0f;
1536                                 dx[1]= dx[2]= 0.0f;
1537                                 dy[0]= 0.0f;
1538                                 dy[1]= dy[2]= 0.0f;
1539                         }
1540                         else continue;  // can happen when texco defines disappear and it renders old files
1541
1542                         /* de pointer defines if bumping happens */
1543                         if(mtex->mapto & (MAP_NORM|MAP_DISPLACE|MAP_WARP)) {
1544                                 texres.nor= norvec;
1545                                 norvec[0]= norvec[1]= norvec[2]= 0.0;
1546                         }
1547                         else texres.nor= NULL;
1548                         
1549                         if(warpdone) {
1550                                 VECADD(tempvec, co, warpvec);
1551                                 co= tempvec;
1552                         }
1553                         
1554                         if(tex->type==TEX_IMAGE) {
1555
1556                                 /* new: first swap coords, then map, then trans/scale */
1557
1558                                 /* placement */
1559                                 if(mtex->projx) texvec[0]= co[mtex->projx-1];
1560                                 else texvec[0]= 0.0;
1561                                 if(mtex->projy) texvec[1]= co[mtex->projy-1];
1562                                 else texvec[1]= 0.0;
1563                                 if(mtex->projz) texvec[2]= co[mtex->projz-1];
1564                                 else texvec[2]= 0.0;
1565
1566                                 if(shi->osatex) {
1567                                         
1568                                         if(mtex->projx) {
1569                                                 dxt[0]= dx[mtex->projx-1];
1570                                                 dyt[0]= dy[mtex->projx-1];
1571                                         }
1572                                         else dxt[0]= dyt[0]= 0.0f;
1573                                         
1574                                         if(mtex->projy) {
1575                                                 dxt[1]= dx[mtex->projy-1];
1576                                                 dyt[1]= dy[mtex->projy-1];
1577                                         }
1578                                         else dxt[1]= dyt[1]= 0.0f;
1579                                         if(mtex->projz) {
1580                                                 dxt[2]= dx[mtex->projz-1];
1581                                                 dyt[2]= dy[mtex->projz-1];
1582                                         }
1583                                         else dxt[2]= dyt[2]= 0.0;
1584                                 }
1585                                 
1586                                 do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
1587
1588                                 /* translate and scale */
1589                                 texvec[0]= mtex->size[0]*(texvec[0]-0.5) +mtex->ofs[0]+0.5;
1590                                 texvec[1]= mtex->size[1]*(texvec[1]-0.5) +mtex->ofs[1]+0.5;
1591                                 if(shi->osatex) {
1592                                         dxt[0]= mtex->size[0]*dxt[0];
1593                                         dxt[1]= mtex->size[1]*dxt[1];
1594                                         dyt[0]= mtex->size[0]*dyt[0];
1595                                         dyt[1]= mtex->size[1]*dyt[1];
1596                                 }
1597                                 
1598                                 /* problem: repeat-mirror is not a 'repeat' but 'extend' in imagetexture.c */
1599                                 if(tex->flag & TEX_REPEAT_XMIR) {
1600                                         if(texvec[0]>1.0f) texvec[0] -= (int)(texvec[0]);
1601                                         else if(texvec[0]<0.0f) texvec[0]+= 1-(int)(texvec[0]);
1602                                 }
1603                                 if(tex->flag & TEX_REPEAT_YMIR) {
1604                                         if(texvec[1]>1.0f) texvec[1] -= (int)(texvec[1]);
1605                                         else if(texvec[1]<0.0f) texvec[1]+= 1-(int)(texvec[1]);
1606                                 }
1607                                 
1608                         }
1609                         else {
1610
1611                                 /* placement */
1612                                 if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
1613                                 else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
1614
1615                                 if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
1616                                 else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
1617
1618                                 if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
1619                                 else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
1620
1621                                 if(shi->osatex) {
1622                                         if(mtex->projx) {
1623                                                 dxt[0]= mtex->size[0]*dx[mtex->projx-1];
1624                                                 dyt[0]= mtex->size[0]*dy[mtex->projx-1];
1625                                         }
1626                                         else dxt[0]= 0.0;
1627                                         if(mtex->projy) {
1628                                                 dxt[1]= mtex->size[1]*dx[mtex->projy-1];
1629                                                 dyt[1]= mtex->size[1]*dy[mtex->projy-1];
1630                                         }
1631                                         else dxt[1]= 0.0;
1632                                         if(mtex->projz) {
1633                                                 dxt[2]= mtex->size[2]*dx[mtex->projz-1];
1634                                                 dyt[2]= mtex->size[2]*dy[mtex->projz-1];
1635                                         }
1636                                         else dxt[2]= 0.0;
1637                                 }
1638                         }
1639
1640                         rgbnor= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres);
1641
1642                         /* texture output */
1643
1644                         if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1645                                 texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1646                                 rgbnor-= TEX_RGB;
1647                         }
1648                         if(mtex->texflag & MTEX_NEGATIVE) {
1649                                 if(rgbnor & TEX_RGB) {
1650                                         texres.tr= 1.0-texres.tr;
1651                                         texres.tg= 1.0-texres.tg;
1652                                         texres.tb= 1.0-texres.tb;
1653                                 }
1654                                 texres.tin= 1.0-texres.tin;
1655                         }
1656                         if(mtex->texflag & MTEX_STENCIL) {
1657                                 if(rgbnor & TEX_RGB) {
1658                                         fact= texres.ta;
1659                                         texres.ta*= stencilTin;
1660                                         stencilTin*= fact;
1661                                 }
1662                                 else {
1663                                         fact= texres.tin;
1664                                         texres.tin*= stencilTin;
1665                                         stencilTin*= fact;
1666                                 }
1667                         }
1668                         else {
1669                                 Tnor*= stencilTin;
1670                         }
1671                         
1672                         if(texres.nor) {
1673                                 if((rgbnor & TEX_NOR)==0) {
1674                                         /* make our own normal */
1675                                         if(rgbnor & TEX_RGB) {
1676                                                 texres.nor[0]= texres.tr;
1677                                                 texres.nor[1]= texres.tg;
1678                                                 texres.nor[2]= texres.tb;
1679                                         }
1680                                         else {
1681                                                 float co= 0.5*cos(texres.tin-0.5);
1682                                                 float si= 0.5*sin(texres.tin-0.5);
1683                                                 float f1, f2;
1684
1685                                                 f1= shi->vn[0];
1686                                                 f2= shi->vn[1];
1687                                                 texres.nor[0]= f1*co+f2*si;
1688                                                 texres.nor[1]= f2*co-f1*si;
1689                                                 f1= shi->vn[1];
1690                                                 f2= shi->vn[2];
1691                                                 texres.nor[1]= f1*co+f2*si;
1692                                                 texres.nor[2]= f2*co-f1*si;
1693                                         }
1694                                 }
1695                                 // warping, local space
1696                                 if(mtex->mapto & MAP_WARP) {
1697                                         warpvec[0]= mtex->warpfac*texres.nor[0];
1698                                         warpvec[1]= mtex->warpfac*texres.nor[1];
1699                                         warpvec[2]= mtex->warpfac*texres.nor[2];
1700                                         warpdone= 1;
1701                                 }
1702 #if 0                           
1703                                 if(mtex->texflag & MTEX_VIEWSPACE) {
1704                                         // rotate to global coords
1705                                         if(mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
1706                                                 if(shi->vlr && shi->obr->ob) {
1707                                                         float len= Normalize(texres.nor);
1708                                                         // can be optimized... (ton)
1709                                                         Mat4Mul3Vecfl(shi->obr->ob->obmat, texres.nor);
1710                                                         Mat4Mul3Vecfl(R.viewmat, texres.nor);
1711                                                         Normalize(texres.nor);
1712                                                         VecMulf(texres.nor, len);
1713                                                 }
1714                                         }
1715                                 }
1716 #endif                          
1717                         }
1718
1719                         /* mapping */
1720                         if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
1721                                 float tcol[3], colfac;
1722                                 
1723                                 /* stencil maps on the texture control slider, not texture intensity value */
1724                                 colfac= mtex->colfac*stencilTin;
1725                                 
1726                                 tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
1727                                 
1728                                 if((rgbnor & TEX_RGB)==0) {
1729                                         tcol[0]= mtex->r;
1730                                         tcol[1]= mtex->g;
1731                                         tcol[2]= mtex->b;
1732                                 }
1733                                 else if(mtex->mapto & MAP_ALPHA) {
1734                                         texres.tin= stencilTin;
1735                                 }
1736                                 else texres.tin= texres.ta;
1737                                 
1738                                 if(mtex->mapto & MAP_COL) {
1739                                         texture_rgb_blend(&shi->r, tcol, &shi->r, texres.tin, colfac, mtex->blendtype);
1740                                 }
1741                                 if(mtex->mapto & MAP_COLSPEC) {
1742                                         texture_rgb_blend(&shi->specr, tcol, &shi->specr, texres.tin, colfac, mtex->blendtype);
1743                                 }
1744                                 if(mtex->mapto & MAP_COLMIR) {
1745                                         // exception for envmap only
1746                                         if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
1747                                                 fact= texres.tin*colfac;
1748                                                 facm= 1.0- fact;
1749                                                 shi->refcol[0]= fact + facm*shi->refcol[0];
1750                                                 shi->refcol[1]= fact*tcol[0] + facm*shi->refcol[1];
1751                                                 shi->refcol[2]= fact*tcol[1] + facm*shi->refcol[2];
1752                                                 shi->refcol[3]= fact*tcol[2] + facm*shi->refcol[3];
1753                                         }
1754                                         else {
1755                                                 texture_rgb_blend(&shi->mirr, tcol, &shi->mirr, texres.tin, colfac, mtex->blendtype);
1756                                         }
1757                                 }
1758                         }
1759                         if( (mtex->mapto & MAP_NORM) ) {
1760                                 if(texres.nor) {
1761                                         if(mtex->maptoneg & MAP_NORM) tex->norfac= -mtex->norfac;
1762                                         else tex->norfac= mtex->norfac;
1763                                         
1764                                         /* we need to code blending modes for normals too once.. now 1 exception hardcoded */
1765                                         
1766                                         if ((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP)) {
1767                                                 /* qdn: for normalmaps, to invert the normalmap vector,
1768                                                    it is better to negate x & y instead of subtracting the vector as was done before */
1769                                                 tex->norfac = mtex->norfac;
1770                                                 if (mtex->maptoneg & MAP_NORM) {
1771                                                         texres.nor[0] = -texres.nor[0];
1772                                                         texres.nor[1] = -texres.nor[1];
1773                                                 }
1774                                                 fact = Tnor*tex->norfac;
1775                                                 if (fact>1.f) fact = 1.f;
1776                                                 facm = 1.f-fact;
1777                                                 if(mtex->normapspace == MTEX_NSPACE_TANGENT) {
1778                                                         /* qdn: tangent space */
1779                                                         float B[3], tv[3];
1780                                                         Crossf(B, shi->vn, shi->tang);  /* bitangent */
1781                                                         /* transform norvec from tangent space to object surface in camera space */
1782                                                         tv[0] = texres.nor[0]*shi->tang[0] + texres.nor[1]*B[0] + texres.nor[2]*shi->vn[0];
1783                                                         tv[1] = texres.nor[0]*shi->tang[1] + texres.nor[1]*B[1] + texres.nor[2]*shi->vn[1];
1784                                                         tv[2] = texres.nor[0]*shi->tang[2] + texres.nor[1]*B[2] + texres.nor[2]*shi->vn[2];
1785                                                         shi->vn[0]= facm*shi->vn[0] + fact*tv[0];
1786                                                         shi->vn[1]= facm*shi->vn[1] + fact*tv[1];
1787                                                         shi->vn[2]= facm*shi->vn[2] + fact*tv[2];
1788                                                 }
1789                                                 else {
1790                                                         float nor[3];
1791
1792                                                         VECCOPY(nor, texres.nor);
1793
1794                                                         if(mtex->normapspace == MTEX_NSPACE_CAMERA);
1795                                                         else if(mtex->normapspace == MTEX_NSPACE_WORLD) {
1796                                                                 Mat4Mul3Vecfl(R.viewmat, nor);
1797                                                         }
1798                                                         else if(mtex->normapspace == MTEX_NSPACE_OBJECT) {
1799                                                                 if(shi->vlr && shi->vlr->obr->ob)
1800                                                                         Mat4Mul3Vecfl(shi->vlr->obr->ob->obmat, nor);
1801                                                                 Mat4Mul3Vecfl(R.viewmat, nor);
1802                                                         }
1803
1804                                                         Normalize(nor);
1805
1806                                                         /* qdn: worldspace */
1807                                                         shi->vn[0]= facm*shi->vn[0] + fact*nor[0];
1808                                                         shi->vn[1]= facm*shi->vn[1] + fact*nor[1];
1809                                                         shi->vn[2]= facm*shi->vn[2] + fact*nor[2];
1810                                                 }
1811                                         }
1812                                         else {
1813                                                 if(shi->mat->mode & MA_TANGENT_V) {
1814                                                         shi->tang[0]+= Tnor*tex->norfac*texres.nor[0];
1815                                                         shi->tang[1]+= Tnor*tex->norfac*texres.nor[1];
1816                                                         shi->tang[2]+= Tnor*tex->norfac*texres.nor[2];
1817                                                 }
1818                                                 else {
1819                                                         float nor[3], dot;
1820                                                         /* prevent bump to become negative normal */
1821                                                         nor[0]= Tnor*tex->norfac*texres.nor[0];
1822                                                         nor[1]= Tnor*tex->norfac*texres.nor[1];
1823                                                         nor[2]= Tnor*tex->norfac*texres.nor[2];
1824                                                         
1825                                                         dot= 0.5f + 0.5f*INPR(nor, shi->vn);
1826                                                         
1827                                                         shi->vn[0]+= dot*nor[0];
1828                                                         shi->vn[1]+= dot*nor[1];
1829                                                         shi->vn[2]+= dot*nor[2];
1830                                                 }
1831                                         }                                       
1832                                         Normalize(shi->vn);
1833                                         
1834                                         /* this makes sure the bump is passed on to the next texture */
1835                                         shi->orn[0]= -shi->vn[0];
1836                                         shi->orn[1]= -shi->vn[1];
1837                                         shi->orn[2]= -shi->vn[2];
1838                                         
1839                                         /* reflection vector */
1840                                         calc_R_ref(shi);
1841                                 }
1842                         }
1843
1844                         if( mtex->mapto & MAP_DISPLACE ) {
1845                                 /* Now that most textures offer both Nor and Intensity, allow  */
1846                                 /* both to work, and let user select with slider.   */
1847                                 if(texres.nor) {
1848                                         if(mtex->maptoneg & MAP_DISPLACE) tex->norfac= -mtex->norfac;
1849                                         else tex->norfac= mtex->norfac;
1850
1851                                         shi->displace[0]+= 0.2f*Tnor*tex->norfac*texres.nor[0];
1852                                         shi->displace[1]+= 0.2f*Tnor*tex->norfac*texres.nor[1];
1853                                         shi->displace[2]+= 0.2f*Tnor*tex->norfac*texres.nor[2];
1854                                 }
1855                                 
1856                                 if(rgbnor & TEX_RGB) {
1857                                         if(texres.talpha) texres.tin= texres.ta;
1858                                         else texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
1859                                 }
1860
1861                                 if(mtex->maptoneg & MAP_DISPLACE) {
1862                                         factt= (texres.tin-0.5f)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
1863                                 }
1864                                 else {
1865                                         factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
1866                                 }
1867
1868                                 if(mtex->blendtype==MTEX_BLEND) {
1869                                         shi->displace[0]= factt*shi->vn[0] + facmm*shi->displace[0];
1870                                         shi->displace[1]= factt*shi->vn[1] + facmm*shi->displace[1];
1871                                         shi->displace[2]= factt*shi->vn[2] + facmm*shi->displace[2];
1872                                 }
1873                                 else if(mtex->blendtype==MTEX_MUL) {
1874                                         shi->displace[0]*= factt*shi->vn[0];
1875                                         shi->displace[1]*= factt*shi->vn[1];
1876                                         shi->displace[2]*= factt*shi->vn[2];
1877                                 }
1878                                 else { /* add or sub */
1879                                         if(mtex->blendtype==MTEX_SUB) factt= -factt;
1880                                         else factt= factt;
1881                                         shi->displace[0]+= factt*shi->vn[0];
1882                                         shi->displace[1]+= factt*shi->vn[1];
1883                                         shi->displace[2]+= factt*shi->vn[2];
1884                                 }
1885                         }
1886
1887                         if(mtex->mapto & MAP_VARS) {
1888                                 /* stencil maps on the texture control slider, not texture intensity value */
1889                                 float varfac= mtex->varfac*stencilTin;
1890                                 
1891                                 if(rgbnor & TEX_RGB) {
1892                                         if(texres.talpha) texres.tin= texres.ta;
1893                                         else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
1894                                 }
1895
1896                                 if(mtex->mapto & MAP_REF) {
1897                                         int flip= mtex->maptoneg & MAP_REF;
1898
1899                                         shi->refl= texture_value_blend(mtex->def_var, shi->refl, texres.tin, varfac, mtex->blendtype, flip);
1900                                         if(shi->refl<0.0) shi->refl= 0.0;
1901                                 }
1902                                 if(mtex->mapto & MAP_SPEC) {
1903                                         int flip= mtex->maptoneg & MAP_SPEC;
1904                                         
1905                                         shi->spec= texture_value_blend(mtex->def_var, shi->spec, texres.tin, varfac, mtex->blendtype, flip);
1906                                         if(shi->spec<0.0) shi->spec= 0.0;
1907                                 }
1908                                 if(mtex->mapto & MAP_EMIT) {
1909                                         int flip= mtex->maptoneg & MAP_EMIT;
1910
1911                                         shi->emit= texture_value_blend(mtex->def_var, shi->emit, texres.tin, varfac, mtex->blendtype, flip);
1912                                         if(shi->emit<0.0) shi->emit= 0.0;
1913                                 }
1914                                 if(mtex->mapto & MAP_ALPHA) {
1915                                         int flip= mtex->maptoneg & MAP_ALPHA;
1916
1917                                         shi->alpha= texture_value_blend(mtex->def_var, shi->alpha, texres.tin, varfac, mtex->blendtype, flip);
1918                                         if(shi->alpha<0.0) shi->alpha= 0.0;
1919                                         else if(shi->alpha>1.0) shi->alpha= 1.0;
1920                                 }
1921                                 if(mtex->mapto & MAP_HAR) {
1922                                         int flip= mtex->maptoneg & MAP_HAR;
1923                                         float har;  // have to map to 0-1
1924                                         
1925                                         har= ((float)shi->har)/128.0;
1926                                         har= 128.0*texture_value_blend(mtex->def_var, har, texres.tin, varfac, mtex->blendtype, flip);
1927                                         
1928                                         if(har<1.0) shi->har= 1; 
1929                                         else if(har>511.0) shi->har= 511;
1930                                         else shi->har= (int)har;
1931                                 }
1932                                 if(mtex->mapto & MAP_RAYMIRR) {
1933                                         int flip= mtex->maptoneg & MAP_RAYMIRR;
1934
1935                                         shi->ray_mirror= texture_value_blend(mtex->def_var, shi->ray_mirror, texres.tin, varfac, mtex->blendtype, flip);
1936                                         if(shi->ray_mirror<0.0) shi->ray_mirror= 0.0;
1937                                         else if(shi->ray_mirror>1.0) shi->ray_mirror= 1.0;
1938                                 }
1939                                 if(mtex->mapto & MAP_TRANSLU) {
1940                                         int flip= mtex->maptoneg & MAP_TRANSLU;
1941
1942                                         shi->translucency= texture_value_blend(mtex->def_var, shi->translucency, texres.tin, varfac, mtex->blendtype, flip);
1943                                         if(shi->translucency<0.0) shi->translucency= 0.0;
1944                                         else if(shi->translucency>1.0) shi->translucency= 1.0;
1945                                 }
1946                                 if(mtex->mapto & MAP_LAYER) {
1947                                         int flip= mtex->maptoneg & MAP_LAYER;
1948                                         
1949                                         shi->layerfac= texture_value_blend(mtex->def_var, shi->layerfac, texres.tin, varfac, mtex->blendtype, flip);
1950                                         if(shi->layerfac<0.0) shi->layerfac= 0.0;
1951                                         else if(shi->layerfac>1.0) shi->layerfac= 1.0;
1952                                 }
1953                                 if(mtex->mapto & MAP_AMB) {
1954                                         int flip= mtex->maptoneg & MAP_AMB;
1955
1956                                         shi->amb= texture_value_blend(mtex->def_var, shi->amb, texres.tin, varfac, mtex->blendtype, flip);
1957                                         if(shi->amb<0.0) shi->amb= 0.0;
1958                                         else if(shi->amb>1.0) shi->amb= 1.0;
1959                                 }
1960                         }
1961                 }
1962         }
1963 }
1964
1965 /* ------------------------------------------------------------------------- */
1966
1967 void do_halo_tex(HaloRen *har, float xn, float yn, float *colf)
1968 {
1969         MTex *mtex;
1970         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
1971         float texvec[3], dxt[3], dyt[3], fact, facm, dx;
1972         int rgb, osatex;
1973
1974         if (R.r.scemode & R_NO_TEX) return;
1975         
1976         mtex= har->mat->mtex[0];
1977         if(mtex->tex==NULL) return;
1978         
1979         /* no normal mapping */
1980         texres.nor= NULL;
1981                 
1982         texvec[0]= xn/har->rad;
1983         texvec[1]= yn/har->rad;
1984         texvec[2]= 0.0;
1985         
1986         osatex= (har->mat->texco & TEXCO_OSA);
1987
1988         /* placement */
1989         if(mtex->projx) texvec[0]= mtex->size[0]*(texvec[mtex->projx-1]+mtex->ofs[0]);
1990         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
1991         
1992         if(mtex->projy) texvec[1]= mtex->size[1]*(texvec[mtex->projy-1]+mtex->ofs[1]);
1993         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
1994         
1995         if(mtex->projz) texvec[2]= mtex->size[2]*(texvec[mtex->projz-1]+mtex->ofs[2]);
1996         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
1997         
1998         if(osatex) {
1999         
2000                 dx= 1.0/har->rad;
2001         
2002                 if(mtex->projx) {
2003                         dxt[0]= mtex->size[0]*dx;
2004                         dyt[0]= mtex->size[0]*dx;
2005                 }
2006                 else dxt[0]= dyt[0]= 0.0;
2007                 
2008                 if(mtex->projy) {
2009                         dxt[1]= mtex->size[1]*dx;
2010                         dyt[1]= mtex->size[1]*dx;
2011                 }
2012                 else dxt[1]= dyt[1]= 0.0;
2013                 
2014                 if(mtex->projz) {
2015                         dxt[2]= 0.0;
2016                         dyt[2]= 0.0;
2017                 }
2018                 else dxt[2]= dyt[2]= 0.0;
2019
2020         }
2021
2022         if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2023         
2024         rgb= multitex(mtex->tex, texvec, dxt, dyt, osatex, &texres);
2025
2026         /* texture output */
2027         if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2028                 texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2029                 rgb= 0;
2030         }
2031         if(mtex->texflag & MTEX_NEGATIVE) {
2032                 if(rgb) {
2033                         texres.tr= 1.0-texres.tr;
2034                         texres.tg= 1.0-texres.tg;
2035                         texres.tb= 1.0-texres.tb;
2036                 }
2037                 else texres.tin= 1.0-texres.tin;
2038         }
2039
2040         /* mapping */
2041         if(mtex->mapto & MAP_COL) {
2042                 
2043                 if(rgb==0) {
2044                         texres.tr= mtex->r;
2045                         texres.tg= mtex->g;
2046                         texres.tb= mtex->b;
2047                 }
2048                 else if(mtex->mapto & MAP_ALPHA) {
2049                         texres.tin= 1.0;
2050                 }
2051                 else texres.tin= texres.ta;
2052
2053                 fact= texres.tin*mtex->colfac;
2054                 facm= 1.0-fact;
2055                 
2056                 if(mtex->blendtype==MTEX_MUL) {
2057                         facm= 1.0-mtex->colfac;
2058                 }
2059                 
2060                 if(mtex->blendtype==MTEX_SUB) fact= -fact;
2061
2062                 if(mtex->blendtype==MTEX_BLEND) {
2063                         colf[0]= (fact*texres.tr + facm*har->r);
2064                         colf[1]= (fact*texres.tg + facm*har->g);
2065                         colf[2]= (fact*texres.tb + facm*har->b);
2066                 }
2067                 else if(mtex->blendtype==MTEX_MUL) {
2068                         colf[0]= (facm+fact*texres.tr)*har->r;
2069                         colf[1]= (facm+fact*texres.tg)*har->g;
2070                         colf[2]= (facm+fact*texres.tb)*har->b;
2071                 }
2072                 else {
2073                         colf[0]= (fact*texres.tr + har->r);
2074                         colf[1]= (fact*texres.tg + har->g);
2075                         colf[2]= (fact*texres.tb + har->b);
2076                         
2077                         CLAMP(colf[0], 0.0, 1.0);
2078                         CLAMP(colf[1], 0.0, 1.0);
2079                         CLAMP(colf[2], 0.0, 1.0);
2080                 }
2081         }
2082         if(mtex->mapto & MAP_ALPHA) {
2083                 if(rgb) {
2084                         if(texres.talpha) texres.tin= texres.ta;
2085                         else texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2086                 }
2087                                 
2088                 colf[3]*= texres.tin;
2089         }
2090 }
2091
2092 /* ------------------------------------------------------------------------- */
2093
2094 /* hor and zen are RGB vectors, blend is 1 float, should all be initialized */
2095 void do_sky_tex(float *rco, float *lo, float *dxyview, float *hor, float *zen, float *blend, int skyflag)
2096 {
2097         MTex *mtex;
2098         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2099         float *co, fact, stencilTin=1.0;
2100         float tempvec[3], texvec[3], dxt[3], dyt[3];
2101         int tex_nr, rgb= 0, ok;
2102         
2103         if (R.r.scemode & R_NO_TEX) return;
2104         /* todo: add flag to test if there's a tex */
2105         texres.nor= NULL;
2106         
2107         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2108                 if(R.wrld.mtex[tex_nr]) {
2109                         mtex= R.wrld.mtex[tex_nr];
2110                         
2111                         if(mtex->tex==0) continue;
2112                         /* if(mtex->mapto==0) continue; */
2113                         
2114                         /* which coords */
2115                         co= lo;
2116                         
2117                         /* dxt dyt just from 1 value */
2118                         if(dxyview) {
2119                                 dxt[0]= dxt[1]= dxt[2]= dxyview[0];
2120                                 dyt[0]= dyt[1]= dyt[2]= dxyview[1];
2121                         }
2122                         else {
2123                                 dxt[0]= dxt[1]= dxt[2]= 0.0;
2124                                 dyt[0]= dyt[1]= dyt[2]= 0.0;
2125                         }
2126                         
2127                         /* Grab the mapping settings for this texture */
2128                         switch(mtex->texco) {
2129                         case TEXCO_ANGMAP:
2130                                 /* only works with texture being "real" */
2131                                 fact= (1.0/M_PI)*acos(lo[2])/(sqrt(lo[0]*lo[0] + lo[1]*lo[1])); 
2132                                 tempvec[0]= lo[0]*fact;
2133                                 tempvec[1]= lo[1]*fact;
2134                                 tempvec[2]= 0.0;
2135                                 co= tempvec;
2136                                 break;
2137                                 
2138                         case TEXCO_H_SPHEREMAP:
2139                         case TEXCO_H_TUBEMAP:
2140                                 if(skyflag & WO_ZENUP) {
2141                                         if(mtex->texco==TEXCO_H_TUBEMAP) tubemap(lo[0], lo[2], lo[1], tempvec, tempvec+1);
2142                                         else spheremap(lo[0], lo[2], lo[1], tempvec, tempvec+1);
2143                                         /* tube/spheremap maps for outside view, not inside */
2144                                         tempvec[0]= 1.0-tempvec[0];
2145                                         /* only top half */
2146                                         tempvec[1]= 2.0*tempvec[1]-1.0;
2147                                         tempvec[2]= 0.0;
2148                                         /* and correction for do_2d_mapping */
2149                                         tempvec[0]= 2.0*tempvec[0]-1.0;
2150                                         tempvec[1]= 2.0*tempvec[1]-1.0;
2151                                         co= tempvec;
2152                                 }
2153                                 else {
2154                                         /* potentially dangerous... check with multitex! */
2155                                         continue;
2156                                 }
2157                                 break;
2158                         case TEXCO_OBJECT:
2159                                 if(mtex->object) {
2160                                         VECCOPY(tempvec, lo);
2161                                         MTC_Mat4MulVecfl(mtex->object->imat, tempvec);
2162                                         co= tempvec;
2163                                 }
2164                                 break;
2165                                 
2166                         case TEXCO_GLOB:
2167                                 if(rco) {
2168                                         VECCOPY(tempvec, rco);
2169                                         MTC_Mat4MulVecfl(R.viewinv, tempvec);
2170                                         co= tempvec;
2171                                 }
2172                                 else
2173                                         co= lo;
2174                                 
2175 //                              VECCOPY(shi->dxgl, shi->dxco);
2176 //                              MTC_Mat3MulVecfl(R.imat, shi->dxco);
2177 //                              VECCOPY(shi->dygl, shi->dyco);
2178 //                              MTC_Mat3MulVecfl(R.imat, shi->dyco);
2179                                 break;
2180                         }
2181                         
2182                         /* placement */                 
2183                         if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2184                         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2185                         
2186                         if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
2187                         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2188                         
2189                         if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
2190                         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2191                         
2192                         /* texture */
2193                         if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2194                 
2195                         rgb= multitex(mtex->tex, texvec, dxt, dyt, R.osa, &texres);
2196                         
2197                         /* texture output */
2198                         if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2199                                 texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2200                                 rgb= 0;
2201                         }
2202                         if(mtex->texflag & MTEX_NEGATIVE) {
2203                                 if(rgb) {
2204                                         texres.tr= 1.0-texres.tr;
2205                                         texres.tg= 1.0-texres.tg;
2206                                         texres.tb= 1.0-texres.tb;
2207                                 }
2208                                 else texres.tin= 1.0-texres.tin;
2209                         }
2210                         if(mtex->texflag & MTEX_STENCIL) {
2211                                 if(rgb) {
2212                                         fact= texres.ta;
2213                                         texres.ta*= stencilTin;
2214                                         stencilTin*= fact;
2215                                 }
2216                                 else {
2217                                         fact= texres.tin;
2218                                         texres.tin*= stencilTin;
2219                                         stencilTin*= fact;
2220                                 }
2221                         }
2222                         else {
2223                                 if(rgb) texres.ta *= stencilTin;
2224                                 else texres.tin*= stencilTin;
2225                         }
2226                         
2227                         /* color mapping */
2228                         if(mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
2229                                 float tcol[3];
2230                                 
2231                                 if(rgb==0) {
2232                                         texres.tr= mtex->r;
2233                                         texres.tg= mtex->g;
2234                                         texres.tb= mtex->b;
2235                                 }
2236                                 else texres.tin= texres.ta;
2237                                 
2238                                 tcol[0]= texres.tr; tcol[1]= texres.tg; tcol[2]= texres.tb;
2239
2240                                 if(mtex->mapto & WOMAP_HORIZ) {
2241                                         texture_rgb_blend(hor, tcol, hor, texres.tin, mtex->colfac, mtex->blendtype);
2242                                 }
2243                                 if(mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
2244                                         ok= 0;
2245                                         if(R.wrld.skytype & WO_SKYREAL) {
2246                                                 if((skyflag & WO_ZENUP)) {
2247                                                         if(mtex->mapto & WOMAP_ZENUP) ok= 1;
2248                                                 }
2249                                                 else if(mtex->mapto & WOMAP_ZENDOWN) ok= 1;
2250                                         }
2251                                         else ok= 1;
2252                                         
2253                                         if(ok) {
2254                                                 texture_rgb_blend(zen, tcol, zen, texres.tin, mtex->colfac, mtex->blendtype);
2255                                         }
2256                                 }
2257                         }
2258                         if(mtex->mapto & WOMAP_BLEND) {
2259                                 if(rgb) texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2260                                 
2261                                 *blend= texture_value_blend(mtex->def_var, *blend, texres.tin, mtex->varfac, mtex->blendtype, 0);
2262                         }
2263                 }
2264         }
2265 }
2266
2267 /* ------------------------------------------------------------------------- */
2268 /* colf supposed to be initialized with la->r,g,b */
2269
2270 void do_lamp_tex(LampRen *la, float *lavec, ShadeInput *shi, float *colf)
2271 {
2272         Object *ob;
2273         MTex *mtex;
2274         Tex *tex;
2275         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2276         float *co = NULL, *dx = NULL, *dy = NULL, fact, stencilTin=1.0;
2277         float texvec[3], dxt[3], dyt[3], tempvec[3];
2278         int tex_nr, rgb= 0;
2279         
2280         if (R.r.scemode & R_NO_TEX) return;
2281         tex_nr= 0;
2282         
2283         for(; tex_nr<MAX_MTEX; tex_nr++) {
2284                 
2285                 if(la->mtex[tex_nr]) {
2286                         mtex= la->mtex[tex_nr];
2287                         
2288                         tex= mtex->tex;
2289                         if(tex==NULL) continue;
2290                         texres.nor= NULL;
2291                         
2292                         /* which coords */
2293                         if(mtex->texco==TEXCO_OBJECT) {
2294                                 ob= mtex->object;
2295                                 if(ob) {
2296                                         co= tempvec;
2297                                         dx= dxt;
2298                                         dy= dyt;
2299                                         VECCOPY(tempvec, shi->co);
2300                                         MTC_Mat4MulVecfl(ob->imat, tempvec);
2301                                         if(shi->osatex) {
2302                                                 VECCOPY(dxt, shi->dxco);
2303                                                 VECCOPY(dyt, shi->dyco);
2304                                                 MTC_Mat4Mul3Vecfl(ob->imat, dxt);
2305                                                 MTC_Mat4Mul3Vecfl(ob->imat, dyt);
2306                                         }
2307                                 }
2308                                 else {
2309                                         co= shi->co;
2310                                         dx= shi->dxco; dy= shi->dyco;
2311                                 }
2312                         }
2313                         else if(mtex->texco==TEXCO_GLOB) {
2314                                 co= shi->gl; dx= shi->dxco; dy= shi->dyco;
2315                                 VECCOPY(shi->gl, shi->co);
2316                                 MTC_Mat4MulVecfl(R.viewinv, shi->gl);
2317                         }
2318                         else if(mtex->texco==TEXCO_VIEW) {
2319                                 
2320                                 VECCOPY(tempvec, lavec);
2321                                 MTC_Mat3MulVecfl(la->imat, tempvec);
2322                                 
2323                                 if(la->type==LA_SPOT) {
2324                                         tempvec[0]*= la->spottexfac;
2325                                         tempvec[1]*= la->spottexfac;
2326                                 }
2327                                 co= tempvec; 
2328                                 
2329                                 dx= dxt; dy= dyt;       
2330                                 if(shi->osatex) {
2331                                         VECCOPY(dxt, shi->dxlv);
2332                                         VECCOPY(dyt, shi->dylv);
2333                                         /* need some matrix conversion here? la->imat is a [3][3]  matrix!!! **/
2334                                         MTC_Mat3MulVecfl(la->imat, dxt);
2335                                         MTC_Mat3MulVecfl(la->imat, dyt);
2336                                         
2337                                         VecMulf(dxt, la->spottexfac);
2338                                         VecMulf(dyt, la->spottexfac);
2339                                 }
2340                         }
2341                         
2342                         
2343                         /* placement */
2344                         if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2345                         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2346                         
2347                         if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
2348                         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2349                         
2350                         if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
2351                         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2352                         
2353                         if(shi->osatex) {
2354                                 if(mtex->projx) {
2355                                         dxt[0]= mtex->size[0]*dx[mtex->projx-1];
2356                                         dyt[0]= mtex->size[0]*dy[mtex->projx-1];
2357                                 }
2358                                 else dxt[0]= 0.0;
2359                                 if(mtex->projy) {
2360                                         dxt[1]= mtex->size[1]*dx[mtex->projy-1];
2361                                         dyt[1]= mtex->size[1]*dy[mtex->projy-1];
2362                                 }
2363                                 else dxt[1]= 0.0;
2364                                 if(mtex->projx) {
2365                                         dxt[2]= mtex->size[2]*dx[mtex->projz-1];
2366                                         dyt[2]= mtex->size[2]*dy[mtex->projz-1];
2367                                 }
2368                                 else dxt[2]= 0.0;
2369                         }
2370                         
2371                         /* texture */
2372                         if(tex->type==TEX_IMAGE) {
2373                                 do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2374                         }
2375                         
2376                         rgb= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres);
2377
2378                         /* texture output */
2379                         if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
2380                                 texres.tin= (0.35*texres.tr+0.45*texres.tg+0.2*texres.tb);
2381                                 rgb= 0;
2382                         }
2383                         if(mtex->texflag & MTEX_NEGATIVE) {
2384                                 if(rgb) {
2385                                         texres.tr= 1.0-texres.tr;
2386                                         texres.tg= 1.0-texres.tg;
2387                                         texres.tb= 1.0-texres.tb;
2388                                 }
2389                                 else texres.tin= 1.0-texres.tin;
2390                         }
2391                         if(mtex->texflag & MTEX_STENCIL) {
2392                                 if(rgb) {
2393                                         fact= texres.ta;
2394                                         texres.ta*= stencilTin;
2395                                         stencilTin*= fact;
2396                                 }
2397                                 else {
2398                                         fact= texres.tin;
2399                                         texres.tin*= stencilTin;
2400                                         stencilTin*= fact;
2401                                 }
2402                         }
2403                         else {
2404                                 if(rgb) texres.ta*= stencilTin;
2405                                 else texres.tin*= stencilTin;
2406                         }
2407                         
2408                         /* mapping */
2409                         if(mtex->mapto & LAMAP_COL) {
2410                                 float col[3];
2411                                 
2412                                 if(rgb==0) {
2413                                         texres.tr= mtex->r;
2414                                         texres.tg= mtex->g;
2415                                         texres.tb= mtex->b;
2416                                 }
2417                                 else if(mtex->mapto & MAP_ALPHA) {
2418                                         texres.tin= stencilTin;
2419                                 }
2420                                 else texres.tin= texres.ta;
2421
2422                                 /* lamp colors were premultiplied with this */
2423                                 col[0]= texres.tr*la->energy;
2424                                 col[1]= texres.tg*la->energy;
2425                                 col[2]= texres.tb*la->energy;
2426                                 
2427                                 texture_rgb_blend(colf, col, colf, texres.tin, mtex->colfac, mtex->blendtype);
2428                         }
2429                 }
2430         }
2431 }
2432
2433 /* ------------------------------------------------------------------------- */
2434
2435 int externtex(MTex *mtex, float *vec, float *tin, float *tr, float *tg, float *tb, float *ta)
2436 {
2437         Tex *tex;
2438         TexResult texr;
2439         float dxt[3], dyt[3], texvec[3];
2440         int rgb;
2441         
2442         tex= mtex->tex;
2443         if(tex==NULL) return 0;
2444         texr.nor= NULL;
2445         
2446         /* placement */
2447         if(mtex->projx) texvec[0]= mtex->size[0]*(vec[mtex->projx-1]+mtex->ofs[0]);
2448         else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2449         
2450         if(mtex->projy) texvec[1]= mtex->size[1]*(vec[mtex->projy-1]+mtex->ofs[1]);
2451         else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
2452         
2453         if(mtex->projz) texvec[2]= mtex->size[2]*(vec[mtex->projz-1]+mtex->ofs[2]);
2454         else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
2455         
2456         /* texture */
2457         if(tex->type==TEX_IMAGE) {
2458                 do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2459         }
2460         
2461         rgb= multitex(tex, texvec, dxt, dyt, 0, &texr);
2462         
2463         if(rgb) {
2464                 texr.tin= (0.35*texr.tr+0.45*texr.tg+0.2*texr.tb);
2465         }
2466         else {
2467                 texr.tr= mtex->r;
2468                 texr.tg= mtex->g;
2469                 texr.tb= mtex->b;
2470         }
2471         
2472         *tin= texr.tin;
2473         *tr= texr.tr;
2474         *tg= texr.tg;
2475         *tb= texr.tb;
2476         *ta= texr.ta;
2477
2478         return (rgb != 0);
2479 }
2480
2481
2482 /* ------------------------------------------------------------------------- */
2483
2484 void render_realtime_texture(ShadeInput *shi, Image *ima)
2485 {
2486         TexResult texr;
2487         static Tex tex1, tex2;  // threadsafe
2488         static int firsttime= 1;
2489         Tex *tex;
2490         float texvec[3], dx[2], dy[2];
2491         ShadeInputUV *suv= &shi->uv[shi->actuv];
2492
2493         if(firsttime) {
2494                 firsttime= 0;
2495                 default_tex(&tex1);
2496                 default_tex(&tex2);
2497                 tex1.type= TEX_IMAGE;
2498                 tex2.type= TEX_IMAGE;
2499         }
2500         
2501         if(shi->ys & 1) tex= &tex1; else tex= &tex2;    // threadsafe
2502         
2503         texvec[0]= 0.5+0.5*suv->uv[0];
2504         texvec[1]= 0.5+0.5*suv->uv[1];
2505         texvec[2] = 0;  // initalize it because imagewrap looks at it.
2506         if(shi->osatex) {
2507                 dx[0]= 0.5*suv->dxuv[0];
2508                 dx[1]= 0.5*suv->dxuv[1];
2509                 dy[0]= 0.5*suv->dyuv[0];
2510                 dy[1]= 0.5*suv->dyuv[1];
2511         }
2512         
2513         texr.nor= NULL;
2514         
2515         if(shi->osatex) imagewraposa(tex, ima, NULL, texvec, dx, dy, &texr); 
2516         else imagewrap(tex, ima, NULL, texvec, &texr); 
2517         
2518         shi->vcol[0]*= texr.tr;
2519         shi->vcol[1]*= texr.tg;
2520         shi->vcol[2]*= texr.tb;
2521         shi->vcol[3]*= texr.ta;
2522 }
2523
2524 /* eof */