Merge pull request #52 from tsoding/48

(#48) Introduce shader recompilation mechanism
This commit is contained in:
Alexey Kutepov 2019-11-14 02:35:20 +07:00 committed by GitHub
commit fbb242988a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View File

@ -8,7 +8,7 @@ import x11/xlib, x11/x, x11/xutil, x11/keysym
import opengl, opengl/glx
import la
type Shader = tuple[path, content: string]
type Shader = tuple[path, content: string, baked: bool]
proc readShader(file: string): Shader =
result.path = file
@ -17,12 +17,24 @@ proc readShader(file: string): Shader =
slurp file
else:
readFile file
result.baked = block:
when nimvm:
true
else:
false
const
isDebug = not (defined(danger) or defined(release))
defaultVertexShader = readShader "vert.glsl"
defaultFragmentShader = readShader "frag.glsl"
proc reloadShader(shader: var Shader) =
if not shader.baked:
shader.content = readFile shader.path
else:
when isDebug:
shader.content = readFile ("src" / shader.path)
proc newShader(shader: Shader, kind: GLenum): GLuint =
result = glCreateShader(kind)
var shaderArray = allocCStringArray([shader.content])
@ -297,6 +309,17 @@ proc main() =
of XK_r:
if configFile.len > 0 and existsFile(configFile):
config = loadConfig(configFile)
if (xev.xkey.state and ControlMask) > 0.uint32:
# TODO(#53): Custom shader file reading and compilation errors crash the whole application
echo "------------------------------"
echo "RELOADING SHADERS"
glDeleteProgram(shaderProgram)
reloadShader(vertexShader)
reloadShader(fragmentShader)
shaderProgram = newShaderProgram(vertexShader, fragmentShader)
echo "Shader program ID: ", shaderProgram
echo "------------------------------"
else:
discard

View File

@ -10,7 +10,7 @@ type Config* = object
const defaultConfig* = Config(
scrollSpeed: 1.0,
dragVelocityFactor: 10.0,
# TODO: For a natual feel dragFriction probably should depend on velocity
# TODO(#54): For a natual feel dragFriction probably should depend on velocity
dragFriction: 600.0,
scaleFriction: 10.0,
fps: 60

View File

@ -3,6 +3,8 @@ import config
import la
import image
const VELOCITY_THRESHOLD = 10.0
type Mouse* = object
curr*: Vec2f
prev*: Vec2f
@ -23,6 +25,6 @@ proc update*(camera: var Camera, config: Config, dt: float, mouse: Mouse, image:
camera.scale = max(camera.scale + camera.delta_scale * dt, 1.0)
camera.delta_scale -= sgn(camera.delta_scale).float * config.scale_friction * dt
if not mouse.drag and (camera.velocity.length > 0.01):
if not mouse.drag and (camera.velocity.length > VELOCITY_THRESHOLD):
camera.position += camera.velocity * dt
camera.velocity -= camera.velocity.normalize * (config.dragFriction * dt)