render: viewport: Decouple screen clearing from viewport switching.
authorDavid Thompson <dthompson2@worcester.edu>
Sun, 12 Apr 2020 02:55:30 +0000 (22:55 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Sun, 12 Apr 2020 02:55:30 +0000 (22:55 -0400)
This makes it possible to a apply a viewport multiple times in a frame
without clearing each time and wiping out part of your work.

chickadee.scm
chickadee/render.scm
chickadee/render/viewport.scm

index 4edeb03..e7fb443 100644 (file)
@@ -321,10 +321,8 @@ border is disabled, otherwise it is enabled.")
       ;; Free any GPU resources that have been GC'd.
       (gpu-reap!))
     (define (render-sdl-opengl alpha)
-      ;; Switch to the default viewport so we can clear the whole screen.
-      (set-gpu-viewport! gpu default-viewport)
-      (gl-clear clear-mask)
       (with-viewport default-viewport
+        (clear-screen)
         (with-projection default-projection
           (draw alpha)))
       (sdl2:swap-gl-window window))
index 49244f9..04295e6 100644 (file)
@@ -43,6 +43,7 @@
             with-depth-test
             with-texture
             with-projection
+            clear-screen
             gpu-apply
             gpu-apply*
             gpu-apply/instanced*
   (with (render-context-viewport set-render-context-viewport! viewport)
         body ...))
 
+(define (clear-screen)
+  (let ((viewport (current-viewport)))
+    (set-gpu-viewport! (current-gpu) viewport)
+    (clear-viewport viewport)))
+
 (define-syntax-rule (with-framebuffer framebuffer body ...)
   (with (render-context-framebuffer set-render-context-framebuffer! framebuffer)
         ;; As a convenience, initialize the viewport and projection
index e61136d..91f50c1 100644 (file)
@@ -39,6 +39,7 @@
             viewport-clear-flags
             null-viewport
             apply-viewport
+            clear-viewport
             %default-clear-flags
             %default-clear-color))
 
@@ -92,6 +93,9 @@ CLEAR-FLAGS are 'color-buffer', 'depth-buffer', 'accum-buffer', and
                   ('color-buffer 16384))
                  flags)))))
 
+(define (clear-viewport viewport)
+  (gl-clear (clear-buffer-mask (viewport-clear-flags viewport))))
+
 (define (apply-viewport viewport)
   "Set the OpenGL state for VIEWPORT.  Clip rendering to the viewport
 area, set the clear color, and clear necessary buffers."
@@ -104,5 +108,4 @@ area, set the clear color, and clear necessary buffers."
       (gl-enable (enable-cap scissor-test))
       (gl-viewport x y w h)
       (gl-scissor x y w h)
-      (gl-clear-color (color-r c) (color-g c) (color-b c) (color-a c))
-      (gl-clear (clear-buffer-mask (viewport-clear-flags viewport))))))
+      (gl-clear-color (color-r c) (color-g c) (color-b c) (color-a c)))))