summaryrefslogtreecommitdiff
path: root/chickadee
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2021-03-14 13:02:15 -0400
committerDavid Thompson <dthompson2@worcester.edu>2021-03-14 13:02:15 -0400
commitfec2bca489fa67f67bb6047215ebceb831305109 (patch)
tree02d5b19e67e72c63506016e70128d63ebe9b0267 /chickadee
parent6c652d155fe6af7af0d151c5187b8c4ed8abba2b (diff)
graphics: blend: Add proper <blend-mode> data type.
No more symbols. Hooray!
Diffstat (limited to 'chickadee')
-rw-r--r--chickadee/graphics/blend.scm146
-rw-r--r--chickadee/graphics/font.scm5
-rw-r--r--chickadee/graphics/particles.scm2
-rw-r--r--chickadee/graphics/path.scm2
-rw-r--r--chickadee/graphics/sprite.scm12
5 files changed, 120 insertions, 47 deletions
diff --git a/chickadee/graphics/blend.scm b/chickadee/graphics/blend.scm
index 8901246..d13d541 100644
--- a/chickadee/graphics/blend.scm
+++ b/chickadee/graphics/blend.scm
@@ -16,54 +16,126 @@
;;; <http://www.gnu.org/licenses/>.
(define-module (chickadee graphics blend)
- #:use-module (ice-9 match)
- #:use-module (gl)
#:use-module (chickadee graphics engine)
#:use-module (chickadee graphics gl)
- #:export (g:blend-mode
+ #:use-module (gl)
+ #:use-module (ice-9 match)
+ #:use-module (srfi srfi-9)
+ #:export (make-blend-mode
+ blend-mode?
+ blend-mode-equation
+ blend-mode-source-function
+ blend-mode-destination-function
+ blend:alpha
+ blend:multiply
+ blend:subtract
+ blend:add
+ blend:lighten
+ blend:darken
+ blend:screen
+ blend:replace
+ g:blend-mode
current-blend-mode))
+(define-record-type <blend-mode>
+ (make-blend-mode equation source-function destination-function)
+ blend-mode?
+ (equation blend-mode-equation)
+ (source-function blend-mode-source-function)
+ (destination-function blend-mode-destination-function))
+
+(define blend:alpha
+ (make-blend-mode 'add 'source-alpha 'one-minus-source-alpha))
+(define blend:multiply
+ (make-blend-mode 'add 'destination-color 'zero))
+(define blend:subtract
+ (make-blend-mode 'reverse-subtract 'one 'zero))
+(define blend:add
+ (make-blend-mode 'add 'one 'one))
+(define blend:lighten
+ (make-blend-mode 'max 'one 'zero))
+(define blend:darken
+ (make-blend-mode 'min 'one 'zero))
+(define blend:screen
+ (make-blend-mode 'add 'one 'one-minus-source-color))
+(define blend:replace
+ (make-blend-mode 'add 'one 'zero))
+
(define (bind-blend-mode blend-mode)
(if blend-mode
(begin
(gl-enable (enable-cap blend))
- (match blend-mode
- ('alpha
- (gl-blend-equation (blend-equation-mode-ext func-add-ext))
- (gl-blend-func (blending-factor-src src-alpha)
- (blending-factor-dest one-minus-src-alpha)))
- ('multiply
- (gl-blend-equation (blend-equation-mode-ext func-add-ext))
- (gl-blend-func (blending-factor-src dst-color)
- (blending-factor-dest zero)))
- ('subtract
- (gl-blend-equation
+ (gl-blend-equation
+ (match (blend-mode-equation blend-mode)
+ ('add
+ (blend-equation-mode-ext func-add-ext))
+ ('subtract
+ (blend-equation-mode-ext func-subtract-ext))
+ ('reverse-subtract
(blend-equation-mode-ext func-reverse-subtract-ext))
- (gl-blend-func (blending-factor-src one)
- (blending-factor-dest zero)))
- ('add
- (gl-blend-equation (blend-equation-mode-ext func-add-ext))
- (gl-blend-func (blending-factor-src one)
- (blending-factor-dest one)))
- ('lighten
- (gl-blend-equation (blend-equation-mode-ext max-ext))
- (gl-blend-func (blending-factor-src one)
- (blending-factor-dest zero)))
- ('darken
- (gl-blend-equation (blend-equation-mode-ext min-ext))
- (gl-blend-func (blending-factor-src one)
- (blending-factor-dest zero)))
- ('screen
- (gl-blend-equation (blend-equation-mode-ext func-add-ext))
- (gl-blend-func (blending-factor-src one)
- (blending-factor-dest one-minus-src-color)))
- ('replace
- (gl-blend-equation (blend-equation-mode-ext func-add-ext))
- (gl-blend-func (blending-factor-src one)
- (blending-factor-dest zero)))))
+ ('min
+ (blend-equation-mode-ext min-ext))
+ ('max
+ (blend-equation-mode-ext max-ext))
+ ('alpha-min
+ (blend-equation-mode-ext alpha-min-sgix))
+ ('alpha-max
+ (blend-equation-mode-ext alpha-min-sgix))))
+ (gl-blend-func (match (blend-mode-source-function blend-mode)
+ ('zero
+ (blending-factor-src zero))
+ ('one
+ (blending-factor-src one))
+ ('destination-color
+ (blending-factor-src dst-color))
+ ('one-minus-destination-color
+ (blending-factor-src one-minus-dst-color))
+ ('source-alpha-saturate
+ (blending-factor-src src-alpha-saturate))
+ ('source-alpha
+ (blending-factor-src src-alpha))
+ ('one-minus-source-alpha
+ (blending-factor-src one-minus-src-alpha))
+ ('destination-alpha
+ (blending-factor-src dst-alpha))
+ ('one-minus-destination-alpha
+ (blending-factor-src one-minus-dst-alpha))
+ ('constant-color
+ (blending-factor-src constant-color-ext))
+ ('one-minus-constant-color
+ (blending-factor-src one-minus-constant-color-ext))
+ ('contstant-alpha
+ (blending-factor-src constant-alpha-ext))
+ ('one-minus-constant-alpha
+ (blending-factor-src one-minus-constant-alpha-ext)))
+ (match (blend-mode-destination-function blend-mode)
+ ('zero
+ (blending-factor-dest zero))
+ ('one
+ (blending-factor-dest one))
+ ('source-color
+ (blending-factor-dest src-color))
+ ('one-minus-source-color
+ (blending-factor-dest one-minus-src-color))
+ ('source-alpha
+ (blending-factor-dest src-alpha))
+ ('one-minus-source-alpha
+ (blending-factor-dest one-minus-src-alpha))
+ ('destination-alpha
+ (blending-factor-dest dst-alpha))
+ ('one-minus-destination-alpha
+ (blending-factor-dest one-minus-dst-alpha))
+ ('constant-color
+ (blending-factor-dest constant-color-ext))
+ ('one-minus-constant-color
+ (blending-factor-dest one-minus-constant-color-ext))
+ ('contstant-alpha
+ (blending-factor-dest constant-alpha-ext))
+ ('one-minus-constant-alpha
+ (blending-factor-dest one-minus-constant-alpha-ext)))))
(gl-disable (enable-cap blend))))
(define-graphics-state g:blend-mode
current-blend-mode
- #:default 'replace
+ #:default blend:replace
#:bind bind-blend-mode)
diff --git a/chickadee/graphics/font.scm b/chickadee/graphics/font.scm
index eb1de3f..8adba8d 100644
--- a/chickadee/graphics/font.scm
+++ b/chickadee/graphics/font.scm
@@ -37,6 +37,7 @@
#:use-module (chickadee math matrix)
#:use-module (chickadee math rect)
#:use-module (chickadee math vector)
+ #:use-module (chickadee graphics blend)
#:use-module (chickadee graphics color)
#:use-module (chickadee graphics engine)
#:use-module (chickadee graphics shader)
@@ -433,7 +434,7 @@ extension must be either .xml or .fnt."
(define draw-text*
(let ((cursor (vec2 0.0 0.0))
(rect (make-rect 0.0 0.0 0.0 0.0)))
- (lambda* (font text matrix #:key (blend-mode 'alpha)
+ (lambda* (font text matrix #:key (blend-mode blend:alpha)
(color white) (start 0) (end (string-length text)))
(let ((batches (font-sprite-batches font))
(kernings (font-kernings font)))
@@ -500,7 +501,7 @@ extension must be either .xml or .fnt."
(origin %null-vec2)
(rotation 0)
(scale %default-scale)
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(start 0)
(end (string-length text)))
"Draw the string TEXT with the first character starting at
diff --git a/chickadee/graphics/particles.scm b/chickadee/graphics/particles.scm
index d735572..08e5957 100644
--- a/chickadee/graphics/particles.scm
+++ b/chickadee/graphics/particles.scm
@@ -204,7 +204,7 @@ void main (void) {
(delete emitter (particles-emitters particles))))
(define* (make-particles capacity #:key
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(start-color white)
(end-color (make-color 0.0 0.0 0.0 0.0))
(texture null-texture)
diff --git a/chickadee/graphics/path.scm b/chickadee/graphics/path.scm
index 8a4edfd..ef5206a 100644
--- a/chickadee/graphics/path.scm
+++ b/chickadee/graphics/path.scm
@@ -1194,7 +1194,7 @@
;; Another mini VM for a simple picture language.
(let loop ((commands (painter-commands painter))
(matrix matrix)
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(fill-color white)
(stroke-color black)
(stroke-width 1.0)
diff --git a/chickadee/graphics/sprite.scm b/chickadee/graphics/sprite.scm
index 76ee94a..0d6f838 100644
--- a/chickadee/graphics/sprite.scm
+++ b/chickadee/graphics/sprite.scm
@@ -108,7 +108,7 @@ void main (void) {
matrix
#:key
(tint white)
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(texcoords (texture-gl-tex-rect texture)))
(let ((shader (graphics-variable-ref sprite-shader))
(geometry (graphics-variable-ref sprite-geometry))
@@ -152,7 +152,7 @@ void main (void) {
(origin %null-vec2)
(scale %default-scale)
(rotation 0.0)
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(rect (texture-gl-rect texture)))
"Draw TEXTURE at POSITION.
@@ -335,7 +335,7 @@ may be specified via the TEXTURE-REGION argument."
#:tint tint
#:texture-region texture-region)))
-(define* (draw-sprite-batch* batch matrix #:key (blend-mode 'alpha))
+(define* (draw-sprite-batch* batch matrix #:key (blend-mode blend:alpha))
"Render the contents of BATCH."
(let ((shader (graphics-variable-ref sprite-batch-shader))
(mvp (graphics-variable-ref sprite-mvp-matrix)))
@@ -356,7 +356,7 @@ may be specified via the TEXTURE-REGION argument."
(origin %null-vec2)
(scale %default-scale)
(rotation 0.0)
- (blend-mode 'alpha))
+ (blend-mode blend:alpha))
"Render the contents of BATCH."
(let ((matrix (graphics-variable-ref sprite-model-matrix)))
(matrix4-2d-transform! matrix
@@ -383,7 +383,7 @@ may be specified via the TEXTURE-REGION argument."
(bottom-margin margin)
(left-margin margin)
(right-margin margin)
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(tint white))
(let* ((x (rect-x rect))
(y (rect-y rect))
@@ -463,7 +463,7 @@ may be specified via the TEXTURE-REGION argument."
(origin %null-vec2)
(rotation 0.0)
(scale %default-scale)
- (blend-mode 'alpha)
+ (blend-mode blend:alpha)
(tint white))
"Draw a \"nine patch\" sprite. A nine patch sprite renders
TEXTURE on the rectangular area RECT whose stretchable areas are