The DEFERRED RENDERER reads different buffers rendered to the screen quad as follows..
(the usual screen quad Vertex Program)
The Deferred Renderer "Fragment Shader":
(This outputs LIT RAW DIFFUSE or/and SPECULAR mixed in by inter buffer render elsewhere)
Code: Select all
// DEFERRED RENDERER..
// 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 NearLeftUpXYZ; // Clockwise starting at Far Left Up..
// uniform vec3 NearRightUpXYZ; // Not used..
uniform vec3 NearLeftDwnXYZ;
uniform vec3 NearRightDwnXYZ;
uniform vec3 FarLeftUpXYZ; // Clockwise starting at Far Left Up..
// uniform vec3 FarRightUpXYZ; // Not used..
uniform vec3 FarLeftDwnXYZ;
uniform vec3 FarRightDwnXYZ;
// - - - - - - - - - - - - - - - - - - - - - - -
struct Light {vec3 Pos; vec3 Col;}; // Structure to hold our lights..
const int LIGHT_COUNT = 6; // 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];
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;
vec4 Depth32BitPacked = texture2D(Image01, UVCoordsXY).xyzw;
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 = normalize((RCNormalIN.xyz - 0.5) * 2);
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..
// * * * * * * * * * * * * * * * * * * * * * * *
vec3 FragmentPosXYZ;
FragmentPosXYZ.x = (CameraPosition.x + ((((FarLeftDwnXYZ.x + ((FarLeftUpXYZ.x - FarLeftDwnXYZ.x) * UVCoordsXY.y)
+ (FarRightDwnXYZ.x - FarLeftDwnXYZ.x) * UVCoordsXY.x))
- // Minus the Near Plane Corners..
((NearLeftDwnXYZ.x + ((NearLeftUpXYZ.x - NearLeftDwnXYZ.x) * UVCoordsXY.y)
+ (NearRightDwnXYZ.x - NearLeftDwnXYZ.x) * UVCoordsXY.x))) * DepthLinearised))
// Plus (Near Minus Camera)..
+ ((((NearLeftDwnXYZ.x + ((NearLeftUpXYZ.x - NearLeftDwnXYZ.x) * UVCoordsXY.y)
+ (NearRightDwnXYZ.x - NearLeftDwnXYZ.x) * UVCoordsXY.x))) - CameraPosition.x );
FragmentPosXYZ.y = (CameraPosition.y + (((FarLeftDwnXYZ.y + ((FarLeftUpXYZ.y - FarLeftDwnXYZ.y) * UVCoordsXY.y)
+ (FarRightDwnXYZ.y - FarLeftDwnXYZ.y) * UVCoordsXY.x)
- // Minus the Near Plane Corners..
((NearLeftDwnXYZ.y + ((NearLeftUpXYZ.y - NearLeftDwnXYZ.y) * UVCoordsXY.y)
+ (NearRightDwnXYZ.y - NearLeftDwnXYZ.y) * UVCoordsXY.x))) * DepthLinearised))
// Plus (Near Minus Camera)..
+ ((((NearLeftDwnXYZ.y + ((NearLeftUpXYZ.y - NearLeftDwnXYZ.y) * UVCoordsXY.y)
+ (NearRightDwnXYZ.y - NearLeftDwnXYZ.y) * UVCoordsXY.x))) - CameraPosition.y);
FragmentPosXYZ.z = (CameraPosition.z + (((FarLeftDwnXYZ.z + ((FarLeftUpXYZ.z - FarLeftDwnXYZ.z) * UVCoordsXY.y)
+ (FarRightDwnXYZ.z - FarLeftDwnXYZ.z) * UVCoordsXY.x)
- // Minus the Near Plane Corners..
((NearLeftDwnXYZ.z + ((NearLeftUpXYZ.z - NearLeftDwnXYZ.z) * UVCoordsXY.y)
+ (NearRightDwnXYZ.z - NearLeftDwnXYZ.z) * UVCoordsXY.x))) * DepthLinearised))
// Plus (Near Minus Camera)..
+ ((((NearLeftDwnXYZ.z + ((NearLeftUpXYZ.z - NearLeftDwnXYZ.z) * UVCoordsXY.y)
+ (NearRightDwnXYZ.z - NearLeftDwnXYZ.z) * UVCoordsXY.x))) - CameraPosition.z );
float Scaler = 100.0;
// A Few Lights of which the first one is at CAMERA POSITION..
// /*
LightsArray [0].Col = vec3( 1.0 , 1.0 , 0.0); // Yellow..
LightsArray [5].Col = vec3( 1.0 , 0.5 , 0.0); // Orange..
LightsArray [4].Col = vec3( 1.0 , 0.0 , 0.0); // Red..
LightsArray [3].Col = vec3( 0.8 , 0.0 , 0.8); // Purple..
LightsArray [2].Col = vec3( 0.0 , 0.4 , 1.0); // Blue..
LightsArray [1].Col = vec3( 0.0 , 1.0 , 0.0); // Green..
// */
/*
LightsArray [0].Col = vec3( 0.5 , 0.5 , 0.5);
LightsArray [1].Col = vec3( 0.5 , 0.5 , 0.5);
LightsArray [2].Col = vec3( 0.5 , 0.5 , 0.5);
LightsArray [3].Col = vec3( 0.5 , 0.5 , 0.5);
LightsArray [4].Col = vec3( 0.5 , 0.5 , 0.5);
LightsArray [5].Col = vec3( 0.5 , 0.5 , 0.5);
// */
LightsArray [0].Pos = vec3( -0.00186 , 6.88994 , 1.6034) * Scaler; // Camera, so overridden..
LightsArray [1].Pos = vec3( 1.94612 , 5.55791 , -0.19679) * Scaler;
LightsArray [2].Pos = vec3( 1.97002 , 3.9126 , 1.75955) * Scaler;
LightsArray [3].Pos = vec3( -0.00186 , 2.24885 , 2.05329) * Scaler;
LightsArray [4].Pos = vec3( -1.876110 , 3.9126 , 1.75955) * Scaler;
LightsArray [5].Pos = vec3( -2.00107 , 5.55791 , -0.19679) * Scaler;
// 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;
// DO THIS HERE OR DO THIS IN ANOThER PASS FOR ANOTHER BUFFER..
// SPECULAR..
//Specular += LightsArray [LI].Col * (clamp(pow(clamp(dot(normalize((normalize((CameraPosition.xyz - FragmentPosXYZ))
// + normalize(LightsArray [LI].Pos - FragmentPosXYZ))),
// URCNormal)
// ,0.0,1.0), 512.0), 0.0 , 1.0 ));
}
// YOU CAN DELETE THIS BUT SHOWS USEFULL LITTLE WINDOWS..
if (UVCoordsXY.y > 0.25)
{
gl_FragColor = vec4(Diffuse * 0.8, 1.0);
}
}
// END..