svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22668:22701
[blender.git] / source / blender / render / intern / source / shadeoutput.c
index 629d60bdffc95ba1443bb9b542ce6a537995fa90..d5c8cf30b3000ec13466bd393144198012f1c701 100644 (file)
@@ -61,12 +61,14 @@ extern struct Render R;
 static ListBase *get_lights(ShadeInput *shi)
 {
        
+       if(R.r.scemode & R_PREVIEWBUTS)
+               return &R.lights;
        if(shi->light_override)
                return &shi->light_override->gobject;
-       else if(shi->mat && shi->mat->group)
+       if(shi->mat && shi->mat->group)
                return &shi->mat->group->gobject;
-       else
-               return &R.lights;
+       
+       return &R.lights;
 }
 
 #if 0
@@ -477,8 +479,12 @@ static float area_lamp_energy_multisample(LampRen *lar, float *co, float *vn)
        float *jitlamp= lar->jitter, vec[3];
        float area[4][3], intens= 0.0f;
        int a= lar->ray_totsamp;
-       
-       
+
+       /* test if co is behind lamp */
+       VECSUB(vec, co, lar->co);
+       if(INPR(vec, lar->vec) < 0.0f)
+               return 0.0f;
+
        while(a--) {
                vec[0]= jitlamp[0];
                vec[1]= jitlamp[1];
@@ -1001,6 +1007,7 @@ static void do_specular_ramp(ShadeInput *shi, float is, float t, float *spec)
 }
 
 /* pure AO, check for raytrace and world should have been done */
