PageRenderTime 565ms CodeModel.GetById 281ms app.highlight 2ms RepoModel.GetById 280ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 159 lines | 123 code | 36 blank | 0 comment | 0 complexity | 5cf08164aa7bd6df597aacafccc6ec51 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;
 29
 30// SKY ////////////////////////////////////////////////////////////////////////
 31// The vertex shader for creating the atmospheric sky
 32///////////////////////////////////////////////////////////////////////////////
 33
 34// Output parameters
 35VARYING vec4 vary_HazeColor;
 36
 37// Inputs
 38uniform vec3 camPosLocal;
 39
 40uniform vec4 lightnorm;
 41uniform vec4 sunlight_color;
 42uniform vec4 ambient;
 43uniform vec4 blue_horizon;
 44uniform vec4 blue_density;
 45uniform vec4 haze_horizon;
 46uniform vec4 haze_density;
 47
 48uniform vec4 cloud_shadow;
 49uniform vec4 density_multiplier;
 50uniform vec4 max_y;
 51
 52uniform vec4 glow;
 53
 54uniform vec4 cloud_color;
 55
 56uniform vec4 cloud_scale;
 57
 58void main()
 59{
 60
 61	// World / view / projection
 62	gl_Position = modelview_projection_matrix * vec4(position.xyz, 1.0);
 63
 64	// Get relative position
 65	vec3 P = position.xyz - camPosLocal.xyz + vec3(0,50,0);
 66	//vec3 P = position.xyz + vec3(0,50,0);
 67
 68	// Set altitude
 69	if (P.y > 0.)
 70	{
 71		P *= (max_y.x / P.y);
 72	}
 73	else
 74	{
 75		P *= (-32000. / P.y);
 76	}
 77
 78	// Can normalize then
 79	vec3 Pn = normalize(P);
 80	float  Plen = length(P);
 81
 82	// Initialize temp variables
 83	vec4 temp1 = vec4(0.);
 84	vec4 temp2 = vec4(0.);
 85	vec4 blue_weight;
 86	vec4 haze_weight;
 87	vec4 sunlight = sunlight_color;
 88	vec4 light_atten;
 89
 90
 91	// Sunlight attenuation effect (hue and brightness) due to atmosphere
 92	// this is used later for sunlight modulation at various altitudes
 93	light_atten = (blue_density * 1.0 + haze_density.x * 0.25) * (density_multiplier.x * max_y.x);
 94
 95	// Calculate relative weights
 96	temp1 = blue_density + haze_density.x;
 97	blue_weight = blue_density / temp1;
 98	haze_weight = haze_density.x / temp1;
 99
100	// Compute sunlight from P & lightnorm (for long rays like sky)
101	temp2.y = max(0., max(0., Pn.y) * 1.0 + lightnorm.y );
102	temp2.y = 1. / temp2.y;
103	sunlight *= exp( - light_atten * temp2.y);
104
105	// Distance
106	temp2.z = Plen * density_multiplier.x;
107
108	// Transparency (-> temp1)
109	// ATI Bugfix -- can't store temp1*temp2.z in a variable because the ati
110	// compiler gets confused.
111	temp1 = exp(-temp1 * temp2.z);
112
113
114	// Compute haze glow
115	temp2.x = dot(Pn, lightnorm.xyz);
116	temp2.x = 1. - temp2.x;
117		// temp2.x is 0 at the sun and increases away from sun
118	temp2.x = max(temp2.x, .001);	
119		// Set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
120	temp2.x *= glow.x;
121		// Higher glow.x gives dimmer glow (because next step is 1 / "angle")
122	temp2.x = pow(temp2.x, glow.z);
123		// glow.z should be negative, so we're doing a sort of (1 / "angle") function
124
125	// Add "minimum anti-solar illumination"
126	temp2.x += .25;
127
128
129	// Haze color above cloud
130	vary_HazeColor = (	  blue_horizon * blue_weight * (sunlight + ambient)
131				+ (haze_horizon.r * haze_weight) * (sunlight * temp2.x + ambient)
132			 );	
133
134
135	// Increase ambient when there are more clouds
136	vec4 tmpAmbient = ambient;
137	tmpAmbient += (1. - tmpAmbient) * cloud_shadow.x * 0.5; 
138
139	// Dim sunlight by cloud shadow percentage
140	sunlight *= (1. - cloud_shadow.x);
141
142	// Haze color below cloud
143	vec4 additiveColorBelowCloud = (	  blue_horizon * blue_weight * (sunlight + tmpAmbient)
144				+ (haze_horizon.r * haze_weight) * (sunlight * temp2.x + tmpAmbient)
145			 );	
146
147	// Final atmosphere additive
148	vary_HazeColor *= (1. - temp1);
149	
150	// Attenuate cloud color by atmosphere
151	temp1 = sqrt(temp1);	//less atmos opacity (more transparency) below clouds
152
153	// At horizon, blend high altitude sky color towards the darker color below the clouds
154	vary_HazeColor += (additiveColorBelowCloud - vary_HazeColor) * (1. - sqrt(temp1));
155	
156	// won't compile on mac without this being set
157	//vary_AtmosAttenuation = vec3(0.0,0.0,0.0);
158}
159