From 467c23b77170e2c3f99315c71cc6673d5509a38c Mon Sep 17 00:00:00 2001 From: rexim Date: Wed, 20 Nov 2019 23:55:07 +0700 Subject: [PATCH 1/8] (#55) Initial flashlight effect implementation --- src/frag.glsl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/frag.glsl b/src/frag.glsl index eb4647a..af830dc 100644 --- a/src/frag.glsl +++ b/src/frag.glsl @@ -2,7 +2,18 @@ out mediump vec4 color; in mediump vec2 texcoord; uniform sampler2D tex; +uniform vec2 cursorPos; +uniform vec2 windowSize; + +const float FLASHLIGHT_RADIUS = 200.0; + void main() { - color = texture(tex, texcoord); + vec4 cursor = vec4(cursorPos.x, windowSize.y - cursorPos.y, 0.0, 1.0); + float f = 0.8; + if (length(cursor - gl_FragCoord) < FLASHLIGHT_RADIUS) { + color = texture(tex, texcoord); + } else { + color = texture(tex, texcoord) - vec4(f, f, f, 0.0); + } } From 68b0f544141f0217cb9293951221450dc563f3c3 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:03:28 +0700 Subject: [PATCH 2/8] (#55) Make the flashlight effect togglable --- src/boomer.nim | 9 +++++++-- src/frag.glsl | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index 1cc4503..8f68f73 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -78,7 +78,7 @@ proc newShaderProgram(vertex, fragment: Shader): GLuint = glUseProgram(result) proc draw(screenshot: Image, camera: var Camera, shader, vao, texture: GLuint, - windowSize: Vec2f, mouse: Mouse) = + windowSize: Vec2f, mouse: Mouse, flShadow: float32) = glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) @@ -95,6 +95,7 @@ proc draw(screenshot: Image, camera: var Camera, shader, vao, texture: GLuint, glUniform2f(glGetUniformLocation(shader, "cursorPos".cstring), mouse.curr.x.float32, mouse.curr.y.float32) + glUniform1f(glGetUniformLocation(shader, "flShadow".cstring), flShadow) glBindVertexArray(vao) glDrawElements(GL_TRIANGLES, count = 6, GL_UNSIGNED_INT, indices = nil) @@ -268,6 +269,7 @@ proc main() = quitting = false camera = Camera(scale: 1.0) mouse: Mouse + flashlight = false while not quitting: var wa: TXWindowAttributes @@ -320,6 +322,9 @@ proc main() = shaderProgram = newShaderProgram(vertexShader, fragmentShader) echo "Shader program ID: ", shaderProgram echo "------------------------------" + + of XK_f: + flashlight = not flashlight else: discard @@ -351,7 +356,7 @@ proc main() = screenshot.draw(camera, shaderProgram, vao, texture, vec2(wa.width.float32, wa.height.float32), - mouse) + mouse, if flashlight: 0.8 else: 0.0) glXSwapBuffers(display, win) diff --git a/src/frag.glsl b/src/frag.glsl index af830dc..dc27938 100644 --- a/src/frag.glsl +++ b/src/frag.glsl @@ -4,16 +4,16 @@ in mediump vec2 texcoord; uniform sampler2D tex; uniform vec2 cursorPos; uniform vec2 windowSize; +uniform float flShadow; const float FLASHLIGHT_RADIUS = 200.0; void main() { vec4 cursor = vec4(cursorPos.x, windowSize.y - cursorPos.y, 0.0, 1.0); - float f = 0.8; if (length(cursor - gl_FragCoord) < FLASHLIGHT_RADIUS) { color = texture(tex, texcoord); } else { - color = texture(tex, texcoord) - vec4(f, f, f, 0.0); + color = texture(tex, texcoord) - vec4(flShadow, flShadow, flShadow, 0.0); } } From 0395345a036c4bf14536e86fe8bbd2f393910358 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:10:33 +0700 Subject: [PATCH 3/8] (#55) Animate flashlight effect --- src/boomer.nim | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index 8f68f73..b18e74f 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -270,6 +270,7 @@ proc main() = camera = Camera(scale: 1.0) mouse: Mouse flashlight = false + f = 0.0 while not quitting: var wa: TXWindowAttributes @@ -352,11 +353,17 @@ proc main() = else: discard - camera.update(config, 1.0 / config.fps.float, mouse, screenshot) + let dt = 1.0 / config.fps.float + camera.update(config, dt, mouse, screenshot) + + if flashlight: + f = min(f + 6.0 * dt, 0.8) + else: + f = max(f - 6.0 * dt, 0.0) screenshot.draw(camera, shaderProgram, vao, texture, vec2(wa.width.float32, wa.height.float32), - mouse, if flashlight: 0.8 else: 0.0) + mouse, f) glXSwapBuffers(display, win) From 3648c27f41c89cf7192f67724826411a55b9e75b Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:18:41 +0700 Subject: [PATCH 4/8] (#55) Use mix for flashlight effect --- src/frag.glsl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/frag.glsl b/src/frag.glsl index dc27938..2ad4524 100644 --- a/src/frag.glsl +++ b/src/frag.glsl @@ -11,9 +11,7 @@ const float FLASHLIGHT_RADIUS = 200.0; void main() { vec4 cursor = vec4(cursorPos.x, windowSize.y - cursorPos.y, 0.0, 1.0); - if (length(cursor - gl_FragCoord) < FLASHLIGHT_RADIUS) { - color = texture(tex, texcoord); - } else { - color = texture(tex, texcoord) - vec4(flShadow, flShadow, flShadow, 0.0); - } + color = mix( + texture(tex, texcoord), vec4(0.0, 0.0, 0.0, 0.0), + length(cursor - gl_FragCoord) < FLASHLIGHT_RADIUS ? 0.0 : flShadow); } From a0cd10aea3e86e3bec81d477e11f9d2f766cee96 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:25:18 +0700 Subject: [PATCH 5/8] (#55) Customizable flashlight radius --- src/boomer.nim | 16 ++++++++++++---- src/frag.glsl | 5 ++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index b18e74f..c490822 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -78,7 +78,7 @@ proc newShaderProgram(vertex, fragment: Shader): GLuint = glUseProgram(result) proc draw(screenshot: Image, camera: var Camera, shader, vao, texture: GLuint, - windowSize: Vec2f, mouse: Mouse, flShadow: float32) = + windowSize: Vec2f, mouse: Mouse, flShadow: float32, flRadius: float32) = glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) @@ -96,6 +96,7 @@ proc draw(screenshot: Image, camera: var Camera, shader, vao, texture: GLuint, mouse.curr.x.float32, mouse.curr.y.float32) glUniform1f(glGetUniformLocation(shader, "flShadow".cstring), flShadow) + glUniform1f(glGetUniformLocation(shader, "flRadius".cstring), flRadius) glBindVertexArray(vao) glDrawElements(GL_TRIANGLES, count = 6, GL_UNSIGNED_INT, indices = nil) @@ -271,6 +272,7 @@ proc main() = mouse: Mouse flashlight = false f = 0.0 + flRadius = 200.0 while not quitting: var wa: TXWindowAttributes @@ -336,10 +338,16 @@ proc main() = mouse.drag = true of Button4: - camera.deltaScale += config.scrollSpeed + if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: + flRadius += 10.0 + else: + camera.deltaScale += config.scrollSpeed of Button5: - camera.deltaScale -= config.scrollSpeed + if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: + flRadius = max(flRadius - 10.0, 0.0) + else: + camera.deltaScale -= config.scrollSpeed else: discard @@ -363,7 +371,7 @@ proc main() = screenshot.draw(camera, shaderProgram, vao, texture, vec2(wa.width.float32, wa.height.float32), - mouse, f) + mouse, f, flRadius) glXSwapBuffers(display, win) diff --git a/src/frag.glsl b/src/frag.glsl index 2ad4524..68c6e5a 100644 --- a/src/frag.glsl +++ b/src/frag.glsl @@ -5,13 +5,12 @@ uniform sampler2D tex; uniform vec2 cursorPos; uniform vec2 windowSize; uniform float flShadow; - -const float FLASHLIGHT_RADIUS = 200.0; +uniform float flRadius; void main() { vec4 cursor = vec4(cursorPos.x, windowSize.y - cursorPos.y, 0.0, 1.0); color = mix( texture(tex, texcoord), vec4(0.0, 0.0, 0.0, 0.0), - length(cursor - gl_FragCoord) < FLASHLIGHT_RADIUS ? 0.0 : flShadow); + length(cursor - gl_FragCoord) < flRadius ? 0.0 : flShadow); } From 81834893fc150a38f15119ba9ca1b854411957ad Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:29:21 +0700 Subject: [PATCH 6/8] (#55) make flashlight radius scale according to cameraScale --- src/frag.glsl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/frag.glsl b/src/frag.glsl index 68c6e5a..7d79073 100644 --- a/src/frag.glsl +++ b/src/frag.glsl @@ -6,11 +6,12 @@ uniform vec2 cursorPos; uniform vec2 windowSize; uniform float flShadow; uniform float flRadius; +uniform float cameraScale; void main() { vec4 cursor = vec4(cursorPos.x, windowSize.y - cursorPos.y, 0.0, 1.0); color = mix( texture(tex, texcoord), vec4(0.0, 0.0, 0.0, 0.0), - length(cursor - gl_FragCoord) < flRadius ? 0.0 : flShadow); + length(cursor - gl_FragCoord) < (flRadius * cameraScale) ? 0.0 : flShadow); } From 6bd2edd5f258d3b8b96fdfad63c404ce4ccd2aef Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:34:55 +0700 Subject: [PATCH 7/8] (#55) rest of the work --- src/boomer.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/boomer.nim b/src/boomer.nim index c490822..97b7d53 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -339,6 +339,7 @@ proc main() = of Button4: if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: + # TODO: changing flashlight radius should be animated flRadius += 10.0 else: camera.deltaScale += config.scrollSpeed From 812604e5e2edcaf3dcac873f6891e7b88b516d31 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 21 Nov 2019 00:37:02 +0700 Subject: [PATCH 8/8] Add TODO(#57) --- src/boomer.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/boomer.nim b/src/boomer.nim index 97b7d53..df78073 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -339,7 +339,7 @@ proc main() = of Button4: if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: - # TODO: changing flashlight radius should be animated + # TODO(#57): changing flashlight radius should be animated flRadius += 10.0 else: camera.deltaScale += config.scrollSpeed