diff options
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | chickadee/render.scm | 10 | ||||
-rw-r--r-- | chickadee/render/blend.scm | 8 | ||||
-rw-r--r-- | chickadee/render/depth.scm | 61 | ||||
-rw-r--r-- | chickadee/render/gl.scm | 10 | ||||
-rw-r--r-- | chickadee/render/gpu.scm | 4 | ||||
-rw-r--r-- | chickadee/render/model.scm | 3 |
7 files changed, 82 insertions, 15 deletions
diff --git a/Makefile.am b/Makefile.am index ce67cd0..d8e0314 100644 --- a/Makefile.am +++ b/Makefile.am @@ -64,6 +64,7 @@ SOURCES = \ chickadee/render/gl.scm \ chickadee/render/gpu.scm \ chickadee/render/blend.scm \ + chickadee/render/depth.scm \ chickadee/render/texture.scm \ chickadee/render/shader.scm \ chickadee/render/buffer.scm \ diff --git a/chickadee/render.scm b/chickadee/render.scm index 00e484c..195529d 100644 --- a/chickadee/render.scm +++ b/chickadee/render.scm @@ -50,13 +50,13 @@ gpu-apply/instanced)) (define-record-type <render-context> - (make-render-context viewport framebuffer blend-mode depth-test? - projection textures) + (make-render-context viewport framebuffer blend-mode depth-test + projection textures) render-context? (viewport render-context-viewport set-render-context-viewport!) (framebuffer render-context-framebuffer set-render-context-framebuffer!) (blend-mode render-context-blend-mode set-render-context-blend-mode!) - (depth-test? render-context-depth-test? set-render-context-depth-test!) + (depth-test render-context-depth-test set-render-context-depth-test!) (projection render-context-projection set-render-context-projection!) (textures render-context-textures)) @@ -78,7 +78,7 @@ (render-context-blend-mode render-context)) (define (current-depth-test) - (render-context-depth-test? render-context)) + (render-context-depth-test render-context)) (define (current-texture i) (vector-ref (render-context-textures render-context) i)) @@ -117,7 +117,7 @@ body ...)) (define-syntax-rule (with-depth-test depth-test body ...) - (with (render-context-depth-test? set-render-context-depth-test! depth-test) + (with (render-context-depth-test set-render-context-depth-test! depth-test) body ...)) (define-syntax-rule (with-texture n texture body ...) diff --git a/chickadee/render/blend.scm b/chickadee/render/blend.scm index e5f3ffc..1c0b215 100644 --- a/chickadee/render/blend.scm +++ b/chickadee/render/blend.scm @@ -20,8 +20,7 @@ #:use-module (gl) #:use-module (chickadee render gl) #:use-module (chickadee render gpu) - #:export (apply-blend-mode - apply-depth-test)) + #:export (apply-blend-mode)) (define (apply-blend-mode blend-mode) (if blend-mode @@ -62,8 +61,3 @@ (gl-blend-func (blending-factor-src one) (blending-factor-dest zero))))) (gl-disable (enable-cap blend)))) - -(define (apply-depth-test depth-test?) - (if depth-test? - (gl-enable (enable-cap depth-test)) - (gl-disable (enable-cap depth-test)))) diff --git a/chickadee/render/depth.scm b/chickadee/render/depth.scm new file mode 100644 index 0000000..2eb4e55 --- /dev/null +++ b/chickadee/render/depth.scm @@ -0,0 +1,61 @@ +;;; Chickadee Game Toolkit +;;; Copyright © 2020 David Thompson <davet@gnu.org> +;;; +;;; Chickadee is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published +;;; by the Free Software Foundation, either version 3 of the License, +;;; or (at your option) any later version. +;;; +;;; Chickadee is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see +;;; <http://www.gnu.org/licenses/>. + +(define-module (chickadee render depth) + #:use-module (ice-9 match) + #:use-module (gl) + #:use-module (chickadee render gl) + #:use-module (chickadee render gpu) + #:use-module (srfi srfi-9) + #:export (make-depth-test + depth-test? + depth-test-write? + depth-test-function + depth-test-near + depth-test-far + default-depth-test + apply-depth-test)) + +(define-record-type <depth-test> + (%make-depth-test write? function near far) + depth-test? + (write? depth-test-write?) + (function depth-test-function) + (near depth-test-near) + (far depth-test-far)) + +(define* (make-depth-test #:key (write? #t) (function 'less-than) (near 0.0) (far 1.0)) + (%make-depth-test write? function near far)) + +(define default-depth-test (make-depth-test)) + +(define (apply-depth-test depth-test) + (if depth-test + (let ((glfunc (match (depth-test-function depth-test) + ('always (depth-function always)) + ('never (depth-function never)) + ('equal (depth-function equal)) + ('not-equal (depth-function notequal)) + ('less-than (depth-function less)) + ('less-than-or-equal (depth-function lequal)) + ('greater-than (depth-function greater)) + ('greater-than-or-equal (depth-function gequal))))) + (gl-enable (enable-cap depth-test)) + (gl-depth-func glfunc) + (gl-depth-mask (depth-test-write? depth-test)) + (gl-depth-range (depth-test-near depth-test) (depth-test-far depth-test))) + (gl-disable (enable-cap depth-test)))) diff --git a/chickadee/render/gl.scm b/chickadee/render/gl.scm index 295a7bb..67d8ef1 100644 --- a/chickadee/render/gl.scm +++ b/chickadee/render/gl.scm @@ -306,3 +306,13 @@ object.") ;;; (re-export (%glGetString . gl-get-string)) + + +;;; +;;; Depth Buffer +;;; + +(re-export (%glDepthFunc . gl-depth-func) + (%glDepthMask . gl-depth-mask) + (%glDepthRange . gl-depth-range)) + diff --git a/chickadee/render/gpu.scm b/chickadee/render/gpu.scm index 554e263..3974c68 100644 --- a/chickadee/render/gpu.scm +++ b/chickadee/render/gpu.scm @@ -127,6 +127,7 @@ from the GPU's memory." (let ((textures (make-vector 32)) ;; Lazily resolve bindings to avoid circular dependencies. (blend-module (resolve-interface '(chickadee render blend))) + (depth-module (resolve-interface '(chickadee render depth))) (buffer-module (resolve-interface '(chickadee render buffer))) (framebuffer-module (resolve-interface '(chickadee render framebuffer))) (shader-module (resolve-interface '(chickadee render shader))) @@ -148,8 +149,7 @@ from the GPU's memory." glsl-version (make-gpu-state (module-ref blend-module 'apply-blend-mode) 'replace) - (make-gpu-state (module-ref blend-module 'apply-depth-test) - #f) + (make-gpu-state (module-ref depth-module 'apply-depth-test) #f) (make-gpu-state (module-ref framebuffer-module 'apply-framebuffer) (module-ref framebuffer-module 'null-framebuffer)) (make-gpu-state (module-ref shader-module 'apply-shader) diff --git a/chickadee/render/model.scm b/chickadee/render/model.scm index 2e73506..a9133c4 100644 --- a/chickadee/render/model.scm +++ b/chickadee/render/model.scm @@ -29,6 +29,7 @@ #:use-module (chickadee render) #:use-module (chickadee render buffer) #:use-module (chickadee render color) + #:use-module (chickadee render depth) #:use-module (chickadee render pbr) #:use-module (chickadee render phong) #:use-module (chickadee render shader) @@ -175,7 +176,7 @@ (%make-model name scenes default-scene render-state)) (define (draw-model model model-matrix view-matrix) - (with-depth-test #t + (with-depth-test default-depth-test (let ((state (model-render-state model))) (render-state-reset! state) (render-state-view-matrix-mult! state view-matrix) |