1
0
Fork 0

Supporting effects and shaders for lights

This commit is contained in:
Frederic Bouvier 2012-03-31 20:32:50 +02:00
parent 503fa5ece2
commit dc3ee9ad6e
5 changed files with 358 additions and 0 deletions

102
Effects/light-point.eff Normal file
View file

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/light-point</name>
<parameters></parameters>
<technique n="10">
<pass>
<depth>
<enabled type="bool">false</enabled>
<write-mask type="bool">false</write-mask>
</depth>
<cull-face>front</cull-face>
<render-bin>
<bin-number>2</bin-number>
<bin-name>RenderBin</bin-name>
</render-bin>
<blend>
<source>one</source>
<destination>one</destination>
</blend>
<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>
<program>
<vertex-shader>Shaders/light-spot.vert</vertex-shader>
<fragment-shader>Shaders/light-point.frag</fragment-shader>
<attribute>
<name>attenuation</name>
<index>12</index>
</attribute>
</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>LightPosition</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/position</use></value>
<positioned type="bool">true</positioned>
</uniform>
<uniform>
<name>Ambient</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/ambient</use></value>
</uniform>
<uniform>
<name>Diffuse</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/diffuse</use></value>
</uniform>
<uniform>
<name>Specular</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/specular</use></value>
</uniform>
<uniform>
<name>Attenuation</name>
<type>float-vec3</type>
<value type="vec3d"><use>light-spot/attenuation</use></value>
</uniform>
<uniform>
<name>Near</name>
<type>float</type>
<value type="float"><use>light-spot/near</use></value>
</uniform>
<uniform>
<name>Far</name>
<type>float</type>
<value type="float"><use>light-spot/far</use></value>
</uniform>
</pass>
</technique>
</PropertyList>

123
Effects/light-spot.eff Normal file
View file

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/light-spot</name>
<parameters></parameters>
<technique n="10">
<pass>
<depth>
<enabled type="bool">false</enabled>
<write-mask type="bool">false</write-mask>
</depth>
<cull-face>front</cull-face>
<render-bin>
<bin-number>2</bin-number>
<bin-name>RenderBin</bin-name>
</render-bin>
<blend>
<source>one</source>
<destination>one</destination>
</blend>
<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>
<program>
<vertex-shader>Shaders/light-spot.vert</vertex-shader>
<fragment-shader>Shaders/light-spot.frag</fragment-shader>
<attribute>
<name>attenuation</name>
<index>12</index>
</attribute>
</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>LightPosition</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/position</use></value>
<positioned type="bool">true</positioned>
</uniform>
<uniform>
<name>LightDirection</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/direction</use></value>
<positioned type="bool">true</positioned>
</uniform>
<uniform>
<name>Ambient</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/ambient</use></value>
</uniform>
<uniform>
<name>Diffuse</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/diffuse</use></value>
</uniform>
<uniform>
<name>Specular</name>
<type>float-vec4</type>
<value type="vec4d"><use>light-spot/specular</use></value>
</uniform>
<uniform>
<name>Attenuation</name>
<type>float-vec3</type>
<value type="vec3d"><use>light-spot/attenuation</use></value>
</uniform>
<uniform>
<name>Exponent</name>
<type>float</type>
<value type="float"><use>light-spot/exponent</use></value>
</uniform>
<uniform>
<name>Cutoff</name>
<type>float</type>
<value type="float"><use>light-spot/cutoff</use></value>
</uniform>
<uniform>
<name>CosCutoff</name>
<type>float</type>
<value type="float"><use>light-spot/cosCutoff</use></value>
</uniform>
<uniform>
<name>Near</name>
<type>float</type>
<value type="float"><use>light-spot/near</use></value>
</uniform>
<uniform>
<name>Far</name>
<type>float</type>
<value type="float"><use>light-spot/far</use></value>
</uniform>
</pass>
</technique>
</PropertyList>

57
Shaders/light-point.frag Normal file
View file

