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