code cleanup: function naming, use BKE_*type* prefix.
[blender.git] / source / blender / render / intern / source / convertblender.c
index 30dc401da27d3b89fea792ac59c01bfcf8ee6686..5df78a3f5da0d9738b90f7a86551d6104f83cd15 100644 (file)
 /* ------------------------------------------------------------------------- */
 
 /* Stuff for stars. This sits here because it uses gl-things. Part of
-this code may move down to the converter.  */
+ * this code may move down to the converter.  */
 /* ------------------------------------------------------------------------- */
 /* this is a bad beast, since it is misused by the 3d view drawing as well. */
 
@@ -149,10 +149,10 @@ static HaloRen *initstar(Render *re, ObjectRen *obr, float *vec, float hasize)
 }
 
 /* there must be a 'fixed' amount of stars generated between
-*         near and far
-* all stars must by preference lie on the far and solely
-*        differ in clarity/color
-*/
+ *         near and far
+ * all stars must by preference lie on the far and solely
+ *        differ in clarity/color
+ */
 
 void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
                                   void (*vertexfunc)(float*),  void (*termfunc)(void))
@@ -171,7 +171,7 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
        int x, y, z, sx, sy, sz, ex, ey, ez, done = 0;
        unsigned int totstar= 0;
        
-       if(initfunc) {
+       if (initfunc) {
                scene= scenev3d;
                wrld= scene->world;
        }
@@ -195,18 +195,18 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
        if (re) re->flag |= R_HALO;
        else stargrid *= 1.0f;                          /* then it draws fewer */
        
-       if(re) invert_m4_m4(mat, re->viewmat);
+       if (re) invert_m4_m4(mat, re->viewmat);
        else unit_m4(mat);
        
        /* BOUNDING BOX CALCULATION
-               * bbox goes from z = loc_near_var | loc_far_var,
-               * x = -z | +z,
-               * y = -z | +z
-               */
+        * bbox goes from z = loc_near_var | loc_far_var,
+        * x = -z | +z,
+        * y = -z | +z
+        */
 
        camera= re ? RE_GetCamera(re) : scene->camera;
 
-       if(camera==NULL || camera->type != OB_CAMERA)
+       if (camera==NULL || camera->type != OB_CAMERA)
                return;
 
        cam = camera->data;
@@ -229,7 +229,7 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
                initfunc();     
        }
 
