code cleanup: function naming, use BKE_*type* prefix.
[blender.git] / source / blender / render / intern / source / render_texture.c
index e19b4a1e669d27746e708c1c11f3654a79bfcac9..0c090e7e9841ea9de0db91a660b09145594be710 100644 (file)
@@ -96,39 +96,39 @@ static void init_render_texture(Render *re, Tex *tex)
 {
        int cfra= re->scene->r.cfra;
        
-       if(re) cfra= re->r.cfra;
+       if (re) cfra= re->r.cfra;
        
        /* imap test */
-       if(tex->ima && ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
+       if (tex->ima && ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
                BKE_image_user_calc_frame(&tex->iuser, cfra, re?re->flag & R_SEC_FIELD:0);
        }
        
-       if(tex->type==TEX_PLUGIN) {
-               if(tex->plugin && tex->plugin->doit) {
-                       if(tex->plugin->cfra) {
+       if (tex->type==TEX_PLUGIN) {
+               if (tex->plugin && tex->plugin->doit) {
+                       if (tex->plugin->cfra) {
                                *(tex->plugin->cfra)= (float)cfra; //BKE_curframe(re->scene); // XXX old animsys - timing stuff to be fixed 
                        }
                }
        }
-       else if(tex->type==TEX_ENVMAP) {
+       else if (tex->type==TEX_ENVMAP) {
                /* just in case */
                tex->imaflag |= TEX_INTERPOL | TEX_MIPMAP;
                tex->extend= TEX_CLIP;
                
-               if(tex->env) {
-                       if(tex->env->type==ENV_PLANE)
+               if (tex->env) {
+                       if (tex->env->type==ENV_PLANE)
                                tex->extend= TEX_EXTEND;
                        
                        /* only free envmap when rendermode was set to render envmaps, for previewrender */
-                       if(G.rendering && re) {
+                       if (G.rendering && re) {
                                if (re->r.mode & R_ENVMAP)
-                                       if(tex->env->stype==ENV_ANIM) 
+                                       if (tex->env->stype==ENV_ANIM)
                                                BKE_free_envmapdata(tex->env);
                        }
                }
        }
        
-       if(tex->nodetree && tex->use_nodes) {
+       if (tex->nodetree && tex->use_nodes) {
                ntreeTexBeginExecTree(tex->nodetree, 1); /* has internal flag to detect it only does it once */
        }
 }
@@ -140,23 +140,23 @@ void init_render_textures(Render *re)
        Tex *tex;
        
        tex= re->main->tex.first;
-       while(tex) {
-               if(tex->id.us) init_render_texture(re, tex);
+       while (tex) {
+               if (tex->id.us) init_render_texture(re, tex);
                tex= tex->id.next;
        }
 }
 
 static void end_render_texture(Tex *tex)
 {
-       if(tex && tex->use_nodes && tex->nodetree && tex->nodetree->execdata)
+       if (tex && tex->use_nodes && tex->nodetree && tex->nodetree->execdata)
                ntreeTexEndExecTree(tex->nodetree->execdata, 1);
 }
 
 void end_render_textures(Render *re)
 {
        Tex *tex;
-       for(tex= re->main->tex.first; tex; tex= tex->id.next)
-               if(tex->id.us)
+       for (tex= re->main->tex.first; tex; tex= tex->id.next)
+               if (tex->id.us)
                        end_render_texture(tex);
 }
 
@@ -197,7 +197,7 @@ static int blend(Tex *tex, float *texvec, TexResult *texres)
 {
        float x, y, t;
 
-       if(tex->flag & TEX_FLIPBLEND) {
+       if (tex->flag & TEX_FLIPBLEND) {
                x= texvec[1];
                y= texvec[0];
        }
@@ -206,33 +206,33 @@ static int blend(Tex *tex, float *texvec, TexResult *texres)
                y= texvec[1];
        }
 
-       if(tex->stype==TEX_LIN) {       /* lin */
+       if (tex->stype==TEX_LIN) {      /* lin */
                texres->tin= (1.0f+x)/2.0f;
        }
-       else if(tex->stype==TEX_QUAD) { /* quad */
+       else if (tex->stype==TEX_QUAD) {        /* quad */
                texres->tin= (1.0f+x)/2.0f;
-               if(texres->tin<0.0f) texres->tin= 0.0f;
+               if (texres->tin<0.0f) texres->tin= 0.0f;
                else texres->tin*= texres->tin;
        }
-       else if(tex->stype==TEX_EASE) { /* ease */
+       else if (tex->stype==TEX_EASE) {        /* ease */
                texres->tin= (1.0f+x)/2.0f;
-               if(texres->tin<=0.0f) texres->tin= 0.0f;
-               else if(texres->tin>=1.0f) texres->tin= 1.0f;
+               if (texres->tin<=0.0f) texres->tin= 0.0f;
+               else if (texres->tin>=1.0f) texres->tin= 1.0f;
                else {
                        t= texres->tin*texres->tin;
                        texres->tin= (3.0f*t-2.0f*t*texres->tin);
                }
        }
-       else if(tex->stype==TEX_DIAG) { /* diag */
+       else if (tex->stype==TEX_DIAG) { /* diag */
                texres->tin= (2.0f+x+y)/4.0f;
        }
-       else if(tex->stype==TEX_RAD) { /* radial */
-               texres->tin= (atan2(y,x) / (2*M_PI) + 0.5);
+       else if (tex->stype==TEX_RAD) { /* radial */
+               texres->tin= (atan2(y, x) / (2*M_PI) + 0.5);
        }
        else {  /* sphere TEX_SPHERE */
                texres->tin= 1.0-sqrt(x*x+      y*y+texvec[2]*texvec[2]);
-               if(texres->tin<0.0f) texres->tin= 0.0f;
-               if(tex->stype==TEX_HALO) texres->tin*= texres->tin;  /* halo */
+               if (texres->tin<0.0f) texres->tin= 0.0f;
+               if (tex->stype==TEX_HALO) texres->tin*= texres->tin;  /* halo */
        }
 
        BRICONT;
@@ -426,37 +426,37 @@ static int magic(Tex *tex, float *texvec, TexResult *texres)
        x=  sin( ( texvec[0]+texvec[1]+texvec[2])*5.0f );
        y=  cos( (-texvec[0]+texvec[1]-texvec[2])*5.0f );
        z= -cos( (-texvec[0]-texvec[1]+texvec[2])*5.0f );
-       if(n>0) {
+       if (n>0) {
                x*= turb;
                y*= turb;
                z*= turb;
                y= -cos(x-y+z);
                y*= turb;
-               if(n>1) {
+               if (n>1) {
                        x= cos(x-y-z);
                        x*= turb;
-                       if(n>2) {
+                       if (n>2) {
                                z= sin(-x-y-z);
                                z*= turb;
-                               if(n>3) {
+                               if (n>3) {
                                        x= -cos(-x+y-z);
                                        x*= turb;
-                                       if(n>4) {
+                                       if (n>4) {
                                                y= -sin(-x+y+z);
                                                y*= turb;
-                                               if(n>5) {
+                                               if (n>5) {
                                                        y= -cos(-x+y+z);
                                                        y*= turb;
-                                                       if(n>6) {
+                                                       if (n>6) {
                                                                x= cos(x+y+z);
                                                                x*= turb;
-                                                               if(n>7) {
+                                                               if (n>7) {
                                                                        z= sin(x+y-z);
                                                                        z*= turb;
-                                                                       if(n>8) {
+                                                                       if (n>8) {
                                                                                x= -cos(-x-y+z);
                                                                                x*= turb;
-                                                                               if(n>9) {
+                                                                               if (n>9) {
                                                                                        y= -sin(x-y+z);
                                                                                        y*= turb;
                                                                                }
@@ -470,7 +470,7 @@ static int magic(Tex *tex, float *texvec, TexResult *texres)
                }
        }
 
-       if(turb!=0.0f) {
+       if (turb!=0.0f) {
                turb*= 2.0f;
                x/= turb; 
                y/= turb; 
@@ -500,19 +500,19 @@ static int stucci(Tex *tex, float *texvec, TexResult *texres)
        
        ofs= tex->turbul/200.0f;
 
-       if(tex->stype) ofs*=(b2*b2);
+       if (tex->stype) ofs*=(b2*b2);
        nor[0] = BLI_gNoise(tex->noisesize, texvec[0]+ofs, texvec[1], texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
        nor[1] = BLI_gNoise(tex->noisesize, texvec[0], texvec[1]+ofs, texvec[2], (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);     
        nor[2] = BLI_gNoise(tex->noisesize, texvec[0], texvec[1], texvec[2]+ofs, (tex->noisetype!=TEX_NOISESOFT), tex->noisebasis);
 
        texres->tin= nor[2];
        
-       if(texres->nor) { 
+       if (texres->nor) {
                
                copy_v3_v3(texres->nor, nor);
                tex_normal_derivate(tex, texres);
                
-               if(tex->stype==TEX_WALLOUT) {
+               if (tex->stype==TEX_WALLOUT) {
                        texres->nor[0]= -texres->nor[0];
                        texres->nor[1]= -texres->nor[1];
                        texres->nor[2]= -texres->nor[2];
@@ -521,10 +521,10 @@ static int stucci(Tex *tex, float *texvec, TexResult *texres)
                retval |= TEX_NOR;
        }
        
-       if(tex->stype==TEX_WALLOUT) 
+       if (tex->stype==TEX_WALLOUT)
                texres->tin= 1.0f-texres->tin;
        
-       if(texres->tin<0.0f)
+       if (texres->tin<0.0f)
                texres->tin= 0.0f;
        
        return retval;
@@ -651,7 +651,7 @@ static float mg_distNoiseTex(Tex *tex, float *texvec, TexResult *texres)
 static float voronoiTex(Tex *tex, float *texvec, TexResult *texres)
 {
        int rv = TEX_INT;
-       float da[4], pa[12];    /* distance and point coordinate arrays of 4 nearest neighbours */
+       float da[4], pa[12];    /* distance and point coordinate arrays of 4 nearest neighbors */
        float aw1 = fabs(tex->vn_w1);
        float aw2 = fabs(tex->vn_w2);
        float aw3 = fabs(tex->vn_w3);
@@ -733,7 +733,7 @@ static int texnoise(Tex *tex, TexResult *texres)
        val= (ran & 3);
        
        loop= tex->noisedepth;
-       while(loop--) {
+       while (loop--) {
                ran= (ran>>2);
                val*= (ran & 3);
                div*= 3.0f;
@@ -756,21 +756,23 @@ static int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex
        texres->tin= 0.0;
 
        pit= tex->plugin;
-       if(pit && pit->doit) {
-               if(texres->nor) {
+       if (pit && pit->doit) {
+               if (texres->nor) {
                        if (pit->version < 6) {
                                copy_v3_v3(pit->result+5, texres->nor);
-                       } else {
+                       }
+                       else {
                                copy_v3_v3(result+5, texres->nor);
                        }
                }
                if (pit->version < 6) {
-                       if(osatex) rgbnor= ((TexDoitold)pit->doit)(tex->stype, 
+                       if (osatex) rgbnor= ((TexDoitold)pit->doit)(tex->stype,
                                pit->data, texvec, dxt, dyt);
                        else rgbnor= ((TexDoitold)pit->doit)(tex->stype, 
                                pit->data, texvec, NULL, NULL);
-               } else {
-                       if(osatex) rgbnor= ((TexDoit)pit->doit)(tex->stype, 
+               }
+               else {
+                       if (osatex) rgbnor= ((TexDoit)pit->doit)(tex->stype,
                                pit->data, texvec, dxt, dyt, result);
                        else rgbnor= ((TexDoit)pit->doit)(tex->stype, 
                                pit->data, texvec, NULL, NULL, result);
@@ -778,27 +780,30 @@ static int plugintex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex
 
                if (pit->version < 6) {
                        texres->tin = pit->result[0];
-               } else {
+               }
+               else {
                        texres->tin = result[0]; /* XXX, assigning garbage value, fixme! */
                }
 
-               if(rgbnor & TEX_NOR) {
-                       if(texres->nor) {
+               if (rgbnor & TEX_NOR) {
+                       if (texres->nor) {
                                if (pit->version < 6) {
                                        copy_v3_v3(texres->nor, pit->result+5);
-                               } else {
+                               }
+                               else {
                                        copy_v3_v3(texres->nor, result+5);
                                }
                        }
                }
                
-               if(rgbnor & TEX_RGB) {
+               if (rgbnor & TEX_RGB) {
                        if (pit->version < 6) {
                                texres->tr = pit->result[1];
                                texres->tg = pit->result[2];
                                texres->tb = pit->result[3];
                                texres->ta = pit->result[4];
-                       } else {
+                       }
+                       else {
                                texres->tr = result[1];
                                texres->tg = result[2];
                                texres->tb = result[3];
@@ -820,7 +825,7 @@ static int cubemap_glob(float *n, float x, float y, float z, float *adr1, float
        float x1, y1, z1, nor[3];
        int ret;
        
-       if(n==NULL) {
+       if (n==NULL) {
                nor[0]= x; nor[1]= y; nor[2]= z;        // use local render coord
        }
        else {
@@ -832,12 +837,12 @@ static int cubemap_glob(float *n, float x, float y, float z, float *adr1, float
        y1= fabs(nor[1]);
        z1= fabs(nor[2]);
        
-       if(z1>=x1 && z1>=y1) {
+       if (z1>=x1 && z1>=y1) {
                *adr1 = (x + 1.0f) / 2.0f;
                *adr2 = (y + 1.0f) / 2.0f;
                ret= 0;
        }
-       else if(y1>=x1 && y1>=z1) {
+       else if (y1>=x1 && y1>=z1) {
                *adr1 = (x + 1.0f) / 2.0f;
                *adr2 = (z + 1.0f) / 2.0f;
                ret= 1;
@@ -857,24 +862,24 @@ static int cubemap(MTex *mtex, VlakRen *vlr, float *n, float x, float y, float z
 {
        int proj[4]={0, ME_PROJXY, ME_PROJXZ, ME_PROJYZ}, ret= 0;
        
-       if(vlr) {
+       if (vlr) {
                int index;
                
                /* Mesh vertices have such flags, for others we calculate it once based on orco */
-               if((vlr->puno & (ME_PROJXY|ME_PROJXZ|ME_PROJYZ))==0) {
+               if ((vlr->puno & (ME_PROJXY|ME_PROJXZ|ME_PROJYZ))==0) {
                        /* test for v1, vlr can be faked for baking */
-                       if(vlr->v1 && vlr->v1->orco) {
+                       if (vlr->v1 && vlr->v1->orco) {
                                float nor[3];
-                               normal_tri_v3( nor,vlr->v1->orco, vlr->v2->orco, vlr->v3->orco);
+                               normal_tri_v3(nor, vlr->v1->orco, vlr->v2->orco, vlr->v3->orco);
                                
-                               if( fabs(nor[0])<fabs(nor[2]) && fabs(nor[1])<fabs(nor[2]) ) vlr->puno |= ME_PROJXY;
-                               else if( fabs(nor[0])<fabs(nor[1]) && fabs(nor[2])<fabs(nor[1]) ) vlr->puno |= ME_PROJXZ;
+                               if ( fabs(nor[0])<fabs(nor[2]) && fabs(nor[1])<fabs(nor[2]) ) vlr->puno |= ME_PROJXY;
+                               else if ( fabs(nor[0])<fabs(nor[1]) && fabs(nor[2])<fabs(nor[1]) ) vlr->puno |= ME_PROJXZ;
                                else vlr->puno |= ME_PROJYZ;
                        }
                        else return cubemap_glob(n, x, y, z, adr1, adr2);
                }
                
-               if(mtex) {
+               if (mtex) {
                        /* the mtex->proj{xyz} have type char. maybe this should be wider? */
                        /* casting to int ensures that the index type is right.            */
                        index = (int) mtex->projx;
@@ -887,11 +892,11 @@ static int cubemap(MTex *mtex, VlakRen *vlr, float *n, float x, float y, float z
                        proj[index]= ME_PROJYZ;
                }
                
-               if(vlr->puno & proj[1]) {
+               if (vlr->puno & proj[1]) {
                        *adr1 = (x + 1.0f) / 2.0f;
                        *adr2 = (y + 1.0f) / 2.0f;
                }
-               else if(vlr->puno & proj[2]) {
+               else if (vlr->puno & proj[2]) {
                        *adr1 = (x + 1.0f) / 2.0f;
                        *adr2 = (z + 1.0f) / 2.0f;
                        ret= 1;
@@ -916,21 +921,21 @@ static int cubemap_ob(Object *ob, float *n, float x, float y, float z, float *ad
        float x1, y1, z1, nor[3];
        int ret;
        
-       if(n==NULL) return 0;
+       if (n==NULL) return 0;
        
        copy_v3_v3(nor, n);
-       if(ob) mul_mat3_m4_v3(ob->imat, nor);
+       if (ob) mul_mat3_m4_v3(ob->imat, nor);
        
        x1= fabs(nor[0]);
        y1= fabs(nor[1]);
        z1= fabs(nor[2]);
        
-       if(z1>=x1 && z1>=y1) {
+       if (z1>=x1 && z1>=y1) {
                *adr1 = (x + 1.0f) / 2.0f;
                *adr2 = (y + 1.0f) / 2.0f;
                ret= 0;
        }
-       else if(y1>=x1 && y1>=z1) {
+       else if (y1>=x1 && y1>=z1) {
                *adr1 = (x + 1.0f) / 2.0f;
                *adr2 = (z + 1.0f) / 2.0f;
                ret= 1;
@@ -958,53 +963,53 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
        ob= mtex->object;
        texco= mtex->texco;
 
-       if(R.osa==0) {
+       if (R.osa==0) {
                
-               if(wrap==MTEX_FLAT) {
+               if (wrap==MTEX_FLAT) {
                        fx = (t[0] + 1.0f) / 2.0f;
                        fy = (t[1] + 1.0f) / 2.0f;
                }
-               else if(wrap==MTEX_TUBE) map_to_tube( &fx, &fy,t[0], t[1], t[2]);
-               else if(wrap==MTEX_SPHERE) map_to_sphere( &fx, &fy,t[0], t[1], t[2]);
+               else if (wrap==MTEX_TUBE) map_to_tube( &fx, &fy, t[0], t[1], t[2]);
+               else if (wrap==MTEX_SPHERE) map_to_sphere(&fx, &fy, t[0], t[1], t[2]);
                else {
-                       if(texco==TEXCO_OBJECT) cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
-                       else if(texco==TEXCO_GLOB) cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
+                       if (texco==TEXCO_OBJECT) cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
+                       else if (texco==TEXCO_GLOB) cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
                        else cubemap(mtex, vlr, n, t[0], t[1], t[2], &fx, &fy);
                }
                
                /* repeat */
-               if(tex->extend==TEX_REPEAT) {
-                       if(tex->xrepeat>1) {
+               if (tex->extend==TEX_REPEAT) {
+                       if (tex->xrepeat>1) {
                                float origf= fx *= tex->xrepeat;
                                
-                               if(fx>1.0f) fx -= (int)(fx);
-                               else if(fx<0.0f) fx+= 1-(int)(fx);
+                               if (fx>1.0f) fx -= (int)(fx);
+                               else if (fx<0.0f) fx+= 1-(int)(fx);
                                
-                               if(tex->flag & TEX_REPEAT_XMIR) {
+                               if (tex->flag & TEX_REPEAT_XMIR) {
                                        int orig= (int)floor(origf);
-                                       if(orig & 1)
+                                       if (orig & 1)
                                                fx= 1.0f-fx;
                                }
                        }
-                       if(tex->yrepeat>1) {
+                       if (tex->yrepeat>1) {
                                float origf= fy *= tex->yrepeat;
                                
-                               if(fy>1.0f) fy -= (int)(fy);
-                               else if(fy<0.0f) fy+= 1-(int)(fy);
+                               if (fy>1.0f) fy -= (int)(fy);
+                               else if (fy<0.0f) fy+= 1-(int)(fy);
                                
-                               if(tex->flag & TEX_REPEAT_YMIR) {
+                               if (tex->flag & TEX_REPEAT_YMIR) {
                                        int orig= (int)floor(origf);
-                                       if(orig & 1) 
+                                       if (orig & 1)
                                                fy= 1.0f-fy;
                                }
                        }
                }
                /* crop */
-               if(tex->cropxmin!=0.0f || tex->cropxmax!=1.0f) {
+               if (tex->cropxmin!=0.0f || tex->cropxmax!=1.0f) {
                        fac1= tex->cropxmax - tex->cropxmin;
                        fx= tex->cropxmin+ fx*fac1;
                }
-               if(tex->cropymin!=0.0f || tex->cropymax!=1.0f) {
+               if (tex->cropymin!=0.0f || tex->cropymax!=1.0f) {
                        fac1= tex->cropymax - tex->cropymin;
                        fy= tex->cropymin+ fy*fac1;
                }
@@ -1014,7 +1019,7 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
        }
        else {
                
-               if(wrap==MTEX_FLAT) {
+               if (wrap==MTEX_FLAT) {
                        fx= (t[0] + 1.0f) / 2.0f;
                        fy= (t[1] + 1.0f) / 2.0f;
                        dxt[0]/= 2.0f;
@@ -1024,32 +1029,32 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
                        dyt[1]/= 2.0f;
                        dyt[2]/= 2.0f;
                }
-               else if ELEM(wrap, MTEX_TUBE, MTEX_SPHERE) {
+               else if (ELEM(wrap, MTEX_TUBE, MTEX_SPHERE)) {
                        /* exception: the seam behind (y<0.0) */
                        ok= 1;
-                       if(t[1]<=0.0f) {
+                       if (t[1]<=0.0f) {
                                fx= t[0]+dxt[0];
                                fy= t[0]+dyt[0];
-                               if(fx>=0.0f && fy>=0.0f && t[0]>=0.0f);
-                               else if(fx<=0.0f && fy<=0.0f && t[0]<=0.0f);
+                               if (fx>=0.0f && fy>=0.0f && t[0]>=0.0f);
+                               else if (fx<=0.0f && fy<=0.0f && t[0]<=0.0f);
                                else ok= 0;
                        }
-                       if(ok) {
-                               if(wrap==MTEX_TUBE) {
-                                       map_to_tube( area, area+1,t[0], t[1], t[2]);
-                                       map_to_tube( area+2, area+3,t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2]);
-                                       map_to_tube( area+4, area+5,t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2]);
+                       if (ok) {
+                               if (wrap==MTEX_TUBE) {
+                                       map_to_tube(area, area+1, t[0], t[1], t[2]);
+                                       map_to_tube(area + 2, area + 3, t[0] + dxt[0], t[1] + dxt[1], t[2] + dxt[2]);
+                                       map_to_tube(area + 4, area + 5, t[0] + dyt[0], t[1] + dyt[1], t[2] + dyt[2]);
                                }
                                else { 
-                                       map_to_sphere(area,area+1,t[0], t[1], t[2]);
-                                       map_to_sphere( area+2, area+3,t[0]+dxt[0], t[1]+dxt[1], t[2]+dxt[2]);
-                                       map_to_sphere( area+4, area+5,t[0]+dyt[0], t[1]+dyt[1], t[2]+dyt[2]);
+                                       map_to_sphere(area, area+1, t[0], t[1], t[2]);
+                                       map_to_sphere(area + 2, area + 3, t[0] + dxt[0], t[1] + dxt[1], t[2] + dxt[2]);
+                                       map_to_sphere(area + 4, area + 5, t[0] + dyt[0], t[1] + dyt[1], t[2] + dyt[2]);
                                }
                                areaflag= 1;
                        }
                        else {
-                               if(wrap==MTEX_TUBE) map_to_tube( &fx, &fy,t[0], t[1], t[2]);
-                               else map_to_sphere( &fx, &fy,t[0], t[1], t[2]);
+                               if (wrap==MTEX_TUBE) map_to_tube( &fx, &fy, t[0], t[1], t[2]);
+                               else map_to_sphere(&fx, &fy, t[0], t[1], t[2]);
                                dxt[0]/= 2.0f;
                                dxt[1]/= 2.0f;
                                dyt[0]/= 2.0f;
@@ -1058,15 +1063,15 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
                }
                else {
 
-                       if(texco==TEXCO_OBJECT) proj = cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
+                       if (texco==TEXCO_OBJECT) proj = cubemap_ob(ob, n, t[0], t[1], t[2], &fx, &fy);
                        else if (texco==TEXCO_GLOB) proj = cubemap_glob(n, t[0], t[1], t[2], &fx, &fy);
                        else proj = cubemap(mtex, vlr, n, t[0], t[1], t[2], &fx, &fy);
 
-                       if(proj==1) {
+                       if (proj==1) {
                                SWAP(float, dxt[1], dxt[2]);
                                SWAP(float, dyt[1], dyt[2]);
                        }
-                       else if(proj==2) {
+                       else if (proj==2) {
                                float f1= dxt[0], f2= dyt[0];
                                dxt[0]= dxt[1];
                                dyt[0]= dyt[1];
@@ -1087,7 +1092,7 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
                }
                
                /* if area, then reacalculate dxt[] and dyt[] */
-               if(areaflag) {
+               if (areaflag) {
                        fx= area[0]; 
                        fy= area[1];
                        dxt[0]= area[2]-fx;
@@ -1097,19 +1102,19 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
                }
                
                /* repeat */
-               if(tex->extend==TEX_REPEAT) {
+               if (tex->extend==TEX_REPEAT) {
                        float max= 1.0f;
-                       if(tex->xrepeat>1) {
+                       if (tex->xrepeat>1) {
                                float origf= fx *= tex->xrepeat;
                                
                                // TXF: omit mirror here, see comments in do_material_tex() after do_2d_mapping() call
                                if (tex->texfilter == TXF_BOX) {
-                                       if(fx>1.0f) fx -= (int)(fx);
-                                       else if(fx<0.0f) fx+= 1-(int)(fx);
+                                       if (fx>1.0f) fx -= (int)(fx);
+                                       else if (fx<0.0f) fx+= 1-(int)(fx);
                                
-                                       if(tex->flag & TEX_REPEAT_XMIR) {
+                                       if (tex->flag & TEX_REPEAT_XMIR) {
                                                int orig= (int)floor(origf);
-                                               if(orig & 1) 
+                                               if (orig & 1)
                                                        fx= 1.0f-fx;
                                        }
                                }
@@ -1119,41 +1124,41 @@ static void do_2d_mapping(MTex *mtex, float *t, VlakRen *vlr, float *n, float *d
                                dxt[0]*= tex->xrepeat;
                                dyt[0]*= tex->xrepeat;
                        }
-                       if(tex->yrepeat>1) {
+                       if (tex->yrepeat>1) {
                                float origf= fy *= tex->yrepeat;
                                
                                // TXF: omit mirror here, see comments in do_material_tex() after do_2d_mapping() call
                                if (tex->texfilter == TXF_BOX) {
-                                       if(fy>1.0f) fy -= (int)(fy);
-                                       else if(fy<0.0f) fy+= 1-(int)(fy);
+                                       if (fy>1.0f) fy -= (int)(fy);
+                                       else if (fy<0.0f) fy+= 1-(int)(fy);
                                
-                                       if(tex->flag & TEX_REPEAT_YMIR) {
+                                       if (tex->flag & TEX_REPEAT_YMIR) {
                                                int orig= (int)floor(origf);
-                                               if(orig & 1) 
+                                               if (orig & 1)
                                                        fy= 1.0f-fy;
                                        }
                                }
                                
-                               if(max<tex->yrepeat)
+                               if (max<tex->yrepeat)
                                        max= tex->yrepeat;
 
                                dxt[1]*= tex->yrepeat;
                                dyt[1]*= tex->yrepeat;
                        }
-                       if(max!=1.0f) {
+                       if (max!=1.0f) {
                                dxt[2]*= max;
                                dyt[2]*= max;
                        }
                        
                }
                /* crop */
-               if(tex->cropxmin!=0.0f || tex->cropxmax!=1.0f) {
+               if (tex->cropxmin!=0.0f || tex->cropxmax!=1.0f) {
                        fac1= tex->cropxmax - tex->cropxmin;
                        fx= tex->cropxmin+ fx*fac1;
                        dxt[0]*= fac1;
                        dyt[0]*= fac1;
                }
-               if(tex->cropymin!=0.0f || tex->cropymax!=1.0f) {
+               if (tex->cropymin!=0.0f || tex->cropymax!=1.0f) {
                        fac1= tex->cropymax - tex->cropymin;
                        fy= tex->cropymin+ fy*fac1;
                        dxt[1]*= fac1;
@@ -1175,12 +1180,12 @@ static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex,
 
        texres->talpha= 0;      /* is set when image texture returns alpha (considered premul) */
        
-       if(tex->use_nodes && tex->nodetree) {
+       if (tex->use_nodes && tex->nodetree) {
                retval = ntreeTexExecTree(tex->nodetree, texres, texvec, dxt, dyt, osatex, thread,
                        tex, which_output, R.r.cfra, (R.r.scemode & R_TEXNODE_PREVIEW) != 0, NULL, NULL);
        }
        else
-       switch(tex->type) {
+       switch (tex->type) {
        
        case 0:
                texres->tin= 0.0f;
@@ -1207,7 +1212,7 @@ static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex,
                retval= texnoise(tex, texres); 
                break;
        case TEX_IMAGE:
-               if(osatex) retval= imagewraposa(tex, tex->ima, NULL, texvec, dxt, dyt, texres);
+               if (osatex) retval= imagewraposa(tex, tex->ima, NULL, texvec, dxt, dyt, texres);
                else retval= imagewrap(tex, tex->ima, NULL, texvec, texres); 
                tag_image_time(tex->ima); /* tag image as having being used */
                break;
@@ -1226,7 +1231,7 @@ static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex,
                copy_v3_v3(tmpvec, texvec);
                mul_v3_fl(tmpvec, 1.0f/tex->noisesize);
                
-               switch(tex->stype) {
+               switch (tex->stype) {
                case TEX_MFRACTAL:
                case TEX_FBM:
                        retval= mg_mFractalOrfBmTex(tex, tmpvec, texres);
@@ -1287,27 +1292,27 @@ static int multitex(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex,
 /* this is called from the shader and texture nodes */
 int multitex_nodes(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex, TexResult *texres, short thread, short which_output, ShadeInput *shi, MTex *mtex)
 {
-       if(tex==NULL) {
+       if (tex==NULL) {
                memset(texres, 0, sizeof(TexResult));
                return 0;
        }
 
-       if(mtex)
+       if (mtex)
                which_output= mtex->which_output;
        
-       if(tex->type==TEX_IMAGE) {
+       if (tex->type==TEX_IMAGE) {
                int rgbnor;
 
-               if(mtex) {
+               if (mtex) {
                        /* we have mtex, use it for 2d mapping images only */
                        do_2d_mapping(mtex, texvec, shi->vlr, shi->facenor, dxt, dyt);
                        rgbnor= multitex(tex, texvec, dxt, dyt, osatex, texres, thread, which_output);
 
-                       if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
+                       if (mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
                                ImBuf *ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
                                
                                /* don't linearize float buffers, assumed to be linear */
-                               if(ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
+                               if (ibuf && !(ibuf->rect_float) && R.r.color_mgt_flag & R_COLOR_MANAGEMENT)
                                        srgb_to_linearrgb_v3_v3(&texres->tr, &texres->tr);
                        }
                }
@@ -1322,7 +1327,7 @@ int multitex_nodes(Tex *tex, float *texvec, float *dxt, float *dyt, int osatex,
                        localmtex.texco= TEXCO_ORCO;
                        
                        copy_v3_v3(texvec_l, texvec);
-                       if(dxt && dyt) {
+                       if (dxt && dyt) {
                                copy_v3_v3(dxt_l, dxt);
                                copy_v3_v3(dyt_l, dyt);
                        }
@@ -1346,9 +1351,9 @@ int multitex_mtex(ShadeInput *shi, MTex *mtex, float *texvec, float *dxt, float
 {
        Tex *tex= mtex->tex;
 
-       if(tex->use_nodes && tex->nodetree) {
+       if (tex->use_nodes && tex->nodetree) {
                /* stupid exception here .. but we have to pass shi and mtex to
-                  textures nodes for 2d mapping and color management for images */
+                * textures nodes for 2d mapping and color management for images */
                return ntreeTexExecTree(tex->nodetree, texres, texvec, dxt, dyt, shi->osatex, shi->thread,
                        tex, mtex->which_output, R.r.cfra, (R.r.scemode & R_TEXNODE_PREVIEW) != 0, shi, mtex);
        }
@@ -1384,7 +1389,7 @@ void texture_rgb_blend(float in[3], const float tex[3], const float out[3], floa
 {
        float facm, col;
        
-       switch(blendtype) {
+       switch (blendtype) {
        case MTEX_BLEND:
                fact*= facg;
                facm= 1.0f-fact;
@@ -1414,15 +1419,15 @@ void texture_rgb_blend(float in[3], const float tex[3], const float out[3], floa
                fact*= facg;
                facm= 1.0f-facg;
                
-               if(out[0] < 0.5f)
+               if (out[0] < 0.5f)
                        in[0] = out[0] * (facm + 2.0f*fact*tex[0]);
                else
                        in[0] = 1.0f - (facm + 2.0f*fact*(1.0f - tex[0])) * (1.0f - out[0]);
-               if(out[1] < 0.5f)
+               if (out[1] < 0.5f)
                        in[1] = out[1] * (facm + 2.0f*fact*tex[1]);
                else
                        in[1] = 1.0f - (facm + 2.0f*fact*(1.0f - tex[1])) * (1.0f - out[1]);
-               if(out[2] < 0.5f)
+               if (out[2] < 0.5f)
                        in[2] = out[2] * (facm + 2.0f*fact*tex[2]);
                else
                        in[2] = 1.0f - (facm + 2.0f*fact*(1.0f - tex[2])) * (1.0f - out[2]);
@@ -1441,11 +1446,11 @@ void texture_rgb_blend(float in[3], const float tex[3], const float out[3], floa
                fact*= facg;
                facm= 1.0f-fact;
                
-               if(tex[0]!=0.0f)
+               if (tex[0]!=0.0f)
                        in[0]= facm*out[0] + fact*out[0]/tex[0];
-               if(tex[1]!=0.0f)
+               if (tex[1]!=0.0f)
                        in[1]= facm*out[1] + fact*out[1]/tex[1];
-               if(tex[2]!=0.0f)
+               if (tex[2]!=0.0f)
                        in[2]= facm*out[2] + fact*out[2]/tex[2];
 
                break;
@@ -1463,22 +1468,22 @@ void texture_rgb_blend(float in[3], const float tex[3], const float out[3], floa
                facm= 1.0f-fact;
                
                col= tex[0]+((1-tex[0])*facm);
-               if(col < out[0]) in[0]= col; else in[0]= out[0];
+               if (col < out[0]) in[0]= col; else in[0]= out[0];
                col= tex[1]+((1-tex[1])*facm);
-               if(col < out[1]) in[1]= col; else in[1]= out[1];
+               if (col < out[1]) in[1]= col; else in[1]= out[1];
                col= tex[2]+((1-tex[2])*facm);
-               if(col < out[2]) in[2]= col; else in[2]= out[2];
+               if (col < out[2]) in[2]= col; else in[2]= out[2];
                break;
 
        case MTEX_LIGHT:
                fact*= facg;
                
                col= fact*tex[0];
-               if(col > out[0]) in[0]= col; else in[0]= out[0];
+               if (col > out[0]) in[0]= col; else in[0]= out[0];
                col= fact*tex[1];
-               if(col > out[1]) in[1]= col; else in[1]= out[1];
+               if (col > out[1]) in[1]= col; else in[1]= out[1];
                col= fact*tex[2];
-               if(col > out[2]) in[2]= col; else in[2]= out[2];
+               if (col > out[2]) in[2]= col; else in[2]= out[2];
                break;
                
        case MTEX_BLEND_HUE:
@@ -1523,9 +1528,9 @@ float texture_value_blend(float tex, float out, float fact, float facg, int blen
        
        fact*= facg;
        facm= 1.0f-fact;
-       if(flip) SWAP(float, fact, facm);
+       if (flip) SWAP(float, fact, facm);
 
-       switch(blendtype) {
+       switch (blendtype) {
        case MTEX_BLEND:
                in= fact*tex + facm*out;
                break;
@@ -1542,7 +1547,7 @@ float texture_value_blend(float tex, float out, float fact, float facg, int blen
 
        case MTEX_OVERLAY:
                facm= 1.0f-facg;
-               if(out < 0.5f)
+               if (out < 0.5f)
                        in = out * (facm + 2.0f*fact*tex);
                else
                        in = 1.0f - (facm + 2.0f*fact*(1.0f - tex)) * (1.0f - out);
@@ -1555,7 +1560,7 @@ float texture_value_blend(float tex, float out, float fact, float facg, int blen
                break;
 
        case MTEX_DIV:
-               if(tex!=0.0f)
+               if (tex!=0.0f)
                        in= facm*out + fact*out/tex;
                break;
 
@@ -1565,12 +1570,12 @@ float texture_value_blend(float tex, float out, float fact, float facg, int blen
 
        case MTEX_DARK:
                col= fact*tex;
-               if(col < out) in= col; else in= out;
+               if (col < out) in= col; else in= out;
                break;
 
        case MTEX_LIGHT:
                col= fact*tex;
-               if(col > out) in= col; else in= out;
+               if (col > out) in= col; else in= out;
                break;
 
        case MTEX_SOFT_LIGHT: 
@@ -1642,7 +1647,7 @@ static void texco_mapping(ShadeInput* shi, Tex* tex, MTex* mtex, float* co, floa
                        }
                }
                if (tex->extend == TEX_REPEAT && (tex->flag & TEX_REPEAT_YMIR)) {
-                       if  (tex->texfilter == TXF_BOX)
+                       if (tex->texfilter == TXF_BOX)
                                texvec[1] -= floorf(texvec[1]);
                        else if (texvec[1] < 0.f || texvec[1] > 1.f) {
                                const float ty = 0.5f*texvec[1];
@@ -1702,14 +1707,14 @@ static void compatible_bump_uv_derivs(CompatibleBump *compat_bump, ShadeInput *s
        // NOTE: test for shi->obr->ob here, since vlr/obr/obi can be 'fake' when called from fastshade(), another reason to move it..
        // NOTE: shi->v1 is NULL when called from displace_render_vert, assigning verts in this case is not trivial because the shi quad face side is not know.
        if ((mtex->texflag & MTEX_COMPAT_BUMP) && shi->obr && shi->obr->ob && shi->v1) {
-               if(mtex->mapto & (MAP_NORM|MAP_WARP) && !((mtex->tex->type==TEX_IMAGE) && (mtex->tex->imaflag & TEX_NORMALMAP))) {
+               if (mtex->mapto & (MAP_NORM|MAP_WARP) && !((mtex->tex->type==TEX_IMAGE) && (mtex->tex->imaflag & TEX_NORMALMAP))) {
                        MTFace* tf = RE_vlakren_get_tface(shi->obr, shi->vlr, i, NULL, 0);
                        int j1 = shi->i1, j2 = shi->i2, j3 = shi->i3;
 
                        vlr_set_uv_indices(shi->vlr, &j1, &j2, &j3);
 
                        // compute ortho basis around normal
-                       if(!compat_bump->nunvdone) {
+                       if (!compat_bump->nunvdone) {
                                // render normal is negated
                                compat_bump->nn[0] = -shi->vn[0];
                                compat_bump->nn[1] = -shi->vn[1];
@@ -1763,7 +1768,7 @@ static int compatible_bump_compute(CompatibleBump *compat_bump, ShadeInput *shi,
        du = dv = 0.01f;
 
        // compute ortho basis around normal
-       if(!compat_bump->nunvdone) {
+       if (!compat_bump->nunvdone) {
                // render normal is negated
                negate_v3_v3(compat_bump->nn, shi->vn);
                ortho_basis_v3v3_v3(compat_bump->nu, compat_bump->nv, compat_bump->nn);
@@ -1932,15 +1937,16 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
        float *nvec = texres->nor;
        texres->nor = NULL;
 
-       if(found_deriv_map==0) {
-               if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
-                       if(tex->ima)
+       if (found_deriv_map==0) {
+               if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
+                       if (tex->ima)
                                Hscale *= 13.0f; // appears to be a sensible default value
-               } else
+               }
+               else
                        Hscale *= 0.1f; // factor 0.1 proved to look like the previous bump code
        }
 
-       if( !ntap_bump->init_done ) {
+       if ( !ntap_bump->init_done ) {
                copy_v3_v3(ntap_bump->vNacc, shi->vn);
                copy_v3_v3(ntap_bump->vNorg, shi->vn);
                ntap_bump->fPrevMagnitude = 1.0f;
@@ -1950,7 +1956,7 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
        }
 
        // resolve image dimensions
-       if(found_deriv_map || (mtex->texflag&MTEX_BUMP_TEXTURESPACE)!=0) {
+       if (found_deriv_map || (mtex->texflag&MTEX_BUMP_TEXTURESPACE)!=0) {
                ImBuf* ibuf = BKE_image_get_ibuf(tex->ima, &tex->iuser);
                if (ibuf) {
                        dimx = ibuf->x;
@@ -1959,16 +1965,16 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                }
        }
        
-       if(found_deriv_map) {
+       if (found_deriv_map) {
                float dBdu, dBdv, auto_bump = 1.0f;
                float s = 1;            // negate this if flipped texture coordinate
                texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
                rgbnor = multitex_mtex(shi, mtex, texvec, dxt, dyt, texres);
 
-               if(shi->obr->ob->derivedFinal)
-               {
+               if (shi->obr->ob->derivedFinal) {
                        auto_bump = shi->obr->ob->derivedFinal->auto_bump_scale;
                }
+
                {
                        float fVirtDim = sqrtf(fabsf((float) (dimx*dimy)*mtex->size[0]*mtex->size[1]));
                        auto_bump /= MAX2(fVirtDim, FLT_EPSILON);
@@ -1982,14 +1988,14 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                dHdx = dBdu*dxt[0] + s * dBdv*dxt[1];
                dHdy = dBdu*dyt[0] + s * dBdv*dyt[1];
        }
-       else if(!(mtex->texflag & MTEX_5TAP_BUMP)) {
+       else if (!(mtex->texflag & MTEX_5TAP_BUMP)) {
                // compute height derivatives with respect to output image pixel coordinates x and y
                float STll[3], STlr[3], STul[3];
                float Hll, Hlr, Hul;
 
                texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
 
-               for(c=0; c<nr_channels; c++) {
+               for (c=0; c<nr_channels; c++) {
                        // dx contains the derivatives (du/dx, dv/dx)
                        // dy contains the derivatives (du/dy, dv/dy)
                        STll[c] = texvec[c];
@@ -1998,7 +2004,7 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                }
 
                // clear unused derivatives
-               for(c=nr_channels; c<3; c++) {
+               for (c=nr_channels; c<3; c++) {
                        STll[c] = 0.0f;
                        STlr[c] = 0.0f;
                        STul[c] = 0.0f;
@@ -2025,7 +2031,7 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
 
                texco_mapping(shi, tex, mtex, co, dx, dy, texvec, dxt, dyt);
 
-               for(c=0; c<nr_channels; c++) {
+               for (c=0; c<nr_channels; c++) {
                        STc[c] = texvec[c];
                        STl[c] = texvec[c] - 0.5f*dxt[c];
                        STr[c] = texvec[c] + 0.5f*dxt[c];
@@ -2034,7 +2040,7 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                }
 
                // clear unused derivatives
-               for(c=nr_channels; c<3; c++) {
+               for (c=nr_channels; c<3; c++) {
                        STc[c] = 0.0f;
                        STl[c] = 0.0f;
                        STr[c] = 0.0f;
@@ -2064,17 +2070,17 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
        texres->nor = nvec;
 
        /* replaced newbump with code based on listing 1 and 2 of
-               [Mik10] Mikkelsen M. S.: Bump Mapping Unparametrized Surfaces on the GPU.
-               -> http://jbit.net/~sparky/sfgrad_bump/mm_sfgrad_bump.pdf */
+        * [Mik10] Mikkelsen M. S.: Bump Mapping Unparametrized Surfaces on the GPU.
+        * -> http://jbit.net/~sparky/sfgrad_bump/mm_sfgrad_bump.pdf */
 
-       if( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
+       if ( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
                iBumpSpace = 1;
-       else if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
+       else if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
                iBumpSpace = 2;
        else
                iBumpSpace = 4; // ViewSpace
        
-       if( ntap_bump->iPrevBumpSpace != iBumpSpace ) {
+       if ( ntap_bump->iPrevBumpSpace != iBumpSpace ) {
                
                // initialize normal perturbation vectors
                int xyz;
@@ -2087,7 +2093,7 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                copy_v3_v3(dPdy, shi->dyco);
                copy_v3_v3(vN, ntap_bump->vNorg);
                
-               if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
+               if ( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
                        // TODO: these calculations happen for every pixel!
                        //      -> move to shi->obi
                        mult_m4_m4m4(tmp, R.viewmat, shi->obr->ob->obmat);
@@ -2096,9 +2102,9 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                
                        // generate the surface derivatives in object space
                        mul_m3_v3(view2obj, dPdx);
-                       mul_m3_v3( view2obj, dPdy );
+                       mul_m3_v3(view2obj, dPdy);
                        // generate the unit normal in object space
-                       mul_transposed_m3_v3( obj2view, vN );
+                       mul_transposed_m3_v3(obj2view, vN);
                        normalize_v3(vN);
                }
                
@@ -2108,8 +2114,8 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                ntap_bump->sgn_det = (fDet < 0)? -1.0f: 1.0f;
                abs_fDet = ntap_bump->sgn_det * fDet;
 
-               if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
-                       if(tex->ima) {
+               if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
+                       if (tex->ima) {
                                // crazy hack solution that gives results similar to normal mapping - part 1
                                normalize_v3(ntap_bump->vR1);
                                normalize_v3(ntap_bump->vR2);
@@ -2118,24 +2124,25 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
                }
                
                fMagnitude = abs_fDet;
-               if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
+               if ( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) {
                        // pre do transform of texres->nor by the inverse transposed of obj2view
-                       mul_transposed_m3_v3( view2obj, vN );
-                       mul_transposed_m3_v3( view2obj, ntap_bump->vR1 );
-                       mul_transposed_m3_v3( view2obj, ntap_bump->vR2 );
+                       mul_transposed_m3_v3(view2obj, vN);
+                       mul_transposed_m3_v3(view2obj, ntap_bump->vR1);
+                       mul_transposed_m3_v3(view2obj, ntap_bump->vR2);
                        
                        fMagnitude *= len_v3(vN);
                }
                
-               for(xyz=0; xyz<3; xyz++)
+               if (ntap_bump->fPrevMagnitude > 0.0f)
+                       for (xyz=0; xyz<3; xyz++)
                                ntap_bump->vNacc[xyz] *= fMagnitude / ntap_bump->fPrevMagnitude;
                
                ntap_bump->fPrevMagnitude = fMagnitude;
                ntap_bump->iPrevBumpSpace = iBumpSpace;
        }
 
-       if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
-               if(tex->ima) {
+       if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
+               if (tex->ima) {
                        // crazy hack solution that gives results similar to normal mapping - part 2
                        float vec[2];
                        const float imag_tspace_dimension_y = aspect*imag_tspace_dimension_x;
@@ -2150,7 +2157,7 @@ static int ntap_bump_compute(NTapBump *ntap_bump, ShadeInput *shi, MTex *mtex, T
        }
        
        // subtract the surface gradient from vNacc
-       for(c=0; c<3; c++) {
+       for (c=0; c<3; c++) {
                float vSurfGrad_compi = ntap_bump->sgn_det * (dHdx * ntap_bump->vR1[c] + dHdy * ntap_bump->vR2[c]);
                ntap_bump->vNacc[c] -= vSurfGrad_compi;
                texres->nor[c] = ntap_bump->vNacc[c]; // copy
@@ -2181,42 +2188,42 @@ void do_material_tex(ShadeInput *shi, Render *re)
        if (re->r.scemode & R_NO_TEX) return;
        /* here: test flag if there's a tex (todo) */
 
-       for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
+       for (tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
                
                /* separate tex switching */
-               if(shi->mat->septex & (1<<tex_nr)) continue;
+               if (shi->mat->septex & (1<<tex_nr)) continue;
                
-               if(shi->mat->mtex[tex_nr]) {
+               if (shi->mat->mtex[tex_nr]) {
                        mtex= shi->mat->mtex[tex_nr];
                        
                        tex= mtex->tex;
-                       if(tex==0) continue;
+                       if (tex==0) continue;
 
                        found_deriv_map = (tex->type==TEX_IMAGE) && (tex->imaflag & TEX_DERIVATIVEMAP);
                        use_compat_bump= (mtex->texflag & MTEX_COMPAT_BUMP);
                        use_ntap_bump= ((mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP|MTEX_BICUBIC_BUMP))!=0 || found_deriv_map!=0) ? 1 : 0;
 
                        /* XXX texture node trees don't work for this yet */
-                       if(tex->nodetree && tex->use_nodes) {
+                       if (tex->nodetree && tex->use_nodes) {
                                use_compat_bump = 0;
                                use_ntap_bump = 0;
                        }
                        
                        /* case displacement mapping */
-                       if(shi->osatex==0 && use_ntap_bump) {
+                       if (shi->osatex==0 && use_ntap_bump) {
                                use_ntap_bump = 0;
                                use_compat_bump = 1;
                        }
                        
                        /* case ocean */
-                       if(tex->type == TEX_OCEAN) {
+                       if (tex->type == TEX_OCEAN) {
                                use_ntap_bump = 0;
                                use_compat_bump = 0;
                        }
 
                        /* which coords */
-                       if(mtex->texco==TEXCO_ORCO) {
-                               if(mtex->texflag & MTEX_DUPLI_MAPTO) {
+                       if (mtex->texco==TEXCO_ORCO) {
+                               if (mtex->texflag & MTEX_DUPLI_MAPTO) {
                                        co= shi->duplilo; dx= dxt; dy= dyt;
                                        dxt[0]= dxt[1]= dxt[2]= 0.0f;
                                        dyt[0]= dyt[1]= dyt[2]= 0.0f;
@@ -2225,21 +2232,21 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        co= shi->lo; dx= shi->dxlo; dy= shi->dylo;
                                }
                        }
-                       else if(mtex->texco==TEXCO_STICKY) {
+                       else if (mtex->texco==TEXCO_STICKY) {
                                co= shi->sticky; dx= shi->dxsticky; dy= shi->dysticky;
                        }
-                       else if(mtex->texco==TEXCO_OBJECT) {
+                       else if (mtex->texco==TEXCO_OBJECT) {
                                Object *ob= mtex->object;
-                               if(ob) {
+                               if (ob) {
                                        co= tempvec;
                                        dx= dxt;
                                        dy= dyt;
                                        copy_v3_v3(tempvec, shi->co);
-                                       if(mtex->texflag & MTEX_OB_DUPLI_ORIG)
-                                               if(shi->obi && shi->obi->duplitexmat)
+                                       if (mtex->texflag & MTEX_OB_DUPLI_ORIG)
+                                               if (shi->obi && shi->obi->duplitexmat)
                                                        mul_m4_v3(shi->obi->duplitexmat, tempvec);
                                        mul_m4_v3(ob->imat_ren, tempvec);
-                                       if(shi->osatex) {
+                                       if (shi->osatex) {
                                                copy_v3_v3(dxt, shi->dxco);
                                                copy_v3_v3(dyt, shi->dyco);
                                                mul_mat3_m4_v3(ob->imat_ren, dxt);
@@ -2252,21 +2259,21 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        dx= shi->dxco; dy= shi->dyco;
                                }
                        }
-                       else if(mtex->texco==TEXCO_REFL) {
+                       else if (mtex->texco==TEXCO_REFL) {
                                calc_R_ref(shi);
                                co= shi->ref; dx= shi->dxref; dy= shi->dyref;
                        }
-                       else if(mtex->texco==TEXCO_NORM) {
+                       else if (mtex->texco==TEXCO_NORM) {
                                co= shi->orn; dx= shi->dxno; dy= shi->dyno;
                        }
-                       else if(mtex->texco==TEXCO_TANGENT) {
+                       else if (mtex->texco==TEXCO_TANGENT) {
                                co= shi->tang; dx= shi->dxno; dy= shi->dyno;
                        }
-                       else if(mtex->texco==TEXCO_GLOB) {
+                       else if (mtex->texco==TEXCO_GLOB) {
                                co= shi->gl; dx= shi->dxgl; dy= shi->dygl;
                        }
-                       else if(mtex->texco==TEXCO_UV) {
-                               if(mtex->texflag & MTEX_DUPLI_MAPTO) {
+                       else if (mtex->texco==TEXCO_UV) {
+                               if (mtex->texflag & MTEX_DUPLI_MAPTO) {
                                        co= shi->dupliuv; dx= dxt; dy= dyt;
                                        dxt[0]= dxt[1]= dxt[2]= 0.0f;
                                        dyt[0]= dyt[1]= dyt[2]= 0.0f;
@@ -2275,9 +2282,9 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        ShadeInputUV *suv= &shi->uv[shi->actuv];
                                        int i = shi->actuv;
 
-                                       if(mtex->uvname[0] != 0) {
-                                               for(i = 0; i < shi->totuv; i++) {
-                                                       if(strcmp(shi->uv[i].name, mtex->uvname)==0) {
+                                       if (mtex->uvname[0] != 0) {
+                                               for (i = 0; i < shi->totuv; i++) {
+                                                       if (strcmp(shi->uv[i].name, mtex->uvname)==0) {
                                                                suv= &shi->uv[i];
                                                                break;
                                                        }
@@ -2291,10 +2298,10 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        compatible_bump_uv_derivs(&compat_bump, shi, mtex, i);
                                }
                        }
-                       else if(mtex->texco==TEXCO_WINDOW) {
+                       else if (mtex->texco==TEXCO_WINDOW) {
                                co= shi->winco; dx= shi->dxwin; dy= shi->dywin;
                        }
-                       else if(mtex->texco==TEXCO_STRAND) {
+                       else if (mtex->texco==TEXCO_STRAND) {
                                co= tempvec; dx= dxt; dy= dyt;
                                co[0]= shi->strandco;
                                co[1]= co[2]= 0.0f;
@@ -2303,7 +2310,7 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                dy[0]= shi->dystrand;
                                dy[1]= dy[2]= 0.0f;
                        }
-                       else if(mtex->texco==TEXCO_STRESS) {
+                       else if (mtex->texco==TEXCO_STRESS) {
                                co= tempvec; dx= dxt; dy= dyt;
                                co[0]= shi->stress;
                                co[1]= co[2]= 0.0f;
@@ -2315,24 +2322,24 @@ void do_material_tex(ShadeInput *shi, Render *re)
                        else continue;  // can happen when texco defines disappear and it renders old files
 
                        /* the pointer defines if bumping happens */
-                       if(mtex->mapto & (MAP_NORM|MAP_WARP)) {
+                       if (mtex->mapto & (MAP_NORM|MAP_WARP)) {
                                texres.nor= norvec;
                                norvec[0]= norvec[1]= norvec[2]= 0.0;
                        }
                        else texres.nor= NULL;
                        
-                       if(warpdone) {
+                       if (warpdone) {
                                add_v3_v3v3(tempvec, co, warpvec);
                                co= tempvec;
                        }
 
                        /* XXX texture node trees don't work for this yet */
-                       if(texres.nor && !((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP))) {
-                               if(use_compat_bump) {
+                       if (texres.nor && !((tex->type==TEX_IMAGE) && (tex->imaflag & TEX_NORMALMAP))) {
+                               if (use_compat_bump) {
                                        rgbnor = compatible_bump_compute(&compat_bump, shi, mtex, tex,
                                                &texres, Tnor*stencilTin, co, dx, dy, texvec, dxt, dyt);
                                }
-                               else if(use_ntap_bump) {
+                               else if (use_ntap_bump) {
                                        rgbnor = ntap_bump_compute(&ntap_bump, shi, mtex, tex,
                                                &texres, Tnor*stencilTin, co, dx, dy, texvec, dxt, dyt);
                                }
@@ -2348,20 +2355,20 @@ void do_material_tex(ShadeInput *shi, Render *re)
 
                        /* texture output */
 
-                       if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
+                       if ( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
                                texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                rgbnor-= TEX_RGB;
                        }
-                       if(mtex->texflag & MTEX_NEGATIVE) {
-                               if(rgbnor & TEX_RGB) {
+                       if (mtex->texflag & MTEX_NEGATIVE) {
+                               if (rgbnor & TEX_RGB) {
                                        texres.tr= 1.0f-texres.tr;
                                        texres.tg= 1.0f-texres.tg;
                                        texres.tb= 1.0f-texres.tb;
                                }
                                texres.tin= 1.0f-texres.tin;
                        }
-                       if(mtex->texflag & MTEX_STENCIL) {
-                               if(rgbnor & TEX_RGB) {
+                       if (mtex->texflag & MTEX_STENCIL) {
+                               if (rgbnor & TEX_RGB) {
                                        fact= texres.ta;
                                        texres.ta*= stencilTin;
                                        stencilTin*= fact;
@@ -2376,10 +2383,10 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                Tnor*= stencilTin;
                        }
                        
-                       if(texres.nor) {
-                               if((rgbnor & TEX_NOR)==0) {
+                       if (texres.nor) {
+                               if ((rgbnor & TEX_NOR)==0) {
                                        /* make our own normal */
-                                       if(rgbnor & TEX_RGB) {
+                                       if (rgbnor & TEX_RGB) {
                                                texres.nor[0]= texres.tr;
                                                texres.nor[1]= texres.tg;
                                                texres.nor[2]= texres.tb;
@@ -2399,10 +2406,10 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        }
                                }
                                // warping, local space
-                               if(mtex->mapto & MAP_WARP) {
+                               if (mtex->mapto & MAP_WARP) {
                                        float *warpnor= texres.nor, warpnor_[3];
                                        
-                                       if(use_ntap_bump) {
+                                       if (use_ntap_bump) {
                                                copy_v3_v3(warpnor_, texres.nor);
                                                warpnor= warpnor_;
                                                normalize_v3(warpnor_);
@@ -2413,10 +2420,10 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        warpdone= 1;
                                }
 #if 0                          
-                               if(mtex->texflag & MTEX_VIEWSPACE) {
+                               if (mtex->texflag & MTEX_VIEWSPACE) {
                                        // rotate to global coords
-                                       if(mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
-                                               if(shi->vlr && shi->obr && shi->obr->ob) {
+                                       if (mtex->texco==TEXCO_ORCO || mtex->texco==TEXCO_UV) {
+                                               if (shi->vlr && shi->obr && shi->obr->ob) {
                                                        float len= normalize_v3(texres.nor);
                                                        // can be optimized... (ton)
                                                        mul_mat3_m4_v3(shi->obr->ob->obmat, texres.nor);
@@ -2430,19 +2437,19 @@ void do_material_tex(ShadeInput *shi, Render *re)
                        }
 
                        /* mapping */
-                       if(mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
+                       if (mtex->mapto & (MAP_COL+MAP_COLSPEC+MAP_COLMIR)) {
                                float tcol[3];
                                
                                /* stencil maps on the texture control slider, not texture intensity value */
                                
                                tcol[0]=texres.tr; tcol[1]=texres.tg; tcol[2]=texres.tb;
                                
-                               if((rgbnor & TEX_RGB)==0) {
+                               if ((rgbnor & TEX_RGB)==0) {
                                        tcol[0]= mtex->r;
                                        tcol[1]= mtex->g;
                                        tcol[2]= mtex->b;
                                }
-                               else if(mtex->mapto & MAP_ALPHA) {
+                               else if (mtex->mapto & MAP_ALPHA) {
                                        texres.tin= stencilTin;
                                }
                                else texres.tin= texres.ta;
@@ -2457,19 +2464,19 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                                srgb_to_linearrgb_v3_v3(tcol, tcol);
                                }
                                
-                               if(mtex->mapto & MAP_COL) {
+                               if (mtex->mapto & MAP_COL) {
                                        float colfac= mtex->colfac*stencilTin;
                                        texture_rgb_blend(&shi->r, tcol, &shi->r, texres.tin, colfac, mtex->blendtype);
                                }
-                               if(mtex->mapto & MAP_COLSPEC) {
+                               if (mtex->mapto & MAP_COLSPEC) {
                                        float colspecfac= mtex->colspecfac*stencilTin;
                                        texture_rgb_blend(&shi->specr, tcol, &shi->specr, texres.tin, colspecfac, mtex->blendtype);
                                }
-                               if(mtex->mapto & MAP_COLMIR) {
+                               if (mtex->mapto & MAP_COLMIR) {
                                        float mirrfac= mtex->mirrfac*stencilTin;
 
                                        // exception for envmap only
-                                       if(tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
+                                       if (tex->type==TEX_ENVMAP && mtex->blendtype==MTEX_BLEND) {
                                                fact= texres.tin*mirrfac;
                                                facm= 1.0f- fact;
                                                shi->refcol[0]= fact + facm*shi->refcol[0];
@@ -2482,8 +2489,8 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        }
                                }
                        }
-                       if( (mtex->mapto & MAP_NORM) ) {
-                               if(texres.nor) {
+                       if ( (mtex->mapto & MAP_NORM) ) {
+                               if (texres.nor) {
                                        float norfac= mtex->norfac;
                                        
                                        /* we need to code blending modes for normals too once.. now 1 exception hardcoded */
@@ -2493,7 +2500,7 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                                found_nmapping = 1;
                                                
                                                /* qdn: for normalmaps, to invert the normalmap vector,
-                                                  it is better to negate x & y instead of subtracting the vector as was done before */
+                                                * it is better to negate x & y instead of subtracting the vector as was done before */
                                                if (norfac < 0.0f) {
                                                        texres.nor[0] = -texres.nor[0];
                                                        texres.nor[1] = -texres.nor[1];
@@ -2501,7 +2508,7 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                                fact = Tnor*fabsf(norfac);
                                                if (fact>1.f) fact = 1.f;
                                                facm = 1.f-fact;
-                                               if(mtex->normapspace == MTEX_NSPACE_TANGENT) {
+                                               if (mtex->normapspace == MTEX_NSPACE_TANGENT) {
                                                        /* qdn: tangent space */
                                                        float B[3], tv[3];
                                                        const float * no = iFirstTimeNMap!=0 ? shi->nmapnorm : shi->vn;
@@ -2521,12 +2528,12 @@ void do_material_tex(ShadeInput *shi, Render *re)
 
                                                        copy_v3_v3(nor, texres.nor);
 
-                                                       if(mtex->normapspace == MTEX_NSPACE_CAMERA);
-                                                       else if(mtex->normapspace == MTEX_NSPACE_WORLD) {
+                                                       if (mtex->normapspace == MTEX_NSPACE_CAMERA);
+                                                       else if (mtex->normapspace == MTEX_NSPACE_WORLD) {
                                                                mul_mat3_m4_v3(re->viewmat, nor);
                                                        }
-                                                       else if(mtex->normapspace == MTEX_NSPACE_OBJECT) {
-                                                               if(shi->obr && shi->obr->ob)
+                                                       else if (mtex->normapspace == MTEX_NSPACE_OBJECT) {
+                                                               if (shi->obr && shi->obr->ob)
                                                                        mul_mat3_m4_v3(shi->obr->ob->obmat, nor);
                                                                mul_mat3_m4_v3(re->viewmat, nor);
                                                        }
@@ -2549,7 +2556,7 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                                else {
                                                        float nor[3], dot;
        
-                                                       if(shi->mat->mode & MA_TANGENT_V) {
+                                                       if (shi->mat->mode & MA_TANGENT_V) {
                                                                shi->tang[0]+= Tnor*norfac*texres.nor[0];
                                                                shi->tang[1]+= Tnor*norfac*texres.nor[1];
                                                                shi->tang[2]+= Tnor*norfac*texres.nor[2];
@@ -2576,10 +2583,10 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                }
                        }
 
-                       if( mtex->mapto & MAP_DISPLACE ) {
+                       if ( mtex->mapto & MAP_DISPLACE ) {
                                /* Now that most textures offer both Nor and Intensity, allow  */
                                /* both to work, and let user select with slider.   */
-                               if(texres.nor) {
+                               if (texres.nor) {
                                        float norfac= mtex->norfac;
 
                                        shi->displace[0]+= 0.2f*Tnor*norfac*texres.nor[0];
@@ -2587,94 +2594,94 @@ void do_material_tex(ShadeInput *shi, Render *re)
                                        shi->displace[2]+= 0.2f*Tnor*norfac*texres.nor[2];
                                }
                                
-                               if(rgbnor & TEX_RGB) {
+                               if (rgbnor & TEX_RGB) {
                                        texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                }
 
                                factt= (0.5f-texres.tin)*mtex->dispfac*stencilTin; facmm= 1.0f-factt;
 
-                               if(mtex->blendtype==MTEX_BLEND) {
+                               if (mtex->blendtype==MTEX_BLEND) {
                                        shi->displace[0]= factt*shi->vn[0] + facmm*shi->displace[0];
                                        shi->displace[1]= factt*shi->vn[1] + facmm*shi->displace[1];
                                        shi->displace[2]= factt*shi->vn[2] + facmm*shi->displace[2];
                                }
-                               else if(mtex->blendtype==MTEX_MUL) {
+                               else if (mtex->blendtype==MTEX_MUL) {
                                        shi->displace[0]*= factt*shi->vn[0];
                                        shi->displace[1]*= factt*shi->vn[1];
                                        shi->displace[2]*= factt*shi->vn[2];
                                }
                                else { /* add or sub */
-                                       if(mtex->blendtype==MTEX_SUB) factt= -factt;
+                                       if (mtex->blendtype==MTEX_SUB) factt= -factt;
                                        shi->displace[0]+= factt*shi->vn[0];
                                        shi->displace[1]+= factt*shi->vn[1];
                                        shi->displace[2]+= factt*shi->vn[2];
                                }
                        }
 
-                       if(mtex->mapto & MAP_VARS) {
+                       if (mtex->mapto & MAP_VARS) {
                                /* stencil maps on the texture control slider, not texture intensity value */
                                
-                               if(rgbnor & TEX_RGB) {
-                                       if(texres.talpha) texres.tin= texres.ta;
+                               if (rgbnor & TEX_RGB) {
+                                       if (texres.talpha) texres.tin= texres.ta;
                                        else texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                }
 
-                               if(mtex->mapto & MAP_REF) {
+                               if (mtex->mapto & MAP_REF) {
                                        float difffac= mtex->difffac*stencilTin;
 
                                        shi->refl= texture_value_blend(mtex->def_var, shi->refl, texres.tin, difffac, mtex->blendtype);
-                                       if(shi->refl<0.0f) shi->refl= 0.0f;
+                                       if (shi->refl<0.0f) shi->refl= 0.0f;
                                }
-                               if(mtex->mapto & MAP_SPEC) {
+                               if (mtex->mapto & MAP_SPEC) {
                                        float specfac= mtex->specfac*stencilTin;
                                        
                                        shi->spec= texture_value_blend(mtex->def_var, shi->spec, texres.tin, specfac, mtex->blendtype);
-                                       if(shi->spec<0.0f) shi->spec= 0.0f;
+                                       if (shi->spec<0.0f) shi->spec= 0.0f;
                                }
-                               if(mtex->mapto & MAP_EMIT) {
+                               if (mtex->mapto & MAP_EMIT) {
                                        float emitfac= mtex->emitfac*stencilTin;
 
                                        shi->emit= texture_value_blend(mtex->def_var, shi->emit, texres.tin, emitfac, mtex->blendtype);
-                                       if(shi->emit<0.0f) shi->emit= 0.0f;
+                                       if (shi->emit<0.0f) shi->emit= 0.0f;
                                }
-                               if(mtex->mapto & MAP_ALPHA) {
+                               if (mtex->mapto & MAP_ALPHA) {
                                        float alphafac= mtex->alphafac*stencilTin;
 
                                        shi->alpha= texture_value_blend(mtex->def_var, shi->alpha, texres.tin, alphafac, mtex->blendtype);
-                                       if(shi->alpha<0.0f) shi->alpha= 0.0f;
-                                       else if(shi->alpha>1.0f) shi->alpha= 1.0f;
+                                       if (shi->alpha<0.0f) shi->alpha= 0.0f;
+                                       else if (shi->alpha>1.0f) shi->alpha= 1.0f;
                                }
-                               if(mtex->mapto & MAP_HAR) {
+                               if (mtex->mapto & MAP_HAR) {
                                        float har;  // have to map to 0-1
                                        float hardfac= mtex->hardfac*stencilTin;
                                        
                                        har= ((float)shi->har)/128.0f;
                                        har= 128.0f*texture_value_blend(mtex->def_var, har, texres.tin, hardfac, mtex->blendtype);
                                        
-                                       if(har<1.0f) shi->har= 1;
-                                       else if(har>511) shi->har= 511;
+                                       if (har<1.0f) shi->har= 1;
+                                       else if (har>511) shi->har= 511;
                                        else shi->har= (int)har;
                                }
-                               if(mtex->mapto & MAP_RAYMIRR) {
+                               if (mtex->mapto & MAP_RAYMIRR) {
                                        float raymirrfac= mtex->raymirrfac*stencilTin;
 
                                        shi->ray_mirror= texture_value_blend(mtex->def_var, shi->ray_mirror, texres.tin, raymirrfac, mtex->blendtype);
-                                       if(shi->ray_mirror<0.0f) shi->ray_mirror= 0.0f;
-                                       else if(shi->ray_mirror>1.0f) shi->ray_mirror= 1.0f;
+                                       if (shi->ray_mirror<0.0f) shi->ray_mirror= 0.0f;
+                                       else if (shi->ray_mirror>1.0f) shi->ray_mirror= 1.0f;
                                }
-                               if(mtex->mapto & MAP_TRANSLU) {
+                               if (mtex->mapto & MAP_TRANSLU) {
                                        float translfac= mtex->translfac*stencilTin;
 
                                        shi->translucency= texture_value_blend(mtex->def_var, shi->translucency, texres.tin, translfac, mtex->blendtype);
-                                       if(shi->translucency<0.0f) shi->translucency= 0.0f;
-                                       else if(shi->translucency>1.0f) shi->translucency= 1.0f;
+                                       if (shi->translucency<0.0f) shi->translucency= 0.0f;
+                                       else if (shi->translucency>1.0f) shi->translucency= 1.0f;
                                }
-                               if(mtex->mapto & MAP_AMB) {
+                               if (mtex->mapto & MAP_AMB) {
                                        float ambfac= mtex->ambfac*stencilTin;
 
                                        shi->amb= texture_value_blend(mtex->def_var, shi->amb, texres.tin, ambfac, mtex->blendtype);
-                                       if(shi->amb<0.0f) shi->amb= 0.0f;
-                                       else if(shi->amb>1.0f) shi->amb= 1.0f;
+                                       if (shi->amb<0.0f) shi->amb= 0.0f;
+                                       else if (shi->amb>1.0f) shi->amb= 1.0f;
                                        
                                        shi->ambr= shi->amb*re->wrld.ambr;
                                        shi->ambg= shi->amb*re->wrld.ambg;
@@ -2704,35 +2711,35 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float *col
        if (re->r.scemode & R_NO_TEX) return;
        /* here: test flag if there's a tex (todo) */
        
-       for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
+       for (tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
                /* separate tex switching */
-               if(shi->mat->septex & (1<<tex_nr)) continue;
+               if (shi->mat->septex & (1<<tex_nr)) continue;
                
-               if(shi->mat->mtex[tex_nr]) {
+               if (shi->mat->mtex[tex_nr]) {
                        mtex= shi->mat->mtex[tex_nr];
                        tex= mtex->tex;
-                       if(tex==0) continue;
+                       if (tex==0) continue;
                        
                        /* only process if this texture is mapped 
                         * to one that we're interested in */
                        if (!(mtex->mapto & mapto_flag)) continue;
                        
                        /* which coords */
-                       if(mtex->texco==TEXCO_OBJECT) { 
+                       if (mtex->texco==TEXCO_OBJECT) {
                                Object *ob= mtex->object;
-                               if(ob) {                                                
+                               if (ob) {
                                        copy_v3_v3(co, xyz);
-                                       if(mtex->texflag & MTEX_OB_DUPLI_ORIG) {
-                                               if(shi->obi && shi->obi->duplitexmat)
+                                       if (mtex->texflag & MTEX_OB_DUPLI_ORIG) {
+                                               if (shi->obi && shi->obi->duplitexmat)
                                                        mul_m4_v3(shi->obi->duplitexmat, co);                                   
                                        } 
                                        mul_m4_v3(ob->imat_ren, co);
                                }
                        }
                        /* not really orco, but 'local' */
-                       else if(mtex->texco==TEXCO_ORCO) {
+                       else if (mtex->texco==TEXCO_ORCO) {
                                
-                               if(mtex->texflag & MTEX_DUPLI_MAPTO) {
+                               if (mtex->texflag & MTEX_DUPLI_MAPTO) {
                                        copy_v3_v3(co, shi->duplilo);
                                }
                                else {
@@ -2741,7 +2748,7 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float *col
                                        mul_m4_v3(ob->imat_ren, co);
                                }
                        }
-                       else if(mtex->texco==TEXCO_GLOB) {
+                       else if (mtex->texco==TEXCO_GLOB) {
                                copy_v3_v3(co, xyz);
                                mul_m4_v3(re->viewinv, co);
                        }
@@ -2749,19 +2756,19 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float *col
 
                        texres.nor= NULL;
                        
-                       if(tex->type==TEX_IMAGE) {
+                       if (tex->type==TEX_IMAGE) {
                                continue;       /* not supported yet */                         
                                //do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
                        }
                        else {
                                /* placement */
-                               if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
+                               if (mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
                                else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
 
-                               if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
+                               if (mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
                                else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
 
-                               if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
+                               if (mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
                                else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
                        }
                        
@@ -2769,20 +2776,20 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float *col
                        
                        /* texture output */
 
-                       if( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
+                       if ( (rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
                                texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                rgbnor-= TEX_RGB;
                        }
-                       if(mtex->texflag & MTEX_NEGATIVE) {
-                               if(rgbnor & TEX_RGB) {
+                       if (mtex->texflag & MTEX_NEGATIVE) {
+                               if (rgbnor & TEX_RGB) {
                                        texres.tr= 1.0f-texres.tr;
                                        texres.tg= 1.0f-texres.tg;
                                        texres.tb= 1.0f-texres.tb;
                                }
                                texres.tin= 1.0f-texres.tin;
                        }
-                       if(mtex->texflag & MTEX_STENCIL) {
-                               if(rgbnor & TEX_RGB) {
+                       if (mtex->texflag & MTEX_STENCIL) {
+                               if (rgbnor & TEX_RGB) {
                                        fact= texres.ta;
                                        texres.ta*= stencilTin;
                                        stencilTin*= fact;
@@ -2795,70 +2802,71 @@ void do_volume_tex(ShadeInput *shi, const float *xyz, int mapto_flag, float *col
                        }
                        
                        
-                       if((mapto_flag & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL)) && (mtex->mapto & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL))) {
+                       if ((mapto_flag & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL)) && (mtex->mapto & (MAP_EMISSION_COL+MAP_TRANSMISSION_COL+MAP_REFLECTION_COL))) {
                                float tcol[3];
                                
                                /* stencil maps on the texture control slider, not texture intensity value */
                                
-                               if((rgbnor & TEX_RGB)==0) {
+                               if ((rgbnor & TEX_RGB)==0) {
                                        tcol[0]= mtex->r;
                                        tcol[1]= mtex->g;
                                        tcol[2]= mtex->b;
-                               } else {
+                               }
+                               else {
                                        tcol[0]=texres.tr;
                                        tcol[1]=texres.tg;
                                        tcol[2]=texres.tb;
-                                       if(texres.talpha)
+                                       if (texres.talpha)
                                                texres.tin= texres.ta;
                                }
                                
                                /* used for emit */
-                               if((mapto_flag & MAP_EMISSION_COL) && (mtex->mapto & MAP_EMISSION_COL)) {
+                               if ((mapto_flag & MAP_EMISSION_COL) && (mtex->mapto & MAP_EMISSION_COL)) {
                                        float colemitfac= mtex->colemitfac*stencilTin;
                                        texture_rgb_blend(col, tcol, col, texres.tin, colemitfac, mtex->blendtype);
                                }
                                
-                               if((mapto_flag & MAP_REFLECTION_COL) && (mtex->mapto & MAP_REFLECTION_COL)) {
+                               if ((mapto_flag & MAP_REFLECTION_COL) && (mtex->mapto & MAP_REFLECTION_COL)) {
                                        float colreflfac= mtex->colreflfac*stencilTin;
                                        texture_rgb_blend(col, tcol, col, texres.tin, colreflfac, mtex->blendtype);
                                }
                                
-                               if((mapto_flag & MAP_TRANSMISSION_COL) && (mtex->mapto & MAP_TRANSMISSION_COL)) {
+                               if ((mapto_flag & MAP_TRANSMISSION_COL) && (mtex->mapto & MAP_TRANSMISSION_COL)) {
                                        float coltransfac= mtex->coltransfac*stencilTin;
                                        texture_rgb_blend(col, tcol, col, texres.tin, coltransfac, mtex->blendtype);
                                }
                        }
                        
-                       if((mapto_flag & MAP_VARS) && (mtex->mapto & MAP_VARS)) {
+                       if ((mapto_flag & MAP_VARS) && (mtex->mapto & MAP_VARS)) {
                                /* stencil maps on the texture control slider, not texture intensity value */
                                
                                /* convert RGB to intensity if intensity info isn't provided */
                                if (!(rgbnor & TEX_INT)) {
                                        if (rgbnor & TEX_RGB) {
-                                               if(texres.talpha) texres.tin= texres.ta;
+                                               if (texres.talpha) texres.tin= texres.ta;
                                                else texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                        }
                                }
                                
-                               if((mapto_flag & MAP_EMISSION) && (mtex->mapto & MAP_EMISSION)) {
+                               if ((mapto_flag & MAP_EMISSION) && (mtex->mapto & MAP_EMISSION)) {
                                        float emitfac= mtex->emitfac*stencilTin;
 
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, emitfac, mtex->blendtype);
-                                       if(*val<0.0f) *val= 0.0f;
+                                       if (*val<0.0f) *val= 0.0f;
                                }
-                               if((mapto_flag & MAP_DENSITY) && (mtex->mapto & MAP_DENSITY)) {
+                               if ((mapto_flag & MAP_DENSITY) && (mtex->mapto & MAP_DENSITY)) {
                                        float densfac= mtex->densfac*stencilTin;
 
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, densfac, mtex->blendtype);
                                        CLAMP(*val, 0.0f, 1.0f);
                                }
-                               if((mapto_flag & MAP_SCATTERING) && (mtex->mapto & MAP_SCATTERING)) {
+                               if ((mapto_flag & MAP_SCATTERING) && (mtex->mapto & MAP_SCATTERING)) {
                                        float scatterfac= mtex->scatterfac*stencilTin;
                                        
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, scatterfac, mtex->blendtype);
                                        CLAMP(*val, 0.0f, 1.0f);
                                }
-                               if((mapto_flag & MAP_REFLECTION) && (mtex->mapto & MAP_REFLECTION)) {
+                               if ((mapto_flag & MAP_REFLECTION) && (mtex->mapto & MAP_REFLECTION)) {
                                        float reflfac= mtex->reflfac*stencilTin;
                                        
                                        *val = texture_value_blend(mtex->def_var, *val, texres.tin, reflfac, mtex->blendtype);
@@ -2882,8 +2890,8 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4])
        if (R.r.scemode & R_NO_TEX) return;
        
        mtex= har->mat->mtex[0];
-       if(har->mat->septex & (1<<0)) return;
-       if(mtex->tex==NULL) return;
+       if (har->mat->septex & (1<<0)) return;
+       if (mtex->tex==NULL) return;
        
        /* no normal mapping */
        texres.nor= NULL;
@@ -2895,32 +2903,32 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4])
        osatex= (har->mat->texco & TEXCO_OSA);
 
        /* placement */
-       if(mtex->projx) texvec[0]= mtex->size[0]*(texvec[mtex->projx-1]+mtex->ofs[0]);
+       if (mtex->projx) texvec[0]= mtex->size[0]*(texvec[mtex->projx-1]+mtex->ofs[0]);
        else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
        
-       if(mtex->projy) texvec[1]= mtex->size[1]*(texvec[mtex->projy-1]+mtex->ofs[1]);
+       if (mtex->projy) texvec[1]= mtex->size[1]*(texvec[mtex->projy-1]+mtex->ofs[1]);
        else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
        
-       if(mtex->projz) texvec[2]= mtex->size[2]*(texvec[mtex->projz-1]+mtex->ofs[2]);
+       if (mtex->projz) texvec[2]= mtex->size[2]*(texvec[mtex->projz-1]+mtex->ofs[2]);
        else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
        
-       if(osatex) {
+       if (osatex) {
        
                dx= 1.0f/har->rad;
        
-               if(mtex->projx) {
+               if (mtex->projx) {
                        dxt[0]= mtex->size[0]*dx;
                        dyt[0]= mtex->size[0]*dx;
                }
                else dxt[0]= dyt[0]= 0.0;
                
-               if(mtex->projy) {
+               if (mtex->projy) {
                        dxt[1]= mtex->size[1]*dx;
                        dyt[1]= mtex->size[1]*dx;
                }
                else dxt[1]= dyt[1]= 0.0;
                
-               if(mtex->projz) {
+               if (mtex->projz) {
                        dxt[2]= 0.0;
                        dyt[2]= 0.0;
                }
@@ -2928,17 +2936,17 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4])
 
        }
 
-       if(mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
+       if (mtex->tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
        
        rgb= multitex(mtex->tex, texvec, dxt, dyt, osatex, &texres, 0, mtex->which_output);
 
        /* texture output */
-       if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
+       if (rgb && (mtex->texflag & MTEX_RGBTOINT)) {
                texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                rgb= 0;
        }
-       if(mtex->texflag & MTEX_NEGATIVE) {
-               if(rgb) {
+       if (mtex->texflag & MTEX_NEGATIVE) {
+               if (rgb) {
                        texres.tr= 1.0f-texres.tr;
                        texres.tg= 1.0f-texres.tg;
                        texres.tb= 1.0f-texres.tb;
@@ -2947,14 +2955,14 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4])
        }
 
        /* mapping */
-       if(mtex->mapto & MAP_COL) {
+       if (mtex->mapto & MAP_COL) {
                
-               if(rgb==0) {
+               if (rgb==0) {
                        texres.tr= mtex->r;
                        texres.tg= mtex->g;
                        texres.tb= mtex->b;
                }
-               else if(mtex->mapto & MAP_ALPHA) {
+               else if (mtex->mapto & MAP_ALPHA) {
                        texres.tin= 1.0;
                }
                else texres.tin= texres.ta;
@@ -2972,18 +2980,18 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4])
                fact= texres.tin*mtex->colfac;
                facm= 1.0f-fact;
                
-               if(mtex->blendtype==MTEX_MUL) {
+               if (mtex->blendtype==MTEX_MUL) {
                        facm= 1.0f-mtex->colfac;
                }
                
-               if(mtex->blendtype==MTEX_SUB) fact= -fact;
+               if (mtex->blendtype==MTEX_SUB) fact= -fact;
 
-               if(mtex->blendtype==MTEX_BLEND) {
+               if (mtex->blendtype==MTEX_BLEND) {
                        col_r[0]= (fact*texres.tr + facm*har->r);
                        col_r[1]= (fact*texres.tg + facm*har->g);
                        col_r[2]= (fact*texres.tb + facm*har->b);
                }
-               else if(mtex->blendtype==MTEX_MUL) {
+               else if (mtex->blendtype==MTEX_MUL) {
                        col_r[0]= (facm+fact*texres.tr)*har->r;
                        col_r[1]= (facm+fact*texres.tg)*har->g;
                        col_r[2]= (facm+fact*texres.tb)*har->b;
@@ -2998,9 +3006,9 @@ void do_halo_tex(HaloRen *har, float xn, float yn, float col_r[4])
                        CLAMP(col_r[2], 0.0f, 1.0f);
                }
        }
-       if(mtex->mapto & MAP_ALPHA) {
-               if(rgb) {
-                       if(texres.talpha) texres.tin= texres.ta;
+       if (mtex->mapto & MAP_ALPHA) {
+               if (rgb) {
+                       if (texres.talpha) texres.tin= texres.ta;
                        else texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                }
                                
@@ -3024,19 +3032,19 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
        /* todo: add flag to test if there's a tex */
        texres.nor= NULL;
        
-       for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
-               if(R.wrld.mtex[tex_nr]) {
+       for (tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
+               if (R.wrld.mtex[tex_nr]) {
                        mtex= R.wrld.mtex[tex_nr];
                        
                        tex= mtex->tex;
-                       if(tex==0) continue;
-                       /* if(mtex->mapto==0) continue; */
+                       if (tex==0) continue;
+                       /* if (mtex->mapto==0) continue; */
                        
                        /* which coords */
                        co= lo;
                        
                        /* dxt dyt just from 1 value */
-                       if(dxyview) {
+                       if (dxyview) {
                                dxt[0]= dxt[1]= dxt[2]= dxyview[0];
                                dyt[0]= dyt[1]= dyt[2]= dxyview[1];
                        }
@@ -3046,11 +3054,11 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                        }
                        
                        /* Grab the mapping settings for this texture */
-                       switch(mtex->texco) {
+                       switch (mtex->texco) {
                        case TEXCO_ANGMAP:
                                /* only works with texture being "real" */
                                /* use saacos(), fixes bug [#22398], float precision caused lo[2] to be slightly less then -1.0 */
-                               if(lo[0] || lo[1]) { /* check for zero case [#24807] */
+                               if (lo[0] || lo[1]) { /* check for zero case [#24807] */
                                        fact= (1.0f/(float)M_PI)*saacos(lo[2])/(sqrtf(lo[0]*lo[0] + lo[1]*lo[1]));
                                        tempvec[0]= lo[0]*fact;
                                        tempvec[1]= lo[1]*fact;
@@ -3068,9 +3076,9 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                                
                        case TEXCO_H_SPHEREMAP:
                        case TEXCO_H_TUBEMAP:
-                               if(skyflag & WO_ZENUP) {
-                                       if(mtex->texco==TEXCO_H_TUBEMAP) map_to_tube( tempvec, tempvec+1,lo[0], lo[2], lo[1]);
-                                       else map_to_sphere( tempvec, tempvec+1,lo[0], lo[2], lo[1]);
+                               if (skyflag & WO_ZENUP) {
+                                       if (mtex->texco==TEXCO_H_TUBEMAP) map_to_tube( tempvec, tempvec+1, lo[0], lo[2], lo[1]);
+                                       else map_to_sphere(tempvec, tempvec+1, lo[0], lo[2], lo[1]);
                                        /* tube/spheremap maps for outside view, not inside */
                                        tempvec[0]= 1.0f-tempvec[0];
                                        /* only top half */
@@ -3093,7 +3101,7 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                                co= tempvec;
                                break;
                        case TEXCO_OBJECT:
-                               if(mtex->object) {
+                               if (mtex->object) {
                                        copy_v3_v3(tempvec, lo);
                                        mul_m4_v3(mtex->object->imat_ren, tempvec);
                                        co= tempvec;
@@ -3101,7 +3109,7 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                                break;
                                
                        case TEXCO_GLOB:
-                               if(rco) {
+                               if (rco) {
                                        copy_v3_v3(tempvec, rco);
                                        mul_m4_v3(R.viewinv, tempvec);
                                        co= tempvec;
@@ -3117,35 +3125,35 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                        }
                        
                        /* placement */                 
-                       if(mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
+                       if (mtex->projx) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
                        else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
                        
-                       if(mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
+                       if (mtex->projy) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
                        else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
                        
-                       if(mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
+                       if (mtex->projz) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
                        else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
                        
                        /* texture */
-                       if(tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
+                       if (tex->type==TEX_IMAGE) do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
                
                        rgb= multitex(mtex->tex, texvec, dxt, dyt, R.osa, &texres, thread, mtex->which_output);
                        
                        /* texture output */
-                       if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
+                       if (rgb && (mtex->texflag & MTEX_RGBTOINT)) {
                                texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                rgb= 0;
                        }
-                       if(mtex->texflag & MTEX_NEGATIVE) {
-                               if(rgb) {
+                       if (mtex->texflag & MTEX_NEGATIVE) {
+                               if (rgb) {
                                        texres.tr= 1.0f-texres.tr;
                                        texres.tg= 1.0f-texres.tg;
                                        texres.tb= 1.0f-texres.tb;
                                }
                                else texres.tin= 1.0f-texres.tin;
                        }
-                       if(mtex->texflag & MTEX_STENCIL) {
-                               if(rgb) {
+                       if (mtex->texflag & MTEX_STENCIL) {
+                               if (rgb) {
                                        fact= texres.ta;
                                        texres.ta*= stencilTin;
                                        stencilTin*= fact;
@@ -3157,15 +3165,15 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                                }
                        }
                        else {
-                               if(rgb) texres.ta *= stencilTin;
+                               if (rgb) texres.ta *= stencilTin;
                                else texres.tin*= stencilTin;
                        }
                        
                        /* color mapping */
-                       if(mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
+                       if (mtex->mapto & (WOMAP_HORIZ+WOMAP_ZENUP+WOMAP_ZENDOWN)) {
                                float tcol[3];
                                
-                               if(rgb==0) {
+                               if (rgb==0) {
                                        texres.tr= mtex->r;
                                        texres.tg= mtex->g;
                                        texres.tb= mtex->b;
@@ -3184,29 +3192,29 @@ void do_sky_tex(const float rco[3], float lo[3], const float dxyview[2], float h
                                                srgb_to_linearrgb_v3_v3(tcol, tcol);
                                }
 
-                               if(mtex->mapto & WOMAP_HORIZ) {
+                               if (mtex->mapto & WOMAP_HORIZ) {
                                        texture_rgb_blend(hor, tcol, hor, texres.tin, mtex->colfac, mtex->blendtype);
                                }
-                               if(mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
+                               if (mtex->mapto & (WOMAP_ZENUP+WOMAP_ZENDOWN)) {
                                        float zenfac = 0.0f;
 
-                                       if(R.wrld.skytype & WO_SKYREAL) {
-                                               if((skyflag & WO_ZENUP)) {
-                                                       if(mtex->mapto & WOMAP_ZENUP) zenfac= mtex->zenupfac;
+                                       if (R.wrld.skytype & WO_SKYREAL) {
+                                               if ((skyflag & WO_ZENUP)) {
+                                                       if (mtex->mapto & WOMAP_ZENUP) zenfac= mtex->zenupfac;
                                                }
-                                               else if(mtex->mapto & WOMAP_ZENDOWN) zenfac= mtex->zendownfac;
+                                               else if (mtex->mapto & WOMAP_ZENDOWN) zenfac= mtex->zendownfac;
                                        }
                                        else {
-                                               if(mtex->mapto & WOMAP_ZENUP) zenfac= mtex->zenupfac;
-                                               else if(mtex->mapto & WOMAP_ZENDOWN) zenfac= mtex->zendownfac;
+                                               if (mtex->mapto & WOMAP_ZENUP) zenfac= mtex->zenupfac;
+                                               else if (mtex->mapto & WOMAP_ZENDOWN) zenfac= mtex->zendownfac;
                                        }
                                        
-                                       if(zenfac != 0.0f)
+                                       if (zenfac != 0.0f)
                                                texture_rgb_blend(zen, tcol, zen, texres.tin, zenfac, mtex->blendtype);
                                }
                        }
-                       if(mtex->mapto & WOMAP_BLEND) {
-                               if(rgb) texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
+                       if (mtex->mapto & WOMAP_BLEND) {
+                               if (rgb) texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                
                                *blend= texture_value_blend(mtex->def_var, *blend, texres.tin, mtex->blendfac, mtex->blendtype);
                        }
@@ -3230,25 +3238,25 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r
        if (R.r.scemode & R_NO_TEX) return;
        tex_nr= 0;
        
-       for(; tex_nr<MAX_MTEX; tex_nr++) {
+       for (; tex_nr<MAX_MTEX; tex_nr++) {
                
-               if(la->mtex[tex_nr]) {
+               if (la->mtex[tex_nr]) {
                        mtex= la->mtex[tex_nr];
                        
                        tex= mtex->tex;
-                       if(tex==NULL) continue;
+                       if (tex==NULL) continue;
                        texres.nor= NULL;
                        
                        /* which coords */
-                       if(mtex->texco==TEXCO_OBJECT) {
+                       if (mtex->texco==TEXCO_OBJECT) {
                                ob= mtex->object;
-                               if(ob) {
+                               if (ob) {
                                        co= tempvec;
                                        dx= dxt;
                                        dy= dyt;
                                        copy_v3_v3(tempvec, shi->co);
                                        mul_m4_v3(ob->imat_ren, tempvec);
-                                       if(shi->osatex) {
+                                       if (shi->osatex) {
                                                copy_v3_v3(dxt, shi->dxco);
                                                copy_v3_v3(dyt, shi->dyco);
                                                mul_mat3_m4_v3(ob->imat_ren, dxt);
@@ -3260,17 +3268,17 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r
                                        dx= shi->dxco; dy= shi->dyco;
                                }
                        }
-                       else if(mtex->texco==TEXCO_GLOB) {
+                       else if (mtex->texco==TEXCO_GLOB) {
                                co= shi->gl; dx= shi->dxco; dy= shi->dyco;
                                copy_v3_v3(shi->gl, shi->co);
                                mul_m4_v3(R.viewinv, shi->gl);
                        }
-                       else if(mtex->texco==TEXCO_VIEW) {
+                       else if (mtex->texco==TEXCO_VIEW) {
                                
                                copy_v3_v3(tempvec, lavec);
                                mul_m3_v3(la->imat, tempvec);
                                
-                               if(la->type==LA_SPOT) {
+                               if (la->type==LA_SPOT) {
                                        tempvec[0]*= la->spottexfac;
                                        tempvec[1]*= la->spottexfac;
                                /* project from 3d to 2d */
@@ -3280,7 +3288,7 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r
                                co= tempvec; 
                                
                                dx= dxt; dy= dyt;       
-                               if(shi->osatex) {
+                               if (shi->osatex) {
                                        copy_v3_v3(dxt, shi->dxlv);
                                        copy_v3_v3(dyt, shi->dylv);
                                        /* need some matrix conversion here? la->imat is a [3][3]  matrix!!! **/
@@ -3294,39 +3302,43 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r
                        
                        
                        /* placement */
-                       if(mtex->projx && co) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
+                       if (mtex->projx && co) texvec[0]= mtex->size[0]*(co[mtex->projx-1]+mtex->ofs[0]);
                        else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
                        
-                       if(mtex->projy && co) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
+                       if (mtex->projy && co) texvec[1]= mtex->size[1]*(co[mtex->projy-1]+mtex->ofs[1]);
                        else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
                        
-                       if(mtex->projz && co) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
+                       if (mtex->projz && co) texvec[2]= mtex->size[2]*(co[mtex->projz-1]+mtex->ofs[2]);
                        else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
                        
-                       if(shi->osatex) {
+                       if (shi->osatex) {
                                if (!dx) {
-                                       for(i=0;i<2;i++) { 
+                                       for (i=0;i<2;i++) {
                                                dxt[i] = dyt[i] = 0.0;
                                        }
-                               } else {
-                                       if(mtex->projx) {
+                               }
+                               else {
+                                       if (mtex->projx) {
                                                dxt[0]= mtex->size[0]*dx[mtex->projx-1];
                                                dyt[0]= mtex->size[0]*dy[mtex->projx-1];
-                                       } else {
+                                       }
+                                       else {
                                                dxt[0]= 0.0;
                                                dyt[0]= 0.0;
                                        }
-                                       if(mtex->projy) {
+                                       if (mtex->projy) {
                                                dxt[1]= mtex->size[1]*dx[mtex->projy-1];
                                                dyt[1]= mtex->size[1]*dy[mtex->projy-1];
-                                       } else {
+                                       }
+                                       else {
                                                dxt[1]= 0.0;
                                                dyt[1]= 0.0;
                                        }
-                                       if(mtex->projz) {
+                                       if (mtex->projz) {
                                                dxt[2]= mtex->size[2]*dx[mtex->projz-1];
                                                dyt[2]= mtex->size[2]*dy[mtex->projz-1];
-                                       } else {
+                                       }
+                                       else {
                                                dxt[2]= 0.0;
                                                dyt[2]= 0.0;
                                        }
@@ -3334,27 +3346,27 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r
                        }
                        
                        /* texture */
-                       if(tex->type==TEX_IMAGE) {
+                       if (tex->type==TEX_IMAGE) {
                                do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
                        }
                        
                        rgb= multitex(tex, texvec, dxt, dyt, shi->osatex, &texres, shi->thread, mtex->which_output);
 
                        /* texture output */
-                       if(rgb && (mtex->texflag & MTEX_RGBTOINT)) {
+                       if (rgb && (mtex->texflag & MTEX_RGBTOINT)) {
                                texres.tin= (0.35f*texres.tr+0.45f*texres.tg+0.2f*texres.tb);
                                rgb= 0;
                        }
-                       if(mtex->texflag & MTEX_NEGATIVE) {
-                               if(rgb) {
+                       if (mtex->texflag & MTEX_NEGATIVE) {
+                               if (rgb) {
                                        texres.tr= 1.0f-texres.tr;
                                        texres.tg= 1.0f-texres.tg;
                                        texres.tb= 1.0f-texres.tb;
                                }
                                else texres.tin= 1.0f-texres.tin;
                        }
-                       if(mtex->texflag & MTEX_STENCIL) {
-                               if(rgb) {
+                       if (mtex->texflag & MTEX_STENCIL) {
+                               if (rgb) {
                                        fact= texres.ta;
                                        texres.ta*= stencilTin;
                                        stencilTin*= fact;
@@ -3366,20 +3378,20 @@ void do_lamp_tex(LampRen *la, const float lavec[3], ShadeInput *shi, float col_r
                                }
                        }
                        else {
-                               if(rgb) texres.ta*= stencilTin;
+                               if (rgb) texres.ta*= stencilTin;
                                else texres.tin*= stencilTin;
                        }
                        
                        /* mapping */
-                       if(((mtex->mapto & LAMAP_COL) && (effect & LA_TEXTURE))||((mtex->mapto & LAMAP_SHAD) && (effect & LA_SHAD_TEX))) {
+                       if (((mtex->mapto & LAMAP_COL) && (effect & LA_TEXTURE))||((mtex->mapto & LAMAP_SHAD) && (effect & LA_SHAD_TEX))) {
                                float col[3];
                                
-                               if(rgb==0) {
+                               if (rgb==0) {
                                        texres.tr= mtex->r;
                                        texres.tg= mtex->g;
                                        texres.tb= mtex->b;
                                }
-                               else if(mtex->mapto & MAP_ALPHA) {
+                               else if (mtex->mapto & MAP_ALPHA) {
                                        texres.tin= stencilTin;
                                }
                                else texres.tin= texres.ta;
@@ -3415,27 +3427,27 @@ int externtex(MTex *mtex, const float vec[3], float *tin, float *tr, float *tg,
        int rgb;
        
        tex= mtex->tex;
-       if(tex==NULL) return 0;
+       if (tex==NULL) return 0;
        texr.nor= NULL;
        
        /* placement */
-       if(mtex->projx) texvec[0]= mtex->size[0]*(vec[mtex->projx-1]+mtex->ofs[0]);
+       if (mtex->projx) texvec[0]= mtex->size[0]*(vec[mtex->projx-1]+mtex->ofs[0]);
        else texvec[0]= mtex->size[0]*(mtex->ofs[0]);
        
-       if(mtex->projy) texvec[1]= mtex->size[1]*(vec[mtex->projy-1]+mtex->ofs[1]);
+       if (mtex->projy) texvec[1]= mtex->size[1]*(vec[mtex->projy-1]+mtex->ofs[1]);
        else texvec[1]= mtex->size[1]*(mtex->ofs[1]);
        
-       if(mtex->projz) texvec[2]= mtex->size[2]*(vec[mtex->projz-1]+mtex->ofs[2]);
+       if (mtex->projz) texvec[2]= mtex->size[2]*(vec[mtex->projz-1]+mtex->ofs[2]);
        else texvec[2]= mtex->size[2]*(mtex->ofs[2]);
        
        /* texture */
-       if(tex->type==TEX_IMAGE) {
+       if (tex->type==TEX_IMAGE) {
                do_2d_mapping(mtex, texvec, NULL, NULL, dxt, dyt);
        }
        
        rgb= multitex(tex, texvec, dxt, dyt, 0, &texr, thread, mtex->which_output);
        
-       if(rgb) {
+       if (rgb) {
                texr.tin= (0.35f*texr.tr+0.45f*texr.tg+0.2f*texr.tb);
        }
        else {
@@ -3466,12 +3478,12 @@ void render_realtime_texture(ShadeInput *shi, Image *ima)
        ShadeInputUV *suv= &shi->uv[shi->actuv];
        int a;
 
-       if(R.r.scemode & R_NO_TEX) return;
+       if (R.r.scemode & R_NO_TEX) return;
 
-       if(firsttime) {
+       if (firsttime) {
                BLI_lock_thread(LOCK_IMAGE);
-               if(firsttime) {
-                       for(a=0; a<BLENDER_MAX_THREADS; a++) {
+               if (firsttime) {
+                       for (a=0; a<BLENDER_MAX_THREADS; a++) {
                                memset(&imatex[a], 0, sizeof(Tex));
                                default_tex(&imatex[a]);
                                imatex[a].type= TEX_IMAGE;
@@ -3488,7 +3500,7 @@ void render_realtime_texture(ShadeInput *shi, Image *ima)
        texvec[0]= 0.5f+0.5f*suv->uv[0];
        texvec[1]= 0.5f+0.5f*suv->uv[1];
        texvec[2] = 0.0f;  // initalize it because imagewrap looks at it.
-       if(shi->osatex) {
+       if (shi->osatex) {
                dx[0]= 0.5f*suv->dxuv[0];
                dx[1]= 0.5f*suv->dxuv[1];
                dy[0]= 0.5f*suv->dyuv[0];
@@ -3497,7 +3509,7 @@ void render_realtime_texture(ShadeInput *shi, Image *ima)
        
        texr.nor= NULL;
        
-       if(shi->osatex) imagewraposa(tex, ima, NULL, texvec, dx, dy, &texr);
+       if (shi->osatex) imagewraposa(tex, ima, NULL, texvec, dx, dy, &texr);
        else imagewrap(tex, ima, NULL, texvec, &texr); 
 
        shi->vcol[0]*= texr.tr;
@@ -3507,7 +3519,7 @@ void render_realtime_texture(ShadeInput *shi, Image *ima)
 }
 
 /* A modified part of shadeinput.c -> shade_input_set_uv()
-*  Used for sampling UV mapped texture color */
+ *  Used for sampling UV mapped texture color */
 static void textured_face_generate_uv(float *uv, float *normal, float *hit, float *v1, float *v2, float *v3)
 {
 
@@ -3549,12 +3561,14 @@ Material *RE_init_sample_material(Material *orig_mat, Scene *scene)
        BKE_animsys_evaluate_animdata(scene, &mat->id, mat->adt, BKE_curframe(scene), ADT_RECALC_ANIM);
 
        /* strip material copy from unsupported flags */
-       for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
-               if(mat->septex & (1<<tex_nr)) continue;
+       for (tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
+               if (mat->septex & (1<<tex_nr)) continue;
        
-               if(mat->mtex[tex_nr]) {
+               if (mat->mtex[tex_nr]) {
                        MTex *mtex = mat->mtex[tex_nr];
 
+                       if (!mtex->tex) continue;
+
                        /* only keep compatible texflags */
                        mtex->texflag = mtex->texflag & (MTEX_RGBTOINT | MTEX_STENCIL | MTEX_NEGATIVE | MTEX_ALPHAMIX);
 
@@ -3579,9 +3593,9 @@ Material *RE_init_sample_material(Material *orig_mat, Scene *scene)
                        }
                        
                        /* if mapped to an object, calculate inverse matrices */
-                       if(mtex->texco==TEXCO_OBJECT) { 
+                       if (mtex->texco==TEXCO_OBJECT) {
                                Object *ob= mtex->object;
-                               if(ob) {
+                               if (ob) {
                                        invert_m4_m4(ob->imat, ob->obmat);
                                        copy_m4_m4(ob->imat_ren, ob->imat);
                                }
@@ -3594,10 +3608,10 @@ Material *RE_init_sample_material(Material *orig_mat, Scene *scene)
                        BKE_animsys_evaluate_animdata(scene, &tex->id, tex->adt, BKE_curframe(scene), ADT_RECALC_ANIM);
 
                        /* update texture cache if required */
-                       if(tex->type==TEX_VOXELDATA) {
+                       if (tex->type==TEX_VOXELDATA) {
                                cache_voxeldata(tex, (int)scene->r.cfra);
                        }
-                       if(tex->type==TEX_POINTDENSITY) {
+                       if (tex->type==TEX_POINTDENSITY) {
                                /* set dummy values for render and do cache */
                                Render dummy_re = {0};
                                dummy_re.scene = scene;
@@ -3609,8 +3623,8 @@ Material *RE_init_sample_material(Material *orig_mat, Scene *scene)
                        }
 
                        /* update image sequences and movies */
-                       if(tex->ima && ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
-                               if(tex->iuser.flag & IMA_ANIM_ALWAYS)
+                       if (tex->ima && ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
+                               if (tex->iuser.flag & IMA_ANIM_ALWAYS)
                                        BKE_image_user_calc_frame(&tex->iuser, (int)scene->r.cfra, 0);
                        }
                }
@@ -3624,32 +3638,35 @@ void RE_free_sample_material(Material *mat)
        int tex_nr;
 
        /* free textures */
-       for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
-               if(mat->septex & (1<<tex_nr)) continue;
-               if(mat->mtex[tex_nr]) {
+       for (tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
+               if (mat->septex & (1<<tex_nr)) continue;
+               if (mat->mtex[tex_nr]) {
                        MTex *mtex= mat->mtex[tex_nr];
-                       free_texture(mtex->tex);
-                       MEM_freeN(mtex->tex);
-                       mtex->tex = NULL;
+       
+                       if (mtex->tex) {
+                               BKE_texture_free(mtex->tex);
+                               MEM_freeN(mtex->tex);
+                               mtex->tex = NULL;
+                       }
                }
        }
 
-       free_material(mat);
+       BKE_material_free(mat);
        MEM_freeN(mat);
 }
 
 
 
 /*
-     Get material diffuse color and alpha (including linked textures) in given coordinates
-*      
-     color,alpha : input/output color values
-     volume_co : sample coordinate in global space. used by volumetric materials
-     surface_co : sample surface coordinate in global space. used by "surface" materials
-     face_index : surface face index
-     hit_quad : whether point is on second "half" of a quad
-     orcoDm : orco state derived mesh
-*/
+ *     Get material diffuse color and alpha (including linked textures) in given coordinates
+ *
+ *     color,alpha : input/output color values
+ *     volume_co : sample coordinate in global space. used by volumetric materials
+ *     surface_co : sample surface coordinate in global space. used by "surface" materials
+ *     face_index : surface face index
+ *     hit_quad : whether point is on second "half" of a quad
+ *     orcoDm : orco state derived mesh
+ */
 void RE_sample_material_color(Material *mat, float color[3], float *alpha, const float volume_co[3], const float surface_co[3], int face_index, short hit_quad, DerivedMesh *orcoDm, Object *ob)
 {
        MFace *mface;
@@ -3666,7 +3683,7 @@ void RE_sample_material_color(Material *mat, float color[3], float *alpha, const
        if (!mvert || !mface || !mat) return;
        v1=mface[face_index].v1, v2=mface[face_index].v2, v3=mface[face_index].v3;
        if (hit_quad) {v2=mface[face_index].v3; v3=mface[face_index].v4;}
-       normal_tri_v3( normal, mvert[v1].co, mvert[v2].co, mvert[v3].co);
+       normal_tri_v3(normal, mvert[v1].co, mvert[v2].co, mvert[v3].co);
 
        /* generate shadeinput with data required */
        shi.mat = mat;
@@ -3697,7 +3714,7 @@ void RE_sample_material_color(Material *mat, float color[3], float *alpha, const
 
                        /* for every uv map set coords and name */
                        for (i=0; i<layers; i++) {
-                               if(layer_index >= 0) {
+                               if (layer_index >= 0) {
                                        float *uv1, *uv2, *uv3;
                                        float l;
                                        CustomData *data = &orcoDm->faceData;
@@ -3720,7 +3737,7 @@ void RE_sample_material_color(Material *mat, float color[3], float *alpha, const
                                }
                        }
                        /* active uv map */
-                       shi.actuv = CustomData_get_active_layer_index(&orcoDm->faceData,CD_MTFACE) - layer_index;
+                       shi.actuv = CustomData_get_active_layer_index(&orcoDm->faceData, CD_MTFACE) - layer_index;
                        shi.totuv = layers;
                }