CVS: data/Shaders landmass.frag, 1.4, 1.5 landmass.vert, 1.3, 1.4

Frederic Bouvier <[email protected]>
Newsgroups gmane.games.flightgear.cvc
Message-ID <[email protected]>
Update of /var/cvs/FlightGear-0.9/data/Shaders
In directory baron.flightgear.org:/tmp/cvs-serv21631/Shaders

Modified Files:
	landmass.frag landmass.vert 
Log Message:
Add relief mapping to landmass effect

Index: landmass.frag
===================================================================
RCS file: /var/cvs/FlightGear-0.9/data/Shaders/landmass.frag,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- landmass.frag	27 Mar 2010 14:02:57 -0000	1.4
+++ landmass.frag	28 Mar 2010 17:00:21 -0000	1.5
@@ -3,18 +3,68 @@
 varying vec4  rawpos;
 varying vec4  ecPosition;
 varying vec3  VNormal;
+varying vec3  VTangent;
+varying vec3  VBinormal;
 varying vec3  Normal;
-varying vec4 constantColor;
+varying vec4  constantColor;
 
 uniform sampler3D NoiseTex;
 uniform sampler2D BaseTex;
-uniform float snowlevel;
+uniform sampler2D NormalTex;
+uniform float depth_factor;
+uniform float snowlevel; // From /sim/rendering/snow-level-m
 
-// From /sim/rendering/snow-level-m
 const float scale = 1.0;
 
+float ray_intersect(sampler2D reliefMap, vec2 dp, vec2 ds)
+{
+	const int linear_search_steps = 20;
+
+	float size = 1.0 / float(linear_search_steps);
+	float depth = 0.0;
+	float best_depth = 1.0;
+
+	for(int i = 0; i < linear_search_steps - 1; ++i)
+	{
+		depth += size;
+		float t = texture2D(reliefMap, dp + ds * depth).a;
+		if(best_depth > 0.996)
+			if(depth >= t)
+				best_depth = depth;
+	}
+	depth = best_depth;
+
+	const int binary_search_steps = 5;
+
+	for(int i = 0; i < binary_search_steps; ++i)
+	{
+		size *= 0.5;
+		float t = texture2D(reliefMap, dp + ds * depth).a;
+		if(depth >= t)
+		{
+			best_depth = depth;
+			depth -= 2.0 * size;
+		}
+		depth += size;
+	}
+
+	return(best_depth);
+}
+
 void main (void)
 {
+	vec3 V = normalize(ecPosition.xyz);
+	float a = dot(VNormal, -V);
+	vec2 s = vec2(dot(V, VTangent), dot(V, VBinormal));
+	s *= depth_factor / a;
+	vec2 ds = s;
+	vec2 dp = gl_TexCoord[0].st;
+	float d = ray_intersect(NormalTex, dp, ds);
+
+	vec2 uv = dp + ds * d;
+	vec3 N = texture2D(NormalTex, uv).xyz * 2.0 - 1.0;
+
+
 	vec4 noisevec   = texture3D(NoiseTex, (rawpos.xyz)*0.01*scale);
 	vec4 nvL   = texture3D(NoiseTex, (rawpos.xyz)*0.00066*scale);
 
@@ -22,7 +72,6 @@
 	float fogCoord = ecPosition.z;
 	const float LOG2 = 1.442695;
 	fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2);
-//	float biasFactor = exp2(-0.00000002 * fogCoord * fogCoord * LOG2);
 	float biasFactor = fogFactor = clamp(fogFactor, 0.0, 1.0);
 
 	float n=0.06;
@@ -37,7 +86,7 @@
 	n += noisevec[3]*2.1;
 	n = mix(0.6, n, biasFactor);
 	// good
-	vec4 c1 = texture2D(BaseTex, gl_TexCoord[0].st);
+	vec4 c1 = texture2D(BaseTex, uv);
 	//brown
 	//c1 = mix(c1, vec4(n-0.46, n-0.45, n-0.53, 1.0), smoothstep(0.50, 0.55, nvL[2]*6.6));
 	//"steep = gray"
@@ -45,8 +94,22 @@
 	//"snow"
 	c1 = mix(c1, clamp(n+nvL[2]*4.1+vec4(0.1, 0.1, nvL[2]*2.2, 1.0), 0.7, 1.0), smoothstep(snowlevel+300.0, snowlevel+360.0, (rawpos.z)+nvL[1]*3000.0));
 
-    vec3 diffuse = gl_Color.rgb * max(0.0, dot(normalize(VNormal), gl_LightSource[0].position.xyz));
-    vec4 ambient_light = constantColor + gl_LightSource[0].diffuse * vec4(diffuse, 1.0);
+	N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
+	vec3 l = gl_LightSource[0].position.xyz;
+	vec3 diffuse = gl_Color.rgb * max(0.0, dot(N, l));
+
+// Shadow
+	dp += ds * d;
+	vec3 sl = normalize( vec3( dot( l, VTangent ), dot( l, VBinormal ), dot( -l, VNormal ) ) );
+	ds = sl.xy * depth_factor / sl.z;
+	dp -= ds * d;
+	float dl = ray_intersect(NormalTex, dp, ds);
+	float shadow_factor = 1.0;
+	if ( dl < d - 0.05 )
+		shadow_factor = dot( constantColor.xyz, vec3( 1.0, 1.0, 1.0 ) ) * 0.25;
+// end shadow
+
+	vec4 ambient_light = constantColor + gl_LightSource[0].diffuse * shadow_factor * vec4(diffuse, 1.0);
 
 	c1 *= ambient_light;
 	vec4 finalColor = c1;

Index: landmass.vert
===================================================================
RCS file: /var/cvs/FlightGear-0.9/data/Shaders/landmass.vert,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- landmass.vert	12 Feb 2010 14:44:55 -0000	1.3
+++ landmass.vert	28 Mar 2010 17:00:21 -0000	1.4
@@ -1,19 +1,26 @@
 varying vec4  rawpos;
 varying vec4  ecPosition;
 varying vec3  VNormal;
+varying vec3  VTangent;
+varying vec3  VBinormal;
 varying vec3  Normal;
-varying vec4 constantColor;
+varying vec4  constantColor;
+
+attribute vec3 tangent;
+attribute vec3 binormal;
 
 void main(void)
 {
-    rawpos     = gl_Vertex;
-    ecPosition = gl_ModelViewMatrix * gl_Vertex;
-    VNormal = normalize(gl_NormalMatrix * gl_Normal);
-    Normal = normalize(gl_Normal);
+	rawpos = gl_Vertex;
+	ecPosition = gl_ModelViewMatrix * rawpos;
+	Normal = normalize(gl_Normal);
+	VNormal = gl_NormalMatrix * gl_Normal;
+	VTangent = gl_NormalMatrix * tangent;
+	VBinormal = gl_NormalMatrix * binormal;
 
-    gl_FrontColor = gl_Color;
-    constantColor = gl_FrontMaterial.emission
-        + gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient);  
-    gl_Position = ftransform();
-    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
+	gl_FrontColor = gl_Color;
+	constantColor = gl_FrontMaterial.emission
+		+ gl_FrontColor * (gl_LightModel.ambient + gl_LightSource[0].ambient);  
+	gl_Position = ftransform();
+	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
 }


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.