1
0
Fork 0

Fix bug #750: allow cards or drivers not supporting dynamic indexing in fragment shaders to have simple shadows

This commit is contained in:
Frederic Bouvier 2012-04-29 13:35:30 +02:00
parent 5909285100
commit 76a11c2d48
4 changed files with 210 additions and 15 deletions

View file

@ -4,14 +4,26 @@
<parameters> <parameters>
<filtering><use>/sim/rendering/shadows/filtering</use></filtering> <filtering><use>/sim/rendering/shadows/filtering</use></filtering>
</parameters> </parameters>
<technique n="9"> <technique n="8">
<predicate> <predicate>
<and> <and>
<property>/sim/rendering/shadows/enabled</property> <property>/sim/rendering/shadows/enabled</property>
<or> <!-- filtering >= 3 || ( filtering >= 2 && num-cascades >= 2 ) -->
<less>
<value type="float">2.0</value>
<float-property>/sim/rendering/shadows/filtering</float-property>
</less>
<and>
<less> <less>
<value type="float">1.0</value> <value type="float">1.0</value>
<float-property>/sim/rendering/shadows/filtering</float-property> <float-property>/sim/rendering/shadows/filtering</float-property>
</less> </less>
<less>
<value type="float">1.0</value>
<float-property>/sim/rendering/shadows/num-cascades</float-property>
</less>
</and>
</or>
</and> </and>
</predicate> </predicate>
<pass> <pass>
@ -96,9 +108,16 @@
--> -->
</pass> </pass>
</technique> </technique>
<technique n="10"> <technique n="9">
<predicate> <predicate>
<and>
<property>/sim/rendering/shadows/enabled</property> <property>/sim/rendering/shadows/enabled</property>
<!-- num-cascades >= 2 -->
<less>
<value type="float">1.0</value>
<float-property>/sim/rendering/shadows/num-cascades</float-property>
</less>
</and>
</predicate> </predicate>
<pass> <pass>
<lighting>false</lighting> <lighting>false</lighting>
@ -177,6 +196,92 @@
--> -->
</pass> </pass>
</technique> </technique>
<technique n="10">
<predicate>
<property>/sim/rendering/shadows/enabled</property>
</predicate>
<pass>
<lighting>false</lighting>
<depth>
<enabled>false</enabled>
</depth>
<blend>
<source>one</source>
<destination>one</destination>
</blend>
<render-bin>
<bin-number>1</bin-number>
<bin-name>RenderBin</bin-name>
</render-bin>
<texture-unit>
<unit>0</unit>
<type>depth-buffer</type>
</texture-unit>
<texture-unit>
<unit>1</unit>
<type>normal-buffer</type>
</texture-unit>
<texture-unit>
<unit>2</unit>
<type>diffuse-buffer</type>
</texture-unit>
<texture-unit>
<unit>3</unit>
<type>spec-emis-buffer</type>
</texture-unit>
<texture-unit>
<unit>4</unit>
<type>shadow-buffer</type>
</texture-unit>
<program>
<vertex-shader>Shaders/sunlight-simple.vert</vertex-shader>
<fragment-shader>Shaders/sunlight-simple.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program>
<uniform>
<name>depth_tex</name>
<type>sampler-2d</type>
<value type="int">0</value>
</uniform>
<uniform>
<name>normal_tex</name>
<type>sampler-2d</type>
<value type="int">1</value>
</uniform>
<uniform>
<name>color_tex</name>
<type>sampler-2d</type>
<value type="int">2</value>
</uniform>
<uniform>
<name>spec_emis_tex</name>
<type>sampler-2d</type>
<value type="int">3</value>
</uniform>
<uniform>
<name>shadow_tex</name>
<type>sampler-2d</type>
<value type="int">4</value>
</uniform>
<uniform>
<name>filtering</name>
<type>int</type>
<value type="int"><use>filtering</use></value>
</uniform>
<!-- The following uniforms are automatically defined and initialized :
- fg_SunAmbientColor
- fg_SunDiffuseColor
- fg_SunSpecularColor
- fg_SunDirection
- fg_ProjectionMatrixInverse
- fg_ViewMatrixInverse
- fg_ViewMatrix
- fg_Planes
- fg_ShadowNumber
- fg_ShadowDistances
-->
</pass>
</technique>
<technique n="11"> <technique n="11">
<pass> <pass>
<lighting>false</lighting> <lighting>false</lighting>
@ -210,6 +315,7 @@
<program> <program>
<vertex-shader>Shaders/sunlight.vert</vertex-shader> <vertex-shader>Shaders/sunlight.vert</vertex-shader>
<fragment-shader>Shaders/sunlight-noshadow.frag</fragment-shader> <fragment-shader>Shaders/sunlight-noshadow.frag</fragment-shader>
<fragment-shader>Shaders/gbuffer-functions.frag</fragment-shader>
</program> </program>
<uniform> <uniform>
<name>depth_tex</name> <name>depth_tex</name>

View file