-       if(re) /* add render object for stars */
+       if (re) /* add render object for stars */
                obr= RE_addRenderObject(re, NULL, NULL, 0, 0, 0);
        
        for (x = sx, fx = sx * stargrid; x <= ex; x++, fx += stargrid) {
@@ -243,16 +243,16 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
                                vec[3] = 1.0;
                                
                                if (vertexfunc) {
-                                       if(done & 1) vertexfunc(vec);
+                                       if (done & 1) vertexfunc(vec);
                                        done++;
                                }
                                else {
                                        mul_m4_v3(re->viewmat, vec);
                                        
                                        /* in vec are global coordinates
-                                       * calculate distance to camera
-                                       * and using that, define the alpha
-                                       */
+                                        * calculate distance to camera
+                                        * and using that, define the alpha
+                                        */
                                        
                                        {
                                                float tx, ty, tz;
@@ -267,7 +267,8 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
                                                else if (alpha <= starmindist) alpha = 0.0;
                                                else if (alpha <= 2.0f * starmindist) {
                                                        alpha = (alpha - starmindist) / starmindist;
-                                               } else {
+                                               }
+                                               else {
                                                        alpha -= 2.0f * starmindist;
                                                        alpha /= (clipend - 2.0f * starmindist);
                                                        alpha = 1.0f - alpha;
@@ -298,8 +299,8 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
                                }
 
                                /* break out of the loop if generating stars takes too long */
-                               if(re && !(totstar % 1000000)) {
-                                       if(re->test_break(re->tbh)) {
+                               if (re && !(totstar % 1000000)) {
+                                       if (re->test_break(re->tbh)) {
                                                x= ex + 1;
                                                y= ey + 1;
                                                z= ez + 1;
@@ -314,14 +315,14 @@ void RE_make_stars(Render *re, Scene *scenev3d, void (*initfunc)(void),
        }
        if (termfunc) termfunc();
 
-       if(obr)
+       if (obr)
                re->tothalo += obr->tothalo;
 }
 
 
 /* ------------------------------------------------------------------------- */
 /* tool functions/defines for ad hoc simplification and possible future 
  cleanup      */
* cleanup      */
 /* ------------------------------------------------------------------------- */
 
 #define UVTOINDEX(u,v) (startvlak + (u) * sizev + (v))
@@ -354,11 +355,13 @@ static void split_v_renderfaces(ObjectRen *obr, int startvlak, int UNUSED(startv
                        if (v==vLen-1) {
                                VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + 0);
                                vlr->v1 = vert;
-                       } else {
+                       }
+                       else {
                                VlakRen *vlr = RE_findOrAddVlak(obr, startvlak + vLen*uIndex + v+1);
                                vlr->v1 = vert;
                        }
-               } else {
+               }
+               else {
                        vlr->v2 = vert;
 
                        if (v<vLen-1) {
@@ -396,16 +399,16 @@ static void calc_edge_stress(Render *UNUSED(re), ObjectRen *obr, Mesh *me)
        float loc[3], size[3], *accum, *acc, *accumoffs, *stress;
        int a;
        
-       if(obr->totvert==0) return;
+       if (obr->totvert==0) return;
        
        mesh_get_texspace(me, loc, NULL, size);
        
        accum= MEM_callocN(2*sizeof(float)*obr->totvert, "temp accum for stress");
        
        /* de-normalize orco */
-       for(a=0; a<obr->totvert; a++) {
+       for (a=0; a<obr->totvert; a++) {
                VertRen *ver= RE_findOrAddVert(obr, a);
-               if(ver->orco) {
+               if (ver->orco) {
                        ver->orco[0]= ver->orco[0]*size[0] +loc[0];
                        ver->orco[1]= ver->orco[1]*size[1] +loc[1];
                        ver->orco[2]= ver->orco[2]*size[2] +loc[2];
@@ -414,14 +417,14 @@ static void calc_edge_stress(Render *UNUSED(re), ObjectRen *obr, Mesh *me)
        
        /* add stress values */
        accumoffs= accum;       /* so we can use vertex index */
-       for(a=0; a<obr->totvlak; a++) {
+       for (a=0; a<obr->totvlak; a++) {
                VlakRen *vlr= RE_findOrAddVlak(obr, a);
 
-               if(vlr->v1->orco && vlr->v4) {
+               if (vlr->v1->orco && vlr->v4) {
                        calc_edge_stress_add(accumoffs, vlr->v1, vlr->v2);
                        calc_edge_stress_add(accumoffs, vlr->v2, vlr->v3);
                        calc_edge_stress_add(accumoffs, vlr->v3, vlr->v1);
-                       if(vlr->v4) {
+                       if (vlr->v4) {
                                calc_edge_stress_add(accumoffs, vlr->v3, vlr->v4);
                                calc_edge_stress_add(accumoffs, vlr->v4, vlr->v1);
                                calc_edge_stress_add(accumoffs, vlr->v2, vlr->v4);
@@ -429,12 +432,12 @@ static void calc_edge_stress(Render *UNUSED(re), ObjectRen *obr, Mesh *me)
                }
        }
        
-       for(a=0; a<obr->totvert; a++) {
+       for (a=0; a<obr->totvert; a++) {
                VertRen *ver= RE_findOrAddVert(obr, a);
-               if(ver->orco) {
+               if (ver->orco) {
                        /* find stress value */
                        acc= accumoffs + 2*ver->index;
-                       if(acc[1]!=0.0f)
+                       if (acc[1]!=0.0f)
                                acc[0]/= acc[1];
                        stress= RE_vertren_get_stress(obr, ver, 1);
                        *stress= *acc;
@@ -458,25 +461,25 @@ static void calc_tangent_vector(ObjectRen *obr, VertexTangent **vtangents, MemAr
        float *uv1, *uv2, *uv3, *uv4;
        float uv[4][2];
        
-       if(tface) {
+       if (tface) {
                uv1= tface->uv[0];
                uv2= tface->uv[1];
                uv3= tface->uv[2];
                uv4= tface->uv[3];
        }
-       else if(v1->orco) {
+       else if (v1->orco) {
                uv1= uv[0]; uv2= uv[1]; uv3= uv[2]; uv4= uv[3];
-               map_to_sphere( &uv[0][0], &uv[0][1],v1->orco[0], v1->orco[1], v1->orco[2]);
-               map_to_sphere( &uv[1][0], &uv[1][1],v2->orco[0], v2->orco[1], v2->orco[2]);
-               map_to_sphere( &uv[2][0], &uv[2][1],v3->orco[0], v3->orco[1], v3->orco[2]);
-               if(v4)
-                       map_to_sphere( &uv[3][0], &uv[3][1],v4->orco[0], v4->orco[1], v4->orco[2]);
+               map_to_sphere(&uv[0][0], &uv[0][1], v1->orco[0], v1->orco[1], v1->orco[2]);
+               map_to_sphere(&uv[1][0], &uv[1][1], v2->orco[0], v2->orco[1], v2->orco[2]);
+               map_to_sphere(&uv[2][0], &uv[2][1], v3->orco[0], v3->orco[1], v3->orco[2]);
+               if (v4)
+                       map_to_sphere(&uv[3][0], &uv[3][1], v4->orco[0], v4->orco[1], v4->orco[2]);
        }
        else return;
 
        tangent_from_uv(uv1, uv2, uv3, v1->co, v2->co, v3->co, vlr->n, tang);
        
-       if(do_tangent) {
+       if (do_tangent) {
                tav= RE_vertren_get_tangent(obr, v1, 1);
                add_v3_v3(tav, tang);
                tav= RE_vertren_get_tangent(obr, v2, 1);
@@ -485,16 +488,16 @@ static void calc_tangent_vector(ObjectRen *obr, VertexTangent **vtangents, MemAr
                add_v3_v3(tav, tang);
        }
        
-       if(do_nmap_tangent) {
+       if (do_nmap_tangent) {
                sum_or_add_vertex_tangent(arena, &vtangents[v1->index], tang, uv1);
                sum_or_add_vertex_tangent(arena, &vtangents[v2->index], tang, uv2);
                sum_or_add_vertex_tangent(arena, &vtangents[v3->index], tang, uv3);
        }
 
-       if(v4) {
+       if (v4) {
                tangent_from_uv(uv1, uv3, uv4, v1->co, v3->co, v4->co, vlr->n, tang);
                
-               if(do_tangent) {
+               if (do_tangent) {
                        tav= RE_vertren_get_tangent(obr, v1, 1);
                        add_v3_v3(tav, tang);
                        tav= RE_vertren_get_tangent(obr, v3, 1);
@@ -503,7 +506,7 @@ static void calc_tangent_vector(ObjectRen *obr, VertexTangent **vtangents, MemAr
                        add_v3_v3(tav, tang);
                }
 
-               if(do_nmap_tangent) {
+               if (do_nmap_tangent) {
                        sum_or_add_vertex_tangent(arena, &vtangents[v1->index], tang, uv1);
                        sum_or_add_vertex_tangent(arena, &vtangents[v3->index], tang, uv3);
                        sum_or_add_vertex_tangent(arena, &vtangents[v4->index], tang, uv4);
@@ -514,8 +517,8 @@ static void calc_tangent_vector(ObjectRen *obr, VertexTangent **vtangents, MemAr
 
 
 /****************************************************************
-************ tangent space generation interface *****************
-****************************************************************/
+ ************ tangent space generation interface ****************
+ ****************************************************************/
 
 typedef struct
 {
@@ -556,11 +559,11 @@ static void GetTextureCoordinate(const SMikkTSpaceContext * pContext, float fUV[
        MTFace *tface= RE_vlakren_get_tface(pMesh->obr, vlr, pMesh->obr->actmtface, NULL, 0);
        const float *coord;
        
-       if(tface != NULL)       {
+       if (tface != NULL)      {
                coord= tface->uv[vert_index];
                fUV[0]= coord[0]; fUV[1]= coord[1];
        }
-       else if((coord= (&vlr->v1)[vert_index]->orco)) {
+       else if ((coord= (&vlr->v1)[vert_index]->orco)) {
                map_to_sphere(&fUV[0], &fUV[1], coord[0], coord[1], coord[2]);
        }
        else { /* else we get un-initialized value, 0.0 ok default? */
@@ -582,7 +585,7 @@ static void SetTSpace(const SMikkTSpaceContext * pContext, const float fvTangent
        SRenderMeshToTangent * pMesh = (SRenderMeshToTangent *) pContext->m_pUserData;
        VlakRen *vlr= RE_findOrAddVlak(pMesh->obr, face_num);
        float * ftang= RE_vlakren_get_nmap_tangent(pMesh->obr, vlr, 1);
-       if(ftang!=NULL) {
+       if (ftang!=NULL)        {
                copy_v3_v3(&ftang[iVert*4+0], fvTangent);
                ftang[iVert*4+3]=fSign;
        }
@@ -594,7 +597,7 @@ static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangen
        VertexTangent **vtangents= NULL;
        int a;
 
-       if(do_nmap_tangent) {
+       if (do_nmap_tangent) {
                arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "nmap tangent arena");
                BLI_memarena_use_calloc(arena);
 
@@ -602,23 +605,23 @@ static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangen
        }
 
                /* clear all vertex normals */
-       for(a=0; a<obr->totvert; a++) {
+       for (a=0; a<obr->totvert; a++) {
                VertRen *ver= RE_findOrAddVert(obr, a);
                ver->n[0]=ver->n[1]=ver->n[2]= 0.0f;
        }
 
                /* calculate cos of angles and point-masses, use as weight factor to
-                  add face normal to vertex */
-       for(a=0; a<obr->totvlak; a++) {
+                * add face normal to vertex */
+       for (a=0; a<obr->totvlak; a++) {
                VlakRen *vlr= RE_findOrAddVlak(obr, a);
-               if(vlr->flag & ME_SMOOTH) {
+               if (vlr->flag & ME_SMOOTH) {
                        float *n4= (vlr->v4)? vlr->v4->n: NULL;
                        float *c4= (vlr->v4)? vlr->v4->co: NULL;
 
                        accumulate_vertex_normals(vlr->v1->n, vlr->v2->n, vlr->v3->n, n4,
                                vlr->n, vlr->v1->co, vlr->v2->co, vlr->v3->co, c4);
                }
-               if(do_nmap_tangent || do_tangent) {
+               if (do_nmap_tangent || do_tangent) {
                        /* tangents still need to be calculated for flat faces too */
                        /* weighting removed, they are not vertexnormals */
                        calc_tangent_vector(obr, vtangents, arena, vlr, do_nmap_tangent, do_tangent);
@@ -626,21 +629,21 @@ static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangen
        }
 
                /* do solid faces */
-       for(a=0; a<obr->totvlak; a++) {
+       for (a=0; a<obr->totvlak; a++) {
                VlakRen *vlr= RE_findOrAddVlak(obr, a);
 
-               if((vlr->flag & ME_SMOOTH)==0) {
-                       if(is_zero_v3(vlr->v1->n)) copy_v3_v3(vlr->v1->n, vlr->n);
-                       if(is_zero_v3(vlr->v2->n)) copy_v3_v3(vlr->v2->n, vlr->n);
-                       if(is_zero_v3(vlr->v3->n)) copy_v3_v3(vlr->v3->n, vlr->n);
-                       if(vlr->v4 && is_zero_v3(vlr->v4->n)) copy_v3_v3(vlr->v4->n, vlr->n);
+               if ((vlr->flag & ME_SMOOTH)==0) {
+                       if (is_zero_v3(vlr->v1->n)) copy_v3_v3(vlr->v1->n, vlr->n);
+                       if (is_zero_v3(vlr->v2->n)) copy_v3_v3(vlr->v2->n, vlr->n);
+                       if (is_zero_v3(vlr->v3->n)) copy_v3_v3(vlr->v3->n, vlr->n);
+                       if (vlr->v4 && is_zero_v3(vlr->v4->n)) copy_v3_v3(vlr->v4->n, vlr->n);
                }
 
-               if(do_nmap_tangent) {
+               if (do_nmap_tangent) {
                        VertRen *v1=vlr->v1, *v2=vlr->v2, *v3=vlr->v3, *v4=vlr->v4;
                        MTFace *tface= RE_vlakren_get_tface(obr, vlr, obr->actmtface, NULL, 0);
 
-                       if(tface) {
+                       if (tface) {
                                int k=0;
                                float *vtang, *ftang= RE_vlakren_get_nmap_tangent(obr, vlr, 1);
 
@@ -653,21 +656,21 @@ static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangen
                                vtang= find_vertex_tangent(vtangents[v3->index], tface->uv[2]);
                                copy_v3_v3(ftang+8, vtang);
                                normalize_v3(ftang+8);
-                               if(v4) {
+                               if (v4) {
                                        vtang= find_vertex_tangent(vtangents[v4->index], tface->uv[3]);
                                        copy_v3_v3(ftang+12, vtang);
                                        normalize_v3(ftang+12);
                                }
-                               for(k=0; k<4; k++) ftang[4*k+3]=1;
+                               for (k=0; k<4; k++) ftang[4*k+3]=1;
                        }
                }
        }
        
-               /* normalize vertex normals */
-       for(a=0; a<obr->totvert; a++) {
+       /* normalize vertex normals */
+       for (a=0; a<obr->totvert; a++) {
                VertRen *ver= RE_findOrAddVert(obr, a);
                normalize_v3(ver->n);
-               if(do_tangent) {
+               if (do_tangent) {
                        float *tav= RE_vertren_get_tangent(obr, ver, 0);
                        if (tav) {
                                /* orthonorm. */
@@ -680,8 +683,7 @@ static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangen
                }
        }
 
-       if(do_nmap_tangent!=0)
-       {
+       if (do_nmap_tangent != 0) {
                SRenderMeshToTangent mesh2tangent;
                SMikkTSpaceContext sContext;
                SMikkTSpaceInterface sInterface;
@@ -703,9 +705,9 @@ static void calc_vertexnormals(Render *UNUSED(re), ObjectRen *obr, int do_tangen
                genTangSpaceDefault(&sContext);
        }
 
-       if(arena)
+       if (arena)
                BLI_memarena_free(arena);
-       if(vtangents)
+       if (vtangents)
                MEM_freeN(vtangents);
 }
 
@@ -729,16 +731,16 @@ static void as_addvert(ASvert *asv, VertRen *v1, VlakRen *vlr)
        ASface *asf;
        int a;
        
-       if(v1 == NULL) return;
+       if (v1 == NULL) return;
        
-       if(asv->faces.first==NULL) {
+       if (asv->faces.first==NULL) {
                asf= MEM_callocN(sizeof(ASface), "asface");
                BLI_addtail(&asv->faces, asf);
        }
        
        asf= asv->faces.last;
-       for(a=0; a<4; a++) {
-               if(asf->vlr[a]==NULL) {
+       for (a=0; a<4; a++) {
+               if (asf->vlr[a]==NULL) {
                        asf->vlr[a]= vlr;
                        asv->totface++;
                        break;
@@ -746,7 +748,7 @@ static void as_addvert(ASvert *asv, VertRen *v1, VlakRen *vlr)
        }
        
        /* new face struct */
-       if(a==4) {
+       if (a==4) {
                asf= MEM_callocN(sizeof(ASface), "asface");
                BLI_addtail(&asv->faces, asf);
                asf->vlr[0]= vlr;
@@ -761,14 +763,14 @@ static int as_testvertex(VlakRen *vlr, VertRen *UNUSED(ver), ASvert *asv, float
        float inp;
        int a;
        
-       if(vlr==0) return 0;
+       if (vlr==0) return 0;
        
        asf= asv->faces.first;
-       while(asf) {
-               for(a=0; a<4; a++) {
-                       if(asf->vlr[a] && asf->vlr[a]!=vlr) {
+       while (asf) {
+               for (a=0; a<4; a++) {
+                       if (asf->vlr[a] && asf->vlr[a]!=vlr) {
                                inp = fabsf(dot_v3v3(vlr->n, asf->vlr[a]->n));
-                               if(inp < thresh) return 1;
+                               if (inp < thresh) return 1;
                        }
                }
                asf= asf->next;
@@ -785,13 +787,13 @@ static VertRen *as_findvertex(VlakRen *vlr, VertRen *UNUSED(ver), ASvert *asv, f
        int a;
        
        asf= asv->faces.first;
-       while(asf) {
-               for(a=0; a<4; a++) {
-                       if(asf->vlr[a] && asf->vlr[a]!=vlr) {
+       while (asf) {
+               for (a=0; a<4; a++) {
+                       if (asf->vlr[a] && asf->vlr[a]!=vlr) {
                                /* this face already made a copy for this vertex! */
-                               if(asf->nver[a]) {
+                               if (asf->nver[a]) {
                                        inp = fabsf(dot_v3v3(vlr->n, asf->vlr[a]->n));
-                                       if(inp >= thresh) {
+                                       if (inp >= thresh) {
                                                return asf->nver[a];
                                        }
                                }
@@ -814,7 +816,7 @@ static void autosmooth(Render *UNUSED(re), ObjectRen *obr, float mat[][4], int d
        float thresh;
        int a, b, totvert;
        
-       if(obr->totvert==0) return;
+       if (obr->totvert==0) return;
        asverts= MEM_callocN(sizeof(ASvert)*obr->totvert, "all smooth verts");
        
        thresh= cosf(DEG2RADF((0.5f + (float)degr)));
@@ -823,43 +825,43 @@ static void autosmooth(Render *UNUSED(re), ObjectRen *obr, float mat[][4], int d
        
        
        /* step one: construct listbase of all vertices and pointers to faces */
-       for(a=0; a<obr->totvlak; a++) {
+       for (a=0; a<obr->totvlak; a++) {
                vlr= RE_findOrAddVlak(obr, a);
                /* skip wire faces */
-               if(vlr->v2 != vlr->v3) {
+               if (vlr->v2 != vlr->v3) {
                        as_addvert(asverts+vlr->v1->index, vlr->v1, vlr);
                        as_addvert(asverts+vlr->v2->index, vlr->v2, vlr);
                        as_addvert(asverts+vlr->v3->index, vlr->v3, vlr);
-                       if(vlr->v4) 
+                       if (vlr->v4)
                                as_addvert(asverts+vlr->v4->index, vlr->v4, vlr);
                }
        }
        
        totvert= obr->totvert;
        /* we now test all vertices, when faces have a normal too much different: they get a new vertex */
-       for(a=0, asv=asverts; a<totvert; a++, asv++) {
-               if(asv && asv->totface>1) {
+       for (a=0, asv=asverts; a<totvert; a++, asv++) {
+               if (asv && asv->totface>1) {
                        ver= RE_findOrAddVert(obr, a);
 
                        asf= asv->faces.first;
-                       while(asf) {
-                               for(b=0; b<4; b++) {
+                       while (asf) {
+                               for (b=0; b<4; b++) {
                                
                                        /* is there a reason to make a new vertex? */
                                        vlr= asf->vlr[b];
-                                       if( as_testvertex(vlr, ver, asv, thresh) ) {
+                                       if ( as_testvertex(vlr, ver, asv, thresh) ) {
                                                
                                                /* already made a new vertex within threshold? */
                                                v1= as_findvertex(vlr, ver, asv, thresh);
-                                               if(v1==NULL) {
+                                               if (v1==NULL) {
                                                        /* make a new vertex */
                                                        v1= RE_vertren_copy(obr, ver);
                                                }
                                                asf->nver[b]= v1;
-                                               if(vlr->v1==ver) vlr->v1= v1;
-                                               if(vlr->v2==ver) vlr->v2= v1;
-                                               if(vlr->v3==ver) vlr->v3= v1;
-                                               if(vlr->v4==ver) vlr->v4= v1;
+                                               if (vlr->v1==ver) vlr->v1= v1;
+                                               if (vlr->v2==ver) vlr->v2= v1;
+                                               if (vlr->v3==ver) vlr->v3= v1;
+                                               if (vlr->v4==ver) vlr->v4= v1;
                                        }
                                }
                                asf= asf->next;
@@ -868,25 +870,25 @@ static void autosmooth(Render *UNUSED(re), ObjectRen *obr, float mat[][4], int d
        }
        
        /* free */
-       for(a=0; a<totvert; a++) {
+       for (a=0; a<totvert; a++) {
                BLI_freelistN(&asverts[a].faces);
        }
        MEM_freeN(asverts);
        
        /* rotate vertices and calculate normal of faces */
-       for(a=0; a<obr->totvert; a++) {
+       for (a=0; a<obr->totvert; a++) {
                ver= RE_findOrAddVert(obr, a);
                mul_m4_v3(mat, ver->co);
        }
-       for(a=0; a<obr->totvlak; a++) {
+       for (a=0; a<obr->totvlak; a++) {
                vlr= RE_findOrAddVlak(obr, a);
                
                /* skip wire faces */
-               if(vlr->v2 != vlr->v3) {
-                       if(vlr->v4) 
-                               normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+               if (vlr->v2 != vlr->v3) {
+                       if (vlr->v4)
+                               normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                        else 
-                               normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                               normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                }
        }               
 }
@@ -906,9 +908,10 @@ static float *get_object_orco(Render *re, Object *ob)
 
        if (!orco) {
                if (ELEM(ob->type, OB_CURVE, OB_FONT)) {
-                       orco = make_orco_curve(re->scene, ob);
-               } else if (ob->type==OB_SURF) {
-                       orco = make_orco_surf(ob);
+                       orco = BKE_curve_make_orco(re->scene, ob);
+               }
+               else if (ob->type==OB_SURF) {
+                       orco = BKE_curve_surf_make_orco(ob);
                }
 
                if (orco)
@@ -943,16 +946,16 @@ static void check_material_mapto(Material *ma)
         * this can avoid a bit of time spent iterating through all the texture slots, map inputs and map tos
         * every time a property which may or may not be textured is accessed */
        
-       for(a=0; a<MAX_MTEX; a++) {
-               if(ma->mtex[a] && ma->mtex[a]->tex) {
+       for (a=0; a<MAX_MTEX; a++) {
+               if (ma->mtex[a] && ma->mtex[a]->tex) {
                        /* currently used only in volume render, so we'll check for those flags */
-                       if(ma->mtex[a]->mapto & MAP_DENSITY) ma->mapto_textured |= MAP_DENSITY;
-                       if(ma->mtex[a]->mapto & MAP_EMISSION) ma->mapto_textured |= MAP_EMISSION;
-                       if(ma->mtex[a]->mapto & MAP_EMISSION_COL) ma->mapto_textured |= MAP_EMISSION_COL;
-                       if(ma->mtex[a]->mapto & MAP_SCATTERING) ma->mapto_textured |= MAP_SCATTERING;
-                       if(ma->mtex[a]->mapto & MAP_TRANSMISSION_COL) ma->mapto_textured |= MAP_TRANSMISSION_COL;
-                       if(ma->mtex[a]->mapto & MAP_REFLECTION) ma->mapto_textured |= MAP_REFLECTION;
-                       if(ma->mtex[a]->mapto & MAP_REFLECTION_COL) ma->mapto_textured |= MAP_REFLECTION_COL;
+                       if (ma->mtex[a]->mapto & MAP_DENSITY) ma->mapto_textured |= MAP_DENSITY;
+                       if (ma->mtex[a]->mapto & MAP_EMISSION) ma->mapto_textured |= MAP_EMISSION;
+                       if (ma->mtex[a]->mapto & MAP_EMISSION_COL) ma->mapto_textured |= MAP_EMISSION_COL;
+                       if (ma->mtex[a]->mapto & MAP_SCATTERING) ma->mapto_textured |= MAP_SCATTERING;
+                       if (ma->mtex[a]->mapto & MAP_TRANSMISSION_COL) ma->mapto_textured |= MAP_TRANSMISSION_COL;
+                       if (ma->mtex[a]->mapto & MAP_REFLECTION) ma->mapto_textured |= MAP_REFLECTION;
+                       if (ma->mtex[a]->mapto & MAP_REFLECTION_COL) ma->mapto_textured |= MAP_REFLECTION_COL;
                }
        }
 }
@@ -960,17 +963,17 @@ static void flag_render_node_material(Render *re, bNodeTree *ntree)
 {
        bNode *node;
 
-       for(node=ntree->nodes.first; node; node= node->next) {
-               if(node->id) {
-                       if(GS(node->id->name)==ID_MA) {
+       for (node=ntree->nodes.first; node; node= node->next) {
+               if (node->id) {
+                       if (GS(node->id->name)==ID_MA) {
                                Material *ma= (Material *)node->id;
 
-                               if((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
+                               if ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
                                        re->flag |= R_ZTRA;
 
                                ma->flag |= MA_IS_USED;
                        }
-                       else if(node->type==NODE_GROUP)
+                       else if (node->type==NODE_GROUP)
                                flag_render_node_material(re, (bNodeTree *)node->id);
                }
        }
@@ -982,22 +985,22 @@ static Material *give_render_material(Render *re, Object *ob, short nr)
        Material *ma;
        
        ma= give_current_material(ob, nr);
-       if(ma==NULL) 
+       if (ma==NULL)
                ma= &defmaterial;
        
-       if(re->r.mode & R_SPEED) ma->texco |= NEED_UV;
+       if (re->r.mode & R_SPEED) ma->texco |= NEED_UV;
        
-       if(ma->material_type == MA_TYPE_VOLUME) {
+       if (ma->material_type == MA_TYPE_VOLUME) {
                ma->mode |= MA_TRANSP;
                ma->mode &= ~MA_SHADBUF;
        }
-       if((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
+       if ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
                re->flag |= R_ZTRA;
        
        /* for light groups and SSS */
        ma->flag |= MA_IS_USED;
 
-       if(ma->nodetree && ma->use_nodes)
+       if (ma->nodetree && ma->use_nodes)
                flag_render_node_material(re, ma->nodetree);
        
        check_material_mapto(ma);
@@ -1037,10 +1040,10 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
        dy= re->winy*cross[1]*re->winmat[1][1];
        w= sqrt(dx*dx + dy*dy)/w;
        
-       if(w!=0.0f) {
+       if (w!=0.0f) {
                float fac;
-               if(ma->strand_ease!=0.0f) {
-                       if(ma->strand_ease<0.0f)
+               if (ma->strand_ease!=0.0f) {
+                       if (ma->strand_ease<0.0f)
                                fac= pow(sd->time, 1.0f+ma->strand_ease);
                        else
                                fac= pow(sd->time, 1.0f/(1.0f-ma->strand_ease));
@@ -1050,15 +1053,15 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                width= ((1.0f-fac)*ma->strand_sta + (fac)*ma->strand_end);
 
                /* use actual Blender units for strand width and fall back to minimum width */
-               if(ma->mode & MA_STR_B_UNITS){
+               if (ma->mode & MA_STR_B_UNITS) {
                        crosslen= len_v3(cross);
                        w= 2.0f*crosslen*ma->strand_min/w;
 
-                       if(width < w)
+                       if (width < w)
                                width= w;
 
                        /*cross is the radius of the strand so we want it to be half of full width */
-                       mul_v3_fl(cross,0.5f/crosslen);
+                       mul_v3_fl(cross, 0.5f/crosslen);
                }
                else
                        width/=w;
@@ -1066,17 +1069,17 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                mul_v3_fl(cross, width);
        }
        
-       if(ma->mode & MA_TANGENT_STR)
+       if (ma->mode & MA_TANGENT_STR)
                flag= R_SMOOTH|R_TANGENT;
        else
                flag= R_SMOOTH;
        
        /* only 1 pixel wide strands filled in as quads now, otherwise zbuf errors */
-       if(ma->strand_sta==1.0f)
+       if (ma->strand_sta==1.0f)
                flag |= R_STRAND;
        
        /* single face line */
-       if(sd->line) {
+       if (sd->line) {
                vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                vlr->flag= flag;
                vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
@@ -1108,28 +1111,28 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                vlr->v3->orco= sd->orco;
                vlr->v3->accum= vlr->v4->accum;
 
-               normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+               normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                
                vlr->mat= ma;
                vlr->ec= ME_V2V3;
 
-               if(sd->surfnor) {
+               if (sd->surfnor) {
                        float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
                        copy_v3_v3(snor, sd->surfnor);
                }
 
-               if(sd->uvco){
-                       for(i=0; i<sd->totuv; i++){
+               if (sd->uvco) {
+                       for (i=0; i<sd->totuv; i++) {
                                MTFace *mtf;
-                               mtf=RE_vlakren_get_tface(obr,vlr,i,NULL,1);
+                               mtf=RE_vlakren_get_tface(obr, vlr, i, NULL, 1);
                                mtf->uv[0][0]=mtf->uv[1][0]=
                                mtf->uv[2][0]=mtf->uv[3][0]=(sd->uvco+2*i)[0];
                                mtf->uv[0][1]=mtf->uv[1][1]=
                                mtf->uv[2][1]=mtf->uv[3][1]=(sd->uvco+2*i)[1];
                        }
-                       if(sd->override_uv>=0){
+                       if (sd->override_uv>=0) {
                                MTFace *mtf;
-                               mtf=RE_vlakren_get_tface(obr,vlr,sd->override_uv,NULL,0);
+                               mtf=RE_vlakren_get_tface(obr, vlr, sd->override_uv, NULL, 0);
                                
                                mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
                                mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
@@ -1138,18 +1141,18 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                                mtf->uv[2][1]=mtf->uv[3][1]=1.0f;
                        }
                }
-               if(sd->mcol){
-                       for(i=0; i<sd->totcol; i++){
+               if (sd->mcol) {
+                       for (i=0; i<sd->totcol; i++) {
                                MCol *mc;
-                               mc=RE_vlakren_get_mcol(obr,vlr,i,NULL,1);
+                               mc=RE_vlakren_get_mcol(obr, vlr, i, NULL, 1);
                                mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
                                mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
                        }
                }
        }
        /* first two vertices of a strand */
-       else if(sd->first) {
-               if(sd->adapt){
+       else if (sd->first) {
+               if (sd->adapt) {
                        copy_v3_v3(anor, nor);
                        copy_v3_v3(avec, vec);
                        second=1;
@@ -1172,7 +1175,7 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
        }
        /* more vertices & faces to strand */
        else {
-               if(sd->adapt==0 || second){
+               if (sd->adapt==0 || second) {
                        vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                        vlr->flag= flag;
                        vlr->v1= v1;
@@ -1184,24 +1187,24 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                        v2= vlr->v3; // cycle
 
                        
-                       if(sd->adapt){
+                       if (sd->adapt) {
                                second=0;
-                               copy_v3_v3(anor,nor);
-                               copy_v3_v3(avec,vec);
+                               copy_v3_v3(anor, nor);
+                               copy_v3_v3(avec, vec);
                        }
 
                }
-               else if(sd->adapt){
-                       float dvec[3],pvec[3];
-                       sub_v3_v3v3(dvec,avec,vec);
-                       project_v3_v3v3(pvec,dvec,vec);
-                       sub_v3_v3v3(dvec,dvec,pvec);
+               else if (sd->adapt) {
+                       float dvec[3], pvec[3];
+                       sub_v3_v3v3(dvec, avec, vec);
+                       project_v3_v3v3(pvec, dvec, vec);
+                       sub_v3_v3v3(dvec, dvec, pvec);
 
                        w= vec[2]*re->winmat[2][3] + re->winmat[3][3];
                        dx= re->winx*dvec[0]*re->winmat[0][0]/w;
                        dy= re->winy*dvec[1]*re->winmat[1][1]/w;
                        w= sqrt(dx*dx + dy*dy);
-                       if(dot_v3v3(anor,nor)<sd->adapt_angle && w>sd->adapt_pix){
+                       if (dot_v3v3(anor, nor)<sd->adapt_angle && w>sd->adapt_pix) {
                                vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                                vlr->flag= flag;
                                vlr->v1= v1;
@@ -1212,10 +1215,10 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                                v1= vlr->v4; // cycle
                                v2= vlr->v3; // cycle
 
-                               copy_v3_v3(anor,nor);
-                               copy_v3_v3(avec,vec);
+                               copy_v3_v3(anor, nor);
+                               copy_v3_v3(avec, vec);
                        }
-                       else{
+                       else {
                                vlr= RE_findOrAddVlak(obr, obr->totvlak-1);
                        }
                }
@@ -1232,28 +1235,28 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                vlr->v3->orco= sd->orco;
                vlr->v3->accum= vlr->v4->accum;
                
-               normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+               normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                
                vlr->mat= ma;
                vlr->ec= ME_V2V3;
 
-               if(sd->surfnor) {
+               if (sd->surfnor) {
                        float *snor= RE_vlakren_get_surfnor(obr, vlr, 1);
                        copy_v3_v3(snor, sd->surfnor);
                }
 
-               if(sd->uvco){
-                       for(i=0; i<sd->totuv; i++){
+               if (sd->uvco) {
+                       for (i=0; i<sd->totuv; i++) {
                                MTFace *mtf;
-                               mtf=RE_vlakren_get_tface(obr,vlr,i,NULL,1);
+                               mtf=RE_vlakren_get_tface(obr, vlr, i, NULL, 1);
                                mtf->uv[0][0]=mtf->uv[1][0]=
                                mtf->uv[2][0]=mtf->uv[3][0]=(sd->uvco+2*i)[0];
                                mtf->uv[0][1]=mtf->uv[1][1]=
                                mtf->uv[2][1]=mtf->uv[3][1]=(sd->uvco+2*i)[1];
                        }
-                       if(sd->override_uv>=0){
+                       if (sd->override_uv>=0) {
                                MTFace *mtf;
-                               mtf=RE_vlakren_get_tface(obr,vlr,sd->override_uv,NULL,0);
+                               mtf=RE_vlakren_get_tface(obr, vlr, sd->override_uv, NULL, 0);
                                
                                mtf->uv[0][0]=mtf->uv[3][0]=0.0f;
                                mtf->uv[1][0]=mtf->uv[2][0]=1.0f;
@@ -1262,10 +1265,10 @@ static void static_particle_strand(Render *re, ObjectRen *obr, Material *ma, Par
                                mtf->uv[2][1]=mtf->uv[3][1]=(vlr->v3->accum+1.0f)/2.0f;
                        }
                }
-               if(sd->mcol){
-                       for(i=0; i<sd->totcol; i++){
+               if (sd->mcol) {
+                       for (i=0; i<sd->totcol; i++) {
                                MCol *mc;
-                               mc=RE_vlakren_get_mcol(obr,vlr,i,NULL,1);
+                               mc=RE_vlakren_get_mcol(obr, vlr, i, NULL, 1);
                                mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
                                mc[0]=mc[1]=mc[2]=mc[3]=sd->mcol[i];
                        }
@@ -1278,7 +1281,7 @@ static void static_particle_wire(ObjectRen *obr, Material *ma, const float vec[3
        VlakRen *vlr;
        static VertRen *v1;
 
-       if(line) {
+       if (line) {
                vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                vlr->v1= RE_findOrAddVert(obr, obr->totvert++);
                vlr->v2= RE_findOrAddVert(obr, obr->totvert++);
@@ -1297,7 +1300,7 @@ static void static_particle_wire(ObjectRen *obr, Material *ma, const float vec[3
                vlr->ec= ME_V1V2;
 
        }
-       else if(first) {
+       else if (first) {
                v1= RE_findOrAddVert(obr, obr->totvert++);
                copy_v3_v3(v1->co, vec);
        }
@@ -1325,11 +1328,11 @@ static void particle_curve(Render *re, ObjectRen *obr, DerivedMesh *dm, Material
 {
        HaloRen *har=0;
 
-       if(ma->material_type == MA_TYPE_WIRE)
+       if (ma->material_type == MA_TYPE_WIRE)
                static_particle_wire(obr, ma, loc, loc1, sd->first, sd->line);
-       else if(ma->material_type == MA_TYPE_HALO) {
+       else if (ma->material_type == MA_TYPE_HALO) {
                har= RE_inithalo_particle(re, obr, dm, ma, loc, loc1, sd->orco, sd->uvco, sd->size, 1.0, seed, pa_co);
-               if(har) har->lay= obr->ob->lay;
+               if (har) har->lay= obr->ob->lay;
        }
        else
                static_particle_strand(re, obr, ma, sd, loc, loc1);
@@ -1369,52 +1372,52 @@ static void particle_billboard(Render *re, ObjectRen *obr, Material *ma, Particl
        sub_v3_v3(vlr->v4->co, yvec);
        mul_m4_v3(re->viewmat, vlr->v4->co);
 
-       normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
-       copy_v3_v3(vlr->v1->n,vlr->n);
-       copy_v3_v3(vlr->v2->n,vlr->n);
-       copy_v3_v3(vlr->v3->n,vlr->n);
-       copy_v3_v3(vlr->v4->n,vlr->n);
+       normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+       copy_v3_v3(vlr->v1->n, vlr->n);
+       copy_v3_v3(vlr->v2->n, vlr->n);
+       copy_v3_v3(vlr->v3->n, vlr->n);
+       copy_v3_v3(vlr->v4->n, vlr->n);
        
        vlr->mat= ma;
        vlr->ec= ME_V2V3;
 
-       if(bb->uv_split > 1){
+       if (bb->uv_split > 1) {
                uvdx = uvdy = 1.0f / (float)bb->uv_split;
 
-               if(ELEM(bb->anim, PART_BB_ANIM_AGE, PART_BB_ANIM_FRAME)) {
-                       if(bb->anim == PART_BB_ANIM_FRAME)
+               if (ELEM(bb->anim, PART_BB_ANIM_AGE, PART_BB_ANIM_FRAME)) {
+                       if (bb->anim == PART_BB_ANIM_FRAME)
                                time = ((int)(bb->time * bb->lifetime) % totsplit)/(float)totsplit;
                        else
                                time = bb->time;
                }
-               else if(bb->anim == PART_BB_ANIM_ANGLE) {
-                       if(bb->align == PART_BB_VIEW) {
+               else if (bb->anim == PART_BB_ANIM_ANGLE) {
+                       if (bb->align == PART_BB_VIEW) {
                                time = (float)fmod((bb->tilt + 1.0f) / 2.0f, 1.0);
                        }
                        else {
-                               float axis1[3] = {0.0f,0.0f,0.0f};
-                               float axis2[3] = {0.0f,0.0f,0.0f};
+                               float axis1[3] = {0.0f, 0.0f, 0.0f};
+                               float axis2[3] = {0.0f, 0.0f, 0.0f};
 
                                axis1[(bb->align + 1) % 3] = 1.0f;
                                axis2[(bb->align + 2) % 3] = 1.0f;
 
-                               if(bb->lock == 0) {
+                               if (bb->lock == 0) {
                                        zvec[bb->align] = 0.0f;
                                        normalize_v3(zvec);
                                }
                                
                                time = saacos(dot_v3v3(zvec, axis1)) / (float)M_PI;
                                
-                               if(dot_v3v3(zvec, axis2) < 0.0f)
+                               if (dot_v3v3(zvec, axis2) < 0.0f)
                                        time = 1.0f - time / 2.0f;
                                else
                                        time /= 2.0f;
                        }
                }
 
-               if(bb->split_offset == PART_BB_OFF_LINEAR)
+               if (bb->split_offset == PART_BB_OFF_LINEAR)
                        time = (float)fmod(time + (float)bb->num / (float)totsplit, 1.0f);
-               else if(bb->split_offset==PART_BB_OFF_RANDOM)
+               else if (bb->split_offset==PART_BB_OFF_RANDOM)
                        time = (float)fmod(time + bb->random, 1.0f);
 
                /* Find the coordinates in tile space (integer), then convert to UV
@@ -1428,7 +1431,7 @@ static void particle_billboard(Render *re, ObjectRen *obr, Material *ma, Particl
        }
 
        /* normal UVs */
-       if(bb->uv[0] >= 0){
+       if (bb->uv[0] >= 0) {
                mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[0], NULL, 1);
                mtf->uv[0][0] = 1.0f;
                mtf->uv[0][1] = 1.0f;
@@ -1441,14 +1444,14 @@ static void particle_billboard(Render *re, ObjectRen *obr, Material *ma, Particl
        }
 
        /* time-index UVs */
-       if(bb->uv[1] >= 0){
+       if (bb->uv[1] >= 0) {
                mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[1], NULL, 1);
                mtf->uv[0][0] = mtf->uv[1][0] = mtf->uv[2][0] = mtf->uv[3][0] = bb->time;
                mtf->uv[0][1] = mtf->uv[1][1] = mtf->uv[2][1] = mtf->uv[3][1] = (float)bb->num/(float)bb->totnum;
        }
 
        /* split UVs */
-       if(bb->uv_split > 1 && bb->uv[2] >= 0){
+       if (bb->uv_split > 1 && bb->uv[2] >= 0) {
                mtf = RE_vlakren_get_tface(obr, vlr, bb->uv[2], NULL, 1);
                mtf->uv[0][0] = uvx + uvdx;
                mtf->uv[0][1] = uvy + uvdy;
@@ -1466,10 +1469,10 @@ static void particle_normal_ren(short ren_as, ParticleSettings *part, Render *re
        
        copy_v3_v3(loc, state->co);
 
-       if(ren_as != PART_DRAW_BB)
+       if (ren_as != PART_DRAW_BB)
                mul_m4_v3(re->viewmat, loc);
 
-       switch(ren_as) {
+       switch (ren_as) {
                case PART_DRAW_LINE:
                        sd->line = 1;
                        sd->time = 0.0f;
@@ -1479,7 +1482,7 @@ static void particle_normal_ren(short ren_as, ParticleSettings *part, Render *re
                        mul_mat3_m4_v3(re->viewmat, vel);
                        normalize_v3(vel);
 
-                       if(part->draw & PART_DRAW_VEL_LENGTH)
+                       if (part->draw & PART_DRAW_VEL_LENGTH)
                                mul_v3_fl(vel, len_v3(state->vel));
 
                        madd_v3_v3v3fl(loc0, loc, vel, -part->draw_line[0]);
@@ -1504,7 +1507,7 @@ static void particle_normal_ren(short ren_as, ParticleSettings *part, Render *re
 
                        har = RE_inithalo_particle(re, obr, dm, ma, loc, NULL, sd->orco, sd->uvco, hasize, 0.0, seed, pa_co);
                        
-                       if(har) har->lay= obr->ob->lay;
+                       if (har) har->lay= obr->ob->lay;
 
                        break;
                }
@@ -1515,9 +1518,9 @@ static void get_particle_uvco_mcol(short from, DerivedMesh *dm, float *fuv, int
        int i;
 
        /* get uvco */
-       if(sd->uvco && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
-               for(i=0; i<sd->totuv; i++) {
-                       if(num != DMCACHE_NOTFOUND) {
+       if (sd->uvco && ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) {
+               for (i=0; i<sd->totuv; i++) {
+                       if (num != DMCACHE_NOTFOUND) {
                                MFace *mface = dm->getTessFaceData(dm, num, CD_MFACE);
                                MTFace *mtface = (MTFace*)CustomData_get_layer_n(&dm->faceData, CD_MTFACE, i);
                                mtface += num;
@@ -1532,9 +1535,9 @@ static void get_particle_uvco_mcol(short from, DerivedMesh *dm, float *fuv, int
        }
 
        /* get mcol */
-       if(sd->mcol && ELEM(from,PART_FROM_FACE,PART_FROM_VOLUME)) {
-               for(i=0; i<sd->totcol; i++) {
-                       if(num != DMCACHE_NOTFOUND) {
+       if (sd->mcol && ELEM(from, PART_FROM_FACE, PART_FROM_VOLUME)) {
+               for (i=0; i<sd->totcol; i++) {
+                       if (num != DMCACHE_NOTFOUND) {
                                MFace *mface = dm->getTessFaceData(dm, num, CD_MFACE);
                                MCol *mc = (MCol*)CustomData_get_layer_n(&dm->faceData, CD_MCOL, i);
                                mc += num * 4;
@@ -1554,7 +1557,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        ParticleSystemModifierData *psmd;
        ParticleSystem *tpsys=0;
        ParticleSettings *part, *tpart=0;
-       ParticleData *pars, *pa=0,*tpa=0;
+       ParticleData *pars, *pa=0, *tpa=0;
        ParticleKey *states=0;
        ParticleKey state;
        ParticleCacheKey *cache=0;
@@ -1566,7 +1569,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        StrandBound *sbound= 0;
        StrandRen *strand=0;
        RNG *rng= 0;
-       float loc[3],loc1[3],loc0[3],mat[4][4],nmat[3][3],co[3],nor[3],duplimat[4][4];
+       float loc[3], loc1[3], loc0[3], mat[4][4], nmat[3][3], co[3], nor[3], duplimat[4][4];
        float strandlen=0.0f, curlen=0.0f;
        float hasize, pa_size, r_tilt, r_length;
        float pa_time, pa_birthtime, pa_dietime;
@@ -1579,23 +1582,23 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        char **uv_name=0;
 
 /* 1. check that everything is ok & updated */
-       if(psys==NULL)
+       if (psys==NULL)
                return 0;
 
        part=psys->part;
        pars=psys->particles;
 
-       if(part==NULL || pars==NULL || !psys_check_enabled(ob, psys))
+       if (part==NULL || pars==NULL || !psys_check_enabled(ob, psys))
                return 0;
        
-       if(part->ren_as==PART_DRAW_OB || part->ren_as==PART_DRAW_GR || part->ren_as==PART_DRAW_NOT)
+       if (part->ren_as==PART_DRAW_OB || part->ren_as==PART_DRAW_GR || part->ren_as==PART_DRAW_NOT)
                return 1;
 
-/* 2. start initialising things */
+/* 2. start initializing things */
 
        /* last possibility to bail out! */
-       psmd = psys_get_modifier(ob,psys);
-       if(!(psmd->modifier.mode & eModifierMode_Render))
+       psmd = psys_get_modifier(ob, psys);
+       if (!(psmd->modifier.mode & eModifierMode_Render))
                return 0;
 
        sim.scene= re->scene;
@@ -1603,16 +1606,16 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        sim.psys= psys;
        sim.psmd= psmd;
 
-       if(part->phystype==PART_PHYS_KEYED)
+       if (part->phystype==PART_PHYS_KEYED)
                psys_count_keyed_targets(&sim);
 
        totchild=psys->totchild;
 
        /* can happen for disconnected/global hair */
-       if(part->type==PART_HAIR && !psys->childcache)
+       if (part->type==PART_HAIR && !psys->childcache)
                totchild= 0;
 
-       if(G.rendering == 0) { /* preview render */
+       if (G.rendering == 0) { /* preview render */
                totchild = (int)((float)totchild * (float)part->disp / 100.0f);
        }
 
@@ -1629,7 +1632,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        ma= give_render_material(re, ob, part->omat);
        
 #if 0 // XXX old animation system
-       if(ma->ipo){
+       if (ma->ipo) {
                calc_ipo(ma->ipo, cfra);
                execute_ipo((ID *)ma, ma->ipo);
        }
@@ -1644,10 +1647,10 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        sd.totuv = CustomData_number_of_layers(&psmd->dm->faceData, CD_MTFACE);
        sd.totcol = CustomData_number_of_layers(&psmd->dm->faceData, CD_MCOL);
 
-       if(ma->texco & TEXCO_UV && sd.totuv) {
+       if (ma->texco & TEXCO_UV && sd.totuv) {
                sd.uvco = MEM_callocN(sd.totuv * 2 * sizeof(float), "particle_uvs");
 
-               if(ma->strand_uvname[0]) {
+               if (ma->strand_uvname[0]) {
                        sd.override_uv = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, ma->strand_uvname);
                        sd.override_uv -= CustomData_get_layer_index(&psmd->dm->faceData, CD_MTFACE);
                }
@@ -1655,22 +1658,22 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        else
                sd.uvco = NULL;
 
-       if(sd.totcol)
+       if (sd.totcol)
                sd.mcol = MEM_callocN(sd.totcol * sizeof(MCol), "particle_mcols");
 
 /* 2.2 setup billboards */
-       if(part->ren_as == PART_DRAW_BB) {
+       if (part->ren_as == PART_DRAW_BB) {
                int first_uv = CustomData_get_layer_index(&psmd->dm->faceData, CD_MTFACE);
 
                bb.uv[0] = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, psys->bb_uvname[0]);
-               if(bb.uv[0] < 0)
+               if (bb.uv[0] < 0)
                        bb.uv[0] = CustomData_get_active_layer_index(&psmd->dm->faceData, CD_MTFACE);
 
                bb.uv[1] = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, psys->bb_uvname[1]);
 
                bb.uv[2] = CustomData_get_named_layer_index(&psmd->dm->faceData, CD_MTFACE, psys->bb_uvname[2]);
 
-               if(first_uv >= 0) {
+               if (first_uv >= 0) {
                        bb.uv[0] -= first_uv;
                        bb.uv[1] -= first_uv;
                        bb.uv[2] -= first_uv;
@@ -1691,24 +1694,24 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
        copy_m3_m4(nmat, ob->imat);
        transpose_m3(nmat);
 
-       if(psys->flag & PSYS_USE_IMAT) {
+       if (psys->flag & PSYS_USE_IMAT) {
                /* psys->imat is the original emitter's inverse matrix, ob->obmat is the duplicated object's matrix */
                mult_m4_m4m4(duplimat, ob->obmat, psys->imat);
                use_duplimat = 1;
        }
 
 /* 2.6 setup strand rendering */
-       if(part->ren_as == PART_DRAW_PATH && psys->pathcache){
-               path_nbr=(int)pow(2.0,(double) part->ren_step);
+       if (part->ren_as == PART_DRAW_PATH && psys->pathcache) {
+               path_nbr=(int)pow(2.0, (double) part->ren_step);
 
-               if(path_nbr) {
-                       if(!ELEM(ma->material_type, MA_TYPE_HALO, MA_TYPE_WIRE)) {
+               if (path_nbr) {
+                       if (!ELEM(ma->material_type, MA_TYPE_HALO, MA_TYPE_WIRE)) {
                                sd.orco = MEM_mallocN(3*sizeof(float)*(totpart+totchild), "particle orcos");
                                set_object_orco(re, psys, sd.orco);
                        }
                }
 
-               if(part->draw & PART_DRAW_REN_ADAPT) {
+               if (part->draw & PART_DRAW_REN_ADAPT) {
                        sd.adapt = 1;
                        sd.adapt_pix = (float)part->adapt_pix;
                        sd.adapt_angle = cosf(DEG2RADF((float)part->adapt_angle));
@@ -1726,29 +1729,29 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                        strandbuf->overrideuv= sd.override_uv;
                        strandbuf->minwidth= ma->strand_min;
 
-                       if(ma->strand_widthfade == 0.0f)
+                       if (ma->strand_widthfade == 0.0f)
                                strandbuf->widthfade= 0.0f;
-                       else if(ma->strand_widthfade >= 1.0f)
+                       else if (ma->strand_widthfade >= 1.0f)
                                strandbuf->widthfade= 2.0f - ma->strand_widthfade;
                        else
                                strandbuf->widthfade= 1.0f/MAX2(ma->strand_widthfade, 1e-5f);
 
-                       if(part->flag & PART_HAIR_BSPLINE)
+                       if (part->flag & PART_HAIR_BSPLINE)
                                strandbuf->flag |= R_STRAND_BSPLINE;
-                       if(ma->mode & MA_STR_B_UNITS)
+                       if (ma->mode & MA_STR_B_UNITS)
                                strandbuf->flag |= R_STRAND_B_UNITS;
 
                        svert= strandbuf->vert;
 
-                       if(re->r.mode & R_SPEED)
+                       if (re->r.mode & R_SPEED)
                                dosurfacecache= 1;
-                       else if((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && (re->wrld.ao_gather_method == WO_AOGATHER_APPROX))
-                               if(ma->amb != 0.0f)
+                       else if ((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && (re->wrld.ao_gather_method == WO_AOGATHER_APPROX))
+                               if (ma->amb != 0.0f)
                                        dosurfacecache= 1;
 
                        totface= psmd->dm->getNumTessFaces(psmd->dm);
                        origindex= psmd->dm->getTessFaceDataArray(psmd->dm, CD_ORIGINDEX);
-                       for(a=0; a<totface; a++)
+                       for (a=0; a<totface; a++)
                                strandbuf->totbound= MAX2(strandbuf->totbound, (origindex)? origindex[a]: a);
 
                        strandbuf->totbound++;
@@ -1758,20 +1761,20 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                }
        }
 
-       if(sd.orco == 0) {
+       if (sd.orco == 0) {
                sd.orco = MEM_mallocN(3 * sizeof(float), "particle orco");
                orco1 = 1;
        }
 
-       if(path_nbr == 0)
+       if (path_nbr == 0)
                psys->lattice = psys_get_lattice(&sim);
 
 /* 3. start creating renderable things */
-       for(a=0,pa=pars; a<totpart+totchild; a++, pa++, seed++) {
+       for (a=0, pa=pars; a<totpart+totchild; a++, pa++, seed++) {
                random = rng_getFloat(rng);
                /* setup per particle individual stuff */
-               if(a<totpart){
-                       if(pa->flag & PARS_UNEXIST) continue;
+               if (a<totpart) {
+                       if (pa->flag & PARS_UNEXIST) continue;
 
                        pa_time=(cfra-pa->time)/pa->lifetime;
                        pa_birthtime = pa->time;
@@ -1779,19 +1782,20 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
 
                        hasize = ma->hasize;
 
+                       /* XXX 'tpsys' is alwyas NULL, this code won't run! */
                        /* get orco */
-                       if(tpsys && part->phystype==PART_PHYS_NO){
-                               tpa=tpsys->particles+pa->num;
-                               psys_particle_on_emitter(psmd,tpart->from,tpa->num,pa->num_dmcache,tpa->fuv,tpa->foffset,co,nor,0,0,sd.orco,0);
+                       if (tpsys && part->phystype == PART_PHYS_NO) {
+                               tpa = tpsys->particles + pa->num;
+                               psys_particle_on_emitter(psmd, tpart->from, tpa->num, pa->num_dmcache, tpa->fuv, tpa->foffset, co, nor, 0, 0, sd.orco, 0);
                        }
                        else
-                               psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,co,nor,0,0,sd.orco,0);
+                               psys_particle_on_emitter(psmd, part->from, pa->num, pa->num_dmcache, pa->fuv, pa->foffset, co, nor, 0, 0, sd.orco, 0);
 
                        /* get uvco & mcol */
                        num= pa->num_dmcache;
 
-                       if(num == DMCACHE_NOTFOUND)
-                               if(pa->num < psmd->dm->getNumTessFaces(psmd->dm))
+                       if (num == DMCACHE_NOTFOUND)
+                               if (pa->num < psmd->dm->getNumTessFaces(psmd->dm))
                                        num= pa->num;
 
                        get_particle_uvco_mcol(part->from, psmd->dm, pa->fuv, num, &sd);
@@ -1801,20 +1805,20 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                        r_tilt = 2.0f*(PSYS_FRAND(a) - 0.5f);
                        r_length = PSYS_FRAND(a+1);
 
-                       if(path_nbr) {
+                       if (path_nbr) {
                                cache = psys->pathcache[a];
                                max_k = (int)cache->steps;
                        }
 
-                       if(totchild && (part->draw&PART_DRAW_PARENT)==0) continue;
+                       if (totchild && (part->draw&PART_DRAW_PARENT)==0) continue;
                }
                else {
                        ChildParticle *cpa= psys->child+a-totpart;
 
-                       if(path_nbr) {
+                       if (path_nbr) {
                                cache = psys->childcache[a-totpart];
 
-                               if(cache->steps < 0)
+                               if (cache->steps < 0)
                                        continue;
 
                                max_k = (int)cache->steps;
@@ -1829,28 +1833,28 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                        num = cpa->num;
 
                        /* get orco */
-                       if(part->childtype == PART_CHILD_FACES) {
+                       if (part->childtype == PART_CHILD_FACES) {
                                psys_particle_on_emitter(psmd,
-                                       PART_FROM_FACE, cpa->num,DMCACHE_ISCHILD,
-                                       cpa->fuv,cpa->foffset,co,nor,0,0,sd.orco,0);
+                                       PART_FROM_FACE, cpa->num, DMCACHE_ISCHILD,
+                                       cpa->fuv, cpa->foffset, co, nor, 0, 0, sd.orco, 0);
                        }
                        else {
                                ParticleData *par = psys->particles + cpa->parent;
                                psys_particle_on_emitter(psmd, part->from,
-                                       par->num,DMCACHE_ISCHILD,par->fuv,
-                                       par->foffset,co,nor,0,0,sd.orco,0);
+                                       par->num, DMCACHE_ISCHILD, par->fuv,
+                                       par->foffset, co, nor, 0, 0, sd.orco, 0);
                        }
 
                        /* get uvco & mcol */
-                       if(part->childtype==PART_CHILD_FACES) {
+                       if (part->childtype==PART_CHILD_FACES) {
                                get_particle_uvco_mcol(PART_FROM_FACE, psmd->dm, cpa->fuv, cpa->num, &sd);
                        }
                        else {
                                ParticleData *parent = psys->particles + cpa->parent;
                                num = parent->num_dmcache;
 
-                               if(num == DMCACHE_NOTFOUND)
-                                       if(parent->num < psmd->dm->getNumTessFaces(psmd->dm))
+                               if (num == DMCACHE_NOTFOUND)
+                                       if (parent->num < psmd->dm->getNumTessFaces(psmd->dm))
                                                num = parent->num;
 
                                get_particle_uvco_mcol(part->from, psmd->dm, parent->fuv, num, &sd);
@@ -1858,10 +1862,10 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
 
                        dosimplify = psys_render_simplify_params(psys, cpa, simplify);
 
-                       if(strandbuf) {
+                       if (strandbuf) {
                                int orignum= (origindex)? origindex[cpa->num]: cpa->num;
 
-                               if(orignum > sbound - strandbuf->bound) {
+                               if (orignum > sbound - strandbuf->bound) {
                                        sbound= strandbuf->bound + orignum;
                                        sbound->start= sbound->end= obr->totstrand;
                                }
@@ -1874,7 +1878,7 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                pa_co[2] = 0.f;
 
                /* surface normal shading setup */
-               if(ma->mode_l & MA_STR_SURFDIFF) {
+               if (ma->mode_l & MA_STR_SURFDIFF) {
                        mul_m3_v3(nmat, nor);
                        sd.surfnor= nor;
                }
@@ -1882,31 +1886,31 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                        sd.surfnor= NULL;
 
                /* strand render setup */
-               if(strandbuf) {
+               if (strandbuf) {
                        strand= RE_findOrAddStrand(obr, obr->totstrand++);
                        strand->buffer= strandbuf;
                        strand->vert= svert;
                        copy_v3_v3(strand->orco, sd.orco);
 
-                       if(dosimplify) {
+                       if (dosimplify) {
                                float *ssimplify= RE_strandren_get_simplify(obr, strand, 1);
                                ssimplify[0]= simplify[0];
                                ssimplify[1]= simplify[1];
                        }
 
-                       if(sd.surfnor) {
+                       if (sd.surfnor) {
                                float *snor= RE_strandren_get_surfnor(obr, strand, 1);
                                copy_v3_v3(snor, sd.surfnor);
                        }
 
-                       if(dosurfacecache && num >= 0) {
+                       if (dosurfacecache && num >= 0) {
                                int *facenum= RE_strandren_get_face(obr, strand, 1);
                                *facenum= num;
                        }
 
-                       if(sd.uvco) {
-                               for(i=0; i<sd.totuv; i++) {
-                                       if(i != sd.override_uv) {
+                       if (sd.uvco) {
+                               for (i=0; i<sd.totuv; i++) {
+                                       if (i != sd.override_uv) {
                                                float *uv= RE_strandren_get_uv(obr, strand, i, NULL, 1);
 
                                                uv[0]= sd.uvco[2*i];
@@ -1914,8 +1918,8 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                                        }
                                }
                        }
-                       if(sd.mcol) {
-                               for(i=0; i<sd.totcol; i++) {
+                       if (sd.mcol) {
+                               for (i=0; i<sd.totcol; i++) {
                                        MCol *mc= RE_strandren_get_mcol(obr, strand, i, NULL, 1);
                                        *mc = sd.mcol[i];
                                }
@@ -1925,47 +1929,47 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                }
 
                /* strandco computation setup */
-               if(path_nbr) {
+               if (path_nbr) {
                        strandlen= 0.0f;
                        curlen= 0.0f;
-                       for(k=1; k<=path_nbr; k++)
-                               if(k<=max_k)
+                       for (k=1; k<=path_nbr; k++)
+                               if (k<=max_k)
                                        strandlen += len_v3v3((cache+k-1)->co, (cache+k)->co);
                }
 
-               if(path_nbr) {
+               if (path_nbr) {
                        /* render strands */
-                       for(k=0; k<=path_nbr; k++){
+                       for (k=0; k<=path_nbr; k++) {
                                float time;
 
-                               if(k<=max_k){
-                                       copy_v3_v3(state.co,(cache+k)->co);
-                                       copy_v3_v3(state.vel,(cache+k)->vel);
+                               if (k<=max_k) {
+                                       copy_v3_v3(state.co, (cache+k)->co);
+                                       copy_v3_v3(state.vel, (cache+k)->vel);
                                }
                                else
                                        continue;       
 
-                               if(k > 0)
+                               if (k > 0)
                                        curlen += len_v3v3((cache+k-1)->co, (cache+k)->co);
                                time= curlen/strandlen;
 
-                               copy_v3_v3(loc,state.co);
-                               mul_m4_v3(re->viewmat,loc);
+                               copy_v3_v3(loc, state.co);
+                               mul_m4_v3(re->viewmat, loc);
 
-                               if(strandbuf) {
+                               if (strandbuf) {
                                        copy_v3_v3(svert->co, loc);
                                        svert->strandco= -1.0f + 2.0f*time;
                                        svert++;
                                        strand->totvert++;
                                }
-                               else{
+                               else {
                                        sd.size = hasize;
 
-                                       if(k==1){
+                                       if (k==1) {
                                                sd.first = 1;
                                                sd.time = 0.0f;
-                                               sub_v3_v3v3(loc0,loc1,loc);
-                                               add_v3_v3v3(loc0,loc1,loc0);
+                                               sub_v3_v3v3(loc0, loc1, loc);
+                                               add_v3_v3v3(loc0, loc1, loc0);
 
                                                particle_curve(re, obr, psmd->dm, ma, &sd, loc1, loc0, seed, pa_co);
                                        }
@@ -1973,17 +1977,17 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                                        sd.first = 0;
                                        sd.time = time;
 
-                                       if(k)
+                                       if (k)
                                                particle_curve(re, obr, psmd->dm, ma, &sd, loc, loc1, seed, pa_co);
 
-                                       copy_v3_v3(loc1,loc);
+                                       copy_v3_v3(loc1, loc);
                                }
                        }
 
                }
                else {
                        /* render normal particles */
-                       if(part->trail_count > 1) {
+                       if (part->trail_count > 1) {
                                float length = part->path_end * (1.0f - part->randlength * r_length);
                                int trail_count = part->trail_count * (1.0f - part->randlength * r_length);
                                float ct = (part->draw & PART_ABS_PATH_TIME) ? cfra : pa_time;
@@ -1992,24 +1996,24 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                                /* make sure we have pointcache in memory before getting particle on path */
                                psys_make_temp_pointcache(ob, psys);
 
-                               for(i=0; i < trail_count; i++, ct -= dt) {
-                                       if(part->draw & PART_ABS_PATH_TIME) {
-                                               if(ct < pa_birthtime || ct > pa_dietime)
+                               for (i=0; i < trail_count; i++, ct -= dt) {
+                                       if (part->draw & PART_ABS_PATH_TIME) {
+                                               if (ct < pa_birthtime || ct > pa_dietime)
                                                        continue;
                                        }
-                                       else if(ct < 0.0f || ct > 1.0f)
+                                       else if (ct < 0.0f || ct > 1.0f)
                                                continue;
 
                                        state.time = (part->draw & PART_ABS_PATH_TIME) ? -ct : ct;
-                                       psys_get_particle_on_path(&sim,a,&state,1);
+                                       psys_get_particle_on_path(&sim, a, &state, 1);
 
-                                       if(psys->parent)
+                                       if (psys->parent)
                                                mul_m4_v3(psys->parent->obmat, state.co);
 
-                                       if(use_duplimat)
+                                       if (use_duplimat)
                                                mul_m4_v4(duplimat, state.co);
 
-                                       if(part->ren_as == PART_DRAW_BB) {
+                                       if (part->ren_as == PART_DRAW_BB) {
                                                bb.random = random;
                                                bb.offset[0] = part->bb_offset[0];
                                                bb.offset[1] = part->bb_offset[1];
@@ -2038,16 +2042,16 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                        }
                        else {
                                state.time=cfra;
-                               if(psys_get_particle_state(&sim,a,&state,0)==0)
+                               if (psys_get_particle_state(&sim, a, &state, 0)==0)
                                        continue;
 
-                               if(psys->parent)
+                               if (psys->parent)
                                        mul_m4_v3(psys->parent->obmat, state.co);
 
-                               if(use_duplimat)
+                               if (use_duplimat)
                                        mul_m4_v3(duplimat, state.co);
 
-                               if(part->ren_as == PART_DRAW_BB) {
+                               if (part->ren_as == PART_DRAW_BB) {
                                        bb.random = random;
                                        bb.offset[0] = part->bb_offset[0];
                                        bb.offset[1] = part->bb_offset[1];
@@ -2073,46 +2077,46 @@ static int render_new_particle_system(Render *re, ObjectRen *obr, ParticleSystem
                        }
                }
 
-               if(orco1==0)
+               if (orco1==0)
                        sd.orco+=3;
 
-               if(re->test_break(re->tbh))
+               if (re->test_break(re->tbh))
                        break;
        }
 
-       if(dosurfacecache)
+       if (dosurfacecache)
                strandbuf->surface= cache_strand_surface(re, obr, psmd->dm, mat, timeoffset);
 
 /* 4. clean up */
 #if 0 // XXX old animation system
-       if(ma) do_mat_ipo(re->scene, ma);
+       if (ma) do_mat_ipo(re->scene, ma);
 #endif // XXX old animation system
        
-       if(orco1)
+       if (orco1)
                MEM_freeN(sd.orco);
 
-       if(sd.uvco)
+       if (sd.uvco)
                MEM_freeN(sd.uvco);
        
-       if(sd.mcol)
+       if (sd.mcol)
                MEM_freeN(sd.mcol);
 
-       if(uv_name)
+       if (uv_name)
                MEM_freeN(uv_name);
 
-       if(states)
+       if (states)
                MEM_freeN(states);
        
        rng_free(rng);
 
        psys->flag &= ~PSYS_DRAWING;
 
-       if(psys->lattice){
+       if (psys->lattice) {
                end_latt_deform(psys->lattice);
                psys->lattice= NULL;
        }
 
-       if(path_nbr && (ma->mode_l & MA_TANGENT_STR)==0)
+       if (path_nbr && (ma->mode_l & MA_TANGENT_STR)==0)
                calc_vertexnormals(re, obr, 0, 0);
 
        return 1;
@@ -2135,16 +2139,16 @@ static void make_render_halos(Render *re, ObjectRen *obr, Mesh *UNUSED(me), int
 
        re->flag |= R_HALO;
 
-       for(a=0; a<totvert; a++, mvert++) {
+       for (a=0; a<totvert; a++, mvert++) {
                ok= 1;
 
-               if(ok) {
+               if (ok) {
                        hasize= ma->hasize;
 
                        copy_v3_v3(vec, mvert->co);
                        mul_m4_v3(mat, vec);
 
-                       if(ma->mode & MA_HALOPUNO) {
+                       if (ma->mode & MA_HALOPUNO) {
                                xn= mvert->no[0];
                                yn= mvert->no[1];
                                zn= mvert->no[2];
@@ -2159,15 +2163,15 @@ static void make_render_halos(Render *re, ObjectRen *obr, Mesh *UNUSED(me), int
                                normalize_v3(view);
 
                                zn = dot_v3v3(nor, view);
-                               if(zn>=0.0f) hasize= 0.0f;
+                               if (zn>=0.0f) hasize= 0.0f;
                                else hasize*= zn*zn*zn*zn;
                        }
 
-                       if(orco) har= RE_inithalo(re, obr, ma, vec, NULL, orco, hasize, 0.0, seed);
+                       if (orco) har= RE_inithalo(re, obr, ma, vec, NULL, orco, hasize, 0.0, seed);
                        else har= RE_inithalo(re, obr, ma, vec, NULL, mvert->co, hasize, 0.0, seed);
-                       if(har) har->lay= ob->lay;
+                       if (har) har->lay= ob->lay;
                }
-               if(orco) orco+= 3;
+               if (orco) orco+= 3;
                seed++;
        }
 }
@@ -2177,8 +2181,8 @@ static int verghalo(const void *a1, const void *a2)
        const HaloRen *har1= *(const HaloRen**)a1;
        const HaloRen *har2= *(const HaloRen**)a2;
        
-       if(har1->zs < har2->zs) return 1;
-       else if(har1->zs > har2->zs) return -1;
+       if (har1->zs < har2->zs) return 1;
+       else if (har1->zs > har2->zs) return -1;
        return 0;
 }
 
@@ -2188,14 +2192,14 @@ static void sort_halos(Render *re, int totsort)
        HaloRen *har= NULL, **haso;
        int a;
 
-       if(re->tothalo==0) return;
+       if (re->tothalo==0) return;
 
        re->sortedhalos= MEM_callocN(sizeof(HaloRen*)*re->tothalo, "sorthalos");
        haso= re->sortedhalos;
 
-       for(obr=re->objecttable.first; obr; obr=obr->next) {
-               for(a=0; a<obr->tothalo; a++) {
-                       if((a & 255)==0) har= obr->bloha[a>>8];
+       for (obr=re->objecttable.first; obr; obr=obr->next) {
+               for (a=0; a<obr->tothalo; a++) {
+                       if ((a & 255)==0) har= obr->bloha[a>>8];
                        else har++;
 
                        *(haso++)= har;
@@ -2218,7 +2222,7 @@ static short test_for_displace(Render *re, Object *ob)
        for (i=1; i<=ob->totcol; i++) {
                ma=give_render_material(re, ob, i);
                /* ma->mapto is ORed total of all mapto channels */
-               if(ma && (ma->mapto & MAP_DISPLACE)) return 1;
+               if (ma && (ma->mapto & MAP_DISPLACE)) return 1;
        }
        return 0;
 }
@@ -2236,10 +2240,10 @@ static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, Ve
        /* vertex normal is used for textures type 'col' and 'var' */
        copy_v3_v3(shi->vn, vr->n);
 
-       if(mat)
+       if (mat)
                mul_m4_v3(mat, shi->co);
 
-       if(imat) {
+       if (imat) {
                shi->vn[0] = dot_v3v3(imat[0], vr->n);
                shi->vn[1] = dot_v3v3(imat[1], vr->n);
                shi->vn[2] = dot_v3v3(imat[2], vr->n);
@@ -2267,7 +2271,7 @@ static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, Ve
        }
        if (texco & TEXCO_STICKY) {
                float *sticky= RE_vertren_get_sticky(obr, vr, 0);
-               if(sticky) {
+               if (sticky) {
                        shi->sticky[0]= sticky[0];
                        shi->sticky[1]= sticky[1];
                        shi->sticky[2]= 0.0f;
@@ -2280,15 +2284,15 @@ static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, Ve
        if (texco & TEXCO_NORM) {
                copy_v3_v3(shi->orn, shi->vn);
        }
-       if(texco & TEXCO_REFL) {
+       if (texco & TEXCO_REFL) {
                /* not (yet?) */
        }
-       if(texco & TEXCO_STRESS) {
+       if (texco & TEXCO_STRESS) {
                float *s= RE_vertren_get_stress(obr, vr, 0);
 
-               if(s) {
+               if (s) {
                        shi->stress= *s;
-                       if(shi->stress<1.0f) shi->stress-= 1.0f;
+                       if (shi->stress<1.0f) shi->stress-= 1.0f;
                        else shi->stress= (shi->stress-1.0f)/shi->stress;
                }
                else
@@ -2306,7 +2310,7 @@ static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, Ve
        displace[1]= shi->displace[1] * scale[1];
        displace[2]= shi->displace[2] * scale[2];
        
-       if(mat)
+       if (mat)
                mul_m3_v3(imat, displace);
 
        /* 0.5 could become button once?  */
@@ -2317,7 +2321,7 @@ static void displace_render_vert(Render *re, ObjectRen *obr, ShadeInput *shi, Ve
        //printf("after co=%f, %f, %f\n", vr->co[0], vr->co[1], vr->co[2]); 
        
        /* we just don't do this vertex again, bad luck for other face using same vertex with
-               different material... */
+        * different material... */
        vr->flag |= 1;
        
        /* Pass sample back so displace_face can decide which way to split the quad */
@@ -2335,13 +2339,13 @@ static void displace_render_face(Render *re, ObjectRen *obr, VlakRen *vlr, float
        ShadeInput shi;
 
        /* Warning, This is not that nice, and possibly a bit slow,
-       however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
+        * however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
        memset(&shi, 0, sizeof(ShadeInput)); 
        /* end warning! - Campbell */
        
        /* set up shadeinput struct for multitex() */
        
-       /* memset above means we dont need this */
+       /* memset above means we don't need this */
        /*shi.osatex= 0;*/              /* signal not to use dx[] and dy[] texture AA vectors */
 
        shi.obr= obr;
@@ -2359,7 +2363,7 @@ static void displace_render_face(Render *re, ObjectRen *obr, VlakRen *vlr, float
 
        /* Displace the verts, flag is set when done */
        if (!vlr->v1->flag)
-               displace_render_vert(re, obr, &shi, vlr->v1,0,  scale, mat, imat);
+               displace_render_vert(re, obr, &shi, vlr->v1, 0,  scale, mat, imat);
        
        if (!vlr->v2->flag)
                displace_render_vert(re, obr, &shi, vlr->v2, 1, scale, mat, imat);
@@ -2378,11 +2382,11 @@ static void displace_render_face(Render *re, ObjectRen *obr, VlakRen *vlr, float
        }
        
        /* Recalculate the face normal  - if flipped before, flip now */
-       if(vlr->v4) {
-               normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+       if (vlr->v4) {
+               normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
        }       
        else {
-               normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
+               normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
        }
 }
 
@@ -2397,19 +2401,19 @@ static void do_displacement(Render *re, ObjectRen *obr, float mat[][4], float im
                
        /* Object Size with parenting */
        obt=obr->ob;
-       while(obt){
+       while (obt) {
                mul_v3_v3v3(temp, obt->size, obt->dscale);
                scale[0]*=temp[0]; scale[1]*=temp[1]; scale[2]*=temp[2];
                obt=obt->parent;
        }
        
        /* Clear all flags */
-       for(i=0; i<obr->totvert; i++){ 
+       for (i=0; i<obr->totvert; i++) {
                vr= RE_findOrAddVert(obr, i);
                vr->flag= 0;
        }
 
-       for(i=0; i<obr->totvlak; i++){
+       for (i=0; i<obr->totvlak; i++) {
                vlr=RE_findOrAddVlak(obr, i);
                displace_render_face(re, obr, vlr, scale, mat, imat);
        }
@@ -2430,40 +2434,41 @@ static void init_render_mball(Render *re, ObjectRen *obr)
        VlakRen *vlr, *vlr1;
        Material *ma;
        float *data, *nors, *orco=NULL, mat[4][4], imat[3][3], xn, yn, zn;
-       int a, need_orco, vlakindex, *index;
+       int a, need_orco, vlakindex, *index, negative_scale;
        ListBase dispbase= {NULL, NULL};
 
-       if (ob!=find_basis_mball(re->scene, ob))
+       if (ob!=BKE_metaball_basis_find(re->scene, ob))
                return;
 
        mult_m4_m4m4(mat, re->viewmat, ob->obmat);
        invert_m4_m4(ob->imat, mat);
        copy_m3_m4(imat, ob->imat);
+       negative_scale = is_negative_m4(mat);
 
        ma= give_render_material(re, ob, 1);
 
        need_orco= 0;
-       if(ma->texco & TEXCO_ORCO) {
+       if (ma->texco & TEXCO_ORCO) {
                need_orco= 1;
        }
 
        makeDispListMBall_forRender(re->scene, ob, &dispbase);
        dl= dispbase.first;
-       if(dl==0) return;
+       if (dl==0) return;
 
        data= dl->verts;
        nors= dl->nors;
-       if(need_orco) {
+       if (need_orco) {
                orco= get_object_orco(re, ob);
 
                if (!orco) {
                        /* orco hasn't been found in cache - create new one and add to cache */
-                       orco= make_orco_mball(ob, &dispbase);
+                       orco= BKE_metaball_make_orco(ob, &dispbase);
                        set_object_orco(re, ob, orco);
                }
        }
 
-       for(a=0; a<dl->nr; a++, data+=3, nors+=3) {
+       for (a=0; a<dl->nr; a++, data+=3, nors+=3) {
 
                ver= RE_findOrAddVert(obr, obr->totvert++);
                copy_v3_v3(ver->co, data);
@@ -2479,16 +2484,16 @@ static void init_render_mball(Render *re, ObjectRen *obr)
                ver->n[1]= imat[1][0]*xn+imat[1][1]*yn+imat[1][2]*zn;
                ver->n[2]= imat[2][0]*xn+imat[2][1]*yn+imat[2][2]*zn;
                normalize_v3(ver->n);
-               //if(ob->transflag & OB_NEG_SCALE) negate_v3(ver->n);
+               //if (ob->transflag & OB_NEG_SCALE) negate_v3(ver->n);
                
-               if(need_orco) {
+               if (need_orco) {
                        ver->orco= orco;
                        orco+=3;
                }
        }
 
        index= dl->index;
-       for(a=0; a<dl->parts; a++, index+=4) {
+       for (a=0; a<dl->parts; a++, index+=4) {
 
                vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                vlr->v1= RE_findOrAddVert(obr, index[0]);
@@ -2496,27 +2501,27 @@ static void init_render_mball(Render *re, ObjectRen *obr)
                vlr->v3= RE_findOrAddVert(obr, index[2]);
                vlr->v4= 0;
 
-               if(ob->transflag & OB_NEG_SCALE) 
-                       normal_tri_v3( vlr->n,vlr->v1->co, vlr->v2->co, vlr->v3->co);
+               if (negative_scale)
+                       normal_tri_v3(vlr->n, vlr->v1->co, vlr->v2->co, vlr->v3->co);
                else
-                       normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                       normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
 
                vlr->mat= ma;
                vlr->flag= ME_SMOOTH;
                vlr->ec= 0;
 
                /* mball -too bad- always has triangles, because quads can be non-planar */
-               if(index[3] && index[3]!=index[2]) {
+               if (index[3] && index[3]!=index[2]) {
                        vlr1= RE_findOrAddVlak(obr, obr->totvlak++);
                        vlakindex= vlr1->index;
                        *vlr1= *vlr;
                        vlr1->index= vlakindex;
                        vlr1->v2= vlr1->v3;
                        vlr1->v3= RE_findOrAddVert(obr, index[3]);
-                       if(ob->transflag & OB_NEG_SCALE) 
-                               normal_tri_v3( vlr1->n,vlr1->v1->co, vlr1->v2->co, vlr1->v3->co);
+                       if (negative_scale)
+                               normal_tri_v3(vlr1->n, vlr1->v1->co, vlr1->v2->co, vlr1->v3->co);
                        else
-                               normal_tri_v3( vlr1->n,vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
+                               normal_tri_v3(vlr1->n, vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
                }
        }
 
@@ -2546,7 +2551,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
        for (u = 0; u < sizeu; u++) {
                v1 = RE_findOrAddVert(obr, obr->totvert++); /* save this for possible V wrapping */
                copy_v3_v3(v1->co, data); data += 3;
-               if(orco) {
+               if (orco) {
                        v1->orco= orco; orco+= 3; orcoret++;
                }       
                mul_m4_v3(mat, v1->co);
@@ -2554,7 +2559,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
                for (v = 1; v < sizev; v++) {
                        ver= RE_findOrAddVert(obr, obr->totvert++);
                        copy_v3_v3(ver->co, data); data += 3;
-                       if(orco) {
+                       if (orco) {
                                ver->orco= orco; orco+= 3; orcoret++;
                        }       
                        mul_m4_v3(mat, ver->co);
@@ -2563,7 +2568,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
                if (dl->flag & DL_CYCL_U) {
                        ver= RE_findOrAddVert(obr, obr->totvert++);
                        copy_v3_v3(ver->co, v1->co);
-                       if(orco) {
+                       if (orco) {
                                ver->orco= orco; orco+=3; orcoret++; //orcobase + 3*(u*sizev + 0);
                        }
                }       
@@ -2579,7 +2584,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
                        v1= RE_findOrAddVert(obr, startvert + v);
                        ver= RE_findOrAddVert(obr, obr->totvert++);
                        copy_v3_v3(ver->co, v1->co);
-                       if(orco) {
+                       if (orco) {
                                ver->orco= orco; orco+=3; orcoret++; //ver->orco= orcobase + 3*(0*sizev + v);
                        }
                }
@@ -2590,13 +2595,13 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
        
        startvlak= obr->totvlak;
        
-       for(u = 0; u < sizeu - 1; u++) {
+       for (u = 0; u < sizeu - 1; u++) {
                p1 = startvert + u * sizev; /* walk through face list */
                p2 = p1 + 1;
                p3 = p2 + sizev;
                p4 = p3 - 1;
                
-               for(v = 0; v < sizev - 1; v++) {
+               for (v = 0; v < sizev - 1; v++) {
                        v1= RE_findOrAddVert(obr, p1);
                        v2= RE_findOrAddVert(obr, p2);
                        v3= RE_findOrAddVert(obr, p3);
@@ -2605,7 +2610,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
                        vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                        vlr->v1= v1; vlr->v2= v2; vlr->v3= v3; vlr->v4= v4;
                        
-                       normal_quad_v3( n1,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                       normal_quad_v3(n1, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                        
                        copy_v3_v3(vlr->n, n1);
                        
@@ -2625,8 +2630,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
        sizeu--; sizev--;  /* dec size for face array */
        if (dl->flag & DL_CYCL_V) {
                
-               for (v = 0; v < sizev; v++)
-               {
+               for (v = 0; v < sizev; v++) {
                        /* optimize! :*/
                        vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, v));
                        vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0, v));
@@ -2638,8 +2642,7 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
        }
        if (dl->flag & DL_CYCL_U) {
                
-               for (u = 0; u < sizeu; u++)
-               {
+               for (u = 0; u < sizeu; u++) {
                        /* optimize! :*/
                        vlr= RE_findOrAddVlak(obr, UVTOINDEX(u, 0));
                        vlr1= RE_findOrAddVlak(obr, UVTOINDEX(u, sizev-1));
@@ -2649,38 +2652,38 @@ static int dl_surf_to_renderdata(ObjectRen *obr, DispList *dl, Material **matar,
                        add_v3_v3(vlr->v4->n, vlr1->n);
                }
        }
+
        /* last vertex is an extra case: 
-               
-               ^       ()----()----()----()
-               |       |     |     ||     |
-               u       |     |(0,n)||(0,0)|
-               |     |     ||     |
-               ()====()====[]====()
-               |     |     ||     |
-               |     |(m,n)||(m,0)|
-               |     |     ||     |
-               ()----()----()----()
-               v ->
-               
-               vertex [] is no longer shared, therefore distribute
-               normals of the surrounding faces to all of the duplicates of []
-               */
-       
-       if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U))
-       {
-               vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, sizev - 1)); /* (m,n) */
-               vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0,0));  /* (0,0) */
+        *
+        *     ^     ()----()----()----()
+        *     |     |     |     ||     |
+        *     u     |     |(0,n)||(0,0)|
+        *     |     |     ||     |
+        *     ()====()====[]====()
+        *     |     |     ||     |
+        *     |     |(m,n)||(m,0)|
+        *     |     |     ||     |
+        *     ()----()----()----()
+        *     v ->
+        *  
+        *  vertex [] is no longer shared, therefore distribute
+        *  normals of the surrounding faces to all of the duplicates of []
+        */
+
+       if ((dl->flag & DL_CYCL_V) && (dl->flag & DL_CYCL_U)) {
+               vlr= RE_findOrAddVlak(obr, UVTOINDEX(sizeu - 1, sizev - 1)); /* (m, n) */
+               vlr1= RE_findOrAddVlak(obr, UVTOINDEX(0, 0));  /* (0, 0) */
                add_v3_v3v3(n1, vlr->n, vlr1->n);
-               vlr2= RE_findOrAddVlak(obr, UVTOINDEX(0, sizev-1)); /* (0,n) */
+               vlr2= RE_findOrAddVlak(obr, UVTOINDEX(0, sizev-1)); /* (0, n) */
                add_v3_v3(n1, vlr2->n);
-               vlr3= RE_findOrAddVlak(obr, UVTOINDEX(sizeu-1, 0)); /* (m,0) */
+               vlr3= RE_findOrAddVlak(obr, UVTOINDEX(sizeu-1, 0)); /* (m, 0) */
                add_v3_v3(n1, vlr3->n);
                copy_v3_v3(vlr->v3->n, n1);
                copy_v3_v3(vlr1->v1->n, n1);
                copy_v3_v3(vlr2->v2->n, n1);
                copy_v3_v3(vlr3->v4->n, n1);
        }
-       for(a = startvert; a < obr->totvert; a++) {
+       for (a = startvert; a < obr->totvert; a++) {
                ver= RE_findOrAddVert(obr, a);
                normalize_v3(ver->n);
        }
@@ -2705,18 +2708,18 @@ static void init_render_dm(DerivedMesh *dm, Render *re, ObjectRen *obr,
        mvert= dm->getVertArray(dm);
        totvert= dm->getNumVerts(dm);
 
-       for(a=0; a<totvert; a++, mvert++) {
+       for (a=0; a<totvert; a++, mvert++) {
                ver= RE_findOrAddVert(obr, obr->totvert++);
                copy_v3_v3(ver->co, mvert->co);
                mul_m4_v3(mat, ver->co);
 
-               if(orco) {
+               if (orco) {
                        ver->orco= orco;
                        orco+=3;
                }
        }
 
-       if(!timeoffset) {
+       if (!timeoffset) {
                /* store customdata names, because DerivedMesh is freed */
                RE_set_customdata_names(obr, &dm->faceData);
 
@@ -2724,16 +2727,16 @@ static void init_render_dm(DerivedMesh *dm, Render *re, ObjectRen *obr,
 
                /* faces in order of color blocks */
                vertofs= obr->totvert - totvert;
-               for(mat_iter= 0; (mat_iter < ob->totcol || (mat_iter==0 && ob->totcol==0)); mat_iter++) {
+               for (mat_iter= 0; (mat_iter < ob->totcol || (mat_iter==0 && ob->totcol==0)); mat_iter++) {
 
                        ma= give_render_material(re, ob, mat_iter+1);
                        end= dm->getNumTessFaces(dm);
                        mface= dm->getTessFaceArray(dm);
 
-                       for(a=0; a<end; a++, mface++) {
+                       for (a=0; a<end; a++, mface++) {
                                int v1, v2, v3, v4, flag;
 
-                               if(mface->mat_nr == mat_iter) {
+                               if (mface->mat_nr == mat_iter) {
                                        float len;
 
                                        v1= mface->v1;
@@ -2746,20 +2749,20 @@ static void init_render_dm(DerivedMesh *dm, Render *re, ObjectRen *obr,
                                        vlr->v1= RE_findOrAddVert(obr, vertofs+v1);
                                        vlr->v2= RE_findOrAddVert(obr, vertofs+v2);
                                        vlr->v3= RE_findOrAddVert(obr, vertofs+v3);
-                                       if(v4) vlr->v4= RE_findOrAddVert(obr, vertofs+v4);
+                                       if (v4) vlr->v4= RE_findOrAddVert(obr, vertofs+v4);
                                        else vlr->v4= 0;
 
                                        /* render normals are inverted in render */
-                                       if(vlr->v4)
-                                               len= normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                                       if (vlr->v4)
+                                               len= normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                                        else
-                                               len= normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                                               len= normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
 
                                        vlr->mat= ma;
                                        vlr->flag= flag;
                                        vlr->ec= 0; /* mesh edges rendered separately */
 
-                                       if(len==0) obr->totvlak--;
+                                       if (len==0) obr->totvlak--;
                                        else {
                                                CustomDataLayer *layer;
                                                MTFace *mtface, *mtf;
@@ -2767,16 +2770,16 @@ static void init_render_dm(DerivedMesh *dm, Render *re, ObjectRen *obr,
                                                int index, mtfn= 0, mcn= 0;
                                                char *name;
 
-                                               for(index=0; index<dm->faceData.totlayer; index++) {
+                                               for (index=0; index<dm->faceData.totlayer; index++) {
                                                        layer= &dm->faceData.layers[index];
                                                        name= layer->name;
 
-                                                       if(layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
+                                                       if (layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
                                                                mtf= RE_vlakren_get_tface(obr, vlr, mtfn++, &name, 1);
                                                                mtface= (MTFace*)layer->data;
                                                                *mtf= mtface[a];
                                                        }
-                                                       else if(layer->type == CD_MCOL && mcn < MAX_MCOL) {
+                                                       else if (layer->type == CD_MCOL && mcn < MAX_MCOL) {
                                                                mc= RE_vlakren_get_mcol(obr, vlr, mcn++, &name, 1);
                                                                mcol= (MCol*)layer->data;
                                                                memcpy(mc, &mcol[a*4], sizeof(MCol)*4);
@@ -2807,7 +2810,7 @@ static void init_render_surf(Render *re, ObjectRen *obr, int timeoffset)
 
        cu= ob->data;
        nu= cu->nurb.first;
-       if(nu==0) return;
+       if (nu==0) return;
 
        mult_m4_m4m4(mat, re->viewmat, ob->obmat);
        invert_m4_m4(ob->imat, mat);
@@ -2816,36 +2819,37 @@ static void init_render_surf(Render *re, ObjectRen *obr, int timeoffset)
        totmat= ob->totcol+1;
        matar= MEM_callocN(sizeof(Material*)*totmat, "init_render_surf matar");
 
-       for(a=0; a<totmat; a++) {
+       for (a=0; a<totmat; a++) {
                matar[a]= give_render_material(re, ob, a+1);
 
-               if(matar[a] && matar[a]->texco & TEXCO_ORCO)
+               if (matar[a] && matar[a]->texco & TEXCO_ORCO)
                        need_orco= 1;
        }
 
-       if(ob->parent && (ob->parent->type==OB_LATTICE)) need_orco= 1;
+       if (ob->parent && (ob->parent->type==OB_LATTICE)) need_orco= 1;
 
        makeDispListSurf(re->scene, ob, &displist, &dm, 1, 0);
 
        if (dm) {
-               if(need_orco) {
+               if (need_orco) {
                        orco= makeOrcoDispList(re->scene, ob, dm, 1);
-                       if(orco) {
+                       if (orco) {
                                set_object_orco(re, ob, orco);
                        }
                }
 
                init_render_dm(dm, re, obr, timeoffset, orco, mat);
                dm->release(dm);
-       } else {
-               if(need_orco) {
+       }
+       else {
+               if (need_orco) {
                        orco= get_object_orco(re, ob);
                }
 
                /* walk along displaylist and create rendervertices/-faces */
-               for(dl=displist.first; dl; dl=dl->next) {
+               for (dl=displist.first; dl; dl=dl->next) {
                        /* watch out: u ^= y, v ^= x !! */
-                       if(dl->type==DL_SURF)
+                       if (dl->type==DL_SURF)
                                orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
                }
        }
@@ -2871,12 +2875,12 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
        int need_orco=0, totmat;
 
        cu= ob->data;
-       if(ob->type==OB_FONT && cu->str==NULL) return;
-       else if(ob->type==OB_CURVE && cu->nurb.first==NULL) return;
+       if (ob->type==OB_FONT && cu->str==NULL) return;
+       else if (ob->type==OB_CURVE && cu->nurb.first==NULL) return;
 
        makeDispListCurveTypes_forRender(re->scene, ob, &disp, &dm, 0);
        dl= disp.first;
-       if(dl==NULL) return;
+       if (dl==NULL) return;
        
        mult_m4_m4m4(mat, re->viewmat, ob->obmat);
        invert_m4_m4(ob->imat, mat);
@@ -2885,39 +2889,40 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
        totmat= ob->totcol+1;
        matar= MEM_callocN(sizeof(Material*)*totmat, "init_render_surf matar");
 
-       for(a=0; a<totmat; a++) {
+       for (a=0; a<totmat; a++) {
                matar[a]= give_render_material(re, ob, a+1);
 
-               if(matar[a] && matar[a]->texco & TEXCO_ORCO)
+               if (matar[a] && matar[a]->texco & TEXCO_ORCO)
                        need_orco= 1;
        }
 
        if (dm) {
-               if(need_orco) {
+               if (need_orco) {
                        orco= makeOrcoDispList(re->scene, ob, dm, 1);
-                       if(orco) {
+                       if (orco) {
                                set_object_orco(re, ob, orco);
                        }
                }
 
                init_render_dm(dm, re, obr, timeoffset, orco, mat);
                dm->release(dm);
-       } else {
-               if(need_orco) {
+       }
+       else {
+               if (need_orco) {
                  orco= get_object_orco(re, ob);
                }
 
-               while(dl) {
-                       if(dl->col > ob->totcol) {
+               while (dl) {
+                       if (dl->col > ob->totcol) {
                                /* pass */
                        }
-                       else if(dl->type==DL_INDEX3) {
+                       else if (dl->type==DL_INDEX3) {
                                int *index;
 
                                startvert= obr->totvert;
                                data= dl->verts;
 
-                               for(a=0; a<dl->nr; a++, data+=3) {
+                               for (a=0; a<dl->nr; a++, data+=3) {
                                        ver= RE_findOrAddVert(obr, obr->totvert++);
                                        copy_v3_v3(ver->co, data);
 
@@ -2929,20 +2934,20 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                        }
                                }
 
-                               if(timeoffset==0) {
+                               if (timeoffset==0) {
                                        float tmp[3];
                                        const int startvlak= obr->totvlak;
 
                                        zero_v3(n);
                                        index= dl->index;
-                                       for(a=0; a<dl->parts; a++, index+=3) {
+                                       for (a=0; a<dl->parts; a++, index+=3) {
                                                vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                                                vlr->v1= RE_findOrAddVert(obr, startvert+index[0]);
                                                vlr->v2= RE_findOrAddVert(obr, startvert+index[1]);
                                                vlr->v3= RE_findOrAddVert(obr, startvert+index[2]);
                                                vlr->v4= NULL;
 
-                                               if(area_tri_v3(vlr->v3->co, vlr->v2->co, vlr->v1->co)>FLT_EPSILON) {
+                                               if (area_tri_v3(vlr->v3->co, vlr->v2->co, vlr->v1->co)>FLT_EPSILON) {
                                                        normal_tri_v3(tmp, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                                                        add_v3_v3(n, tmp);
                                                }
@@ -2955,7 +2960,7 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                        normalize_v3(n);
 
                                        /* vertex normals */
-                                       for(a= startvlak; a<obr->totvlak; a++) {
+                                       for (a= startvlak; a<obr->totvlak; a++) {
                                                vlr= RE_findOrAddVlak(obr, a);
 
                                                copy_v3_v3(vlr->n, n);
@@ -2963,7 +2968,7 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                                add_v3_v3(vlr->v3->n, vlr->n);
                                                add_v3_v3(vlr->v2->n, vlr->n);
                                        }
-                                       for(a=startvert; a<obr->totvert; a++) {
+                                       for (a=startvert; a<obr->totvert; a++) {
                                                ver= RE_findOrAddVert(obr, a);
                                                normalize_v3(ver->n);
                                        }
@@ -2976,13 +2981,13 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                        orco+= 3*dl_surf_to_renderdata(obr, dl, matar, orco, mat);
                                }
                                else {
-                                       int p1,p2,p3,p4;
+                                       int p1, p2, p3, p4;
 
                                        fp= dl->verts;
                                        startvert= obr->totvert;
                                        nr= dl->nr*dl->parts;
 
-                                       while(nr--) {
+                                       while (nr--) {
                                                ver= RE_findOrAddVert(obr, obr->totvert++);
 
                                                copy_v3_v3(ver->co, fp);
@@ -2995,10 +3000,10 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                                }
                                        }
 
-                                       if(dl->bevelSplitFlag || timeoffset==0) {
+                                       if (dl->bevelSplitFlag || timeoffset==0) {
                                                const int startvlak= obr->totvlak;
 
-                                               for(a=0; a<dl->parts; a++) {
+                                               for (a=0; a<dl->parts; a++) {
 
                                                        if (surfindex_displist(dl, a, &b, &p1, &p2, &p3, &p4)==0)
                                                                break;
@@ -3008,7 +3013,7 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                                        p3+= startvert;
                                                        p4+= startvert;
 
-                                                       for(; b<dl->nr; b++) {
+                                                       for (; b<dl->nr; b++) {
                                                                vlr= RE_findOrAddVlak(obr, obr->totvlak++);
                                                                /* important 1 offset in order is kept [#24913] */
                                                                vlr->v1= RE_findOrAddVert(obr, p2);
@@ -3016,7 +3021,7 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                                                vlr->v3= RE_findOrAddVert(obr, p3);
                                                                vlr->v4= RE_findOrAddVert(obr, p4);
                                                                vlr->ec= ME_V2V3+ME_V3V4;
-                                                               if(a==0) vlr->ec+= ME_V1V2;
+                                                               if (a==0) vlr->ec+= ME_V1V2;
 
                                                                vlr->flag= dl->rt;
 
@@ -3031,13 +3036,13 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                                }
 
                                                if (dl->bevelSplitFlag) {
-                                                       for(a=0; a<dl->parts-1+!!(dl->flag&DL_CYCL_V); a++)
-                                                               if(dl->bevelSplitFlag[a>>5]&(1<<(a&0x1F)))
+                                                       for (a=0; a<dl->parts-1+!!(dl->flag&DL_CYCL_V); a++)
+                                                               if (dl->bevelSplitFlag[a>>5]&(1<<(a&0x1F)))
                                                                        split_v_renderfaces(obr, startvlak, startvert, dl->parts, dl->nr, a, dl->flag&DL_CYCL_V, dl->flag&DL_CYCL_U);
                                                }
 
                                                /* vertex normals */
-                                               for(a= startvlak; a<obr->totvlak; a++) {
+                                               for (a= startvlak; a<obr->totvlak; a++) {
                                                        vlr= RE_findOrAddVlak(obr, a);
 
                                                        add_v3_v3(vlr->v1->n, vlr->n);
@@ -3045,7 +3050,7 @@ static void init_render_curve(Render *re, ObjectRen *obr, int timeoffset)
                                                        add_v3_v3(vlr->v2->n, vlr->n);
                                                        add_v3_v3(vlr->v4->n, vlr->n);
                                                }
-                                               for(a=startvert; a<obr->totvert; a++) {
+                                               for (a=startvert; a<obr->totvert; a++) {
                                                        ver= RE_findOrAddVert(obr, a);
                                                        normalize_v3(ver->n);
                                                }
@@ -3093,10 +3098,10 @@ static int vergedgesort(const void *v1, const void *v2)
 {
        const struct edgesort *x1=v1, *x2=v2;
 
-       if( x1->v1 > x2->v1) return 1;
-       else if( x1->v1 < x2->v1) return -1;
-       else if( x1->v2 > x2->v2) return 1;
-       else if( x1->v2 < x2->v2) return -1;
+       if ( x1->v1 > x2->v1) return 1;
+       else if ( x1->v1 < x2->v1) return -1;
+       else if ( x1->v2 > x2->v2) return 1;
+       else if ( x1->v2 < x2->v2) return -1;
 
        return 0;
 }
@@ -3114,27 +3119,27 @@ static struct edgesort *make_mesh_edge_lookup(DerivedMesh *dm, int *totedgesort)
        tface= dm->getTessFaceDataArray(dm, CD_MTFACE);
        mcol= dm->getTessFaceDataArray(dm, CD_MCOL);
 
-       if(mcol==NULL && tface==NULL) return NULL;
+       if (mcol==NULL && tface==NULL) return NULL;
 
        /* make sorted table with edges and face indices in it */
-       for(a= totface, mf= mface; a>0; a--, mf++) {
-               if(mf->v4) totedge+=4;
-               else if(mf->v3) totedge+=3;
+       for (a= totface, mf= mface; a>0; a--, mf++) {
+               if (mf->v4) totedge+=4;
+               else if (mf->v3) totedge+=3;
        }
 
-       if(totedge==0)
+       if (totedge==0)
                return NULL;
 
        ed= edsort= MEM_callocN(totedge*sizeof(struct edgesort), "edgesort");
 
-       for(a=0, mf=mface; a<totface; a++, mf++) {
+       for (a=0, mf=mface; a<totface; a++, mf++) {
                to_edgesort(ed++, 0, 1, mf->v1, mf->v2, a);
                to_edgesort(ed++, 1, 2, mf->v2, mf->v3, a);
-               if(mf->v4) {
+               if (mf->v4) {
                        to_edgesort(ed++, 2, 3, mf->v3, mf->v4, a);
                        to_edgesort(ed++, 3, 0, mf->v4, mf->v1, a);
                }
-               else if(mf->v3)
+               else if (mf->v3)
                        to_edgesort(ed++, 2, 3, mf->v3, mf->v1, a);
        }
 
@@ -3154,7 +3159,7 @@ static void use_mesh_edge_lookup(ObjectRen *obr, DerivedMesh *dm, MEdge *medge,
        int index, mtfn, mcn;
        char *name;
 
-       if(medge->v1 < medge->v2) {
+       if (medge->v1 < medge->v2) {
                ed.v1= medge->v1;
                ed.v2= medge->v2;
        }
@@ -3166,14 +3171,14 @@ static void use_mesh_edge_lookup(ObjectRen *obr, DerivedMesh *dm, MEdge *medge,
        edp= bsearch(&ed, edgetable, totedge, sizeof(struct edgesort), vergedgesort);
 
        /* since edges have different index ordering, we have to duplicate mcol and tface */
-       if(edp) {
+       if (edp) {
                mtfn= mcn= 0;
 
-               for(index=0; index<dm->faceData.totlayer; index++) {
+               for (index=0; index<dm->faceData.totlayer; index++) {
                        layer= &dm->faceData.layers[index];
                        name= layer->name;
 
-                       if(layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
+                       if (layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
                                mtface= &((MTFace*)layer->data)[edp->f];
                                mtf= RE_vlakren_get_tface(obr, vlr, mtfn++, &name, 1);
 
@@ -3184,7 +3189,7 @@ static void use_mesh_edge_lookup(ObjectRen *obr, DerivedMesh *dm, MEdge *medge,
                                memcpy(mtf->uv[2], mtface->uv[1], sizeof(float)*2);
                                memcpy(mtf->uv[3], mtface->uv[1], sizeof(float)*2);
                        }
-                       else if(layer->type == CD_MCOL && mcn < MAX_MCOL) {
+                       else if (layer->type == CD_MCOL && mcn < MAX_MCOL) {
                                mcol= &((MCol*)layer->data)[edp->f*4];
                                mc= RE_vlakren_get_mcol(obr, vlr, mcn++, &name, 1);
 
@@ -3206,8 +3211,8 @@ static void init_camera_inside_volumes(Render *re)
        VolumeOb *vo;
        float co[3] = {0.f, 0.f, 0.f};
 
-       for(vo= re->volumes.first; vo; vo= vo->next) {
-               for(obi= re->instancetable.first; obi; obi= obi->next) {
+       for (vo= re->volumes.first; vo; vo= vo->next) {
+               for (obi= re->instancetable.first; obi; obi= obi->next) {
                        if (obi->obr == vo->obr) {
                                if (point_inside_volume_objectinstance(re, obi, co)) {
                                        MatInside *mi;
@@ -3225,7 +3230,7 @@ static void init_camera_inside_volumes(Render *re)
        /* debug {
        MatInside *m;
        for (m=re->render_volumes_inside.first; m; m=m->next) {
-               printf("matinside: ma: %s \n", m->ma->id.name+2);
+               printf("matinside: ma: %s\n", m->ma->id.name+2);
        }
        }*/
 }
@@ -3270,25 +3275,25 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
        copy_m3_m4(imat, ob->imat);
        negative_scale= is_negative_m4(mat);
 
-       if(me->totvert==0)
+       if (me->totvert==0)
                return;
        
        need_orco= 0;
-       for(a=1; a<=ob->totcol; a++) {
+       for (a=1; a<=ob->totcol; a++) {
                ma= give_render_material(re, ob, a);
-               if(ma) {
-                       if(ma->texco & (TEXCO_ORCO|TEXCO_STRESS))
+               if (ma) {
+                       if (ma->texco & (TEXCO_ORCO|TEXCO_STRESS))
                                need_orco= 1;
-                       if(ma->texco & TEXCO_STRESS)
+                       if (ma->texco & TEXCO_STRESS)
                                need_stress= 1;
                        /* normalmaps, test if tangents needed, separated from shading */
-                       if(ma->mode_l & MA_TANGENT_V) {
+                       if (ma->mode_l & MA_TANGENT_V) {
                                need_tangent= 1;
-                               if(me->mtface==NULL)
+                               if (me->mtface==NULL)
                                        need_orco= 1;
                        }
-                       if(ma->mode_l & MA_NORMAP_TANG) {
-                               if(me->mtface==NULL) {
+                       if (ma->mode_l & MA_NORMAP_TANG) {
+                               if (me->mtface==NULL) {
                                        need_orco= 1;
                                        need_tangent= 1;
                                }
@@ -3297,9 +3302,9 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                }
        }
 
-       if(re->flag & R_NEED_TANGENT) {
+       if (re->flag & R_NEED_TANGENT) {
                /* exception for tangent space baking */
-               if(me->mtface==NULL) {
+               if (me->mtface==NULL) {
                        need_orco= 1;
                        need_tangent= 1;
                }
@@ -3308,22 +3313,22 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
        
        /* check autosmooth and displacement, we then have to skip only-verts optimize */
        do_autosmooth |= (me->flag & ME_AUTOSMOOTH);
-       if(do_autosmooth)
+       if (do_autosmooth)
                timeoffset= 0;
-       if(test_for_displace(re, ob ) )
+       if (test_for_displace(re, ob ) )
                timeoffset= 0;
        
        mask= CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL;
-       if(!timeoffset)
-               if(need_orco)
+       if (!timeoffset)
+               if (need_orco)
                        mask |= CD_MASK_ORCO;
 
        dm= mesh_create_derived_render(re->scene, ob, mask);
-       if(dm==NULL) return;    /* in case duplicated object fails? */
+       if (dm==NULL) return;   /* in case duplicated object fails? */
 
-       if(mask & CD_MASK_ORCO) {
+       if (mask & CD_MASK_ORCO) {
                orco= dm->getVertDataArray(dm, CD_ORCO);
-               if(orco) {
+               if (orco) {
                        orco= MEM_dupallocN(orco);
                        set_object_orco(re, ob, orco);
                }
@@ -3333,7 +3338,7 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
        totvert= dm->getNumVerts(dm);
 
        /* attempt to autsmooth on original mesh, only without subsurf */
-       if(do_autosmooth && me->totvert==totvert && me->totface==dm->getNumTessFaces(dm))
+       if (do_autosmooth && me->totvert==totvert && me->totface==dm->getNumTessFaces(dm))
                use_original_normals= 1;
        
        ms = (totvert==me->totvert)?me->msticky:NULL;
@@ -3341,15 +3346,15 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
        ma= give_render_material(re, ob, 1);
 
 
-       if(ma->material_type == MA_TYPE_HALO) {
+       if (ma->material_type == MA_TYPE_HALO) {
                make_render_halos(re, obr, me, totvert, mvert, ma, orco);
        }
        else {
 
-               for(a=0; a<totvert; a++, mvert++) {
+               for (a=0; a<totvert; a++, mvert++) {
                        ver= RE_findOrAddVert(obr, obr->totvert++);
                        copy_v3_v3(ver->co, mvert->co);
-                       if(do_autosmooth==0) {  /* autosmooth on original unrotated data to prevent differences between frames */
+                       if (do_autosmooth==0) { /* autosmooth on original unrotated data to prevent differences between frames */
                                normal_short_to_float_v3(ver->n, mvert->no);
                                mul_m4_v3(mat, ver->co);
                                mul_transposed_m3_v3(imat, ver->n);
@@ -3357,60 +3362,59 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                                negate_v3(ver->n);
                        }
   
-                       if(orco) {
+                       if (orco) {
                                ver->orco= orco;
                                orco+=3;
                        }
-                       if(ms) {
+                       if (ms) {
                                float *sticky= RE_vertren_get_sticky(obr, ver, 1);
-                               sticky[0]= ms->co[0];
-                               sticky[1]= ms->co[1];
+                               copy_v2_v2(sticky, ms->co);
                                ms++;
                        }
                }
                
-               if(!timeoffset) {
+               if (!timeoffset) {
                        /* store customdata names, because DerivedMesh is freed */
                        RE_set_customdata_names(obr, &dm->faceData);
 
                        /* add tangent layer if we need one */
-                       if(need_nmap_tangent!=0 && CustomData_get_layer_index(&dm->faceData, CD_TANGENT) == -1)
+                       if (need_nmap_tangent!=0 && CustomData_get_layer_index(&dm->faceData, CD_TANGENT) == -1)
                                DM_add_tangent_layer(dm);
                        
                        /* still to do for keys: the correct local texture coordinate */
 
                        /* faces in order of color blocks */
                        vertofs= obr->totvert - totvert;
-                       for(a1=0; (a1<ob->totcol || (a1==0 && ob->totcol==0)); a1++) {
+                       for (a1=0; (a1<ob->totcol || (a1==0 && ob->totcol==0)); a1++) {
 
                                ma= give_render_material(re, ob, a1+1);
                                
                                /* test for 100% transparant */
                                ok= 1;
-                               if(ma->alpha==0.0f && ma->spectra==0.0f && ma->filter==0.0f && (ma->mode & MA_TRANSP) && (ma->mode & MA_RAYMIRROR)==0) { 
+                               if (ma->alpha==0.0f && ma->spectra==0.0f && ma->filter==0.0f && (ma->mode & MA_TRANSP) && (ma->mode & MA_RAYMIRROR)==0) {
                                        ok= 0;
                                        /* texture on transparency? */
-                                       for(a=0; a<MAX_MTEX; a++) {
-                                               if(ma->mtex[a] && ma->mtex[a]->tex) {
-                                                       if(ma->mtex[a]->mapto & MAP_ALPHA) ok= 1;
+                                       for (a=0; a<MAX_MTEX; a++) {
+                                               if (ma->mtex[a] && ma->mtex[a]->tex) {
+                                                       if (ma->mtex[a]->mapto & MAP_ALPHA) ok= 1;
                                                }
                                        }
                                }
                                
                                /* if wire material, and we got edges, don't do the faces */
-                               if(ma->material_type == MA_TYPE_WIRE) {
+                               if (ma->material_type == MA_TYPE_WIRE) {
                                        end= dm->getNumEdges(dm);
-                                       if(end) ok= 0;
+                                       if (end) ok= 0;
                                }
 
-                               if(ok) {
+                               if (ok) {
                                        end= dm->getNumTessFaces(dm);
                                        mface= dm->getTessFaceArray(dm);
                                        
-                                       for(a=0; a<end; a++, mface++) {
+                                       for (a=0; a<end; a++, mface++) {
                                                int v1, v2, v3, v4, flag;
                                                
-                                               if( mface->mat_nr==a1 ) {
+                                               if ( mface->mat_nr==a1 ) {
                                                        float len;
                                                        int reverse_verts = negative_scale!=0 && do_autosmooth==0;
                                                        int rev_tab[] = {reverse_verts==0 ? 0 : 2, 1, reverse_verts==0 ? 2 : 0, 3};
@@ -3424,31 +3428,31 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                                                        vlr->v1= RE_findOrAddVert(obr, vertofs+v1);
                                                        vlr->v2= RE_findOrAddVert(obr, vertofs+v2);
                                                        vlr->v3= RE_findOrAddVert(obr, vertofs+v3);
-                                                       if(v4) vlr->v4= RE_findOrAddVert(obr, vertofs+v4);
+                                                       if (v4) vlr->v4= RE_findOrAddVert(obr, vertofs+v4);
                                                        else vlr->v4= 0;
 
                                                        /* render normals are inverted in render */
-                                                       if(use_original_normals) {
+                                                       if (use_original_normals) {
                                                                MFace *mf= me->mface+a;
                                                                MVert *mv= me->mvert;
                                                                
-                                                               if(vlr->v4) 
-                                                                       len= normal_quad_v3( vlr->n, mv[mf->v4].co, mv[mf->v3].co, mv[mf->v2].co, mv[mf->v1].co);
+                                                               if (vlr->v4)
+                                                                       len= normal_quad_v3(vlr->n, mv[mf->v4].co, mv[mf->v3].co, mv[mf->v2].co, mv[mf->v1].co);
                                                                else 
-                                                                       len= normal_tri_v3( vlr->n,mv[mf->v3].co, mv[mf->v2].co, mv[mf->v1].co);
+                                                                       len= normal_tri_v3(vlr->n, mv[mf->v3].co, mv[mf->v2].co, mv[mf->v1].co);
                                                        }
                                                        else {
-                                                               if(vlr->v4) 
-                                                                       len= normal_quad_v3( vlr->n,vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                                                               if (vlr->v4)
+                                                                       len= normal_quad_v3(vlr->n, vlr->v4->co, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                                                                else 
-                                                                       len= normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                                                                       len= normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
                                                        }
 
                                                        vlr->mat= ma;
                                                        vlr->flag= flag;
                                                        vlr->ec= 0; /* mesh edges rendered separately */
 
-                                                       if(len==0) obr->totvlak--;
+                                                       if (len==0) obr->totvlak--;
                                                        else {
                                                                CustomDataLayer *layer;
                                                                MTFace *mtface, *mtf;
@@ -3457,33 +3461,30 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                                                                char *name;
                                                                int nr_verts = v4!=0 ? 4 : 3;
 
-                                                               for(index=0; index<dm->faceData.totlayer; index++) {
+                                                               for (index=0; index<dm->faceData.totlayer; index++) {
                                                                        layer= &dm->faceData.layers[index];
                                                                        name= layer->name;
                                                                        
-                                                                       if(layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
+                                                                       if (layer->type == CD_MTFACE && mtfn < MAX_MTFACE) {
                                                                                int t;
                                                                                mtf= RE_vlakren_get_tface(obr, vlr, mtfn++, &name, 1);
                                                                                mtface= (MTFace*)layer->data;
                                                                                *mtf= mtface[a];        // copy face info
-                                                                               for(vindex=0; vindex<nr_verts; vindex++)
-                                                                                       for(t=0; t<2; t++)
+                                                                               for (vindex=0; vindex<nr_verts; vindex++)
+                                                                                       for (t=0; t<2; t++)
                                                                                                mtf->uv[vindex][t]=mtface[a].uv[rev_tab[vindex]][t];
                                                                        }
-                                                                       else if(layer->type == CD_MCOL && mcn < MAX_MCOL) {
+                                                                       else if (layer->type == CD_MCOL && mcn < MAX_MCOL) {
                                                                                mc= RE_vlakren_get_mcol(obr, vlr, mcn++, &name, 1);
                                                                                mcol= (MCol*)layer->data;
-                                                                               for(vindex=0; vindex<nr_verts; vindex++)
+                                                                               for (vindex=0; vindex<nr_verts; vindex++)
                                                                                        mc[vindex]=mcol[a*4+rev_tab[vindex]];
                                                                        }
-                                                                       else if(layer->type == CD_TANGENT && mtng < 1)
-                                                                       {
-                                                                               if(need_nmap_tangent!=0)
-                                                                               {
+                                                                       else if (layer->type == CD_TANGENT && mtng < 1) {
+                                                                               if (need_nmap_tangent != 0) {
                                                                                        const float * tangent = (const float *) layer->data;
                                                                                        float * ftang = RE_vlakren_get_nmap_tangent(obr, vlr, 1);
-                                                                                       for(vindex=0; vindex<nr_verts; vindex++)
-                                                                                       {
+                                                                                       for (vindex=0; vindex<nr_verts; vindex++) {
                                                                                                copy_v4_v4(ftang+vindex*4, tangent+a*16+rev_tab[vindex]*4);
                                                                                                mul_mat3_m4_v3(mat, ftang+vindex*4);
                                                                                                normalize_v3(ftang+vindex*4);
@@ -3501,7 +3502,7 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                        end= dm->getNumEdges(dm);
                        mvert= dm->getVertArray(dm);
                        ma= give_render_material(re, ob, 1);
-                       if(end && (ma->material_type == MA_TYPE_WIRE)) {
+                       if (end && (ma->material_type == MA_TYPE_WIRE)) {
                                MEdge *medge;
                                struct edgesort *edgetable;
                                int totedge= 0;
@@ -3512,7 +3513,7 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                                /* we want edges to have UV and vcol too... */
                                edgetable= make_mesh_edge_lookup(dm, &totedge);
                                
-                               for(a1=0; a1<end; a1++, medge++) {
+                               for (a1=0; a1<end; a1++, medge++) {
                                        if (medge->flag&ME_EDGERENDER) {
                                                MVert *v0 = &mvert[medge->v1];
                                                MVert *v1 = &mvert[medge->v2];
@@ -3523,7 +3524,7 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                                                vlr->v3= vlr->v2;
                                                vlr->v4= NULL;
                                                
-                                               if(edgetable)
+                                               if (edgetable)
                                                        use_mesh_edge_lookup(obr, dm, medge, vlr, edgetable, totedge);
                                                
                                                xn= -(v0->no[0]+v1->no[0]);
@@ -3540,31 +3541,31 @@ static void init_render_mesh(Render *re, ObjectRen *obr, int timeoffset)
                                                vlr->ec= ME_V1V2;
                                        }
                                }
-                               if(edgetable)
+                               if (edgetable)
                                        MEM_freeN(edgetable);
                        }
                }
        }
        
-       if(!timeoffset) {
-               if(need_stress)
+       if (!timeoffset) {
+               if (need_stress)
                        calc_edge_stress(re, obr, me);
 
                if (test_for_displace(re, ob ) ) {
                        recalc_normals= 1;
                        calc_vertexnormals(re, obr, 0, 0);
-                       if(do_autosmooth)
+                       if (do_autosmooth)
                                do_displacement(re, obr, mat, imat);
                        else
                                do_displacement(re, obr, NULL, NULL);
                }
 
-               if(do_autosmooth) {
+               if (do_autosmooth) {
                        recalc_normals= 1;
                        autosmooth(re, obr, mat, me->smoothresh);
                }
 
-               if(recalc_normals!=0 || need_tangent!=0)
+               if (recalc_normals!=0 || need_tangent!=0)
                        calc_vertexnormals(re, obr, need_tangent, need_nmap_tangent);
        }
 
@@ -3580,21 +3581,21 @@ static void initshadowbuf(Render *re, LampRen *lar, float mat[][4])
        struct ShadBuf *shb;
        float viewinv[4][4];
        
-       /* if(la->spsi<16) return; */
+       /* if (la->spsi<16) return; */
        
        /* memory alloc */
-       shb= (struct ShadBuf *)MEM_callocN( sizeof(struct ShadBuf),"initshadbuf");
+       shb= (struct ShadBuf *)MEM_callocN(sizeof(struct ShadBuf), "initshadbuf");
        lar->shb= shb;
        
-       if(shb==NULL) return;
+       if (shb==NULL) return;
        
        VECCOPY(shb->co, lar->co); /* int copy */
        
        /* percentage render: keep track of min and max */
        shb->size= (lar->bufsize*re->r.size)/100;
        
-       if(shb->size<512) shb->size= 512;
-       else if(shb->size > lar->bufsize) shb->size= lar->bufsize;
+       if (shb->size<512) shb->size= 512;
+       else if (shb->size > lar->bufsize) shb->size= lar->bufsize;
        
        shb->size &= ~15;       /* make sure its multiples of 16 */
        
@@ -3619,7 +3620,7 @@ static void initshadowbuf(Render *re, LampRen *lar, float mat[][4])
        shb->bias= (0.02f*lar->bias)*0x7FFFFFFF;
        
        /* halfway method (average of first and 2nd z) reduces bias issues */
-       if(ELEM(lar->buftype, LA_SHADBUF_HALFWAY, LA_SHADBUF_DEEP))
+       if (ELEM(lar->buftype, LA_SHADBUF_HALFWAY, LA_SHADBUF_DEEP))
                shb->bias= 0.1f*shb->bias;
        
        shb->compressthresh= lar->compressthresh;
@@ -3668,11 +3669,11 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        int c;
 
        /* previewrender sets this to zero... prevent accidents */
-       if(la==NULL) return NULL;
+       if (la==NULL) return NULL;
        
        /* prevent only shadow from rendering light */
-       if(la->mode & LA_ONLYSHADOW)
-               if((re->r.mode & R_SHADOW)==0)
+       if (la->mode & LA_ONLYSHADOW)
+               if ((re->r.mode & R_SHADOW)==0)
                        return NULL;
        
        re->totlamp++;
@@ -3682,7 +3683,7 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        BLI_addtail(&re->lights, go);
        go->ob= ob;
        /* lamprens are in own list, for freeing */
-       lar= (LampRen *)MEM_callocN(sizeof(LampRen),"lampren");
+       lar= (LampRen *)MEM_callocN(sizeof(LampRen), "lampren");
        BLI_addtail(&re->lampren, lar);
        go->lampren= lar;
 
@@ -3695,7 +3696,7 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        lar->bufsize = la->bufsize;
        lar->samp = la->samp;
        lar->buffers= la->buffers;
-       if(lar->buffers==0) lar->buffers= 1;
+       if (lar->buffers==0) lar->buffers= 1;
        lar->buftype= la->buftype;
        lar->filtertype= la->filtertype;
        lar->soft = la->soft;
@@ -3710,7 +3711,7 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        lar->mode= la->mode;
 
        lar->energy= la->energy;
-       if(la->mode & LA_NEG) lar->energy= -lar->energy;
+       if (la->mode & LA_NEG) lar->energy= -lar->energy;
 
        lar->vec[0]= -mat[2][0];
        lar->vec[1]= -mat[2][1];
@@ -3743,7 +3744,7 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        
        /* Annoying, lamp UI does this, but the UI might not have been used? - add here too.
         * make sure this matches buttons_shading.c's logic */
-       if(ELEM4(la->type, LA_AREA, LA_SPOT, LA_SUN, LA_LOCAL) && (la->mode & LA_SHAD_RAY))
+       if (ELEM4(la->type, LA_AREA, LA_SPOT, LA_SUN, LA_LOCAL) && (la->mode & LA_SHAD_RAY))
                if (ELEM3(la->type, LA_SPOT, LA_SUN, LA_LOCAL))
                        if (la->ray_samp_method == LA_SAMP_CONSTANT) la->ray_samp_method = LA_SAMP_HALTON;
        
@@ -3753,13 +3754,13 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        lar->adapt_thresh= la->adapt_thresh;
        lar->sunsky = NULL;
        
-       if( ELEM(lar->type, LA_SPOT, LA_LOCAL)) {
+       if ( ELEM(lar->type, LA_SPOT, LA_LOCAL)) {
                lar->ray_totsamp= lar->ray_samp*lar->ray_samp;
                lar->area_shape = LA_AREA_SQUARE;
                lar->area_sizey= lar->area_size;
        }
-       else if(lar->type==LA_AREA) {
-               switch(lar->area_shape) {
+       else if (lar->type==LA_AREA) {
+               switch (lar->area_shape) {
                case LA_AREA_SQUARE:
                        lar->ray_totsamp= lar->ray_samp*lar->ray_samp;
                        lar->ray_sampy= lar->ray_samp;
@@ -3783,17 +3784,17 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
                area_lamp_vectors(lar);
                init_jitter_plane(lar); // subsamples
        }
-       else if(lar->type==LA_SUN){
+       else if (lar->type==LA_SUN) {
                lar->ray_totsamp= lar->ray_samp*lar->ray_samp;
                lar->area_shape = LA_AREA_SQUARE;
                lar->area_sizey= lar->area_size;
 
-               if((la->sun_effect_type & LA_SUN_EFFECT_SKY) ||
-                               (la->sun_effect_type & LA_SUN_EFFECT_AP)){
+               if ((la->sun_effect_type & LA_SUN_EFFECT_SKY) ||
+                               (la->sun_effect_type & LA_SUN_EFFECT_AP)) {
                        lar->sunsky = (struct SunSky*)MEM_callocN(sizeof(struct SunSky), "sunskyren");
                        lar->sunsky->effect_type = la->sun_effect_type;
                
-                       copy_v3_v3(vec,ob->obmat[2]);
+                       copy_v3_v3(vec, ob->obmat[2]);
                        normalize_v3(vec);
 
                        InitSunSky(lar->sunsky, la->atm_turbidity, vec, la->horizon_brightness, 
@@ -3807,8 +3808,8 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        else lar->ray_totsamp= 0;
        
        lar->spotsi= la->spotsize;
-       if(lar->mode & LA_HALO) {
-               if(lar->spotsi>170.0f) lar->spotsi= 170.0f;
+       if (lar->mode & LA_HALO) {
+               if (lar->spotsi>170.0f) lar->spotsi= 170.0f;
        }
        lar->spotsi= cosf( (float)M_PI*lar->spotsi/360.0f );
        lar->spotbl= (1.0f-lar->spotsi)*la->spotblend;
@@ -3822,7 +3823,7 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        lar->ld2= la->att2;
        lar->curfalloff = curvemapping_copy(la->curfalloff);
 
-       if(lar->type==LA_SPOT) {
+       if (lar->type==LA_SPOT) {
 
                normalize_v3(lar->imat[0]);
                normalize_v3(lar->imat[1]);
@@ -3832,18 +3833,18 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
                xn= sin(xn)/cos(xn);
                lar->spottexfac= 1.0f/(xn);
 
-               if(lar->mode & LA_ONLYSHADOW) {
-                       if((lar->mode & (LA_SHAD_BUF|LA_SHAD_RAY))==0) lar->mode -= LA_ONLYSHADOW;
+               if (lar->mode & LA_ONLYSHADOW) {
+                       if ((lar->mode & (LA_SHAD_BUF|LA_SHAD_RAY))==0) lar->mode -= LA_ONLYSHADOW;
                }
 
        }
 
        /* set flag for spothalo en initvars */
-       if(la->type==LA_SPOT && (la->mode & LA_HALO) && (la->buftype != LA_SHADBUF_DEEP)) {
-               if(la->haint>0.0f) {
+       if (la->type==LA_SPOT && (la->mode & LA_HALO) && (la->buftype != LA_SHADBUF_DEEP)) {
+               if (la->haint>0.0f) {
                        re->flag |= R_LAMPHALO;
 
-                       /* camera position (0,0,0) rotate around lamp */
+                       /* camera position (0, 0, 0) rotate around lamp */
                        lar->sh_invcampos[0]= -lar->co[0];
                        lar->sh_invcampos[1]= -lar->co[1];
                        lar->sh_invcampos[2]= -lar->co[2];
@@ -3858,25 +3859,25 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
                        lar->sh_invcampos[2]*= lar->sh_zfac;
 
                        /* halfway shadow buffer doesn't work for volumetric effects */
-                       if(lar->buftype == LA_SHADBUF_HALFWAY)
+                       if (lar->buftype == LA_SHADBUF_HALFWAY)
                                lar->buftype = LA_SHADBUF_REGULAR;
 
                }
        }
-       else if(la->type==LA_HEMI) {
+       else if (la->type==LA_HEMI) {
                lar->mode &= ~(LA_SHAD_RAY|LA_SHAD_BUF);
        }
 
-       for(c=0; c<MAX_MTEX; c++) {
-               if(la->mtex[c] && la->mtex[c]->tex) {
+       for (c=0; c<MAX_MTEX; c++) {
+               if (la->mtex[c] && la->mtex[c]->tex) {
                        if (la->mtex[c]->mapto & LAMAP_COL) 
                                lar->mode |= LA_TEXTURE;
                        if (la->mtex[c]->mapto & LAMAP_SHAD)
                                lar->mode |= LA_SHAD_TEX;
 
-                       if(G.rendering) {
-                               if(re->osa) {
-                                       if(la->mtex[c]->tex->type==TEX_IMAGE) lar->mode |= LA_OSATEX;
+                       if (G.rendering) {
+                               if (re->osa) {
+                                       if (la->mtex[c]->tex->type==TEX_IMAGE) lar->mode |= LA_OSATEX;
                                }
                        }
                }
@@ -3885,15 +3886,15 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
        /* old code checked for internal render (aka not yafray) */
        {
                /* to make sure we can check ray shadow easily in the render code */
-               if(lar->mode & LA_SHAD_RAY) {
-                       if( (re->r.mode & R_RAYTRACE)==0)
+               if (lar->mode & LA_SHAD_RAY) {
+                       if ( (re->r.mode & R_RAYTRACE)==0)
                                lar->mode &= ~LA_SHAD_RAY;
                }
        
 
-               if(re->r.mode & R_SHADOW) {
+               if (re->r.mode & R_SHADOW) {
                        
-                       if(la->type==LA_AREA && (lar->mode & LA_SHAD_RAY) && (lar->ray_samp_method == LA_SAMP_CONSTANT)) {
+                       if (la->type==LA_AREA && (lar->mode & LA_SHAD_RAY) && (lar->ray_samp_method == LA_SAMP_CONSTANT)) {
                                init_jitter_plane(lar);
                        }
                        else if (la->type==LA_SPOT && (lar->mode & LA_SHAD_BUF) ) {
@@ -3905,7 +3906,7 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
                        
                        
                        /* this is the way used all over to check for shadow */
-                       if(lar->shb || (lar->mode & LA_SHAD_RAY)) {
+                       if (lar->shb || (lar->mode & LA_SHAD_RAY)) {
                                LampShadowSample *ls;
                                LampShadowSubSample *lss;
                                int a, b;
@@ -3916,9 +3917,9 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
                                ls= lar->shadsamp;
 
                                /* shadfacs actually mean light, let's put them to 1 to prevent unitialized accidents */
-                               for(a=0; a<re->r.threads; a++, ls++) {
+                               for (a=0; a<re->r.threads; a++, ls++) {
                                        lss= ls->s;
-                                       for(b=0; b<re->r.osa; b++, lss++) {
+                                       for (b=0; b<re->r.osa; b++, lss++) {
                                                lss->samplenr= -1;      /* used to detect whether we store or read */
                                                lss->shadfac[0]= 1.0f;
                                                lss->shadfac[1]= 1.0f;
@@ -3942,20 +3943,20 @@ static void add_lightgroup(Render *re, Group *group, int exclusive)
 
        /* it's a bit too many loops in loops... but will survive */
        /* note that 'exclusive' will remove it from the global list */
-       for(go= group->gobject.first; go; go= go->next) {
+       for (go= group->gobject.first; go; go= go->next) {
                go->lampren= NULL;
                
-               if(go->ob->lay & re->lay) {
-                       if(go->ob && go->ob->type==OB_LAMP) {
-                               for(gol= re->lights.first; gol; gol= gol->next) {
-                                       if(gol->ob==go->ob) {
+               if (go->ob->lay & re->lay) {
+                       if (go->ob && go->ob->type==OB_LAMP) {
+                               for (gol= re->lights.first; gol; gol= gol->next) {
+                                       if (gol->ob==go->ob) {
                                                go->lampren= gol->lampren;
                                                break;
                                        }
                                }
-                               if(go->lampren==NULL) 
+                               if (go->lampren==NULL)
                                        gol= add_render_lamp(re, go->ob);
-                               if(gol && exclusive) {
+                               if (gol && exclusive) {
                                        BLI_remlink(&re->lights, gol);
                                        MEM_freeN(gol);
                                }
@@ -3970,16 +3971,16 @@ static void set_material_lightgroups(Render *re)
        Material *ma;
        
        /* not for preview render */
-       if(re->scene->r.scemode & R_PREVIEWBUTS)
+       if (re->scene->r.scemode & R_PREVIEWBUTS)
                return;
        
-       for(group= re->main->group.first; group; group=group->id.next)
+       for (group= re->main->group.first; group; group=group->id.next)
                group->id.flag |= LIB_DOIT;
        
        /* it's a bit too many loops in loops... but will survive */
        /* hola! materials not in use...? */
-       for(ma= re->main->mat.first; ma; ma=ma->id.next) {
-               if(ma->group && (ma->group->id.flag & LIB_DOIT))
+       for (ma= re->main->mat.first; ma; ma=ma->id.next) {
+               if (ma->group && (ma->group->id.flag & LIB_DOIT))
                        add_lightgroup(re, ma->group, ma->mode & MA_GROUP_NOLAY);
        }
 }
@@ -3988,8 +3989,8 @@ static void set_renderlayer_lightgroups(Render *re, Scene *sce)
 {
        SceneRenderLayer *srl;
        
-       for(srl= sce->r.layers.first; srl; srl= srl->next) {
-               if(srl->light_override)
+       for (srl= sce->r.layers.first; srl; srl= srl->next) {
+               if (srl->light_override)
                        add_lightgroup(re, srl->light_override, 0);
        }
 }
@@ -4003,7 +4004,7 @@ void init_render_world(Render *re)
        int a;
        char *cp;
        
-       if(re->scene && re->scene->world) {
+       if (re->scene && re->scene->world) {
                re->wrld= *(re->scene->world);
                
                cp= (char *)&re->wrld.fastcol;
@@ -4017,14 +4018,14 @@ void init_render_world(Render *re)
                normalize_v3(re->grvec);
                copy_m3_m4(re->imat, re->viewinv);
                
-               for(a=0; a<MAX_MTEX; a++) 
-                       if(re->wrld.mtex[a] && re->wrld.mtex[a]->tex) re->wrld.skytype |= WO_SKYTEX;
+               for (a=0; a<MAX_MTEX; a++)
+                       if (re->wrld.mtex[a] && re->wrld.mtex[a]->tex) re->wrld.skytype |= WO_SKYTEX;
                
                /* AO samples should be OSA minimum */
-               if(re->osa)
-                       while(re->wrld.aosamp*re->wrld.aosamp < re->osa) 
+               if (re->osa)
+                       while (re->wrld.aosamp*re->wrld.aosamp < re->osa)
                                re->wrld.aosamp++;
-               if(!(re->r.mode & R_RAYTRACE) && (re->wrld.ao_gather_method == WO_AOGATHER_RAYTRACE))
+               if (!(re->r.mode & R_RAYTRACE) && (re->wrld.ao_gather_method == WO_AOGATHER_RAYTRACE))
                        re->wrld.mode &= ~(WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT);
        }
        else {
@@ -4057,47 +4058,47 @@ static void set_phong_threshold(ObjectRen *obr)
        int tot=0, i;
        
        /* Added check for 'pointy' situations, only dotproducts of 0.9 and larger 
-          are taken into account. This threshold is meant to work on smooth geometry, not
-          for extreme cases (ton) */
+        * are taken into account. This threshold is meant to work on smooth geometry, not
+        * for extreme cases (ton) */
        
-       for(i=0; i<obr->totvlak; i++) {
+       for (i=0; i<obr->totvlak; i++) {
                vlr= RE_findOrAddVlak(obr, i);
-               if(vlr->flag & R_SMOOTH) {
+               if (vlr->flag & R_SMOOTH) {
                        dot= dot_v3v3(vlr->n, vlr->v1->n);
                        dot= ABS(dot);
-                       if(dot>0.9f) {
+                       if (dot>0.9f) {
                                thresh+= dot; tot++;
                        }
                        dot= dot_v3v3(vlr->n, vlr->v2->n);
                        dot= ABS(dot);
-                       if(dot>0.9f) {
+                       if (dot>0.9f) {
                                thresh+= dot; tot++;
                        }
 
                        dot= dot_v3v3(vlr->n, vlr->v3->n);
                        dot= ABS(dot);
-                       if(dot>0.9f) {
+                       if (dot>0.9f) {
                                thresh+= dot; tot++;
                        }
 
-                       if(vlr->v4) {
+                       if (vlr->v4) {
                                dot= dot_v3v3(vlr->n, vlr->v4->n);
                                dot= ABS(dot);
-                               if(dot>0.9f) {
+                               if (dot>0.9f) {
                                        thresh+= dot; tot++;
                                }
                        }
                }
        }
        
-       if(tot) {
+       if (tot) {
                thresh/= (float)tot;
                obr->ob->smoothresh= cosf(0.5f*(float)M_PI-saacos(thresh));
        }
 }
 
 /* per face check if all samples should be taken.
  if raytrace or multisample, do always for raytraced material, or when material full_osa set */
* if raytrace or multisample, do always for raytraced material, or when material full_osa set */
 static void set_fullsample_trace_flag(Render *re, ObjectRen *obr)
 {
        VlakRen *vlr;
@@ -4106,21 +4107,21 @@ static void set_fullsample_trace_flag(Render *re, ObjectRen *obr)
        osa= re->osa;
        trace= re->r.mode & R_RAYTRACE;
        
-       for(a=obr->totvlak-1; a>=0; a--) {
+       for (a=obr->totvlak-1; a>=0; a--) {
                vlr= RE_findOrAddVlak(obr, a);
                mode= vlr->mat->mode;
 
-               if(trace && (mode & MA_TRACEBLE))
+               if (trace && (mode & MA_TRACEBLE))
                        vlr->flag |= R_TRACEBLE;
                
-               if(osa) {
-                       if(mode & MA_FULL_OSA) {
+               if (osa) {
+                       if (mode & MA_FULL_OSA) {
                                vlr->flag |= R_FULL_OSA;
                        }
-                       else if(trace) {
-                               if(mode & MA_SHLESS);
-                               else if(vlr->mat->material_type == MA_TYPE_VOLUME);
-                               else if((mode & MA_RAYMIRROR) || ((mode & MA_TRANSP) && (mode & MA_RAYTRANSP))) {
+                       else if (trace) {
+                               if (mode & MA_SHLESS);
+                               else if (vlr->mat->material_type == MA_TYPE_VOLUME);
+                               else if ((mode & MA_RAYMIRROR) || ((mode & MA_TRANSP) && (mode & MA_RAYTRANSP))) {
                                        /* for blurry reflect/refract, better to take more samples 
                                         * inside the raytrace than as OSA samples */
                                        if ((vlr->mat->gloss_mir == 1.0f) && (vlr->mat->gloss_tra == 1.0f))
@@ -4132,25 +4133,25 @@ static void set_fullsample_trace_flag(Render *re, ObjectRen *obr)
 }
 
 /* split quads for predictable baking
- * dir 1 == (0,1,2) (0,2,3),  2 == (1,3,0) (1,2,3) 
+ * dir 1 == (0, 1, 2) (0, 2, 3),  2 == (1, 3, 0) (1, 2, 3)
  */
 static void split_quads(ObjectRen *obr, int dir) 
 {
        VlakRen *vlr, *vlr1;
        int a;
 
-       for(a=obr->totvlak-1; a>=0; a--) {
+       for (a=obr->totvlak-1; a>=0; a--) {
                vlr= RE_findOrAddVlak(obr, a);
                
                /* test if rendering as a quad or triangle, skip wire */
-               if(vlr->v4 && (vlr->flag & R_STRAND)==0 && (vlr->mat->material_type != MA_TYPE_WIRE)) {
+               if (vlr->v4 && (vlr->flag & R_STRAND)==0 && (vlr->mat->material_type != MA_TYPE_WIRE)) {
                        
-                       if(vlr->v4) {
+                       if (vlr->v4) {
 
                                vlr1= RE_vlakren_copy(obr, vlr);
                                vlr1->flag |= R_FACE_SPLIT;
                                
-                               if( dir==2 ) vlr->flag |= R_DIVIDE_24;
+                               if ( dir==2 ) vlr->flag |= R_DIVIDE_24;
                                else vlr->flag &= ~R_DIVIDE_24;
 
                                /* new vertex pointers */
@@ -4173,8 +4174,8 @@ static void split_quads(ObjectRen *obr, int dir)
                                vlr->v4 = vlr1->v4 = NULL;
                                
                                /* new normals */
-                               normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
-                               normal_tri_v3( vlr1->n,vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
+                               normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                               normal_tri_v3(vlr1->n, vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
                        }
                        /* clear the flag when not divided */
                        else vlr->flag &= ~R_DIVIDE_24;
@@ -4189,11 +4190,11 @@ static void check_non_flat_quads(ObjectRen *obr)
        float nor[3], xn, flen;
        int a;
 
-       for(a=obr->totvlak-1; a>=0; a--) {
+       for (a=obr->totvlak-1; a>=0; a--) {
                vlr= RE_findOrAddVlak(obr, a);
                
                /* test if rendering as a quad or triangle, skip wire */
-               if(vlr->v4 && (vlr->flag & R_STRAND)==0 && (vlr->mat->material_type != MA_TYPE_WIRE)) {
+               if (vlr->v4 && (vlr->flag & R_STRAND)==0 && (vlr->mat->material_type != MA_TYPE_WIRE)) {
                        
                        /* check if quad is actually triangle */
                        v1= vlr->v1;
@@ -4201,7 +4202,7 @@ static void check_non_flat_quads(ObjectRen *obr)
                        v3= vlr->v3;
                        v4= vlr->v4;
                        sub_v3_v3v3(nor, v1->co, v2->co);
-                       if( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
+                       if ( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
                                vlr->v1= v2;
                                vlr->v2= v3;
                                vlr->v3= v4;
@@ -4209,26 +4210,26 @@ static void check_non_flat_quads(ObjectRen *obr)
                        }
                        else {
                                sub_v3_v3v3(nor, v2->co, v3->co);
-                               if( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
+                               if ( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
                                        vlr->v2= v3;
                                        vlr->v3= v4;
                                        vlr->v4= NULL;
                                }
                                else {
                                        sub_v3_v3v3(nor, v3->co, v4->co);
-                                       if( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
+                                       if ( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
                                                vlr->v4= NULL;
                                        }
                                        else {
                                                sub_v3_v3v3(nor, v4->co, v1->co);
-                                               if( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
+                                               if ( ABS(nor[0])<FLT_EPSILON10 &&  ABS(nor[1])<FLT_EPSILON10 && ABS(nor[2])<FLT_EPSILON10 ) {
                                                        vlr->v4= NULL;
                                                }
                                        }
                                }
                        }
                        
-                       if(vlr->v4) {
+                       if (vlr->v4) {
                                
                                /* Face is divided along edge with the least gradient           */
                                /* Flagged with R_DIVIDE_24 if divide is from vert 2 to 4       */
@@ -4238,12 +4239,12 @@ static void check_non_flat_quads(ObjectRen *obr)
                                /*              1---2           1---2   0 = orig face, 1 = new face */
                                
                                /* render normals are inverted in render! we calculate normal of single tria here */
-                               flen= normal_tri_v3( nor,vlr->v4->co, vlr->v3->co, vlr->v1->co);
-                               if(flen==0.0f) normal_tri_v3( nor,vlr->v4->co, vlr->v2->co, vlr->v1->co);
+                               flen= normal_tri_v3(nor, vlr->v4->co, vlr->v3->co, vlr->v1->co);
+                               if (flen==0.0f) normal_tri_v3(nor, vlr->v4->co, vlr->v2->co, vlr->v1->co);
                                
                                xn = dot_v3v3(nor, vlr->n);
 
-                               if(ABS(xn) < 0.999995f ) {      // checked on noisy fractal grid
+                               if (ABS(xn) < 0.999995f ) {     // checked on noisy fractal grid
                                        
                                        float d1, d2;
 
@@ -4251,13 +4252,13 @@ static void check_non_flat_quads(ObjectRen *obr)
                                        vlr1->flag |= R_FACE_SPLIT;
                                        
                                        /* split direction based on vnorms */
-                                       normal_tri_v3( nor,vlr->v1->co, vlr->v2->co, vlr->v3->co);
+                                       normal_tri_v3(nor, vlr->v1->co, vlr->v2->co, vlr->v3->co);
                                        d1 = dot_v3v3(nor, vlr->v1->n);
 
-                                       normal_tri_v3( nor,vlr->v2->co, vlr->v3->co, vlr->v4->co);
+                                       normal_tri_v3(nor, vlr->v2->co, vlr->v3->co, vlr->v4->co);
                                        d2 = dot_v3v3(nor, vlr->v2->n);
 
-                                       if( fabs(d1) < fabs(d2) ) vlr->flag |= R_DIVIDE_24;
+                                       if ( fabs(d1) < fabs(d2) ) vlr->flag |= R_DIVIDE_24;
                                        else vlr->flag &= ~R_DIVIDE_24;
 
                                        /* new vertex pointers */
@@ -4280,8 +4281,8 @@ static void check_non_flat_quads(ObjectRen *obr)
                                        vlr->v4 = vlr1->v4 = NULL;
                                        
                                        /* new normals */
-                                       normal_tri_v3( vlr->n,vlr->v3->co, vlr->v2->co, vlr->v1->co);
-                                       normal_tri_v3( vlr1->n,vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
+                                       normal_tri_v3(vlr->n, vlr->v3->co, vlr->v2->co, vlr->v1->co);
+                                       normal_tri_v3(vlr1->n, vlr1->v3->co, vlr1->v2->co, vlr1->v1->co);
                                }
                                /* clear the flag when not divided */
                                else vlr->flag &= ~R_DIVIDE_24;
@@ -4299,25 +4300,26 @@ static void finalize_render_object(Render *re, ObjectRen *obr, int timeoffset)
        float min[3], max[3], smin[3], smax[3];
        int a, b;
 
-       if(obr->totvert || obr->totvlak || obr->tothalo || obr->totstrand) {
+       if (obr->totvert || obr->totvlak || obr->tothalo || obr->totstrand) {
                /* the exception below is because displace code now is in init_render_mesh call, 
-               I will look at means to have autosmooth enabled for all object types 
-               and have it as general postprocess, like displace */
-               if(ob->type!=OB_MESH && test_for_displace(re, ob)) 
+                * I will look at means to have autosmooth enabled for all object types
+                * and have it as general postprocess, like displace */
+               if (ob->type!=OB_MESH && test_for_displace(re, ob))
                        do_displacement(re, obr, NULL, NULL);
        
-               if(!timeoffset) {
+               if (!timeoffset) {
                        /* phong normal interpolation can cause error in tracing
                         * (terminator problem) */
                        ob->smoothresh= 0.0;
-                       if((re->r.mode & R_RAYTRACE) && (re->r.mode & R_SHADOW)) 
+                       if ((re->r.mode & R_RAYTRACE) && (re->r.mode & R_SHADOW))
                                set_phong_threshold(obr);
                        
                        if (re->flag & R_BAKING && re->r.bake_quad_split != 0) {
                                /* Baking lets us define a quad split order */
                                split_quads(obr, re->r.bake_quad_split);
-                       } else {
-                               if((re->r.mode & R_SIMPLIFY && re->r.simplify_flag & R_SIMPLE_NO_TRIANGULATE) == 0)
+                       }
+                       else {
+                               if ((re->r.mode & R_SIMPLIFY && re->r.simplify_flag & R_SIMPLE_NO_TRIANGULATE) == 0)
                                        check_non_flat_quads(obr);
                        }
                        
@@ -4325,29 +4327,29 @@ static void finalize_render_object(Render *re, ObjectRen *obr, int timeoffset)
 
                        /* compute bounding boxes for clipping */
                        INIT_MINMAX(min, max);
-                       for(a=0; a<obr->totvert; a++) {
-                               if((a & 255)==0) ver= obr->vertnodes[a>>8].vert;
+                       for (a=0; a<obr->totvert; a++) {
+                               if ((a & 255)==0) ver= obr->vertnodes[a>>8].vert;
                                else ver++;
 
                                DO_MINMAX(ver->co, min, max);
                        }
 
-                       if(obr->strandbuf) {
+                       if (obr->strandbuf) {
                                float width;
                                
                                /* compute average bounding box of strandpoint itself (width) */
-                               if(obr->strandbuf->flag & R_STRAND_B_UNITS)
+                               if (obr->strandbuf->flag & R_STRAND_B_UNITS)
                                        obr->strandbuf->maxwidth= MAX2(obr->strandbuf->ma->strand_sta, obr->strandbuf->ma->strand_end);
                                else
                                        obr->strandbuf->maxwidth= 0.0f;
                                
                                width= obr->strandbuf->maxwidth;
                                sbound= obr->strandbuf->bound;
-                               for(b=0; b<obr->strandbuf->totbound; b++, sbound++) {
+                               for (b=0; b<obr->strandbuf->totbound; b++, sbound++) {
                                        
                                        INIT_MINMAX(smin, smax);
 
-                                       for(a=sbound->start; a<sbound->end; a++) {
+                                       for (a=sbound->start; a<sbound->end; a++) {
                                                strand= RE_findOrAddStrand(obr, a);
                                                strand_minmax(strand, smin, smax, width);
                                        }
@@ -4387,8 +4389,8 @@ static void find_dupli_instances(Render *re, ObjectRen *obr)
        /* for objects instanced by dupliverts/faces/particles, we go over the
         * list of instances to find ones that instance obr, and setup their
         * matrices and obr pointer */
-       for(obi=re->instancetable.last; obi; obi=obi->prev) {
-               if(!obi->obr && obi->ob == obr->ob && obi->psysindex == obr->psysindex) {
+       for (obi=re->instancetable.last; obi; obi=obi->prev) {
+               if (!obi->obr && obi->ob == obr->ob && obi->psysindex == obr->psysindex) {
                        obi->obr= obr;
 
                        /* compute difference between object matrix and
@@ -4400,7 +4402,7 @@ static void find_dupli_instances(Render *re, ObjectRen *obr)
                        invert_m3_m3(obi->nmat, nmat);
                        transpose_m3(obi->nmat);
 
-                       if(!first) {
+                       if (!first) {
                                re->totvert += obr->totvert;
                                re->totvlak += obr->totvlak;
                                re->tothalo += obr->tothalo;
@@ -4442,13 +4444,13 @@ static ObjectRen *find_dupligroup_dupli(Render *re, Object *ob, int psysindex)
 
        /* if the object is itself instanced, we don't want to create an instance
         * for it */
-       if(ob->transflag & OB_RENDER_DUPLI)
+       if (ob->transflag & OB_RENDER_DUPLI)
                return NULL;
 
        /* try to find an object that was already created so we can reuse it
         * and save memory */
-       for(obr=re->objecttable.first; obr; obr=obr->next)
-               if(obr->ob == ob && obr->psysindex == psysindex && (obr->flag & R_INSTANCEABLE))
+       for (obr=re->objecttable.first; obr; obr=obr->next)
+               if (obr->ob == ob && obr->psysindex == psysindex && (obr->flag & R_INSTANCEABLE))
                        return obr;
        
        return NULL;
@@ -4464,14 +4466,14 @@ static void set_dupli_tex_mat(Render *re, ObjectInstanceRen *obi, DupliObject *d
        static int needtexmat= 0;
 
        /* init */
-       if(!re) {
+       if (!re) {
                lastob= NULL;
                needtexmat= 0;
                return;
        }
 
        /* check if we actually need it */
-       if(lastob != dob->ob) {
+       if (lastob != dob->ob) {
                Material ***material;
                short a, *totmaterial;
 
@@ -4481,13 +4483,13 @@ static void set_dupli_tex_mat(Render *re, ObjectInstanceRen *obi, DupliObject *d
                totmaterial= give_totcolp(dob->ob);
                material= give_matarar(dob->ob);
 
-               if(totmaterial && material)
-                       for(a= 0; a<*totmaterial; a++)
-                               if((*material)[a] && (*material)[a]->texco & TEXCO_OBJECT)
+               if (totmaterial && material)
+                       for (a= 0; a<*totmaterial; a++)
+                               if ((*material)[a] && (*material)[a]->texco & TEXCO_OBJECT)
                                        needtexmat= 1;
        }
 
-       if(needtexmat) {
+       if (needtexmat) {
                float imat[4][4];
 
                obi->duplitexmat= BLI_memarena_alloc(re->memArena, sizeof(float)*4*4);
@@ -4502,8 +4504,8 @@ static void init_render_object_data(Render *re, ObjectRen *obr, int timeoffset)
        ParticleSystem *psys;
        int i;
 
-       if(obr->psysindex) {
-               if((!obr->prev || obr->prev->ob != ob || (obr->prev->flag & R_INSTANCEABLE)==0) && ob->type==OB_MESH) {
+       if (obr->psysindex) {
+               if ((!obr->prev || obr->prev->ob != ob || (obr->prev->flag & R_INSTANCEABLE)==0) && ob->type==OB_MESH) {
                        /* the emitter mesh wasn't rendered so the modifier stack wasn't
                         * evaluated with render settings */
                        DerivedMesh *dm;
@@ -4511,19 +4513,19 @@ static void init_render_object_data(Render *re, ObjectRen *obr, int timeoffset)
                        dm->release(dm);
                }
 
-               for(psys=ob->particlesystem.first, i=0; i<obr->psysindex-1; i++)
+               for (psys=ob->particlesystem.first, i=0; i<obr->psysindex-1; i++)
                        psys= psys->next;
 
                render_new_particle_system(re, obr, psys, timeoffset);
        }
        else {
-               if ELEM(ob->type, OB_FONT, OB_CURVE)
+               if (ELEM(ob->type, OB_FONT, OB_CURVE))
                        init_render_curve(re, obr, timeoffset);
-               else if(ob->type==OB_SURF)
+               else if (ob->type==OB_SURF)
                        init_render_surf(re, obr, timeoffset);
-               else if(ob->type==OB_MESH)
+               else if (ob->type==OB_MESH)
                        init_render_mesh(re, obr, timeoffset);
-               else if(ob->type==OB_MBALL)
+               else if (ob->type==OB_MBALL)
                        init_render_mball(re, obr);
        }
 
@@ -4546,31 +4548,31 @@ static void add_render_object(Render *re, Object *ob, Object *par, DupliObject *
 
        /* the emitter has to be processed first (render levels of modifiers) */
        /* so here we only check if the emitter should be rendered */
-       if(ob->particlesystem.first) {
+       if (ob->particlesystem.first) {
                show_emitter= 0;
-               for(psys=ob->particlesystem.first; psys; psys=psys->next) {
+               for (psys=ob->particlesystem.first; psys; psys=psys->next) {
                        show_emitter += psys->part->draw & PART_DRAW_EMITTER;
                        psys_render_set(ob, psys, re->viewmat, re->winmat, re->winx, re->winy, timeoffset);
                }
 
                /* if no psys has "show emitter" selected don't render emitter */
-               if(show_emitter == 0)
+               if (show_emitter == 0)
                        allow_render= 0;
        }
 
        /* one render object for the data itself */
-       if(allow_render) {
+       if (allow_render) {
                obr= RE_addRenderObject(re, ob, par, index, 0, ob->lay);
-               if((dob && !dob->animated) || (ob->transflag & OB_RENDER_DUPLI)) {
+               if ((dob && !dob->animated) || (ob->transflag & OB_RENDER_DUPLI)) {
                        obr->flag |= R_INSTANCEABLE;
                        copy_m4_m4(obr->obmat, ob->obmat);
                }
                init_render_object_data(re, obr, timeoffset);
 
                /* only add instance for objects that have not been used for dupli */
-               if(!(ob->transflag & OB_RENDER_DUPLI)) {
+               if (!(ob->transflag & OB_RENDER_DUPLI)) {
                        obi= RE_addRenderInstance(re, obr, ob, par, index, 0, NULL, ob->lay);
-                       if(dob) set_dupli_tex_mat(re, obi, dob);
+                       if (dob) set_dupli_tex_mat(re, obi, dob);
                }
                else
                        find_dupli_instances(re, obr);
@@ -4583,24 +4585,24 @@ static void add_render_object(Render *re, Object *ob, Object *par, DupliObject *
        }
 
        /* and one render object per particle system */
-       if(ob->particlesystem.first) {
+       if (ob->particlesystem.first) {
                psysindex= 1;
-               for(psys=ob->particlesystem.first; psys; psys=psys->next, psysindex++) {
+               for (psys=ob->particlesystem.first; psys; psys=psys->next, psysindex++) {
                        obr= RE_addRenderObject(re, ob, par, index, psysindex, ob->lay);
-                       if((dob && !dob->animated) || (ob->transflag & OB_RENDER_DUPLI)) {
+                       if ((dob && !dob->animated) || (ob->transflag & OB_RENDER_DUPLI)) {
                                obr->flag |= R_INSTANCEABLE;
                                copy_m4_m4(obr->obmat, ob->obmat);
                        }
-                       if(dob)
+                       if (dob)
                                psys->flag |= PSYS_USE_IMAT;
                        init_render_object_data(re, obr, timeoffset);
                        psys_render_restore(ob, psys);
                        psys->flag &= ~PSYS_USE_IMAT;
 
                        /* only add instance for objects that have not been used for dupli */
-                       if(!(ob->transflag & OB_RENDER_DUPLI)) {
+                       if (!(ob->transflag & OB_RENDER_DUPLI)) {
                                obi= RE_addRenderInstance(re, obr, ob, par, index, psysindex, NULL, ob->lay);
-                               if(dob) set_dupli_tex_mat(re, obi, dob);
+                               if (dob) set_dupli_tex_mat(re, obi, dob);
                        }
                        else
                                find_dupli_instances(re, obr);
@@ -4616,9 +4618,9 @@ static void init_render_object(Render *re, Object *ob, Object *par, DupliObject
        double time;
        float mat[4][4];
 
-       if(ob->type==OB_LAMP)
+       if (ob->type==OB_LAMP)
                add_render_lamp(re, ob);
-       else if(render_object_type(ob->type))
+       else if (render_object_type(ob->type))
                add_render_object(re, ob, par, dob, timeoffset);
        else {
                mult_m4_m4m4(mat, re->viewmat, ob->obmat);
@@ -4626,7 +4628,7 @@ static void init_render_object(Render *re, Object *ob, Object *par, DupliObject
        }
        
        time= PIL_check_seconds_timer();
-       if(time - lasttime > 1.0) {
+       if (time - lasttime > 1.0) {
                lasttime= time;
                /* clumsy copying still */
                re->i.totvert= re->totvert;
@@ -4645,8 +4647,8 @@ void RE_Database_Free(Render *re)
        LampRen *lar;
        
        /* statistics for debugging render memory usage */
-       if((G.f & G_DEBUG) && (G.rendering)) {
-               if((re->r.scemode & R_PREVIEWBUTS)==0) {
+       if ((G.debug & G_DEBUG) && (G.rendering)) {
+               if ((re->r.scemode & R_PREVIEWBUTS)==0) {
                        BKE_image_print_memlist();
                        MEM_printmemlist_stats();
                }
@@ -4654,11 +4656,11 @@ void RE_Database_Free(Render *re)
 
        /* FREE */
        
-       for(lar= re->lampren.first; lar; lar= lar->next) {
+       for (lar= re->lampren.first; lar; lar= lar->next) {
                freeshadowbuf(lar);
-               if(lar->jitter) MEM_freeN(lar->jitter);
-               if(lar->shadsamp) MEM_freeN(lar->shadsamp);
-               if(lar->sunsky) MEM_freeN(lar->sunsky);
+               if (lar->jitter) MEM_freeN(lar->jitter);
+               if (lar->shadsamp) MEM_freeN(lar->shadsamp);
+               if (lar->sunsky) MEM_freeN(lar->sunsky);
                curvemapping_free(lar->curfalloff);
        }
        
@@ -4671,9 +4673,7 @@ void RE_Database_Free(Render *re)
 
        /* free orco */
        free_mesh_orco_hash(re);
-#if 0  /* radio can be redone better */
-       end_radio_render();
-#endif
+
        end_render_materials(re->main);
        end_render_textures(re);
        
@@ -4681,20 +4681,20 @@ void RE_Database_Free(Render *re)
        
        free_camera_inside_volumes(re);
        
-       if(re->wrld.aosphere) {
+       if (re->wrld.aosphere) {
                MEM_freeN(re->wrld.aosphere);
                re->wrld.aosphere= NULL;
                re->scene->world->aosphere= NULL;
        }
-       if(re->wrld.aotables) {
+       if (re->wrld.aotables) {
                MEM_freeN(re->wrld.aotables);
                re->wrld.aotables= NULL;
                re->scene->world->aotables= NULL;
        }
-       if(re->r.mode & R_RAYTRACE)
+       if (re->r.mode & R_RAYTRACE)
                free_render_qmcsampler(re);
        
-       if(re->r.mode & R_RAYTRACE) freeraytree(re);
+       if (re->r.mode & R_RAYTRACE) freeraytree(re);
 
        free_sss(re);
        free_occ(re);
@@ -4705,12 +4705,12 @@ void RE_Database_Free(Render *re)
 
        re->bakebuf= NULL;
 
-       if(re->scene)
-               if(re->scene->r.scemode & R_FREE_IMAGE)
-                       if((re->r.scemode & R_PREVIEWBUTS)==0)
+       if (re->scene)
+               if (re->scene->r.scemode & R_FREE_IMAGE)
+                       if ((re->r.scemode & R_PREVIEWBUTS)==0)
                                BKE_image_free_all_textures();
 
-       if(re->memArena) {
+       if (re->memArena) {
                BLI_memarena_free(re->memArena);
                re->memArena = NULL;
        }
@@ -4719,19 +4719,19 @@ void RE_Database_Free(Render *re)
 static int allow_render_object(Render *re, Object *ob, int nolamps, int onlyselected, Object *actob)
 {
        /* override not showing object when duplis are used with particles */
-       if(ob->transflag & OB_DUPLIPARTS)
+       if (ob->transflag & OB_DUPLIPARTS)
                ; /* let particle system(s) handle showing vs. not showing */
-       else if((ob->transflag & OB_DUPLI) && !(ob->transflag & OB_DUPLIFRAMES))
+       else if ((ob->transflag & OB_DUPLI) && !(ob->transflag & OB_DUPLIFRAMES))
                return 0;
        
        /* don't add non-basic meta objects, ends up having renderobjects with no geometry */
-       if (ob->type == OB_MBALL && ob!=find_basis_mball(re->scene, ob))
+       if (ob->type == OB_MBALL && ob!=BKE_metaball_basis_find(re->scene, ob))
                return 0;
        
-       if(nolamps && (ob->type==OB_LAMP))
+       if (nolamps && (ob->type==OB_LAMP))
                return 0;
        
-       if(onlyselected && (ob!=actob && !(ob->flag & SELECT)))
+       if (onlyselected && (ob!=actob && !(ob->flag & SELECT)))
                return 0;
        
        return 1;
@@ -4747,16 +4747,16 @@ static int allow_render_dupli_instance(Render *UNUSED(re), DupliObject *dob, Obj
         * all halo's to sort them globally in advance */
        totmaterial= give_totcolp(obd);
 
-       if(totmaterial) {
-               for(a= 0; a<*totmaterial; a++) {
+       if (totmaterial) {
+               for (a= 0; a<*totmaterial; a++) {
                        ma= give_current_material(obd, a);
-                       if(ma && (ma->material_type == MA_TYPE_HALO))
+                       if (ma && (ma->material_type == MA_TYPE_HALO))
                                return 0;
                }
        }
 
-       for(psys=obd->particlesystem.first; psys; psys=psys->next)
-               if(!ELEM5(psys->part->ren_as, PART_DRAW_BB, PART_DRAW_LINE, PART_DRAW_PATH, PART_DRAW_OB, PART_DRAW_GR))
+       for (psys=obd->particlesystem.first; psys; psys=psys->next)
+               if (!ELEM5(psys->part->ren_as, PART_DRAW_BB, PART_DRAW_LINE, PART_DRAW_PATH, PART_DRAW_OB, PART_DRAW_GR))
                        return 0;
 
        /* don't allow lamp, animated duplis, or radio render */
@@ -4773,35 +4773,35 @@ static void dupli_render_particle_set(Render *re, Object *ob, int timeoffset, in
        ParticleSystem *psys;
        DerivedMesh *dm;
 
-       if(level >= MAX_DUPLI_RECUR)
+       if (level >= MAX_DUPLI_RECUR)
                return;
        
-       if(ob->transflag & OB_DUPLIPARTS) {
-               for(psys=ob->particlesystem.first; psys; psys=psys->next) {
-                       if(ELEM(psys->part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
-                               if(enable)
+       if (ob->transflag & OB_DUPLIPARTS) {
+               for (psys=ob->particlesystem.first; psys; psys=psys->next) {
+                       if (ELEM(psys->part->ren_as, PART_DRAW_OB, PART_DRAW_GR)) {
+                               if (enable)
                                        psys_render_set(ob, psys, re->viewmat, re->winmat, re->winx, re->winy, timeoffset);
                                else
                                        psys_render_restore(ob, psys);
                        }
                }
 
-               if(enable) {
+               if (enable) {
                        /* this is to make sure we get render level duplis in groups:
-                       * the derivedmesh must be created before init_render_mesh,
-                       * since object_duplilist does dupliparticles before that */
+                        * the derivedmesh must be created before init_render_mesh,
+                        * since object_duplilist does dupliparticles before that */
                        dm = mesh_create_derived_render(re->scene, ob, CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
                        dm->release(dm);
 
-                       for(psys=ob->particlesystem.first; psys; psys=psys->next)
+                       for (psys=ob->particlesystem.first; psys; psys=psys->next)
                                psys_get_modifier(ob, psys)->flag &= ~eParticleSystemFlag_psys_updated;
                }
        }
 
-       if(ob->dup_group==NULL) return;
+       if (ob->dup_group==NULL) return;
        group= ob->dup_group;
 
-       for(go= group->gobject.first; go; go= go->next)
+       for (go= group->gobject.first; go; go= go->next)
                dupli_render_particle_set(re, go->ob, timeoffset, level+1, enable);
 }
 
@@ -4810,8 +4810,8 @@ static int get_vector_renderlayers(Scene *sce)
        SceneRenderLayer *srl;
        unsigned int lay= 0;
 
-       for(srl= sce->r.layers.first; srl; srl= srl->next)
-               if(srl->passflag & SCE_PASS_VECTOR)
+       for (srl= sce->r.layers.first; srl; srl= srl->next)
+               if (srl->passflag & SCE_PASS_VECTOR)
                        lay |= srl->lay;
 
        return lay;
@@ -4823,20 +4823,20 @@ static void add_group_render_dupli_obs(Render *re, Group *group, int nolamps, in
        Object *ob;
 
        /* simple preventing of too deep nested groups */
-       if(level>MAX_DUPLI_RECUR) return;
+       if (level>MAX_DUPLI_RECUR) return;
 
        /* recursively go into dupligroups to find objects with OB_RENDER_DUPLI
         * that were not created yet */
-       for(go= group->gobject.first; go; go= go->next) {
+       for (go= group->gobject.first; go; go= go->next) {
                ob= go->ob;
 
-               if(ob->flag & OB_DONE) {
-                       if(ob->transflag & OB_RENDER_DUPLI) {
-                               if(allow_render_object(re, ob, nolamps, onlyselected, actob)) {
+               if (ob->flag & OB_DONE) {
+                       if (ob->transflag & OB_RENDER_DUPLI) {
+                               if (allow_render_object(re, ob, nolamps, onlyselected, actob)) {
                                        init_render_object(re, ob, NULL, 0, timeoffset);
                                        ob->transflag &= ~OB_RENDER_DUPLI;
 
-                                       if(ob->dup_group)
+                                       if (ob->dup_group)
                                                add_group_render_dupli_obs(re, ob->dup_group, nolamps, onlyselected, actob, timeoffset, level+1);
                                }
                        }
@@ -4860,12 +4860,12 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
        set_dupli_tex_mat(NULL, NULL, NULL);
 
        /* loop over all objects rather then using SETLOOPER because we may
-        * reference an mtex-mapped object which isnt rendered or is an
+        * reference an mtex-mapped object which isn't rendered or is an
         * empty in a dupli group. We could scan all render material/lamp/world
         * mtex's for mapto objects but its easier just to set the
         * 'imat' / 'imat_ren' on all and unlikely to be a performance hit
-       * See bug: [#28744] - campbell */
-       for(ob= re->main->object.first; ob; ob= ob->id.next) {
+        * See bug: [#28744] - campbell */
+       for (ob= re->main->object.first; ob; ob= ob->id.next) {
                /* imat objects has to be done here, since displace can have texture using Object map-input */
                mult_m4_m4m4(mat, re->viewmat, ob->obmat);
                invert_m4_m4(ob->imat_ren, mat);
@@ -4875,7 +4875,7 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                ob->transflag &= ~OB_RENDER_DUPLI;
        }
 
-       for(SETLOOPER(re->scene, sce_iter, base)) {
+       for (SETLOOPER(re->scene, sce_iter, base)) {
                ob= base->object;
 
                /* in the prev/next pass for making speed vectors, avoid creating
@@ -4885,21 +4885,21 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                lay= (timeoffset)? renderlay & vectorlay: renderlay;
 
                /* if the object has been restricted from rendering in the outliner, ignore it */
-               if(ob->restrictflag & OB_RESTRICT_RENDER) continue;
+               if (ob->restrictflag & OB_RESTRICT_RENDER) continue;
 
                /* OB_DONE means the object itself got duplicated, so was already converted */
-               if(ob->flag & OB_DONE) {
+               if (ob->flag & OB_DONE) {
                        /* OB_RENDER_DUPLI means instances for it were already created, now
                         * it still needs to create the ObjectRen containing the data */
-                       if(ob->transflag & OB_RENDER_DUPLI) {
-                               if(allow_render_object(re, ob, nolamps, onlyselected, actob)) {
+                       if (ob->transflag & OB_RENDER_DUPLI) {
+                               if (allow_render_object(re, ob, nolamps, onlyselected, actob)) {
                                        init_render_object(re, ob, NULL, 0, timeoffset);
                                        ob->transflag &= ~OB_RENDER_DUPLI;
                                }
                        }
                }
-               else if((base->lay & lay) || (ob->type==OB_LAMP && (base->lay & re->lay)) ) {
-                       if((ob->transflag & OB_DUPLI) && (ob->type!=OB_MBALL)) {
+               else if ((base->lay & lay) || (ob->type==OB_LAMP && (base->lay & re->lay)) ) {
+                       if ((ob->transflag & OB_DUPLI) && (ob->type!=OB_MBALL)) {
                                DupliObject *dob;
                                ListBase *lb;
 
@@ -4909,25 +4909,25 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                                lb= object_duplilist(re->scene, ob);
                                dupli_render_particle_set(re, ob, timeoffset, 0, 0);
 
-                               for(dob= lb->first; dob; dob= dob->next) {
+                               for (dob= lb->first; dob; dob= dob->next) {
                                        Object *obd= dob->ob;
                                        
                                        copy_m4_m4(obd->obmat, dob->mat);
 
                                        /* group duplis need to set ob matrices correct, for deform. so no_draw is part handled */
-                                       if(!(obd->transflag & OB_RENDER_DUPLI) && dob->no_draw)
+                                       if (!(obd->transflag & OB_RENDER_DUPLI) && dob->no_draw)
                                                continue;
 
-                                       if(obd->restrictflag & OB_RESTRICT_RENDER)
+                                       if (obd->restrictflag & OB_RESTRICT_RENDER)
                                                continue;
 
-                                       if(obd->type==OB_MBALL)
+                                       if (obd->type==OB_MBALL)
                                                continue;
 
-                                       if(!allow_render_object(re, obd, nolamps, onlyselected, actob))
+                                       if (!allow_render_object(re, obd, nolamps, onlyselected, actob))
                                                continue;
 
-                                       if(allow_render_dupli_instance(re, dob, obd)) {
+                                       if (allow_render_dupli_instance(re, dob, obd)) {
                                                ParticleSystem *psys;
                                                ObjectRen *obr = NULL;
                                                int psysindex;
@@ -4938,13 +4938,13 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                                                /* instances instead of the actual object are added in two cases, either
                                                 * this is a duplivert/face/particle, or it is a non-animated object in
                                                 * a dupligroup that has already been created before */
-                                               if(dob->type != OB_DUPLIGROUP || (obr=find_dupligroup_dupli(re, obd, 0))) {
+                                               if (dob->type != OB_DUPLIGROUP || (obr=find_dupligroup_dupli(re, obd, 0))) {
                                                        mult_m4_m4m4(mat, re->viewmat, dob->mat);
                                                        obi= RE_addRenderInstance(re, NULL, obd, ob, dob->index, 0, mat, obd->lay);
 
                                                        /* fill in instance variables for texturing */
                                                        set_dupli_tex_mat(re, obi, dob);
-                                                       if(dob->type != OB_DUPLIGROUP) {
+                                                       if (dob->type != OB_DUPLIGROUP) {
                                                                copy_v3_v3(obi->dupliorco, dob->orco);
                                                                obi->dupliuv[0]= dob->uv[0];
                                                                obi->dupliuv[1]= dob->uv[1];
@@ -4955,7 +4955,7 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                                                                 * itself was duplicated. for the first case find_dupli_instances
                                                                 * will be called later. */
                                                                assign_dupligroup_dupli(re, obi, obr);
-                                                               if(obd->transflag & OB_RENDER_DUPLI)
+                                                               if (obd->transflag & OB_RENDER_DUPLI)
                                                                        find_dupli_instances(re, obr);
                                                        }
                                                }
@@ -4963,31 +4963,31 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                                                /* same logic for particles, each particle system has it's own object, so
                                                 * need to go over them separately */
                                                psysindex= 1;
-                                               for(psys=obd->particlesystem.first; psys; psys=psys->next) {
-                                                       if(dob->type != OB_DUPLIGROUP || (obr=find_dupligroup_dupli(re, obd, psysindex))) {
-                                                               if(obi == NULL)
+                                               for (psys=obd->particlesystem.first; psys; psys=psys->next) {
+                                                       if (dob->type != OB_DUPLIGROUP || (obr=find_dupligroup_dupli(re, obd, psysindex))) {
+                                                               if (obi == NULL)
                                                                        mult_m4_m4m4(mat, re->viewmat, dob->mat);
                                                                obi= RE_addRenderInstance(re, NULL, obd, ob, dob->index, psysindex++, mat, obd->lay);
 
                                                                set_dupli_tex_mat(re, obi, dob);
-                                                               if(dob->type != OB_DUPLIGROUP) {
+                                                               if (dob->type != OB_DUPLIGROUP) {
                                                                        copy_v3_v3(obi->dupliorco, dob->orco);
                                                                        obi->dupliuv[0]= dob->uv[0];
                                                                        obi->dupliuv[1]= dob->uv[1];
                                                                }
                                                                else {
                                                                        assign_dupligroup_dupli(re, obi, obr);
-                                                                       if(obd->transflag & OB_RENDER_DUPLI)
+                                                                       if (obd->transflag & OB_RENDER_DUPLI)
                                                                                find_dupli_instances(re, obr);
                                                                }
                                                        }
                                                }
 
-                                               if(obi==NULL)
+                                               if (obi==NULL)
                                                        /* can't instance, just create the object */
                                                        init_render_object(re, obd, ob, dob, timeoffset);
                                                
-                                               if(dob->type != OB_DUPLIGROUP) {
+                                               if (dob->type != OB_DUPLIGROUP) {
                                                        obd->flag |= OB_DONE;
                                                        obd->transflag |= OB_RENDER_DUPLI;
                                                }
@@ -4995,26 +4995,26 @@ static void database_init_objects(Render *re, unsigned int renderlay, int nolamp
                                        else
                                                init_render_object(re, obd, ob, dob, timeoffset);
                                        
-                                       if(re->test_break(re->tbh)) break;
+                                       if (re->test_break(re->tbh)) break;
                                }
                                free_object_duplilist(lb);
 
-                               if(allow_render_object(re, ob, nolamps, onlyselected, actob))
+                               if (allow_render_object(re, ob, nolamps, onlyselected, actob))
                                        init_render_object(re, ob, NULL, 0, timeoffset);
                        }
-                       else if(allow_render_object(re, ob, nolamps, onlyselected, actob))
+                       else if (allow_render_object(re, ob, nolamps, onlyselected, actob))
                                init_render_object(re, ob, NULL, 0, timeoffset);
                }
 
-               if(re->test_break(re->tbh)) break;
+               if (re->test_break(re->tbh)) break;
        }
 
        /* objects in groups with OB_RENDER_DUPLI set still need to be created,
         * since they may not be part of the scene */
-       for(group= re->main->group.first; group; group=group->id.next)
+       for (group= re->main->group.first; group; group=group->id.next)
                add_group_render_dupli_obs(re, group, nolamps, onlyselected, actob, timeoffset, 0);
 
-       if(!re->test_break(re->tbh))
+       if (!re->test_break(re->tbh))
                RE_makeRenderInstances(re);
 }
 
@@ -5046,18 +5046,18 @@ void RE_Database_FromScene(Render *re, Main *bmain, Scene *scene, unsigned int l
        re->i.partsdone= 0;     /* signal now in use for previewrender */
        
        /* in localview, lamps are using normal layers, objects only local bits */
-       if(re->lay & 0xFF000000)
+       if (re->lay & 0xFF000000)
                lay &= 0xFF000000;
        
        /* applies changes fully */
-       if((re->r.scemode & (R_NO_FRAME_UPDATE|R_PREVIEWBUTS))==0)
+       if ((re->r.scemode & (R_NO_FRAME_UPDATE|R_PREVIEWBUTS))==0)
                scene_update_for_newframe(re->main, re->scene, lay);
        
        /* if no camera, viewmat should have been set! */
-       if(use_camera_view && camera) {
-               /* called before but need to call again incase of lens animation from the
+       if (use_camera_view && camera) {
+               /* called before but need to call again in case of lens animation from the
                 * above call to scene_update_for_newframe, fixes bug. [#22702].
-                * following calls dont depend on 'RE_SetCamera' */
+                * following calls don't depend on 'RE_SetCamera' */
                RE_SetCamera(re, camera);
 
                normalize_m4(camera->obmat);
@@ -5067,10 +5067,10 @@ void RE_Database_FromScene(Render *re, Main *bmain, Scene *scene, unsigned int l
        }
        
        init_render_world(re);  /* do first, because of ambient. also requires re->osa set correct */
-       if(re->r.mode & R_RAYTRACE) {
+       if (re->r.mode & R_RAYTRACE) {
                init_render_qmcsampler(re);
 
-               if(re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT))
+               if (re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT))
                        if (re->wrld.ao_samp_method == WO_AOSAMP_CONSTANT)
                                init_ao_sphere(&re->wrld);
        }
@@ -5084,11 +5084,11 @@ void RE_Database_FromScene(Render *re, Main *bmain, Scene *scene, unsigned int l
        /* MAKE RENDER DATA */
        database_init_objects(re, lay, 0, 0, 0, 0);
        
-       if(!re->test_break(re->tbh)) {
+       if (!re->test_break(re->tbh)) {
                int tothalo;
 
                set_material_lightgroups(re);
-               for(sce= re->scene; sce; sce= sce->set)
+               for (sce= re->scene; sce; sce= sce->set)
                        set_renderlayer_lightgroups(re, sce);
                
                slurph_opt= 1;
@@ -5103,8 +5103,8 @@ void RE_Database_FromScene(Render *re, Main *bmain, Scene *scene, unsigned int l
                
                /* don't sort stars */
                tothalo= re->tothalo;
-               if(!re->test_break(re->tbh)) {
-                       if(re->wrld.mode & WO_STARS) {
+               if (!re->test_break(re->tbh)) {
+                       if (re->wrld.mode & WO_STARS) {
                                re->i.infostr= "Creating Starfield";
                                re->stats_draw(re->sdh, &re->i);
                                RE_make_stars(re, NULL, NULL, NULL, NULL);
@@ -5123,43 +5123,43 @@ void RE_Database_FromScene(Render *re, Main *bmain, Scene *scene, unsigned int l
                /* old code checked for internal render (aka not yafray) */
                {
                        /* raytree */
-                       if(!re->test_break(re->tbh)) {
-                               if(re->r.mode & R_RAYTRACE) {
+                       if (!re->test_break(re->tbh)) {
+                               if (re->r.mode & R_RAYTRACE) {
                                        makeraytree(re);
                                }
                        }
                        /* ENVIRONMENT MAPS */
-                       if(!re->test_break(re->tbh))
+                       if (!re->test_break(re->tbh))
                                make_envmaps(re);
                                
                        /* point density texture */
-                       if(!re->test_break(re->tbh))
+                       if (!re->test_break(re->tbh))
                                make_pointdensities(re);
                        /* voxel data texture */
-                       if(!re->test_break(re->tbh))
+                       if (!re->test_break(re->tbh))
                                make_voxeldata(re);
                }
                
-               if(!re->test_break(re->tbh))
+               if (!re->test_break(re->tbh))
                        project_renderdata(re, projectverto, re->r.mode & R_PANORAMA, 0, 1);
                
                /* Occlusion */
-               if((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && !re->test_break(re->tbh))
-                       if(re->wrld.ao_gather_method == WO_AOGATHER_APPROX)
-                               if(re->r.mode & R_SHADOW)
+               if ((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && !re->test_break(re->tbh))
+                       if (re->wrld.ao_gather_method == WO_AOGATHER_APPROX)
+                               if (re->r.mode & R_SHADOW)
                                        make_occ_tree(re);
 
                /* SSS */
-               if((re->r.mode & R_SSS) && !re->test_break(re->tbh))
+               if ((re->r.mode & R_SSS) && !re->test_break(re->tbh))
                        make_sss_tree(re);
                
-               if(!re->test_break(re->tbh))
-                       if(re->r.mode & R_RAYTRACE)
+               if (!re->test_break(re->tbh))
+                       if (re->r.mode & R_RAYTRACE)
                                volume_precache(re);
                
        }
        
-       if(re->test_break(re->tbh))
+       if (re->test_break(re->tbh))
                RE_Database_Free(re);
        else
                re->i.convertdone= 1;
@@ -5201,7 +5201,7 @@ static void database_fromscene_vectors(Render *re, Scene *scene, unsigned int la
        slurph_opt= 0;
        
        /* in localview, lamps are using normal layers, objects only local bits */
-       if(re->lay & 0xFF000000)
+       if (re->lay & 0xFF000000)
                lay &= 0xFF000000;
        
        /* applies changes fully */
@@ -5209,7 +5209,7 @@ static void database_fromscene_vectors(Render *re, Scene *scene, unsigned int la
        scene_update_for_newframe(re->main, re->scene, lay);
        
        /* if no camera, viewmat should have been set! */
-       if(camera) {
+       if (camera) {
                normalize_m4(camera->obmat);
                invert_m4_m4(mat, camera->obmat);
                RE_SetView(re, mat);
@@ -5218,7 +5218,7 @@ static void database_fromscene_vectors(Render *re, Scene *scene, unsigned int la
        /* MAKE RENDER DATA */
        database_init_objects(re, lay, 0, 0, 0, timeoffset);
        
-       if(!re->test_break(re->tbh))
+       if (!re->test_break(re->tbh))
                project_renderdata(re, projectverto, re->r.mode & R_PANORAMA, 0, 1);
 
        /* do this in end, particles for example need cfra */
@@ -5233,11 +5233,11 @@ static void speedvector_project(Render *re, float zco[2], const float co[3], con
        float div;
        
        /* initialize */
-       if(re) {
+       if (re) {
                pano= re->r.mode & R_PANORAMA;
                
                /* precalculate amount of radians 1 pixel rotates */
-               if(pano) {
+               if (pano) {
                        /* size of 1 pixel mapped to viewplane coords */
                        float psize= (re->viewplane.xmax-re->viewplane.xmin)/(float)re->winx;
                        /* x angle of a pixel */
@@ -5254,17 +5254,17 @@ static void speedvector_project(Render *re, float zco[2], const float co[3], con
        }
        
        /* now map hocos to screenspace, uses very primitive clip still */
-       if(ho[3]<0.1f) div= 10.0f;
+       if (ho[3]<0.1f) div= 10.0f;
        else div= 1.0f/ho[3];
        
        /* use cylinder projection */
-       if(pano) {
+       if (pano) {
                float vec[3], ang;
-               /* angle between (0,0,-1) and (co) */
+               /* angle between (0, 0, -1) and (co) */
                copy_v3_v3(vec, co);
 
                ang= saacos(-vec[2]/sqrtf(vec[0]*vec[0] + vec[2]*vec[2]));
-               if(vec[0]<0.0f) ang= -ang;
+               if (vec[0]<0.0f) ang= -ang;
                zco[0]= ang/pixelphix + zmulx;
                
                ang= 0.5f*(float)M_PI - saacos(vec[1] / len_v3(vec));
@@ -5287,21 +5287,21 @@ static void calculate_speedvector(const float vectors[2], int step, float winsq,
        zco[1]= vectors[1] - zco[1];
        
        /* enable nice masks for hardly moving stuff or float inaccuracy */
-       if(zco[0]<0.1f && zco[0]>-0.1f && zco[1]<0.1f && zco[1]>-0.1f ) {
+       if (zco[0]<0.1f && zco[0]>-0.1f && zco[1]<0.1f && zco[1]>-0.1f ) {
                zco[0]= 0.0f;
                zco[1]= 0.0f;
        }
        
        /* maximize speed for image width, otherwise it never looks good */
        len= zco[0]*zco[0] + zco[1]*zco[1];
-       if(len > winsq) {
+       if (len > winsq) {
                len= winroot/sqrtf(len);
                zco[0]*= len;
                zco[1]*= len;
        }
        
        /* note; in main vecblur loop speedvec is negated again */
-       if(step) {
+       if (step) {
                speed[2]= -zco[0];
                speed[3]= -zco[1];
        }
@@ -5313,21 +5313,21 @@ static void calculate_speedvector(const float vectors[2], int step, float winsq,
 
 static float *calculate_strandsurface_speedvectors(Render *re, ObjectInstanceRen *obi, StrandSurface *mesh)
 {
-       if(mesh->co && mesh->prevco && mesh->nextco) {
+       if (mesh->co && mesh->prevco && mesh->nextco) {
                float winsq= (float)re->winx*(float)re->winy; /* int's can wrap on large images */
                float winroot= sqrt(winsq);
                float (*winspeed)[4];
                float ho[4], prevho[4], nextho[4], winmat[4][4], vec[2];
                int a;
 
-               if(obi->flag & R_TRANSFORMED)
+               if (obi->flag & R_TRANSFORMED)
                        mult_m4_m4m4(winmat, re->winmat, obi->mat);
                else
                        copy_m4_m4(winmat, re->winmat);
 
                winspeed= MEM_callocN(sizeof(float)*4*mesh->totvert, "StrandSurfWin");
 
-               for(a=0; a<mesh->totvert; a++) {
+               for (a=0; a<mesh->totvert; a++) {
                        projectvert(mesh->co[a], winmat, ho);
 
                        projectvert(mesh->prevco[a], winmat, prevho);
@@ -5357,14 +5357,14 @@ static void calculate_speedvectors(Render *re, ObjectInstanceRen *obi, float *ve
        float winsq= (float)re->winx*(float)re->winy, winroot= sqrt(winsq);  /* int's can wrap on large images */
        int a, *face, *index;
 
-       if(obi->flag & R_TRANSFORMED)
+       if (obi->flag & R_TRANSFORMED)
                mult_m4_m4m4(winmat, re->winmat, obi->mat);
        else
                copy_m4_m4(winmat, re->winmat);
 
-       if(obr->vertnodes) {
-               for(a=0; a<obr->totvert; a++, vectors+=2) {
-                       if((a & 255)==0) ver= obr->vertnodes[a>>8].vert;
+       if (obr->vertnodes) {
+               for (a=0; a<obr->totvert; a++, vectors+=2) {
+                       if ((a & 255)==0) ver= obr->vertnodes[a>>8].vert;
                        else ver++;
 
                        speed= RE_vertren_get_winspeed(obi, ver, 1);
@@ -5373,21 +5373,21 @@ static void calculate_speedvectors(Render *re, ObjectInstanceRen *obi, float *ve
                }
        }
 
-       if(obr->strandnodes) {
+       if (obr->strandnodes) {
                strandbuf= obr->strandbuf;
                mesh= (strandbuf)? strandbuf->surface: NULL;
 
                /* compute speed vectors at surface vertices */
-               if(mesh)
+               if (mesh)
                        winspeed= (float(*)[4])calculate_strandsurface_speedvectors(re, obi, mesh);
 
-               if(winspeed) {
-                       for(a=0; a<obr->totstrand; a++, vectors+=2) {
-                               if((a & 255)==0) strand= obr->strandnodes[a>>8].strand;
+               if (winspeed) {
+                       for (a=0; a<obr->totstrand; a++, vectors+=2) {
+                               if ((a & 255)==0) strand= obr->strandnodes[a>>8].strand;
                                else strand++;
 
                                index= RE_strandren_get_face(obr, strand, 0);
-                               if(index && *index < mesh->totface) {
+                               if (index && *index < mesh->totface) {
                                        speed= RE_strandren_get_winspeed(obi, strand, 1);
 
                                        /* interpolate speed vectors from strand surface */
@@ -5398,13 +5398,13 @@ static void calculate_speedvectors(Render *re, ObjectInstanceRen *obi, float *ve
                                        co3= mesh->co[face[2]];
                                        co4= (face[3])? mesh->co[face[3]]: NULL;
 
-                                       interp_weights_face_v3( w,co1, co2, co3, co4, strand->vert->co);
+                                       interp_weights_face_v3(w, co1, co2, co3, co4, strand->vert->co);
 
                                        zero_v4(speed);
                                        madd_v4_v4fl(speed, winspeed[face[0]], w[0]);
                                        madd_v4_v4fl(speed, winspeed[face[1]], w[1]);
                                        madd_v4_v4fl(speed, winspeed[face[2]], w[2]);
-                                       if(face[3])
+                                       if (face[3])
                                                madd_v4_v4fl(speed, winspeed[face[3]], w[3]);
                                }
                        }
@@ -5431,9 +5431,9 @@ static int load_fluidsimspeedvectors(Render *re, ObjectInstanceRen *obi, float *
        FluidVertexVelocity *velarray = NULL;
        
        /* only one step needed */
-       if(step) return 1;
+       if (step) return 1;
        
-       if(fluidmd)
+       if (fluidmd)
                fss = fluidmd->fss;
        else
                return 0;
@@ -5442,45 +5442,44 @@ static int load_fluidsimspeedvectors(Render *re, ObjectInstanceRen *obi, float *
        invert_m4_m4(imat, mat);
 
        /* set first vertex OK */
-       if(!fss->meshVelocities) return 0;
+       if (!fss->meshVelocities) return 0;
        
-       if( obr->totvert != fss->totvert) {
+       if ( obr->totvert != fss->totvert) {
                //fprintf(stderr, "load_fluidsimspeedvectors - modified fluidsim mesh, not using speed vectors (%d,%d)...\n", obr->totvert, fsob->fluidsimSettings->meshSurface->totvert); // DEBUG
                return 0;
        }
        
        velarray = fss->meshVelocities;
 
-       if(obi->flag & R_TRANSFORMED)
+       if (obi->flag & R_TRANSFORMED)
                mult_m4_m4m4(winmat, re->winmat, obi->mat);
        else
                copy_m4_m4(winmat, re->winmat);
        
        /* (bad) HACK calculate average velocity */
        /* better solution would be fixing getVelocityAt() in intern/elbeem/intern/solver_util.cpp
-       so that also small drops/little water volumes return a velocity != 0. 
-       But I had no luck in fixing that function - DG */
-       for(a=0; a<obr->totvert; a++) {
-               for(j=0;j<3;j++) avgvel[j] += velarray[a].vel[j];
+        * so that also small drops/little water volumes return a velocity != 0.
+        * But I had no luck in fixing that function - DG */
+       for (a=0; a<obr->totvert; a++) {
+               for (j=0;j<3;j++) avgvel[j] += velarray[a].vel[j];
                
        }
-       for(j=0;j<3;j++) avgvel[j] /= (float)(obr->totvert);
+       for (j=0;j<3;j++) avgvel[j] /= (float)(obr->totvert);
        
        
-       for(a=0; a<obr->totvert; a++, vectors+=2) {
-               if((a & 255)==0)
+       for (a=0; a<obr->totvert; a++, vectors+=2) {
+               if ((a & 255)==0)
                        ver= obr->vertnodes[a>>8].vert;
                else
                        ver++;
 
                // get fluid velocity
-               fsvec[3] = 0.
-               //fsvec[0] = fsvec[1] = fsvec[2] = fsvec[3] = 0.; fsvec[2] = 2.; // NT fixed test
-               for(j=0;j<3;j++) fsvec[j] = velarray[a].vel[j];
+               fsvec[3] = 0.0f;
+               //fsvec[0] = fsvec[1] = fsvec[2] = fsvec[3] = 0.0; fsvec[2] = 2.0f; // NT fixed test
+               for (j=0;j<3;j++) fsvec[j] = velarray[a].vel[j];
                
                /* (bad) HACK insert average velocity if none is there (see previous comment) */
-               if((fsvec[0] == 0.0f) && (fsvec[1] == 0.0f) && (fsvec[2] == 0.0f))
-               {
+               if ((fsvec[0] == 0.0f) && (fsvec[1] == 0.0f) && (fsvec[2] == 0.0f)) {
                        fsvec[0] = avgvel[0];
                        fsvec[1] = avgvel[1];
                        fsvec[2] = avgvel[2];
@@ -5497,14 +5496,14 @@ static int load_fluidsimspeedvectors(Render *re, ObjectInstanceRen *obi, float *
                
                /* now map hocos to screenspace, uses very primitive clip still */
                // use ho[3] of original vertex, xy component of vel. direction
-               if(ho[3]<0.1f) div= 10.0f;
+               if (ho[3]<0.1f) div= 10.0f;
                else div= 1.0f/ho[3];
                zco[0]= zmulx*hoco[0]*div;
                zco[1]= zmuly*hoco[1]*div;
                
                // maximize speed as usual
                len= zco[0]*zco[0] + zco[1]*zco[1];
-               if(len > winsq) {
+               if (len > winsq) {
                        len= winroot/sqrtf(len);
                        zco[0]*= len; zco[1]*= len;
                }
@@ -5513,7 +5512,7 @@ static int load_fluidsimspeedvectors(Render *re, ObjectInstanceRen *obi, float *
                // set both to the same value
                speed[0]= speed[2]= zco[0];
                speed[1]= speed[3]= zco[1];
-               //if(a<20) fprintf(stderr,"speed %d %f,%f | camco %f,%f,%f | hoco %f,%f,%f,%f  \n", a, speed[0], speed[1], camco[0],camco[1], camco[2], hoco[0],hoco[1], hoco[2],hoco[3]); // NT DEBUG
+               //if (a < 20) fprintf(stderr,"speed %d %f,%f | camco %f,%f,%f | hoco %f,%f,%f,%f\n", a, speed[0], speed[1], camco[0],camco[1], camco[2], hoco[0],hoco[1], hoco[2],hoco[3]); // NT DEBUG
        }
 
        return 1;
@@ -5529,7 +5528,7 @@ static void copy_dbase_object_vectors(Render *re, ListBase *lb)
        float *vec, ho[4], winmat[4][4];
        int a, totvector;
 
-       for(obi= re->instancetable.first; obi; obi= obi->next) {
+       for (obi= re->instancetable.first; obi; obi= obi->next) {
                obr= obi->obr;
 
                obilb= MEM_mallocN(sizeof(ObjectInstanceRen), "ObInstanceVector");
@@ -5538,16 +5537,16 @@ static void copy_dbase_object_vectors(Render *re, ListBase *lb)
 
                obilb->totvector= totvector= obr->totvert;
 
-               if(totvector > 0) {
+               if (totvector > 0) {
                        vec= obilb->vectors= MEM_mallocN(2*sizeof(float)*totvector, "vector array");
 
-                       if(obi->flag & R_TRANSFORMED)
+                       if (obi->flag & R_TRANSFORMED)
                                mult_m4_m4m4(winmat, re->winmat, obi->mat);
                        else
                                copy_m4_m4(winmat, re->winmat);
 
-                       for(a=0; a<obr->totvert; a++, vec+=2) {
-                               if((a & 255)==0) ver= obr->vertnodes[a>>8].vert;
+                       for (a=0; a<obr->totvert; a++, vec+=2) {
+                               if ((a & 255)==0) ver= obr->vertnodes[a>>8].vert;
                                else ver++;
                                
                                projectvert(ver->co, winmat, ho);
@@ -5561,8 +5560,8 @@ static void free_dbase_object_vectors(ListBase *lb)
 {
        ObjectInstanceRen *obi;
        
-       for(obi= lb->first; obi; obi= obi->next)
-               if(obi->vectors)
+       for (obi= lb->first; obi; obi= obi->next)
+               if (obi->vectors)
                        MEM_freeN(obi->vectors);
        BLI_freelistN(lb);
 }
@@ -5593,7 +5592,7 @@ void RE_Database_FromScene_Vectors(Render *re, Main *bmain, Scene *sce, unsigned
        RE_Database_Free(re);
        re->strandsurface= strandsurface;
        
-       if(!re->test_break(re->tbh)) {
+       if (!re->test_break(re->tbh)) {
                /* creates entire dbase */
                re->i.infostr= "Calculating next frame vectors";
                
@@ -5608,55 +5607,55 @@ void RE_Database_FromScene_Vectors(Render *re, Main *bmain, Scene *sce, unsigned
        RE_Database_Free(re);
        re->strandsurface= strandsurface;
        
-       if(!re->test_break(re->tbh))
+       if (!re->test_break(re->tbh))
                RE_Database_FromScene(re, bmain, sce, lay, 1);
        
-       if(!re->test_break(re->tbh)) {
+       if (!re->test_break(re->tbh)) {
                int vectorlay= get_vector_renderlayers(re->scene);
 
-               for(step= 0; step<2; step++) {
+               for (step= 0; step<2; step++) {
                        
-                       if(step)
+                       if (step)
                                table= &newtable;
                        else
                                table= &oldtable;
                        
                        oldobi= table->first;
-                       for(obi= re->instancetable.first; obi && oldobi; obi= obi->next) {
+                       for (obi= re->instancetable.first; obi && oldobi; obi= obi->next) {
                                int ok= 1;
                                FluidsimModifierData *fluidmd;
 
-                               if(!(obi->lay & vectorlay))
+                               if (!(obi->lay & vectorlay))
                                        continue;
 
                                obi->totvector= obi->obr->totvert;
 
                                /* find matching object in old table */
-                               if(oldobi->ob!=obi->ob || oldobi->par!=obi->par || oldobi->index!=obi->index || oldobi->psysindex!=obi->psysindex) {
+                               if (oldobi->ob!=obi->ob || oldobi->par!=obi->par || oldobi->index!=obi->index || oldobi->psysindex!=obi->psysindex) {
                                        ok= 0;
-                                       for(oldobi= table->first; oldobi; oldobi= oldobi->next)
-                                               if(oldobi->ob==obi->ob && oldobi->par==obi->par && oldobi->index==obi->index && oldobi->psysindex==obi->psysindex)
+                                       for (oldobi= table->first; oldobi; oldobi= oldobi->next)
+                                               if (oldobi->ob==obi->ob && oldobi->par==obi->par && oldobi->index==obi->index && oldobi->psysindex==obi->psysindex)
                                                        break;
-                                       if(oldobi==NULL)
+                                       if (oldobi==NULL)
                                                oldobi= table->first;
                                        else
                                                ok= 1;
                                }
-                               if(ok==0) {
+                               if (ok==0) {
                                         printf("speed table: missing object %s\n", obi->ob->id.name+2);
                                        continue;
                                }
 
                                // NT check for fluidsim special treatment
                                fluidmd = (FluidsimModifierData *)modifiers_findByType(obi->ob, eModifierType_Fluidsim);
-                               if(fluidmd && fluidmd->fss && (fluidmd->fss->type & OB_FLUIDSIM_DOMAIN)) {
-                                       // use preloaded per vertex simulation data , only does calculation for step=1
+                               if (fluidmd && fluidmd->fss && (fluidmd->fss->type & OB_FLUIDSIM_DOMAIN)) {
+                                       // use preloaded per vertex simulation data, only does calculation for step=1
                                        // NOTE/FIXME - velocities and meshes loaded unnecessarily often during the database_fromscene_vectors calls...
                                        load_fluidsimspeedvectors(re, obi, oldobi->vectors, step);
                                }
                                else {
                                        /* check if both have same amounts of vertices */
-                                       if(obi->totvector==oldobi->totvector)
+                                       if (obi->totvector==oldobi->totvector)
                                                calculate_speedvectors(re, obi, oldobi->vectors, step);
                                        else
                                                printf("Warning: object %s has different amount of vertices or strands on other frame\n", obi->ob->id.name+2);
@@ -5670,12 +5669,12 @@ void RE_Database_FromScene_Vectors(Render *re, Main *bmain, Scene *sce, unsigned
        free_dbase_object_vectors(&oldtable);
        free_dbase_object_vectors(&newtable);
 
-       for(mesh=re->strandsurface.first; mesh; mesh=mesh->next) {
-               if(mesh->prevco) {
+       for (mesh=re->strandsurface.first; mesh; mesh=mesh->next) {
+               if (mesh->prevco) {
                        MEM_freeN(mesh->prevco);
                        mesh->prevco= NULL;
                }
-               if(mesh->nextco) {
+               if (mesh->nextco) {
                        MEM_freeN(mesh->nextco);
                        mesh->nextco= NULL;
                }
@@ -5692,14 +5691,14 @@ void RE_Database_FromScene_Vectors(Render *re, Main *bmain, Scene *sce, unsigned
 
 /* setup for shaded view or bake, so only lamps and materials are initialized */
 /* type:
  RE_BAKE_LIGHT:  for shaded view, only add lamps
  RE_BAKE_ALL:    for baking, all lamps and objects
  RE_BAKE_NORMALS:for baking, no lamps and only selected objects
  RE_BAKE_AO:     for baking, no lamps, but all objects
  RE_BAKE_TEXTURE:for baking, no lamps, only selected objects
  RE_BAKE_DISPLACEMENT:for baking, no lamps, only selected objects
  RE_BAKE_SHADOW: for baking, only shadows, but all objects
-*/
* RE_BAKE_LIGHT:  for shaded view, only add lamps
* RE_BAKE_ALL:    for baking, all lamps and objects
* RE_BAKE_NORMALS:for baking, no lamps and only selected objects
* RE_BAKE_AO:     for baking, no lamps, but all objects
* RE_BAKE_TEXTURE:for baking, no lamps, only selected objects
* RE_BAKE_DISPLACEMENT:for baking, no lamps, only selected objects
* RE_BAKE_SHADOW: for baking, only shadows, but all objects
+ */
 void RE_Database_Baking(Render *re, Main *bmain, Scene *scene, unsigned int lay, const int type, Object *actob)
 {
        Object *camera;
@@ -5719,18 +5718,18 @@ void RE_Database_Baking(Render *re, Main *bmain, Scene *scene, unsigned int lay,
        
        re->flag |= R_BAKING;
        re->excludeob= actob;
-       if(actob)
+       if (actob)
                re->flag |= R_BAKE_TRACE;
 
-       if(type==RE_BAKE_NORMALS && re->r.bake_normal_space==R_BAKE_SPACE_TANGENT)
+       if (type==RE_BAKE_NORMALS && re->r.bake_normal_space==R_BAKE_SPACE_TANGENT)
                re->flag |= R_NEED_TANGENT;
        
-       if(!actob && ELEM4(type, RE_BAKE_LIGHT, RE_BAKE_NORMALS, RE_BAKE_TEXTURE, RE_BAKE_DISPLACEMENT)) {
+       if (!actob && ELEM4(type, RE_BAKE_LIGHT, RE_BAKE_NORMALS, RE_BAKE_TEXTURE, RE_BAKE_DISPLACEMENT)) {
                re->r.mode &= ~R_SHADOW;
                re->r.mode &= ~R_RAYTRACE;
        }
        
-       if(!actob && (type==RE_BAKE_SHADOW)) {
+       if (!actob && (type==RE_BAKE_SHADOW)) {
                re->r.mode |= R_SHADOW;
        }
        
@@ -5742,13 +5741,13 @@ void RE_Database_Baking(Render *re, Main *bmain, Scene *scene, unsigned int lay,
        re->lampren.first= re->lampren.last= NULL;
 
        /* in localview, lamps are using normal layers, objects only local bits */
-       if(re->lay & 0xFF000000)
+       if (re->lay & 0xFF000000)
                lay &= 0xFF000000;
        
        camera= RE_GetCamera(re);
        
        /* if no camera, set unit */
-       if(camera) {
+       if (camera) {
                normalize_m4(camera->obmat);
                invert_m4_m4(mat, camera->obmat);
                RE_SetView(re, mat);
@@ -5767,10 +5766,10 @@ void RE_Database_Baking(Render *re, Main *bmain, Scene *scene, unsigned int lay,
        /* done setting dummy values */
 
        init_render_world(re);  /* do first, because of ambient. also requires re->osa set correct */
-       if(re->r.mode & R_RAYTRACE) {
+       if (re->r.mode & R_RAYTRACE) {
                init_render_qmcsampler(re);
                
-               if(re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT))
+               if (re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT))
                        if (re->wrld.ao_samp_method == WO_AOSAMP_CONSTANT)
                                init_ao_sphere(&re->wrld);
        }
@@ -5789,27 +5788,27 @@ void RE_Database_Baking(Render *re, Main *bmain, Scene *scene, unsigned int lay,
        set_material_lightgroups(re);
        
        /* SHADOW BUFFER */
-       if(type!=RE_BAKE_LIGHT)
-               if(re->r.mode & R_SHADOW)
+       if (type!=RE_BAKE_LIGHT)
+               if (re->r.mode & R_SHADOW)
                        threaded_makeshadowbufs(re);
 
        /* raytree */
-       if(!re->test_break(re->tbh))
-               if(re->r.mode & R_RAYTRACE)
+       if (!re->test_break(re->tbh))
+               if (re->r.mode & R_RAYTRACE)
                        makeraytree(re);
        
        /* point density texture */
-       if(!re->test_break(re->tbh))
+       if (!re->test_break(re->tbh))
                make_pointdensities(re);
 
        /* voxel data texture */
-       if(!re->test_break(re->tbh))
+       if (!re->test_break(re->tbh))
                make_voxeldata(re);
 
        /* occlusion */
-       if((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && !re->test_break(re->tbh))
-               if(re->wrld.ao_gather_method == WO_AOGATHER_APPROX)
-                       if(re->r.mode & R_SHADOW)
+       if ((re->wrld.mode & (WO_AMB_OCC|WO_ENV_LIGHT|WO_INDIRECT_LIGHT)) && !re->test_break(re->tbh))
+               if (re->wrld.ao_gather_method == WO_AOGATHER_APPROX)
+                       if (re->r.mode & R_SHADOW)
                                make_occ_tree(re);
 }
 
@@ -5829,19 +5828,19 @@ void RE_make_sticky(Scene *scene, View3D *v3d)
        int a;
        Object *camera= NULL;
 
-       if(v3d==NULL) {
+       if (v3d==NULL) {
                printf("Need a 3d view to make sticky\n");
                return;
        }
 
-       if(v3d)                         camera= V3D_CAMERA_LOCAL(v3d);
-       if(camera == NULL)      camera= scene->camera;
+       if (v3d)                                camera= V3D_CAMERA_LOCAL(v3d);
+       if (camera == NULL)     camera= scene->camera;
 
-       if(camera==NULL) {
+       if (camera==NULL) {
                printf("Need camera to make sticky\n");
                return;
        }
-       if(scene->obedit) {
+       if (scene->obedit) {
                printf("Unable to make sticky in Edit Mode\n");
                return;
        }
@@ -5857,23 +5856,23 @@ void RE_make_sticky(Scene *scene, View3D *v3d)
        invert_m4_m4(mat, camera->obmat);
        RE_SetView(re, mat);
        
-       for(base= FIRSTBASE; base; base= base->next) {
-               if TESTBASELIB(v3d, base) {
-                       if(base->object->type==OB_MESH) {
+       for (base= FIRSTBASE; base; base= base->next) {
+               if (TESTBASELIB(v3d, base)) {
+                       if (base->object->type==OB_MESH) {
                                ob= base->object;
                                
                                me= ob->data;
                                mvert= me->mvert;
-                               if(me->msticky)
+                               if (me->msticky)
                                        CustomData_free_layer_active(&me->vdata, CD_MSTICKY, me->totvert);
                                me->msticky= CustomData_add_layer(&me->vdata, CD_MSTICKY,
                                        CD_CALLOC, NULL, me->totvert);
                                
-                               where_is_object(scene, ob);
+                               BKE_object_where_is_calc(scene, ob);
                                mult_m4_m4m4(mat, re->viewmat, ob->obmat);
                                
                                ms= me->msticky;
-                               for(a=0; a<me->totvert; a++, ms++, mvert++) {
+                               for (a=0; a<me->totvert; a++, ms++, mvert++) {
                                        copy_v3_v3(ho, mvert->co);
                                        mul_m4_v3(mat, ho);
                                        projectverto(ho, re->winmat, ho);