Skip to content

Commit

Permalink
Add the frame shader from cool-retro-term
Browse files Browse the repository at this point in the history
  • Loading branch information
RelativisticMechanic committed Jul 27, 2023
1 parent a208487 commit 19ee71e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/CRTerm.h
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion src/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/resources/config/default_cmd.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/resources/config/default_wsl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/resources/config/wsl_warp.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"shell_command": "wsl.exe ~",
"blink_interval": 300,
"default_fg": 6,
"default_bg": 6,
"default_bg": 15,
"maxlines": 1000,

"color_scheme":
Expand Down
2 changes: 1 addition & 1 deletion src/resources/default
Original file line number Diff line number Diff line change
@@ -1 +1 @@
config\wsl_warp.json
config\default_cmd.json
4 changes: 2 additions & 2 deletions src/resources/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Collapsed=0

[Window][##title]
Pos=32,0
Size=1456,32
Size=1168,32
Collapsed=0

[Window][##XButton]
Expand All @@ -54,7 +54,7 @@ Size=64,32
Collapsed=0

[Window][##Window_Buttons]
Pos=1360,0
Pos=1072,0
Size=96,32
Collapsed=0

77 changes: 67 additions & 10 deletions src/resources/shaders/crt.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 19ee71e

Please sign in to comment.