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