Advanced Effects

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

[DEPRECATED] Ignore..

Here is the important shader code..

Code: Select all

 
 // I'm sure the "Extracted Linearized Depth" is Correct..
 float UnPackDepth_002(vec3 RGBDepth) 
  {return RGBDepth.x + (RGBDepth.y / 255.0) + (RGBDepth.z / 65535.0);
 
It is this piece I'm worried about..

Code: Select all

 
   vec3 CamRelFarLeftDwnXYZ;
   vec3 CamRelFarRightUpXYZ;
   CamRelFarLeftDwnXYZ.xyz = CameraPosition.xyz - NDCFarLeftDwnXYZ.xyz;
   CamRelFarRightUpXYZ.xyz = CameraPosition.xyz - NDCFarRightUpXYZ.xyz;
   vec3 DeltaOpCorners;
   DeltaOpCorners.xyz = CamRelFarRightUpXYZ.xyz - CamRelFarLeftDwnXYZ.xyz;
   vec3 FragmentPosXYZ;
   float DepthLinearised = UnPackDepth_002(Depth24BitPacked) ;
 
   // Here there are + and - issues..
   // But I dont think I'm doing NDC correctly..
   FragmentPosXYZ.x = -(CameraPosition.x + ((CamRelFarLeftDwnXYZ.x + (DeltaOpCorners.x * gl_TexCoord[0].x)) * DepthLinearised));
   FragmentPosXYZ.y =  (CameraPosition.y + ((CamRelFarLeftDwnXYZ.y + (DeltaOpCorners.y * gl_TexCoord[0].y)) * DepthLinearised));
   FragmentPosXYZ.z = -(CameraPosition.z + ((CamRelFarLeftDwnXYZ.z + (DeltaOpCorners.z * gl_TexCoord[0].x)) * DepthLinearised));
   
   // All World Space..
 
Aaaargh! :cry:
Last edited by Vectrotek on Wed Feb 01, 2017 5:29 pm, edited 3 times in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

[DEPRECATED]

Messy but there..

O.K. here is the whole PP Deferred Rendering Shader..

It is fed..

a. 24 Bit Depth packed RGB..
b. Range Compressed Normals..
c. FarPlaneLeftDownXYZ..
d. FarPlaneRightUpXYZ..
e. Camera PositionXYZ..

So close yet so far..

Code: Select all

 
 
 #version 120
 
 
 uniform sampler2D Image01;
 uniform sampler2D Image02;
 
 uniform vec3  CameraPosition;
 uniform vec3  NDCFarLeftDwnXYZ;
 uniform vec3  NDCFarRightUpXYZ;
 
 struct Light   {vec3 Pos; vec3 Col;}; // Structur to hold our lights..
 const int LIGHT_COUNT = 4;
 Light LightsArray [LIGHT_COUNT];
 
 // NEW..
  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 FragPosRCIN     = texture2D(Image01, UVCoordsXY).xyz; 
   vec3 RCNormalIN = texture2D(Image02, UVCoordsXY).xyz;
   vec3 Depth24BitPacked = texture2D(Image01, UVCoordsXY).xyz;
   vec3 URCNormal = ((RCNormalIN.xyz - 0.5) * 2);
 
   // - -  
   vec3 CamRelFarLeftDwnXYZ;
   vec3 CamRelFarRightUpXYZ;
   CamRelFarLeftDwnXYZ.xyz = CameraPosition.xyz - NDCFarLeftDwnXYZ.xyz;
   CamRelFarRightUpXYZ.xyz = CameraPosition.xyz - NDCFarRightUpXYZ.xyz;
   vec3 DeltaOpCorners;
   DeltaOpCorners.xyz = CamRelFarRightUpXYZ.xyz - CamRelFarLeftDwnXYZ.xyz;
   vec3 FragmentPosXYZ;
   float DepthLinearised = UnPackDepth_002(Depth24BitPacked) ;
   FragmentPosXYZ.x = -(CameraPosition.x + ((CamRelFarLeftDwnXYZ.x + (DeltaOpCorners.x * gl_TexCoord[0].x)) * DepthLinearised));
   FragmentPosXYZ.y = (CameraPosition.y + ((CamRelFarLeftDwnXYZ.y + (DeltaOpCorners.y * gl_TexCoord[0].y)) * DepthLinearised));
   FragmentPosXYZ.z = -(CameraPosition.z + ((CamRelFarLeftDwnXYZ.z + (DeltaOpCorners.z * gl_TexCoord[0].x)) * DepthLinearised));
 
   // - - - not used now.. WE HAVE CAMERA LIGHT..
   LightsArray [0].Col = vec3(1.0  ,  1.0   ,0.0);
   LightsArray [1].Col = vec3(0.0  ,  1.0   ,0.0);
   LightsArray [2].Col = vec3(0.0  ,  0.0   ,0.0);
   LightsArray [3].Col = vec3(0.0 ,   0.0   ,0.0);
 
   LightsArray [0].Pos = vec3( 10.0,  5.0,   0.0);
   LightsArray [1].Pos = vec3( 0.0,   15.0,  0.0);
   LightsArray [2].Pos = vec3( 0.0,   100.0, 0.0);
   LightsArray [3].Pos = vec3( 100.0,  0.0,  0.0);
 
   vec3 JJJCameraPosition = vec3(CameraPosition.xyz);
 
   vec3 diffuse;
   vec3 specular;
 
   diffuse += max(dot(URCNormal, normalize((JJJCameraPosition.xyz) - FragmentPosXYZ)), 0.0) * LightsArray [0].Col;
   // CAMERA SPECULAR LIGHT????  
   specular += LightsArray [0].Col
      * (clamp(pow(clamp(dot(normalize((normalize((JJJCameraPosition.xyz) - (FragmentPosXYZ * 1))
      + (JJJCameraPosition.xyz) - (FragmentPosXYZ * 1))), URCNormal),0.0,1.0),  77.77 ), 0.0 , 1.0 ) ) ; 
 
   gl_FragColor = vec4((diffuse * 0.5) + specular, 1.0); 
    
   // Little windows in shader UV..
   if (gl_TexCoord[0].x < 0.25 && gl_TexCoord[0].y < 0.25)
    {gl_FragColor = vec4(RCNormalIN,1.0);}
   if (gl_TexCoord[0].x > 0.25 && gl_TexCoord[0].x < 0.5 && gl_TexCoord[0].y < 0.25)
    {gl_FragColor = vec4(Depth24BitPacked,1.0);}
   if (gl_TexCoord[0].x > 0.5 && gl_TexCoord[0].x < 0.75 && gl_TexCoord[0].y < 0.25)
    {gl_FragColor = vec4(DepthLinearised,DepthLinearised,DepthLinearised,1.0);}
 
 
  }  
 
  
  
  
  
 
 
