PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class2/windlight/cloudsV.glsl

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