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