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

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 163 lines | 130 code | 33 blank | 0 comment | 0 complexity | a3c2e543f075a0b5dc447656151c446b MD5 | raw file
  1/** 
  2 * @file waterF.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_FragData[3];
 30#endif
 31
 32vec3 scaleSoftClip(vec3 inColor);
 33vec3 atmosTransport(vec3 inColor);
 34
 35uniform sampler2D bumpMap;   
 36uniform sampler2D screenTex;
 37uniform sampler2D refTex;
 38uniform sampler2DRectShadow shadowMap0;
 39uniform sampler2DRectShadow shadowMap1;
 40uniform sampler2DRectShadow shadowMap2;
 41uniform sampler2DRectShadow shadowMap3;
 42uniform sampler2D noiseMap;
 43
 44uniform mat4 shadow_matrix[6];
 45uniform vec4 shadow_clip;
 46
 47uniform float sunAngle;
 48uniform float sunAngle2;
 49uniform vec3 lightDir;
 50uniform vec3 specular;
 51uniform float lightExp;
 52uniform float refScale;
 53uniform float kd;
 54uniform vec2 screenRes;
 55uniform vec3 normScale;
 56uniform float fresnelScale;
 57uniform float fresnelOffset;
 58uniform float blurMultiplier;
 59uniform vec2 screen_res;
 60uniform mat4 norm_mat; //region space to screen space
 61
 62//bigWave is (refCoord.w, view.w);
 63VARYING vec4 refCoord;
 64VARYING vec4 littleWave;
 65VARYING vec4 view;
 66VARYING vec4 vary_position;
 67
 68void main() 
 69{
 70	vec4 color;
 71	float dist = length(view.xy);
 72	
 73	//normalize view vector
 74	vec3 viewVec = normalize(view.xyz);
 75	
 76	//get wave normals
 77	vec3 wave1 = texture2D(bumpMap, vec2(refCoord.w, view.w)).xyz*2.0-1.0;
 78	vec3 wave2 = texture2D(bumpMap, littleWave.xy).xyz*2.0-1.0;
 79	vec3 wave3 = texture2D(bumpMap, littleWave.zw).xyz*2.0-1.0;
 80	//get base fresnel components	
 81	
 82	vec3 df = vec3(
 83					dot(viewVec, wave1),
 84					dot(viewVec, (wave2 + wave3) * 0.5),
 85					dot(viewVec, wave3)
 86				 ) * fresnelScale + fresnelOffset;
 87	df *= df;
 88		    
 89	vec2 distort = (refCoord.xy/refCoord.z) * 0.5 + 0.5;
 90	
 91	float dist2 = dist;
 92	dist = max(dist, 5.0);
 93	
 94	float dmod = sqrt(dist);
 95	
 96	vec2 dmod_scale = vec2(dmod*dmod, dmod);
 97	
 98	//get reflected color
 99	vec2 refdistort1 = wave1.xy*normScale.x;
100	vec2 refvec1 = distort+refdistort1/dmod_scale;
101	vec4 refcol1 = texture2D(refTex, refvec1);
102	
103	vec2 refdistort2 = wave2.xy*normScale.y;
104	vec2 refvec2 = distort+refdistort2/dmod_scale;
105	vec4 refcol2 = texture2D(refTex, refvec2);
106	
107	vec2 refdistort3 = wave3.xy*normScale.z;
108	vec2 refvec3 = distort+refdistort3/dmod_scale;
109	vec4 refcol3 = texture2D(refTex, refvec3);
110
111	vec4 refcol = refcol1 + refcol2 + refcol3;
112	float df1 = df.x + df.y + df.z;
113	refcol *= df1 * 0.333;
114	
115	vec3 wavef = (wave1 + wave2 * 0.4 + wave3 * 0.6) * 0.5;
116	//wavef.z *= max(-viewVec.z, 0.1);
117	wavef = normalize(wavef);
118	
119	float df2 = dot(viewVec, wavef) * fresnelScale+fresnelOffset;
120	
121	vec2 refdistort4 = wavef.xy*0.125;
122	refdistort4.y -= abs(refdistort4.y);
123	vec2 refvec4 = distort+refdistort4/dmod;
124	float dweight = min(dist2*blurMultiplier, 1.0);
125	vec4 baseCol = texture2D(refTex, refvec4);
126	refcol = mix(baseCol*df2, refcol, dweight);
127
128	//get specular component
129	//float spec = clamp(dot(lightDir, (reflect(viewVec,wavef))),0.0,1.0);
130		
131	//harden specular
132	//spec = pow(spec, 128.0);
133
134	//figure out distortion vector (ripply)   
135	vec2 distort2 = distort+wavef.xy*refScale/max(dmod*df1, 1.0);
136		
137	vec4 fb = texture2D(screenTex, distort2);
138	
139	//mix with reflection
140	// Note we actually want to use just df1, but multiplying by 0.999999 gets around an nvidia compiler bug
141	color.rgb = mix(fb.rgb, refcol.rgb, df1 * 0.99999);
142	
143	float shadow = 1.0;
144	vec4 pos = vary_position;
145	
146	//vec3 nz = texture2D(noiseMap, gl_FragCoord.xy/128.0).xyz;
147	vec4 spos = pos;
148		
149	//spec *= shadow;
150	//color.rgb += spec * specular;
151	
152	//color.rgb = atmosTransport(color.rgb);
153	//color.rgb = scaleSoftClip(color.rgb);
154	//color.a = spec * sunAngle2;
155
156	//wavef.z *= 0.1f;
157	//wavef = normalize(wavef);
158	vec3 screenspacewavef = (norm_mat*vec4(wavef, 1.0)).xyz;
159	
160	gl_FragData[0] = vec4(color.rgb, 0.5); // diffuse
161	gl_FragData[1] = vec4(0.5,0.5,0.5, 0.95); // speccolor*spec, spec
162	gl_FragData[2] = vec4(screenspacewavef.xy*0.5+0.5, screenspacewavef.z, screenspacewavef.z*0.5); // normalxyz, displace
163}