The "Deferred Rendering Shader"..
Code: Select all
// Written by Jacques Pretorius 2017.. "VECTROTEK"..
#version 120
uniform sampler2D Image01; // 24 Bit DEPTH (see "Packing / Encoding" versus "Un-Packing / Decoding")..
uniform sampler2D Image02; // Range Compressed NORMALS (to be Un-Range Compressed in here)..
// No Matrix-Transforms needed!
uniform vec3 CameraPosition;
uniform vec3 FarLeftUpXYZ; // Clockwise starting at Far Left Up..
uniform vec3 FarRightUpXYZ;
uniform vec3 FarRightDwnXYZ;
uniform vec3 FarLeftDwnXYZ;
// - - - - - - - - - - - - - - - - - - - - - - -
struct Light {vec3 Pos; vec3 Col;}; // Structure to hold our lights..
const int LIGHT_COUNT = 4; // See how "const" or "#define" must be used to size the array..
//(alas, App-fed value can't be used) (may-be later GLSL's)..
Light LightsArray [LIGHT_COUNT];
// Unpack Linearised Depth..
float UnPackDepth_002(vec3 RGBDepth) {return RGBDepth.x + (RGBDepth.y / 255.0) + (RGBDepth.z / 65535.0);}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void main()
{
vec2 UVCoordsXY = gl_TexCoord[0].xy;
vec3 Depth24BitPacked = texture2D(Image01, UVCoordsXY).xyz;
vec3 RCNormalIN = texture2D(Image02, UVCoordsXY).xyz;
vec3 RCNormalINWINDOW = texture2D(Image02, (UVCoordsXY.xy * vec2(4.0,4.0))).xyz;
vec3 Depth24BitPackedWINDOW = texture2D(Image01, (UVCoordsXY.xy * vec2(4.0,4.0)) - vec2(1.0, 0.0) ).xyz;
vec3 Depth24BitPackedWINDOWB = texture2D(Image01, (UVCoordsXY.xy * vec2(4.0,4.0)) - vec2(2.0, 0.0) ).xyz;
vec3 URCNormal = ((RCNormalIN.xyz - 0.5) * 2);
vec3 FragmentPosXYZ;
float DepthLinearised = UnPackDepth_002(Depth24BitPacked) ;
// When a name is Postfixed by "WINDOW" then it only means that it has to be read with "Special Fixed" UV Coords..
float DepthLinearisedWINDOW = UnPackDepth_002(Depth24BitPackedWINDOWB) ;
// Vectrotek brings you..
// * * * * * * * * * * * * * * * * * * * * * * *
FragmentPosXYZ.x = (CameraPosition.x + ((FarLeftDwnXYZ.x
+ ((FarLeftUpXYZ.x - FarLeftDwnXYZ.x) * UVCoordsXY.y)
+ (FarRightDwnXYZ.x - FarLeftDwnXYZ.x) * UVCoordsXY.x)
* DepthLinearised));
FragmentPosXYZ.y = (CameraPosition.y + ((FarLeftDwnXYZ.y
+ ((FarLeftUpXYZ.y - FarLeftDwnXYZ.y) * UVCoordsXY.y)
+ (FarRightDwnXYZ.y - FarLeftDwnXYZ.y) * UVCoordsXY.x)
* DepthLinearised));
FragmentPosXYZ.z = (CameraPosition.z + ((FarLeftDwnXYZ.z
+ ((FarLeftUpXYZ.z - FarLeftDwnXYZ.z) * UVCoordsXY.y)
+ (FarRightDwnXYZ.z - FarLeftDwnXYZ.z) * UVCoordsXY.x)
* DepthLinearised));
// * * * * * * * * * * * * * * * * * * * * * * *
// A Few Lights of which the first one is at CAMERA POSITION..
LightsArray [0].Col = vec3( 1.0 , 1.0 , 0.0);
LightsArray [1].Col = vec3( 1.0 , 0.5 , 0.0);
LightsArray [2].Col = vec3( 0.5 , 1.0 , 0.0);
LightsArray [3].Col = vec3( 0.0 , 0.5 , 1.0);
LightsArray [0].Pos = vec3( 999.0 , 999.0 , 999.0); // Camera, so overridden..
LightsArray [1].Pos = vec3( 1000.0 , 0.0 , 0.0);
LightsArray [2].Pos = vec3( 0.0 , 1000.0 , 0.0);
LightsArray [3].Pos = vec3( 0.0 , 0.0 , 1000.0);
// Just sothat we have at least one light at Cam pos..
LightsArray [0].Pos = CameraPosition.xyz;
vec3 diffuse;
vec3 specular;
//==================================================
for (int LI = 0; LI < LIGHT_COUNT - 0 ; LI++)
{// LOTS more can be done here, but this shows Basic Deferred Rendering at work..
// DIFFUSE..
diffuse += max(dot(URCNormal, normalize((LightsArray [LI].Pos.xyz) - FragmentPosXYZ)), 0.0) * LightsArray [LI].Col;
// SPECULAR.. (edited)..
Specular += LightsArray [LI].Col * (clamp(pow(clamp(dot(normalize((normalize((CameraPosition.xyz - FragmentPosXYZ))
+ normalize(LightsArray [LI].Pos - FragmentPosXYZ))),
URCNormal)
,0.0,1.0), 128.0), 0.0 , 1.0 ));
}
//==================================================
// Diffuse subdued a bit and Specular Boosted a bit..
// Could still Mix/Lerp Diffuse (Albedo) for "Ambient" Emulation..
gl_FragColor = vec4((diffuse * 0.35) + (specular * 3.0), 1.0);
// Little Windows in shader UV.. :)
if (UVCoordsXY.x < 0.25 && UVCoordsXY.y < 0.25)
{gl_FragColor = vec4(RCNormalINWINDOW,1.0);}
if (UVCoordsXY.x > 0.25 && UVCoordsXY.x < 0.5 && UVCoordsXY.y < 0.25)
{gl_FragColor = vec4(Depth24BitPackedWINDOW,1.0);}
if (UVCoordsXY.x > 0.5 && UVCoordsXY.x < 0.75 && UVCoordsXY.y < 0.25)
{gl_FragColor = vec4(DepthLinearisedWINDOW,DepthLinearisedWINDOW,DepthLinearisedWINDOW,1.0);}
if (UVCoordsXY.x > 0.75 && UVCoordsXY.x < 1.0 && UVCoordsXY.y < 0.25)
{gl_FragColor = vec4(FragmentPosXYZ * 0.01 ,1.0);}
}
// END..
Project (based on the previous) coming soon..
In fact, if you replace the relevant shader and feed the Plane and Camera Values from the previous app
it would work.. (I did do some cleanups though)
(comments welcome)
Cheers!