diff --git a/indra/llinventory/llsettingssky.cpp b/indra/llinventory/llsettingssky.cpp
index f3519dc7cbe83208bdeec7e607332498bbfa349a..612c97168cd5436a9e40926ee19b53ae1c40797b 100644
--- a/indra/llinventory/llsettingssky.cpp
+++ b/indra/llinventory/llsettingssky.cpp
@@ -34,7 +34,7 @@
 
 //=========================================================================
 namespace {
-    const F32 NIGHTTIME_ELEVATION = -8.0f; // degrees
+    const F32 NIGHTTIME_ELEVATION = 8.0f; // degrees
     const F32 NIGHTTIME_ELEVATION_SIN = (F32)sinf(NIGHTTIME_ELEVATION * DEG_TO_RAD);
     const LLUUID IMG_BLOOM1("3c59f7fe-9dc8-47f9-8aaf-a9dd1fbc3bef");
     const LLUUID IMG_RAINBOW("11b4c57c-56b3-04ed-1f82-2004363882e4");
@@ -952,16 +952,38 @@ void LLSettingsSky::updateSettings()
     calculateLightSettings();
 }
 
+F32 LLSettingsSky::getSunMoonGlowFactor() const
+{
+    LLVector3 sunDir = getSunDirection();
+    LLVector3 moonDir = getMoonDirection();
+
+    // sun glow at full iff moon is not up
+    if (sunDir.mV[VZ] > -NIGHTTIME_ELEVATION_SIN)
+    {
+        if (moonDir.mV[2] <= 0.0f)
+        {
+            return 1.0f;
+        }
+    }
+
+    if (moonDir.mV[2] > 0.0f)
+    {
+        return moonDir.mV[VZ] / 3.0f; // ramp moon glow at moonset
+    }
+
+    return 0.0f;
+}
+
 bool LLSettingsSky::getIsSunUp() const
 {
     LLVector3 sunDir = getSunDirection();
-    return sunDir.mV[2] > NIGHTTIME_ELEVATION_SIN;
+    return (sunDir.mV[2] >= 0.0f) || ((sunDir.mV[2] > -NIGHTTIME_ELEVATION_SIN) && !getIsMoonUp());
 }
 
 bool LLSettingsSky::getIsMoonUp() const
 {
     LLVector3 moonDir = getMoonDirection();
-    return moonDir.mV[2] > NIGHTTIME_ELEVATION_SIN;
+    return moonDir.mV[2] > 0.0f;
 }
 
 void LLSettingsSky::calculateHeavenlyBodyPositions()  const
diff --git a/indra/llinventory/llsettingssky.h b/indra/llinventory/llsettingssky.h
index e82ab8db0c055b367360dd67c07a98bd06b0bd73..65c0f2c58163bf2e83c086a4c8ed0ec25d52f318 100644
--- a/indra/llinventory/llsettingssky.h
+++ b/indra/llinventory/llsettingssky.h
@@ -272,6 +272,9 @@ class LLSettingsSky: public LLSettingsBase
     bool getIsSunUp() const;
     bool getIsMoonUp() const;
 
+    // determines how much the haze glow effect occurs in rendering
+    F32 getSunMoonGlowFactor() const;
+
     LLVector3 getLightDirection() const;
     LLColor3  getLightDiffuse() const;
 
diff --git a/indra/llrender/llshadermgr.cpp b/indra/llrender/llshadermgr.cpp
index 807e41dc9710db8625e0e91c0155a8fb98db9efe..3989c1e2e4ca57f2f8ddc6ee5b0608f8d97bf2f0 100644
--- a/indra/llrender/llshadermgr.cpp
+++ b/indra/llrender/llshadermgr.cpp
@@ -1350,7 +1350,7 @@ void LLShaderMgr::initAttribsAndUniforms()
     mReservedUniforms.push_back("sh_input_g");
     mReservedUniforms.push_back("sh_input_b");
 
-    mReservedUniforms.push_back("sun_up_factor");
+    mReservedUniforms.push_back("sun_moon_glow_factor");
     mReservedUniforms.push_back("water_edge");
 
 	llassert(mReservedUniforms.size() == END_RESERVED_UNIFORMS);
