;; focal depth (i.e. focal point) in meters
;; focal length in mm
;; fstop (camera)
;; show_focus draws orange colour at focus depth
(bind-func xtm_dof_fx
(let ((idmat:float* (alloc 16)))
(fill_identity_matrix idmat)
(lambda (fbo:FBO* autofocus focusx focusy fdepth flength fstop show_focus model view projection)
(let ((shader xtm_render_dof_shader)
(focuspt:float* (salloc 2)))
(fill_identity_matrix idmat)
(glDisable GL_DEPTH_TEST)
(start shader)
(shader_update_matrices shader model view projection)
(bind (depth_tex fbo) 0)
(glUniform1i (glGetUniformLocation (id shader) "bgl_DepthTexture") 0)
(bind (colour_tex fbo) 2)
(glUniform1i (glGetUniformLocation (id shader) "bgl_RenderedTexture") 2)
(glUniform1f (glGetUniformLocation (id shader) "znear") xtm_render_near)
(glUniform1f (glGetUniformLocation (id shader) "zfar") xtm_render_far)
(glUniform1f (glGetUniformLocation (id shader) "focalDepth") fdepth)
(glUniform1f (glGetUniformLocation (id shader) "focalLength") flength)
(glUniform1f (glGetUniformLocation (id shader) "fstop") fstop)
(glUniform1i (glGetUniformLocation (id shader) "showFocus") show_focus)
(glUniform1i (glGetUniformLocation (id shader) "autofocus") autofocus)
(glUniform1f (glGetUniformLocation (id shader) "bgl_RenderedTextureWidth") (i32tof xtm_render_w))
(glUniform1f (glGetUniformLocation (id shader) "bgl_RenderedTextureHeight") (i32tof xtm_render_h))
(pfill! focuspt focusx focusy)
(glUniform2fv (glGetUniformLocation (id shader) "focus") 1 focuspt)
(draw_quad shader idmat)
(unbind (colour_tex fbo) 0)
(unbind (depth_tex fbo) 2)
(stop shader)
void))))