PageRenderTime 55ms CodeModel.GetById 50ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class1/deferred/skyV.glsl

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 162 lines | 126 code | 36 blank | 0 comment | 0 complexity | c7289d54ca98477c7e6cf216ac7cbb11 MD5 | raw file
  1/** 
  2 * @file WLSkyV.glsl
  3 *
  4 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2005, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 */
 25
 26uniform mat4 modelview_projection_matrix;
 27
 28ATTRIBUTE vec3 position;
 29ATTRIBUTE vec2 texcoord0;
 30
 31// SKY ////////////////////////////////////////////////////////////////////////
 32// The vertex shader for creating the atmospheric sky
 33///////////////////////////////////////////////////////////////////////////////
 34
 35// Output parameters
 36VARYING vec4 vary_HazeColor;
 37VARYING vec2 vary_texcoord0;
 38
 39// Inputs
 40uniform vec3 camPosLocal;
 41
 42uniform vec4 lightnorm;
 43uniform vec4 sunlight_color;
 44uniform vec4 ambient;
 45uniform vec4 blue_horizon;
 46uniform vec4 blue_density;
 47uniform vec4 haze_horizon;
 48uniform vec4 haze_density;
 49
 50uniform vec4 cloud_shadow;
 51uniform vec4 density_multiplier;
 52uniform vec4 max_y;
 53
 54uniform vec4 glow;
 55
 56uniform vec4 cloud_color;
 57
 58uniform vec4 cloud_scale;
 59
 60void main()
 61{
 62
 63	// World / view / projection
 64	gl_Position = modelview_projection_matrix * vec4(position.xyz, 1.0);
 65	vary_texcoord0 = texcoord0;
 66
 67	// Get relative position
 68	vec3 P = position.xyz - camPosLocal.xyz + vec3(0,50,0);
 69	//vec3 P = position.xyz + vec3(0,50,0);
 70
 71	// Set altitude
 72	if (P.y > 0.)
 73	{
 74		P *= (max_y.x / P.y);
 75	}
 76	else
 77	{
 78		P *= (-32000. / P.y);
 79	}
 80
 81	// Can normalize then
 82	vec3 Pn = normalize(P);
 83	float  Plen = length(P);
 84
 85	// Initialize temp variables
 86	vec4 temp1 = vec4(0.);
 87	vec4 temp2 = vec4(0.);
 88	vec4 blue_weight;
 89	vec4 haze_weight;
 90	vec4 sunlight = sunlight_color;
 91	vec4 light_atten;
 92
 93
 94	// Sunlight attenuation effect (hue and brightness) due to atmosphere
 95	// this is used later for sunlight modulation at various altitudes
 96	light_atten = (blue_density * 1.0 + haze_density.x * 0.25) * (density_multiplier.x * max_y.x);
 97
 98	// Calculate relative weights
 99	temp1 = blue_density + haze_density.x;
100	blue_weight = blue_density / temp1;
101	haze_weight = haze_density.x / temp1;
102
103	// Compute sunlight from P & lightnorm (for long rays like sky)
104	temp2.y = max(0., max(0., Pn.y) * 1.0 + lightnorm.y );
105	temp2.y = 1. / temp2.y;
106	sunlight *= exp( - light_atten * temp2.y);
107
108	// Distance
109	temp2.z = Plen * density_multiplier.x;
110
111	// Transparency (-> temp1)
112	// ATI Bugfix -- can't store temp1*temp2.z in a variable because the ati
113	// compiler gets confused.
114	temp1 = exp(-temp1 * temp2.z);
115
116
117	// Compute haze glow
118	temp2.x = dot(Pn, lightnorm.xyz);
119	temp2.x = 1. - temp2.x;
120		// temp2.x is 0 at the sun and increases away from sun
121	temp2.x = max(temp2.x, .001);	
122		// Set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
123	temp2.x *= glow.x;
124		// Higher glow.x gives dimmer glow (because next step is 1 / "angle")
125	temp2.x = pow(temp2.x, glow.z);
126		// glow.z should be negative, so we're doing a sort of (1 / "angle") function
127
128	// Add "minimum anti-solar illumination"
129	temp2.x += .25;
130
131
132	// Haze color above cloud
133	vary_HazeColor = (	  blue_horizon * blue_weight * (sunlight + ambient)
134				+ (haze_horizon.r * haze_weight) * (sunlight * temp2.x + ambient)
135			 );	
136
137
138	// Increase ambient when there are more clouds
139	vec4 tmpAmbient = ambient;
140	tmpAmbient += (1. - tmpAmbient) * cloud_shadow.x * 0.5; 
141
142	// Dim sunlight by cloud shadow percentage
143	sunlight *= (1. - cloud_shadow.x);
144
145	// Haze color below cloud
146	vec4 additiveColorBelowCloud = (	  blue_horizon * blue_weight * (sunlight + tmpAmbient)
147				+ (haze_horizon.r * haze_weight) * (sunlight * temp2.x + tmpAmbient)
148			 );	
149
150	// Final atmosphere additive
151	vary_HazeColor *= (1. - temp1);
152	
153	// Attenuate cloud color by atmosphere
154	temp1 = sqrt(temp1);	//less atmos opacity (more transparency) below clouds
155
156	// At horizon, blend high altitude sky color towards the darker color below the clouds
157	vary_HazeColor += (additiveColorBelowCloud - vary_HazeColor) * (1. - sqrt(temp1));
158	
159	// won't compile on mac without this being set
160	//vary_AtmosAttenuation = vec3(0.0,0.0,0.0);
161}
162