From 5d03142642897740b7cb5de23ce11085ddf59bb6 Mon Sep 17 00:00:00 2001 From: rexim Date: Wed, 27 Nov 2019 23:21:34 +0700 Subject: [PATCH 1/5] (#57) f -> flShadow --- src/boomer.nim | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index 40b824f..c8a1974 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -256,7 +256,7 @@ proc main() = camera = Camera(scale: 1.0) mouse: Mouse flashlight = false - f = 0.0 + flShadow = 0.0 flRadius = 200.0 while not quitting: @@ -323,14 +323,14 @@ proc main() = mouse.prev = mouse.curr mouse.drag = true - of Button4: + of Button4: # Scroll up if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: # TODO(#57): changing flashlight radius should be animated flRadius += 10.0 else: camera.deltaScale += config.scrollSpeed - of Button5: + of Button5: # Scoll down if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: flRadius = max(flRadius - 10.0, 0.0) else: @@ -353,13 +353,13 @@ proc main() = vec2(wa.width.float32, wa.height.float32)) if flashlight: - f = min(f + 6.0 * dt, 0.8) + flShadow = min(flShadow + 6.0 * dt, 0.8) else: - f = max(f - 6.0 * dt, 0.0) + flShadow = max(flShadow - 6.0 * dt, 0.0) screenshot.draw(camera, shaderProgram, vao, texture, vec2(wa.width.float32, wa.height.float32), - mouse, f, flRadius) + mouse, flShadow, flRadius) glXSwapBuffers(display, win) From ac541cd52fcaf17ee75b12fc3e3cb9a3c9fbc9e1 Mon Sep 17 00:00:00 2001 From: rexim Date: Wed, 27 Nov 2019 23:30:29 +0700 Subject: [PATCH 2/5] (#57) Don't pass camera to draw() by reference --- src/boomer.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/boomer.nim b/src/boomer.nim index c8a1974..8045e40 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -75,7 +75,7 @@ proc newShaderProgram(vertex, fragment: Shader): GLuint = glUseProgram(result) -proc draw(screenshot: Image, camera: var Camera, shader, vao, texture: GLuint, +proc draw(screenshot: Image, camera: Camera, shader, vao, texture: GLuint, 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) From a50f087b6760066dfdded3688c85e000c3235bb5 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 28 Nov 2019 00:23:24 +0700 Subject: [PATCH 3/5] (#57) Implement flashlight entity --- src/boomer.nim | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index 8045e40..df6234b 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -6,6 +6,7 @@ import config import x11/xlib, x11/x, x11/xutil, x11/keysym import opengl, opengl/glx +import math import la type Shader = tuple[path, content: string] @@ -75,8 +76,14 @@ proc newShaderProgram(vertex, fragment: Shader): GLuint = glUseProgram(result) +type Flashlight = object + isEnabled: bool + shadow: float32 + radius: float32 + deltaRadius: float32 + proc draw(screenshot: Image, camera: Camera, shader, vao, texture: GLuint, - windowSize: Vec2f, mouse: Mouse, flShadow: float32, flRadius: float32) = + windowSize: Vec2f, mouse: Mouse, flashlight: Flashlight) = glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) @@ -93,8 +100,8 @@ proc draw(screenshot: Image, camera: Camera, shader, vao, texture: GLuint, glUniform2f(glGetUniformLocation(shader, "cursorPos".cstring), mouse.curr.x.float32, mouse.curr.y.float32) - glUniform1f(glGetUniformLocation(shader, "flShadow".cstring), flShadow) - glUniform1f(glGetUniformLocation(shader, "flRadius".cstring), flRadius) + glUniform1f(glGetUniformLocation(shader, "flShadow".cstring), flashlight.shadow) + glUniform1f(glGetUniformLocation(shader, "flRadius".cstring), flashlight.radius) glBindVertexArray(vao) glDrawElements(GL_TRIANGLES, count = 6, GL_UNSIGNED_INT, indices = nil) @@ -255,9 +262,13 @@ proc main() = quitting = false camera = Camera(scale: 1.0) mouse: Mouse - flashlight = false - flShadow = 0.0 - flRadius = 200.0 + flashlight = Flashlight( + isEnabled: false, + radius: 200.0) + + const + INITIAL_FL_DELTA_RADIUS = 100.0 + FL_DELTA_RADIUS_DECELERATION = 400.0 while not quitting: var wa: TXWindowAttributes @@ -313,7 +324,7 @@ proc main() = echo "------------------------------" of XK_f: - flashlight = not flashlight + flashlight.isEnabled = not flashlight.isEnabled else: discard @@ -324,15 +335,14 @@ proc main() = mouse.drag = true of Button4: # Scroll up - if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: - # TODO(#57): changing flashlight radius should be animated - flRadius += 10.0 + if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight.isEnabled: + flashlight.deltaRadius += INITIAL_FL_DELTA_RADIUS else: camera.deltaScale += config.scrollSpeed of Button5: # Scoll down - if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight: - flRadius = max(flRadius - 10.0, 0.0) + if (xev.xkey.state and ControlMask) > 0.uint32 and flashlight.isEnabled: + flashlight.deltaRadius -= INITIAL_FL_DELTA_RADIUS else: camera.deltaScale -= config.scrollSpeed @@ -352,14 +362,18 @@ proc main() = camera.update(config, dt, mouse, screenshot, vec2(wa.width.float32, wa.height.float32)) - if flashlight: - flShadow = min(flShadow + 6.0 * dt, 0.8) + flashlight.radius = max(0.0, flashlight.radius + flashlight.deltaRadius * dt) + if abs(flashlight.deltaRadius) > 0.0: + flashlight.deltaRadius -= sgn(flashlight.deltaRadius).float32 * FL_DELTA_RADIUS_DECELERATION * dt + + if flashlight.isEnabled: + flashlight.shadow = min(flashlight.shadow + 6.0 * dt, 0.8) else: - flShadow = max(flShadow - 6.0 * dt, 0.0) + flashlight.shadow = max(flashlight.shadow - 6.0 * dt, 0.0) screenshot.draw(camera, shaderProgram, vao, texture, vec2(wa.width.float32, wa.height.float32), - mouse, flShadow, flRadius) + mouse, flashlight) glXSwapBuffers(display, win) From 33291f11dc4232d1dcada9a8e88c925f2d77bdc7 Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 28 Nov 2019 00:24:53 +0700 Subject: [PATCH 4/5] (#57) Add threshold for flashlight delta radius --- src/boomer.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/boomer.nim b/src/boomer.nim index df6234b..991f21f 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -363,7 +363,7 @@ proc main() = vec2(wa.width.float32, wa.height.float32)) flashlight.radius = max(0.0, flashlight.radius + flashlight.deltaRadius * dt) - if abs(flashlight.deltaRadius) > 0.0: + if abs(flashlight.deltaRadius) > 0.5: flashlight.deltaRadius -= sgn(flashlight.deltaRadius).float32 * FL_DELTA_RADIUS_DECELERATION * dt if flashlight.isEnabled: From dedcd5ec7b2cb9085ac51e550c0ea3157ddaf98c Mon Sep 17 00:00:00 2001 From: rexim Date: Thu, 28 Nov 2019 00:31:13 +0700 Subject: [PATCH 5/5] (#57) factor out flashlight.update operation --- src/boomer.nim | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/boomer.nim b/src/boomer.nim index 991f21f..335230a 100644 --- a/src/boomer.nim +++ b/src/boomer.nim @@ -82,6 +82,21 @@ type Flashlight = object radius: float32 deltaRadius: float32 +const + INITIAL_FL_DELTA_RADIUS = 100.0 + FL_DELTA_RADIUS_DECELERATION = 400.0 + +proc update(flashlight: var Flashlight, dt: float32) = + flashlight.radius = max(0.0, flashlight.radius + flashlight.deltaRadius * dt) + if abs(flashlight.deltaRadius) > 0.5: + flashlight.deltaRadius -= sgn(flashlight.deltaRadius).float32 * FL_DELTA_RADIUS_DECELERATION * dt + + if flashlight.isEnabled: + flashlight.shadow = min(flashlight.shadow + 6.0 * dt, 0.8) + else: + flashlight.shadow = max(flashlight.shadow - 6.0 * dt, 0.0) + + proc draw(screenshot: Image, camera: Camera, shader, vao, texture: GLuint, windowSize: Vec2f, mouse: Mouse, flashlight: Flashlight) = glClearColor(0.1, 0.1, 0.1, 1.0) @@ -266,10 +281,6 @@ proc main() = isEnabled: false, radius: 200.0) - const - INITIAL_FL_DELTA_RADIUS = 100.0 - FL_DELTA_RADIUS_DECELERATION = 400.0 - while not quitting: var wa: TXWindowAttributes discard XGetWindowAttributes(display, win, addr wa) @@ -361,15 +372,7 @@ proc main() = let dt = 1.0 / config.fps.float camera.update(config, dt, mouse, screenshot, vec2(wa.width.float32, wa.height.float32)) - - flashlight.radius = max(0.0, flashlight.radius + flashlight.deltaRadius * dt) - if abs(flashlight.deltaRadius) > 0.5: - flashlight.deltaRadius -= sgn(flashlight.deltaRadius).float32 * FL_DELTA_RADIUS_DECELERATION * dt - - if flashlight.isEnabled: - flashlight.shadow = min(flashlight.shadow + 6.0 * dt, 0.8) - else: - flashlight.shadow = max(flashlight.shadow - 6.0 * dt, 0.0) + flashlight.update(dt) screenshot.draw(camera, shaderProgram, vao, texture, vec2(wa.width.float32, wa.height.float32),