@ -8,23 +8,19 @@ uniform vec4 fg_SunSpecularColor;
uniform vec3 fg_SunDirection; uniform vec3 fg_SunDirection;
uniform vec3 fg_Planes; uniform vec3 fg_Planes;
varying vec3 ray; varying vec3 ray;
vec3 normal_decode(vec2 enc);
void main() { void main() {
vec2 coords = gl_TexCoord[0].xy; vec2 coords = gl_TexCoord[0].xy;
vec4 spec_emis = texture2D( spec_emis_tex, coords ); vec4 spec_emis = texture2D( spec_emis_tex, coords );
if ( spec_emis.a < 0.1 ) if ( spec_emis.a < 0.1 )
discard; discard;
vec3 normal; vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
normal.z = sqrt( 1.0 - dot( normal.xy, normal.xy ) );
float len = length(normal); float len = length(normal);
normal /= len; normal /= len;
vec3 viewDir = normalize(ray); vec3 viewDir = normalize(ray);
float depth = texture2D( depth_tex, coords ).r;
vec3 pos;
pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
pos.xy = viewDir.xy / viewDir.z * pos.z;
vec4 tint;
vec3 lightDir = (fg_ViewMatrix * vec4( fg_SunDirection, 0.0 )).xyz; vec3 lightDir = (fg_ViewMatrix * vec4( fg_SunDirection, 0.0 )).xyz;
lightDir = normalize( lightDir ); lightDir = normalize( lightDir );
vec3 color = texture2D( color_tex, coords ).rgb; vec3 color = texture2D( color_tex, coords ).rgb;

View file

@ -0,0 +1,74 @@
uniform mat4 fg_ViewMatrix;
uniform sampler2D depth_tex;
uniform sampler2D normal_tex;
uniform sampler2D color_tex;
uniform sampler2D spec_emis_tex;
uniform sampler2DShadow shadow_tex;
uniform vec4 fg_SunDiffuseColor;
uniform vec4 fg_SunSpecularColor;
uniform vec3 fg_SunDirection;
uniform vec3 fg_Planes;
uniform vec4 fg_ShadowDistances;
uniform int filtering;
varying vec3 ray;
varying vec4 eyePlaneS;
varying vec4 eyePlaneT;
varying vec4 eyePlaneR;
varying vec4 eyePlaneQ;
vec3 position( vec3 viewdir, float depth );
vec3 normal_decode(vec2 enc);
vec4 DynamicShadow( in vec4 ecPosition, out vec4 tint )
{
vec4 coords;
if (ecPosition.z > -fg_ShadowDistances.x) {
tint = vec4(0.0,1.0,0.0,1.0);
coords.s = dot( ecPosition, eyePlaneS );
coords.t = dot( ecPosition, eyePlaneT );
coords.p = dot( ecPosition, eyePlaneR );
coords.q = dot( ecPosition, eyePlaneQ );
} else {
return vec4(1.1,1.1,0.0,1.0); // outside, clamp to border
}
return coords;
}
void main() {
vec2 coords = gl_TexCoord[0].xy;
vec4 spec_emis = texture2D( spec_emis_tex, coords );
if ( spec_emis.a < 0.1 )
discard;
vec3 normal = normal_decode(texture2D( normal_tex, coords ).rg);
float len = length(normal);
normal /= len;
vec3 viewDir = normalize(ray);
vec3 pos = position( viewDir, texture2D( depth_tex, coords ).r );
vec4 tint;
float shadow;
if (filtering == 2) {
shadow += 0.333 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos, 1.0), tint ) ).r;
shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.003 * pos.z, -0.002 * pos.z, 0), 1.0), tint ) ).r;
shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3( 0.003 * pos.z, 0.002 * pos.z, 0), 1.0), tint ) ).r;
shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3(-0.003 * pos.z, 0.002 * pos.z, 0), 1.0), tint ) ).r;
shadow += 0.166 * shadow2DProj( shadow_tex, DynamicShadow( vec4(pos + vec3( 0.003 * pos.z, -0.002 * pos.z, 0), 1.0), tint ) ).r;
} else {
shadow = shadow2DProj( shadow_tex, DynamicShadow( vec4( pos, 1.0 ), tint ) ).r;
}
vec3 lightDir = (fg_ViewMatrix * vec4( fg_SunDirection, 0.0 )).xyz;
lightDir = normalize( lightDir );
vec3 color = texture2D( color_tex, coords ).rgb;
vec3 Idiff = clamp( dot( lightDir, normal ), 0.0, 1.0 ) * color * fg_SunDiffuseColor.rgb;
vec3 halfDir = lightDir - viewDir;
len = length( halfDir );
vec3 Ispec = vec3(0.0);
vec3 Iemis = spec_emis.z * color;
if (len > 0.0001) {
halfDir /= len;
Ispec = pow( clamp( dot( halfDir, normal ), 0.0, 1.0 ), spec_emis.y * 128.0 ) * spec_emis.x * fg_SunSpecularColor.rgb;
}
gl_FragColor = vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0);
// gl_FragColor = mix(tint, vec4(mix(vec3(0.0), Idiff + Ispec, shadow) + Iemis, 1.0), 0.92);
}

View file

@ -0,0 +1,19 @@
//uniform mat4 fg_ViewMatrixInverse;
uniform mat4 fg_ProjectionMatrixInverse;
varying vec3 ray;
varying vec4 eyePlaneS;
varying vec4 eyePlaneT;
varying vec4 eyePlaneR;
varying vec4 eyePlaneQ;
void main() {
gl_Position = gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
// ray = (fg_ViewMatrixInverse * vec4((fg_ProjectionMatrixInverse * gl_Vertex).xyz, 0.0)).xyz;
ray = (fg_ProjectionMatrixInverse * gl_Vertex).xyz;
eyePlaneS = gl_EyePlaneS[1];
eyePlaneT = gl_EyePlaneT[1];
eyePlaneR = gl_EyePlaneR[1];
eyePlaneQ = gl_EyePlaneQ[1];
}