Cycles: svn merge -r39457:39669 https://svn.blender.org/svnroot/bf-blender/trunk...
[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                 ntreeBeginExecTree(tex->nodetree); /* 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)
148                 ntreeEndExecTree(tex->nodetree);
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                 VECCOPY(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 ];
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                                 VECCOPY(pit->result+5, texres->nor);
759                         } else {
760                                 VECCOPY(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                                         VECCOPY(texres->nor, pit->result+5);
785                                 } else {
786                                         VECCOPY(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                 VECCOPY(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         VECCOPY(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                 VECCOPY(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                 VECCOPY(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                 VECCOPY(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         // XXX tex->use_nodes= 0;
1366         retval= multitex_nodes(tex, texvec, NULL, NULL, 0, texres, 0, 0, NULL, NULL);
1367         // XXX 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, float *tex, float *out, 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                 VECCOPY(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                 VECCOPY(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                 VECCOPY(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                 VECCOPY(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                 VECCOPY(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                 VECCOPY(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[1], 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         float Hscale = Tnor*mtex->norfac;
1908         int dimx=512, dimy=512;
1909
1910         // 2 channels for 2D texture and 3 for 3D textures.
1911         const int nr_channels = (mtex->texco == TEXCO_UV)? 2 : 3;
1912         int c, rgbnor, iBumpSpace;
1913         float dHdx, dHdy;
1914         int found_deriv_map = (tex->type==TEX_IMAGE) && (tex->imaflag & TEX_DERIVATIVEMAP);
1915
1916         // disable internal bump eval in sampler, save pointer
1917         float *nvec = texres->nor;
1918         texres->nor = NULL;
1919
1920         if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
1921                 if(tex->ima)
1922                         Hscale *= 13.0f; // appears to be a sensible default value
1923         } else
1924                 Hscale *= 0.1f; // factor 0.1 proved to look like the previous bump code
1925
1926         if( !ntap_bump->init_done ) {
1927                 VECCOPY(ntap_bump->vNacc, shi->vn);
1928                 VECCOPY(ntap_bump->vNorg, shi->vn);
1929                 ntap_bump->fPrevMagnitude = 1.0f;
1930                 ntap_bump->iPrevBumpSpace = 0;
1931                 
1932                 ntap_bump->init_done = 1;
1933         }
1934
1935         // resolve image dimensions
1936         if(found_deriv_map || (mtex->texflag&MTEX_BUMP_TEXTURESPACE)!=0) {
1937                 ImBuf* ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1938                 if (ibuf) {
1939                         dimx = ibuf->x;
1940                         dimy = ibuf->y;
1941                 }
1942         }
1943         
1944         if(found_deriv_map) {
1945                 float dBdu, dBdv;
1946                 float s = 1;            // negate this if flipped texture coordinate
1947                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1948                 rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, texres);
1949                 
1950                 // this variant using a derivative map is described here
1951                 // http://mmikkelsen3d.blogspot.com/2011/07/derivative-maps.html
1952                 dBdu = Hscale*dimx*(2*texres->tr-1);
1953                 dBdv = Hscale*dimy*(2*texres->tg-1);
1954
1955                 dHdx = dBdu*dxt[0] + s * dBdv*dxt[1];
1956                 dHdy = dBdu*dyt[0] + s * dBdv*dyt[1];
1957         }
1958         else if(!(mtex->texflag & MTEX_5TAP_BUMP)) {
1959                 // compute height derivatives with respect to output image pixel coordinates x and y
1960                 float STll[3], STlr[3], STul[3];
1961                 float Hll, Hlr, Hul;
1962
1963                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1964
1965                 for(c=0; c<nr_channels; c++) {
1966                         // dx contains the derivatives (du/dx, dv/dx)
1967                         // dy contains the derivatives (du/dy, dv/dy)
1968                         STll[c] = texvec[c];
1969                         STlr[c] = texvec[c]+dxt[c];
1970                         STul[c] = texvec[c]+dyt[c];
1971                 }
1972
1973                 // clear unused derivatives
1974                 for(c=nr_channels; c<3; c++) {
1975                         STll[c] = 0.0f;
1976                         STlr[c] = 0.0f;
1977                         STul[c] = 0.0f;
1978                 }
1979
1980                 // use texres for the center sample, set rgbnor
1981                 rgbnor = multitex_mtex(shi, mtex, STll, dxt, dyt, texres);
1982                 Hll = (fromrgb)? RGBTOBW(texres->tr, texres->tg, texres->tb) : texres->tin;
1983
1984                 // use ttexr for the other 2 taps
1985                 multitex_mtex(shi, mtex, STlr, dxt, dyt, &ttexr);
1986                 Hlr = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
1987
1988                 multitex_mtex(shi, mtex, STul, dxt, dyt, &ttexr);
1989                 Hul = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
1990
1991                 dHdx = Hscale*(Hlr - Hll);
1992                 dHdy = Hscale*(Hul - Hll);
1993         }
1994         else {
1995                 /* same as above, but doing 5 taps, increasing quality at cost of speed */
1996                 float STc[3], STl[3], STr[3], STd[3], STu[3];
1997                 float Hc, Hl, Hr, Hd, Hu;
1998
1999                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
2000
2001                 for(c=0; c<nr_channels; c++) {
2002                         STc[c] = texvec[c];
2003                         STl[c] = texvec[c] - 0.5f*dxt[c];
2004                         STr[c] = texvec[c] + 0.5f*dxt[c];
2005                         STd[c] = texvec[c] - 0.5f*dyt[c];
2006                         STu[c] = texvec[c] + 0.5f*dyt[c];
2007                 }
2008
2009                 // clear unused derivatives
2010                 for(c=nr_channels; c<3; c++) {
2011                         STc[c] = 0.0f;
2012                         STl[c] = 0.0f;
2013                         STr[c] = 0.0f;
2014                         STd[c] = 0.0f;
2015                         STu[c] = 0.0f;
2016                 }
2017
2018                 // use texres for the center sample, set rgbnor
2019                 rgbnor = multitex_mtex(shi, mtex, STc, dxt, dyt, texres);
2020                 Hc = (fromrgb)? RGBTOBW(texres->tr, texres->tg, texres->tb) : texres->tin;
2021
2022                 // use ttexr for the other taps
2023                 multitex_mtex(shi, mtex, STl, dxt, dyt, &ttexr);
2024                 Hl = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2025                 multitex_mtex(shi, mtex, STr, dxt, dyt, &ttexr);
2026                 Hr = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2027                 multitex_mtex(shi, mtex, STd, dxt, dyt, &ttexr);
2028                 Hd = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2029                 multitex_mtex(shi, mtex, STu, dxt, dyt, &ttexr);
2030                 Hu = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2031
2032                 dHdx = Hscale*(Hr - Hl);
2033                 dHdy = Hscale*(Hu - Hd);
2034         }
2035
2036         // restore pointer
2037         texres->nor = nvec;
2038
2039         /* replaced newbump with code based on listing 1 and 2 of
2040                 [Mik10] Mikkelsen M. S.: Bump Mapping Unparametrized Surfaces on the GPU.
2041                 -> http://jbit.net/~sparky/sfgrad_bump/mm_sfgrad_bump.pdf */
2042
2043         if( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
2044                 iBumpSpace = 1;
2045         else if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
2046                 iBumpSpace = 2;
2047         else
2048                 iBumpSpace = 4; // ViewSpace
2049         
2050         if( ntap_bump->iPrevBumpSpace != iBumpSpace ) {
2051                 
2052                 // initialize normal perturbation vectors
2053                 int xyz;
2054                 float fDet, abs_fDet, fMagnitude;
2055                 // object2view and inverted matrix
2056                 float obj2view[3][3], view2obj[3][3], tmp[4][4];
2057                 // local copies of derivatives and normal
2058                 float dPdx[3], dPdy[3], vN[3];
2059                 VECCOPY(dPdx, shi->dxco);
2060                 VECCOPY(dPdy, shi->dyco);
2061                 VECCOPY(vN, ntap_bump->vNorg);
2062                 
2063                 if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
2064                         // TODO: these calculations happen for every pixel!
2065                         //      -> move to shi->obi
2066                         mul_m4_m4m4(tmp, shi->obr->ob->obmat, R.viewmat);
2067                         copy_m3_m4(obj2view, tmp); // use only upper left 3x3 matrix
2068                         invert_m3_m3(view2obj, obj2view);
2069                 
2070                         // generate the surface derivatives in object space
2071                         mul_m3_v3(view2obj, dPdx);
2072                         mul_m3_v3( view2obj, dPdy );
2073                         // generate the unit normal in object space
2074                         mul_transposed_m3_v3( obj2view, vN );
2075                         normalize_v3(vN);
2076                 }
2077                 
2078                 cross_v3_v3v3(ntap_bump->vR1, dPdy, vN);
2079                 cross_v3_v3v3(ntap_bump->vR2, vN, dPdx);
2080                 fDet = dot_v3v3(dPdx, ntap_bump->vR1);
2081                 ntap_bump->sgn_det = (fDet < 0)? -1.0f: 1.0f;
2082                 abs_fDet = ntap_bump->sgn_det * fDet;
2083
2084                 if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
2085                         if(tex->ima) {
2086                                 // crazy hack solution that gives results similar to normal mapping - part 1
2087                                 normalize_v3(ntap_bump->vR1);
2088                                 normalize_v3(ntap_bump->vR2);
2089                                 abs_fDet = 1.0f;
2090                         }
2091                 }
2092                 
2093                 fMagnitude = abs_fDet;
2094                 if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
2095                         // pre do transform of texres->nor by the inverse transposed of obj2view
2096                         mul_transposed_m3_v3( view2obj, vN );
2097                         mul_transposed_m3_v3( view2obj, ntap_bump->vR1 );
2098                         mul_transposed_m3_v3( view2obj, ntap_bump->vR2 );
2099                         
2100                         fMagnitude *= len_v3(vN);
2101                 }
2102                 
2103                 for(xyz=0; xyz<3; xyz++)
2104                                 ntap_bump->vNacc[xyz] *= fMagnitude / ntap_bump->fPrevMagnitude;
2105                 
2106                 ntap_bump->fPrevMagnitude = fMagnitude;
2107                 ntap_bump->iPrevBumpSpace = iBumpSpace;
2108         }
2109
2110         if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
2111                 if(tex->ima) {
2112                         // crazy hack solution that gives results similar to normal mapping - part 2
2113                         float vec[2];
2114                         
2115                         vec[0] = dimx*dxt[0];
2116                         vec[1] = dimy*dxt[1];
2117                         dHdx *= 1.0f/len_v2(vec);
2118                         vec[0] = dimx*dyt[0];
2119                         vec[1] = dimy*dyt[1];
2120                         dHdy *= 1.0f/len_v2(vec);
2121                 }
2122         }
2123         
2124         // subtract the surface gradient from vNacc
2125         for(c=0; c<3; c++) {
2126                 float vSurfGrad_compi = ntap_bump->sgn_det * (dHdx * ntap_bump->vR1[c] + dHdy * ntap_bump->vR2[c]);
2127                 ntap_bump->vNacc[c] -= vSurfGrad_compi;
2128                 texres->nor[c] = ntap_bump->vNacc[c]; // copy
2129         }
2130
2131         rgbnor |= TEX_NOR;
2132         return rgbnor;
2133 }
2134
2135 void do_material_tex(ShadeInput *shi)
2136 {
2137         CompatibleBump compat_bump;
2138         NTapBump ntap_bump;
2139         MTex *mtex;
2140         Tex *tex;
2141         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2142         float *co = NULL, *dx = NULL, *dy = NULL;
2143         float fact, facm, factt, facmm, stencilTin=1.0;
2144         float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3], warpvec[3]={0.0f, 0.0f, 0.0f}, Tnor=1.0;
2145         int tex_nr, rgbnor= 0, warpdone=0;
2146         int use_compat_bump = 0, use_ntap_bump = 0;
2147         int found_nmapping = 0, found_deriv_map = 0;
2148         int iFirstTimeNMap=1;
2149
2150         compatible_bump_init(&compat_bump);
2151         ntap_bump_init(&ntap_bump);
2152
2153         if (R.r.scemode & R_NO_TEX) return;
2154         /* here: test flag if there's a tex (todo) */
2155
2156         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2157                 
2158                 /* separate tex switching */
2159                 if(shi->mat->septex & (1<<tex_nr)) continue;
2160                 
2161                 if(shi->mat->mtex[tex_nr]) {
2162                         mtex= shi->mat->mtex[tex_nr];
2163                         
2164                         tex= mtex->tex;
2165                         if(tex==0) continue;
2166
2167                         found_deriv_map = (tex->type==TEX_IMAGE) && (tex->imaflag & TEX_DERIVATIVEMAP);
2168                         use_compat_bump= (mtex->texflag & MTEX_COMPAT_BUMP);
2169                         use_ntap_bump= ((mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP))!=0 || found_deriv_map!=0) ? 1 : 0;
2170
2171                         /* XXX texture node trees don't work for this yet */
2172                         if(tex->nodetree && tex->use_nodes) {
2173                                 use_compat_bump = 0;
2174                                 use_ntap_bump = 0;
2175                         }
2176                         
2177                         /* case displacement mapping */
2178                         if(shi->osatex==0 && use_ntap_bump) {
2179                                 use_ntap_bump = 0;
2180                                 use_compat_bump = 1;
2181                         }
2182
2183                         /* which coords */
2184                         if(mtex->texco==TEXCO_ORCO) {
2185                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2186                                         co= shi->duplilo; dx= dxt; dy= dyt;
2187                                         dxt[0]= dxt[1]= dxt[2]= 0.0f;
2188                                         dyt[0]= dyt[1]= dyt[2]= 0.0f;
2189                                 }
2190                                 else {
2191                                         co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
2192                                 }
2193                         }
2194                         else if(mtex->texco==TEXCO_STICKY) {
2195                                 co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
2196                         }
2197                         else if(mtex->texco==TEXCO_OBJECT) {
2198                                 Object *ob= mtex->object;
2199                                 if(ob) {
2200                                         co= tempvec;
2201                                         dx= dxt;
2202                                         dy= dyt;
2203                                         VECCOPY(tempvec, shi->co);
2204                                         if(mtex->texflag & MTEX_OB_DUPLI_ORIG)
2205                                                 if(shi->obi && shi->obi->duplitexmat)
2206                                                         mul_m4_v3(shi->obi->duplitexmat, tempvec);
2207                                         mul_m4_v3(ob->imat_ren, tempvec);
2208                                         if(shi->osatex) {
2209                                                 VECCOPY(dxt, shi->dxco);
2210                                                 VECCOPY(dyt, shi->dyco);
2211                                                 mul_mat3_m4_v3(ob->imat_ren, dxt);
2212                                                 mul_mat3_m4_v3(ob->imat_ren, dyt);
2213                                         }
2214                                 }
2215                                 else {
2216                                         /* if object doesn't exist, do not use orcos (not initialized) */
2217                                         co= shi->co;
2218                                         dx= shi->dxco; dy= shi->dyco;
2219                                 }
2220                         }
2221                         else if(mtex->texco==TEXCO_REFL) {
2222                                 calc_R_ref(shi);
2223                                 co= shi->ref; dx= shi->dxref; dy= shi->dyref;
2224                         }
2225                         else if(mtex->texco==TEXCO_NORM) {
2226                                 co= shi->orn; dx= shi->dxno; dy= shi->dyno;
2227                         }
2228                         else if(mtex->texco==TEXCO_TANGENT) {
2229                                 co= shi->tang; dx= shi->dxno; dy= shi->dyno;
2230                         }
2231                         else if(mtex->texco==TEXCO_GLOB) {
2232                                 co= shi->gl; dx= shi->dxgl; dy= shi->dygl;
2233                         }
2234                         else if(mtex->texco==TEXCO_UV) {
2235                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2236                                         co= shi->dupliuv; dx= dxt; dy= dyt;
2237                                         dxt[0]= dxt[1]= dxt[2]= 0.0f;
2238                                         dyt[0]= dyt[1]= dyt[2]= 0.0f;
2239                                 }
2240                                 else {
2241                                         ShadeInputUV *suv= &shi->uv[shi->actuv];
2242                                         int i = shi->actuv;
2243
2244                                         if(mtex->uvname[0] != 0) {
2245                                                 for(i = 0; i < shi->totuv; i++) {
2246                                                         if(strcmp(shi->uv[i].name, mtex->uvname)==0) {
2247                                                                 suv= &shi->uv[i];
2248                                                                 break;
2249                                                         }
2250                                                 }
2251                                         }
2252
2253                                         co= suv->uv;
2254                                         dx= suv->dxuv;
2255                                         dy= suv->dyuv; 
2256
2257                                         compatible_bump_uv_derivs(&compat_bump, shi, mtex, i);
2258                                 }
2259                         }
2260                         else if(mtex->texco==TEXCO_WINDOW) {
2261                                 co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
2262                         }
2263                         else if(mtex->texco==TEXCO_STRAND) {
2264                                 co= tempvec; dx= dxt; dy= dyt;
2265                                 co[0]= shi->strandco;
2266                                 co[1]= co[2]= 0.0f;
2267                                 dx[0]= shi->dxstrand;
2268                                 dx[1]= dx[2]= 0.0f;
2269                                 dy[0]= shi->dystrand;
2270                                 dy[1]= dy[2]= 0.0f;
2271                         }
2272                         else if(mtex->texco==TEXCO_STRESS) {
2273                                 co= tempvec; dx= dxt; dy= dyt;
2274                                 co[0]= shi->stress;
2275                                 co[1]= co[2]= 0.0f;
2276                                 dx[0]= 0.0f;
2277                                 dx[1]= dx[2]= 0.0f;
2278                                 dy[0]= 0.0f;
2279                                 dy[1]= dy[2]= 0.0f;
2280                         }
2281                         else continue;  // can happen when texco defines disappear and it renders old files
2282
2283                         /* the pointer defines if bumping happens */
2284                         if(mtex->mapto & (MAP_NORM|MAP_WARP)) {
2285                                 texres.nor= norvec;
2286                                 norvec[0]= norvec[1]= norvec[2]= 0.0;
2287                         }
2288                         else texres.nor= NULL;
2289                         
2290                         if(warpdone) {
2291                                 VECADD(tempvec, co, warpvec);
2292                                 co= tempvec;
2293                         }
2294
2295                         /* XXX texture node trees don't work for this yet */
2296                         if(texres.nor && !((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP))) {
2297                                 if(use_compat_bump) {
2298                                         rgbnor = compatible_bump_compute(&compat_bump, shi, mtex, tex,
2299                                                 &texres, Tnor*stencilTin, co, dx, dy, texvec, dxt, dyt);
2300                                 }
2301                                 else if(use_ntap_bump) {
2302                                         rgbnor = ntap_bump_compute(&ntap_bump, shi, mtex, tex,
2303                                                 &texres, Tnor*stencilTin, co, dx, dy, texvec, dxt, dyt);
2304                                 }
2305                                 else {
2306                                         texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
2307                                         rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
2308                                 }
2309                         }
2310                         else {
2311                                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
2312                                 rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
2313                         }
2314
2315                         /* texture output */
2316
2317                         if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
2318                                 texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2319                                 rgbnor-= TEX_RGB;
2320                         }
2321                         if(mtex->texflag & MTEX_NEGATIVE) {
2322                                 if(rgbnor & TEX_RGB) {
2323                                         texres.tr= 1.0f-texres.tr;
2324                                         texres.tg= 1.0f-texres.tg;
2325                                         texres.tb= 1.0f-texres.tb;
2326                                 }
2327                                 texres.tin= 1.0f-texres.tin;
2328                         }
2329                         if(mtex->texflag & MTEX_STENCIL) {
2330                                 if(rgbnor & TEX_RGB) {
2331                                         fact= texres.ta;
2332                                         texres.ta*= stencilTin;
2333                                         stencilTin*= fact;
2334                                 }
2335                                 else {
2336                                         fact= texres.tin;
2337                                         texres.tin*= stencilTin;
2338                                         stencilTin*= fact;
2339                                 }
2340                         }
2341                         else {
2342                                 Tnor*= stencilTin;
2343                         }
2344                         
2345                         if(texres.nor) {
2346                                 if((rgbnor & TEX_NOR)==0) {
2347                                         /* make our own normal */
2348                                         if(rgbnor & TEX_RGB) {
2349                                                 texres.nor[0]= texres.tr;
2350                                                 texres.nor[1]= texres.tg;
2351                                                 texres.nor[2]= texres.tb;
2352                                         }
2353                                         else {
2354                                                 float co_nor= 0.5*cos(texres.tin-0.5f);
2355                                                 float si= 0.5*sin(texres.tin-0.5f);
2356                                                 float f1, f2;
2357
2358                                                 f1= shi->vn[0];
2359                                                 f2= shi->vn[1];
2360                                                 texres.nor[0]= f1*co_nor+f2*si;
2361                                                 texres.nor[1]= f2*co_nor-f1*si;
2362                                                 f1= shi->vn[1];
2363                                                 f2= shi->vn[2];
2364                                                 texres.nor[1]= f1*co_nor+f2*si;
2365                                                 texres.nor[2]= f2*co_nor-f1*si;
2366                                         }
2367                                 }
2368                                 // warping, local space
2369                                 if(mtex->mapto & MAP_WARP) {
2370                                         float *warpnor= texres.nor, warpnor_[3];
2371                                         
2372                                         if(use_ntap_bump) {
2373                                                 VECCOPY(warpnor_, texres.nor);
2374                                                 warpnor= warpnor_;
2375                                                 normalize_v3(warpnor_);
2376                                         }
2377                                         warpvec[0]= mtex->warpfac*warpnor[0];
2378                                         warpvec[1]= mtex->warpfac*warpnor[1];
2379                                         warpvec[2]= mtex->warpfac*warpnor[2];
2380                                         warpdone= 1;
2381                                 }
2382 #if 0                           
2383                                 if(mtex->texflag & MTEX_VIEWSPACE) {
2384                                         // rotate to global coords
2385                                         if(mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
2386                                                 if(shi->vlr && shi->obr && shi->obr->ob) {
2387                                                         float len= normalize_v3(texres.nor);
2388                                                         // can be optimized... (ton)
2389                                                         mul_mat3_m4_v3(shi->obr->ob->obmat, texres.nor);
2390                                                         mul_mat3_m4_v3(R.viewmat, texres.nor);
2391                                                         normalize_v3(texres.nor);
2392                                                         mul_v3_fl(texres.nor, len);
2393                                                 }
2394                                         }
2395                                 }
2396 #endif                          
2397                         }
2398
2399                         /* mapping */
2400                         if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
2401                                 float tcol[3];
2402                                 
2403                                 /* stencil maps on the texture control slider, not texture intensity value */
2404                                 
2405                                 tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
2406                                 
2407                                 if((rgbnor & TEX_RGB)==0) {
2408                                         tcol[0]= mtex->r;
2409                                         tcol[1]= mtex->g;
2410                                         tcol[2]= mtex->b;
2411                                 }
2412                                 else if(mtex->mapto & MAP_ALPHA) {
2413                                         texres.tin= stencilTin;
2414                                 }
2415                                 else texres.tin= texres.ta;
2416                                 
2417                                 /* inverse gamma correction */
2418                                 if (tex->type==TEX_IMAGE) {
2419                                         Image *ima = tex->ima;
2420                                         ImBuf *ibuf = BKE_image_get_ibuf(ima, &tex->iuser);
2421                                         
2422                                         /* don't linearize float buffers, assumed to be linear */
2423                                         if (ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
2424                                                 srgb_to_linearrgb_v3_v3(tcol, tcol);
2425                                 }
2426                                 
2427                                 if(mtex->mapto & MAP_COL) {
2428                                         float colfac= mtex->colfac*stencilTin;
2429                                         texture_rgb_blend(&shi->r, tcol, &shi->r, texres.tin, colfac, mtex->blendtype);
2430                                 }
2431                                 if(mtex->mapto & MAP_COLSPEC) {
2432                                         float colspecfac= mtex->colspecfac*stencilTin;
2433                                         texture_rgb_blend(&shi->specr, tcol, &shi->specr, texres.tin, colspecfac, mtex->blendtype);
2434                                 }
2435                                 if(mtex->mapto & MAP_COLMIR) {
2436                                         float mirrfac= mtex->mirrfac*stencilTin;
2437
2438                                         // exception for envmap only
2439                                         if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
2440                                                 fact= texres.tin*mirrfac;
2441                                                 facm= 1.0f- fact;
2442                                                 shi->refcol[0]= fact + facm*shi->refcol[0];
2443                                                 shi->refcol[1]= fact*tcol[0] + facm*shi->refcol[1];
2444                                                 shi->refcol[2]= fact*tcol[1] + facm*shi->refcol[2];
2445                                                 shi->refcol[3]= fact*tcol[2] + facm*shi->refcol[3];
2446                                         }
2447                                         else {
2448                                                 texture_rgb_blend(&shi->mirr, tcol, &shi->mirr, texres.tin, mirrfac, mtex->blendtype);
2449                                         }
2450                                 }
2451                         }
2452                         if( (mtex->mapto & MAP_NORM) ) {
2453                                 if(texres.nor) {
2454                                         float norfac= mtex->norfac;
2455                                         
2456                                         /* we need to code blending modes for normals too once.. now 1 exception hardcoded */
2457                                         
2458                                         if ((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP)) {
2459                                                 
2460                                                 found_nmapping = 1;
2461                                                 
2462                                                 /* qdn: for normalmaps, to invert the normalmap vector,
2463                                                    it is better to negate x & y instead of subtracting the vector as was done before */
2464                                                 if (norfac < 0.0f) {
2465                                                         texres.nor[0] = -texres.nor[0];
2466                                                         texres.nor[1] = -texres.nor[1];
2467                                                 }
2468                                                 fact = Tnor*fabs(norfac);
2469                                                 if (fact>1.f) fact = 1.f;
2470                                                 facm = 1.f-fact;
2471                                                 if(mtex->normapspace == MTEX_NSPACE_TANGENT) {
2472                                                         /* qdn: tangent space */
2473                                                         float B[3], tv[3];
2474                                                         const float * no = iFirstTimeNMap!=0 ? shi->nmapnorm : shi->vn;
2475                                                         iFirstTimeNMap=0;
2476                                                         cross_v3_v3v3(B, no, shi->nmaptang);    /* bitangent */
2477                                                         mul_v3_fl(B, shi->nmaptang[3]);
2478                                                         /* transform norvec from tangent space to object surface in camera space */
2479                                                         tv[0] = texres.nor[0]*shi->nmaptang[0] + texres.nor[1]*B[0] + texres.nor[2]*no[0];
2480                                                         tv[1] = texres.nor[0]*shi->nmaptang[1] + texres.nor[1]*B[1] + texres.nor[2]*no[1];
2481                                                         tv[2] = texres.nor[0]*shi->nmaptang[2] + texres.nor[1]*B[2] + texres.nor[2]*no[2];
2482                                                         shi->vn[0]= facm*no[0] + fact*tv[0];
2483                                                         shi->vn[1]= facm*no[1] + fact*tv[1];
2484                                                         shi->vn[2]= facm*no[2] + fact*tv[2];
2485                                                 }
2486                                                 else {
2487                                                         float nor[3];
2488
2489                                                         VECCOPY(nor, texres.nor);
2490
2491                                                         if(mtex->normapspace == MTEX_NSPACE_CAMERA);
2492                                                         else if(mtex->normapspace == MTEX_NSPACE_WORLD) {
2493                                                                 mul_mat3_m4_v3(R.viewmat, nor);
2494                                                         }
2495                                                         else if(mtex->normapspace == MTEX_NSPACE_OBJECT) {
2496                                                                 if(shi->obr && shi->obr->ob)
2497                                                                         mul_mat3_m4_v3(shi->obr->ob->obmat, nor);
2498                                                                 mul_mat3_m4_v3(R.viewmat, nor);
2499                                                         }
2500
2501                                                         normalize_v3(nor);
2502
2503                                                         /* qdn: worldspace */
2504                                                         shi->vn[0]= facm*shi->vn[0] + fact*nor[0];
2505                                                         shi->vn[1]= facm*shi->vn[1] + fact*nor[1];
2506                                                         shi->vn[2]= facm*shi->vn[2] + fact*nor[2];
2507                                                 }
2508                                         }
2509                                         else {
2510                                                 /* XXX texture node trees don't work for this yet */
2511                                                 if (use_compat_bump || use_ntap_bump) {
2512                                                         shi->vn[0] = texres.nor[0];
2513                                                         shi->vn[1] = texres.nor[1];
2514                                                         shi->vn[2] = texres.nor[2];
2515                                                 }
2516                                                 else {
2517                                                         float nor[3], dot;
2518         
2519                                                         if(shi->mat->mode & MA_TANGENT_V) {
2520                                                                 shi->tang[0]+= Tnor*norfac*texres.nor[0];
2521                                                                 shi->tang[1]+= Tnor*norfac*texres.nor[1];
2522                                                                 shi->tang[2]+= Tnor*norfac*texres.nor[2];
2523                                                         }
2524         
2525                                                         /* prevent bump to become negative normal */
2526                                                         nor[0]= Tnor*norfac*texres.nor[0];
2527                                                         nor[1]= Tnor*norfac*texres.nor[1];
2528                                                         nor[2]= Tnor*norfac*texres.nor[2];
2529                                                         
2530                                                         dot= 0.5f + 0.5f*INPR(nor, shi->vn);
2531                                                         
2532                                                         shi->vn[0]+= dot*nor[0];
2533                                                         shi->vn[1]+= dot*nor[1];
2534                                                         shi->vn[2]+= dot*nor[2];
2535                                                 }
2536                                         }
2537                                         normalize_v3(shi->vn);
2538                                         
2539                                         /* this makes sure the bump is passed on to the next texture */
2540                                         shi->orn[0]= -shi->vn[0];
2541                                         shi->orn[1]= -shi->vn[1];
2542                                         shi->orn[2]= -shi->vn[2];
2543                                 }
2544                         }
2545
2546                         if( mtex->mapto & MAP_DISPLACE ) {
2547                                 /* Now that most textures offer both Nor and Intensity, allow  */
2548                                 /* both to work, and let user select with slider.   */
2549                                 if(texres.nor) {
2550                                         float norfac= mtex->norfac;
2551
2552                                         shi->displace[0]+= 0.2f*Tnor*norfac*texres.nor[0];
2553                                         shi->displace[1]+= 0.2f*Tnor*norfac*texres.nor[1];
2554                                         shi->displace[2]+= 0.2f*Tnor*norfac*texres.nor[2];
2555                                 }
2556                                 
2557                                 if(rgbnor & TEX_RGB) {
2558                                         texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2559                                 }
2560
2561                                 factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
2562
2563                                 if(mtex->blendtype==MTEX_BLEND) {
2564                                         shi->displace[0]= factt*shi->vn[0] + facmm*shi->displace[0];
2565                                         shi->displace[1]= factt*shi->vn[1] + facmm*shi->displace[1];
2566                                         shi->displace[2]= factt*shi->vn[2] + facmm*shi->displace[2];
2567                                 }
2568                                 else if(mtex->blendtype==MTEX_MUL) {
2569                                         shi->displace[0]*= factt*shi->vn[0];
2570                                         shi->displace[1]*= factt*shi->vn[1];
2571                                         shi->displace[2]*= factt*shi->vn[2];
2572                                 }
2573                                 else { /* add or sub */
2574                                         if(mtex->blendtype==MTEX_SUB) factt= -factt;
2575                                         shi->displace[0]+= factt*shi->vn[0];
2576                                         shi->displace[1]+= factt*shi->vn[1];
2577                                         shi->displace[2]+= factt*shi->vn[2];
2578                                 }
2579                         }
2580
2581                         if(mtex->mapto & MAP_VARS) {
2582                                 /* stencil maps on the texture control slider, not texture intensity value */
2583                                 
2584                                 if(rgbnor & TEX_RGB) {
2585                                         if(texres.talpha) texres.tin= texres.ta;
2586                                         else texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2587                                 }
2588
2589                                 if(mtex->mapto & MAP_REF) {
2590                                         float difffac= mtex->difffac*stencilTin;
2591
2592                                         shi->refl= texture_value_blend(mtex->def_var, shi->refl, texres.tin, difffac, mtex->blendtype);
2593                                         if(shi->refl<0.0f) shi->refl= 0.0f;
2594                                 }
2595                                 if(mtex->mapto & MAP_SPEC) {
2596                                         float specfac= mtex->specfac*stencilTin;
2597                                         
2598                                         shi->spec= texture_value_blend(mtex->def_var, shi->spec, texres.tin, specfac, mtex->blendtype);
2599                                         if(shi->spec<0.0f) shi->spec= 0.0f;
2600                                 }
2601                                 if(mtex->mapto & MAP_EMIT) {
2602                                         float emitfac= mtex->emitfac*stencilTin;
2603
2604                                         shi->emit= texture_value_blend(mtex->def_var, shi->emit, texres.tin, emitfac, mtex->blendtype);
2605                                         if(shi->emit<0.0f) shi->emit= 0.0f;
2606                                 }
2607                                 if(mtex->mapto & MAP_ALPHA) {
2608                                         float alphafac= mtex->alphafac*stencilTin;
2609
2610                                         shi->alpha= texture_value_blend(mtex->def_var, shi->alpha, texres.tin, alphafac, mtex->blendtype);
2611                                         if(shi->alpha<0.0f) shi->alpha= 0.0f;
2612                                         else if(shi->alpha>1.0f) shi->alpha= 1.0f;
2613                                 }
2614                                 if(mtex->mapto & MAP_HAR) {
2615                                         float har;  // have to map to 0-1
2616                                         float hardfac= mtex->hardfac*stencilTin;
2617                                         
2618                                         har= ((float)shi->har)/128.0f;
2619                                         har= 128.0f*texture_value_blend(mtex->def_var, har, texres.tin, hardfac, mtex->blendtype);
2620                                         
2621                                         if(har<1.0f) shi->har= 1;
2622                                         else if(har>511) shi->har= 511;
2623                                         else shi->har= (int)har;
2624                                 }
2625                                 if(mtex->mapto & MAP_RAYMIRR) {
2626                                         float raymirrfac= mtex->raymirrfac*stencilTin;
2627
2628                                         shi->ray_mirror= texture_value_blend(mtex->def_var, shi->ray_mirror, texres.tin, raymirrfac, mtex->blendtype);
2629                                         if(shi->ray_mirror<0.0f) shi->ray_mirror= 0.0f;
2630                                         else if(shi->ray_mirror>1.0f) shi->ray_mirror= 1.0f;
2631                                 }
2632                                 if(mtex->mapto & MAP_TRANSLU) {
2633                                         float translfac= mtex->translfac*stencilTin;
2634
2635                                         shi->translucency= texture_value_blend(mtex->def_var, shi->translucency, texres.tin, translfac, mtex->blendtype);
2636                                         if(shi->translucency<0.0f) shi->translucency= 0.0f;
2637                                         else if(shi->translucency>1.0f) shi->translucency= 1.0f;
2638                                 }
2639                                 if(mtex->mapto & MAP_AMB) {
2640                                         float ambfac= mtex->ambfac*stencilTin;
2641
2642                                         shi->amb= texture_value_blend(mtex->def_var, shi->amb, texres.tin, ambfac, mtex->blendtype);
2643                                         if(shi->amb<0.0f) shi->amb= 0.0f;
2644                                         else if(shi->amb>1.0f) shi->amb= 1.0f;
2645                                         
2646                                         shi->ambr= shi->amb*R.wrld.ambr;
2647                                         shi->ambg= shi->amb*R.wrld.ambg;
2648                                         shi->ambb= shi->amb*R.wrld.ambb;
2649                                 }
2650                         }
2651                 }
2652         }
2653         if ((use_compat_bump || use_ntap_bump || found_nmapping) && (shi->mat->mode & MA_TANGENT_V)!=0) {
2654                 const float fnegdot = -dot_v3v3(shi->vn, shi->tang);
2655                 // apply Gram-Schmidt projection
2656                 madd_v3_v3fl(shi->tang,  shi->vn, fnegdot);
2657                 normalize_v3(shi->tang);
2658         }
2659 }
2660
2661
2662 void do_volume_tex(ShadeInput *shi, float *xyz, int mapto_flag, float *col, float *val)
2663 {
2664         MTex *mtex;
2665         Tex *tex;
2666         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2667         int tex_nr, rgbnor= 0;
2668         float co[3], texvec[3];
2669         float fact, stencilTin=1.0;
2670         
2671         if (R.r.scemode & R_NO_TEX) return;
2672         /* here: test flag if there's a tex (todo) */
2673         
2674         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2675                 /* separate tex switching */
2676                 if(shi->mat->septex & (1<<tex_nr)) continue;
2677                 
2678                 if(shi->mat->mtex[tex_nr]) {
2679                         mtex= shi->mat->mtex[tex_nr];
2680                         tex= mtex->tex;
2681                         if(tex==0) continue;
2682                         
2683                         /* only process if this texture is mapped 
2684                          * to one that we're interested in */
2685                         if (!(mtex->mapto & mapto_flag)) continue;
2686                         
2687                         /* which coords */
2688                         if(mtex->texco==TEXCO_OBJECT) { 
2689                                 Object *ob= mtex->object;
2690                                 if(ob) {                                                
2691                                         VECCOPY(co, xyz);       
2692                                         if(mtex->texflag & MTEX_OB_DUPLI_ORIG) {
2693                                                 if(shi->obi && shi->obi->duplitexmat)
2694                                                         mul_m4_v3(shi->obi->duplitexmat, co);                                   
2695                                         } 
2696                                         mul_m4_v3(ob->imat_ren, co);
2697                                 }
2698                         }
2699                         /* not really orco, but 'local' */
2700                         else if(mtex->texco==TEXCO_ORCO) {
2701                                 
2702                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2703                                         VECCOPY(co, shi->duplilo);
2704                                 }
2705                                 else {
2706                                         Object *ob= shi->obi->ob;
2707 &