Skip to content

Commit

Permalink
(tsoding#55) Make the flashlight effect togglable
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Nov 20, 2019
1 parent 467c23b commit 68b0f54
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/boomer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -268,6 +269,7 @@ proc main() =
quitting = false
camera = Camera(scale: 1.0)
mouse: Mouse
flashlight = false

while not quitting:
var wa: TXWindowAttributes
Expand Down Expand Up @@ -320,6 +322,9 @@ proc main() =
shaderProgram = newShaderProgram(vertexShader, fragmentShader)
echo "Shader program ID: ", shaderProgram
echo "------------------------------"

of XK_f:
flashlight = not flashlight
else:
discard

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions src/frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 68b0f54

Please sign in to comment.