Last edited by Vectrotek on Wed Feb 01, 2017 5:27 pm, edited 3 times in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

[DEPRECATED] Ignore..

Multiplying "Left Down" and "Right Up" Far Plane Corners with "VIEW MATRIX" made it so that Specular Highlights don't move
when Camera is Aimed (not moved) which is better, but the Rays between Camera and FragPos seems wrong
(in a NON Perspective way)..

Well, were working on it..
(there might be something wrong with the FragPos from Depth Calculations too)

I also found no real helpfull stuff on NDC!!! (not one single solitary working executable glsl based project on the net)
NDC.xy = W H A T ?

(moermeter nou ver in die rooi)

Image
Last edited by Vectrotek on Wed Feb 01, 2017 5:30 pm, edited 1 time in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

By some incredible coincidence the mock-up checks were correct but the formula was TOTALLY wrong!
Now working on a new one..
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

[SOLVED]

(without Camera Position, Near Plane and Depth considerations which would be added later)

Image

Code: Select all

 
vec3 GetFragPosAtUV (vec2 UV, vec3 FarLeftDown, vec3 FarLeftUp, vec3 FarRightUp, vec3 FarRightDown)
 {vec3 UltimateFragPos;
 
  // The Challenge..
 
  UltimateFragPos.x = ???
  UltimateFragPos.y = ???
  UltimateFragPos.z = ???
 
  return UltimateFragPos;
 }
 
There must be a way..
Last edited by Vectrotek on Wed Feb 01, 2017 5:30 pm, edited 1 time in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

Got it!! (actually quite simple)
Last edited by Vectrotek on Tue Jan 31, 2017 10:46 pm, edited 2 times in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

Here is an unrelated but interesting piece..
http://stackoverflow.com/questions/3464 ... oss-produc
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

[SOLVED] Deferred Rendering in WORLD SPACE basis..
Image Image
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

How I finally got to a Working Formula..
A Test Case Mockup..
Image
Last edited by Vectrotek on Wed Feb 01, 2017 5:21 pm, edited 1 time in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

How I tested the "Actual Working Formula"..
Image
Last edited by Vectrotek on Wed Feb 01, 2017 6:03 pm, edited 1 time in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

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!
Last edited by Vectrotek on Sat Feb 04, 2017 8:32 pm, edited 2 times in total.
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

Now it's possible..

Image
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

Normal Maps, Tangent & Binormals, Screen Space Reflection, SSAO, Gloss Maps
and Diffuse / Albedo soon to be added..
Image
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

Got rid of "Specular Artifacts" by Normalizing the Un-Range Compressed Normal..

Changed..

Code: Select all

vec3 URCNormal = (RCNormalIN.xyz - 0.5) * 2
into..

Code: Select all

vec3 URCNormal = normalize((RCNormalIN.xyz - 0.5) * 2)
Looks much better..

Image
Vectrotek
Competition winner
Posts: 1087
Joined: Sat May 02, 2015 5:05 pm

Re: Advanced Effects

Post by Vectrotek »

So we have a Buffer containing the result of the "Lights Coloured Specular and Raw Grey Diffuse Deferred Renderer" which is
then "Added" to another buffer holding the assigned model "Albedo / Diffuse" materials and "SSAO" applied to it..
This buffer then had "FXAA" applied to it and fed on to the "Final Render Buffer".

The Deferred Shader was fed only two buffers, namely the "24 Bit RGB Encoded Depth in View Space Z"
and the "Range Compressed Normals" buffer in addition to "Camera World Position XYZ" and the
"Four Far Plane Corners XYZ's"..

Image
Post Reply