+/* preprocess, textures were not done, don't use shi->amb for that reason */
 void ambient_occlusion(ShadeInput *shi)
 {
        if((R.wrld.ao_gather_method == WO_AOGATHER_APPROX) && shi->mat->amb!=0.0f)
@@ -1016,8 +1023,8 @@ void ambient_occlusion(ShadeInput *shi)
 void ambient_occlusion_to_diffuse(ShadeInput *shi, float *diff)
 {
        if((R.r.mode & R_RAYTRACE) || R.wrld.ao_gather_method == WO_AOGATHER_APPROX) {
-               if(shi->mat->amb!=0.0f) {
-                       float f= R.wrld.aoenergy*shi->mat->amb;
+               if(shi->amb!=0.0f) {
+                       float f= R.wrld.aoenergy*shi->amb;
 
                        if (R.wrld.aomix==WO_AOADDSUB) {
                                diff[0] = 2.0f*shi->ao[0]-1.0f;
@@ -1175,7 +1182,7 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
 {
        Material *ma= shi->mat;
        VlakRen *vlr= shi->vlr;
-       float lv[3], lampdist, lacol[3], shadfac[4];
+       float lv[3], lampdist, lacol[3], shadfac[4], lashdw[3];
        float i, is, i_noshad, inp, *vn, *view, vnor[3], phongcorr=1.0f;
        float visifac;
        
@@ -1215,7 +1222,12 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
        lacol[1]= lar->g;
        lacol[2]= lar->b;
        
-       if(lar->mode & LA_TEXTURE)  do_lamp_tex(lar, lv, shi, lacol);
+       lashdw[0]= lar->shdwr;
+       lashdw[1]= lar->shdwg;
+       lashdw[2]= lar->shdwb;
+       
+       if(lar->mode & LA_TEXTURE)      do_lamp_tex(lar, lv, shi, lacol, LA_TEXTURE);
+       if(lar->mode & LA_SHAD_TEX)     do_lamp_tex(lar, lv, shi, lashdw, LA_SHAD_TEX);
 
                /* tangent case; calculate fake face normal, aligned with lampvector */ 
                /* note, vnor==vn is used as tangent trigger for buffer shadow */
@@ -1236,6 +1248,7 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
                else {
                        Crossf(cross, lv, vn);
                        Crossf(vnor, cross, vn);
+                       Normalize(vnor);
                }
 
                if(ma->strand_surfnor > 0.0f) {
@@ -1253,6 +1266,7 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
                float cross[3];
                Crossf(cross, lv, shi->tang);
                Crossf(vnor, cross, shi->tang);
+               Normalize(vnor);
                vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
                vn= vnor;
        }
@@ -1336,13 +1350,13 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
                                        if((lar->mode & LA_ONLYSHADOW) && i>0.0) {
                                                
                                                shadfac[3]= i*lar->energy*(1.0f-shadfac[3]);
-                                               shr->shad[0] -= shadfac[3]*shi->r;
-                                               shr->shad[1] -= shadfac[3]*shi->g;
-                                               shr->shad[2] -= shadfac[3]*shi->b;
+                                               shr->shad[0] -= shadfac[3]*shi->r*(1.0f-lashdw[0]);
+                                               shr->shad[1] -= shadfac[3]*shi->g*(1.0f-lashdw[1]);
+                                               shr->shad[2] -= shadfac[3]*shi->b*(1.0f-lashdw[2]);
                                                
-                                               shr->spec[0] -= shadfac[3]*shi->specr;
-                                               shr->spec[1] -= shadfac[3]*shi->specg;
-                                               shr->spec[2] -= shadfac[3]*shi->specb;
+                                               shr->spec[0] -= shadfac[3]*shi->specr*(1.0f-lashdw[0]);
+                                               shr->spec[1] -= shadfac[3]*shi->specg*(1.0f-lashdw[1]);
+                                               shr->spec[2] -= shadfac[3]*shi->specb*(1.0f-lashdw[2]);
                                                
                                                return;
                                        }
@@ -1360,6 +1374,10 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
                                else
                                        add_to_diffuse(shr->shad, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
                        }
+                       /* add light for colored shadow */
+                       if (i_noshad>i && !(lashdw[0]==0 && lashdw[1]==0 && lashdw[2]==0)) {
+                               add_to_diffuse(shr->shad, shi, is, lashdw[0]*(i_noshad-i)*lacol[0], lashdw[1]*(i_noshad-i)*lacol[1], lashdw[2]*(i_noshad-i)*lacol[2]);
+                       }
                        if(i_noshad>0.0f) {
                                if(passflag & (SCE_PASS_DIFFUSE|SCE_PASS_SHADOW)) {
                                        if(ma->mode & MA_SHADOW_TRA)
@@ -1373,6 +1391,8 @@ static void shade_one_light(LampRen *lar, ShadeInput *shi, ShadeResult *shr, int
                }
                
                /* specularity */
+               shadfac[3]*= phongcorr; /* note, shadfac not allowed to be stored nonlocal */
+               
                if(shadfac[3]>0.0f && shi->spec!=0.0f && !(lar->mode & LA_NO_SPEC) && !(lar->mode & LA_ONLYSHADOW)) {
                        
                        if(!(passflag & (SCE_PASS_COMBINED|SCE_PASS_SPEC)));
@@ -1443,10 +1463,10 @@ static void shade_lamp_loop_only_shadow(ShadeInput *shi, ShadeResult *shr)
                LampRen *lar;
                GroupObject *go;
                float inpr, lv[3];
-               float *vn, *view, shadfac[4];
+               float *view, shadfac[4];
                float ir, accum, visifac, lampdist;
                
-               vn= shi->vn;
+
                view= shi->view;
 
                accum= ir= 0.0f;
@@ -1550,6 +1570,24 @@ void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
                shr->col[1]= shi->g*shi->alpha;
                shr->col[2]= shi->b*shi->alpha;
                shr->col[3]= shi->alpha;
+
+               if((ma->sss_flag & MA_DIFF_SSS) && !sss_pass_done(&R, ma)) {
+                       if(ma->sss_texfac == 0.0f) {
+                               shi->r= shi->g= shi->b= shi->alpha= 1.0f;
+                               shr->col[0]= shr->col[1]= shr->col[2]= shr->col[3]= 1.0f;
+                       }
+                       else {
+                               shi->r= pow(shi->r, ma->sss_texfac);
+                               shi->g= pow(shi->g, ma->sss_texfac);
+                               shi->b= pow(shi->b, ma->sss_texfac);
+                               shi->alpha= pow(shi->alpha, ma->sss_texfac);
+                               
+                               shr->col[0]= pow(shr->col[0], ma->sss_texfac);
+                               shr->col[1]= pow(shr->col[1], ma->sss_texfac);
+                               shr->col[2]= pow(shr->col[2], ma->sss_texfac);
+                               shr->col[3]= pow(shr->col[3], ma->sss_texfac);
+                       }
+               }
        }
        
        if(ma->mode & MA_SHLESS) {
@@ -1616,29 +1654,31 @@ void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
                if (shr->shad[2] < 0) shr->shad[2] = 0;
                                                
                if(ma->sss_flag & MA_DIFF_SSS) {
-                       float sss[3], col[3], alpha, invalpha, texfac= ma->sss_texfac;
+                       float sss[3], col[3], invalpha, texfac= ma->sss_texfac;
 
                        /* this will return false in the preprocess stage */
                        if(sample_sss(&R, ma, shi->co, sss)) {
-                               alpha= shr->col[3];
-                               invalpha= (alpha > FLT_EPSILON)? 1.0f/alpha: 1.0f;
+                               invalpha= (shr->col[3] > FLT_EPSILON)? 1.0f/shr->col[3]: 1.0f;
 
                                if(texfac==0.0f) {
                                        VECCOPY(col, shr->col);
+                                       VecMulf(col, invalpha);
                                }
                                else if(texfac==1.0f) {
                                        col[0]= col[1]= col[2]= 1.0f;
+                                       VecMulf(col, invalpha);
                                }
                                else {
                                        VECCOPY(col, shr->col);
-                                       col[0]= alpha*pow(col[0]*invalpha, 1.0f-texfac);
-                                       col[1]= alpha*pow(col[1]*invalpha, 1.0f-texfac);
-                                       col[2]= alpha*pow(col[2]*invalpha, 1.0f-texfac);
+                                       VecMulf(col, invalpha);
+                                       col[0]= pow(col[0], 1.0f-texfac);
+                                       col[1]= pow(col[1], 1.0f-texfac);
+                                       col[2]= pow(col[2], 1.0f-texfac);
                                }
 
-                               shr->diff[0]= sss[0]*col[0]*invalpha;
-                               shr->diff[1]= sss[1]*col[1]*invalpha;
-                               shr->diff[2]= sss[2]*col[2]*invalpha;
+                               shr->diff[0]= sss[0]*col[0];
+                               shr->diff[1]= sss[1]*col[1];
+                               shr->diff[2]= sss[2]*col[2];
 
                                if(shi->combinedflag & SCE_PASS_SHADOW) {
                                        shr->shad[0]= shr->diff[0];
@@ -1673,7 +1713,7 @@ void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
                        shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
                        
                /* note: shi->mode! */
-               if(shi->mode & (MA_ZTRA|MA_RAYTRANSP)) {
+               if(shi->mode & MA_TRANSP) {
                        if(shi->spectra!=0.0f) {
                                float t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
                                t *= shi->spectra;
@@ -1685,16 +1725,17 @@ void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
        shr->alpha= shi->alpha;
        
        /* from now stuff everything in shr->combined: ambient, AO, radio, ramps, exposure */
-       if(!(ma->sss_flag & MA_DIFF_SSS) || !has_sss_tree(&R, ma)) {
+       if(!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
                shr->combined[0]+= shi->ambr;
                shr->combined[1]+= shi->ambg;
                shr->combined[2]+= shi->ambb;
 
+               /* removed
                if(shi->combinedflag & SCE_PASS_RADIO) {
                        shr->combined[0]+= shi->r*shi->amb*shi->rad[0];
                        shr->combined[1]+= shi->g*shi->amb*shi->rad[1];
                        shr->combined[2]+= shi->b*shi->amb*shi->rad[2];
-               }
+               }*/
                
                /* add AO in combined? */
                if(R.wrld.mode & WO_AMB_OCC) {
@@ -1733,7 +1774,21 @@ void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
        if(shi->combinedflag & SCE_PASS_SPEC)
                VECADD(shr->combined, shr->combined, shr->spec);
 
+       /* modulate by the object color */
+       if((ma->shade_flag & MA_OBCOLOR) && shi->obr->ob) {
+               if(!(ma->sss_flag & MA_DIFF_SSS) || !sss_pass_done(&R, ma)) {
+                       float obcol[4];
+
+                       QUATCOPY(obcol, shi->obr->ob->col);
+                       CLAMP(obcol[3], 0.0f, 1.0f);
+
+                       shr->combined[0] *= obcol[0];
+                       shr->combined[1] *= obcol[1];
+                       shr->combined[2] *= obcol[2];
+                       shr->alpha *= obcol[3];
+               }
+       }
+
        shr->combined[3]= shr->alpha;
 }
 
-