PageRenderTime 52ms CodeModel.GetById 30ms app.highlight 2ms RepoModel.GetById 18ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 146 lines | 120 code | 26 blank | 0 comment | 0 complexity | b489749465c68e734b0ccc089ea6ef31 MD5 | raw file
  1/** 
  2 * @file multiPointLightF.glsl
  3 *
  4 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2007, 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
 26#extension GL_ARB_texture_rectangle : enable
 27
 28#ifdef DEFINE_GL_FRAGCOLOR
 29out vec4 gl_FragColor;
 30#endif
 31
 32uniform sampler2DRect depthMap;
 33uniform sampler2DRect diffuseRect;
 34uniform sampler2DRect specularRect;
 35uniform sampler2DRect normalMap;
 36uniform samplerCube environmentMap;
 37uniform sampler2D noiseMap;
 38uniform sampler2D lightFunc;
 39
 40
 41uniform vec3 env_mat[3];
 42uniform float sun_wash;
 43
 44uniform int light_count;
 45
 46#define MAX_LIGHT_COUNT		16
 47uniform vec4 light[MAX_LIGHT_COUNT];
 48uniform vec4 light_col[MAX_LIGHT_COUNT];
 49
 50VARYING vec4 vary_fragcoord;
 51uniform vec2 screen_res;
 52
 53uniform float far_z;
 54
 55uniform mat4 inv_proj;
 56
 57vec4 getPosition(vec2 pos_screen)
 58{
 59	float depth = texture2DRect(depthMap, pos_screen.xy).r;
 60	vec2 sc = pos_screen.xy*2.0;
 61	sc /= screen_res;
 62	sc -= vec2(1.0,1.0);
 63	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 64	vec4 pos = inv_proj * ndc;
 65	pos /= pos.w;
 66	pos.w = 1.0;
 67	return pos;
 68}
 69
 70void main() 
 71{
 72	vec2 frag = (vary_fragcoord.xy*0.5+0.5)*screen_res;
 73	vec3 pos = getPosition(frag.xy).xyz;
 74	if (pos.z < far_z)
 75	{
 76		discard;
 77	}
 78	
 79	vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
 80	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
 81	norm = normalize(norm);
 82	vec4 spec = texture2DRect(specularRect, frag.xy);
 83	vec3 diff = texture2DRect(diffuseRect, frag.xy).rgb;
 84	float noise = texture2D(noiseMap, frag.xy/128.0).b;
 85	vec3 out_col = vec3(0,0,0);
 86	vec3 npos = normalize(-pos);
 87
 88	// As of OSX 10.6.7 ATI Apple's crash when using a variable size loop
 89	for (int i = 0; i < MAX_LIGHT_COUNT; ++i)
 90	{
 91		bool light_contrib = (i < light_count);
 92		
 93		vec3 lv = light[i].xyz-pos;
 94		float dist2 = dot(lv,lv);
 95		dist2 /= light[i].w;
 96		if (dist2 > 1.0)
 97		{
 98			light_contrib = false;
 99		}
100		
101		float da = dot(norm, lv);
102		if (da < 0.0)
103		{
104			light_contrib = false;
105		}
106		
107		if (light_contrib)
108		{
109			lv = normalize(lv);
110			da = dot(norm, lv);
111					
112			float fa = light_col[i].a+1.0;
113			float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
114			dist_atten *= noise;
115
116			float lit = da * dist_atten;
117			
118			vec3 col = light_col[i].rgb*lit*diff;
119			//vec3 col = vec3(dist2, light_col[i].a, lit);
120			
121			if (spec.a > 0.0)
122			{
123				//vec3 ref = dot(pos+lv, norm);
124				
125				float sa = dot(normalize(lv+npos),norm);
126				
127				if (sa > 0.0)
128				{
129					sa = texture2D(lightFunc,vec2(sa, spec.a)).r * min(dist_atten*4.0, 1.0);
130					sa *= noise;
131					col += da*sa*light_col[i].rgb*spec.rgb;
132				}
133			}
134			
135			out_col += col;
136		}
137	}
138	
139	if (dot(out_col, out_col) <= 0.0)
140	{
141		discard;
142	}
143	
144	gl_FragColor.rgb = out_col;
145	gl_FragColor.a = 0.0;
146}