diff --git a/indra/llrender/llshadermgr.h b/indra/llrender/llshadermgr.h
index 86512658348fc6c1139178af84241754a1751b8e..ef6f33f2e52f273ffa8690c2fbb8a76445bad907 100644
--- a/indra/llrender/llshadermgr.h
+++ b/indra/llrender/llshadermgr.h
@@ -248,7 +248,7 @@ class LLShaderMgr
         SH_INPUT_L1G,
         SH_INPUT_L1B,
 
-        SUN_UP_FACTOR,
+        SUN_MOON_GLOW_FACTOR,
         WATER_EDGE_FACTOR,
 		END_RESERVED_UNIFORMS
 	} eGLSLReservedUniforms;
diff --git a/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl b/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
index baf54c1fbe84a83167453d32aa033de9aedc5fc7..53391da9aa34ea166be7e4544ab8dbc62f80dac1 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/cloudsV.glsl
@@ -58,7 +58,7 @@ uniform float density_multiplier;
 uniform float max_y;
 
 uniform vec4 glow;
-uniform float sun_up_factor;
+uniform float sun_moon_glow_factor;
 
 uniform vec4 cloud_color;
 
@@ -132,7 +132,7 @@ void main()
 	temp2.x = pow(temp2.x, glow.z);
 		// glow.z should be negative, so we're doing a sort of (1 / "angle") function
 
-        //temp2.x *= sun_up_factor;
+        temp2.x *= sun_moon_glow_factor;
 
 	// Add "minimum anti-solar illumination"
 	temp2.x += .25;
diff --git a/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl b/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl
index 7265275e364b944e4757a6d5beccc93221d42215..c6c12f80f816597ab8da218a122b2c68d5698555 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/moonF.glsl
@@ -61,5 +61,7 @@ void main()
     frag_data[0] = vec4(c.rgb, c.a);
     frag_data[1] = vec4(0.0);
     frag_data[2] = vec4(0.0f);
+
+    gl_FragDepth = 0.9996f;
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/shadowUtil.glsl b/indra/newview/app_settings/shaders/class1/deferred/shadowUtil.glsl
index 843901ea6ad703968a12b5371d8cef5c69360f11..6c509b76163ffd52a44c256d849fbbaaf2d87c7d 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/shadowUtil.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/shadowUtil.glsl
@@ -42,7 +42,6 @@ uniform float shadow_bias;
 
 uniform float spot_shadow_bias;
 uniform float spot_shadow_offset;
