From 19ee71ed283c6c7711678ba3a935969cca254ec8 Mon Sep 17 00:00:00 2001 From: RelativisticMechanic Date: Thu, 27 Jul 2023 19:59:37 +0530 Subject: [PATCH] Add the frame shader from cool-retro-term --- src/CRTerm.h | 2 +- src/Console.cpp | 4 +- src/resources/config/default_cmd.json | 2 +- src/resources/config/default_wsl.json | 2 +- src/resources/config/wsl_warp.json | 2 +- src/resources/default | 2 +- src/resources/imgui.ini | 4 +- src/resources/shaders/crt.fs.glsl | 77 +++++++++++++++++++++++---- 8 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/CRTerm.h b/src/CRTerm.h index 471af05..8819f23 100644 --- a/src/CRTerm.h +++ b/src/CRTerm.h @@ -1,7 +1,7 @@ #ifndef CRTERM_H #define CRTERM_H -#define CRTERM_VERSION_STRING "CRTerm 0.2.0" +#define CRTERM_VERSION_STRING "CRTerm 0.2.5" #define CRTERM_CREDIT_STRING "(C) Siddharth Gautam, 2023\nThis software comes with NO WARRANTY.\n" #endif \ No newline at end of file diff --git a/src/Console.cpp b/src/Console.cpp index 2696631..d46f935 100644 --- a/src/Console.cpp +++ b/src/Console.cpp @@ -393,7 +393,9 @@ void Console::Render(GPU_Target* t, int xloc, int yloc, float scale) if (this->show_cursor && this->start_line == this->last_line) { GPU_SetUniformfv(GPU_GetUniformLocation(this->text_shader_id, "text_color"), 3, 1, this->color_scheme[this->default_fore_color].returnArray()); - GPU_Blit(this->char_blocks[219], NULL, this->render_buffer->target, this->cursor_x * this->font_w + font_w / 2, this->cursor_y * this->font_h + font_h / 2); + /* Cursor should be partially see-through */ + GPU_SetUniformf(GPU_GetUniformLocation(this->text_shader_id, "alpha"), 0.85); + GPU_RectangleFilled(this->render_buffer->target, cursor_x * font_w, cursor_y * font_h, (cursor_x + 1) * font_w, (cursor_y + 1) * font_h, SDL_Color{ 255, 255, 255, 255 }); /* Draw cursor shadow */ if (this->cursor_shadow_width > 0.0) diff --git a/src/resources/config/default_cmd.json b/src/resources/config/default_cmd.json index e95e14a..a968e96 100644 --- a/src/resources/config/default_cmd.json +++ b/src/resources/config/default_cmd.json @@ -8,7 +8,7 @@ "background": "crt.png", "text_shader": "shaders/text", "crt_shader": "shaders/crt", - "crt_warp": 0.0, + "crt_warp": 0.85, "shell_command": "cmd /K \"cd %userprofile%\"", "blink_interval": 300, "default_fg": 2, diff --git a/src/resources/config/default_wsl.json b/src/resources/config/default_wsl.json index e01c6bb..47e037f 100644 --- a/src/resources/config/default_wsl.json +++ b/src/resources/config/default_wsl.json @@ -8,7 +8,7 @@ "background": "crt.png", "text_shader": "shaders/text", "crt_shader": "shaders/crt", - "crt_warp": 0.0, + "crt_warp": 0.85, "shell_command": "wsl.exe ~", "blink_interval": 300, "default_fg": 6, diff --git a/src/resources/config/wsl_warp.json b/src/resources/config/wsl_warp.json index eb3e672..fb621d1 100644 --- a/src/resources/config/wsl_warp.json +++ b/src/resources/config/wsl_warp.json @@ -12,7 +12,7 @@ "shell_command": "wsl.exe ~", "blink_interval": 300, "default_fg": 6, - "default_bg": 6, + "default_bg": 15, "maxlines": 1000, "color_scheme": diff --git a/src/resources/default b/src/resources/default index f3ba6f3..e897746 100644 --- a/src/resources/default +++ b/src/resources/default @@ -1 +1 @@ -config\wsl_warp.json \ No newline at end of file +config\default_cmd.json \ No newline at end of file diff --git a/src/resources/imgui.ini b/src/resources/imgui.ini index 9f71cc1..ea4e0b0 100644 --- a/src/resources/imgui.ini +++ b/src/resources/imgui.ini @@ -45,7 +45,7 @@ Collapsed=0 [Window][##title] Pos=32,0 -Size=1456,32 +Size=1168,32 Collapsed=0 [Window][##XButton] @@ -54,7 +54,7 @@ Size=64,32 Collapsed=0 [Window][##Window_Buttons] -Pos=1360,0 +Pos=1072,0 Size=96,32 Collapsed=0 diff --git a/src/resources/shaders/crt.fs.glsl b/src/resources/shaders/crt.fs.glsl index f40b0d5..408ca92 100644 --- a/src/resources/shaders/crt.fs.glsl +++ b/src/resources/shaders/crt.fs.glsl @@ -27,6 +27,13 @@ float scan = 0.75; float scanline_speed = 0.5; float scanline_intensity = 0.15; float scanline_spread = 0.2; +float vigenette_intensity = 0.25; + +// CRT Frame Settings +float frameShadowCoeff = 15.0; +float screenShadowCoeff = 15.0; +vec2 margin = vec2(0.03, 0.03); + /* The CRT glowing text effect, downsample, then upscale to cause a glowy blur */ vec4 crtGlow(in vec2 uv) @@ -79,35 +86,85 @@ vec3 vigenette(in vec2 uv, in vec3 oricol) uv *= 1.0 - uv.yx; float vig = uv.x*uv.y * 15.0; - vig = pow(vig, 0.20); + vig = pow(vig, vigenette_intensity); return vig * oricol; } +/* + The following code was borrowed from Cool-Retro-Term. + It creates a nice frame around the terminal screen. +*/ + +float max2(vec2 v) +{ + return max(v.x, v.y); +} + +float min2(vec2 v) +{ + return min(v.x, v.y); +} + +float prod2(vec2 v) +{ + return v.x * v.y; +} + +float sum2(vec2 v) +{ + return v.x + v.y; +} + +vec2 positiveLog(vec2 x) +{ + return clamp(log(x), vec2(0.0), vec2(100.0)); +} + +vec4 crtFrame(in vec2 staticCoords, in vec2 uv) +{ + vec2 coords = uv * (vec2(1.0) + margin * 2.0) - margin; + + vec2 vignetteCoords = staticCoords * (1.0 - staticCoords.yx); + float vignette = pow(prod2(vignetteCoords) * 15.0, 0.25); + + vec3 color = frame_color.rgb * vec3(1.0 - vignette); + float alpha = 0.0; + + float frameShadow = max2(positiveLog(-coords * frameShadowCoeff + vec2(1.0)) + positiveLog(coords * frameShadowCoeff - (vec2(frameShadowCoeff) - vec2(1.0)))); + frameShadow = max(sqrt(frameShadow), 0.0); + color *= frameShadow; + alpha = sum2(1.0 - step(vec2(0.0), coords) + step(vec2(1.0), coords)); + alpha = clamp(alpha, 0.0, 1.0); + alpha *= mix(1.0, 0.9, frameShadow); + + float screenShadow = 1.0 - prod2(positiveLog(coords * screenShadowCoeff + vec2(1.0)) * positiveLog(-coords * screenShadowCoeff + vec2(screenShadowCoeff + 1.0))); + alpha = max(0.8 * screenShadow, alpha); + return vec4(color*alpha, alpha); +} + +/* End of cool-retro-term code */ + void main(void) { - // squared distance from center + /* Turn texCoord into distorted CRT coordinates */ vec2 uv = texCoord; vec2 dc = abs(0.5-uv); dc *= dc; - // warp the fragment coordinates uv.x -= 0.5; uv.x *= 1.0 + (dc.y * (0.3 * warp)); uv.x += 0.5; uv.y -= 0.5; uv.y *= 1.0 + (dc.x * (0.3 * warp)); uv.y += 0.5; - // sample inside boundaries, otherwise set to transparent - float distance_from_center = length(uv - vec2(0.5, 0.5)); - - if (uv.y > 1.0 || uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0) + if ((uv.y > 1.0 || uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0) && warp > 0.0) { - fragColor = frame_color; + /* If we are out of bounds, draw the frame */ + fragColor = crtFrame(texCoord, uv); } else { float apply = abs(sin(texCoord.y)*0.5*scan); fragColor = vec4(mix(crtGlow(uv).rgb, vec3(0.0),apply),1.0); fragColor = vec4(mix(fragColor.rgb, length(texture(crt_background, uv).rgb)*back_color, background_brightness),1.0); - // Add a scanline going up and down + /* Add a scanline going up and down */ fragColor.rgb += scanline_intensity * exp(-1.0*abs((1/scanline_spread) * sin((uv.y - abs(cos(scanline_speed*time)))))) * back_color; - // Add noise fragColor.rgb = mix(fragColor.rgb, vec3(crtNoise(uv, time)), crt_noise_fraction); fragColor.rgb = vigenette(uv, fragColor.rgb); }