style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / render / intern / source / shadbuf.c
index 49e331cc44c23886c8913bfb61b77a18427a39b0..25705a9fc995f33f8c287bb40b8f7c5dbaf070b7 100644 (file)
@@ -91,14 +91,14 @@ static void copy_to_ztile(int *rectz, int size, int x1, int y1, int tile, char *
        
        x2= x1+tile;
        y2= y1+tile;
-       if(x2>=size) x2= size-1;
-       if(y2>=size) y2= size-1;
+       if (x2>=size) x2= size-1;
+       if (y2>=size) y2= size-1;
 
-       if(x1>=x2 || y1>=y2) return;
+       if (x1>=x2 || y1>=y2) return;
 
        len4= 4*(x2- x1);
        rz= rectz + size*y1 + x1;
-       for(; y1<y2; y1++) {
+       for (; y1<y2; y1++) {
                memcpy(r1, rz, len4);
                rz+= size;
                r1+= len4;
@@ -132,12 +132,12 @@ static float *give_jitter_tab(int samp)
        static char ctab[17]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int a, offset=0;
        
-       if(samp<2) samp= 2;
-       else if(samp>16) samp= 16;
+       if (samp<2) samp= 2;
+       else if (samp>16) samp= 16;
 
-       for(a=0; a<samp-1; a++) offset+= tab[a];
+       for (a=0; a<samp-1; a++) offset+= tab[a];
 
-       if(ctab[samp]==0) {
+       if (ctab[samp]==0) {
                ctab[samp]= 1;
                BLI_initjit(jit[offset], samp*samp);
        }
@@ -154,10 +154,10 @@ static void make_jitter_weight_tab(Render *re, ShadBuf *shb, short filtertype)
        
        shb->weight= MEM_mallocN(sizeof(float)*tot, "weight tab lamp");
        
-       for(jit= shb->jit, a=0; a<tot; a++, jit+=2) {
-               if(filtertype==LA_SHADBUF_TENT) 
+       for (jit= shb->jit, a=0; a<tot; a++, jit+=2) {
+               if (filtertype==LA_SHADBUF_TENT)
                        shb->weight[a]= 0.71f - sqrt(jit[0]*jit[0] + jit[1]*jit[1]);
-               else if(filtertype==LA_SHADBUF_GAUSS) 
+               else if (filtertype==LA_SHADBUF_GAUSS)
                        shb->weight[a]= RE_filter_value(R_FILTER_GAUSS, 1.8f*sqrt(jit[0]*jit[0] + jit[1]*jit[1]));
                else
                        shb->weight[a]= 1.0f;
@@ -166,7 +166,7 @@ static void make_jitter_weight_tab(Render *re, ShadBuf *shb, short filtertype)
        }
        
        totw= 1.0f/totw;
-       for(a=0; a<tot; a++) {
+       for (a=0; a<tot; a++) {
                shb->weight[a]*= totw;
        }
 }
@@ -176,8 +176,8 @@ static int verg_deepsample(const void *poin1, const void *poin2)
        const DeepSample *ds1= (const DeepSample*)poin1;
        const DeepSample *ds2= (const DeepSample*)poin2;
 
-       if(ds1->z < ds2->z) return -1;
-       else if(ds1->z == ds2->z) return 0;
+       if (ds1->z < ds2->z) return -1;
+       else if (ds1->z == ds2->z) return 0;
        else return 1;
 }
 
@@ -191,8 +191,8 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
        int a, first, z, newtot= 0;
 
 #if 0
-       if(print) {
-               for(a=0, ds=dsample; a<tot; a++, ds++)
+       if (print) {
+               for (a=0, ds=dsample; a<tot; a++, ds++)
                        printf("%lf,%f ", ds->z/(double)0x7FFFFFFF, ds->v);
                printf("\n");
        }
@@ -204,17 +204,17 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
        a= 0;
 
        /* as long as we are not at the end of the array */
-       for(a++, ds++; a<tot; a++, ds++) {
+       for (a++, ds++; a<tot; a++, ds++) {
                slopemin= 0.0f;
                slopemax= 0.0f;
                first= 1;
 
-               for(; a<tot; a++, ds++) {
+               for (; a<tot; a++, ds++) {
                        //dz= ds->z - newds->z;
-                       if(ds->z == newds->z) {
+                       if (ds->z == newds->z) {
                                /* still in same z position, simply check
                                 * visibility difference against epsilon */
-                               if(!(fabs(newds->v - ds->v) <= epsilon)) {
+                               if (!(fabs(newds->v - ds->v) <= epsilon)) {
                                        break;
                                }
                        }
@@ -225,7 +225,7 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
                                max= ((ds->v + epsilon) - newds->v)*div;
 
                                /* adapt existing slopes */
-                               if(first) {
+                               if (first) {
                                        newmin= min;
                                        newmax= max;
                                        first= 0;
@@ -235,7 +235,7 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
                                        newmax= MIN2(slopemax, max);
 
                                        /* verify if there is still space between the slopes */
-                                       if(newmin > newmax) {
+                                       if (newmin > newmax) {
                                                ds--;
                                                a--;
                                                break;
@@ -247,7 +247,7 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
                        }
                }
 
-               if(a == tot) {
+               if (a == tot) {
                        ds--;
                        a--;
                }
@@ -255,7 +255,7 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
                /* always previous z */
                z= ds->z;
 
-               if(first || a==tot-1) {
+               if (first || a==tot-1) {
                        /* if slopes were not initialized, use last visibility */
                        v= ds->v;
                }
@@ -272,12 +272,12 @@ static int compress_deepsamples(DeepSample *dsample, int tot, float epsilon)
                newds->v= v;
        }
 
-       if(newtot == 0 || (newds->v != (newds-1)->v))
+       if (newtot == 0 || (newds->v != (newds-1)->v))
                newtot++;
 
 #if 0
-       if(print) {
-               for(a=0, ds=dsample; a<newtot; a++, ds++)
+       if (print) {
+               for (a=0, ds=dsample; a<newtot; a++, ds++)
                        printf("%lf,%f ", ds->z/(double)0x7FFFFFFF, ds->v);
                printf("\n");
        }
@@ -291,7 +291,7 @@ static float deep_alpha(Render *re, int obinr, int facenr, int strand)
        ObjectInstanceRen *obi= &re->objectinstance[obinr];
        Material *ma;
 
-       if(strand) {
+       if (strand) {
                StrandRen *strand= RE_findOrAddStrand(obi->obr, facenr-1);
                ma= strand->buffer->ma;
        }
@@ -327,32 +327,32 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
 
        ap= apixbuf;
        aps= apixbufstrand;
-       for(a=0; a<size*size; a++, ap++, aps++) {
+       for (a=0; a<size*size; a++, ap++, aps++) {
                /* count number of samples */
-               for(c=0; c<totbuf; c++)
+               for (c=0; c<totbuf; c++)
                        sampletot[c]= 0;
 
                tot= 0;
-               for(apn=ap; apn; apn=apn->next)
-                       for(b=0; b<4; b++)
-                               if(apn->p[b])
-                                       for(c=0; c<totbuf; c++)
-                                               if(apn->mask[b] & (1<<c))
+               for (apn=ap; apn; apn=apn->next)
+                       for (b=0; b<4; b++)
+                               if (apn->p[b])
+                                       for (c=0; c<totbuf; c++)
+                                               if (apn->mask[b] & (1<<c))
                                                        sampletot[c]++;
 
-               if(apixbufstrand) {
-                       for(apns=aps; apns; apns=apns->next)
-                               for(b=0; b<4; b++)
-                                       if(apns->p[b])
-                                               for(c=0; c<totbuf; c++)
-                                                       if(apns->mask[b] & (1<<c))
+               if (apixbufstrand) {
+                       for (apns=aps; apns; apns=apns->next)
+                               for (b=0; b<4; b++)
+                                       if (apns->p[b])
+                                               for (c=0; c<totbuf; c++)
+                                                       if (apns->mask[b] & (1<<c))
                                                                sampletot[c]++;
                }
 
-               for(c=0; c<totbuf; c++)
+               for (c=0; c<totbuf; c++)
                        tot += sampletot[c];
 
-               if(tot == 0) {
+               if (tot == 0) {
                        shsample->deepbuf[a]= NULL;
                        shsample->totbuf[a]= 0;
                        continue;
@@ -360,14 +360,14 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
 
                /* fill samples */
                ds[0]= sampleds[0]= MEM_callocN(sizeof(DeepSample)*tot*2, "deepsample");
-               for(c=1; c<totbuf; c++)
+               for (c=1; c<totbuf; c++)
                        ds[c]= sampleds[c]= sampleds[c-1] + sampletot[c-1]*2;
 
-               for(apn=ap; apn; apn=apn->next) {
-                       for(b=0; b<4; b++) {
-                               if(apn->p[b]) {
-                                       for(c=0; c<totbuf; c++) {
-                                               if(apn->mask[b] & (1<<c)) {
+               for (apn=ap; apn; apn=apn->next) {
+                       for (b=0; b<4; b++) {
+                               if (apn->p[b]) {
+                                       for (c=0; c<totbuf; c++) {
+                                               if (apn->mask[b] & (1<<c)) {
                                                        /* two entries to create step profile */
                                                        ds[c]->z= apn->z[b];
                                                        ds[c]->v= 1.0f; /* not used */
@@ -381,12 +381,12 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
                        }
                }
 
-               if(apixbufstrand) {
-                       for(apns=aps; apns; apns=apns->next) {
-                               for(b=0; b<4; b++) {
-                                       if(apns->p[b]) {
-                                               for(c=0; c<totbuf; c++) {
-                                                       if(apns->mask[b] & (1<<c)) {
+               if (apixbufstrand) {
+                       for (apns=aps; apns; apns=apns->next) {
+                               for (b=0; b<4; b++) {
+                                       if (apns->p[b]) {
+                                               for (c=0; c<totbuf; c++) {
+                                                       if (apns->mask[b] & (1<<c)) {
                                                                /* two entries to create step profile */
                                                                ds[c]->z= apns->z[b];
                                                                ds[c]->v= 1.0f; /* not used */
@@ -401,7 +401,7 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
                        }
                }
 
-               for(c=0; c<totbuf; c++) {
+               for (c=0; c<totbuf; c++) {
                        /* sort by increasing z */
                        qsort(sampleds[c], sampletot[c], sizeof(DeepSample)*2, verg_deepsample);
 
@@ -409,7 +409,7 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
                        visibility= 1.0f;
                        ds[c]= sampleds[c];
 
-                       for(b=0; b<sampletot[c]; b++) {
+                       for (b=0; b<sampletot[c]; b++) {
                                /* two entries creating step profile */
                                ds[c]->v= visibility;
                                ds[c]++;
@@ -421,8 +421,8 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
 
                        /* halfway trick, probably won't work well for volumes? */
                        ds[c]= sampleds[c];
-                       for(b=0; b<sampletot[c]; b++) {
-                               if(b+1 < sampletot[c]) {
+                       for (b=0; b<sampletot[c]; b++) {
+                               if (b+1 < sampletot[c]) {
                                        ds[c]->z= (ds[c]->z>>1) + ((ds[c]+2)->z>>1);
                                        ds[c]++;
                                        ds[c]->z= (ds[c]->z>>1) + ((ds[c]+2)->z>>1);
@@ -450,27 +450,27 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
                        minz= 0;
                        found= 0;
 
-                       for(c=0; c<totbuf; c++) {
-                               if(sampletot[c] && (!found || ds[c]->z < minz)) {
+                       for (c=0; c<totbuf; c++) {
+                               if (sampletot[c] && (!found || ds[c]->z < minz)) {
                                        minz= ds[c]->z;
                                        found= 1;
                                }
                        }
 
-                       if(!found)
+                       if (!found)
                                break;
 
                        dsb->z= minz;
                        dsb->v= 0.0f;
 
                        visibility= 0.0f;
-                       for(c=0; c<totbuf; c++) {
-                               if(sampletot[c] && ds[c]->z == minz) {
+                       for (c=0; c<totbuf; c++) {
+                               if (sampletot[c] && ds[c]->z == minz) {
                                        ds[c]++;
                                        sampletot[c]--;
                                }
 
-                               if(sampleds[c] == ds[c])
+                               if (sampleds[c] == ds[c])
                                        visibility += totbuf_f_inv;
                                else
                                        visibility += (ds[c]-1)->v / totbuf_f;
@@ -488,7 +488,7 @@ static void compress_deepshadowbuf(Render *re, ShadBuf *shb, APixstr *apixbuf, A
                shsample->totbuf[a]= newtot;
                totsamplec += newtot;
 
-               if(newtot < prevtot) {
+               if (newtot < prevtot) {
                        newbuf= MEM_mallocN(sizeof(DeepSample)*newtot, "cdeepsample");
                        memcpy(newbuf, shsample->deepbuf[a], sizeof(DeepSample)*newtot);
                        MEM_freeN(shsample->deepbuf[a]);
@@ -523,20 +523,20 @@ static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
        /* help buffer */
        rcline= MEM_mallocN(256*4+sizeof(int), "makeshadbuf2");
        
-       for(y=0; y<size; y+=16) {
-               if(y< size/2) miny= y+15-size/2;
+       for (y=0; y<size; y+=16) {
+               if (y< size/2) miny= y+15-size/2;
                else miny= y-size/2;    
                
-               for(x=0; x<size; x+=16) {
+               for (x=0; x<size; x+=16) {
                        
                        /* is tile within spotbundle? */
                        a= size/2;
-                       if(x< a) minx= x+15-a;
+                       if (x< a) minx= x+15-a;
                        else minx= x-a; 
                        
                        dist= sqrt( (float)(minx*minx+miny*miny) );
                        
-                       if(square==0 && dist>(float)(a+12)) {   /* 12, tested with a onlyshadow lamp */
+                       if (square==0 && dist>(float)(a+12)) {  /* 12, tested with a onlyshadow lamp */
                                a= 256; verg= 0; /* 0x80000000; */ /* 0x7FFFFFFF; */
                                rz1= (&verg)+1;
                        } 
@@ -546,11 +546,11 @@ static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
                                
                                verg= (*rz1 & 0xFFFFFF00);
                                
-                               for(a=0;a<256;a++,rz1++) {
-                                       if( (*rz1 & 0xFFFFFF00) !=verg) break;
+                               for (a=0;a<256;a++,rz1++) {
+                                       if ( (*rz1 & 0xFFFFFF00) !=verg) break;
                                }
                        }
-                       if(a==256) { /* complete empty tile */
+                       if (a==256) { /* complete empty tile */
                                *ctile= 0;
                                *ztile= *(rz1-1);
                        }
@@ -564,13 +564,13 @@ static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
                                verg1= rc[BCOMP];
                                rc+= 4;
                                byt1= 1; byt2= 1;
-                               for(a=1;a<256;a++,rc+=4) {
+                               for (a=1;a<256;a++,rc+=4) {
                                        byt1 &= (verg==rc[ACOMP]);
                                        byt2 &= (verg1==rc[BCOMP]);
                                        
-                                       if(byt1==0) break;
+                                       if (byt1==0) break;
                                }
-                               if(byt1 && byt2) {      /* only store byte */
+                               if (byt1 && byt2) {     /* only store byte */
                                        *ctile= 1;
                                        *ztile= (uintptr_t)MEM_mallocN(256+4, "tile1");
                                        rz= (int *)*ztile;
@@ -578,9 +578,9 @@ static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
                                        
                                        zt= (char *)(rz+1);
                                        rc= rcline;
-                                       for(a=0; a<256; a++, zt++, rc+=4) *zt= rc[GCOMP];       
+                                       for (a=0; a<256; a++, zt++, rc+=4) *zt= rc[GCOMP];
                                }
-                               else if(byt1) {         /* only store short */
+                               else if (byt1) {                /* only store short */
                                        *ctile= 2;
                                        *ztile= (uintptr_t)MEM_mallocN(2*256+4,"Tile2");
                                        rz= (int *)*ztile;
@@ -588,7 +588,7 @@ static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
                                        
                                        zt= (char *)(rz+1);
                                        rc= rcline;
-                                       for(a=0; a<256; a++, zt+=2, rc+=4) {
+                                       for (a=0; a<256; a++, zt+=2, rc+=4) {
                                                zt[0]= rc[BCOMP];
                                                zt[1]= rc[GCOMP];
                                        }
@@ -599,7 +599,7 @@ static void compress_shadowbuf(ShadBuf *shb, int *rectz, int square)
 
                                        zt= (char *)*ztile;
                                        rc= rcline;
-                                       for(a=0; a<256; a++, zt+=3, rc+=4) {
+                                       for (a=0; a<256; a++, zt+=3, rc+=4) {
                                                zt[0]= rc[ACOMP];
                                                zt[1]= rc[BCOMP];
                                                zt[2]= rc[GCOMP];
@@ -630,19 +630,19 @@ static void shadowbuf_autoclip(Render *re, LampRen *lar)
        minz= 1.0e30f; maxz= -1.0e30f;
        copy_m4_m4(viewmat, lar->shb->viewmat);
        
-       if(lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
+       if (lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
 
        maxtotvert= 0;
-       for(obr=re->objecttable.first; obr; obr=obr->next)
+       for (obr=re->objecttable.first; obr; obr=obr->next)
                maxtotvert= MAX2(obr->totvert, maxtotvert);
 
        clipflag= MEM_callocN(sizeof(char)*maxtotvert, "autoclipflag");
 
        /* set clip in vertices when face visible */
-       for(i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
+       for (i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
                obr= obi->obr;
 
-               if(obi->flag & R_TRANSFORMED)
+               if (obi->flag & R_TRANSFORMED)
                        mult_m4_m4m4(obviewmat, viewmat, obi->mat);
                else
                        copy_m4_m4(obviewmat, viewmat);
@@ -650,35 +650,35 @@ static void shadowbuf_autoclip(Render *re, LampRen *lar)
                memset(clipflag, 0, sizeof(char)*obr->totvert);
 
                /* clear clip, is being set if face is visible (clip is calculated for real later) */
-               for(a=0; a<obr->totvlak; a++) {
-                       if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
+               for (a=0; a<obr->totvlak; a++) {
+                       if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
                        else vlr++;
                        
                        /* note; these conditions are copied from zbuffer_shadow() */
-                       if(vlr->mat!= ma) {
+                       if (vlr->mat!= ma) {
                                ma= vlr->mat;
                                ok= 1;
-                               if((ma->mode & MA_SHADBUF)==0) ok= 0;
+                               if ((ma->mode & MA_SHADBUF)==0) ok= 0;
                        }
                        
-                       if(ok && (obi->lay & lay)) {
+                       if (ok && (obi->lay & lay)) {
                                clipflag[vlr->v1->index]= 1;
                                clipflag[vlr->v2->index]= 1;
                                clipflag[vlr->v3->index]= 1;
-                               if(vlr->v4) clipflag[vlr->v4->index]= 1;
+                               if (vlr->v4) clipflag[vlr->v4->index]= 1;
                        }                               
                }               
                
                /* calculate min and max */
-               for(a=0; a< obr->totvert;a++) {
-                       if((a & 255)==0) ver= RE_findOrAddVert(obr, a);
+               for (a=0; a< obr->totvert;a++) {
+                       if ((a & 255)==0) ver= RE_findOrAddVert(obr, a);
                        else ver++;
                        
-                       if(clipflag[a]) {
+                       if (clipflag[a]) {
                                copy_v3_v3(vec, ver->co);
                                mul_m4_v3(obviewmat, vec);
                                /* Z on visible side of lamp space */
-                               if(vec[2] < 0.0f) {
+                               if (vec[2] < 0.0f) {
                                        float inpr, z= -vec[2];
                                        
                                        /* since vec is rotated in lampspace, this is how to get the cosine of angle */
@@ -687,9 +687,9 @@ static void shadowbuf_autoclip(Render *re, LampRen *lar)
                                        normalize_v3(vec);
                                        inpr= - vec[2];
 
-                                       if(inpr>=lar->spotsi) {
-                                               if(z<minz) minz= z;
-                                               if(z>maxz) maxz= z;
+                                       if (inpr>=lar->spotsi) {
+                                               if (z<minz) minz= z;
+                                               if (z>maxz) maxz= z;
                                        }
                                }
                        }
@@ -699,13 +699,13 @@ static void shadowbuf_autoclip(Render *re, LampRen *lar)
        MEM_freeN(clipflag);
        
        /* set clipping min and max */
-       if(minz < maxz) {
+       if (minz < maxz) {
                float delta= (maxz - minz);     /* threshold to prevent precision issues */
                
                //printf("minz %f maxz %f delta %f\n", minz, maxz, delta);
-               if(lar->bufflag & LA_SHADBUF_AUTO_START)
+               if (lar->bufflag & LA_SHADBUF_AUTO_START)
                        lar->shb->d= minz - delta*0.02f;        /* 0.02 is arbitrary... needs more thinking! */
-               if(lar->bufflag & LA_SHADBUF_AUTO_END)
+               if (lar->bufflag & LA_SHADBUF_AUTO_END)
                        lar->shb->clipend= maxz + delta*0.1f;
                
                /* bias was calculated as percentage, we scale it to prevent animation issues */
@@ -723,12 +723,12 @@ static void makeflatshadowbuf(Render *re, LampRen *lar, float *jitbuf)
        /* zbuffering */
        rectz= MEM_mapallocN(sizeof(int)*shb->size*shb->size, "makeshadbuf");
        
-       for(samples=0; samples<shb->totbuf; samples++) {
+       for (samples=0; samples<shb->totbuf; samples++) {
                zbuffer_shadow(re, shb->persmat, lar, rectz, shb->size, jitbuf[2*samples], jitbuf[2*samples+1]);
                /* create Z tiles (for compression): this system is 24 bits!!! */
                compress_shadowbuf(shb, rectz, lar->mode & LA_SQUARE);
 
-               if(re->test_break(re->tbh))
+               if (re->test_break(re->tbh))
                        break;
        }
        
@@ -744,7 +744,7 @@ static void makedeepshadowbuf(Render *re, LampRen *lar, float *jitbuf)
 
        /* zbuffering */
        apixbuf= MEM_callocN(sizeof(APixstr)*shb->size*shb->size, "APixbuf");
-       if(re->totstrand)
+       if (re->totstrand)
                apixbufstrand= MEM_callocN(sizeof(APixstrand)*shb->size*shb->size, "APixbufstrand");
 
        zbuffer_abuf_shadow(re, lar, shb->persmat, apixbuf, apixbufstrand, &apsmbase, shb->size,
@@ -754,7 +754,7 @@ static void makedeepshadowbuf(Render *re, LampRen *lar, float *jitbuf)
        compress_deepshadowbuf(re, shb, apixbuf, apixbufstrand);
        
        MEM_freeN(apixbuf);
-       if(apixbufstrand)
+       if (apixbufstrand)
                MEM_freeN(apixbufstrand);
        freepsA(&apsmbase);
 }
@@ -764,11 +764,11 @@ void makeshadowbuf(Render *re, LampRen *lar)
        ShadBuf *shb= lar->shb;
        float wsize, *jitbuf, twozero[2]= {0.0f, 0.0f}, angle, temp;
        
-       if(lar->bufflag & (LA_SHADBUF_AUTO_START|LA_SHADBUF_AUTO_END))
+       if (lar->bufflag & (LA_SHADBUF_AUTO_START|LA_SHADBUF_AUTO_END))
                shadowbuf_autoclip(re, lar);
        
        /* just to enforce identical behavior of all irregular buffers */
-       if(lar->buftype==LA_SHADBUF_IRREGULAR)
+       if (lar->buftype==LA_SHADBUF_IRREGULAR)
                shb->size= 1024;
        
        /* matrices and window: in winmat the transformation is being put,
@@ -782,7 +782,7 @@ void makeshadowbuf(Render *re, LampRen *lar)
        perspective_m4( shb->winmat,-wsize, wsize, -wsize, wsize, shb->d, shb->clipend);
        mult_m4_m4m4(shb->persmat, shb->winmat, shb->viewmat);
 
-       if(ELEM3(lar->buftype, LA_SHADBUF_REGULAR, LA_SHADBUF_HALFWAY, LA_SHADBUF_DEEP)) {
+       if (ELEM3(lar->buftype, LA_SHADBUF_REGULAR, LA_SHADBUF_HALFWAY, LA_SHADBUF_DEEP)) {
                shb->totbuf= lar->buffers;
 
                /* jitter, weights - not threadsafe! */
@@ -791,12 +791,12 @@ void makeshadowbuf(Render *re, LampRen *lar)
                make_jitter_weight_tab(re, shb, lar->filtertype);
                BLI_unlock_thread(LOCK_CUSTOM1);
                
-               if(shb->totbuf==4) jitbuf= give_jitter_tab(2);
-               else if(shb->totbuf==9) jitbuf= give_jitter_tab(3);
+               if (shb->totbuf==4) jitbuf= give_jitter_tab(2);
+               else if (shb->totbuf==9) jitbuf= give_jitter_tab(3);
                else jitbuf= twozero;
                
                /* zbuffering */
-               if(lar->buftype == LA_SHADBUF_DEEP) {
+               if (lar->buftype == LA_SHADBUF_DEEP) {
                        makedeepshadowbuf(re, lar, jitbuf);
                        shb->totbuf= 1;
                }
@@ -814,8 +814,8 @@ static void *do_shadow_thread(void *re_v)
 
        do {
                BLI_lock_thread(LOCK_CUSTOM1);
-               for(lar=re->lampren.first; lar; lar=lar->next) {
-                       if(lar->shb && !lar->thread_assigned) {
+               for (lar=re->lampren.first; lar; lar=lar->next) {
+                       if (lar->shb && !lar->thread_assigned) {
                                lar->thread_assigned= 1;
                                break;
                        }
@@ -823,7 +823,7 @@ static void *do_shadow_thread(void *re_v)
                BLI_unlock_thread(LOCK_CUSTOM1);
 
                /* if type is irregular, this only sets the perspective matrix and autoclips */
-               if(lar) {
+               if (lar) {
                        makeshadowbuf(re, lar);
                        BLI_lock_thread(LOCK_CUSTOM1);
                        lar->thread_ready= 1;
@@ -848,9 +848,9 @@ void threaded_makeshadowbufs(Render *re)
        int (*test_break)(void *);
 
        /* count number of threads to use */
-       if(G.rendering) {
-               for(lar=re->lampren.first; lar; lar= lar->next)
-                       if(lar->shb)
+       if (G.rendering) {
+               for (lar=re->lampren.first; lar; lar= lar->next)
+                       if (lar->shb)
                                totthread++;
                
                totthread= MIN2(totthread, re->r.threads);
@@ -858,10 +858,10 @@ void threaded_makeshadowbufs(Render *re)
        else
                totthread= 1; /* preview render */
 
-       if(totthread <= 1) {
-               for(lar=re->lampren.first; lar; lar= lar->next) {
-                       if(re->test_break(re->tbh)) break;
-                       if(lar->shb) {
+       if (totthread <= 1) {
+               for (lar=re->lampren.first; lar; lar= lar->next) {
+                       if (re->test_break(re->tbh)) break;
+                       if (lar->shb) {
                                /* if type is irregular, this only sets the perspective matrix and autoclips */
                                makeshadowbuf(re, lar);
                        }
@@ -872,26 +872,26 @@ void threaded_makeshadowbufs(Render *re)
                test_break= re->test_break;
                re->test_break= thread_break;
 
-               for(lar=re->lampren.first; lar; lar= lar->next) {
+               for (lar=re->lampren.first; lar; lar= lar->next) {
                        lar->thread_assigned= 0;
                        lar->thread_ready= 0;
                }
 
                BLI_init_threads(&threads, do_shadow_thread, totthread);
                
-               for(a=0; a<totthread; a++)
+               for (a=0; a<totthread; a++)
                        BLI_insert_thread(&threads, re);
 
                /* keep rendering as long as there are shadow buffers not ready */
                do {
-                       if((g_break=test_break(re->tbh)))
+                       if ((g_break=test_break(re->tbh)))
                                break;
 
                        PIL_sleep_ms(50);
 
                        BLI_lock_thread(LOCK_CUSTOM1);
-                       for(lar=re->lampren.first; lar; lar= lar->next)
-                               if(lar->shb && !lar->thread_ready)
+                       for (lar=re->lampren.first; lar; lar= lar->next)
+                               if (lar->shb && !lar->thread_ready)
                                        break;
                        BLI_unlock_thread(LOCK_CUSTOM1);
                } while(lar);
@@ -906,16 +906,16 @@ void threaded_makeshadowbufs(Render *re)
 
 void freeshadowbuf(LampRen *lar)
 {
-       if(lar->shb) {
+       if (lar->shb) {
                ShadBuf *shb= lar->shb;
                ShadSampleBuf *shsample;
                int b, v;
                
-               for(shsample= shb->buffers.first; shsample; shsample= shsample->next) {
-                       if(shsample->deepbuf) {
+               for (shsample= shb->buffers.first; shsample; shsample= shsample->next) {
+                       if (shsample->deepbuf) {
                                v= shb->size*shb->size;
-                               for(b=0; b<v; b++)
-                                       if(shsample->deepbuf[b])
+                               for (b=0; b<v; b++)
+                                       if (shsample->deepbuf[b])
                                                MEM_freeN(shsample->deepbuf[b]);
                                        
                                MEM_freeN(shsample->deepbuf);
@@ -926,8 +926,8 @@ void freeshadowbuf(LampRen *lar)
                                char *ctile= shsample->cbuf;
                                
                                v= (shb->size*shb->size)/256;
-                               for(b=0; b<v; b++, ztile++, ctile++)
-                                       if(*ctile) MEM_freeN((void *) *ztile);
+                               for (b=0; b<v; b++, ztile++, ctile++)
+                                       if (*ctile) MEM_freeN((void *) *ztile);
                                
                                MEM_freeN(shsample->zbuf);
                                MEM_freeN(shsample->cbuf);
@@ -935,7 +935,7 @@ void freeshadowbuf(LampRen *lar)
                }
                BLI_freelistN(&shb->buffers);
                
-               if(shb->weight) MEM_freeN(shb->weight);
+               if (shb->weight) MEM_freeN(shb->weight);
                MEM_freeN(lar->shb);
                
                lar->shb= NULL;
@@ -949,22 +949,22 @@ static int firstreadshadbuf(ShadBuf *shb, ShadSampleBuf *shsample, int **rz, int
        int ofs;
        char *ct;
 
-       if(shsample->deepbuf)
+       if (shsample->deepbuf)
                return 0;
 
        /* always test borders of shadowbuffer */
-       if(xs<0) xs= 0; else if(xs>=shb->size) xs= shb->size-1;
-       if(ys<0) ys= 0; else if(ys>=shb->size) ys= shb->size-1;
+       if (xs<0) xs= 0; else if (xs>=shb->size) xs= shb->size-1;
+       if (ys<0) ys= 0; else if (ys>=shb->size) ys= shb->size-1;
 
        /* calc z */
        ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
        ct= shsample->cbuf+ofs;
-       if(*ct==0) {
-               if(nr==0) {
+       if (*ct==0) {
+               if (nr==0) {
                        *rz= *( (int **)(shsample->zbuf+ofs) );
                        return 1;
                }
-               else if(*rz!= *( (int **)(shsample->zbuf+ofs) )) return 0;
+               else if (*rz!= *( (int **)(shsample->zbuf+ofs) )) return 0;
                
                return 1;
        }
@@ -983,21 +983,21 @@ static float readdeepvisibility(DeepSample *dsample, int tot, int z, int bias, f
        ds= dsample;
        for (a=0; a<tot && (z-bias > ds->z); a++, ds++) {}
 
-       if(a == tot) {
-               if(biast)
+       if (a == tot) {
+               if (biast)
                        *biast= 0.0f;
                return (ds-1)->v; /* completely behind all samples */
        }
        
        /* check if this read needs bias blending */
-       if(biast) {
-               if(z > ds->z)
+       if (biast) {
+               if (z > ds->z)
                        *biast= (float)(z - ds->z)/(float)bias;
                else
                        *biast= 0.0f;
        }
 
-       if(a == 0)
+       if (a == 0)
                return 1.0f; /* completely in front of all samples */
 
        /* converting to float early here because ds->z - prevds->z can overflow */
@@ -1011,18 +1011,18 @@ static float readdeepshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias,
        float v, biasv, biast;
        int ofs, tot;
 
-       if(zs < - 0x7FFFFE00 + bias)
+       if (zs < - 0x7FFFFE00 + bias)
                return 1.0;     /* extreme close to clipstart */
 
        /* calc z */
        ofs= ys*shb->size + xs;
        tot= shsample->totbuf[ofs];
-       if(tot == 0)
+       if (tot == 0)
                return 1.0f;
 
        v= readdeepvisibility(shsample->deepbuf[ofs], tot, zs, bias, &biast);
 
-       if(biast != 0.0f) {
+       if (biast != 0.0f) {
                /* in soft bias area */
                biasv= readdeepvisibility(shsample->deepbuf[ofs], tot, zs, 0, 0);
 
@@ -1046,10 +1046,10 @@ static float readshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int
        /* if(xs>=shb->size || ys>=shb->size) return 1.0; */
        
        /* always test borders of shadowbuffer */
-       if(xs<0) xs= 0; else if(xs>=shb->size) xs= shb->size-1;
-       if(ys<0) ys= 0; else if(ys>=shb->size) ys= shb->size-1;
+       if (xs<0) xs= 0; else if (xs>=shb->size) xs= shb->size-1;
+       if (ys<0) ys= 0; else if (ys>=shb->size) ys= shb->size-1;
 
-       if(shsample->deepbuf)
+       if (shsample->deepbuf)
                return readdeepshadowbuf(shb, shsample, bias, xs, ys, zs);
 
        /* calc z */
@@ -1057,14 +1057,14 @@ static float readshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int
        ct= shsample->cbuf+ofs;
        rz= *( (int **)(shsample->zbuf+ofs) );
 
-       if(*ct==3) {
+       if (*ct==3) {
                ct= ((char *)rz)+3*16*(ys & 15)+3*(xs & 15);
                cz= (char *)&zsamp;
                cz[ACOMP]= ct[0];
                cz[BCOMP]= ct[1];
                cz[GCOMP]= ct[2];
        }
-       else if(*ct==2) {
+       else if (*ct==2) {
                ct= ((char *)rz);
                ct+= 4+2*16*(ys & 15)+2*(xs & 15);
                zsamp= *rz;
@@ -1073,7 +1073,7 @@ static float readshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int
                cz[BCOMP]= ct[0];
                cz[GCOMP]= ct[1];
        }
-       else if(*ct==1) {
+       else if (*ct==1) {
                ct= ((char *)rz);
                ct+= 4+16*(ys & 15)+(xs & 15);
                zsamp= *rz;
@@ -1090,9 +1090,9 @@ static float readshadowbuf(ShadBuf *shb, ShadSampleBuf *shsample, int bias, int
 
        /* tricky stuff here; we use ints which can overflow easily with bias values */
        
-       if(zsamp > zs) return 1.0;              /* absolute no shadow */
-       else if(zs < - 0x7FFFFE00 + bias) return 1.0;   /* extreme close to clipstart */
-       else if(zsamp < zs-bias) return 0.0 ;   /* absolute in shadow */
+       if (zsamp > zs) return 1.0;             /* absolute no shadow */
+       else if (zs < - 0x7FFFFE00 + bias) return 1.0;  /* extreme close to clipstart */
+       else if (zsamp < zs-bias) return 0.0;   /* absolute in shadow */
        else {                                  /* soft area */
                
                temp=  ( (float)(zs- zsamp) )/(float)bias;
@@ -1112,7 +1112,7 @@ static void shadowbuf_project_co(float *x, float *y, float *z, ShadBuf *shb, con
 
        *x= size*(1.0f+hco[0]/hco[3]);
        *y= size*(1.0f+hco[1]/hco[3]);
-       if(z) *z= (hco[2]/hco[3]);
+       if (z) *z= (hco[2]/hco[3]);
 }
 
 /* the externally called shadow testing (reading) function */
@@ -1126,11 +1126,11 @@ float testshadowbuf(Render *re, ShadBuf *shb, const float co[3], const float dxc
        short a, num;
        
        /* crash preventer */
-       if(shb->buffers.first==NULL)
+       if (shb->buffers.first==NULL)
                return 1.0f;
        
        /* when facing away, assume fully in shadow */
-       if(inp <= 0.0f)
+       if (inp <= 0.0f)
                return 0.0f;
 
        /* project coordinate to pixel space */
@@ -1138,9 +1138,9 @@ float testshadowbuf(Render *re, ShadBuf *shb, const float co[3], const float dxc
 
        /* clip z coordinate, z is projected so that (-1.0, 1.0) matches
         * (clipstart, clipend), so we can do this simple test */
-       if(zs1>=1.0f)
+       if (zs1>=1.0f)
                return 0.0f;
-       else if(zs1<= -1.0f)
+       else if (zs1<= -1.0f)
                return 1.0f;
 
        zs= ((float)0x7FFFFFFF)*zs1;
@@ -1151,15 +1151,15 @@ float testshadowbuf(Render *re, ShadBuf *shb, const float co[3], const float dxc
        fac= shb->soft;
        
        /* compute z bias */
-       if(mat_bias!=0.0f) biasf= shb->bias*mat_bias;
+       if (mat_bias!=0.0f) biasf= shb->bias*mat_bias;
        else biasf= shb->bias;
        /* with inp==1.0, bias is half the size. correction value was 1.1, giving errors 
         * on cube edges, with one side being almost frontal lighted (ton)  */
        bias= (1.5f-inp*inp)*biasf;
        
        /* in case of no filtering we can do things simpler */
-       if(num==1) {
-               for(shsample= shb->buffers.first; shsample; shsample= shsample->next)
+       if (num==1) {
+               for (shsample= shb->buffers.first; shsample; shsample= shsample->next)
                        shadfac += readshadowbuf(shb, shsample, bias, (int)xs1, (int)ys1, zs);
                
                return shadfac/(float)shb->totbuf;
@@ -1178,20 +1178,20 @@ float testshadowbuf(Render *re, ShadBuf *shb, const float co[3], const float dxc
        
        xres= fac*(fabs(dx[0]) + fabs(dy[0]));
        yres= fac*(fabs(dx[1]) + fabs(dy[1]));
-       if(xres<1.0f) xres= 1.0f;
-       if(yres<1.0f) yres= 1.0f;
+       if (xres<1.0f) xres= 1.0f;
+       if (yres<1.0f) yres= 1.0f;
        
        /* make xs1/xs1 corner of sample area */
        xs1 -= xres*0.5f;
        ys1 -= yres*0.5f;
 
        /* in case we have a constant value in a tile, we can do quicker lookup */
-       if(xres<16.0f && yres<16.0f) {
+       if (xres<16.0f && yres<16.0f) {
                shsample= shb->buffers.first;
-               if(firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)ys1, 0)) {
-                       if(firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)ys1, 1)) {
-                               if(firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)(ys1+yres), 1)) {
-                                       if(firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)(ys1+yres), 1)) {
+               if (firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)ys1, 0)) {
+                       if (firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)ys1, 1)) {
+                               if (firstreadshadbuf(shb, shsample, &rz, (int)xs1, (int)(ys1+yres), 1)) {
+                                       if (firstreadshadbuf(shb, shsample, &rz, (int)(xs1+xres), (int)(ys1+yres), 1)) {
                                                return readshadowbuf(shb, shsample, bias,(int)xs1, (int)ys1, zs);
                                        }
                                }
@@ -1200,11 +1200,11 @@ float testshadowbuf(Render *re, ShadBuf *shb, const float co[3], const float dxc
        }
        
        /* full jittered shadow buffer lookup */
-       for(shsample= shb->buffers.first; shsample; shsample= shsample->next) {
+       for (shsample= shb->buffers.first; shsample; shsample= shsample->next) {
                jit= shb->jit;
                weight= shb->weight;
                
-               for(a=num; a>0; a--, jit+=2, weight++) {
+               for (a=num; a>0; a--, jit+=2, weight++) {
                        /* instead of jit i tried random: ugly! */
                        /* note: the plus 0.5 gives best sampling results, jit goes from -0.5 to 0.5 */
                        /* xs1 and ys1 are already corrected to be corner of sample area */
@@ -1232,15 +1232,15 @@ static float readshadowbuf_halo(ShadBuf *shb, ShadSampleBuf *shsample, int xs, i
        bias= -shb->bias;
        
        /* simpleclip */
-       if(xs<0 || ys<0) return 0.0;
-       if(xs>=shb->size || ys>=shb->size) return 0.0;
+       if (xs<0 || ys<0) return 0.0;
+       if (xs>=shb->size || ys>=shb->size) return 0.0;
 
        /* calc z */
        ofs= (ys>>4)*(shb->size>>4) + (xs>>4);
        ct= shsample->cbuf+ofs;
        rz= *( (int **)(shsample->zbuf+ofs) );
 
-       if(*ct==3) {
+       if (*ct==3) {
                ct= ((char *)rz)+3*16*(ys & 15)+3*(xs & 15);
                cz= (char *)&zsamp;
                zsamp= 0;
@@ -1248,7 +1248,7 @@ static float readshadowbuf_halo(ShadBuf *shb, ShadSampleBuf *shsample, int xs, i
                cz[BCOMP]= ct[1];
                cz[GCOMP]= ct[2];
        }
-       else if(*ct==2) {
+       else if (*ct==2) {
                ct= ((char *)rz);
                ct+= 4+2*16*(ys & 15)+2*(xs & 15);
                zsamp= *rz;
@@ -1257,7 +1257,7 @@ static float readshadowbuf_halo(ShadBuf *shb, ShadSampleBuf *shsample, int xs, i
                cz[BCOMP]= ct[0];
                cz[GCOMP]= ct[1];
        }
-       else if(*ct==1) {
+       else if (*ct==1) {
                ct= ((char *)rz);
                ct+= 4+16*(ys & 15)+(xs & 15);
                zsamp= *rz;
@@ -1274,16 +1274,16 @@ static float readshadowbuf_halo(ShadBuf *shb, ShadSampleBuf *shsample, int xs, i
 
        /* NO schadow when sampled at 'eternal' distance */
 
-       if(zsamp >= 0x7FFFFE00) return 1.0; 
+       if (zsamp >= 0x7FFFFE00) return 1.0;
 
-       if(zsamp > zs) return 1.0;              /* absolute no shadww */
+       if (zsamp > zs) return 1.0;             /* absolute no shadww */
        else {
                /* bias is negative, so the (zs-bias) can be beyond 0x7fffffff */
                zbias= 0x7fffffff - zs;
-               if(zbias > -bias) {
-                       if( zsamp < zs-bias) return 0.0 ;       /* absolute in shadow */
+               if (zbias > -bias) {
+                       if ( zsamp < zs-bias) return 0.0;       /* absolute in shadow */
                }
-               else return 0.0 ;       /* absolute shadow */
+               else return 0.0       /* absolute shadow */
        }
 
        /* soft area */
@@ -1331,8 +1331,8 @@ float shadow_halo(LampRen *lar, const float p1[3], const float p2[3])
        xs1= (int)xf1;
        ys1= (int)yf1;
 
-       if(xf1 != xf2) {
-               if(xf2-xf1 > 0.0f) {
+       if (xf1 != xf2) {
+               if (xf2-xf1 > 0.0f) {
                        labdax= (xf1-xs1-1.0f)/(xf1-xf2);
                        ldx= -shb->shadhalostep/(xf1-xf2);
                        dx= shb->shadhalostep;
@@ -1348,8 +1348,8 @@ float shadow_halo(LampRen *lar, const float p1[3], const float p2[3])
                ldx= 0.0;
        }
 
-       if(yf1 != yf2) {
-               if(yf2-yf1 > 0.0f) {
+       if (yf1 != yf2) {
+               if (yf2-yf1 > 0.0f) {
                        labday= (yf1-ys1-1.0f)/(yf1-yf2);
                        ldy= -shb->shadhalostep/(yf1-yf2);
                        dy= shb->shadhalostep;
@@ -1374,42 +1374,43 @@ float shadow_halo(LampRen *lar, const float p1[3], const float p2[3])
        while(1) {
                labdao= labda;
                
-               if(labdax==labday) {
+               if (labdax==labday) {
                        labdax+= ldx;
                        x+= dx;
                        labday+= ldy;
                        y+= dy;
                }
                else {
-                       if(labdax<labday) {
+                       if (labdax<labday) {
                                labdax+= ldx;
                                x+= dx;
-                       } else {
+                       }
+                       else {
                                labday+= ldy;
                                y+= dy;
                        }
                }
                
                labda= MIN2(labdax, labday);
-               if(labda==labdao || labda>=1.0f) break;
+               if (labda==labdao || labda>=1.0f) break;
                
                zf= zf1 + labda*(zf2-zf1);
                count+= (float)shb->totbuf;
 
-               if(zf<= -1.0f) lightcount += 1.0f;      /* close to the spot */
+               if (zf<= -1.0f) lightcount += 1.0f;     /* close to the spot */
                else {
                
                        /* make sure, behind the clipend we extend halolines. */
-                       if(zf>=1.0f) z= 0x7FFFF000;
+                       if (zf>=1.0f) z= 0x7FFFF000;
                        else z= (int)(0x7FFFF000*zf);
                        
-                       for(shsample= shb->buffers.first; shsample; shsample= shsample->next)
+                       for (shsample= shb->buffers.first; shsample; shsample= shsample->next)
                                lightcount+= readshadowbuf_halo(shb, shsample, x, y, z);
                        
                }
        }
        
-       if(count!=0.0f) return (lightcount/count);
+       if (count!=0.0f) return (lightcount/count);
        return 0.0f;
        
 }
@@ -1473,21 +1474,21 @@ static void init_box(Boxf *box)
 /* use v1 to calculate boundbox */
 static void bound_boxf(Boxf *box, const float v1[3])
 {
-       if(v1[0] < box->xmin) box->xmin = v1[0];
-       if(v1[0] > box->xmax) box->xmax = v1[0];
-       if(v1[1] < box->ymin) box->ymin = v1[1];
-       if(v1[1] > box->ymax) box->ymax = v1[1];
-       if(v1[2] < box->zmin) box->zmin= v1[2];
-       if(v1[2] > box->zmax) box->zmax= v1[2];
+       if (v1[0] < box->xmin) box->xmin = v1[0];
+       if (v1[0] > box->xmax) box->xmax = v1[0];
+       if (v1[1] < box->ymin) box->ymin = v1[1];
+       if (v1[1] > box->ymax) box->ymax = v1[1];
+       if (v1[2] < box->zmin) box->zmin= v1[2];
+       if (v1[2] > box->zmax) box->zmax= v1[2];
 }
 
 /* use v1 to calculate boundbox */
 static void bound_rectf(rctf *box, const float v1[2])
 {
-       if(v1[0] < box->xmin) box->xmin = v1[0];
-       if(v1[0] > box->xmax) box->xmax = v1[0];
-       if(v1[1] < box->ymin) box->ymin = v1[1];
-       if(v1[1] > box->ymax) box->ymax = v1[1];
+       if (v1[0] < box->xmin) box->xmin = v1[0];
+       if (v1[0] > box->xmax) box->xmax = v1[0];
+       if (v1[1] < box->ymin) box->ymin = v1[1];
+       if (v1[1] > box->ymax) box->ymax = v1[1];
 }
 
 
@@ -1496,7 +1497,7 @@ static void isb_bsp_split_init(ISBBranch *root, MemArena *mem, int level)
 {
        
        /* if level > 0 we create new branches and go deeper*/
-       if(level > 0) {
+       if (level > 0) {
                ISBBranch *left, *right;
                int i;
                
@@ -1505,7 +1506,7 @@ static void isb_bsp_split_init(ISBBranch *root, MemArena *mem, int level)
                root->divider[1]= 0.5f*(root->box.ymin+root->box.ymax);
                
                /* find best splitpoint */
-               if(root->box.xmax-root->box.xmin > root->box.ymax-root->box.ymin)
+               if (root->box.xmax-root->box.xmin > root->box.ymax-root->box.ymin)
                        i= root->index= 0;
                else
                        i= root->index= 1;
@@ -1516,7 +1517,7 @@ static void isb_bsp_split_init(ISBBranch *root, MemArena *mem, int level)
                /* box info */
                left->box= root->box;
                right->box= root->box;
-               if(i==0) {
+               if (i==0) {
                        left->box.xmax = root->divider[0];
                        right->box.xmin = root->divider[0];
                }
@@ -1542,7 +1543,7 @@ static void isb_bsp_split(ISBBranch *root, MemArena *mem)
 
        /* splitpoint */
        root->divider[0]= root->divider[1]= 0.0f;
-       for(a=BSPMAX_SAMPLE-1; a>=0; a--) {
+       for (a=BSPMAX_SAMPLE-1; a>=0; a--) {
                root->divider[0]+= root->samples[a]->zco[0];
                root->divider[1]+= root->samples[a]->zco[1];
        }
@@ -1550,7 +1551,7 @@ static void isb_bsp_split(ISBBranch *root, MemArena *mem)
        root->divider[1]/= BSPMAX_SAMPLE;
        
        /* find best splitpoint */
-       if(root->box.xmax-root->box.xmin > root->box.ymax-root->box.ymin)
+       if (root->box.xmax-root->box.xmin > root->box.ymax-root->box.ymin)
                i= root->index= 0;
        else
                i= root->index= 1;
@@ -1564,15 +1565,15 @@ static void isb_bsp_split(ISBBranch *root, MemArena *mem)
        right->samples= samples; // tmp
        
        /* split samples */
-       for(a=BSPMAX_SAMPLE-1; a>=0; a--) {
+       for (a=BSPMAX_SAMPLE-1; a>=0; a--) {
                int comp= 0;
                /* this prevents adding samples all to 1 branch when divider is equal to samples */
-               if(root->samples[a]->zco[i] == root->divider[i])
+               if (root->samples[a]->zco[i] == root->divider[i])
                        comp= a & 1;
-               else if(root->samples[a]->zco[i] < root->divider[i])
+               else if (root->samples[a]->zco[i] < root->divider[i])
                        comp= 1;
                
-               if(comp==1) {
+               if (comp==1) {
                        left->samples[left->totsamp]= root->samples[a];
                        left->totsamp++;
                }
@@ -1590,7 +1591,7 @@ static void isb_bsp_split(ISBBranch *root, MemArena *mem)
        /* box info */
        left->box= root->box;
        right->box= root->box;
-       if(i==0) {
+       if (i==0) {
                left->box.xmax = root->divider[0];
                right->box.xmin = root->divider[0];
        }
@@ -1613,7 +1614,7 @@ static int isb_bsp_insert(ISBBranch *root, MemArena *memarena, ISBSample *sample
        
        /* going over branches until last one found */
        while(bspn->left) {
-               if(zco[bspn->index] <= bspn->divider[bspn->index])
+               if (zco[bspn->index] <= bspn->divider[bspn->index])
                        bspn= bspn->left;
                else
                        bspn= bspn->right;
@@ -1621,7 +1622,7 @@ static int isb_bsp_insert(ISBBranch *root, MemArena *memarena, ISBSample *sample
        }
        /* bspn now is the last branch */
        
-       if(bspn->totsamp==BSPMAX_SAMPLE) {
+       if (bspn->totsamp==BSPMAX_SAMPLE) {
                printf("error in bsp branch\n");        /* only for debug, cannot happen */
                return 1;
        }
@@ -1631,8 +1632,8 @@ static int isb_bsp_insert(ISBBranch *root, MemArena *memarena, ISBSample *sample
        bspn->totsamp++;
 
        /* split if allowed and needed */
-       if(bspn->totsamp==BSPMAX_SAMPLE) {
-               if(i==BSPMAX_DEPTH) {
+       if (bspn->totsamp==BSPMAX_SAMPLE) {
+               if (i==BSPMAX_DEPTH) {
                        bspn->totsamp--;        /* stop filling in... will give errors */
                        return 1;
                }
@@ -1648,7 +1649,7 @@ static void bspface_init_strand(BSPFace *face)
        face->radline= 0.5f* len_v2v2(face->v1, face->v2);
        
        mid_v3_v3v3(face->vec1, face->v1, face->v2);
-       if(face->v4)
+       if (face->v4)
                mid_v3_v3v3(face->vec2, face->v3, face->v4);
        else
                copy_v3_v3(face->vec2, face->v3);
@@ -1659,7 +1660,7 @@ static void bspface_init_strand(BSPFace *face)
        
        face->len= face->rc[0]*face->rc[0]+ face->rc[1]*face->rc[1];
        
-       if(face->len!=0.0f) {
+       if (face->len!=0.0f) {
                face->radline_end= face->radline/sqrt(face->len);
                face->len= 1.0f/face->len;
        }
@@ -1673,18 +1674,18 @@ static int point_behind_strand(const float p[3], BSPFace *face)
        
        /* using code from dist_to_line_segment_v2(), distance vec to line-piece */
 
-       if(face->len==0.0f) {
+       if (face->len==0.0f) {
                rc[0]= p[0]-face->vec1[0];
                rc[1]= p[1]-face->vec1[1];
                dist= (float)(sqrt(rc[0]*rc[0]+ rc[1]*rc[1]));
                
-               if(dist < face->radline)
+               if (dist < face->radline)
                        return 1;
        }
        else {
                float labda= ( face->rc[0]*(p[0]-face->vec1[0]) + face->rc[1]*(p[1]-face->vec1[1]) )*face->len;
                
-               if(labda > -face->radline_end && labda < 1.0f+face->radline_end) { 
+               if (labda > -face->radline_end && labda < 1.0f+face->radline_end) {
                        /* hesse for dist: */
                        //dist= (float)(fabs( (p[0]-vec2[0])*rc[1] + (p[1]-vec2[1])*rc[0])/len);
                        
@@ -1695,9 +1696,9 @@ static int point_behind_strand(const float p[3], BSPFace *face)
                        rc[1]= pt[1]-p[1];
                        dist= (float)sqrt(rc[0]*rc[0]+ rc[1]*rc[1]);
                        
-                       if(dist < face->radline) {
+                       if (dist < face->radline) {
                                float zval= face->vec1[2] + labda*face->rc[2];
-                               if(p[2] > zval)
+                               if (p[2] > zval)
                                        return 1;
                        }
                }
@@ -1718,7 +1719,7 @@ static int point_behind_tria2d(const float p[3], const float v1[3], const float
        c[1] = v3[1] - v1[1];
        
        div = a[0]*c[1] - a[1]*c[0];
-       if(div==0.0f)
+       if (div==0.0f)
                return 0;
        
        h[0] = p[0] - v1[0];
@@ -1727,13 +1728,13 @@ static int point_behind_tria2d(const float p[3], const float v1[3], const float
        div = 1.0f/div;
        
        u = (h[0]*c[1] - h[1]*c[0])*div;
-       if(u >= 0.0f) {
+       if (u >= 0.0f) {
                v = (a[0]*h[1] - a[1]*h[0])*div;
-               if(v >= 0.0f) {
-                       if( u + v <= 1.0f) {
+               if (v >= 0.0f) {
+                       if ( u + v <= 1.0f) {
                                /* inside, now check if point p is behind */
                                float z=  (1.0f-u-v)*v1[2] + u*v2[2] + v*v3[2];
-                               if(z <= p[2])
+                               if (z <= p[2])
                                        return 1;
                        }
                }
@@ -1758,10 +1759,10 @@ static int rect_outside_line(rctf *rect, const float v1[3], const float v2[3], c
        side= a*v3[0] + b*v3[1] + c < 0.0f;
        
        /* the four quad points */
-       if( side==(rect->xmin*a + rect->ymin*b + c >= 0.0f) )
-               if( side==(rect->xmax*a + rect->ymin*b + c >= 0.0f) )
-                       if( side==(rect->xmax*a + rect->ymax*b + c >= 0.0f) )
-                               if( side==(rect->xmin*a + rect->ymax*b + c >= 0.0f) )
+       if ( side==(rect->xmin*a + rect->ymin*b + c >= 0.0f) )
+               if ( side==(rect->xmax*a + rect->ymin*b + c >= 0.0f) )
+                       if ( side==(rect->xmax*a + rect->ymax*b + c >= 0.0f) )
+                               if ( side==(rect->xmin*a + rect->ymax*b + c >= 0.0f) )
                                        return 1;
        return 0;
 }
@@ -1769,11 +1770,11 @@ static int rect_outside_line(rctf *rect, const float v1[3], const float v2[3], c
 /* check if one of the triangle edges separates all rect points on 1 side */
 static int rect_isect_tria(rctf *rect, const float v1[3], const float v2[3], const float v3[3])
 {
-       if(rect_outside_line(rect, v1, v2, v3))
+       if (rect_outside_line(rect, v1, v2, v3))
                return 0;
-       if(rect_outside_line(rect, v2, v3, v1))
+       if (rect_outside_line(rect, v2, v3, v1))
                return 0;
-       if(rect_outside_line(rect, v3, v1, v2))
+       if (rect_outside_line(rect, v3, v1, v2))
                return 0;
        return 1;
 }
@@ -1784,18 +1785,18 @@ static void isb_bsp_face_inside(ISBBranch *bspn, BSPFace *face)
 {
        
        /* are we descending? */
-       if(bspn->left) {
+       if (bspn->left) {
                /* hrmf, the box struct cannot be addressed with index */
-               if(bspn->index==0) {
-                       if(face->box.xmin <= bspn->divider[0])
+               if (bspn->index==0) {
+                       if (face->box.xmin <= bspn->divider[0])
                                isb_bsp_face_inside(bspn->left, face);
-                       if(face->box.xmax > bspn->divider[0])
+                       if (face->box.xmax > bspn->divider[0])
                                isb_bsp_face_inside(bspn->right, face);
                }
                else {
-                       if(face->box.ymin <= bspn->divider[1])
+                       if (face->box.ymin <= bspn->divider[1])
                                isb_bsp_face_inside(bspn->left, face);
-                       if(face->box.ymax > bspn->divider[1])
+                       if (face->box.ymax > bspn->divider[1])
                                isb_bsp_face_inside(bspn->right, face);
                }
        }
@@ -1803,42 +1804,42 @@ static void isb_bsp_face_inside(ISBBranch *bspn, BSPFace *face)
                /* else: end branch reached */
                int a;
                
-               if(bspn->totsamp==0) return;
+               if (bspn->totsamp==0) return;
                
                /* check for nodes entirely in shadow, can be skipped */
-               if(bspn->totsamp==bspn->full)
+               if (bspn->totsamp==bspn->full)
                        return;
                
                /* if bsp node is entirely in front of face, give up */
-               if(bspn->box.zmax < face->box.zmin)
+               if (bspn->box.zmax < face->box.zmin)
                        return;
                
                /* if face boundbox is outside of branch rect, give up */
-               if(0==BLI_isect_rctf((rctf *)&face->box, (rctf *)&bspn->box, NULL))
+               if (0==BLI_isect_rctf((rctf *)&face->box, (rctf *)&bspn->box, NULL))
                        return;
                
                /* test all points inside branch */
-               for(a=bspn->totsamp-1; a>=0; a--) {
+               for (a=bspn->totsamp-1; a>=0; a--) {
                        ISBSample *samp= bspn->samples[a];
                        
-                       if((samp->facenr!=face->facenr || samp->obi!=face->obi) && samp->shadfac) {
-                               if(face->box.zmin < samp->zco[2]) {
-                                       if(BLI_in_rctf((rctf *)&face->box, samp->zco[0], samp->zco[1])) {
+                       if ((samp->facenr!=face->facenr || samp->obi!=face->obi) && samp->shadfac) {
+                               if (face->box.zmin < samp->zco[2]) {
+                                       if (BLI_in_rctf((rctf *)&face->box, samp->zco[0], samp->zco[1])) {
                                                int inshadow= 0;
                                                
-                                               if(face->type) {
-                                                       if(point_behind_strand(samp->zco, face)) 
+                                               if (face->type) {
+                                                       if (point_behind_strand(samp->zco, face))
                                                                inshadow= 1;
                                                }
-                                               else if( point_behind_tria2d(samp->zco, face->v1, face->v2, face->v3))
+                                               else if ( point_behind_tria2d(samp->zco, face->v1, face->v2, face->v3))
                                                        inshadow= 1;
-                                               else if(face->v4 && point_behind_tria2d(samp->zco, face->v1, face->v3, face->v4))
+                                               else if (face->v4 && point_behind_tria2d(samp->zco, face->v1, face->v3, face->v4))
                                                        inshadow= 1;
 
-                                               if(inshadow) {
+                                               if (inshadow) {
                                                        *(samp->shadfac) += face->shad_alpha;
                                                        /* optimize; is_full means shad_alpha==4096 */
-                                                       if(*(samp->shadfac) >= 4096 || face->is_full) {
+                                                       if (*(samp->shadfac) >= 4096 || face->is_full) {
                                                                bspn->full++;
                                                                samp->shadfac= NULL;
                                                        }
@@ -1853,15 +1854,15 @@ static void isb_bsp_face_inside(ISBBranch *bspn, BSPFace *face)
 /* based on available samples, recalculate the bounding box for bsp nodes, recursive */
 static void isb_bsp_recalc_box(ISBBranch *root)
 {
-       if(root->left) {
+       if (root->left) {
                isb_bsp_recalc_box(root->left);
                isb_bsp_recalc_box(root->right);
        }
-       else if(root->totsamp) {
+       else if (root->totsamp) {
                int a;
                
                init_box(&root->box);
-               for(a=root->totsamp-1; a>=0; a--)
+               for (a=root->totsamp-1; a>=0; a--)
                        bound_boxf(&root->box, root->samples[a]->zco);
        }       
 }
@@ -1878,7 +1879,7 @@ static void isb_bsp_test_strand(ZSpan *zspan, int obi, int zvlnr, float *v1, flo
        face.obi= obi;
        face.facenr= zvlnr & ~RE_QUAD_OFFS;
        face.type= R_STRAND;
-       if(R.osa)
+       if (R.osa)
                face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha/(float)R.osa);
        else
                face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha);
@@ -1890,7 +1891,7 @@ static void isb_bsp_test_strand(ZSpan *zspan, int obi, int zvlnr, float *v1, flo
        bound_boxf(&face.box, v1);
        bound_boxf(&face.box, v2);
        bound_boxf(&face.box, v3);
-       if(v4)
+       if (v4)
                bound_boxf(&face.box, v4);
        
        /* optimize values */
@@ -1912,7 +1913,7 @@ static void isb_bsp_test_face(ZSpan *zspan, int obi, int zvlnr, float *v1, float
        face.obi= obi;
        face.facenr= zvlnr & ~RE_QUAD_OFFS;
        face.type= 0;
-       if(R.osa)
+       if (R.osa)
                face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha/(float)R.osa);
        else
                face.shad_alpha= (short)ceil(4096.0f*zspan->shad_alpha);
@@ -1924,7 +1925,7 @@ static void isb_bsp_test_face(ZSpan *zspan, int obi, int zvlnr, float *v1, float
        bound_boxf(&face.box, v1);
        bound_boxf(&face.box, v2);
        bound_boxf(&face.box, v3);
-       if(v4)
+       if (v4)
                bound_boxf(&face.box, v4);
 
        isb_bsp_face_inside((ISBBranch *)zspan->rectz, &face);
@@ -1935,11 +1936,11 @@ static int testclip_minmax(const float ho[4], const float minmax[4])
        float wco= ho[3];
        int flag= 0;
        
-       if( ho[0] > minmax[1]*wco) flag = 1;
-       else if( ho[0]< minmax[0]*wco) flag = 2;
+       if ( ho[0] > minmax[1]*wco) flag = 1;
+       else if ( ho[0]< minmax[0]*wco) flag = 2;
        
-       if( ho[1] > minmax[3]*wco) flag |= 4;
-       else if( ho[1]< minmax[2]*wco) flag |= 8;
+       if ( ho[1] > minmax[3]*wco) flag |= 4;
+       else if ( ho[1]< minmax[2]*wco) flag |= 8;
        
        return flag;
 }
@@ -1966,7 +1967,7 @@ static void isb_bsp_fillfaces(Render *re, LampRen *lar, ISBBranch *root)
        minmaxf[2]= (2.0f*root->box.ymin - size-2.0f)/size;
        minmaxf[3]= (2.0f*root->box.ymax - size+2.0f)/size;
        
-       if(lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
+       if (lar->mode & (LA_LAYER|LA_LAYER_SHADOW)) lay= lar->lay;
        
        /* (ab)use zspan, since we use zbuffer clipping code */
        zbuf_alloc_span(&zspan, size, size, re->clipcrop);
@@ -1985,29 +1986,29 @@ static void isb_bsp_fillfaces(Render *re, LampRen *lar, ISBBranch *root)
        zspan.zbuffunc= isb_bsp_test_face;
        zspanstrand.zbuffunc= isb_bsp_test_strand;
        
-       for(i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
+       for (i=0, obi=re->instancetable.first; obi; i++, obi=obi->next) {
                obr= obi->obr;
 
-               if(obi->flag & R_TRANSFORMED)
+               if (obi->flag & R_TRANSFORMED)
                        mult_m4_m4m4(winmat, shb->persmat, obi->mat);
                else
                        copy_m4_m4(winmat, shb->persmat);
 
-               for(a=0; a<obr->totvlak; a++) {
+               for (a=0; a<obr->totvlak; a++) {
                        
-                       if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
+                       if ((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
                        else vlr++;
                        
                        /* note, these conditions are copied in shadowbuf_autoclip() */
-                       if(vlr->mat!= ma) {
+                       if (vlr->mat!= ma) {
                                ma= vlr->mat;
                                ok= 1;
-                               if((ma->mode & MA_SHADBUF)==0) ok= 0;
-                               if(ma->material_type == MA_TYPE_WIRE) ok= 0;
+                               if ((ma->mode & MA_SHADBUF)==0) ok= 0;
+                               if (ma->material_type == MA_TYPE_WIRE) ok= 0;
                                zspanstrand.shad_alpha= zspan.shad_alpha= ma->shad_alpha;
                        }
                        
-                       if(ok && (obi->lay & lay)) {
+                       if (ok && (obi->lay & lay)) {
                                float hoco[4][4];
                                int c1, c2, c3, c4=0;
                                int d1, d2, d3, d4=0;
@@ -2017,32 +2018,32 @@ static void isb_bsp_fillfaces(Render *re, LampRen *lar, ISBBranch *root)
                                projectvert(vlr->v1->co, winmat, hoco[0]); d1= testclip_minmax(hoco[0], minmaxf);
                                projectvert(vlr->v2->co, winmat, hoco[1]); d2= testclip_minmax(hoco[1], minmaxf);
                                projectvert(vlr->v3->co, winmat, hoco[2]); d3= testclip_minmax(hoco[2], minmaxf);
-                               if(vlr->v4) {
+                               if (vlr->v4) {
                                        projectvert(vlr->v4->co, winmat, hoco[3]); d4= testclip_minmax(hoco[3], minmaxf);
                                }
 
                                /* minmax clipping */
-                               if(vlr->v4) partclip= d1 & d2 & d3 & d4;
+                               if (vlr->v4) partclip= d1 & d2 & d3 & d4;
                                else partclip= d1 & d2 & d3;
                                
-                               if(partclip==0) {
+                               if (partclip==0) {
                                        
                                        /* window clipping */
                                        c1= testclip(hoco[0]); 
                                        c2= testclip(hoco[1]); 
                                        c3= testclip(hoco[2]); 
-                                       if(vlr->v4)
+                                       if (vlr->v4)
                                                c4= testclip(hoco[3]); 
                                        
                                        /* ***** NO WIRE YET */                 
-                                       if(ma->material_type == MA_TYPE_WIRE) {
-                                               if(vlr->v4)
+                                       if (ma->material_type == MA_TYPE_WIRE) {
+                                               if (vlr->v4)
                                                        zbufclipwire(&zspan, i, a+1, vlr->ec, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
                                                else
                                                        zbufclipwire(&zspan, i, a+1, vlr->ec, hoco[0], hoco[1], hoco[2], 0, c1, c2, c3, 0);
                                        }
-                                       else if(vlr->v4) {
-                                               if(vlr->flag & R_STRAND)
+                                       else if (vlr->v4) {
+                                               if (vlr->flag & R_STRAND)
                                                        zbufclip4(&zspanstrand, i, a+1, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
                                                else
                                                        zbufclip4(&zspan, i, a+1, hoco[0], hoco[1], hoco[2], hoco[3], c1, c2, c3, c4);
@@ -2066,7 +2067,7 @@ static int viewpixel_to_lampbuf(ShadBuf *shb, ObjectInstanceRen *obi, VlakRen *v
        
        RE_vlakren_get_normal(&R, obi, vlr, nor);
        copy_v3_v3(v1, vlr->v1->co);
-       if(obi->flag & R_TRANSFORMED)
+       if (obi->flag & R_TRANSFORMED)
                mul_m4_v3(obi->mat, v1);
 
        /* from shadepixel() */
@@ -2074,7 +2075,7 @@ static int viewpixel_to_lampbuf(ShadBuf *shb, ObjectInstanceRen *obi, VlakRen *v
        hoco[3]= 1.0f;
        
        /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
-       if(R.r.mode & R_ORTHO) {
+       if (R.r.mode & R_ORTHO) {
                /* x and y 3d coordinate can be derived from pixel coord and winmat */
                float fx= 2.0f/(R.winx*R.winmat[0][0]);
                float fy= 2.0f/(R.winy*R.winmat[1][1]);
@@ -2083,7 +2084,7 @@ static int viewpixel_to_lampbuf(ShadBuf *shb, ObjectInstanceRen *obi, VlakRen *v
                hoco[1]= (y - 0.5f*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
                
                /* using a*x + b*y + c*z = d equation, (a b c) is normal */
-               if(nor[2]!=0.0f)
+               if (nor[2]!=0.0f)
                        hoco[2]= (dface - nor[0]*hoco[0] - nor[1]*hoco[1])/nor[2];
                else
                        hoco[2]= 0.0f;
@@ -2110,11 +2111,11 @@ static int viewpixel_to_lampbuf(ShadBuf *shb, ObjectInstanceRen *obi, VlakRen *v
        /* clip We can test for -1.0/1.0 because of the properties of the
         * coordinate transformations. */
        fac= fabs(hoco[3]);
-       if(hoco[0]<-fac || hoco[0]>fac)
+       if (hoco[0]<-fac || hoco[0]>fac)
                return 0;
-       if(hoco[1]<-fac || hoco[1]>fac)
+       if (hoco[1]<-fac || hoco[1]>fac)
                return 0;
-       if(hoco[2]<-fac || hoco[2]>fac)
+       if (hoco[2]<-fac || hoco[2]>fac)
                return 0;
        
        siz= 0.5f*(float)shb->size;
@@ -2135,7 +2136,7 @@ static void isb_add_shadfac(ISBShadfacA **isbsapp, MemArena *mem, int obi, int f
        float shadfacf;
        
        /* in osa case, the samples were filled in with factor 1.0/R.osa. if fewer samples we have to correct */
-       if(R.osa)
+       if (R.osa)
                shadfacf= ((float)shadfac*R.osa)/(4096.0f*samples);
        else
                shadfacf= ((float)shadfac)/(4096.0f);
@@ -2144,7 +2145,7 @@ static void isb_add_shadfac(ISBShadfacA **isbsapp, MemArena *mem, int obi, int f
        new->obi= obi;
        new->facenr= facenr & ~RE_QUAD_OFFS;
        new->shadfac= shadfacf;
-       if(*isbsapp)
+       if (*isbsapp)
                new->next= (*isbsapp);
        else
                new->next= NULL;
@@ -2161,25 +2162,25 @@ static int isb_add_samples(RenderPart *pa, ISBBranch *root, MemArena *memarena,
        /* bsp split doesn't like to handle regular sequences */
        xcos= MEM_mallocN( pa->rectx*sizeof(int), "xcos");
        ycos= MEM_mallocN( pa->recty*sizeof(int), "ycos");
-       for(xi=0; xi<pa->rectx; xi++)
+       for (xi=0; xi<pa->rectx; xi++)
                xcos[xi]= xi;
-       for(yi=0; yi<pa->recty; yi++)
+       for (yi=0; yi<pa->recty; yi++)
                ycos[yi]= yi;
        BLI_array_randomize(xcos, sizeof(int), pa->rectx, 12345);
        BLI_array_randomize(ycos, sizeof(int), pa->recty, 54321);
        
-       for(sample=0; sample<(R.osa?R.osa:1); sample++) {
+       for (sample=0; sample<(R.osa?R.osa:1); sample++) {
                ISBSample *samp= samplebuf[sample], *samp1;
                
-               for(yi=0; yi<pa->recty; yi++) {
+               for (yi=0; yi<pa->recty; yi++) {
                        int y= ycos[yi];
-                       for(xi=0; xi<pa->rectx; xi++) {
+                       for (xi=0; xi<pa->rectx; xi++) {
                                int x= xcos[xi];
                                samp1= samp + y*pa->rectx + x;
-                               if(samp1->facenr)
+                               if (samp1->facenr)
                                        bsp_err |= isb_bsp_insert(root, memarena, samp1);
                        }
-                       if(bsp_err) break;
+                       if (bsp_err) break;
                }
        }       
        
@@ -2215,11 +2216,11 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
        BLI_memarena_use_calloc(memarena);
        
        /* samplebuf is in camera view space (pixels) */
-       for(sample=0; sample<(R.osa?R.osa:1); sample++)
+       for (sample=0; sample<(R.osa?R.osa:1); sample++)
                samplebuf[sample]= MEM_callocN(sizeof(ISBSample)*pa->rectx*pa->recty, "isb samplebuf");
        
        /* for end result, ISBSamples point to this in non OSA case, otherwise to pixstruct->shadfac */
-       if(R.osa==0)
+       if (R.osa==0)
                isbdata->shadfacs= MEM_callocN(pa->rectx*pa->recty*sizeof(short), "isb shadfacs");
        
        /* setup bsp root */
@@ -2228,34 +2229,34 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
        root.box.ymin = (float)shb->size;
        
        /* create the sample buffers */
-       for(sindex=0, y=0; y<pa->recty; y++) {
-               for(x=0; x<pa->rectx; x++, sindex++) {
+       for (sindex=0, y=0; y<pa->recty; y++) {
+               for (x=0; x<pa->rectx; x++, sindex++) {
                        
                        /* this makes it a long function, but splitting it out would mean 10+ arguments */
                        /* first check OSA case */
-                       if(R.osa) {
+                       if (R.osa) {
                                rd= pa->rectdaps + sindex;
-                               if(*rd) {
+                               if (*rd) {
                                        float xs= (float)(x + pa->disprect.xmin);
                                        float ys= (float)(y + pa->disprect.ymin);
                                        
-                                       for(sample=0; sample<R.osa; sample++) {
+                                       for (sample=0; sample<R.osa; sample++) {
                                                PixStr *ps= (PixStr *)(*rd);
                                                int mask= (1<<sample);
                                                
                                                while(ps) {
-                                                       if(ps->mask & mask)
+                                                       if (ps->mask & mask)
                                                                break;
                                                        ps= ps->next;
                                                }
-                                               if(ps && ps->facenr>0) {
+                                               if (ps && ps->facenr>0) {
                                                        ObjectInstanceRen *obi= &R.objectinstance[ps->obi];
                                                        ObjectRen *obr= obi->obr;
                                                        VlakRen *vlr= RE_findOrAddVlak(obr, (ps->facenr-1) & RE_QUAD_MASK);
                                                        
                                                        samp= samplebuf[sample] + sindex;
                                                        /* convert image plane pixel location to lamp buffer space */
-                                                       if(viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], samp->zco)) {
+                                                       if (viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], samp->zco)) {
                                                                samp->obi= ps->obi;
                                                                samp->facenr= ps->facenr & ~RE_QUAD_OFFS;
                                                                ps->shadfac= 0;
@@ -2269,7 +2270,7 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
                        else {
                                rectp= pa->rectp + sindex;
                                recto= pa->recto + sindex;
-                               if(*rectp>0) {
+                               if (*rectp>0) {
                                        ObjectInstanceRen *obi= &R.objectinstance[*recto];
                                        ObjectRen *obr= obi->obr;
                                        VlakRen *vlr= RE_findOrAddVlak(obr, (*rectp-1) & RE_QUAD_MASK);
@@ -2278,7 +2279,7 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
                                        
                                        samp= samplebuf[0] + sindex;
                                        /* convert image plane pixel location to lamp buffer space */
-                                       if(viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, samp->zco)) {
+                                       if (viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, samp->zco)) {
                                                samp->obi= *recto;
                                                samp->facenr= *rectp & ~RE_QUAD_OFFS;
                                                samp->shadfac= isbdata->shadfacs + sindex;
@@ -2290,7 +2291,7 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
        }
        
        /* simple method to see if we have samples */
-       if(root.box.xmin != (float)shb->size) {
+       if (root.box.xmin != (float)shb->size) {
                /* now create a regular split, root.box has the initial bounding box of all pixels */
                /* split bsp 8 levels deep, in regular grid (16 x 16) */
                isb_bsp_split_init(&root, memarena, 8);
@@ -2298,24 +2299,24 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
                /* insert all samples in BSP now */
                bsp_err= isb_add_samples(pa, &root, memarena, samplebuf);
                        
-               if(bsp_err==0) {
+               if (bsp_err==0) {
                        /* go over all faces and fill in shadow values */
                        
                        isb_bsp_fillfaces(&R, lar, &root);      /* shb->persmat should have been calculated */
                        
                        /* copy shadow samples to persistent buffer, reduce memory overhead */
-                       if(R.osa) {
+                       if (R.osa) {
                                ISBShadfacA **isbsa= isbdata->shadfaca= MEM_callocN(pa->rectx*pa->recty*sizeof(void *), "isb shadfacs");
                                
                                isbdata->memarena = BLI_memarena_new(0x8000 * sizeof(ISBSampleA), "isb arena");
                                BLI_memarena_use_calloc(isbdata->memarena);
 
-                               for(rd= pa->rectdaps, x=pa->rectx*pa->recty; x>0; x--, rd++, isbsa++) {
+                               for (rd= pa->rectdaps, x=pa->rectx*pa->recty; x>0; x--, rd++, isbsa++) {
                                        
-                                       if(*rd) {
+                                       if (*rd) {
                                                PixStr *ps= (PixStr *)(*rd);
                                                while(ps) {
-                                                       if(ps->shadfac)
+                                                       if (ps->shadfac)
                                                                isb_add_shadfac(isbsa, isbdata->memarena, ps->obi, ps->facenr, ps->shadfac, count_mask(ps->mask));
                                                        ps= ps->next;
                                                }
@@ -2325,7 +2326,7 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
                }
        }
        else {
-               if(isbdata->shadfacs) {
+               if (isbdata->shadfacs) {
                        MEM_freeN(isbdata->shadfacs);
                        isbdata->shadfacs= NULL;
                }
@@ -2335,10 +2336,10 @@ static void isb_make_buffer(RenderPart *pa, LampRen *lar)
        BLI_memarena_free(memarena);
        
        /* free samples */
-       for(x=0; x<(R.osa?R.osa:1); x++)
+       for (x=0; x<(R.osa?R.osa:1); x++)
                MEM_freeN(samplebuf[x]);
        
-       if(bsp_err) printf("error in filling bsp\n");
+       if (bsp_err) printf("error in filling bsp\n");
 }
 
 /* add sample to buffer, isbsa is the root sample in a buffer */
@@ -2347,7 +2348,7 @@ static ISBSampleA *isb_alloc_sample_transp(ISBSampleA **isbsa, MemArena *mem)
        ISBSampleA *new;
        
        new= BLI_memarena_alloc(mem, sizeof(ISBSampleA));
-       if(*isbsa)
+       if (*isbsa)
                new->next= (*isbsa);
        else
                new->next= NULL;
@@ -2365,19 +2366,19 @@ static int isb_add_samples_transp(RenderPart *pa, ISBBranch *root, MemArena *mem
        /* bsp split doesn't like to handle regular sequences */
        xcos= MEM_mallocN( pa->rectx*sizeof(int), "xcos");
        ycos= MEM_mallocN( pa->recty*sizeof(int), "ycos");
-       for(xi=0; xi<pa->rectx; xi++)
+       for (xi=0; xi<pa->rectx; xi++)
                xcos[xi]= xi;
-       for(yi=0; yi<pa->recty; yi++)
+       for (yi=0; yi<pa->recty; yi++)
                ycos[yi]= yi;
        BLI_array_randomize(xcos, sizeof(int), pa->rectx, 12345);
        BLI_array_randomize(ycos, sizeof(int), pa->recty, 54321);
        
-       for(sample=0; sample<(R.osa?R.osa:1); sample++) {
+       for (sample=0; sample<(R.osa?R.osa:1); sample++) {
                ISBSampleA **samp= samplebuf[sample], *samp1;
                
-               for(yi=0; yi<pa->recty; yi++) {
+               for (yi=0; yi<pa->recty; yi++) {
                        int y= ycos[yi];
-                       for(xi=0; xi<pa->rectx; xi++) {
+                       for (xi=0; xi<pa->rectx; xi++) {
                                int x= xcos[xi];
                                
                                samp1= *(samp + y*pa->rectx + x);
@@ -2386,7 +2387,7 @@ static int isb_add_samples_transp(RenderPart *pa, ISBBranch *root, MemArena *mem
                                        samp1= samp1->next;
                                }
                        }
-                       if(bsp_err) break;
+                       if (bsp_err) break;
                }
        }       
        
@@ -2423,7 +2424,7 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
        BLI_memarena_use_calloc(memarena);
        
        /* samplebuf is in camera view space (pixels) */
-       for(sample=0; sample<(R.osa?R.osa:1); sample++)
+       for (sample=0; sample<(R.osa?R.osa:1); sample++)
                samplebuf[sample]= MEM_callocN(sizeof(void *)*pa->rectx*pa->recty, "isb alpha samplebuf");
        
        /* setup bsp root */
@@ -2432,18 +2433,18 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
        root.box.ymin = (float)shb->size;
 
        /* create the sample buffers */
-       for(ap= apixbuf, sindex=0, y=0; y<pa->recty; y++) {
-               for(x=0; x<pa->rectx; x++, sindex++, ap++) {
+       for (ap= apixbuf, sindex=0, y=0; y<pa->recty; y++) {
+               for (x=0; x<pa->rectx; x++, sindex++, ap++) {
                        
-                       if(ap->p[0]) {
+                       if (ap->p[0]) {
                                APixstr *apn;
                                float xs= (float)(x + pa->disprect.xmin);
                                float ys= (float)(y + pa->disprect.ymin);
                                
-                               for(apn=ap; apn; apn= apn->next) {
+                               for (apn=ap; apn; apn= apn->next) {
                                        int a;
-                                       for(a=0; a<4; a++) {
-                                               if(apn->p[a]) {
+                                       for (a=0; a<4; a++) {
+                                               if (apn->p[a]) {
                                                        ObjectInstanceRen *obi= &R.objectinstance[apn->obi[a]];
                                                        ObjectRen *obr= obi->obr;
                                                        VlakRen *vlr= RE_findOrAddVlak(obr, (apn->p[a]-1) & RE_QUAD_MASK);
@@ -2452,14 +2453,14 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
                                                        /* here we store shadfac, easier to create the end storage buffer. needs zero'ed, multiple shadowbufs use it */
                                                        apn->shadfac[a]= 0;
                                                        
-                                                       if(R.osa) {
-                                                               for(sample=0; sample<R.osa; sample++) {
+                                                       if (R.osa) {
+                                                               for (sample=0; sample<R.osa; sample++) {
                                                                        int mask= (1<<sample);
                                                                        
-                                                                       if(apn->mask[a] & mask) {
+                                                                       if (apn->mask[a] & mask) {
                                                                                
                                                                                /* convert image plane pixel location to lamp buffer space */
-                                                                               if(viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], zco)) {
+                                                                               if (viewpixel_to_lampbuf(shb, obi, vlr, xs + R.jit[sample][0], ys + R.jit[sample][1], zco)) {
                                                                                        samp= isb_alloc_sample_transp(samplebuf[sample] + sindex, memarena);
                                                                                        samp->obi= apn->obi[a];
                                                                                        samp->facenr= apn->p[a] & ~RE_QUAD_OFFS;
@@ -2474,7 +2475,7 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
                                                        else {
                                                                
                                                                /* convert image plane pixel location to lamp buffer space */
-                                                               if(viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, zco)) {
+                                                               if (viewpixel_to_lampbuf(shb, obi, vlr, xs, ys, zco)) {
                                                                        
                                                                        samp= isb_alloc_sample_transp(samplebuf[0] + sindex, memarena);
                                                                        samp->obi= apn->obi[a];
@@ -2493,7 +2494,7 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
        }
        
        /* simple method to see if we have samples */
-       if(root.box.xmin != (float)shb->size) {
+       if (root.box.xmin != (float)shb->size) {
                /* now create a regular split, root.box has the initial bounding box of all pixels */
                /* split bsp 8 levels deep, in regular grid (16 x 16) */
                isb_bsp_split_init(&root, memarena, 8);
@@ -2501,7 +2502,7 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
                /* insert all samples in BSP now */
                bsp_err= isb_add_samples_transp(pa, &root, memarena, samplebuf);
                
-               if(bsp_err==0) {
+               if (bsp_err==0) {
                        ISBShadfacA **isbsa;
                        
                        /* go over all faces and fill in shadow values */
@@ -2512,15 +2513,15 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
                        
                        isbdata->memarena = BLI_memarena_new(0x8000 * sizeof(ISBSampleA), "isb arena");
                        
-                       for(ap= apixbuf, x=pa->rectx*pa->recty; x>0; x--, ap++, isbsa++) {
+                       for (ap= apixbuf, x=pa->rectx*pa->recty; x>0; x--, ap++, isbsa++) {
                                        
-                               if(ap->p[0]) {
+                               if (ap->p[0]) {
                                        APixstr *apn;
-                                       for(apn=ap; apn; apn= apn->next) {
+                                       for (apn=ap; apn; apn= apn->next) {
                                                int a;
-                                               for(a=0; a<4; a++) {
-                                                       if(apn->p[a] && apn->shadfac[a]) {
-                                                               if(R.osa)
+                                               for (a=0; a<4; a++) {
+                                                       if (apn->p[a] && apn->shadfac[a]) {
+                                                               if (R.osa)
                                                                        isb_add_shadfac(isbsa, isbdata->memarena, apn->obi[a], apn->p[a], apn->shadfac[a], count_mask(apn->mask[a]));
                                                                else
                                                                        isb_add_shadfac(isbsa, isbdata->memarena, apn->obi[a], apn->p[a], apn->shadfac[a], 0);
@@ -2536,10 +2537,10 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
        BLI_memarena_free(memarena);
 
        /* free samples */
-       for(x=0; x<(R.osa?R.osa:1); x++)
+       for (x=0; x<(R.osa?R.osa:1); x++)
                MEM_freeN(samplebuf[x]);
 
-       if(bsp_err) printf("error in filling bsp\n");
+       if (bsp_err) printf("error in filling bsp\n");
 }
 
 
@@ -2551,18 +2552,18 @@ static void isb_make_buffer_transp(RenderPart *pa, APixstr *apixbuf, LampRen *la
 float ISB_getshadow(ShadeInput *shi, ShadBuf *shb)
 {
        /* if raytracing, we can't accept irregular shadow */
-       if(shi->depth==0) {
+       if (shi->depth==0) {
                ISBData *isbdata= shb->isb_result[shi->thread];
                
-               if(isbdata) {
-                       if(isbdata->shadfacs || isbdata->shadfaca) {
+               if (isbdata) {
+                       if (isbdata->shadfacs || isbdata->shadfaca) {
                                int x= shi->xs - isbdata->minx;
                                
-                               if(x >= 0 && x < isbdata->rectx) {
+                               if (x >= 0 && x < isbdata->rectx) {
                                        int y= shi->ys - isbdata->miny;
                        
-                                       if(y >= 0 && y < isbdata->recty) {
-                                               if(isbdata->shadfacs) {
+                                       if (y >= 0 && y < isbdata->recty) {
+                                               if (isbdata->shadfacs) {
                                                        short *sp= isbdata->shadfacs + y*isbdata->rectx + x;
                                                        return *sp>=4096?0.0f:1.0f - ((float)*sp)/4096.0f;
                                                }
@@ -2572,7 +2573,7 @@ float ISB_getshadow(ShadeInput *shi, ShadBuf *shb)
                                                        ISBShadfacA *isbsa= *(isbdata->shadfaca + sindex);
                                                        
                                                        while(isbsa) {
-                                                               if(isbsa->facenr==shi->facenr+1 && isbsa->obi==obi)
+                                                               if (isbsa->facenr==shi->facenr+1 && isbsa->obi==obi)
                                                                        return isbsa->shadfac>=1.0f?0.0f:1.0f - isbsa->shadfac;
                                                                isbsa= isbsa->next;
                                                        }
@@ -2591,15 +2592,15 @@ void ISB_create(RenderPart *pa, APixstr *apixbuf)
        GroupObject *go;
        
        /* go over all lamps, and make the irregular buffers */
-       for(go=R.lights.first; go; go= go->next) {
+       for (go=R.lights.first; go; go= go->next) {
                LampRen *lar= go->lampren;
                
-               if(lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
+               if (lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
                        
                        /* create storage for shadow, per thread */
                        lar->shb->isb_result[pa->thread]= MEM_callocN(sizeof(ISBData), "isb data");
                        
-                       if(apixbuf)
+                       if (apixbuf)
                                isb_make_buffer_transp(pa, apixbuf, lar);
                        else
                                isb_make_buffer(pa, lar);
@@ -2614,19 +2615,19 @@ void ISB_free(RenderPart *pa)
        GroupObject *go;
        
        /* go over all lamps, and free the irregular buffers */
-       for(go=R.lights.first; go; go= go->next) {
+       for (go=R.lights.first; go; go= go->next) {
                LampRen *lar= go->lampren;
                
-               if(lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
+               if (lar->type==LA_SPOT && lar->shb && lar->buftype==LA_SHADBUF_IRREGULAR) {
                        ISBData *isbdata= lar->shb->isb_result[pa->thread];
 
-                       if(isbdata) {
-                               if(isbdata->shadfacs)
+                       if (isbdata) {
+                               if (isbdata->shadfacs)
                                        MEM_freeN(isbdata->shadfacs);
-                               if(isbdata->shadfaca)
+                               if (isbdata->shadfaca)
                                        MEM_freeN(isbdata->shadfaca);
                                
-                               if(isbdata->memarena)
+                               if (isbdata->memarena)
                                        BLI_memarena_free(isbdata->memarena);
                                
                                MEM_freeN(isbdata);