From 39fdda4aaba16990ac271b880059ebeb0d747b43 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 16 Aug 2014 22:08:13 -0400 Subject: Use a single default shader for all meshes. * data/shaders/default-fragment.glsl: New file. * data/shaders/default-vertex.glsl: New file. * data/shaders/font-fragment.glsl: Delete. * data/shaders/font-vertex.glsl: Delete. * data/shaders/sprite-fragment.glsl: Delete. * data/shaders/sprite-vertex.glsl: Delete. * examples/common.scm: Remove call to enable-sprites. * sly/font.scm (font-shader): Delete. (enable-fonts): Remove shader loading. (make-label): Use default shader. * sly/shape.scm (make-cube): Make texture and shader optional keyword arguments. * sly/sprite.scm (sprite-shader, enable-sprites): Delete. (make-sprite): Make shader an optional keyword argument. --- data/shaders/default-fragment.glsl | 8 ++++ data/shaders/default-vertex.glsl | 11 ++++++ data/shaders/font-fragment.glsl | 8 ---- data/shaders/font-vertex.glsl | 15 -------- data/shaders/sprite-fragment.glsl | 8 ---- data/shaders/sprite-vertex.glsl | 26 ------------- examples/common.scm | 1 - sly/font.scm | 12 +----- sly/shape.scm | 79 ++++++++++++++++++++------------------ sly/sprite.scm | 15 +------- 10 files changed, 65 insertions(+), 118 deletions(-) create mode 100644 data/shaders/default-fragment.glsl create mode 100644 data/shaders/default-vertex.glsl delete mode 100644 data/shaders/font-fragment.glsl delete mode 100644 data/shaders/font-vertex.glsl delete mode 100644 data/shaders/sprite-fragment.glsl delete mode 100644 data/shaders/sprite-vertex.glsl diff --git a/data/shaders/default-fragment.glsl b/data/shaders/default-fragment.glsl new file mode 100644 index 0000000..e90adbe --- /dev/null +++ b/data/shaders/default-fragment.glsl @@ -0,0 +1,8 @@ +#version 130 + +in vec2 frag_tex; +uniform sampler2D color_texture; + +void main (void) { + gl_FragColor = texture2D(color_texture, frag_tex); +} diff --git a/data/shaders/default-vertex.glsl b/data/shaders/default-vertex.glsl new file mode 100644 index 0000000..11f389f --- /dev/null +++ b/data/shaders/default-vertex.glsl @@ -0,0 +1,11 @@ +#version 130 + +in vec3 position; +in vec2 tex; +out vec2 frag_tex; +uniform mat4 mvp; + +void main(void) { + frag_tex = tex; + gl_Position = mvp * vec4(position.xyz, 1.0); +} diff --git a/data/shaders/font-fragment.glsl b/data/shaders/font-fragment.glsl deleted file mode 100644 index 3dd10f1..0000000 --- a/data/shaders/font-fragment.glsl +++ /dev/null @@ -1,8 +0,0 @@ -#version 120 - -uniform sampler2D color_texture; -uniform vec4 color; - -void main (void) { - gl_FragColor = texture2D(color_texture, gl_TexCoord[0].st) * color; -} diff --git a/data/shaders/font-vertex.glsl b/data/shaders/font-vertex.glsl deleted file mode 100644 index 196da12..0000000 --- a/data/shaders/font-vertex.glsl +++ /dev/null @@ -1,15 +0,0 @@ -#version 120 - -uniform mat4 projection; -uniform vec2 position; -uniform vec2 anchor; - -void main(void) { - mat4 translation = mat4(1.0, 0.0, 0.0, position.x - anchor.x, - 0.0, 1.0, 0.0, position.y - anchor.y, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); - - gl_Position = projection * (gl_Vertex * translation); - gl_TexCoord[0] = gl_MultiTexCoord0; -} diff --git a/data/shaders/sprite-fragment.glsl b/data/shaders/sprite-fragment.glsl deleted file mode 100644 index 3dd10f1..0000000 --- a/data/shaders/sprite-fragment.glsl +++ /dev/null @@ -1,8 +0,0 @@ -#version 120 - -uniform sampler2D color_texture; -uniform vec4 color; - -void main (void) { - gl_FragColor = texture2D(color_texture, gl_TexCoord[0].st) * color; -} diff --git a/data/shaders/sprite-vertex.glsl b/data/shaders/sprite-vertex.glsl deleted file mode 100644 index d6efd3f..0000000 --- a/data/shaders/sprite-vertex.glsl +++ /dev/null @@ -1,26 +0,0 @@ -#version 120 - -uniform mat4 projection; -uniform vec2 position; -uniform vec2 anchor; -uniform vec2 scale; -uniform float rotation; - -void main(void) { - mat4 rotationMatrix = mat4(cos(rotation), -sin(rotation), 0.0, 0.0, - sin(rotation), cos(rotation), 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); - mat4 translationMatrix = mat4(1.0, 0.0, 0.0, position.x - anchor.x, - 0.0, 1.0, 0.0, position.y - anchor.y, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); - mat4 scaleMatrix = mat4(scale.x, 0.0, 0.0, 0.0, - 0.0, scale.y, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0); - - gl_Position = projection * (gl_Vertex * scaleMatrix * - rotationMatrix * translationMatrix); - gl_TexCoord[0] = gl_MultiTexCoord0; -} diff --git a/examples/common.scm b/examples/common.scm index 03a0a28..07e7aae 100644 --- a/examples/common.scm +++ b/examples/common.scm @@ -25,7 +25,6 @@ (sly window)) (open-window) -(enable-sprites) (add-hook! key-press-hook (lambda (key unicode) (when (eq? key 'escape) diff --git a/sly/font.scm b/sly/font.scm index fd0a304..ae80263 100644 --- a/sly/font.scm +++ b/sly/font.scm @@ -46,16 +46,8 @@ ;;; Font ;;; -(define font-shader #f) - (define (enable-fonts) - (SDL:ttf-init) - (set! font-shader - (load-shader-program - (string-append %pkgdatadir - "/shaders/font-vertex.glsl") - (string-append %pkgdatadir - "/shaders/font-fragment.glsl")))) + (SDL:ttf-init)) (define-record-type (make-font ttf point-size) @@ -105,7 +97,7 @@ argument with a default value of 12." (define* (make-label font text #:optional #:key (anchor 'top-left) (color white) - (shader font-shader)) + (shader (load-default-shader))) (let ((texture (render-text font text))) (let ((w (texture-width texture)) (h (texture-height texture)) diff --git a/sly/shape.scm b/sly/shape.scm index 3d96ca7..1526caf 100644 --- a/sly/shape.scm +++ b/sly/shape.scm @@ -24,15 +24,13 @@ (define-module (sly shape) #:use-module (sly math) #:use-module (sly mesh) + #:use-module (sly shader) #:use-module (sly texture) #:export (make-cube)) -(define (make-cube texture shader size) - (let* ((s1 (texture-s1 texture)) - (t1 (texture-t1 texture)) - (s2 (texture-s2 texture)) - (t2 (texture-t2 texture)) - (half-size (half size))) +(define* (make-cube size #:optional #:key (texture #f) + (shader (load-default-shader))) + (let ((half-size (half size))) (make-mesh #:shader shader #:texture texture @@ -80,34 +78,41 @@ (vector half-size half-size (- half-size)) (vector half-size half-size half-size) (vector half-size (- half-size) half-size))) - ("tex" ,(vector - ;; Front - (vector s1 t1) - (vector s2 t1) - (vector s2 t2) - (vector s1 t2) - ;; Back - (vector s1 t1) - (vector s2 t1) - (vector s2 t2) - (vector s1 t2) - ;; Top - (vector s1 t1) - (vector s2 t1) - (vector s2 t2) - (vector s1 t2) - ;; Bottom - (vector s1 t1) - (vector s2 t1) - (vector s2 t2) - (vector s1 t2) - ;; Left - (vector s1 t1) - (vector s2 t1) - (vector s2 t2) - (vector s1 t2) - ;; Right - (vector s1 t1) - (vector s2 t1) - (vector s2 t2) - (vector s1 t2))))))) + ,@(if texture + (let ((s1 (texture-s1 texture)) + (t1 (texture-t1 texture)) + (s2 (texture-s2 texture)) + (t2 (texture-t2 texture))) + `("texture" + ,(vector + ;; Front + (vector s1 t1) + (vector s2 t1) + (vector s2 t2) + (vector s1 t2) + ;; Back + (vector s1 t1) + (vector s2 t1) + (vector s2 t2) + (vector s1 t2) + ;; Top + (vector s1 t1) + (vector s2 t1) + (vector s2 t2) + (vector s1 t2) + ;; Bottom + (vector s1 t1) + (vector s2 t1) + (vector s2 t2) + (vector s1 t2) + ;; Left + (vector s1 t1) + (vector s2 t1) + (vector s2 t2) + (vector s1 t2) + ;; Right + (vector s1 t1) + (vector s2 t1) + (vector s2 t2) + (vector s1 t2)))) + '()))))) diff --git a/sly/sprite.scm b/sly/sprite.scm index 9427373..f92687f 100644 --- a/sly/sprite.scm +++ b/sly/sprite.scm @@ -34,24 +34,13 @@ #:use-module (sly mesh) #:use-module (sly shader) #:use-module (sly texture) - #:export (enable-sprites - make-sprite)) + #:export (make-sprite)) ;;; ;;; Sprites ;;; -(define sprite-shader #f) - -(define (enable-sprites) - (set! sprite-shader - (load-shader-program - (string-append %pkgdatadir - "/shaders/sprite-vertex.glsl") - (string-append %pkgdatadir - "/shaders/sprite-fragment.glsl")))) - -(define (make-sprite texture shader) +(define* (make-sprite texture #:optional #:key (shader (load-default-shader))) (let* ((half-width (half (texture-width texture))) (half-height (half (texture-height texture))) (s1 (texture-s1 texture)) -- cgit v1.2.3