style cleanup: comment blocks
[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                         } else {
764                                 copy_v3_v3(result+5, texres->nor);
765                         }
766                 }
767                 if (pit->version < 6) {
768                         if(osatex) rgbnor= ((TexDoitold)pit->doit)(tex->stype, 
769                                 pit->data, texvec, dxt, dyt);
770                         else rgbnor= ((TexDoitold)pit->doit)(tex->stype, 
771                                 pit->data, texvec, NULL, NULL);
772                 } else {
773                         if(osatex) rgbnor= ((TexDoit)pit->doit)(tex->stype, 
774                                 pit->data, texvec, dxt, dyt, result);
775                         else rgbnor= ((TexDoit)pit->doit)(tex->stype, 
776                                 pit->data, texvec, NULL, NULL, result);
777                 }
778
779                 if (pit->version < 6) {
780                         texres->tin = pit->result[0];
781                 } else {
782                         texres->tin = result[0]; /* XXX, assigning garbage value, fixme! */
783                 }
784
785                 if(rgbnor & TEX_NOR) {
786                         if(texres->nor) {
787                                 if (pit->version < 6) {
788                                         copy_v3_v3(texres->nor, pit->result+5);
789                                 } else {
790                                         copy_v3_v3(texres->nor, result+5);
791                                 }
792                         }
793                 }
794                 
795                 if(rgbnor & TEX_RGB) {
796                         if (pit->version < 6) {
797                                 texres->tr = pit->result[1];
798                                 texres->tg = pit->result[2];
799                                 texres->tb = pit->result[3];
800                                 texres->ta = pit->result[4];
801                         } else {
802                                 texres->tr = result[1];
803                                 texres->tg = result[2];
804                                 texres->tb = result[3];
805                                 texres->ta = result[4];
806                         }
807
808                         BRICONTRGB;
809                 }
810                 
811                 BRICONT;
812         }
813
814         return rgbnor;
815 }
816
817
818 static int cubemap_glob(float *n, float x, float y, float z, float *adr1, float *adr2)
819 {
820         float x1, y1, z1, nor[3];
821         int ret;
822         
823         if(n==NULL) {
824                 nor[0]= x; nor[1]= y; nor[2]= z;        // use local render coord
825         }
826         else {
827                 copy_v3_v3(nor, n);
828         }
829         mul_mat3_m4_v3(R.viewinv, nor);
830
831         x1= fabs(nor[0]);
832         y1= fabs(nor[1]);
833         z1= fabs(nor[2]);
834         
835         if(z1>=x1 && z1>=y1) {
836                 *adr1 = (x + 1.0f) / 2.0f;
837                 *adr2 = (y + 1.0f) / 2.0f;
838                 ret= 0;
839         }
840         else if(y1>=x1 && y1>=z1) {
841                 *adr1 = (x + 1.0f) / 2.0f;
842                 *adr2 = (z + 1.0f) / 2.0f;
843                 ret= 1;
844         }
845         else {
846                 *adr1 = (y + 1.0f) / 2.0f;
847                 *adr2 = (z + 1.0f) / 2.0f;
848                 ret= 2;         
849         }
850         return ret;
851 }
852
853 /* ------------------------------------------------------------------------- */
854
855 /* mtex argument only for projection switches */
856 static int cubemap(MTex *mtex, VlakRen *vlr, float *n, float x, float y, float z, float *adr1, float *adr2)
857 {
858         int proj[4]={0, ME_PROJXY, ME_PROJXZ, ME_PROJYZ}, ret= 0;
859         
860         if(vlr) {
861                 int index;
862                 
863                 /* Mesh vertices have such flags, for others we calculate it once based on orco */
864                 if((vlr->puno & (ME_PROJXY|ME_PROJXZ|ME_PROJYZ))==0) {
865                         /* test for v1, vlr can be faked for baking */
866                         if(vlr->v1 && vlr->v1->orco) {
867                                 float nor[3];
868                                 normal_tri_v3( nor,vlr->v1->orco, vlr->v2->orco, vlr->v3->orco);
869                                 
870                                 if( fabs(nor[0])<fabs(nor[2]) && fabs(nor[1])<fabs(nor[2]) ) vlr->puno |= ME_PROJXY;
871                                 else if( fabs(nor[0])<fabs(nor[1]) && fabs(nor[2])<fabs(nor[1]) ) vlr->puno |= ME_PROJXZ;
872                                 else vlr->puno |= ME_PROJYZ;
873                         }
874                         else return cubemap_glob(n, x, y, z, adr1, adr2);
875                 }
876                 
877                 if(mtex) {
878                         /* the mtex->proj{xyz} have type char. maybe this should be wider? */
879                         /* casting to int ensures that the index type is right.            */
880                         index = (int) mtex->projx;
881                         proj[index]= ME_PROJXY;
882
883                         index = (int) mtex->projy;
884                         proj[index]= ME_PROJXZ;
885
886                         index = (int) mtex->projz;
887                         proj[index]= ME_PROJYZ;
888                 }
889                 
890                 if(vlr->puno & proj[1]) {
891                         *adr1 = (x + 1.0f) / 2.0f;
892                         *adr2 = (y + 1.0f) / 2.0f;
893                 }
894                 else if(vlr->puno & proj[2]) {
895                         *adr1 = (x + 1.0f) / 2.0f;
896                         *adr2 = (z + 1.0f) / 2.0f;
897                         ret= 1;
898                 }
899                 else {
900                         *adr1 = (y + 1.0f) / 2.0f;
901                         *adr2 = (z + 1.0f) / 2.0f;
902                         ret= 2;
903                 }               
904         } 
905         else {
906                 return cubemap_glob(n, x, y, z, adr1, adr2);
907         }
908         
909         return ret;
910 }
911
912 /* ------------------------------------------------------------------------- */
913
914 static int cubemap_ob(Object *ob, float *n, float x, float y, float z, float *adr1, float *adr2)
915 {
916         float x1, y1, z1, nor[3];
917         int ret;
918         
919         if(n==NULL) return 0;
920         
921         copy_v3_v3(nor, n);
922         if(ob) mul_mat3_m4_v3(ob->imat, nor);
923         
924         x1= fabs(nor[0]);
925         y1= fabs(nor[1]);
926         z1= fabs(nor[2]);
927         
928         if(z1>=x1 && z1>=y1) {
929                 *adr1 = (x + 1.0f) / 2.0f;
930                 *adr2 = (y + 1.0f) / 2.0f;
931                 ret= 0;
932         }
933         else if(y1>=x1 && y1>=z1) {
934                 *adr1 = (x + 1.0f) / 2.0f;
935                 *adr2 = (z + 1.0f) / 2.0f;
936                 ret= 1;
937         }
938         else {
939                 *adr1 = (y + 1.0f) / 2.0f;
940                 *adr2 = (z + 1.0f) / 2.0f;
941                 ret= 2;         
942         }
943         return ret;
944 }
945
946 /* ------------------------------------------------------------------------- */
947
948 static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *dxt, float *dyt)
949 {
950         Tex *tex;
951         Object *ob= NULL;
952         float fx, fy, fac1, area[8];
953         int ok, proj, areaflag= 0, wrap, texco;
954         
955         /* mtex variables localized, only cubemap doesn't cooperate yet... */
956         wrap= mtex->mapping;
957         tex= mtex->tex;
958         ob= mtex->object;
959         texco= mtex->texco;
960
961         if(R.osa==0) {
962                 
963                 if(wrap==MTEX_FLAT) {
964                         fx = (t[0] + 1.0f) / 2.0f;
965                         fy = (t[1] + 1.0f) / 2.0f;
966                 }
967                 else if(wrap==MTEX_TUBE) map_to_tube( &fx, &fy,t[0], t[1], t[2]);
968                 else if(wrap==MTEX_SPHERE) map_to_sphere( &fx, &fy,t[0], t[1], t[2]);
969                 else {
970                         if(texco==TEXCO_OBJECT) cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
971                         else if(texco==TEXCO_GLOB) cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
972                         else cubemap(mtex, vlr, n, t[0], t[1], t[2], &fx, &fy);
973                 }
974                 
975                 /* repeat */
976                 if(tex->extend==TEX_REPEAT) {
977                         if(tex->xrepeat>1) {
978                                 float origf= fx *= tex->xrepeat;
979                                 
980                                 if(fx>1.0f) fx -= (int)(fx);
981                                 else if(fx<0.0f) fx+= 1-(int)(fx);
982                                 
983                                 if(tex->flag & TEX_REPEAT_XMIR) {
984                                         int orig= (int)floor(origf);
985                                         if(orig & 1)
986                                                 fx= 1.0f-fx;
987                                 }
988                         }
989                         if(tex->yrepeat>1) {
990                                 float origf= fy *= tex->yrepeat;
991                                 
992                                 if(fy>1.0f) fy -= (int)(fy);
993                                 else if(fy<0.0f) fy+= 1-(int)(fy);
994                                 
995                                 if(tex->flag & TEX_REPEAT_YMIR) {
996                                         int orig= (int)floor(origf);
997                                         if(orig & 1) 
998                                                 fy= 1.0f-fy;
999                                 }
1000                         }
1001                 }
1002                 /* crop */
1003                 if(tex->cropxmin!=0.0f || tex->cropxmax!=1.0f) {
1004                         fac1= tex->cropxmax - tex->cropxmin;
1005                         fx= tex->cropxmin+ fx*fac1;
1006                 }
1007                 if(tex->cropymin!=0.0f || tex->cropymax!=1.0f) {
1008                         fac1= tex->cropymax - tex->cropymin;
1009                         fy= tex->cropymin+ fy*fac1;
1010                 }
1011
1012                 t[0]= fx;
1013                 t[1]= fy;
1014         }
1015         else {
1016                 
1017                 if(wrap==MTEX_FLAT) {
1018                         fx= (t[0] + 1.0f) / 2.0f;
1019                         fy= (t[1] + 1.0f) / 2.0f;
1020                         dxt[0]/= 2.0f;
1021                         dxt[1]/= 2.0f;
1022                         dxt[2]/= 2.0f;
1023                         dyt[0]/= 2.0f;
1024                         dyt[1]/= 2.0f;
1025                         dyt[2]/= 2.0f;
1026                 }
1027                 else if ELEM(wrap, MTEX_TUBE, MTEX_SPHERE) {
1028                         /* exception: the seam behind (y<0.0) */
1029                         ok= 1;
1030                         if(t[1]<=0.0f) {
1031                                 fx= t[0]+dxt[0];
1032                                 fy= t[0]+dyt[0];
1033                                 if(fx>=0.0f && fy>=0.0f && t[0]>=0.0f);
1034                                 else if(fx<=0.0f && fy<=0.0f && t[0]<=0.0f);
1035                                 else ok= 0;
1036                         }
1037                         if(ok) {
1038                                 if(wrap==MTEX_TUBE) {
1039                                         map_to_tube( area, area+1,t[0], t[1], t[2]);
1040                                         map_to_tube( area+2, area+3,t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2]);
1041                                         map_to_tube( area+4, area+5,t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2]);
1042                                 }
1043                                 else { 
1044                                         map_to_sphere(area,area+1,t[0], t[1], t[2]);
1045                                         map_to_sphere( area+2, area+3,t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2]);
1046                                         map_to_sphere( area+4, area+5,t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2]);
1047                                 }
1048                                 areaflag= 1;
1049                         }
1050                         else {
1051                                 if(wrap==MTEX_TUBE) map_to_tube( &fx, &fy,t[0], t[1], t[2]);
1052                                 else map_to_sphere( &fx, &fy,t[0], t[1], t[2]);
1053                                 dxt[0]/= 2.0f;
1054                                 dxt[1]/= 2.0f;
1055                                 dyt[0]/= 2.0f;
1056                                 dyt[1]/= 2.0f;
1057                         }
1058                 }
1059                 else {
1060
1061                         if(texco==TEXCO_OBJECT) proj = cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
1062                         else if (texco==TEXCO_GLOB) proj = cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
1063                         else proj = cubemap(mtex, vlr, n, t[0], t[1], t[2], &fx, &fy);
1064
1065                         if(proj==1) {
1066                                 SWAP(float, dxt[1], dxt[2]);
1067                                 SWAP(float, dyt[1], dyt[2]);
1068                         }
1069                         else if(proj==2) {
1070                                 float f1= dxt[0], f2= dyt[0];
1071                                 dxt[0]= dxt[1];
1072                                 dyt[0]= dyt[1];
1073                                 dxt[1]= dxt[2];
1074                                 dyt[1]= dyt[2];
1075                                 dxt[2]= f1;
1076                                 dyt[2]= f2;
1077                         }
1078
1079                         dxt[0] *= 0.5f;
1080                         dxt[1] *= 0.5f;
1081                         dxt[2] *= 0.5f;
1082
1083                         dyt[0] *= 0.5f;
1084                         dyt[1] *= 0.5f;
1085                         dyt[2] *= 0.5f;
1086
1087                 }
1088                 
1089                 /* if area, then reacalculate dxt[] and dyt[] */
1090                 if(areaflag) {
1091                         fx= area[0]; 
1092                         fy= area[1];
1093                         dxt[0]= area[2]-fx;
1094                         dxt[1]= area[3]-fy;
1095                         dyt[0]= area[4]-fx;
1096                         dyt[1]= area[5]-fy;
1097                 }
1098                 
1099                 /* repeat */
1100                 if(tex->extend==TEX_REPEAT) {
1101                         float max= 1.0f;
1102                         if(tex->xrepeat>1) {
1103                                 float origf= fx *= tex->xrepeat;
1104                                 
1105                                 // TXF: omit mirror here, see comments in do_material_tex() after do_2d_mapping() call
1106                                 if (tex->texfilter == TXF_BOX) {
1107                                         if(fx>1.0f) fx -= (int)(fx);
1108                                         else if(fx<0.0f) fx+= 1-(int)(fx);
1109                                 
1110                                         if(tex->flag & TEX_REPEAT_XMIR) {
1111                                                 int orig= (int)floor(origf);
1112                                                 if(orig & 1) 
1113                                                         fx= 1.0f-fx;
1114                                         }
1115                                 }
1116                                 
1117                                 max= tex->xrepeat;
1118                                 
1119                                 dxt[0]*= tex->xrepeat;
1120                                 dyt[0]*= tex->xrepeat;
1121                         }
1122                         if(tex->yrepeat>1) {
1123                                 float origf= fy *= tex->yrepeat;
1124                                 
1125                                 // TXF: omit mirror here, see comments in do_material_tex() after do_2d_mapping() call
1126                                 if (tex->texfilter == TXF_BOX) {
1127                                         if(fy>1.0f) fy -= (int)(fy);
1128                                         else if(fy<0.0f) fy+= 1-(int)(fy);
1129                                 
1130                                         if(tex->flag & TEX_REPEAT_YMIR) {
1131                                                 int orig= (int)floor(origf);
1132                                                 if(orig & 1) 
1133                                                         fy= 1.0f-fy;
1134                                         }
1135                                 }
1136                                 
1137                                 if(max<tex->yrepeat)
1138                                         max= tex->yrepeat;
1139
1140                                 dxt[1]*= tex->yrepeat;
1141                                 dyt[1]*= tex->yrepeat;
1142                         }
1143                         if(max!=1.0f) {
1144                                 dxt[2]*= max;
1145                                 dyt[2]*= max;
1146                         }
1147                         
1148                 }
1149                 /* crop */
1150                 if(tex->cropxmin!=0.0f || tex->cropxmax!=1.0f) {
1151                         fac1= tex->cropxmax - tex->cropxmin;
1152                         fx= tex->cropxmin+ fx*fac1;
1153                         dxt[0]*= fac1;
1154                         dyt[0]*= fac1;
1155                 }
1156                 if(tex->cropymin!=0.0f || tex->cropymax!=1.0f) {
1157                         fac1= tex->cropymax - tex->cropymin;
1158                         fy= tex->cropymin+ fy*fac1;
1159                         dxt[1]*= fac1;
1160                         dyt[1]*= fac1;
1161                 }
1162                 
1163                 t[0]= fx;
1164                 t[1]= fy;
1165
1166         }
1167 }
1168
1169 /* ************************************** */
1170
1171 static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres, short thread, short which_output)
1172 {
1173         float tmpvec[3];
1174         int retval=0; /* return value, int:0, col:1, nor:2, everything:3 */
1175
1176         texres->talpha= 0;      /* is set when image texture returns alpha (considered premul) */
1177         
1178         if(tex->use_nodes && tex->nodetree) {
1179                 retval = ntreeTexExecTree(tex->nodetree, texres, texvec, dxt, dyt, osatex, thread,
1180                         tex, which_output, R.r.cfra, (R.r.scemode & R_TEXNODE_PREVIEW) != 0, NULL, NULL);
1181         }
1182         else
1183         switch(tex->type) {
1184         
1185         case 0:
1186                 texres->tin= 0.0f;
1187                 return 0;
1188         case TEX_CLOUDS:
1189                 retval= clouds(tex, texvec, texres);
1190                 break;
1191         case TEX_WOOD:
1192                 retval= wood(tex, texvec, texres); 
1193                 break;
1194         case TEX_MARBLE:
1195                 retval= marble(tex, texvec, texres); 
1196                 break;
1197         case TEX_MAGIC:
1198                 retval= magic(tex, texvec, texres); 
1199                 break;
1200         case TEX_BLEND:
1201                 retval= blend(tex, texvec, texres);
1202                 break;
1203         case TEX_STUCCI:
1204                 retval= stucci(tex, texvec, texres); 
1205                 break;
1206         case TEX_NOISE:
1207                 retval= texnoise(tex, texres); 
1208                 break;
1209         case TEX_IMAGE:
1210                 if(osatex) retval= imagewraposa(tex, tex->ima, NULL, texvec, dxt, dyt, texres);
1211                 else retval= imagewrap(tex, tex->ima, NULL, texvec, texres); 
1212                 tag_image_time(tex->ima); /* tag image as having being used */
1213                 break;
1214         case TEX_PLUGIN:
1215                 retval= plugintex(tex, texvec, dxt, dyt, osatex, texres);
1216                 break;
1217         case TEX_ENVMAP:
1218                 retval= envmaptex(tex, texvec, dxt, dyt, osatex, texres);
1219                 break;
1220         case TEX_MUSGRAVE:
1221                 /* newnoise: musgrave types */
1222                 
1223                 /* ton: added this, for Blender convention reason. 
1224                  * artificer: added the use of tmpvec to avoid scaling texvec
1225                  */
1226                 copy_v3_v3(tmpvec, texvec);
1227                 mul_v3_fl(tmpvec, 1.0f/tex->noisesize);
1228                 
1229                 switch(tex->stype) {
1230                 case TEX_MFRACTAL:
1231                 case TEX_FBM:
1232                         retval= mg_mFractalOrfBmTex(tex, tmpvec, texres);
1233                         break;
1234                 case TEX_RIDGEDMF:
1235                 case TEX_HYBRIDMF:
1236                         retval= mg_ridgedOrHybridMFTex(tex, tmpvec, texres);
1237                         break;
1238                 case TEX_HTERRAIN:
1239                         retval= mg_HTerrainTex(tex, tmpvec, texres);
1240                         break;
1241                 }
1242                 break;
1243         /* newnoise: voronoi type */
1244         case TEX_VORONOI:
1245                 /* ton: added this, for Blender convention reason.
1246                  * artificer: added the use of tmpvec to avoid scaling texvec
1247                  */
1248                 copy_v3_v3(tmpvec, texvec);
1249                 mul_v3_fl(tmpvec, 1.0f/tex->noisesize);
1250                 
1251                 retval= voronoiTex(tex, tmpvec, texres);
1252                 break;
1253         case TEX_DISTNOISE:
1254                 /* ton: added this, for Blender convention reason.
1255                  * artificer: added the use of tmpvec to avoid scaling texvec
1256                  */
1257                 copy_v3_v3(tmpvec, texvec);
1258                 mul_v3_fl(tmpvec, 1.0f/tex->noisesize);
1259                 
1260                 retval= mg_distNoiseTex(tex, tmpvec, texres);
1261                 break;
1262         case TEX_POINTDENSITY:
1263                 retval= pointdensitytex(tex, texvec, texres);
1264                 break;
1265         case TEX_VOXELDATA:
1266                 retval= voxeldatatex(tex, texvec, texres);  
1267                 break;
1268         case TEX_OCEAN:
1269                 retval= ocean_texture(tex, texvec, texres);  
1270                 break;
1271         }
1272
1273         if (tex->flag & TEX_COLORBAND) {
1274                 float col[4];
1275                 if (do_colorband(tex->coba, texres->tin, col)) {
1276                         texres->talpha= 1;
1277                         texres->tr= col[0];
1278                         texres->tg= col[1];
1279                         texres->tb= col[2];
1280                         texres->ta= col[3];
1281                         retval |= TEX_RGB;
1282                 }
1283         }
1284         return retval;
1285 }
1286
1287 /* this is called from the shader and texture nodes */
1288 int multitex_nodes(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres, short thread, short which_output, ShadeInput *shi, MTex *mtex)
1289 {
1290         if(tex==NULL) {
1291                 memset(texres, 0, sizeof(TexResult));
1292                 return 0;
1293         }
1294
1295         if(mtex)
1296                 which_output= mtex->which_output;
1297         
1298         if(tex->type==TEX_IMAGE) {
1299                 int rgbnor;
1300
1301                 if(mtex) {
1302                         /* we have mtex, use it for 2d mapping images only */
1303                         do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
1304                         rgbnor= multitex(tex, texvec, dxt, dyt, osatex, texres, thread, which_output);
1305
1306                         if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
1307                                 ImBuf *ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1308                                 
1309                                 /* don't linearize float buffers, assumed to be linear */
1310                                 if(ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
1311                                         srgb_to_linearrgb_v3_v3(&texres->tr, &texres->tr);
1312                         }
1313                 }
1314                 else {
1315                         /* we don't have mtex, do default flat 2d projection */
1316                         MTex localmtex;
1317                         float texvec_l[3], dxt_l[3], dyt_l[3];
1318                         
1319                         localmtex.mapping= MTEX_FLAT;
1320                         localmtex.tex= tex;
1321                         localmtex.object= NULL;
1322                         localmtex.texco= TEXCO_ORCO;
1323                         
1324                         copy_v3_v3(texvec_l, texvec);
1325                         if(dxt && dyt) {
1326                                 copy_v3_v3(dxt_l, dxt);
1327                                 copy_v3_v3(dyt_l, dyt);
1328                         }
1329                         else {
1330                                 zero_v3(dxt_l);
1331                                 zero_v3(dyt_l);
1332                         }
1333                         
1334                         do_2d_mapping(&localmtex, texvec_l, NULL, NULL, dxt_l, dyt_l);
1335                         rgbnor= multitex(tex, texvec_l, dxt_l, dyt_l, osatex, texres, thread, which_output);
1336                 }
1337
1338                 return rgbnor;
1339         }
1340         else
1341                 return multitex(tex, texvec, dxt, dyt, osatex, texres, thread, which_output);
1342 }
1343
1344 /* this is called for surface shading */
1345 int multitex_mtex(ShadeInput *shi, MTex *mtex, float *texvec, float *dxt, float *dyt, TexResult *texres)
1346 {
1347         Tex *tex= mtex->tex;
1348
1349         if(tex->use_nodes && tex->nodetree) {
1350                 /* stupid exception here .. but we have to pass shi and mtex to
1351                  * textures nodes for 2d mapping and color management for images */
1352                 return ntreeTexExecTree(tex->nodetree, texres, texvec, dxt, dyt, shi->osatex, shi->thread,
1353                         tex, mtex->which_output, R.r.cfra, (R.r.scemode & R_TEXNODE_PREVIEW) != 0, shi, mtex);
1354         }
1355         else
1356                 return multitex(mtex->tex, texvec, dxt, dyt, shi->osatex, texres, shi->thread, mtex->which_output);
1357 }
1358
1359 /* Warning, if the texres's values are not declared zero, check the return value to be sure
1360  * the color values are set before using the r/g/b values, otherwise you may use uninitialized values - Campbell */
1361 int multitex_ext(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres)
1362 {
1363         return multitex_nodes(tex, texvec, dxt, dyt, osatex, texres, 0, 0, NULL, NULL);
1364 }
1365
1366 /* extern-tex doesn't support nodes (ntreeBeginExec() can't be called when rendering is going on) */
1367 int multitex_ext_safe(Tex *tex, float *texvec, TexResult *texres)
1368 {
1369         int use_nodes= tex->use_nodes, retval;
1370         
1371         tex->use_nodes= 0;
1372         retval= multitex_nodes(tex, texvec, NULL, NULL, 0, texres, 0, 0, NULL, NULL);
1373         tex->use_nodes= use_nodes;
1374         
1375         return retval;
1376 }
1377
1378
1379 /* ------------------------------------------------------------------------- */
1380
1381 /* in = destination, tex = texture, out = previous color */
1382 /* fact = texture strength, facg = button strength value */
1383 void texture_rgb_blend(float in[3], const float tex[3], const float out[3], float fact, float facg, int blendtype)
1384 {
1385         float facm, col;
1386         
1387         switch(blendtype) {
1388         case MTEX_BLEND:
1389                 fact*= facg;
1390                 facm= 1.0f-fact;
1391
1392                 in[0]= (fact*tex[0] + facm*out[0]);
1393                 in[1]= (fact*tex[1] + facm*out[1]);
1394                 in[2]= (fact*tex[2] + facm*out[2]);
1395                 break;
1396                 
1397         case MTEX_MUL:
1398                 fact*= facg;
1399                 facm= 1.0f-facg;
1400                 in[0]= (facm+fact*tex[0])*out[0];
1401                 in[1]= (facm+fact*tex[1])*out[1];
1402                 in[2]= (facm+fact*tex[2])*out[2];
1403                 break;
1404
1405         case MTEX_SCREEN:
1406                 fact*= facg;
1407                 facm= 1.0f-facg;
1408                 in[0]= 1.0f - (facm+fact*(1.0f-tex[0])) * (1.0f-out[0]);
1409                 in[1]= 1.0f - (facm+fact*(1.0f-tex[1])) * (1.0f-out[1]);
1410                 in[2]= 1.0f - (facm+fact*(1.0f-tex[2])) * (1.0f-out[2]);
1411                 break;
1412
1413         case MTEX_OVERLAY:
1414                 fact*= facg;
1415                 facm= 1.0f-facg;
1416                 
1417                 if(out[0] < 0.5f)
1418                         in[0] = out[0] * (facm + 2.0f*fact*tex[0]);
1419                 else
1420                         in[0] = 1.0f - (facm + 2.0f*fact*(1.0f - tex[0])) * (1.0f - out[0]);
1421                 if(out[1] < 0.5f)
1422                         in[1] = out[1] * (facm + 2.0f*fact*tex[1]);
1423                 else
1424                         in[1] = 1.0f - (facm + 2.0f*fact*(1.0f - tex[1])) * (1.0f - out[1]);
1425                 if(out[2] < 0.5f)
1426                         in[2] = out[2] * (facm + 2.0f*fact*tex[2]);
1427                 else
1428                         in[2] = 1.0f - (facm + 2.0f*fact*(1.0f - tex[2])) * (1.0f - out[2]);
1429                 break;
1430                 
1431         case MTEX_SUB:
1432                 fact= -fact;
1433         case MTEX_ADD:
1434                 fact*= facg;
1435                 in[0]= (fact*tex[0] + out[0]);
1436                 in[1]= (fact*tex[1] + out[1]);
1437                 in[2]= (fact*tex[2] + out[2]);
1438                 break;
1439
1440         case MTEX_DIV:
1441                 fact*= facg;
1442                 facm= 1.0f-fact;
1443                 
1444                 if(tex[0]!=0.0f)
1445                         in[0]= facm*out[0] + fact*out[0]/tex[0];
1446                 if(tex[1]!=0.0f)
1447                         in[1]= facm*out[1] + fact*out[1]/tex[1];
1448                 if(tex[2]!=0.0f)
1449                         in[2]= facm*out[2] + fact*out[2]/tex[2];
1450
1451                 break;
1452
1453         case MTEX_DIFF:
1454                 fact*= facg;
1455                 facm= 1.0f-fact;
1456                 in[0]= facm*out[0] + fact*fabsf(tex[0]-out[0]);
1457                 in[1]= facm*out[1] + fact*fabsf(tex[1]-out[1]);
1458                 in[2]= facm*out[2] + fact*fabsf(tex[2]-out[2]);
1459                 break;
1460
1461         case MTEX_DARK:
1462                 fact*= facg;
1463                 facm= 1.0f-fact;
1464                 
1465                 col= tex[0]+((1-tex[0])*facm);
1466                 if(col < out[0]) in[0]= col; else in[0]= out[0];
1467                 col= tex[1]+((1-tex[1])*facm);
1468                 if(col < out[1]) in[1]= col; else in[1]= out[1];
1469                 col= tex[2]+((1-tex[2])*facm);
1470                 if(col < out[2]) in[2]= col; else in[2]= out[2];
1471                 break;
1472
1473         case MTEX_LIGHT:
1474                 fact*= facg;
1475                 
1476                 col= fact*tex[0];
1477                 if(col > out[0]) in[0]= col; else in[0]= out[0];
1478                 col= fact*tex[1];
1479                 if(col > out[1]) in[1]= col; else in[1]= out[1];
1480                 col= fact*tex[2];
1481                 if(col > out[2]) in[2]= col; else in[2]= out[2];
1482                 break;
1483                 
1484         case MTEX_BLEND_HUE:
1485                 fact*= facg;
1486                 copy_v3_v3(in, out);
1487                 ramp_blend(MA_RAMP_HUE, in, fact, tex);
1488                 break;
1489         case MTEX_BLEND_SAT:
1490                 fact*= facg;
1491                 copy_v3_v3(in, out);
1492                 ramp_blend(MA_RAMP_SAT, in, fact, tex);
1493                 break;
1494         case MTEX_BLEND_VAL:
1495                 fact*= facg;
1496                 copy_v3_v3(in, out);
1497                 ramp_blend(MA_RAMP_VAL, in, fact, tex);
1498                 break;
1499         case MTEX_BLEND_COLOR:
1500                 fact*= facg;
1501                 copy_v3_v3(in, out);
1502                 ramp_blend(MA_RAMP_COLOR, in, fact, tex);
1503                 break;
1504         case MTEX_SOFT_LIGHT: 
1505                 fact*= facg; 
1506                 copy_v3_v3(in, out);
1507                 ramp_blend(MA_RAMP_SOFT, in, fact, tex);
1508                 break; 
1509         case MTEX_LIN_LIGHT: 
1510                 fact*= facg; 
1511                 copy_v3_v3(in, out);
1512                 ramp_blend(MA_RAMP_LINEAR, in, fact, tex);
1513                 break; 
1514         }
1515 }
1516
1517 float texture_value_blend(float tex, float out, float fact, float facg, int blendtype)
1518 {
1519         float in=0.0, facm, col, scf;
1520         int flip= (facg < 0.0f);
1521
1522         facg= fabsf(facg);
1523         
1524         fact*= facg;
1525         facm= 1.0f-fact;
1526         if(flip) SWAP(float, fact, facm);
1527
1528         switch(blendtype) {
1529         case MTEX_BLEND:
1530                 in= fact*tex + facm*out;
1531                 break;
1532
1533         case MTEX_MUL:
1534                 facm= 1.0f-facg;
1535                 in= (facm+fact*tex)*out;
1536                 break;
1537
1538         case MTEX_SCREEN:
1539                 facm= 1.0f-facg;
1540                 in= 1.0f-(facm+fact*(1.0f-tex))*(1.0f-out);
1541                 break;
1542
1543         case MTEX_OVERLAY:
1544                 facm= 1.0f-facg;
1545                 if(out < 0.5f)
1546                         in = out * (facm + 2.0f*fact*tex);
1547                 else
1548                         in = 1.0f - (facm + 2.0f*fact*(1.0f - tex)) * (1.0f - out);
1549                 break;
1550
1551         case MTEX_SUB:
1552                 fact= -fact;
1553         case MTEX_ADD:
1554                 in= fact*tex + out;
1555                 break;
1556
1557         case MTEX_DIV:
1558                 if(tex!=0.0f)
1559                         in= facm*out + fact*out/tex;
1560                 break;
1561
1562         case MTEX_DIFF:
1563                 in= facm*out + fact*fabsf(tex-out);
1564                 break;
1565
1566         case MTEX_DARK:
1567                 col= fact*tex;
1568                 if(col < out) in= col; else in= out;
1569                 break;
1570
1571         case MTEX_LIGHT:
1572                 col= fact*tex;
1573                 if(col > out) in= col; else in= out;
1574                 break;
1575
1576         case MTEX_SOFT_LIGHT: 
1577                 scf=1.0f - (1.0f - tex) * (1.0f - out);
1578                 in= facm*out + fact * ((1.0f - out) * tex * out) + (out * scf);
1579                 break;       
1580
1581         case MTEX_LIN_LIGHT: 
1582                 if (tex > 0.5f)
1583                         in = out + fact*(2.0f*(tex - 0.5f));
1584                 else 
1585                         in = out + fact*(2.0f*tex - 1.0f);
1586                 break;
1587         }
1588         
1589         return in;
1590 }
1591
1592 static void texco_mapping(ShadeInput* shi, Tex* tex, MTex* mtex, float* co, float* dx, float* dy, float* texvec, float* dxt, float* dyt)
1593 {
1594         // new: first swap coords, then map, then trans/scale
1595         if (tex->type == TEX_IMAGE) {
1596                 // placement
1597                 texvec[0] = mtex->projx ? co[mtex->projx - 1] : 0.f;
1598                 texvec[1] = mtex->projy ? co[mtex->projy - 1] : 0.f;
1599                 texvec[2] = mtex->projz ? co[mtex->projz - 1] : 0.f;
1600
1601                 if (shi->osatex) {
1602                         if (mtex->projx) {
1603                                 dxt[0] = dx[mtex->projx - 1];
1604                                 dyt[0] = dy[mtex->projx - 1];
1605                         }
1606                         else dxt[0] = dyt[0] = 0.f;
1607                         if (mtex->projy) {
1608                                 dxt[1] = dx[mtex->projy - 1];
1609                                 dyt[1] = dy[mtex->projy - 1];
1610                         }
1611                         else dxt[1] = dyt[1] = 0.f;
1612                         if (mtex->projz) {
1613                                 dxt[2] = dx[mtex->projz - 1];
1614                                 dyt[2] = dy[mtex->projz - 1];
1615                         }
1616                         else dxt[2] = dyt[2] = 0.f;
1617                 }
1618                 do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
1619
1620                 // translate and scale
1621                 texvec[0] = mtex->size[0]*(texvec[0] - 0.5f) + mtex->ofs[0] + 0.5f;
1622                 texvec[1] = mtex->size[1]*(texvec[1] - 0.5f) + mtex->ofs[1] + 0.5f;
1623                 if (shi->osatex) {
1624                         dxt[0] = mtex->size[0]*dxt[0];
1625                         dxt[1] = mtex->size[1]*dxt[1];
1626                         dyt[0] = mtex->size[0]*dyt[0];
1627                         dyt[1] = mtex->size[1]*dyt[1];
1628                 }
1629                 
1630                 /* problem: repeat-mirror is not a 'repeat' but 'extend' in imagetexture.c */
1631                 // TXF: bug was here, only modify texvec when repeat mode set, old code affected other modes too.
1632                 // New texfilters solve mirroring differently so that it also works correctly when
1633                 // textures are scaled (sizeXYZ) as well as repeated. See also modification in do_2d_mapping().
1634                 // (since currently only done in osa mode, results will look incorrect without osa TODO) 
1635                 if (tex->extend == TEX_REPEAT && (tex->flag & TEX_REPEAT_XMIR)) {
1636                         if (tex->texfilter == TXF_BOX)
1637                                 texvec[0] -= floorf(texvec[0]); // this line equivalent to old code, same below
1638                         else if (texvec[0] < 0.f || texvec[0] > 1.f) {
1639                                 const float tx = 0.5f*texvec[0];
1640                                 texvec[0] = 2.f*(tx - floorf(tx));
1641                                 if (texvec[0] > 1.f) texvec[0] = 2.f - texvec[0];
1642                         }
1643                 }
1644                 if (tex->extend == TEX_REPEAT && (tex->flag & TEX_REPEAT_YMIR)) {
1645                         if  (tex->texfilter == TXF_BOX)
1646                                 texvec[1] -= floorf(texvec[1]);
1647                         else if (texvec[1] < 0.f || texvec[1] > 1.f) {
1648                                 const float ty = 0.5f*texvec[1];
1649                                 texvec[1] = 2.f*(ty - floorf(ty));
1650                                 if (texvec[1] > 1.f) texvec[1] = 2.f - texvec[1];
1651                         }
1652                 }
1653                 
1654         }
1655         else {  // procedural
1656                 // placement
1657                 texvec[0] = mtex->size[0]*(mtex->projx ? (co[mtex->projx - 1] + mtex->ofs[0]) : mtex->ofs[0]);
1658                 texvec[1] = mtex->size[1]*(mtex->projy ? (co[mtex->projy - 1] + mtex->ofs[1]) : mtex->ofs[1]);
1659                 texvec[2] = mtex->size[2]*(mtex->projz ? (co[mtex->projz - 1] + mtex->ofs[2]) : mtex->ofs[2]);
1660
1661                 if (shi->osatex) {
1662                         if (mtex->projx) {
1663                                 dxt[0] = mtex->size[0]*dx[mtex->projx - 1];
1664                                 dyt[0] = mtex->size[0]*dy[mtex->projx - 1];
1665                         }
1666                         else dxt[0] = dyt[0] = 0.f;
1667                         if (mtex->projy) {
1668                                 dxt[1] = mtex->size[1]*dx[mtex->projy - 1];
1669                                 dyt[1] = mtex->size[1]*dy[mtex->projy - 1];
1670                         }
1671                         else dxt[1] = dyt[1] = 0.f;
1672                         if (mtex->projz) {
1673                                 dxt[2] = mtex->size[2]*dx[mtex->projz - 1];
1674                                 dyt[2] = mtex->size[2]*dy[mtex->projz - 1];
1675                         }
1676                         else dxt[2]= dyt[2] = 0.f;
1677                 }
1678         }
1679 }
1680
1681 /* Bump code from 2.5 development cycle, has a number of bugs, but here for compatibility */
1682
1683 typedef struct CompatibleBump {
1684         float nu[3], nv[3], nn[3];
1685         float dudnu, dudnv, dvdnu, dvdnv;
1686         int nunvdone;
1687 } CompatibleBump;
1688
1689 static void compatible_bump_init(CompatibleBump *compat_bump)
1690 {
1691         memset(compat_bump, 0, sizeof(*compat_bump));
1692
1693         compat_bump->dudnu = 1.0f;
1694         compat_bump->dvdnv = 1.0f;
1695 }
1696
1697 static void compatible_bump_uv_derivs(CompatibleBump *compat_bump, ShadeInput *shi, MTex *mtex, int i)
1698 {
1699         // uvmapping only, calculation of normal tangent u/v partial derivatives
1700         // (should not be here, dudnu, dudnv, dvdnu & dvdnv should probably be part of ShadeInputUV struct,
1701         //  nu/nv in ShadeInput and this calculation should then move to shadeinput.c, shade_input_set_shade_texco() func.)
1702         // NOTE: test for shi->obr->ob here, since vlr/obr/obi can be 'fake' when called from fastshade(), another reason to move it..
1703         // 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.
1704         if ((mtex->texflag & MTEX_COMPAT_BUMP) && shi->obr && shi->obr->ob && shi->v1) {
1705                 if(mtex->mapto & (MAP_NORM|MAP_WARP) && !((mtex->tex->type==TEX_IMAGE) && (mtex->tex->imaflag & TEX_NORMALMAP))) {
1706                         MTFace* tf = RE_vlakren_get_tface(shi->obr, shi->vlr, i, NULL, 0);
1707                         int j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
1708
1709                         vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
1710
1711                         // compute ortho basis around normal
1712                         if(!compat_bump->nunvdone) {
1713                                 // render normal is negated
1714                                 compat_bump->nn[0] = -shi->vn[0];
1715                                 compat_bump->nn[1] = -shi->vn[1];
1716                                 compat_bump->nn[2] = -shi->vn[2];
1717                                 ortho_basis_v3v3_v3(compat_bump->nu, compat_bump->nv, compat_bump->nn);
1718                                 compat_bump->nunvdone= 1;
1719                         }
1720
1721                         if (tf) {
1722                                 float *uv1 = tf->uv[j1], *uv2 = tf->uv[j2], *uv3 = tf->uv[j3];
1723                                 const float an[3] = {fabsf(compat_bump->nn[0]), fabsf(compat_bump->nn[1]), fabsf(compat_bump->nn[2])};
1724                                 const int a1 = (an[0] > an[1] && an[0] > an[2]) ? 1 : 0;
1725                                 const int a2 = (an[2] > an[0] && an[2] > an[1]) ? 1 : 2;
1726                                 const float dp1_a1 = shi->v1->co[a1] - shi->v3->co[a1];
1727                                 const float dp1_a2 = shi->v1->co[a2] - shi->v3->co[a2];
1728                                 const float dp2_a1 = shi->v2->co[a1] - shi->v3->co[a1];
1729                                 const float dp2_a2 = shi->v2->co[a2] - shi->v3->co[a2];
1730                                 const float du1 = uv1[0] - uv3[0], du2 = uv2[0] - uv3[0];
1731                                 const float dv1 = uv1[1] - uv3[1], dv2 = uv2[1] - uv3[1];
1732                                 const float dpdu_a1 = dv2*dp1_a1 - dv1*dp2_a1;
1733                                 const float dpdu_a2 = dv2*dp1_a2 - dv1*dp2_a2;
1734                                 const float dpdv_a1 = du1*dp2_a1 - du2*dp1_a1;
1735                                 const float dpdv_a2 = du1*dp2_a2 - du2*dp1_a2;
1736                                 float d = dpdu_a1*dpdv_a2 - dpdv_a1*dpdu_a2;
1737                                 float uvd = du1*dv2 - dv1*du2;
1738
1739                                 if (uvd == 0.f) uvd = 1e-5f;
1740                                 if (d == 0.f) d = 1e-5f;
1741                                 d = uvd / d;
1742
1743                                 compat_bump->dudnu = (dpdv_a2*compat_bump->nu[a1] - dpdv_a1*compat_bump->nu[a2])*d;
1744                                 compat_bump->dvdnu = (dpdu_a1*compat_bump->nu[a2] - dpdu_a2*compat_bump->nu[a1])*d;
1745                                 compat_bump->dudnv = (dpdv_a2*compat_bump->nv[a1] - dpdv_a1*compat_bump->nv[a2])*d;
1746                                 compat_bump->dvdnv = (dpdu_a1*compat_bump->nv[a2] - dpdu_a2*compat_bump->nv[a1])*d;
1747                         }
1748                 }
1749         }
1750 }
1751
1752 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)
1753 {
1754         TexResult ttexr = {0, 0, 0, 0, 0, texres->talpha, NULL};        // temp TexResult
1755         float tco[3], texv[3], cd, ud, vd, du, dv, idu, idv;
1756         const int fromrgb = ((tex->type == TEX_IMAGE) || ((tex->flag & TEX_COLORBAND)!=0));
1757         const float bf = -0.04f*Tnor*mtex->norfac;
1758         int rgbnor;
1759         // disable internal bump eval
1760         float* nvec = texres->nor;
1761         texres->nor = NULL;
1762         // du & dv estimates, constant value defaults
1763         du = dv = 0.01f;
1764
1765         // compute ortho basis around normal
1766         if(!compat_bump->nunvdone) {
1767                 // render normal is negated
1768                 negate_v3_v3(compat_bump->nn, shi->vn);
1769                 ortho_basis_v3v3_v3(compat_bump->nu, compat_bump->nv, compat_bump->nn);
1770                 compat_bump->nunvdone= 1;
1771         }
1772
1773         // two methods, either constant based on main image resolution,
1774         // (which also works without osa, though of course not always good (or even very bad) results),
1775         // or based on tex derivative max values (osa only). Not sure which is best...
1776
1777         if (!shi->osatex && (tex->type == TEX_IMAGE) && tex->ima) {
1778                 // in case we have no proper derivatives, fall back to
1779                 // computing du/dv it based on image size
1780                 ImBuf* ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1781                 if (ibuf) {
1782                         du = 1.f/(float)ibuf->x;
1783                         dv = 1.f/(float)ibuf->y;
1784                 }
1785         }
1786         else if (shi->osatex) {
1787                 // we have derivatives, can compute proper du/dv
1788                 if (tex->type == TEX_IMAGE) {   // 2d image, use u & v max. of dx/dy 2d vecs
1789                         const float adx[2] = {fabsf(dx[0]), fabsf(dx[1])};
1790                         const float ady[2] = {fabsf(dy[0]), fabsf(dy[1])};
1791                         du = MAX2(adx[0], ady[0]);
1792                         dv = MAX2(adx[1], ady[1]);
1793                 }
1794                 else {  // 3d procedural, estimate from all dx/dy elems
1795                         const float adx[3] = {fabsf(dx[0]), fabsf(dx[1]), fabsf(dx[2])};
1796                         const float ady[3] = {fabsf(dy[0]), fabsf(dy[1]), fabsf(dy[2])};
1797                         du = MAX3(adx[0], adx[1], adx[2]);
1798                         dv = MAX3(ady[0], ady[1], ady[2]);
1799                 }
1800         }
1801
1802         // center, main return value
1803         texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1804         rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, texres);
1805         cd = fromrgb ? (texres->tr + texres->tg + texres->tb)*0.33333333f : texres->tin;
1806
1807         if (mtex->texco == TEXCO_UV) {
1808                 // for the uv case, use the same value for both du/dv,
1809                 // since individually scaling the normal derivatives makes them useless...
1810                 du = MIN2(du, dv);
1811                 idu = (du < 1e-5f) ? bf : (bf/du);
1812
1813                 // +u val
1814                 tco[0] = co[0] + compat_bump->dudnu*du;
1815                 tco[1] = co[1] + compat_bump->dvdnu*du;
1816                 tco[2] = 0.f;
1817                 texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1818                 multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1819                 ud = idu*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1820
1821                 // +v val
1822                 tco[0] = co[0] + compat_bump->dudnv*du;
1823                 tco[1] = co[1] + compat_bump->dvdnv*du;
1824                 tco[2] = 0.f;
1825                 texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1826                 multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1827                 vd = idu*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1828         }
1829         else {
1830                 float tu[3], tv[3];
1831
1832                 copy_v3_v3(tu, compat_bump->nu);
1833                 copy_v3_v3(tv, compat_bump->nv);
1834
1835                 idu = (du < 1e-5f) ? bf : (bf/du);
1836                 idv = (dv < 1e-5f) ? bf : (bf/dv);
1837
1838                 if ((mtex->texco == TEXCO_ORCO) && shi->obr && shi->obr->ob) {
1839                         mul_mat3_m4_v3(shi->obr->ob->imat_ren, tu);
1840                         mul_mat3_m4_v3(shi->obr->ob->imat_ren, tv);
1841                         normalize_v3(tu);
1842                         normalize_v3(tv);
1843                 }
1844                 else if (mtex->texco == TEXCO_GLOB) {
1845                         mul_mat3_m4_v3(R.viewinv, tu);
1846                         mul_mat3_m4_v3(R.viewinv, tv);
1847                 }
1848                 else if (mtex->texco == TEXCO_OBJECT && mtex->object) {
1849                         mul_mat3_m4_v3(mtex->object->imat_ren, tu);
1850                         mul_mat3_m4_v3(mtex->object->imat_ren, tv);
1851                         normalize_v3(tu);
1852                         normalize_v3(tv);
1853                 }
1854
1855                 // +u val
1856                 tco[0] = co[0] + tu[0]*du;
1857                 tco[1] = co[1] + tu[1]*du;
1858                 tco[2] = co[2] + tu[2]*du;
1859                 texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1860                 multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1861                 ud = idu*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1862
1863                 // +v val
1864                 tco[0] = co[0] + tv[0]*dv;
1865                 tco[1] = co[1] + tv[1]*dv;
1866                 tco[2] = co[2] + tv[2]*dv;
1867                 texco_mapping(shi, tex, mtex, tco, dx, dy, texv, dxt, dyt);
1868                 multitex_mtex(shi, mtex, texv, dxt, dyt, &ttexr);
1869                 vd = idv*(cd - (fromrgb ? (ttexr.tr + ttexr.tg + ttexr.tb)*0.33333333f : ttexr.tin));
1870         }
1871
1872         // bumped normal
1873         compat_bump->nu[0] += ud*compat_bump->nn[0];
1874         compat_bump->nu[1] += ud*compat_bump->nn[1];
1875         compat_bump->nu[2] += ud*compat_bump->nn[2];
1876         compat_bump->nv[0] += vd*compat_bump->nn[0];
1877         compat_bump->nv[1] += vd*compat_bump->nn[1];
1878         compat_bump->nv[2] += vd*compat_bump->nn[2];
1879         cross_v3_v3v3(nvec, compat_bump->nu, compat_bump->nv);
1880
1881         nvec[0] = -nvec[0];
1882         nvec[1] = -nvec[1];
1883         nvec[2] = -nvec[2];
1884         texres->nor = nvec;
1885
1886         rgbnor |= TEX_NOR;
1887         return rgbnor;
1888 }
1889
1890 /* Improved bump code from later in 2.5 development cycle */
1891
1892 typedef struct NTapBump {
1893         int init_done;
1894         int iPrevBumpSpace;     // 0: uninitialized, 1: objectspace, 2: texturespace, 4: viewspace
1895         // bumpmapping
1896         float vNorg[3]; // backup copy of shi->vn
1897         float vNacc[3]; // original surface normal minus the surface gradient of every bump map which is encountered
1898         float vR1[3], vR2[3]; // cross products (sigma_y, original_normal), (original_normal, sigma_x)
1899         float sgn_det; // sign of the determinant of the matrix {sigma_x, sigma_y, original_normal}
1900         float fPrevMagnitude; // copy of previous magnitude, used for multiple bumps in different spaces
1901 } NTapBump;
1902
1903 static void ntap_bump_init(NTapBump *ntap_bump)
1904 {
1905         memset(ntap_bump, 0, sizeof(*ntap_bump));
1906 }
1907
1908 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)
1909 {
1910         TexResult ttexr = {0, 0, 0, 0, 0, texres->talpha, NULL};        // temp TexResult
1911
1912         const int fromrgb = ((tex->type == TEX_IMAGE) || ((tex->flag & TEX_COLORBAND)!=0));
1913
1914         // The negate on Hscale is done because the
1915         // normal in the renderer points inward which corresponds
1916         // to inverting the bump map. The normals are generated
1917         // this way in calc_vertexnormals(). Should this ever change
1918         // this negate must be removed.
1919         float Hscale = -Tnor*mtex->norfac;
1920
1921         int dimx=512, dimy=512;
1922         const int imag_tspace_dimension_x = 1024;               // only used for texture space variant
1923         float aspect = 1.0f;
1924
1925         // 2 channels for 2D texture and 3 for 3D textures.
1926         const int nr_channels = (mtex->texco == TEXCO_UV)? 2 : 3;
1927         int c, rgbnor, iBumpSpace;
1928         float dHdx, dHdy;
1929         int found_deriv_map = (tex->type==TEX_IMAGE) && (tex->imaflag & TEX_DERIVATIVEMAP);
1930
1931         // disable internal bump eval in sampler, save pointer
1932         float *nvec = texres->nor;
1933         texres->nor = NULL;
1934
1935         if(found_deriv_map==0) {
1936                 if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
1937                         if(tex->ima)
1938                                 Hscale *= 13.0f; // appears to be a sensible default value
1939                 } else
1940                         Hscale *= 0.1f; // factor 0.1 proved to look like the previous bump code
1941         }
1942
1943         if( !ntap_bump->init_done ) {
1944                 copy_v3_v3(ntap_bump->vNacc, shi->vn);
1945                 copy_v3_v3(ntap_bump->vNorg, shi->vn);
1946                 ntap_bump->fPrevMagnitude = 1.0f;
1947                 ntap_bump->iPrevBumpSpace = 0;
1948                 
1949                 ntap_bump->init_done = 1;
1950         }
1951
1952         // resolve image dimensions
1953         if(found_deriv_map || (mtex->texflag&MTEX_BUMP_TEXTURESPACE)!=0) {
1954                 ImBuf* ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
1955                 if (ibuf) {
1956                         dimx = ibuf->x;
1957                         dimy = ibuf->y;
1958                         aspect = ((float) dimy) / dimx;
1959                 }
1960         }
1961         
1962         if(found_deriv_map) {
1963                 float dBdu, dBdv, auto_bump = 1.0f;
1964                 float s = 1;            // negate this if flipped texture coordinate
1965                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1966                 rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, texres);
1967
1968                 if(shi->obr->ob->derivedFinal) {
1969                         auto_bump = shi->obr->ob->derivedFinal->auto_bump_scale;
1970                 }
1971
1972                 {
1973                         float fVirtDim = sqrtf(fabsf((float) (dimx*dimy)*mtex->size[0]*mtex->size[1]));
1974                         auto_bump /= MAX2(fVirtDim, FLT_EPSILON);
1975                 }
1976                 
1977                 // this variant using a derivative map is described here
1978                 // http://mmikkelsen3d.blogspot.com/2011/07/derivative-maps.html
1979                 dBdu = auto_bump*Hscale*dimx*(2*texres->tr-1);
1980                 dBdv = auto_bump*Hscale*dimy*(2*texres->tg-1);
1981
1982                 dHdx = dBdu*dxt[0] + s * dBdv*dxt[1];
1983                 dHdy = dBdu*dyt[0] + s * dBdv*dyt[1];
1984         }
1985         else if(!(mtex->texflag & MTEX_5TAP_BUMP)) {
1986                 // compute height derivatives with respect to output image pixel coordinates x and y
1987                 float STll[3], STlr[3], STul[3];
1988                 float Hll, Hlr, Hul;
1989
1990                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
1991
1992                 for(c=0; c<nr_channels; c++) {
1993                         // dx contains the derivatives (du/dx, dv/dx)
1994                         // dy contains the derivatives (du/dy, dv/dy)
1995                         STll[c] = texvec[c];
1996                         STlr[c] = texvec[c]+dxt[c];
1997                         STul[c] = texvec[c]+dyt[c];
1998                 }
1999
2000                 // clear unused derivatives
2001                 for(c=nr_channels; c<3; c++) {
2002                         STll[c] = 0.0f;
2003                         STlr[c] = 0.0f;
2004                         STul[c] = 0.0f;
2005                 }
2006
2007                 // use texres for the center sample, set rgbnor
2008                 rgbnor = multitex_mtex(shi, mtex, STll, dxt, dyt, texres);
2009                 Hll = (fromrgb)? RGBTOBW(texres->tr, texres->tg, texres->tb) : texres->tin;
2010
2011                 // use ttexr for the other 2 taps
2012                 multitex_mtex(shi, mtex, STlr, dxt, dyt, &ttexr);
2013                 Hlr = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2014
2015                 multitex_mtex(shi, mtex, STul, dxt, dyt, &ttexr);
2016                 Hul = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2017
2018                 dHdx = Hscale*(Hlr - Hll);
2019                 dHdy = Hscale*(Hul - Hll);
2020         }
2021         else {
2022                 /* same as above, but doing 5 taps, increasing quality at cost of speed */
2023                 float STc[3], STl[3], STr[3], STd[3], STu[3];
2024                 float /* Hc, */ /* UNUSED */  Hl, Hr, Hd, Hu;
2025
2026                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
2027
2028                 for(c=0; c<nr_channels; c++) {
2029                         STc[c] = texvec[c];
2030                         STl[c] = texvec[c] - 0.5f*dxt[c];
2031                         STr[c] = texvec[c] + 0.5f*dxt[c];
2032                         STd[c] = texvec[c] - 0.5f*dyt[c];
2033                         STu[c] = texvec[c] + 0.5f*dyt[c];
2034                 }
2035
2036                 // clear unused derivatives
2037                 for(c=nr_channels; c<3; c++) {
2038                         STc[c] = 0.0f;
2039                         STl[c] = 0.0f;
2040                         STr[c] = 0.0f;
2041                         STd[c] = 0.0f;
2042                         STu[c] = 0.0f;
2043                 }
2044
2045                 // use texres for the center sample, set rgbnor
2046                 rgbnor = multitex_mtex(shi, mtex, STc, dxt, dyt, texres);
2047                 /* Hc = (fromrgb)? RGBTOBW(texres->tr, texres->tg, texres->tb) : texres->tin; */ /* UNUSED */
2048
2049                 // use ttexr for the other taps
2050                 multitex_mtex(shi, mtex, STl, dxt, dyt, &ttexr);
2051                 Hl = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2052                 multitex_mtex(shi, mtex, STr, dxt, dyt, &ttexr);
2053                 Hr = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2054                 multitex_mtex(shi, mtex, STd, dxt, dyt, &ttexr);
2055                 Hd = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2056                 multitex_mtex(shi, mtex, STu, dxt, dyt, &ttexr);
2057                 Hu = (fromrgb)? RGBTOBW(ttexr.tr, ttexr.tg, ttexr.tb) : ttexr.tin;
2058
2059                 dHdx = Hscale*(Hr - Hl);
2060                 dHdy = Hscale*(Hu - Hd);
2061         }
2062
2063         // restore pointer
2064         texres->nor = nvec;
2065
2066         /* replaced newbump with code based on listing 1 and 2 of
2067          * [Mik10] Mikkelsen M. S.: Bump Mapping Unparametrized Surfaces on the GPU.
2068          * -> http://jbit.net/~sparky/sfgrad_bump/mm_sfgrad_bump.pdf */
2069
2070         if( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
2071                 iBumpSpace = 1;
2072         else if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
2073                 iBumpSpace = 2;
2074         else
2075                 iBumpSpace = 4; // ViewSpace
2076         
2077         if( ntap_bump->iPrevBumpSpace != iBumpSpace ) {
2078                 
2079                 // initialize normal perturbation vectors
2080                 int xyz;
2081                 float fDet, abs_fDet, fMagnitude;
2082                 // object2view and inverted matrix
2083                 float obj2view[3][3], view2obj[3][3], tmp[4][4];
2084                 // local copies of derivatives and normal
2085                 float dPdx[3], dPdy[3], vN[3];
2086                 copy_v3_v3(dPdx, shi->dxco);
2087                 copy_v3_v3(dPdy, shi->dyco);
2088                 copy_v3_v3(vN, ntap_bump->vNorg);
2089                 
2090                 if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
2091                         // TODO: these calculations happen for every pixel!
2092                         //      -> move to shi->obi
2093                         mult_m4_m4m4(tmp, R.viewmat, shi->obr->ob->obmat);
2094                         copy_m3_m4(obj2view, tmp); // use only upper left 3x3 matrix
2095                         invert_m3_m3(view2obj, obj2view);
2096                 
2097                         // generate the surface derivatives in object space
2098                         mul_m3_v3(view2obj, dPdx);
2099                         mul_m3_v3( view2obj, dPdy );
2100                         // generate the unit normal in object space
2101                         mul_transposed_m3_v3( obj2view, vN );
2102                         normalize_v3(vN);
2103                 }
2104                 
2105                 cross_v3_v3v3(ntap_bump->vR1, dPdy, vN);
2106                 cross_v3_v3v3(ntap_bump->vR2, vN, dPdx);
2107                 fDet = dot_v3v3(dPdx, ntap_bump->vR1);
2108                 ntap_bump->sgn_det = (fDet < 0)? -1.0f: 1.0f;
2109                 abs_fDet = ntap_bump->sgn_det * fDet;
2110
2111                 if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
2112                         if(tex->ima) {
2113                                 // crazy hack solution that gives results similar to normal mapping - part 1
2114                                 normalize_v3(ntap_bump->vR1);
2115                                 normalize_v3(ntap_bump->vR2);
2116                                 abs_fDet = 1.0f;
2117                         }
2118                 }
2119                 
2120                 fMagnitude = abs_fDet;
2121                 if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
2122                         // pre do transform of texres->nor by the inverse transposed of obj2view
2123                         mul_transposed_m3_v3( view2obj, vN );
2124                         mul_transposed_m3_v3( view2obj, ntap_bump->vR1 );
2125                         mul_transposed_m3_v3( view2obj, ntap_bump->vR2 );
2126                         
2127                         fMagnitude *= len_v3(vN);
2128                 }
2129                 
2130                 if(ntap_bump->fPrevMagnitude > 0.0f)
2131                         for(xyz=0; xyz<3; xyz++)
2132                                 ntap_bump->vNacc[xyz] *= fMagnitude / ntap_bump->fPrevMagnitude;
2133                 
2134                 ntap_bump->fPrevMagnitude = fMagnitude;
2135                 ntap_bump->iPrevBumpSpace = iBumpSpace;
2136         }
2137
2138         if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
2139                 if(tex->ima) {
2140                         // crazy hack solution that gives results similar to normal mapping - part 2
2141                         float vec[2];
2142                         const float imag_tspace_dimension_y = aspect*imag_tspace_dimension_x;
2143                         
2144                         vec[0] = imag_tspace_dimension_x*dxt[0];
2145                         vec[1] = imag_tspace_dimension_y*dxt[1];
2146                         dHdx *= 1.0f/len_v2(vec);
2147                         vec[0] = imag_tspace_dimension_x*dyt[0];
2148                         vec[1] = imag_tspace_dimension_y*dyt[1];
2149                         dHdy *= 1.0f/len_v2(vec);
2150                 }
2151         }
2152         
2153         // subtract the surface gradient from vNacc
2154         for(c=0; c<3; c++) {
2155                 float vSurfGrad_compi = ntap_bump->sgn_det * (dHdx * ntap_bump->vR1[c] + dHdy * ntap_bump->vR2[c]);
2156                 ntap_bump->vNacc[c] -= vSurfGrad_compi;
2157                 texres->nor[c] = ntap_bump->vNacc[c]; // copy
2158         }
2159
2160         rgbnor |= TEX_NOR;
2161         return rgbnor;
2162 }
2163
2164 void do_material_tex(ShadeInput *shi, Render *re)
2165 {
2166         CompatibleBump compat_bump;
2167         NTapBump ntap_bump;
2168         MTex *mtex;
2169         Tex *tex;
2170         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2171         float *co = NULL, *dx = NULL, *dy = NULL;
2172         float fact, facm, factt, facmm, stencilTin=1.0;
2173         float texvec[3], dxt[3], dyt[3], tempvec[3], norvec[3], warpvec[3]={0.0f, 0.0f, 0.0f}, Tnor=1.0;
2174         int tex_nr, rgbnor= 0, warpdone=0;
2175         int use_compat_bump = 0, use_ntap_bump = 0;
2176         int found_nmapping = 0, found_deriv_map = 0;
2177         int iFirstTimeNMap=1;
2178
2179         compatible_bump_init(&compat_bump);
2180         ntap_bump_init(&ntap_bump);
2181
2182         if (re->r.scemode & R_NO_TEX) return;
2183         /* here: test flag if there's a tex (todo) */
2184
2185         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2186                 
2187                 /* separate tex switching */
2188                 if(shi->mat->septex & (1<<tex_nr)) continue;
2189                 
2190                 if(shi->mat->mtex[tex_nr]) {
2191                         mtex= shi->mat->mtex[tex_nr];
2192                         
2193                         tex= mtex->tex;
2194                         if(tex==0) continue;
2195
2196                         found_deriv_map = (tex->type==TEX_IMAGE) && (tex->imaflag & TEX_DERIVATIVEMAP);
2197                         use_compat_bump= (mtex->texflag & MTEX_COMPAT_BUMP);
2198                         use_ntap_bump= ((mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP|MTEX_BICUBIC_BUMP))!=0 || found_deriv_map!=0) ? 1 : 0;
2199
2200                         /* XXX texture node trees don't work for this yet */
2201                         if(tex->nodetree && tex->use_nodes) {
2202                                 use_compat_bump = 0;
2203                                 use_ntap_bump = 0;
2204                         }
2205                         
2206                         /* case displacement mapping */
2207                         if(shi->osatex==0 && use_ntap_bump) {
2208                                 use_ntap_bump = 0;
2209                                 use_compat_bump = 1;
2210                         }
2211                         
2212                         /* case ocean */
2213                         if(tex->type == TEX_OCEAN) {
2214                                 use_ntap_bump = 0;
2215                                 use_compat_bump = 0;
2216                         }
2217
2218                         /* which coords */
2219                         if(mtex->texco==TEXCO_ORCO) {
2220                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2221                                         co= shi->duplilo; dx= dxt; dy= dyt;
2222                                         dxt[0]= dxt[1]= dxt[2]= 0.0f;
2223                                         dyt[0]= dyt[1]= dyt[2]= 0.0f;
2224                                 }
2225                                 else {
2226                                         co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
2227                                 }
2228                         }
2229                         else if(mtex->texco==TEXCO_STICKY) {
2230                                 co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
2231                         }
2232                         else if(mtex->texco==TEXCO_OBJECT) {
2233                                 Object *ob= mtex->object;
2234                                 if(ob) {
2235                                         co= tempvec;
2236                                         dx= dxt;
2237                                         dy= dyt;
2238                                         copy_v3_v3(tempvec, shi->co);
2239                                         if(mtex->texflag & MTEX_OB_DUPLI_ORIG)
2240                                                 if(shi->obi && shi->obi->duplitexmat)
2241                                                         mul_m4_v3(shi->obi->duplitexmat, tempvec);
2242                                         mul_m4_v3(ob->imat_ren, tempvec);
2243                                         if(shi->osatex) {
2244                                                 copy_v3_v3(dxt, shi->dxco);
2245                                                 copy_v3_v3(dyt, shi->dyco);
2246                                                 mul_mat3_m4_v3(ob->imat_ren, dxt);
2247                                                 mul_mat3_m4_v3(ob->imat_ren, dyt);
2248                                         }
2249                                 }
2250                                 else {
2251                                         /* if object doesn't exist, do not use orcos (not initialized) */
2252                                         co= shi->co;
2253                                         dx= shi->dxco; dy= shi->dyco;
2254                                 }
2255                         }
2256                         else if(mtex->texco==TEXCO_REFL) {
2257                                 calc_R_ref(shi);
2258                                 co= shi->ref; dx= shi->dxref; dy= shi->dyref;
2259                         }
2260                         else if(mtex->texco==TEXCO_NORM) {
2261                                 co= shi->orn; dx= shi->dxno; dy= shi->dyno;
2262                         }
2263                         else if(mtex->texco==TEXCO_TANGENT) {
2264                                 co= shi->tang; dx= shi->dxno; dy= shi->dyno;
2265                         }
2266                         else if(mtex->texco==TEXCO_GLOB) {
2267                                 co= shi->gl; dx= shi->dxgl; dy= shi->dygl;
2268                         }
2269                         else if(mtex->texco==TEXCO_UV) {
2270                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2271                                         co= shi->dupliuv; dx= dxt; dy= dyt;
2272                                         dxt[0]= dxt[1]= dxt[2]= 0.0f;
2273                                         dyt[0]= dyt[1]= dyt[2]= 0.0f;
2274                                 }
2275                                 else {
2276                                         ShadeInputUV *suv= &shi->uv[shi->actuv];
2277                                         int i = shi->actuv;
2278
2279                                         if(mtex->uvname[0] != 0) {
2280                                                 for(i = 0; i < shi->totuv; i++) {
2281                                                         if(strcmp(shi->uv[i].name, mtex->uvname)==0) {
2282                                                                 suv= &shi->uv[i];
2283                                                                 break;
2284                                                         }
2285                                                 }
2286                                         }
2287
2288                                         co= suv->uv;
2289                                         dx= suv->dxuv;
2290                                         dy= suv->dyuv; 
2291
2292                                         compatible_bump_uv_derivs(&compat_bump, shi, mtex, i);
2293                                 }
2294                         }
2295                         else if(mtex->texco==TEXCO_WINDOW) {
2296                                 co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
2297                         }
2298                         else if(mtex->texco==TEXCO_STRAND) {
2299                                 co= tempvec; dx= dxt; dy= dyt;
2300                                 co[0]= shi->strandco;
2301                                 co[1]= co[2]= 0.0f;
2302                                 dx[0]= shi->dxstrand;
2303                                 dx[1]= dx[2]= 0.0f;
2304                                 dy[0]= shi->dystrand;
2305                                 dy[1]= dy[2]= 0.0f;
2306                         }
2307                         else if(mtex->texco==TEXCO_STRESS) {
2308                                 co= tempvec; dx= dxt; dy= dyt;
2309                                 co[0]= shi->stress;
2310                                 co[1]= co[2]= 0.0f;
2311                                 dx[0]= 0.0f;
2312                                 dx[1]= dx[2]= 0.0f;
2313                                 dy[0]= 0.0f;
2314                                 dy[1]= dy[2]= 0.0f;
2315                         }
2316                         else continue;  // can happen when texco defines disappear and it renders old files
2317
2318                         /* the pointer defines if bumping happens */
2319                         if(mtex->mapto & (MAP_NORM|MAP_WARP)) {
2320                                 texres.nor= norvec;
2321                                 norvec[0]= norvec[1]= norvec[2]= 0.0;
2322                         }
2323                         else texres.nor= NULL;
2324                         
2325                         if(warpdone) {
2326                                 add_v3_v3v3(tempvec, co, warpvec);
2327                                 co= tempvec;
2328                         }
2329
2330                         /* XXX texture node trees don't work for this yet */
2331                         if(texres.nor && !((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP))) {
2332                                 if(use_compat_bump) {
2333                                         rgbnor = compatible_bump_compute(&compat_bump, shi, mtex, tex,
2334                                                 &texres, Tnor*stencilTin, co, dx, dy, texvec, dxt, dyt);
2335                                 }
2336                                 else if(use_ntap_bump) {
2337                                         rgbnor = ntap_bump_compute(&ntap_bump, shi, mtex, tex,
2338                                                 &texres, Tnor*stencilTin, co, dx, dy, texvec, dxt, dyt);
2339                                 }
2340                                 else {
2341                                         texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
2342                                         rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
2343                                 }
2344                         }
2345                         else {
2346                                 texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
2347                                 rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, &texres);
2348                         }
2349
2350                         /* texture output */
2351
2352                         if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
2353                                 texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2354                                 rgbnor-= TEX_RGB;
2355                         }
2356                         if(mtex->texflag & MTEX_NEGATIVE) {
2357                                 if(rgbnor & TEX_RGB) {
2358                                         texres.tr= 1.0f-texres.tr;
2359                                         texres.tg= 1.0f-texres.tg;
2360                                         texres.tb= 1.0f-texres.tb;
2361                                 }
2362                                 texres.tin= 1.0f-texres.tin;
2363                         }
2364                         if(mtex->texflag & MTEX_STENCIL) {
2365                                 if(rgbnor & TEX_RGB) {
2366                                         fact= texres.ta;
2367                                         texres.ta*= stencilTin;
2368                                         stencilTin*= fact;
2369                                 }
2370                                 else {
2371                                         fact= texres.tin;
2372                                         texres.tin*= stencilTin;
2373                                         stencilTin*= fact;
2374                                 }
2375                         }
2376                         else {
2377                                 Tnor*= stencilTin;
2378                         }
2379                         
2380                         if(texres.nor) {
2381                                 if((rgbnor & TEX_NOR)==0) {
2382                                         /* make our own normal */
2383                                         if(rgbnor & TEX_RGB) {
2384                                                 texres.nor[0]= texres.tr;
2385                                                 texres.nor[1]= texres.tg;
2386                                                 texres.nor[2]= texres.tb;
2387                                         }
2388                                         else {
2389                                                 float co_nor= 0.5*cos(texres.tin-0.5f);
2390                                                 float si= 0.5*sin(texres.tin-0.5f);
2391                                                 float f1, f2;
2392
2393                                                 f1= shi->vn[0];
2394                                                 f2= shi->vn[1];
2395                                                 texres.nor[0]= f1*co_nor+f2*si;
2396                                                 f1= shi->vn[1];
2397                                                 f2= shi->vn[2];
2398                                                 texres.nor[1]= f1*co_nor+f2*si;
2399                                                 texres.nor[2]= f2*co_nor-f1*si;
2400                                         }
2401                                 }
2402                                 // warping, local space
2403                                 if(mtex->mapto & MAP_WARP) {
2404                                         float *warpnor= texres.nor, warpnor_[3];
2405                                         
2406                                         if(use_ntap_bump) {
2407                                                 copy_v3_v3(warpnor_, texres.nor);
2408                                                 warpnor= warpnor_;
2409                                                 normalize_v3(warpnor_);
2410                                         }
2411                                         warpvec[0]= mtex->warpfac*warpnor[0];
2412                                         warpvec[1]= mtex->warpfac*warpnor[1];
2413                                         warpvec[2]= mtex->warpfac*warpnor[2];
2414                                         warpdone= 1;
2415                                 }
2416 #if 0                           
2417                                 if(mtex->texflag & MTEX_VIEWSPACE) {
2418                                         // rotate to global coords
2419                                         if(mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
2420                                                 if(shi->vlr && shi->obr && shi->obr->ob) {
2421                                                         float len= normalize_v3(texres.nor);
2422                                                         // can be optimized... (ton)
2423                                                         mul_mat3_m4_v3(shi->obr->ob->obmat, texres.nor);
2424                                                         mul_mat3_m4_v3(re->viewmat, texres.nor);
2425                                                         normalize_v3(texres.nor);
2426                                                         mul_v3_fl(texres.nor, len);
2427                                                 }
2428                                         }
2429                                 }
2430 #endif                          
2431                         }
2432
2433                         /* mapping */
2434                         if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
2435                                 float tcol[3];
2436                                 
2437                                 /* stencil maps on the texture control slider, not texture intensity value */
2438                                 
2439                                 tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
2440                                 
2441                                 if((rgbnor & TEX_RGB)==0) {
2442                                         tcol[0]= mtex->r;
2443                                         tcol[1]= mtex->g;
2444                                         tcol[2]= mtex->b;
2445                                 }
2446                                 else if(mtex->mapto & MAP_ALPHA) {
2447                                         texres.tin= stencilTin;
2448                                 }
2449                                 else texres.tin= texres.ta;
2450                                 
2451                                 /* inverse gamma correction */
2452                                 if (tex->type==TEX_IMAGE) {
2453                                         Image *ima = tex->ima;
2454                                         ImBuf *ibuf = BKE_image_get_ibuf(ima, &tex->iuser);
2455                                         
2456                                         /* don't linearize float buffers, assumed to be linear */
2457                                         if (ibuf && !(ibuf->rect_float) && re->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2458                                                 srgb_to_linearrgb_v3_v3(tcol, tcol);
2459                                 }
2460                                 
2461                                 if(mtex->mapto & MAP_COL) {
2462                                         float colfac= mtex->colfac*stencilTin;
2463                                         texture_rgb_blend(&shi->r, tcol, &shi->r, texres.tin, colfac, mtex->blendtype);
2464                                 }
2465                                 if(mtex->mapto & MAP_COLSPEC) {
2466                                         float colspecfac= mtex->colspecfac*stencilTin;
2467                                         texture_rgb_blend(&shi->specr, tcol, &shi->specr, texres.tin, colspecfac, mtex->blendtype);
2468                                 }
2469                                 if(mtex->mapto & MAP_COLMIR) {
2470                                         float mirrfac= mtex->mirrfac*stencilTin;
2471
2472                                         // exception for envmap only
2473                                         if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
2474                                                 fact= texres.tin*mirrfac;
2475                                                 facm= 1.0f- fact;
2476                                                 shi->refcol[0]= fact + facm*shi->refcol[0];
2477                                                 shi->refcol[1]= fact*tcol[0] + facm*shi->refcol[1];
2478                                                 shi->refcol[2]= fact*tcol[1] + facm*shi->refcol[2];
2479                                                 shi->refcol[3]= fact*tcol[2] + facm*shi->refcol[3];
2480                                         }
2481                                         else {
2482                                                 texture_rgb_blend(&shi->mirr, tcol, &shi->mirr, texres.tin, mirrfac, mtex->blendtype);
2483                                         }
2484                                 }
2485                         }
2486                         if( (mtex->mapto & MAP_NORM) ) {
2487                                 if(texres.nor) {
2488                                         float norfac= mtex->norfac;
2489                                         
2490                                         /* we need to code blending modes for normals too once.. now 1 exception hardcoded */
2491                                         
2492                                         if ((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP)) {
2493                                                 
2494                                                 found_nmapping = 1;
2495                                                 
2496                                                 /* qdn: for normalmaps, to invert the normalmap vector,
2497                                                  * it is better to negate x & y instead of subtracting the vector as was done before */
2498                                                 if (norfac < 0.0f) {
2499                                                         texres.nor[0] = -texres.nor[0];
2500                                                         texres.nor[1] = -texres.nor[1];
2501                                                 }
2502                                                 fact = Tnor*fabsf(norfac);
2503                                                 if (fact>1.f) fact = 1.f;
2504                                                 facm = 1.f-fact;
2505                                                 if(mtex->normapspace == MTEX_NSPACE_TANGENT) {
2506                                                         /* qdn: tangent space */
2507                                                         float B[3], tv[3];
2508                                                         const float * no = iFirstTimeNMap!=0 ? shi->nmapnorm : shi->vn;
2509                                                         iFirstTimeNMap=0;
2510                                                         cross_v3_v3v3(B, no, shi->nmaptang);    /* bitangent */
2511                                                         mul_v3_fl(B, shi->nmaptang[3]);
2512                                                         /* transform norvec from tangent space to object surface in camera space */
2513                                                         tv[0] = texres.nor[0]*shi->nmaptang[0] + texres.nor[1]*B[0] + texres.nor[2]*no[0];
2514                                                         tv[1] = texres.nor[0]*shi->nmaptang[1] + texres.nor[1]*B[1] + texres.nor[2]*no[1];
2515                                                         tv[2] = texres.nor[0]*shi->nmaptang[2] + texres.nor[1]*B[2] + texres.nor[2]*no[2];
2516                                                         shi->vn[0]= facm*no[0] + fact*tv[0];
2517                                                         shi->vn[1]= facm*no[1] + fact*tv[1];
2518                                                         shi->vn[2]= facm*no[2] + fact*tv[2];
2519                                                 }
2520                                                 else {
2521                                                         float nor[3];
2522
2523                                                         copy_v3_v3(nor, texres.nor);
2524
2525                                                         if(mtex->normapspace == MTEX_NSPACE_CAMERA);
2526                                                         else if(mtex->normapspace == MTEX_NSPACE_WORLD) {
2527                                                                 mul_mat3_m4_v3(re->viewmat, nor);
2528                                                         }
2529                                                         else if(mtex->normapspace == MTEX_NSPACE_OBJECT) {
2530                                                                 if(shi->obr && shi->obr->ob)
2531                                                                         mul_mat3_m4_v3(shi->obr->ob->obmat, nor);
2532                                                                 mul_mat3_m4_v3(re->viewmat, nor);
2533                                                         }
2534
2535                                                         normalize_v3(nor);
2536
2537                                                         /* qdn: worldspace */
2538                                                         shi->vn[0]= facm*shi->vn[0] + fact*nor[0];
2539                                                         shi->vn[1]= facm*shi->vn[1] + fact*nor[1];
2540                                                         shi->vn[2]= facm*shi->vn[2] + fact*nor[2];
2541                                                 }
2542                                         }
2543                                         else {
2544                                                 /* XXX texture node trees don't work for this yet */
2545                                                 if (use_compat_bump || use_ntap_bump) {
2546                                                         shi->vn[0] = texres.nor[0];
2547                                                         shi->vn[1] = texres.nor[1];
2548                                                         shi->vn[2] = texres.nor[2];
2549                                                 }
2550                                                 else {
2551                                                         float nor[3], dot;
2552         
2553                                                         if(shi->mat->mode & MA_TANGENT_V) {
2554                                                                 shi->tang[0]+= Tnor*norfac*texres.nor[0];
2555                                                                 shi->tang[1]+= Tnor*norfac*texres.nor[1];
2556                                                                 shi->tang[2]+= Tnor*norfac*texres.nor[2];
2557                                                         }
2558         
2559                                                         /* prevent bump to become negative normal */
2560                                                         nor[0]= Tnor*norfac*texres.nor[0];
2561                                                         nor[1]= Tnor*norfac*texres.nor[1];
2562                                                         nor[2]= Tnor*norfac*texres.nor[2];
2563                                                         
2564                                                         dot= 0.5f + 0.5f * dot_v3v3(nor, shi->vn);
2565                                                         
2566                                                         shi->vn[0]+= dot*nor[0];
2567                                                         shi->vn[1]+= dot*nor[1];
2568                                                         shi->vn[2]+= dot*nor[2];
2569                                                 }
2570                                         }
2571                                         normalize_v3(shi->vn);
2572                                         
2573                                         /* this makes sure the bump is passed on to the next texture */
2574                                         shi->orn[0]= -shi->vn[0];
2575                                         shi->orn[1]= -shi->vn[1];
2576                                         shi->orn[2]= -shi->vn[2];
2577                                 }
2578                         }
2579
2580                         if( mtex->mapto & MAP_DISPLACE ) {
2581                                 /* Now that most textures offer both Nor and Intensity, allow  */
2582                                 /* both to work, and let user select with slider.   */
2583                                 if(texres.nor) {
2584                                         float norfac= mtex->norfac;
2585
2586                                         shi->displace[0]+= 0.2f*Tnor*norfac*texres.nor[0];
2587                                         shi->displace[1]+= 0.2f*Tnor*norfac*texres.nor[1];
2588                                         shi->displace[2]+= 0.2f*Tnor*norfac*texres.nor[2];
2589                                 }
2590                                 
2591                                 if(rgbnor & TEX_RGB) {
2592                                         texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2593                                 }
2594
2595                                 factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
2596
2597                                 if(mtex->blendtype==MTEX_BLEND) {
2598                                         shi->displace[0]= factt*shi->vn[0] + facmm*shi->displace[0];
2599                                         shi->displace[1]= factt*shi->vn[1] + facmm*shi->displace[1];
2600                                         shi->displace[2]= factt*shi->vn[2] + facmm*shi->displace[2];
2601                                 }
2602                                 else if(mtex->blendtype==MTEX_MUL) {
2603                                         shi->displace[0]*= factt*shi->vn[0];
2604                                         shi->displace[1]*= factt*shi->vn[1];
2605                                         shi->displace[2]*= factt*shi->vn[2];
2606                                 }
2607                                 else { /* add or sub */
2608                                         if(mtex->blendtype==MTEX_SUB) factt= -factt;
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                         }
2614
2615                         if(mtex->mapto & MAP_VARS) {
2616                                 /* stencil maps on the texture control slider, not texture intensity value */
2617                                 
2618                                 if(rgbnor & TEX_RGB) {
2619                                         if(texres.talpha) texres.tin= texres.ta;
2620                                         else texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
2621                                 }
2622
2623                                 if(mtex->mapto & MAP_REF) {
2624                                         float difffac= mtex->difffac*stencilTin;
2625
2626                                         shi->refl= texture_value_blend(mtex->def_var, shi->refl, texres.tin, difffac, mtex->blendtype);
2627                                         if(shi->refl<0.0f) shi->refl= 0.0f;
2628                                 }
2629                                 if(mtex->mapto & MAP_SPEC) {
2630                                         float specfac= mtex->specfac*stencilTin;
2631                                         
2632                                         shi->spec= texture_value_blend(mtex->def_var, shi->spec, texres.tin, specfac, mtex->blendtype);
2633                                         if(shi->spec<0.0f) shi->spec= 0.0f;
2634                                 }
2635                                 if(mtex->mapto & MAP_EMIT) {
2636                                         float emitfac= mtex->emitfac*stencilTin;
2637
2638                                         shi->emit= texture_value_blend(mtex->def_var, shi->emit, texres.tin, emitfac, mtex->blendtype);
2639                                         if(shi->emit<0.0f) shi->emit= 0.0f;
2640                                 }
2641                                 if(mtex->mapto & MAP_ALPHA) {
2642                                         float alphafac= mtex->alphafac*stencilTin;
2643
2644                                         shi->alpha= texture_value_blend(mtex->def_var, shi->alpha, texres.tin, alphafac, mtex->blendtype);
2645                                         if(shi->alpha<0.0f) shi->alpha= 0.0f;
2646                                         else if(shi->alpha>1.0f) shi->alpha= 1.0f;
2647                                 }
2648                                 if(mtex->mapto & MAP_HAR) {
2649                                         float har;  // have to map to 0-1
2650                                         float hardfac= mtex->hardfac*stencilTin;
2651                                         
2652                                         har= ((float)shi->har)/128.0f;
2653                                         har= 128.0f*texture_value_blend(mtex->def_var, har, texres.tin, hardfac, mtex->blendtype);
2654                                         
2655                                         if(har<1.0f) shi->har= 1;
2656                                         else if(har>511) shi->har= 511;
2657                                         else shi->har= (int)har;
2658                                 }
2659                                 if(mtex->mapto & MAP_RAYMIRR) {
2660                                         float raymirrfac= mtex->raymirrfac*stencilTin;
2661
2662                                         shi->ray_mirror= texture_value_blend(mtex->def_var, shi->ray_mirror, texres.tin, raymirrfac, mtex->blendtype);
2663                                         if(shi->ray_mirror<0.0f) shi->ray_mirror= 0.0f;
2664                                         else if(shi->ray_mirror>1.0f) shi->ray_mirror= 1.0f;
2665                                 }
2666                                 if(mtex->mapto & MAP_TRANSLU) {
2667                                         float translfac= mtex->translfac*stencilTin;
2668
2669                                         shi->translucency= texture_value_blend(mtex->def_var, shi->translucency, texres.tin, translfac, mtex->blendtype);
2670                                         if(shi->translucency<0.0f) shi->translucency= 0.0f;
2671                                         else if(shi->translucency>1.0f) shi->translucency= 1.0f;
2672                                 }
2673                                 if(mtex->mapto & MAP_AMB) {
2674                                         float ambfac= mtex->ambfac*stencilTin;
2675
2676                                         shi->amb= texture_value_blend(mtex->def_var, shi->amb, texres.tin, ambfac, mtex->blendtype);
2677                                         if(shi->amb<0.0f) shi->amb= 0.0f;
2678                                         else if(shi->amb>1.0f) shi->amb= 1.0f;
2679                                         
2680                                         shi->ambr= shi->amb*re->wrld.ambr;
2681                                         shi->ambg= shi->amb*re->wrld.ambg;
2682                                         shi->ambb= shi->amb*re->wrld.ambb;
2683                                 }
2684                         }
2685                 }
2686         }
2687         if ((use_compat_bump || use_ntap_bump || found_nmapping) && (shi->mat->mode & MA_TANGENT_V)!=0) {
2688                 const float fnegdot = -dot_v3v3(shi->vn, shi->tang);
2689                 // apply Gram-Schmidt projection
2690                 madd_v3_v3fl(shi->tang,  shi->vn, fnegdot);
2691                 normalize_v3(shi->tang);
2692         }
2693 }
2694
2695
2696 void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float *col, float *val, Render *re)
2697 {
2698         MTex *mtex;
2699         Tex *tex;
2700         TexResult texres= {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL};
2701         int tex_nr, rgbnor= 0;
2702         float co[3], texvec[3];
2703         float fact, stencilTin=1.0;
2704         
2705         if (re->r.scemode & R_NO_TEX) return;
2706         /* here: test flag if there's a tex (todo) */
2707         
2708         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
2709                 /* separate tex switching */
2710                 if(shi->mat->septex & (1<<tex_nr)) continue;
2711                 
2712                 if(shi->mat->mtex[tex_nr]) {
2713                         mtex= shi->mat->mtex[tex_nr];
2714                         tex= mtex->tex;
2715                         if(tex==0) continue;
2716                         
2717                         /* only process if this texture is mapped 
2718                          * to one that we're interested in */
2719                         if (!(mtex->mapto & mapto_flag)) continue;
2720                         
2721                         /* which coords */
2722                         if(mtex->texco==TEXCO_OBJECT) { 
2723                                 Object *ob= mtex->object;
2724                                 if(ob) {                                                
2725                                         copy_v3_v3(co, xyz);
2726                                         if(mtex->texflag & MTEX_OB_DUPLI_ORIG) {
2727                                                 if(shi->obi && shi->obi->duplitexmat)
2728                                                         mul_m4_v3(shi->obi->duplitexmat, co);                                   
2729                                         } 
2730                                         mul_m4_v3(ob->imat_ren, co);
2731                                 }
2732                         }
2733                         /* not really orco, but 'local' */
2734                         else if(mtex->texco==TEXCO_ORCO) {
2735                                 
2736                                 if(mtex->texflag & MTEX_DUPLI_MAPTO) {
2737                                         copy_v3_v3(co, shi->duplilo);
2738                                 }
2739                                 else {
2740                                         Object *ob= shi->obi->ob;
2741                                         copy_v3_v3(co, xyz);
2742                                         mul_m4_v3(ob->imat_ren, co);
2743                                 }
2744                         }
2745                         else if(mtex->texco==TEXCO_GLOB) {
2746                                 copy_v3_v3(co, xyz);
2747                                 mul_m4_v3(re->viewinv, co);
2748                         }
2749                         else continue;  // can happen when texco defines disappear and it renders old files
2750
2751                         texres.nor= NULL;
2752                         
2753                         if(tex->type==TEX_IMAGE) {
2754                                 continue;       /* not supported yet */                         
2755                                 //do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
2756                         }
2757                         else {
2758                                 /* placement */
2759                                 if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
2760                                 else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
2761
2762                                 if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);