style cleanup: bake
authorCampbell Barton <ideasman42@gmail.com>
Wed, 23 Jan 2013 23:28:22 +0000 (23:28 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 23 Jan 2013 23:28:22 +0000 (23:28 +0000)
source/blender/render/intern/source/bake.c

index 41ee4a46e405ae6c824de2ed1cec9a83b263f6a0..d4451d570a46470443dfd043bc17ae1990295ba5 100644 (file)
@@ -87,7 +87,8 @@ typedef struct BakeShade {
        Image *ima;
        ImBuf *ibuf;
        
-       int rectx, recty, quad, type, vdone, ready;
+       int rectx, recty, quad, type, vdone;
+       bool ready;
 
        float dir[3];
        Object *actob;
@@ -102,11 +103,11 @@ typedef struct BakeShade {
        float *rect_float;
 
        /* displacement buffer used for normalization with unknown maximal distance */
-       int use_displacement_buffer;
+       bool use_displacement_buffer;
        float *displacement_buffer;
        float displacement_min, displacement_max;
        
-       int use_mask;
+       bool use_mask;
        char *rect_mask; /* bake pixel mask */
 
        float dxco[3], dyco[3];
@@ -118,21 +119,21 @@ typedef struct BakeShade {
 
 static void bake_set_shade_input(ObjectInstanceRen *obi, VlakRen *vlr, ShadeInput *shi, int quad, int UNUSED(isect), int x, int y, float u, float v)
 {
-       if (quad) 
+       if (quad)
                shade_input_set_triangle_i(shi, obi, vlr, 0, 2, 3);
        else
                shade_input_set_triangle_i(shi, obi, vlr, 0, 1, 2);
                
        /* cache for shadow */
-       shi->samplenr= R.shadowsamplenr[shi->thread]++;
+       shi->samplenr = R.shadowsamplenr[shi->thread]++;
+
+       shi->mask = 0xFFFF; /* all samples */
+
+       shi->u = -u;
+       shi->v = -v;
+       shi->xs = x;
+       shi->ys = y;
 
-       shi->mask= 0xFFFF; /* all samples */
-       
-       shi->u= -u;
-       shi->v= -v;
-       shi->xs= x;
-       shi->ys= y;
-       
        shade_input_set_uv(shi);
        shade_input_set_normals(shi);
 
@@ -142,21 +143,21 @@ static void bake_set_shade_input(ObjectInstanceRen *obi, VlakRen *vlr, ShadeInpu
 
        /* set up view vector to look right at the surface (note that the normal
         * is negated in the renderer so it does not need to be done here) */
-       shi->view[0]= shi->vn[0];
-       shi->view[1]= shi->vn[1];
-       shi->view[2]= shi->vn[2];
+       shi->view[0] = shi->vn[0];
+       shi->view[1] = shi->vn[1];
+       shi->view[2] = shi->vn[2];
 }
 
 static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(quad), int x, int y, float UNUSED(u), float UNUSED(v), float *tvn, float *ttang)
 {
-       BakeShade *bs= handle;
-       ShadeSample *ssamp= &bs->ssamp;
+       BakeShade *bs = handle;
+       ShadeSample *ssamp = &bs->ssamp;
        ShadeResult shr;
-       VlakRen *vlr= shi->vlr;
-       
+       VlakRen *vlr = shi->vlr;
+
        shade_input_init_material(shi);
-       
-       if (bs->type==RE_BAKE_AO) {
+
+       if (bs->type == RE_BAKE_AO) {
                ambient_occlusion(shi);
 
                if (R.r.bake_flag & R_BAKE_NORMALIZE) {
@@ -168,24 +169,24 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
                }
        }
        else {
-               if (bs->type==RE_BAKE_SHADOW) /* Why do shadows set the color anyhow?, ignore material color for baking */
+               if (bs->type == RE_BAKE_SHADOW) /* Why do shadows set the color anyhow?, ignore material color for baking */
                        shi->r = shi->g = shi->b = 1.0f;
        
                shade_input_set_shade_texco(shi);
                
                /* only do AO for a full bake (and obviously AO bakes)
                 * AO for light bakes is a leftover and might not be needed */
-               if ( ELEM3(bs->type, RE_BAKE_ALL, RE_BAKE_AO, RE_BAKE_LIGHT))
+               if (ELEM3(bs->type, RE_BAKE_ALL, RE_BAKE_AO, RE_BAKE_LIGHT))
                        shade_samples_do_AO(ssamp);
                
                if (shi->mat->nodetree && shi->mat->use_nodes) {
                        ntreeShaderExecTree(shi->mat->nodetree, shi, &shr);
-                       shi->mat= vlr->mat;             /* shi->mat is being set in nodetree */
+                       shi->mat = vlr->mat;  /* shi->mat is being set in nodetree */
                }
                else
                        shade_material_loop(shi, &shr);
-               
-               if (bs->type==RE_BAKE_NORMALS) {
+
+               if (bs->type == RE_BAKE_NORMALS) {
                        float nor[3];
 
                        copy_v3_v3(nor, shi->vn);
@@ -214,7 +215,7 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
                                mul_m3_v3(imat, nor);
                        }
                        else if (R.r.bake_normal_space == R_BAKE_SPACE_OBJECT)
-                               mul_mat3_m4_v3(ob->imat_ren, nor); /* ob->imat_ren includes viewinv! */
+                               mul_mat3_m4_v3(ob->imat_ren, nor);  /* ob->imat_ren includes viewinv! */
                        else if (R.r.bake_normal_space == R_BAKE_SPACE_WORLD)
                                mul_mat3_m4_v3(R.viewinv, nor);
 
@@ -226,66 +227,52 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
                         * the normal used in the renderer points inward. It is generated
                         * this way in calc_vertexnormals(). Should this ever change
                         * this negate must be removed. */
-                       shr.combined[0]= (-nor[0])/2.0f + 0.5f;
-                       shr.combined[1]= nor[1]/2.0f + 0.5f;
-                       shr.combined[2]= nor[2]/2.0f + 0.5f;
+                       shr.combined[0] = (-nor[0]) / 2.0f + 0.5f;
+                       shr.combined[1] = nor[1]    / 2.0f + 0.5f;
+                       shr.combined[2] = nor[2]    / 2.0f + 0.5f;
                }
-               else if (bs->type==RE_BAKE_TEXTURE) {
-                       shr.combined[0]= shi->r;
-                       shr.combined[1]= shi->g;
-                       shr.combined[2]= shi->b;
+               else if (bs->type == RE_BAKE_TEXTURE) {
+                       copy_v3_v3(shr.combined, &shi->r);
                        shr.alpha = shi->alpha;
                }
-               else if (bs->type==RE_BAKE_SHADOW) {
+               else if (bs->type == RE_BAKE_SHADOW) {
                        copy_v3_v3(shr.combined, shr.shad);
                        shr.alpha = shi->alpha;
                }
-               else if (bs->type==RE_BAKE_SPEC_COLOR) {
-                       shr.combined[0]= shi->specr;
-                       shr.combined[1]= shi->specg;
-                       shr.combined[2]= shi->specb;
+               else if (bs->type == RE_BAKE_SPEC_COLOR) {
+                       copy_v3_v3(shr.combined, &shi->specr);
                        shr.alpha = 1.0f;
                }
-               else if (bs->type==RE_BAKE_SPEC_INTENSITY) {
-                       shr.combined[0]=
-                       shr.combined[1]=
-                       shr.combined[2]= shi->spec;
+               else if (bs->type == RE_BAKE_SPEC_INTENSITY) {
+                       copy_v3_fl(shr.combined, shi->spec);
                        shr.alpha = 1.0f;
                }
-               else if (bs->type==RE_BAKE_MIRROR_COLOR) {
-                       shr.combined[0]= shi->mirr;
-                       shr.combined[1]= shi->mirg;
-                       shr.combined[2]= shi->mirb;
+               else if (bs->type == RE_BAKE_MIRROR_COLOR) {
+                       copy_v3_v3(shr.combined, &shi->mirr);
                        shr.alpha = 1.0f;
                }
-               else if (bs->type==RE_BAKE_MIRROR_INTENSITY) {
-                       shr.combined[0]=
-                       shr.combined[1]=
-                       shr.combined[2]= shi->ray_mirror;
+               else if (bs->type == RE_BAKE_MIRROR_INTENSITY) {
+                       copy_v3_fl(shr.combined, shi->ray_mirror);
                        shr.alpha = 1.0f;
                }
-               else if (bs->type==RE_BAKE_ALPHA) {
-                       shr.combined[0]=
-                       shr.combined[1]=
-                       shr.combined[2]= shi->alpha;
+               else if (bs->type == RE_BAKE_ALPHA) {
+                       copy_v3_fl(shr.combined, shi->alpha);
                        shr.alpha = 1.0f;
                }
-               else if (bs->type==RE_BAKE_EMIT) {
-                       shr.combined[0]=
-                       shr.combined[1]=
-                       shr.combined[2]= shi->emit;
+               else if (bs->type == RE_BAKE_EMIT) {
+                       copy_v3_fl(shr.combined, shi->emit);
                        shr.alpha = 1.0f;
                }
        }
        
        if (bs->rect_float && !bs->vcol) {
-               float *col= bs->rect_float + 4*(bs->rectx*y + x);
+               float *col = bs->rect_float + 4 * (bs->rectx * y + x);
                copy_v3_v3(col, shr.combined);
-               if (bs->type==RE_BAKE_ALL || bs->type==RE_BAKE_TEXTURE) {
-                       col[3]= shr.alpha;
+               if (bs->type == RE_BAKE_ALL || bs->type == RE_BAKE_TEXTURE) {
+                       col[3] = shr.alpha;
                }
                else {
-                       col[3]= 1.0;
+                       col[3] = 1.0;
                }
        }
        else {
@@ -311,10 +298,10 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
                }
                
                if (ELEM(bs->type, RE_BAKE_ALL, RE_BAKE_TEXTURE)) {
-                       col[3]= FTOCHAR(shr.alpha);
+                       col[3] = FTOCHAR(shr.alpha);
                }
                else {
-                       col[3]= 255;
+                       col[3] = 255;
                }
 
                if (bs->vcol) {
@@ -325,25 +312,25 @@ static void bake_shade(void *handle, Object *ob, ShadeInput *shi, int UNUSED(qua
                        bs->vcol->b = col[2];
                }
                else {
-                       unsigned char *imcol= (unsigned char *)(bs->rect + bs->rectx*y + x);
+                       unsigned char *imcol = (unsigned char *)(bs->rect + bs->rectx * y + x);
                        copy_v4_v4_char((char *)imcol, (char *)col);
                }
 
        }
        
        if (bs->rect_mask) {
-               bs->rect_mask[bs->rectx*y + x] = FILTER_MASK_USED;
+               bs->rect_mask[bs->rectx * y + x] = FILTER_MASK_USED;
        }
 }
 
 static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist, int x, int y)
 {
-       BakeShade *bs= handle;
+       BakeShade *bs = handle;
        float disp;
-       
+
        if (R.r.bake_flag & R_BAKE_NORMALIZE) {
                if (R.r.bake_maxdist)
-                       disp = (dist+R.r.bake_maxdist) / (R.r.bake_maxdist*2); /* alter the range from [-bake_maxdist, bake_maxdist] to [0, 1]*/
+                       disp = (dist + R.r.bake_maxdist) / (R.r.bake_maxdist * 2);  /* alter the range from [-bake_maxdist, bake_maxdist] to [0, 1]*/
                else
                        disp = dist;
        }
@@ -359,9 +346,9 @@ static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist,
        }
 
        if (bs->rect_float && !bs->vcol) {
-               float *col= bs->rect_float + 4*(bs->rectx*y + x);
+               float *col = bs->rect_float + 4 * (bs->rectx * y + x);
                col[0] = col[1] = col[2] = disp;
-               col[3]= 1.0f;
+               col[3] = 1.0f;
        }
        else {
                /* Target is char (LDR). */
@@ -369,7 +356,7 @@ static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist,
                col[0] = col[1] = col[2] = FTOCHAR(disp);
                col[3] = 255;
 
-               if(bs->vcol) {
+               if (bs->vcol) {
                        /* Vertex color baking. Vcol has no useful alpha channel (it exists
                         * but is used only for vertex painting). */
                        bs->vcol->r = col[0];
@@ -377,25 +364,25 @@ static void bake_displacement(void *handle, ShadeInput *UNUSED(shi), float dist,
                        bs->vcol->b = col[2];
                }
                else {
-                       char *imcol= (char *)(bs->rect + bs->rectx*y + x);
+                       char *imcol = (char *)(bs->rect + bs->rectx * y + x);
                        copy_v4_v4_char((char *)imcol, (char *)col);
                }
        }
        if (bs->rect_mask) {
-               bs->rect_mask[bs->rectx*y + x] = FILTER_MASK_USED;
+               bs->rect_mask[bs->rectx * y + x] = FILTER_MASK_USED;
        }
 }
 
-static int bake_intersect_tree(RayObject* raytree, Isect* isect, float *start, float *dir, float sign, float *hitco, float *dist)
+static int bake_intersect_tree(RayObject *raytree, Isect *isect, float *start, float *dir, float sign, float *hitco, float *dist)
 {
        float maxdist;
        int hit;
 
        /* might be useful to make a user setting for maxsize*/
        if (R.r.bake_maxdist > 0.0f)
-               maxdist= R.r.bake_maxdist;
+               maxdist = R.r.bake_maxdist;
        else
-               maxdist= RE_RAYTRACE_MAXDIST + R.r.bake_biasdist;
+               maxdist = RE_RAYTRACE_MAXDIST + R.r.bake_biasdist;
 
        /* 'dir' is always normalized */
        madd_v3_v3v3fl(isect->start, start, dir, -R.r.bake_biasdist);
@@ -408,7 +395,7 @@ static int bake_intersect_tree(RayObject* raytree, Isect* isect, float *start, f
        if (hit) {
                madd_v3_v3v3fl(hitco, isect->start, isect->dir, isect->dist);
 
-               *dist= isect->dist;
+               *dist = isect->dist;
        }
 
        return hit;
@@ -416,45 +403,45 @@ static int bake_intersect_tree(RayObject* raytree, Isect* isect, float *start, f
 
 static void bake_set_vlr_dxyco(BakeShade *bs, float *uv1, float *uv2, float *uv3)
 {
-       VlakRen *vlr= bs->vlr;
+       VlakRen *vlr = bs->vlr;
        float A, d1, d2, d3, *v1, *v2, *v3;
 
        if (bs->quad) {
-               v1= vlr->v1->co;
-               v2= vlr->v3->co;
-               v3= vlr->v4->co;
+               v1 = vlr->v1->co;
+               v2 = vlr->v3->co;
+               v3 = vlr->v4->co;
        }
        else {
-               v1= vlr->v1->co;
-               v2= vlr->v2->co;
-               v3= vlr->v3->co;
+               v1 = vlr->v1->co;
+               v2 = vlr->v2->co;
+               v3 = vlr->v3->co;
        }
 
        /* formula derived from barycentric coordinates:
         * (uvArea1*v1 + uvArea2*v2 + uvArea3*v3)/uvArea
         * then taking u and v partial derivatives to get dxco and dyco */
-       A= (uv2[0] - uv1[0])*(uv3[1] - uv1[1]) - (uv3[0] - uv1[0])*(uv2[1] - uv1[1]);
+       A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
 
        if (fabsf(A) > FLT_EPSILON) {
-               A= 0.5f/A;
-
-               d1= uv2[1] - uv3[1];
-               d2= uv3[1] - uv1[1];
-               d3= uv1[1] - uv2[1];
-               bs->dxco[0]= (v1[0]*d1 + v2[0]*d2 + v3[0]*d3)*A;
-               bs->dxco[1]= (v1[1]*d1 + v2[1]*d2 + v3[1]*d3)*A;
-               bs->dxco[2]= (v1[2]*d1 + v2[2]*d2 + v3[2]*d3)*A;
-
-               d1= uv3[0] - uv2[0];
-               d2= uv1[0] - uv3[0];
-               d3= uv2[0] - uv1[0];
-               bs->dyco[0]= (v1[0]*d1 + v2[0]*d2 + v3[0]*d3)*A;
-               bs->dyco[1]= (v1[1]*d1 + v2[1]*d2 + v3[1]*d3)*A;
-               bs->dyco[2]= (v1[2]*d1 + v2[2]*d2 + v3[2]*d3)*A;
+               A = 0.5f / A;
+
+               d1 = uv2[1] - uv3[1];
+               d2 = uv3[1] - uv1[1];
+               d3 = uv1[1] - uv2[1];
+               bs->dxco[0] = (v1[0] * d1 + v2[0] * d2 + v3[0] * d3) * A;
+               bs->dxco[1] = (v1[1] * d1 + v2[1] * d2 + v3[1] * d3) * A;
+               bs->dxco[2] = (v1[2] * d1 + v2[2] * d2 + v3[2] * d3) * A;
+
+               d1 = uv3[0] - uv2[0];
+               d2 = uv1[0] - uv3[0];
+               d3 = uv2[0] - uv1[0];
+               bs->dyco[0] = (v1[0] * d1 + v2[0] * d2 + v3[0] * d3) * A;
+               bs->dyco[1] = (v1[1] * d1 + v2[1] * d2 + v3[1] * d3) * A;
+               bs->dyco[2] = (v1[2] * d1 + v2[2] * d2 + v3[2] * d3) * A;
        }
        else {
-               bs->dxco[0]= bs->dxco[1]= bs->dxco[2]= 0.0f;
-               bs->dyco[0]= bs->dyco[1]= bs->dyco[2]= 0.0f;
+               bs->dxco[0] = bs->dxco[1] = bs->dxco[2] = 0.0f;
+               bs->dyco[0] = bs->dyco[1] = bs->dyco[2] = 0.0f;
        }
 
        if (bs->obi->flag & R_TRANSFORMED) {
@@ -465,32 +452,32 @@ static void bake_set_vlr_dxyco(BakeShade *bs, float *uv1, float *uv2, float *uv3
 
 static void do_bake_shade(void *handle, int x, int y, float u, float v)
 {
-       BakeShade *bs= handle;
-       VlakRen *vlr= bs->vlr;
-       ObjectInstanceRen *obi= bs->obi;
-       Object *ob= obi->obr->ob;
+       BakeShade *bs = handle;
+       VlakRen *vlr = bs->vlr;
+       ObjectInstanceRen *obi = bs->obi;
+       Object *ob = obi->obr->ob;
        float l, *v1, *v2, *v3, tvn[3], ttang[4];
        int quad;
-       ShadeSample *ssamp= &bs->ssamp;
-       ShadeInput *shi= ssamp->shi;
-       
+       ShadeSample *ssamp = &bs->ssamp;
+       ShadeInput *shi = ssamp->shi;
+
        /* fast threadsafe break test */
        if (R.test_break(R.tbh))
                return;
-       
+
        /* setup render coordinates */
        if (bs->quad) {
-               v1= vlr->v1->co;
-               v2= vlr->v3->co;
-               v3= vlr->v4->co;
+               v1 = vlr->v1->co;
+               v2 = vlr->v3->co;
+               v3 = vlr->v4->co;
        }
        else {
-               v1= vlr->v1->co;
-               v2= vlr->v2->co;
-               v3= vlr->v3->co;
+               v1 = vlr->v1->co;
+               v2 = vlr->v2->co;
+               v3 = vlr->v3->co;
        }
 
-       l= 1.0f-u-v;
+       l = 1.0f - u - v;
 
        /* shrink barycentric coordinates inwards slightly to avoid some issues
         * where baking selected to active might just miss the other face at the
@@ -499,22 +486,22 @@ static void do_bake_shade(void *handle, int x, int y, float u, float v)
                const float eps = 1.0f - 1e-4f;
                float invsum;
 
-               u = (u - 0.5f)*eps + 0.5f;
-               v = (v - 0.5f)*eps + 0.5f;
-               l = (l - 0.5f)*eps + 0.5f;
+               u = (u - 0.5f) * eps + 0.5f;
+               v = (v - 0.5f) * eps + 0.5f;
+               l = (l - 0.5f) * eps + 0.5f;
 
-               invsum = 1.0f/(u + v + l);
+               invsum = 1.0f / (u + v + l);
 
                u *= invsum;
                v *= invsum;
                l *= invsum;
        }
-       
+
        /* renderco */
-       shi->co[0]= l*v3[0]+u*v1[0]+v*v2[0];
-       shi->co[1]= l*v3[1]+u*v1[1]+v*v2[1];
-       shi->co[2]= l*v3[2]+u*v1[2]+v*v2[2];
-       
+       shi->co[0] = l * v3[0] + u * v1[0] + v * v2[0];
+       shi->co[1] = l * v3[1] + u * v1[1] + v * v2[1];
+       shi->co[2] = l * v3[2] + u * v1[2] + v * v2[2];
+
        /* avoid self shadow with vertex bake from adjacent faces [#33729] */
        if ((bs->vcol != NULL) && (bs->actob == NULL)) {
                madd_v3_v3fl(shi->co, vlr->n, 0.0001f);
@@ -522,14 +509,14 @@ static void do_bake_shade(void *handle, int x, int y, float u, float v)
 
        if (obi->flag & R_TRANSFORMED)
                mul_m4_v3(obi->mat, shi->co);
-       
+
        copy_v3_v3(shi->dxco, bs->dxco);
        copy_v3_v3(shi->dyco, bs->dyco);
 
-       quad= bs->quad;
+       quad = bs->quad;
        bake_set_shade_input(obi, vlr, shi, quad, 0, x, y, u, v);
 
-       if (bs->type==RE_BAKE_NORMALS && R.r.bake_normal_space==R_BAKE_SPACE_TANGENT) {
+       if (bs->type == RE_BAKE_NORMALS && R.r.bake_normal_space == R_BAKE_SPACE_TANGENT) {
                shade_input_set_shade_texco(shi);
                copy_v3_v3(tvn, shi->nmapnorm);
                copy_v4_v4(ttang, shi->nmaptang);
@@ -538,40 +525,40 @@ static void do_bake_shade(void *handle, int x, int y, float u, float v)
        /* if we are doing selected to active baking, find point on other face */
        if (bs->actob) {
                Isect isec, minisec;
-               float co[3], minco[3], dist, mindist=0.0f;
-               int hit, sign, dir=1;
-               
+               float co[3], minco[3], dist, mindist = 0.0f;
+               int hit, sign, dir = 1;
+
                /* intersect with ray going forward and backward*/
-               hit= 0;
+               hit = 0;
                memset(&minisec, 0, sizeof(minisec));
-               minco[0]= minco[1]= minco[2]= 0.0f;
-               
+               minco[0] = minco[1] = minco[2] = 0.0f;
+
                copy_v3_v3(bs->dir, shi->vn);
-               
-               for (sign=-1; sign<=1; sign+=2) {
+
+               for (sign = -1; sign <= 1; sign += 2) {
                        memset(&isec, 0, sizeof(isec));
-                       isec.mode= RE_RAY_MIRROR;
+                       isec.mode = RE_RAY_MIRROR;
 
                        isec.orig.ob   = obi;
                        isec.orig.face = vlr;
-                       isec.userdata= bs->actob;
+                       isec.userdata = bs->actob;
                        isec.check = RE_CHECK_VLR_BAKE;
                        isec.skip = RE_SKIP_VLR_NEIGHBOUR;
-                       
+
                        if (bake_intersect_tree(R.raytree, &isec, shi->co, shi->vn, sign, co, &dist)) {
                                if (!hit || len_squared_v3v3(shi->co, co) < len_squared_v3v3(shi->co, minco)) {
-                                       minisec= isec;
-                                       mindist= dist;
+                                       minisec = isec;
+                                       mindist = dist;
                                        copy_v3_v3(minco, co);
-                                       hit= 1;
+                                       hit = 1;
                                        dir = sign;
                                }
                        }
                }
 
-               if (bs->type==RE_BAKE_DISPLACEMENT) {
+               if (bs->type == RE_BAKE_DISPLACEMENT) {
                        if (hit)
-                               bake_displacement(handle, shi, (dir==-1)? mindist:-mindist, x, y);
+                               bake_displacement(handle, shi, (dir == -1) ? mindist : -mindist, x, y);
                        else
                                bake_displacement(handle, shi, 0.0f, x, y);
                        return;
@@ -581,16 +568,16 @@ static void do_bake_shade(void *handle, int x, int y, float u, float v)
                if (hit) {
                        obi = (ObjectInstanceRen *)minisec.hit.ob;
                        vlr = (VlakRen *)minisec.hit.face;
-                       quad= (minisec.isect == 2);
+                       quad = (minisec.isect == 2);
                        copy_v3_v3(shi->co, minco);
-                       
-                       u= -minisec.u;
-                       v= -minisec.v;
+
+                       u = -minisec.u;
+                       v = -minisec.v;
                        bake_set_shade_input(obi, vlr, shi, quad, 1, x, y, u, v);
                }
        }
 
-       if (bs->type==RE_BAKE_NORMALS && R.r.bake_normal_space==R_BAKE_SPACE_TANGENT)
+       if (bs->type == RE_BAKE_NORMALS && R.r.bake_normal_space == R_BAKE_SPACE_TANGENT)
                bake_shade(handle, ob, shi, quad, x, y, u, v, tvn, ttang);
        else
                bake_shade(handle, ob, shi, quad, x, y, u, v, 0, 0);
@@ -601,37 +588,37 @@ static int get_next_bake_face(BakeShade *bs)
        ObjectRen *obr;
        VlakRen *vlr;
        MTFace *tface;
-       static int v= 0, vdone = FALSE;
-       static ObjectInstanceRen *obi= NULL;
-       
-       if (bs==NULL) {
-               vlr= NULL;
-               v= vdone = FALSE;
-               obi= R.instancetable.first;
+       static int v = 0, vdone = false;
+       static ObjectInstanceRen *obi = NULL;
+
+       if (bs == NULL) {
+               vlr = NULL;
+               v = vdone = false;
+               obi = R.instancetable.first;
                return 0;
        }
        
        BLI_lock_thread(LOCK_CUSTOM1);
 
-       for (; obi; obi=obi->next, v=0) {
-               obr= obi->obr;
+       for (; obi; obi = obi->next, v = 0) {
+               obr = obi->obr;
 
-               for (; v<obr->totvlak; v++) {
-                       vlr= RE_findOrAddVlak(obr, v);
+               for (; v < obr->totvlak; v++) {
+                       vlr = RE_findOrAddVlak(obr, v);
 
                        if ((bs->actob && bs->actob == obr->ob) || (!bs->actob && (obr->ob->flag & SELECT))) {
-                               if(R.r.bake_flag & R_BAKE_VCOL) {
+                               if (R.r.bake_flag & R_BAKE_VCOL) {
                                        /* Gather face data for vertex color bake */
                                        Mesh *me;
                                        int *origindex, vcollayer;
                                        CustomDataLayer *cdl;
 
-                                       if(obr->ob->type != OB_MESH)
+                                       if (obr->ob->type != OB_MESH)
                                                continue;
                                        me = obr->ob->data;
 
                                        origindex = RE_vlakren_get_origindex(obr, vlr, 0);
-                                       if(origindex == NULL)
+                                       if (origindex == NULL)
                                                continue;
                                        if (*origindex >= me->totpoly) {
                                                /* Small hack for Array modifier, which gives false
@@ -640,17 +627,17 @@ static int get_next_bake_face(BakeShade *bs)
                                        }
 #if 0
                                        /* Only shade selected faces. */
-                                       if((me->mface[*origindex].flag & ME_FACE_SEL) == 0)
+                                       if ((me->mface[*origindex].flag & ME_FACE_SEL) == 0)
                                                continue;
 #endif
 
                                        vcollayer = CustomData_get_render_layer_index(&me->ldata, CD_MLOOPCOL);
-                                       if(vcollayer == -1)
+                                       if (vcollayer == -1)
                                                continue;
 
                                        cdl = &me->ldata.layers[vcollayer];
                                        bs->mpoly = me->mpoly + *origindex;
-                                       bs->vcol = ((MLoopCol*)cdl->data) + bs->mpoly->loopstart;
+                                       bs->vcol = ((MLoopCol *)cdl->data) + bs->mpoly->loopstart;
                                        bs->mloop = me->mloop + bs->mpoly->loopstart;
 
                                        /* Tag mesh for reevaluation. */
@@ -659,26 +646,26 @@ static int get_next_bake_face(BakeShade *bs)
                                else {
                                        Image *ima = NULL;
                                        ImBuf *ibuf = NULL;
-                                       const float vec_alpha[4]= {0.0f, 0.0f, 0.0f, 0.0f};
-                                       const float vec_solid[4]= {0.0f, 0.0f, 0.0f, 1.0f};
+                                       const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+                                       const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
 
-                                       tface= RE_vlakren_get_tface(obr, vlr, obr->bakemtface, NULL, 0);
+                                       tface = RE_vlakren_get_tface(obr, vlr, obr->bakemtface, NULL, 0);
 
                                        if (!tface || !tface->tpage)
                                                continue;
 
                                        ima = tface->tpage;
                                        ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
-                                       
-                                       if (ibuf==NULL)
+
+                                       if (ibuf == NULL)
                                                continue;
-                                       
-                                       if (ibuf->rect==NULL && ibuf->rect_float==NULL) {
+
+                                       if (ibuf->rect == NULL && ibuf->rect_float == NULL) {
                                                BKE_image_release_ibuf(ima, ibuf, NULL);
                                                continue;
                                        }
-                                       
-                                       if (ibuf->rect_float && !(ibuf->channels==0 || ibuf->channels==4)) {
+
+                                       if (ibuf->rect_float && !(ibuf->channels == 0 || ibuf->channels == 4)) {
                                                BKE_image_release_ibuf(ima, ibuf, NULL);
                                                continue;
                                        }
@@ -701,7 +688,7 @@ static int get_next_bake_face(BakeShade *bs)
                                                        IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? vec_alpha : vec_solid);
 
                                                /* might be read by UI to set active image for display */
-                                               R.bakebuf= ima;
+                                               R.bakebuf = ima;
                                        }
 
                                        /* Tag image for redraw. */
@@ -711,7 +698,7 @@ static int get_next_bake_face(BakeShade *bs)
 
                                bs->obi = obi;
                                bs->vlr = vlr;
-                               bs->vdone++;    /* only for error message if nothing was rendered */
+                               bs->vdone++;  /* only for error message if nothing was rendered */
                                v++;
                                BLI_unlock_thread(LOCK_CUSTOM1);
                                return 1;
@@ -796,32 +783,32 @@ static void shade_verts(BakeShade *bs)
 /* already have tested for tface and ima and zspan */
 static void shade_tface(BakeShade *bs)
 {
-       VlakRen *vlr= bs->vlr;
-       ObjectInstanceRen *obi= bs->obi;
-       ObjectRen *obr= obi->obr;
-       MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->bakemtface, NULL, 0);
-       Image *ima= tface->tpage;
+       VlakRen *vlr = bs->vlr;
+       ObjectInstanceRen *obi = bs->obi;
+       ObjectRen *obr = obi->obr;
+       MTFace *tface = RE_vlakren_get_tface(obr, vlr, obr->bakemtface, NULL, 0);
+       Image *ima = tface->tpage;
        float vec[4][2];
        int a, i1, i2, i3;
        
        /* check valid zspan */
-       if (ima!=bs->ima) {
+       if (ima != bs->ima) {
                BKE_image_release_ibuf(bs->ima, bs->ibuf, NULL);
 
-               bs->ima= ima;
-               bs->ibuf= BKE_image_acquire_ibuf(ima, NULL, NULL);
+               bs->ima = ima;
+               bs->ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
                /* note, these calls only free/fill contents of zspan struct, not zspan itself */
                zbuf_free_span(bs->zspan);
                zbuf_alloc_span(bs->zspan, bs->ibuf->x, bs->ibuf->y, R.clipcrop);
        }
-       
-       bs->rectx= bs->ibuf->x;
-       bs->recty= bs->ibuf->y;
-       bs->rect= bs->ibuf->rect;
-       bs->rect_colorspace= bs->ibuf->rect_colorspace;
-       bs->rect_float= bs->ibuf->rect_float;
+
+       bs->rectx = bs->ibuf->x;
+       bs->recty = bs->ibuf->y;
+       bs->rect = bs->ibuf->rect;
+       bs->rect_colorspace = bs->ibuf->rect_colorspace;
+       bs->rect_float = bs->ibuf->rect_float;
        bs->vcol = NULL;
-       bs->quad= 0;
+       bs->quad = 0;
        bs->rect_mask = NULL;
        bs->displacement_buffer = NULL;
 
@@ -849,23 +836,23 @@ static void shade_tface(BakeShade *bs)
        }
        
        /* get pixel level vertex coordinates */
-       for (a=0; a<4; a++) {
+       for (a = 0; a < 4; a++) {
                /* Note, workaround for pixel aligned UVs which are common and can screw up our intersection tests
                 * where a pixel gets in between 2 faces or the middle of a quad,
                 * camera aligned quads also have this problem but they are less common.
                 * Add a small offset to the UVs, fixes bug #18685 - Campbell */
-               vec[a][0]= tface->uv[a][0]*(float)bs->rectx - (0.5f + 0.001f);
-               vec[a][1]= tface->uv[a][1]*(float)bs->recty - (0.5f + 0.002f);
+               vec[a][0] = tface->uv[a][0] * (float)bs->rectx - (0.5f + 0.001f);
+               vec[a][1] = tface->uv[a][1] * (float)bs->recty - (0.5f + 0.002f);
        }
-       
+
        /* UV indices have to be corrected for possible quad->tria splits */
-       i1= 0; i2= 1; i3= 2;
+       i1 = 0; i2 = 1; i3 = 2;
        vlr_set_uv_indices(vlr, &i1, &i2, &i3);
        bake_set_vlr_dxyco(bs, vec[i1], vec[i2], vec[i3]);
        zspan_scanconvert(bs->zspan, bs, vec[i1], vec[i2], vec[i3], do_bake_shade);
        
        if (vlr->v4) {
-               bs->quad= 1;
+               bs->quad = 1;
                bake_set_vlr_dxyco(bs, vec[0], vec[2], vec[3]);
                zspan_scanconvert(bs->zspan, bs, vec[0], vec[2], vec[3], do_bake_shade);
        }
@@ -873,13 +860,15 @@ static void shade_tface(BakeShade *bs)
 
 static void *do_bake_thread(void *bs_v)
 {
-       BakeShade *bs= bs_v;
-       
+       BakeShade *bs = bs_v;
+
        while (get_next_bake_face(bs)) {
-               if (R.r.bake_flag & R_BAKE_VCOL)
+               if (R.r.bake_flag & R_BAKE_VCOL) {
                        shade_verts(bs);
-               else
+               }
+               else {
                        shade_tface(bs);
+               }
                
                /* fast threadsafe break test */
                if (R.test_break(R.tbh))
@@ -887,10 +876,11 @@ static void *do_bake_thread(void *bs_v)
 
                /* access is not threadsafe but since its just true/false probably ok
                 * only used for interactive baking */
-               if (bs->do_update)
-                       *bs->do_update= TRUE;
+               if (bs->do_update) {
+                       *bs->do_update = true;
+               }
        }
-       bs->ready= 1;
+       bs->ready = true;
 
        BKE_image_release_ibuf(bs->ima, bs->ibuf, NULL);
 
@@ -900,7 +890,7 @@ static void *do_bake_thread(void *bs_v)
 void RE_bake_ibuf_filter(ImBuf *ibuf, char *mask, const int filter)
 {
        /* must check before filtering */
-       const short is_new_alpha= (ibuf->planes != R_IMF_PLANES_RGBA) && BKE_imbuf_alpha_test(ibuf);
+       const short is_new_alpha = (ibuf->planes != R_IMF_PLANES_RGBA) && BKE_imbuf_alpha_test(ibuf);
 
        /* Margin */
        if (filter) {
@@ -909,7 +899,7 @@ void RE_bake_ibuf_filter(ImBuf *ibuf, char *mask, const int filter)
 
        /* if the bake results in new alpha then change the image setting */
        if (is_new_alpha) {
-               ibuf->planes= R_IMF_PLANES_RGBA;
+               ibuf->planes = R_IMF_PLANES_RGBA;
        }
        else {
                if (filter && ibuf->planes != R_IMF_PLANES_RGBA) {
@@ -964,27 +954,28 @@ int RE_bake_shade_all_selected(Render *re, int type, Object *actob, short *do_up
        BakeShade *handles;
        ListBase threads;
        Image *ima;
-       int a, vdone = FALSE, use_mask = FALSE, result = BAKE_RESULT_OK;
-       int use_displacement_buffer = FALSE;
+       int a, vdone = false, result = BAKE_RESULT_OK;
+       bool use_mask = false;
+       bool use_displacement_buffer = false;
        
        re->scene_color_manage = BKE_scene_check_color_management_enabled(re->scene);
        
        /* initialize render global */
-       R= *re;
-       R.bakebuf= NULL;
-       
+       R = *re;
+       R.bakebuf = NULL;
+
        /* initialize static vars */
        get_next_bake_face(NULL);
        
        /* do we need a mask? */
        if (re->r.bake_filter)
-               use_mask = TRUE;
+               use_mask = true;
 
        /* do we need buffer to store displacements  */
        if (type == RE_BAKE_DISPLACEMENT) {
                if ((R.r.bake_flag & R_BAKE_NORMALIZE) && R.r.bake_maxdist == 0.0f) {
-                       use_displacement_buffer = TRUE;
-                       use_mask = TRUE;
+                       use_displacement_buffer = true;
+                       use_mask = true;
                }
        }
 
@@ -1000,28 +991,28 @@ int RE_bake_shade_all_selected(Render *re, int type, Object *actob, short *do_up
                        BKE_image_release_ibuf(ima, ibuf, NULL);
                }
        }
-       
+
        BLI_init_threads(&threads, do_bake_thread, re->r.threads);
 
-       handles= MEM_callocN(sizeof(BakeShade)*re->r.threads, "BakeShade");
+       handles = MEM_callocN(sizeof(BakeShade) * re->r.threads, "BakeShade");
 
        /* get the threads running */
-       for (a=0; a<re->r.threads; a++) {
+       for (a = 0; a < re->r.threads; a++) {
                /* set defaults in handles */
-               handles[a].ssamp.shi[0].lay= re->lay;
-               
-               if (type==RE_BAKE_SHADOW) {
-                       handles[a].ssamp.shi[0].passflag= SCE_PASS_SHADOW;
+               handles[a].ssamp.shi[0].lay = re->lay;
+
+               if (type == RE_BAKE_SHADOW) {
+                       handles[a].ssamp.shi[0].passflag = SCE_PASS_SHADOW;
                }
                else {
-                       handles[a].ssamp.shi[0].passflag= SCE_PASS_COMBINED;
+                       handles[a].ssamp.shi[0].passflag = SCE_PASS_COMBINED;
                }
-               handles[a].ssamp.shi[0].combinedflag= ~(SCE_PASS_SPEC);
-               handles[a].ssamp.shi[0].thread= a;
-               handles[a].ssamp.tot= 1;
-               
-               handles[a].type= type;
-               handles[a].actob= actob;
+               handles[a].ssamp.shi[0].combinedflag = ~(SCE_PASS_SPEC);
+               handles[a].ssamp.shi[0].thread = a;
+               handles[a].ssamp.tot = 1;
+
+               handles[a].type = type;
+               handles[a].actob = actob;
                if (R.r.bake_flag & R_BAKE_VCOL)
                        handles[a].zspan = NULL;
                else
@@ -1039,19 +1030,20 @@ int RE_bake_shade_all_selected(Render *re, int type, Object *actob, short *do_up
        }
        
        /* wait for everything to be done */
-       a= 0;
-       while (a!=re->r.threads) {
+       a = 0;
+       while (a != re->r.threads) {
                PIL_sleep_ms(50);
 
                /* calculate progress */
-               for (vdone = FALSE, a=0; a<re->r.threads; a++)
-                       vdone+= handles[a].vdone;
+               for (vdone = false, a = 0; a < re->r.threads; a++)
+                       vdone += handles[a].vdone;
                if (progress)
                        *progress = (float)(vdone / (float)re->totvlak);
-               
-               for (a=0; a<re->r.threads; a++) {
-                       if (handles[a].ready==0)
+
+               for (a = 0; a < re->r.threads; a++) {
+                       if (handles[a].ready == false) {
                                break;
+                       }
                }
        }
 
@@ -1067,7 +1059,7 @@ int RE_bake_shade_all_selected(Render *re, int type, Object *actob, short *do_up
                }
 
                for (ima = G.main->image.first; ima; ima = ima->id.next) {
-                       if ((ima->id.flag & LIB_DOIT)==0) {
+                       if ((ima->id.flag & LIB_DOIT) == 0) {
                                ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
                                BakeImBufuserData *userdata;
 
@@ -1101,8 +1093,9 @@ int RE_bake_shade_all_selected(Render *re, int type, Object *actob, short *do_up
        
        BLI_end_threads(&threads);
 
-       if (vdone==0)
-               result= BAKE_RESULT_NO_OBJECTS;
+       if (vdone == 0) {
+               result = BAKE_RESULT_NO_OBJECTS;
+       }
 
        return result;
 }