PageRenderTime 376ms CodeModel.GetById 191ms app.highlight 2ms RepoModel.GetById 181ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 191 lines | 148 code | 43 blank | 0 comment | 0 complexity | 4cb18c3cfe66a46d11fa8464d8ae6c0f MD5 | raw file
  1/** 
  2 * @file WLCloudsV.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//////////////////////////////////////////////////////////////////////////
 32// The vertex shader for creating the atmospheric sky
 33///////////////////////////////////////////////////////////////////////////////
 34
 35// Output parameters
 36VARYING vec4 vary_CloudColorSun;
 37VARYING vec4 vary_CloudColorAmbient;
 38VARYING float vary_CloudDensity;
 39
 40VARYING vec2 vary_texcoord0;
 41VARYING vec2 vary_texcoord1;
 42VARYING vec2 vary_texcoord2;
 43VARYING vec2 vary_texcoord3;
 44
 45// Inputs
 46uniform vec3 camPosLocal;
 47
 48uniform vec4 lightnorm;
 49uniform vec4 sunlight_color;
 50uniform vec4 ambient;
 51uniform vec4 blue_horizon;
 52uniform vec4 blue_density;
 53uniform vec4 haze_horizon;
 54uniform vec4 haze_density;
 55
 56uniform vec4 cloud_shadow;
 57uniform vec4 density_multiplier;
 58uniform vec4 max_y;
 59
 60uniform vec4 glow;
 61
 62uniform vec4 cloud_color;
 63
 64uniform vec4 cloud_scale;
 65
 66void main()
 67{
 68
 69	// World / view / projection
 70	gl_Position = modelview_projection_matrix * vec4(position.xyz, 1.0);
 71
 72	vary_texcoord0 = texcoord0;
 73
 74	// Get relative position
 75	vec3 P = position.xyz - camPosLocal.xyz + vec3(0,50,0);
 76
 77	// Set altitude
 78	if (P.y > 0.)
 79	{
 80		P *= (max_y.x / P.y);
 81	}
 82	else
 83	{
 84		P *= (-32000. / P.y);
 85	}
 86
 87	// Can normalize then
 88	vec3 Pn = normalize(P);
 89	float  Plen = length(P);
 90
 91	// Initialize temp variables
 92	vec4 temp1 = vec4(0.);
 93	vec4 temp2 = vec4(0.);
 94	vec4 blue_weight;
 95	vec4 haze_weight;
 96	vec4 sunlight = sunlight_color;
 97	vec4 light_atten;
 98
 99
100	// Sunlight attenuation effect (hue and brightness) due to atmosphere
101	// this is used later for sunlight modulation at various altitudes
102	light_atten = (blue_density * 1.0 + haze_density.x * 0.25) * (density_multiplier.x * max_y.x);
103
104	// Calculate relative weights
105	temp1 = blue_density + haze_density.x;
106	blue_weight = blue_density / temp1;
107	haze_weight = haze_density.x / temp1;
108
109	// Compute sunlight from P & lightnorm (for long rays like sky)
110	temp2.y = max(0., max(0., Pn.y) * 1.0 + lightnorm.y );
111	temp2.y = 1. / temp2.y;
112	sunlight *= exp( - light_atten * temp2.y);
113
114	// Distance
115	temp2.z = Plen * density_multiplier.x;
116
117	// Transparency (-> temp1)
118	// ATI Bugfix -- can't store temp1*temp2.z in a variable because the ati
119	// compiler gets confused.
120	temp1 = exp(-temp1 * temp2.z);
121
122
123	// Compute haze glow
124	temp2.x = dot(Pn, lightnorm.xyz);
125	temp2.x = 1. - temp2.x;
126		// temp2.x is 0 at the sun and increases away from sun
127	temp2.x = max(temp2.x, .001);	
128		// Set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
129	temp2.x *= glow.x;
130		// Higher glow.x gives dimmer glow (because next step is 1 / "angle")
131	temp2.x = pow(temp2.x, glow.z);
132		// glow.z should be negative, so we're doing a sort of (1 / "angle") function
133
134	// Add "minimum anti-solar illumination"
135	temp2.x += .25;
136
137	// Increase ambient when there are more clouds
138	vec4 tmpAmbient = ambient;
139	tmpAmbient += (1. - tmpAmbient) * cloud_shadow.x * 0.5; 
140
141	// Dim sunlight by cloud shadow percentage
142	sunlight *= (1. - cloud_shadow.x);
143
144	// Haze color below cloud
145	vec4 additiveColorBelowCloud = (	  blue_horizon * blue_weight * (sunlight + tmpAmbient)
146				+ (haze_horizon.r * haze_weight) * (sunlight * temp2.x + tmpAmbient)
147			 );	
148
149	// CLOUDS
150
151	sunlight = sunlight_color;
152	temp2.y = max(0., lightnorm.y * 2.);
153	temp2.y = 1. / temp2.y;
154	sunlight *= exp( - light_atten * temp2.y);
155
156	// Cloud color out
157	vary_CloudColorSun = (sunlight * temp2.x) * cloud_color;
158	vary_CloudColorAmbient = tmpAmbient * cloud_color;
159	
160	// Attenuate cloud color by atmosphere
161	temp1 = sqrt(temp1);	//less atmos opacity (more transparency) below clouds
162	vary_CloudColorSun *= temp1;
163	vary_CloudColorAmbient *= temp1;
164	vec4 oHazeColorBelowCloud = additiveColorBelowCloud * (1. - temp1);
165
166	// Make a nice cloud density based on the cloud_shadow value that was passed in.
167	vary_CloudDensity = 2. * (cloud_shadow.x - 0.25);
168
169
170	// Texture coords
171	vary_texcoord0 = texcoord0;
172	vary_texcoord0.xy -= 0.5;
173	vary_texcoord0.xy /= cloud_scale.x;
174	vary_texcoord0.xy += 0.5;
175
176	vary_texcoord1 = vary_texcoord0;
177	vary_texcoord1.x += lightnorm.x * 0.0125;
178	vary_texcoord1.y += lightnorm.z * 0.0125;
179
180	vary_texcoord2 = vary_texcoord0 * 16.;
181	vary_texcoord3 = vary_texcoord1 * 16.;
182
183	// Combine these to minimize register use
184	vary_CloudColorAmbient += oHazeColorBelowCloud;
185
186	// needs this to compile on mac
187	//vary_AtmosAttenuation = vec3(0.0,0.0,0.0);
188
189	// END CLOUDS
190}
191