-uniform float sun_up_factor;
 uniform mat4 inv_proj;
 uniform vec2 screen_res;
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/skyF.glsl b/indra/newview/app_settings/shaders/class1/deferred/skyF.glsl
index 06131116327c2f10de8b3256cfe9318bb7070535..f8172cae17e34fdd85a214b7e0a073357851e83b 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/skyF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/skyF.glsl
@@ -44,18 +44,20 @@ vec3 scaleSoftClip(vec3 light);
 
 void main()
 {
-	// Potential Fill-rate optimization.  Add cloud calculation 
-	// back in and output alpha of 0 (so that alpha culling kills 
-	// the fragment) if the sky wouldn't show up because the clouds 
-	// are fully opaque.
-
-	vec4 color;
-	color = vary_HazeColor;
-	color *= 2.;
-
-	/// Gamma correct for WL (soft clip effect).
-	frag_data[0] = vec4(scaleSoftClip(color.rgb), 1.0);
-	frag_data[1] = vec4(0.0,0.0,0.0,0.0);
-	frag_data[2] = vec4(0.5,0.5,0.0,1.0); //1.0 in norm.w masks off fog
+    // Potential Fill-rate optimization.  Add cloud calculation 
+    // back in and output alpha of 0 (so that alpha culling kills 
+    // the fragment) if the sky wouldn't show up because the clouds 
+    // are fully opaque.
+
+    vec4 color;
+    color = vary_HazeColor;
+    color *= 2.;
+
+    /// Gamma correct for WL (soft clip effect).
+    frag_data[0] = vec4(scaleSoftClip(color.rgb), 1.0);
+    frag_data[1] = vec4(0.0,0.0,0.0,0.0);
+    frag_data[2] = vec4(0.5,0.5,0.0,1.0); //1.0 in norm.w masks off fog
+
+    gl_FragDepth = 0.999f;
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/starsF.glsl b/indra/newview/app_settings/shaders/class1/deferred/starsF.glsl
index 4ae3f7b76fc557aa028961924b8a961768f8e826..67e7604e22fdeaa42bc5f122571dfc90cb4ee8c2 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/starsF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/starsF.glsl
@@ -49,8 +49,8 @@ float twinkle(){
 
 void main() 
 {
-	vec4 col_a = texture2D(diffuseMap, vary_texcoord0.xy);
-	vec4 col_b = texture2D(diffuseMap, vary_texcoord0.xy);
+    vec4 col_a = texture2D(diffuseMap, vary_texcoord0.xy);
+    vec4 col_b = texture2D(diffuseMap, vary_texcoord0.xy);
     vec4 col = mix(col_b, col_a, blend_factor);
     col.rgb *= vertex_color.rgb;
  
@@ -59,8 +59,10 @@ void main()
     col.a = (col.a * factor) * 32.0f;
     col.a *= twinkle();
 
-	frag_data[0] = col;
+    frag_data[0] = col;
     frag_data[1] = vec4(0.0f);
     frag_data[2] = vec4(0.0, 1.0, 0.0, 1.0);
+
+    gl_FragDepth = 0.9998;
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl b/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl
index 40d4c24d342fce104a0da7766adec2c53b8c6f67..ee028d819457baccc1807d63e7b51ca36e2349ff 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl
@@ -165,11 +165,10 @@ void main()
     
     color.rgb = atmosTransport(color.rgb);
     color.rgb = scaleSoftClipFrag(color.rgb);
-    color.a   = spec * sunAngle2;
 
     vec3 screenspacewavef = normalize((norm_mat*vec4(wavef, 1.0)).xyz);
 
-    frag_data[0] = vec4(color.rgb, color); // diffuse
+    frag_data[0] = vec4(color.rgb, 1); // diffuse
     frag_data[1] = vec4(0);     // speccolor, spec
     frag_data[2] = vec4(encode_normal(screenspacewavef.xyz*0.5+0.5), 0.05, 0);// normalxy, 0, 0
 }
diff --git a/indra/newview/app_settings/shaders/class2/deferred/skyF.glsl b/indra/newview/app_settings/shaders/class2/deferred/skyF.glsl
index 792aa4e85cd39247cf8fe57d6d69a43c26d6608a..7e2d41cf7b90f2a748a3225caba05041c5396a2a 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/skyF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/skyF.glsl
@@ -45,7 +45,7 @@ uniform float density_multiplier;
 uniform float max_y;
 
 uniform vec4 glow;
-uniform float sun_up_factor;
+uniform float sun_moon_glow_factor;
 
 uniform vec4 cloud_color;
 
@@ -153,7 +153,7 @@ void main()
     // Add "minimum anti-solar illumination"
     temp2.x += .25;
 
-    //temp2.x *= sun_up_factor;
+    temp2.x *= sun_moon_glow_factor;
 
     // Haze color above cloud
     vec4 color = (    blue_horizon * blue_weight * (sunlight + ambient)
diff --git a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
index 4fd3e7333624ffb0c0e35d34ad06697cfb7efad9..2c1cc3026f853db83b4ec905adcec2e53dbe76b9 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
@@ -91,7 +91,7 @@ void main()
         
     float da_sun  = dot(norm.xyz, normalize(sun_dir.xyz));
     float da_moon = dot(norm.xyz, normalize(moon_dir.xyz));
-    float da = max(da_sun, da_moon);
+    float da = (da_sun > 0.0) ? da_sun : da_moon;
           da = clamp(da, 0.0, 1.0);
 
     da = pow(da, global_gamma + 0.3);
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
index 8d1e5e3281f3e2514d2a0a680357aeaa2b4af631..683ab794d384a26f3de009aebec9e75c4648808b 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsF.glsl
@@ -42,7 +42,7 @@ uniform vec4 glow;
 uniform float scene_light_strength;
 uniform mat3 ssao_effect_mat;
 uniform int no_atmo;
-uniform float sun_up_factor;
+uniform float sun_moon_glow_factor;
 
 vec3 scaleSoftClipFrag(vec3 light);
 
@@ -119,7 +119,7 @@ void calcFragAtmospherics(vec3 inPositionEye, float ambFactor, out vec3 sunlit,
     //add "minimum anti-solar illumination"
     temp2.x += .25;
 
-    //temp2.x *= sun_up_factor;
+    temp2.x *= sun_moon_glow_factor;
     
     //increase ambient when there are more clouds
     vec4 tmpAmbient = ambient + (vec4(1.) - ambient) * cloud_shadow * 0.5;
diff --git a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl
index 5ccf786fce66f2490204257904b5889d30d76ca4..86cdae8768ce10cc37601f8fbdabc1dd43c454b6 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl
@@ -54,7 +54,7 @@ uniform float density_multiplier;
 uniform float distance_multiplier;
 uniform float max_y;
 uniform vec4 glow;
-uniform float sun_up_factor;
+uniform float sun_moon_glow_factor;
 
 void calcAtmospherics(vec3 inPositionEye) {
 
@@ -118,7 +118,7 @@ void calcAtmospherics(vec3 inPositionEye) {
     temp2.x = pow(temp2.x, glow.z);
         //glow.z should be negative, so we're doing a sort of (1 / "angle") function
 
-        temp2.x *= sun_up_factor;
+    temp2.x *= sun_moon_glow_factor;
 
     //add "minimum anti-solar illumination"
     temp2.x += .25;
diff --git a/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl b/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
index a250cf2c454c8dbfbac39b0ab2e3600a772e32d0..3e3bd929e1347b57fac3d9b977e18e59c8e2235b 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl
@@ -57,7 +57,7 @@ uniform float density_multiplier;
 uniform float max_y;
 
 uniform vec4 glow;
-uniform float sun_up_factor;
+uniform float sun_moon_glow_factor;
 
 uniform vec4 cloud_color;
 
@@ -131,7 +131,7 @@ void main()
     temp2.x = pow(temp2.x, glow.z);
         // glow.z should be negative, so we're doing a sort of (1 / "angle") function
 
-    //temp2.x *= sun_up_factor;
+    temp2.x *= sun_moon_glow_factor;
 
     // Add "minimum anti-solar illumination"
     temp2.x += .25;
diff --git a/indra/newview/app_settings/shaders/class2/windlight/skyV.glsl b/indra/newview/app_settings/shaders/class2/windlight/skyV.glsl
index f509f9f8d4652afdef2c839c44b6b2eff1ad3d8e..f9f1eb735519d1504654311cbdc451b7bc6a52e0 100644
--- a/indra/newview/app_settings/shaders/class2/windlight/skyV.glsl
+++ b/indra/newview/app_settings/shaders/class2/windlight/skyV.glsl
@@ -50,7 +50,7 @@ uniform float density_multiplier;
 uniform float max_y;
 
 uniform vec4 glow;
-uniform float sun_up_factor;
+uniform float sun_moon_glow_factor;
 uniform vec4 cloud_color;
 
 void main()
@@ -119,7 +119,7 @@ void main()
 	temp2.x = pow(temp2.x, glow.z);
 		// glow.z should be negative, so we're doing a sort of (1 / "angle") function
 
-        //temp2.x *= sun_up_factor;
+        temp2.x *= sun_moon_glow_factor;
 
 	// Add "minimum anti-solar illumination"
 	temp2.x += .25;
diff --git a/indra/newview/lldrawpoolavatar.cpp b/indra/newview/lldrawpoolavatar.cpp
index 996c7eed4b6e49da81515ba387a54fdf4dbcc426..eaf5b7dd68effd4a3295e2aeb8ba31006069e727 100644
--- a/indra/newview/lldrawpoolavatar.cpp
+++ b/indra/newview/lldrawpoolavatar.cpp
@@ -2001,10 +2001,13 @@ void LLDrawPoolAvatar::renderRigged(LLVOAvatar* avatar, U32 type, bool glow)
 			{
 				gGL.matrixMode(LLRender::MM_TEXTURE);
 				gGL.loadMatrix((F32*) face->mTextureMatrix->mMatrix);
+
 				buff->setBuffer(data_mask);
 				buff->drawRange(LLRender::TRIANGLES, start, end, count, offset);
+
+                gGL.matrixMode(LLRender::MM_TEXTURE);
 				gGL.loadIdentity();
-				gGL.matrixMode(LLRender::MM_MODELVIEW);
+				
 			}
 			else
 			{
diff --git a/indra/newview/lldrawpoolwater.cpp b/indra/newview/lldrawpoolwater.cpp
index c674d9a57672d86df2029c74cf100ec3c65153bd..23749d7adb04f9e0eef7a8cc62a9fce60535e25c 100644
--- a/indra/newview/lldrawpoolwater.cpp
+++ b/indra/newview/lldrawpoolwater.cpp
@@ -592,6 +592,7 @@ void LLDrawPoolWater::shade2(bool edge, LLGLSLShader* shader, const LLColor3& li
 	shader->uniform1f(LLShaderMgr::WATER_SUN_ANGLE, sunAngle);
 	shader->uniform1f(LLShaderMgr::WATER_SCALED_ANGLE, scaledAngle);
 	shader->uniform1f(LLShaderMgr::WATER_SUN_ANGLE2, 0.1f + 0.2f*sunAngle);
+    shader->uniform1i(LLShaderMgr::WATER_EDGE_FACTOR, edge ? 1 : 0);
 
 	LLColor4 water_color;
 	LLVector3 camera_up = LLViewerCamera::getInstance()->getUpAxis();
diff --git a/indra/newview/lldrawpoolwlsky.cpp b/indra/newview/lldrawpoolwlsky.cpp
index f9ab241988a577a67faeb637cb98c0cd00f0c509..acf2a6ea6ecf36703f6c0dee749abc28be07dfd5 100644
--- a/indra/newview/lldrawpoolwlsky.cpp
+++ b/indra/newview/lldrawpoolwlsky.cpp
@@ -232,7 +232,7 @@ void LLDrawPoolWLSky::renderSkyHazeAdvanced(const LLVector3& camPosLocal, F32 ca
 
 	    sky_shader->uniformMatrix4fv(LLShaderMgr::INVERSE_PROJECTION_MATRIX, 1, FALSE, inv_proj.m);
 
-        sky_shader->uniform1f(LLShaderMgr::SUN_UP_FACTOR, psky->getIsSunUp() ? 1.0f : 0.0f);
+        sky_shader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR, psky->getSunMoonGlowFactor());
 
         sky_shader->uniform3f(sCamPosLocal, camPosLocal.mV[0], camPosLocal.mV[1], camPosLocal.mV[2]);
 
@@ -248,7 +248,7 @@ void LLDrawPoolWLSky::renderSkyHazeDeferred(const LLVector3& camPosLocal, F32 ca
 
 	if (gPipeline.canUseWindLightShaders() && gPipeline.hasRenderType(LLPipeline::RENDER_TYPE_SKY))
 	{
-        LLGLSPipelineDepthTestSkyBox sky(true, false);
+        LLGLSPipelineDepthTestSkyBox sky(true, true);
 
         sky_shader->bind();
 
@@ -268,7 +268,7 @@ void LLDrawPoolWLSky::renderSkyHazeDeferred(const LLVector3& camPosLocal, F32 ca
         sky_shader->uniform1f(LLShaderMgr::DROPLET_RADIUS, droplet_radius);
         sky_shader->uniform1f(LLShaderMgr::ICE_LEVEL, ice_level);
 
-        sky_shader->uniform1f(LLShaderMgr::SUN_UP_FACTOR, psky->getIsSunUp() ? 1.0f : 0.0f);
+        sky_shader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR, psky->getSunMoonGlowFactor());
 
         /// Render the skydome
         renderDome(origin, camHeightLocal, sky_shader);	
@@ -479,7 +479,8 @@ void LLDrawPoolWLSky::renderSkyCloudsAdvanced(const LLVector3& camPosLocal, F32
 
         cloudshader->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
         cloudshader->uniform1f(LLShaderMgr::CLOUD_VARIANCE, cloud_variance);
-        cloudshader->uniform1f(LLShaderMgr::SUN_UP_FACTOR, psky->getIsSunUp() ? 1.0f : 0.0f);
+        cloudshader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR, psky->getSunMoonGlowFactor());
+
         cloudshader->uniform3f(sCamPosLocal, camPosLocal.mV[0], camPosLocal.mV[1], camPosLocal.mV[2]);
 
 		/// Render the skydome
@@ -534,7 +535,7 @@ void LLDrawPoolWLSky::renderSkyCloudsDeferred(const LLVector3& camPosLocal, F32
 
         cloudshader->uniform1f(LLShaderMgr::BLEND_FACTOR, blend_factor);
         cloudshader->uniform1f(LLShaderMgr::CLOUD_VARIANCE, cloud_variance);
-        cloudshader->uniform1f(LLShaderMgr::SUN_UP_FACTOR, psky->getIsSunUp() ? 1.0f : 0.0f);
+        cloudshader->uniform1f(LLShaderMgr::SUN_MOON_GLOW_FACTOR, psky->getSunMoonGlowFactor());
 
 		/// Render the skydome
         renderDome(camPosLocal, camHeightLocal, cloudshader);