PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 123 lines | 105 code | 18 blank | 0 comment | 0 complexity | edf52213de06b70bcb58d8dadf84cad6 MD5 | raw file
  1/** 
  2 * @file pointLightF.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 diffuseRect;
 33uniform sampler2DRect specularRect;
 34uniform sampler2DRect normalMap;
 35uniform samplerCube environmentMap;
 36uniform sampler2D noiseMap;
 37uniform sampler2D lightFunc;
 38uniform sampler2DRect depthMap;
 39
 40uniform vec3 env_mat[3];
 41uniform float sun_wash;
 42
 43uniform vec3 center;
 44uniform vec3 color;
 45uniform float falloff;
 46uniform float size;
 47
 48VARYING vec4 vary_fragcoord;
 49uniform vec2 screen_res;
 50
 51uniform mat4 inv_proj;
 52uniform vec4 viewport;
 53
 54vec4 getPosition(vec2 pos_screen)
 55{
 56	float depth = texture2DRect(depthMap, pos_screen.xy).r;
 57	vec2 sc = (pos_screen.xy-viewport.xy)*2.0;
 58	sc /= viewport.zw;
 59	sc -= vec2(1.0,1.0);
 60	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 61	vec4 pos = inv_proj * ndc;
 62	pos /= pos.w;
 63	pos.w = 1.0;
 64	return pos;
 65}
 66
 67void main() 
 68{
 69	vec4 frag = vary_fragcoord;
 70	frag.xyz /= frag.w;
 71	frag.xyz = frag.xyz*0.5+0.5;
 72	frag.xy *= screen_res;
 73	
 74	vec3 pos = getPosition(frag.xy).xyz;
 75	vec3 lv = center.xyz-pos;
 76	float dist2 = dot(lv,lv);
 77	dist2 /= size;
 78	if (dist2 > 1.0)
 79	{
 80		discard;
 81	}
 82	
 83	vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
 84	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
 85	float da = dot(norm, lv);
 86	if (da < 0.0)
 87	{
 88		discard;
 89	}
 90	
 91	norm = normalize(norm);
 92	lv = normalize(lv);
 93	da = dot(norm, lv);
 94	
 95	float noise = texture2D(noiseMap, frag.xy/128.0).b;
 96	
 97	vec3 col = texture2DRect(diffuseRect, frag.xy).rgb;
 98	float fa = falloff+1.0;
 99	float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
100	float lit = da * dist_atten * noise;
101	
102	col = color.rgb*lit*col;
103
104	vec4 spec = texture2DRect(specularRect, frag.xy);
105	if (spec.a > 0.0)
106	{
107		float sa = dot(normalize(lv-normalize(pos)),norm);
108		if (sa > 0.0)
109		{
110			sa = texture2D(lightFunc, vec2(sa, spec.a)).r * min(dist_atten*4.0, 1.0);
111			sa *= noise;
112			col += da*sa*color.rgb*spec.rgb;
113		}
114	}
115	
116	if (dot(col, col) <= 0.0)
117	{
118		discard;
119	}
120		
121	gl_FragColor.rgb = col;	
122	gl_FragColor.a = 0.0;
123}