#version 330 core
#pragma debug(on)
#pragma optimize(off)
in vec3 Position;
in vec3 Normal;
in vec2 TexCoords;
uniform int RenderPass;
uniform sampler2D HDRTex;
uniform sampler2D FilterTex;
uniform float White;
uniform float Exposure;
uniform float AveLum;
uniform float LumThreshold;
struct LightInfo
{
vec4 Position; // Light position in eye coords.
vec3 Intensity; // Light intensity
};
uniform LightInfo Light;
struct MaterialInfo
{
vec3 Ka; // Ambient reflectivity
vec3 Kd; // Diffuse reflectivity
vec3 Ks; // Specular reflectivity
float Shininess; // Specular shininess factor
};
uniform MaterialInfo Material;
layout( location = 0 ) out vec4 FragColor;
float Weight[10] = float[] (0.084612906, 0.082937457, 0.078107551, 0.070674635, 0.061441574,
0.051320318, 0.041185521, 0.031756159, 0.023525544, 0.016744783);
float PixOffset[10] = float[](0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0);
mat3 rgb2xyz = mat3(
0.4124564, 0.2126729, 0.0193339,
0.3575761, 0.7151522, 0.1191920,
0.1804375, 0.0721750, 0.9503041 );
mat3 xyz2rgb = mat3(
3.2404542, -0.9692660, 0.0556434,
-1.5371385, 1.8760108, -0.2040259,
-0.4985314, 0.0415560, 1.0572252 );
float luminance( vec3 color )
{
return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
}
vec3 phongModel( )
{
vec3 n = normalize( Normal );
vec3 s = normalize( vec3( Light.Position) - Position );
vec3 v = normalize( vec3(-Position));
vec3 total = vec3(0.0f, 0.0f, 0.0f);
vec3 r = reflect( -s, n );
//for (int i = 0; i < 3; i++)
total += (Light.Intensity * ( Material.Ka + Material.Kd * max( dot( s, n), 0.0 ) +
Material.Ks * pow( max( dot( r, v), 0.0 ), Material.Shininess ) ));
return(total);
}
void main()
{
vec4 texColor;
vec4 texColor2;
vec2 tc = TexCoords;
vec4 sum;
tc.x *= -1.0;
tc.x += 1.0;
switch(RenderPass)
{
case 1:
FragColor = vec4(phongModel(), 1.0);
break;
case 2:
texColor = texture(HDRTex, tc);
if (luminance(texColor.rgb) > LumThreshold)
FragColor = texColor;
else
FragColor = vec4(0.0);
break;
case 3:
float dy = 1.0 / (textureSize(FilterTex, 0)).y;
sum = texture(FilterTex, tc) * Weight[0];
for( int i = 1; i < 10; i++ )
{
sum += texture( FilterTex, tc + vec2(0.0,PixOffset[i]) * dy ) * Weight[i];
sum += texture( FilterTex, tc - vec2(0.0,PixOffset[i]) * dy ) * Weight[i];
}
FragColor = sum;
break;
case 4:
float dx = 1.0 / (textureSize(FilterTex,0)).x;
sum = texture(FilterTex, tc) * Weight[0];
for( int i = 1; i < 10; i++ )
{
sum += texture( FilterTex, tc + vec2(PixOffset[i],0.0) * dx ) * Weight[i];
sum += texture( FilterTex, tc - vec2(PixOffset[i],0.0) * dx ) * Weight[i];
}
FragColor = sum ;
break;
case 5:
///////////////// Tone mapping ///////////////
//// Retrieve high-res color from texture
vec4 color = texture( HDRTex, tc );
vec4 blurTex = texture(FilterTex, tc);
// Convert to XYZ
vec3 xyzCol = rgb2xyz * vec3( color);
// Convert to xyY
float xyzSum = xyzCol.x + xyzCol.y + xyzCol.z;
vec3 xyYCol = vec3( 0.0);
if( xyzSum > 0.0 ) // Avoid divide by zero
xyYCol = vec3( xyzCol.x / xyzSum, xyzCol.y / xyzSum, xyzCol.y);
// Apply the tone mapping operation to the luminance
// (xyYCol.z or xyzCol.y)
float L = (Exposure * xyYCol.z) / AveLum;
L = (L * ( 1.0 + L / (White * White) )) / ( 1.0 + L );
// Using the new luminance, convert back to XYZ
if( xyYCol.y > 0.0 )
{
xyzCol.x = (L * xyYCol.x) / (xyYCol.y);
xyzCol.y = L;
xyzCol.z = (L * (1.0 - xyYCol.x - xyYCol.y))/ xyYCol.y;
}
////// Convert back to RGB
vec4 toneMapColor = vec4(( xyz2rgb * xyzCol), 1.0);
/////////////// Combine with blurred texture /////////////
//// We want linear filtering on this texture access so that
//// we get additional blurring.
FragColor = toneMapColor + blurTex;
break;
}
}