Skip to content

Commit

Permalink
implmented simple TV bevel and screen shine effect
Browse files Browse the repository at this point in the history
  • Loading branch information
JetSetIlly committed Aug 28, 2022
1 parent 2577f3a commit aa13646
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
14 changes: 14 additions & 0 deletions gui/crt/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Preferences struct {

Curve prefs.Bool
RoundedCorners prefs.Bool
Bevel prefs.Bool
Shine prefs.Bool
Mask prefs.Bool
Scanlines prefs.Bool
Interference prefs.Bool
Expand Down Expand Up @@ -66,6 +68,8 @@ const (
enabled = true
curve = true
roundedCorners = false
bevel = false
shine = false
mask = true
scanlines = true
interference = true
Expand Down Expand Up @@ -120,6 +124,14 @@ func NewPreferences() (*Preferences, error) {
if err != nil {
return nil, err
}
err = p.dsk.Add("crt.bevel", &p.Bevel)
if err != nil {
return nil, err
}
err = p.dsk.Add("crt.shine", &p.Shine)
if err != nil {
return nil, err
}
err = p.dsk.Add("crt.mask", &p.Mask)
if err != nil {
return nil, err
Expand Down Expand Up @@ -234,6 +246,8 @@ func (p *Preferences) SetDefaults() {
p.Enabled.Set(enabled)
p.Curve.Set(curve)
p.RoundedCorners.Set(roundedCorners)
p.Bevel.Set(bevel)
p.Shine.Set(shine)
p.Mask.Set(mask)
p.Scanlines.Set(scanlines)
p.Interference.Set(interference)
Expand Down
6 changes: 6 additions & 0 deletions gui/sdlimgui/glsl_shaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ type effectsShader struct {
numClocks int32
curve int32
roundedCorners int32
bevel int32
shine int32
shadowMask int32
scanlines int32
interference int32
Expand Down Expand Up @@ -223,6 +225,8 @@ func newEffectsShader(img *SdlImgui, yflip bool) shaderProgram {
sh.numClocks = gl.GetUniformLocation(sh.handle, gl.Str("NumClocks"+"\x00"))
sh.curve = gl.GetUniformLocation(sh.handle, gl.Str("Curve"+"\x00"))
sh.roundedCorners = gl.GetUniformLocation(sh.handle, gl.Str("RoundedCorners"+"\x00"))
sh.bevel = gl.GetUniformLocation(sh.handle, gl.Str("Bevel"+"\x00"))
sh.shine = gl.GetUniformLocation(sh.handle, gl.Str("Shine"+"\x00"))
sh.shadowMask = gl.GetUniformLocation(sh.handle, gl.Str("ShadowMask"+"\x00"))
sh.scanlines = gl.GetUniformLocation(sh.handle, gl.Str("Scanlines"+"\x00"))
sh.interference = gl.GetUniformLocation(sh.handle, gl.Str("Interference"+"\x00"))
Expand Down Expand Up @@ -252,6 +256,8 @@ func (sh *effectsShader) setAttributesArgs(env shaderEnvironment, numScanlines i
gl.Uniform1i(sh.numClocks, int32(numClocks))
gl.Uniform1i(sh.curve, boolToInt32(sh.img.crtPrefs.Curve.Get().(bool)))
gl.Uniform1i(sh.roundedCorners, boolToInt32(sh.img.crtPrefs.RoundedCorners.Get().(bool)))
gl.Uniform1i(sh.bevel, boolToInt32(sh.img.crtPrefs.Bevel.Get().(bool)))
gl.Uniform1i(sh.shine, boolToInt32(sh.img.crtPrefs.Shine.Get().(bool)))
gl.Uniform1i(sh.shadowMask, boolToInt32(sh.img.crtPrefs.Mask.Get().(bool)))
gl.Uniform1i(sh.scanlines, boolToInt32(sh.img.crtPrefs.Scanlines.Get().(bool)))
gl.Uniform1i(sh.interference, boolToInt32(interference))
Expand Down
55 changes: 43 additions & 12 deletions gui/sdlimgui/shaders/crt_effects.frag
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ uniform int NumScanlines;
uniform int NumClocks;
uniform int Curve;
uniform int RoundedCorners;
uniform int Bevel;
uniform int Shine;
uniform int ShadowMask;
uniform int Scanlines;
uniform int Interference;
Expand Down Expand Up @@ -108,22 +110,19 @@ void main() {
uv = mix(curve(uv), uv, m);
}

// bevel (if in use) should bend the same as the screen
vec2 uv_bevel = uv;

// reduce size of main display if bevel is active
if (Bevel == 1) {
uv = (uv - 0.5) * 1.1 + 0.5;
}

// after this point every UV reference is to the curved UV

// basic color
Crt_Color = Frag_Color * texture(Texture, uv.st);

// rounded corners
if (RoundedCorners == 1) {
float margin = RoundedCornersAmount / 2.6666;
vec2 bl = smoothstep(vec2(-margin), vec2(RoundedCornersAmount), uv.st);
vec2 tr = smoothstep(vec2(-margin), vec2(RoundedCornersAmount), 1.0-uv.st);
float pct = bl.x * bl.y * tr.x * tr.y;
if (pct <= 0.2) {
Crt_Color *= vec4(pct);
}
}

// using y axis to determine scaling.
float scaling = float(ScreenDim.y) / float(NumScanlines);

Expand Down Expand Up @@ -212,10 +211,42 @@ void main() {
Crt_Color.b += texture(Texture, vec2(uv.x+(1.8*ab.x), uv.y+(1.8*ab.y))).b;
Crt_Color.rgb *= 0.50;

// shine affect
if (Shine == 1) {
vec2 uv_shine = (uv - 0.5) * 1.2 + 0.5;
float shine = (1.0-uv_shine.s)*(1.0-uv_shine.t);
Crt_Color = mix(Crt_Color, vec4(1.0), shine*0.05);
}

// vignette effect
if (Curve == 1) {
float vignette = 10*uv.x*uv.y*(1.0-uv.x)*(1.0-uv.y);
Crt_Color.rgb *= pow(vignette, 0.10) * 1.3;
Crt_Color.rgb *= pow(vignette, 0.12) * 1.1;
}

// rounded corners
if (RoundedCorners == 1) {
float margin = RoundedCornersAmount / 4.0;
vec2 bl = smoothstep(vec2(-margin), vec2(RoundedCornersAmount)*1.1, uv.st);
vec2 tr = smoothstep(vec2(-margin), vec2(RoundedCornersAmount)*1.1, 1.0-uv.st);
float pct = bl.x * bl.y * tr.x * tr.y;
if (pct < 0.1) {
Crt_Color = vec4(pct);
}
}

// bevel
if (Bevel == 1) {
float bevelAmount = 0.0167;
if (RoundedCorners == 1) {
bevelAmount = RoundedCornersAmount / 1.2;
}
vec2 bl = smoothstep(vec2(-bevelAmount), vec2(bevelAmount), uv_bevel.st);
vec2 tr = smoothstep(vec2(-bevelAmount), vec2(bevelAmount), 1.0-uv_bevel.st);
float pct = bl.x * bl.y * tr.x * tr.y;
if (pct < 0.9) {
Crt_Color = vec4(0.5, 0.5, 0.5, 1.0) * (1.0-pct);
}
}

// finalise color
Expand Down
16 changes: 13 additions & 3 deletions gui/sdlimgui/win_prefs_crt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
Expand Down Expand Up @@ -77,7 +76,7 @@ func (win *winPrefs) drawCRT() {
imgui.Spacing()
win.drawBlackLevel()
imgui.Spacing()
win.drawRoundedCorners()
win.drawRoundedCornersBevelAndShine()
imgui.PopItemWidth()

imgui.EndTable()
Expand Down Expand Up @@ -382,7 +381,7 @@ func (win *winPrefs) drawBlackLevel() {
}
}

func (win *winPrefs) drawRoundedCorners() {
func (win *winPrefs) drawRoundedCornersBevelAndShine() {
b := win.img.crtPrefs.RoundedCorners.Get().(bool)
if imgui.Checkbox("Rounded Corners##roundedcorners", &b) {
win.img.crtPrefs.RoundedCorners.Set(b)
Expand All @@ -405,6 +404,17 @@ func (win *winPrefs) drawRoundedCorners() {
if imgui.SliderFloatV("##roundedcornersamount", &f, 0.02, 0.09, label, 1.0) {
win.img.crtPrefs.RoundedCornersAmount.Set(f)
}

b = win.img.crtPrefs.Bevel.Get().(bool)
if imgui.Checkbox("Bevel##bevel", &b) {
win.img.crtPrefs.Bevel.Set(b)
}
imgui.SameLine()

b = win.img.crtPrefs.Shine.Get().(bool)
if imgui.Checkbox("Shine##shine", &b) {
win.img.crtPrefs.Shine.Set(b)
}
}

func (win *winPrefs) drawPixelPerfect() bool {
Expand Down

0 comments on commit aa13646

Please sign in to comment.