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