From cfe6c559a41ef52611abbd2a0ff510d3a21e8978 Mon Sep 17 00:00:00 2001
From: Filippo Scognamiglio <flscogna@gmail.com>
Date: Tue, 25 Mar 2014 16:30:07 +0100
Subject: [PATCH] Flickering is now much more realistic

---
 app/ShaderManager.qml            | 55 ++++++++++++--------------------
 app/ShaderSettings.qml           |  4 ++-
 app/app.qmlproject.user          |  2 +-
 app/frames/utils/FrameShader.qml |  2 +-
 4 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/app/ShaderManager.qml b/app/ShaderManager.qml
index 07aee34..94dfb12 100644
--- a/app/ShaderManager.qml
+++ b/app/ShaderManager.qml
@@ -30,56 +30,41 @@ ShaderEffect {
     property real noise_strength: shadersettings.noise_strength
     property real screen_distorsion: shadersettings.screen_distortion
     property real glowing_line_strength: shadersettings.glowing_line_strength
-    property real brightness: 1.0
 
     property real scanlines: shadersettings.scanlines ? 1.0 : 0.0
 
-    Behavior on brightness {
-        NumberAnimation{
-            duration: 250
-            onRunningChanged:
-                if(!running) shadercontainer.brightness = 1.0;
-        }
-    }
 
     Behavior on horizontal_distortion {
         NumberAnimation{
-            duration: 150
+            duration: 100
             onRunningChanged:
                 if(!running) shadercontainer.horizontal_distortion = 0.0;
         }
     }
 
-    Loader{
-        active: shadersettings.screen_flickering !== 0
-        sourceComponent: Timer{
-            property real randval
-            id: flickertimer
-            interval: 500
-            onTriggered: {
-                randval = Math.random();
-                if(randval < shadersettings.screen_flickering){
-                    shadercontainer.horizontal_distortion = Math.random() * shadersettings.screen_flickering;
-                }
-                randval = Math.random();
-                if(randval < shadersettings.screen_flickering)
-                    shadercontainer.brightness = Math.random() * 0.5 + 0.5;
-            }
 
-            repeat: true
-            running: true
-        }
-    }
+    //Manage brightness the function might be improved
+    property real screen_flickering: shadersettings.screen_flickering
+    property real _A: 0.5 + Math.random() * 0.2
+    property real _B: 0.3 + Math.random() * 0.2
+    property real _C: 1.2 - _A - _B
+    property real a: (0.2 + Math.random() * 0.2) * 0.05
+    property real b: (0.4 + Math.random() * 0.2) * 0.05
+    property real c: (0.7 + Math.random() * 0.2) * 0.05
+    property real brightness: screen_flickering * (
+                                  _A * Math.cos(a * time) +
+                                  _B * Math.sin(b * time) +
+                                  _C * Math.cos(c * time))
+
 
     property real deltay: 3 / terminal.height
     property real deltax: 3 / terminal.width
     property real horizontal_distortion: 0.0
-    //property real faulty_screen_prob: shadersettings.faulty_screen_prob
 
     NumberAnimation on time{
         from: -1
-        to: 100
-        duration: 5000
+        to: 10000
+        duration: 10000
 
         loops: Animation.Infinite
     }
@@ -99,7 +84,7 @@ ShaderEffect {
             (noise_strength !== 0 ? "uniform highp float noise_strength;" : "") +
             (screen_distorsion !== 0 ? "uniform highp float screen_distorsion;" : "")+
             (glowing_line_strength !== 0 ? "uniform highp float glowing_line_strength;" : "")+
-            "uniform highp float brightness;" +
+            "uniform lowp float brightness;" +
 
             (scanlines != 0 ? "uniform highp float scanlines;" : "") +
 
@@ -127,7 +112,7 @@ ShaderEffect {
 
             (glowing_line_strength !== 0 ?
             "float randomPass(vec2 coords){
-                return fract(smoothstep(-0.2, 0.0, coords.y - time * 0.03)) * glowing_line_strength;
+                return fract(smoothstep(-0.2, 0.0, coords.y - time * 0.0007)) * glowing_line_strength;
             }" : "") +
 
 
@@ -152,8 +137,8 @@ ShaderEffect {
                 "vec3 finalColor = mix(background_color, font_color, color).rgb;
                 finalColor = mix(finalColor, vec3(0.0), scanline_alpha);" +
 
-                (brightness !== 1.0 ?
-                "finalColor = finalColor * brightness;" : "") +
+                (screen_flickering !== 0 ?
+                "finalColor = mix(finalColor, vec3(0.0), brightness);" : "") +
 
                 "gl_FragColor = vec4(finalColor, 1.0);
             }"
diff --git a/app/ShaderSettings.qml b/app/ShaderSettings.qml
index e06317f..6179dd9 100644
--- a/app/ShaderSettings.qml
+++ b/app/ShaderSettings.qml
@@ -28,13 +28,15 @@ Item{
     property string background_color: "#002200"
     property string font_color: "#00ff00"
 
-    property real screen_flickering: 0.05
     property real noise_strength: 0.1
     property real screen_distortion: 0.15
     property real glowing_line_strength: 0.4
     property real motion_blur: 0.65
     property real bloom_strength: 0.8
 
+    property real horizontal_sinc: 0.2
+    property real screen_flickering: 0.12
+
     property bool scanlines: false
 
     property string frame_source: frames_list.get(frames_index).source
diff --git a/app/app.qmlproject.user b/app/app.qmlproject.user
index 8c3dae5..f048009 100644
--- a/app/app.qmlproject.user
+++ b/app/app.qmlproject.user
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE QtCreatorProject>
-<!-- Written by QtCreator 3.0.1, 2014-03-24T21:35:08. -->
+<!-- Written by QtCreator 3.0.1, 2014-03-25T13:54:12. -->
 <qtcreator>
  <data>
   <variable>ProjectExplorer.Project.ActiveTarget</variable>
diff --git a/app/frames/utils/FrameShader.qml b/app/frames/utils/FrameShader.qml
index 1fa58e7..a24e75c 100644
--- a/app/frames/utils/FrameShader.qml
+++ b/app/frames/utils/FrameShader.qml
@@ -38,7 +38,7 @@ ShaderEffect{
                                 vec4 txt_normal = texture2D(normals, coords);
                                 vec3 normal = normalize(txt_normal.rgb * 2.0 - 1.0);
                                 vec3 light_dir = normalize(vec3(0.5,0.5, 0.0) - vec3(qt_TexCoord0, 0.0));
-                                float reflection = dot(normal, light_dir) * 0.4 * brightness + 0.2;
+                                float reflection = (dot(normal, light_dir) * 0.4 + 0.2) * (1.0-brightness);
                                 vec3 final_color = reflection_color * reflection * 0.5;
                                 final_color += txt_color * ambient_light;
                                 gl_FragColor = vec4(final_color * txt_normal.a, txt_color.a);