svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / render / intern / source / pixelshading.c
index fc5ac68e8c9f42edd5ef1d72a00d96c399704ff7..2e3509f0471b6aae0665763ed2d8b32caeea73d7 100644 (file)
@@ -57,6 +57,7 @@
 #include "rendercore.h"
 #include "shadbuf.h"
 #include "pixelshading.h"
+#include "sunsky.h"
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
@@ -567,13 +568,49 @@ void shadeSkyView(float *colf, float *rco, float *view, float *dxyview)
        }
 }
 
+/* shade sky according to sun lamps, all parameters are like shadeSkyView except sunsky*/
+void shadeSunView(struct SunSky *sunsky, float *colf, float *rco, float *view, float *dxyview)
+{
+       float colorxyz[3];
+       float scale;
+                       
+       /**
+       sunAngle = sqrt(sunsky->sunSolidAngle / M_PI);
+
+       sunDir[0] = sunsky->toSun[0];
+       sunDir[1] = sunsky->toSun[1];
+       sunDir[2] = sunsky->toSun[2];
+       */
+                       
+       Normalize(view);
+       MTC_Mat3MulVecfl(R.imat, view);
+       if (view[2] < 0.0)
+               view[2] = 0.0;
+       Normalize(view);
+       GetSkyXYZRadiancef(sunsky, view, colorxyz);
+       scale = MAX3(colorxyz[0], colorxyz[1], colorxyz[2]);
+       colorxyz[0] /= scale;
+       colorxyz[1] /= scale;
+       colorxyz[2] /= scale;
+       
+       xyz_to_rgb(colorxyz[0], colorxyz[1], colorxyz[2], &colf[0], &colf[1], &colf[2]);
+
+       ClipColor(colf);
+}
+
+
 /*
   Stuff the sky color into the collector.
  */
 void shadeSkyPixel(float *collector, float fx, float fy) 
 {
        float view[3], dxyview[2];
-       
+       float sun_collector[3];
+       float suns_color[3];
+       short num_sun_lamp;
+       GroupObject *go;
+       LampRen *lar;
+
        /*
          The rules for sky:
          1. Draw an image, if a background image was provided. Stop
@@ -585,7 +622,6 @@ void shadeSkyPixel(float *collector, float fx, float fy)
        /* 1. Do a backbuffer image: */ 
        if(R.r.bufflag & 1) {
                fillBackgroundImage(collector, fx, fy);
-               return;
        } 
        else if((R.wrld.skytype & (WO_SKYBLEND+WO_SKYTEX))==0) {
                /* 2. solid color */
@@ -620,7 +656,45 @@ void shadeSkyPixel(float *collector, float fx, float fy)
                shadeSkyView(collector, NULL, view, dxyview);
                collector[3] = 0.0f;
        }
+               
+       suns_color[0] = suns_color[1] = suns_color[2] = 0;
+       num_sun_lamp = 0;
+       for(go=R.lights.first; go; go= go->next) {
+               lar= go->lampren;
+               if(lar->type==LA_SUN && lar->sunsky && (lar->sunsky->effect_type & LA_SUN_EFFECT_SKY)){
+
+                       num_sun_lamp ++;
+                       calc_view_vector(view, fx, fy);
+                       Normalize(view);
+
+                       shadeSunView(lar->sunsky, sun_collector, NULL, view, NULL);
+                       suns_color[0] += sun_collector[0];
+                       suns_color[1] += sun_collector[1];
+                       suns_color[2] += sun_collector[2];
+
+               }
+       }
+       if( num_sun_lamp > 0 ){
+               suns_color[0] /= num_sun_lamp;
+               suns_color[1] /= num_sun_lamp;
+               suns_color[2] /= num_sun_lamp;
+
+               collector[0] += suns_color[0];
+               collector[1] += suns_color[1];
+               collector[2] += suns_color[2];
+               ClipColor(collector);
+       }
 }
 
+/* aerial perspective */
+void shadeAtmPixel(struct SunSky *sunsky, float *collector, float fx, float fy, float distance)
+{
+       float view[3];
+               
+       calc_view_vector(view, fx, fy);
+       Normalize(view);
+       /*MTC_Mat3MulVecfl(R.imat, view);*/
+       AtmospherePixleShader(sunsky, view, distance, collector);
+}
 
 /* eof */