@ -0,0 +1,57 @@
uniform vec2 fg_BufferSize;
uniform vec3 fg_Planes;
uniform sampler2D depth_tex;
uniform sampler2D normal_tex;
uniform sampler2D color_tex;
uniform sampler2D spec_emis_tex;
uniform vec4 LightPosition;
uniform vec4 Ambient;
uniform vec4 Diffuse;
uniform vec4 Specular;
uniform vec3 Attenuation;
uniform float Near;
uniform float Far;
varying vec4 ecPosition;
void main() {
vec3 ray = ecPosition.xyz / ecPosition.w;
vec3 ecPos3 = ray;
vec3 viewDir = normalize(ray);
vec2 coords = gl_FragCoord.xy / fg_BufferSize;
float depth = texture2D( depth_tex, coords ).r;
vec3 normal;
normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
normal.z = sqrt( 1 - dot( normal.xy, normal.xy ) );
vec4 spec_emis = texture2D( spec_emis_tex, coords );
vec3 pos;
pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
pos.xy = viewDir.xy * pos.z / viewDir.z;
if ( pos.z < ecPos3.z ) // Negative direction in z
discard; // Don't light surface outside the light volume
vec3 VP = LightPosition.xyz - pos;
if ( dot( VP, VP ) > ( Far * Far ) )
discard; // Don't light surface outside the light volume
float d = length( VP );
VP /= d;
vec3 halfVector = normalize(VP - viewDir);
float att = 1.0 / (Attenuation.x + Attenuation.y * d + Attenuation.z *d*d);
float nDotVP = max(0.0, dot(normal, VP));
float nDotHV = max(0.0, dot(normal, halfVector));
vec4 color = texture2D( color_tex, coords );
vec4 Iamb = Ambient * color * att;
vec4 Idiff = Diffuse * color * att * nDotVP;
vec3 Ispec = pow( nDotHV, spec_emis.y ) * spec_emis.x * att * Specular.rgb;
gl_FragColor = vec4(Iamb.rgb + Idiff.rgb + Ispec, 1.0);
}

70
Shaders/light-spot.frag Normal file
View file

@ -0,0 +1,70 @@
uniform vec2 fg_BufferSize;
uniform vec3 fg_Planes;
uniform sampler2D depth_tex;
uniform sampler2D normal_tex;
uniform sampler2D color_tex;
uniform sampler2D spec_emis_tex;
uniform vec4 LightPosition;
uniform vec4 LightDirection;
uniform vec4 Ambient;
uniform vec4 Diffuse;
uniform vec4 Specular;
uniform vec3 Attenuation;
uniform float Exponent;
uniform float Cutoff;
uniform float CosCutoff;
uniform float Near;
uniform float Far;
varying vec4 ecPosition;
void main() {
vec3 ray = ecPosition.xyz / ecPosition.w;
vec3 ecPos3 = ray;
vec3 viewDir = normalize(ray);
vec2 coords = gl_FragCoord.xy / fg_BufferSize;
float depth = texture2D( depth_tex, coords ).r;
vec3 normal;
normal.xy = texture2D( normal_tex, coords ).rg * 2.0 - vec2(1.0,1.0);
normal.z = sqrt( 1 - dot( normal.xy, normal.xy ) );
vec4 spec_emis = texture2D( spec_emis_tex, coords );
vec3 pos;
pos.z = - fg_Planes.y / (fg_Planes.x + depth * fg_Planes.z);
pos.xy = viewDir.xy * pos.z / viewDir.z;
if ( pos.z < ecPos3.z ) // Negative direction in z
discard; // Don't light surface outside the light volume
vec3 VP = LightPosition.xyz - pos;
if ( dot( VP, VP ) > ( Far * Far ) )
discard; // Don't light surface outside the light volume
float d = length( VP );
VP /= d;
vec3 halfVector = normalize(VP - viewDir);
float att = 1.0 / (Attenuation.x + Attenuation.y * d + Attenuation.z *d*d);
float spotDot = dot(-VP, normalize(LightDirection.xyz));
float spotAttenuation = 0.0;
if (spotDot < CosCutoff)
spotAttenuation = 0.0;
else
spotAttenuation = pow(spotDot, Exponent);
att *= spotAttenuation;
float nDotVP = max(0.0, dot(normal, VP));
float nDotHV = max(0.0, dot(normal, halfVector));
vec4 color = texture2D( color_tex, coords );
vec4 Iamb = Ambient * color * att;
vec4 Idiff = Diffuse * color * att * nDotVP;
vec3 Ispec = pow( nDotHV, spec_emis.y ) * spec_emis.x * att * Specular.rgb;
vec3 Ispec = 0.0;
gl_FragColor = vec4(Iamb.rgb + Idiff.rgb + Ispec, 1.0);
}

6
Shaders/light-spot.vert Normal file
View file

@ -0,0 +1,6 @@
varying vec4 ecPosition;
void main() {
ecPosition = gl_ModelViewMatrix * gl_Vertex;
gl_Position = ftransform();
}