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