Reverted incorrect merge (missing files)
[blender.git] / source / blender / radiosity / intern / source / radfactors.c
index 2f6ac2043c20d4143866a495e1772a521a3accbc..b87473dd8116de567b35ab30375140676e1e7eaf 100644 (file)
@@ -1,14 +1,11 @@
 /* ***************************************
  *
- * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. The Blender
- * Foundation also sells licenses for use in proprietary software under
- * the Blender License.  See http://www.blender.org/BL/ for information
- * about this.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,7 +23,7 @@
  *
  * Contributor(s): none yet.
  *
- * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
 
 
 
@@ -40,7 +37,6 @@
 #include <string.h>
 #include <math.h>
 
-
 #include "MEM_guardedalloc.h"
 
 #include "BLI_blenlib.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
 
-#include "BIF_screen.h"
-
 #include "radio.h"
-#include "render.h"       /* for `RE_zbufferall_radio and RE_zbufferall_radio */
-#include "blendertimer.h" /* timer functions */
+#include "RE_render_ext.h"       /* for `RE_zbufferall_radio and RE_zbufferall_radio */
 
 /* locals */
-void rad_setmatrices(RadView *vw);
-void clearsubflagelem(RNode *rn);
-void setsubflagelem(RNode *rn);
+static void rad_setmatrices(RadView *vw);
+static void clearsubflagelem(RNode *rn);
+static void setsubflagelem(RNode *rn);
 
 RadView hemitop, hemiside;
 
 float calcStokefactor(RPatch *shoot, RPatch *rp, RNode *rn, float *area)
 {
        float tvec[3], fac;
-       float vec[4][3];        /* vectoren van shootcent naar vertices rp */
-       float cross[4][3];      /* uitprodukten hiervan */
-       float rad[4];   /* hoeken tussen vecs */
+       float vec[4][3];        /* vectors of shoot->cent to vertices rp */
+       float cross[4][3];      /* cross products of this */
+       float rad[4];   /* anlgles between vecs */
 
-       /* test op richting */
+       /* test for direction */
        VecSubf(tvec, shoot->cent, rp->cent);
        if( tvec[0]*shoot->norm[0]+ tvec[1]*shoot->norm[1]+ tvec[2]*shoot->norm[2]>0.0)
                return 0.0;
        
        if(rp->type==4) {
 
-               /* hoekvectors */
+               /* corner vectors */
                VecSubf(vec[0], shoot->cent, rn->v1);
                VecSubf(vec[1], shoot->cent, rn->v2);
                VecSubf(vec[2], shoot->cent, rn->v3);
                VecSubf(vec[3], shoot->cent, rn->v4);
 
-               Normalise(vec[0]);
-               Normalise(vec[1]);
-               Normalise(vec[2]);
-               Normalise(vec[3]);
+               Normalize(vec[0]);
+               Normalize(vec[1]);
+               Normalize(vec[2]);
+               Normalize(vec[3]);
 
-               /* uitprod */
+               /* cross product */
                Crossf(cross[0], vec[0], vec[1]);
                Crossf(cross[1], vec[1], vec[2]);
                Crossf(cross[2], vec[2], vec[3]);
                Crossf(cross[3], vec[3], vec[0]);
-               Normalise(cross[0]);
-               Normalise(cross[1]);
-               Normalise(cross[2]);
-               Normalise(cross[3]);
+               Normalize(cross[0]);
+               Normalize(cross[1]);
+               Normalize(cross[2]);
+               Normalize(cross[3]);
 
-               /* hoeken */
+               /* angles */
                rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
                rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
                rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
@@ -110,7 +103,7 @@ float calcStokefactor(RPatch *shoot, RPatch *rp, RNode *rn, float *area)
                rad[2]= acos(rad[2]);
                rad[3]= acos(rad[3]);
 
-               /* Stoke formule */
+               /* Stoke formula */
                VecMulf(cross[0], rad[0]);
                VecMulf(cross[1], rad[1]);
                VecMulf(cross[2], rad[2]);
@@ -123,24 +116,24 @@ float calcStokefactor(RPatch *shoot, RPatch *rp, RNode *rn, float *area)
                fac+= tvec[0]*cross[3][0]+ tvec[1]*cross[3][1]+ tvec[2]*cross[3][2];
        }
        else {
-               /* hoekvectors */
+               /* corner vectors */
                VecSubf(vec[0], shoot->cent, rn->v1);
                VecSubf(vec[1], shoot->cent, rn->v2);
                VecSubf(vec[2], shoot->cent, rn->v3);
 
-               Normalise(vec[0]);
-               Normalise(vec[1]);
-               Normalise(vec[2]);
+               Normalize(vec[0]);
+               Normalize(vec[1]);
+               Normalize(vec[2]);
 
-               /* uitprod */
+               /* cross product */
                Crossf(cross[0], vec[0], vec[1]);
                Crossf(cross[1], vec[1], vec[2]);
                Crossf(cross[2], vec[2], vec[0]);
-               Normalise(cross[0]);
-               Normalise(cross[1]);
-               Normalise(cross[2]);
+               Normalize(cross[0]);
+               Normalize(cross[1]);
+               Normalize(cross[2]);
 
-               /* hoeken */
+               /* angles */
                rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
                rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
                rad[2]= vec[2][0]*vec[0][0]+ vec[2][1]*vec[0][1]+ vec[2][2]*vec[0][2];
@@ -149,7 +142,7 @@ float calcStokefactor(RPatch *shoot, RPatch *rp, RNode *rn, float *area)
                rad[1]= acos(rad[1]);
                rad[2]= acos(rad[2]);
 
-               /* Stoke formule */
+               /* Stoke formula */
                VecMulf(cross[0], rad[0]);
                VecMulf(cross[1], rad[1]);
                VecMulf(cross[2], rad[2]);
@@ -226,8 +219,8 @@ void calcSidefactors()
 
 void initradiosity()
 {
-       /* alloceert en maakt LUTs voor top/side factors */
-       /* alloceert en maakt index array */
+       /* allocates and makes LUTs for top/side factors */
+       /* allocates and makes index array */
        int a, hres2;
 
        if(RG.topfactors) MEM_freeN(RG.topfactors);
@@ -259,7 +252,7 @@ void rad_make_hocos(RadView *vw)
        /* } */
 }
 
-void rad_setmatrices(RadView *vw)          /* voor hemi's */
+static void rad_setmatrices(RadView *vw)           /* for hemi's */
 {
        float up1[3], len, twist;
 
@@ -288,9 +281,9 @@ void hemizbuf(RadView *vw)
        int a, b, inda, hres;
 
        rad_setmatrices(vw);
-       RE_zbufferall_radio(vw, RG.elem, RG.totelem);
+       RE_zbufferall_radio(vw, RG.elem, RG.totelem, RG.re);    /* Render for when we got renderfaces */
 
-       /* factors tellen */
+       /* count factors */
        if(vw->recty==vw->rectx) factors= RG.topfactors;
        else factors= RG.sidefactors;
        hres= RG.hemires/2;
@@ -310,20 +303,23 @@ int makeformfactors(RPatch *shoot)
 {
        RNode **re;
        float len, vec[3], up[3], side[3], tar[5][3], *fp;
-       int a, overfl;
+       int a=0, overfl;
 
        if(RG.totelem==0) return 0;
-
+       
        memset(RG.formfactors, 0, 4*RG.totelem);
 
        /* set up hemiview */
        /* first: random upvector */
        do {
+               a++;
                vec[0]= (float)BLI_drand();
                vec[1]= (float)BLI_drand();
                vec[2]= (float)BLI_drand();
                Crossf(up, shoot->norm, vec);
-               len= Normalise(up);
+               len= Normalize(up);
+               /* this safety for input normals that are zero or illegal sized */
+               if(a>3) return 0;
        } while(len==0.0 || len>1.0);
 
        VECCOPY(hemitop.up, up);
@@ -370,16 +366,6 @@ int makeformfactors(RPatch *shoot)
        }
        
        if(overfl) {
-               /*
-               drawOverflowElem();
-               while(get_mbut()&L_MOUSE==0) {
-                       if(get_mbut()&M_MOUSE) {
-                               viewmove();
-                               drawpatch_ext(shoot,0xFF77FF);
-                               drawOverflowElem();
-                       }
-               }
-               */
                if(shoot->first->down1) {
                        splitpatch(shoot);
                        return 0;
@@ -477,7 +463,7 @@ void backface_test(RPatch *shoot)
                if(rp!=shoot) {
                
                        VecSubf(tvec, shoot->cent, rp->cent);
-                       if( tvec[0]*shoot->norm[0]+ tvec[1]*shoot->norm[1]+ tvec[2]*shoot->norm[2]>0.0) {
+                       if( tvec[0]*rp->norm[0]+ tvec[1]*rp->norm[1]+ tvec[2]*rp->norm[2]<0.0) {                
                                setnodeflags(rp->first, RAD_BACKFACE, 1);
                        }
                }
@@ -529,6 +515,7 @@ void rad_init_energy()
 void progressiverad()
 {
        RPatch *shoot;
+       float unshot[3];
        int it= 0;
 
        rad_printstatus();
@@ -544,12 +531,20 @@ void progressiverad()
                
                drawpatch_ext(shoot, 0x88FF00);
 
+               if(shoot->first->f & RAD_TWOSIDED) {
+                       VECCOPY(unshot, shoot->unshot);
+                       VecNegf(shoot->norm);
+                       if(makeformfactors(shoot))
+                               applyformfactors(shoot);
+                       VecNegf(shoot->norm);
+                       VECCOPY(shoot->unshot, unshot);
+               }
+       
                if( makeformfactors(shoot) ) {
-               
                        applyformfactors(shoot);
        
                        it++;
-                       set_timecursor(it);
+                       //XXX set_timecursor(it);
                        if( (it & 3)==1 ) {
                                make_node_display();
                                rad_forcedraw();
@@ -559,7 +554,7 @@ void progressiverad()
                
                clear_backface_test();
                
-               if(MISC_test_break()) break;
+               //XXX if(blender_test_break()) break;
                if(RG.maxiter && RG.maxiter<=it) break;
 
                shoot=findshootpatch();
@@ -631,7 +626,7 @@ void minmaxradelemfilt(RNode *rn, float *min, float *max, float *errmin, float *
        }
 }
 
-void setsubflagelem(RNode *rn)
+static void setsubflagelem(RNode *rn)
 {
        
        if(rn->down1) {
@@ -643,7 +638,7 @@ void setsubflagelem(RNode *rn)
        }
 }
 
-void clearsubflagelem(RNode *rn)
+static void clearsubflagelem(RNode *rn)
 {
        
        if(rn->down1) {
@@ -675,7 +670,7 @@ void subdivideshootElements(int it)
                        shoot= findshootpatch();
                        if(shoot==0) break;
                        
-                       set_timecursor(a);
+                       //XXX set_timecursor(a);
                        drawpatch_ext(shoot, 0x88FF00);
                        
                        setnodeflags(shoot->first, RAD_SHOOT, 1);
@@ -725,16 +720,16 @@ void subdivideshootElements(int it)
                        }
                        else a--;
                        
-                       if(MISC_test_break()) break;
+                       //XXX if(blender_test_break()) break;
                }
                
-               /* test op extreem weinig kleurverloop binnen patch met subdivflag */
+               /* test for extreme small color change within a patch with subdivflag */
                
                rp= RG.patchbase.first;
                
                while(rp) {
-                       if(rp->f & RAD_SUBDIV) {                /* rp heeft elems die moet gesubd */
-                               /* minstens 4 levels diep */
+                       if(rp->f & RAD_SUBDIV) {                /* rp has elems that need subdiv */
+                               /* at least 4 levels deep */
                                rn= rp->first->down1;
                                if(rn) {
                                        rn= rn->down1;
@@ -744,15 +739,13 @@ void subdivideshootElements(int it)
                                        }
                                }
                                if(rn) {
-                                       min[0]= min[1]= min[2]= 1.0e10;
-                                       max[0]= max[1]= max[2]= -1.0e10;
-                                       /* errmin en max zijn de gefilterde kleuren */
-                                       errmin[0]= errmin[1]= errmin[2]= 1.0e10;
-                                       errmax[0]= errmax[1]= errmax[2]= -1.0e10;
+                                       INIT_MINMAX(min, max);
+                                       /* errmin and max are the filtered colors */
+                                       INIT_MINMAX(errmin, errmax);
                                        minmaxradelemfilt(rp->first, min, max, errmin, errmax);
                                        
-                                       /* verschil tussen kleuren klein: toch maar niet subd */
-                                       /* test ook voor de gefilterde: maar kritischer */
+                                       /* if small difference between colors: no subdiv */
+                                       /* also test for the filtered ones: but with higher critical level */
                                        
                                        contin= 0;
                                        a= abs( calculatecolor(min[0])-calculatecolor(max[0]));
@@ -796,7 +789,7 @@ void subdivideshootElements(int it)
                }
                makeGlobalElemArray();
 
-               if(contin==0 || MISC_test_break()) break;
+               //XXX if(contin==0 || blender_test_break()) break;
        }
        
        make_node_display();
@@ -821,7 +814,7 @@ void subdivideshootPatches(int it)
                        shoot= findshootpatch();
                        if(shoot==0) break;
                        
-                       set_timecursor(a);
+                       //XXX set_timecursor(a);
                        drawpatch_ext(shoot, 0x88FF00);
                        
                        setnodeflags(shoot->first, RAD_SHOOT, 1);
@@ -835,7 +828,7 @@ void subdivideshootPatches(int it)
                                        
                                                stoke= calcStokefactor(shoot, rp, rp->first, &area);
                                                if(stoke!= 0.0) {
-                                                       if(area>.1) {   /* ontvangt patch meer dan (ong)10% van energie? */
+                                                       if(area>.1) {   /* does patch receive more than (about)10% of energy? */
                                                                rp->f= RAD_SUBDIV;
                                                        }
                                                        else {
@@ -851,16 +844,6 @@ void subdivideshootPatches(int it)
                                                                                                                
                                                                                        rp->f= RAD_SUBDIV;
                                                                                        
-                                                                                       /* if(get_qual()&LR_SHIFTKEY);
-                                                                                       else {
-                                                                                               drawpatch_ext(rp, 0xFF77FF);
-                                       
-                                                                                               printf("Pa hemi %f stoke %f err %f area %f\n", *fp, stoke, err, area);
-                                       
-                                                                                               while(get_mbut()&L_MOUSE==0);
-                                                                                               while(get_mbut()&L_MOUSE);
-                                                                                       }
-                                                                                       */
                                                                                }
                                                                        }
                                                                }
@@ -881,7 +864,7 @@ void subdivideshootPatches(int it)
                                
                                setnodeflags(shoot->first, RAD_SHOOT, 0);
                                
-                               if(MISC_test_break()) break;
+                               //XXX if(blender_test_break()) break;
                        }
                        else a--;
                        
@@ -908,7 +891,7 @@ void subdivideshootPatches(int it)
                converttopatches();
                makeGlobalElemArray();
 
-               if(contin==0 || MISC_test_break()) break;
+               //XXX if(contin==0 || blender_test_break()) break;
        }
        make_node_display();
 }
@@ -917,13 +900,13 @@ void inithemiwindows()
 {
        RadView *vw;
 
-       /* de hemiwindows */
+       /* the hemiwindows */
        vw= &(hemitop);
        memset(vw, 0, sizeof(RadView));
        vw->rectx= RG.hemires;
        vw->recty= RG.hemires;
-       vw->rectz= MEM_mallocN(4*vw->rectx*vw->recty, "initwindows");
-       vw->rect= MEM_mallocN(4*vw->rectx*vw->recty, "initwindows");
+       vw->rectz= MEM_mallocN(sizeof(int)*vw->rectx*vw->recty, "initwindows");
+       vw->rect= MEM_mallocN(sizeof(int)*vw->rectx*vw->recty, "initwindows");
        vw->mynear= RG.maxsize/2000.0;
        vw->myfar= 2.0*RG.maxsize;
        vw->wx1= -vw->mynear;