summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-02-09 15:09:18 -0500
committerDavid Thompson <dthompson2@worcester.edu>2014-02-09 15:15:27 -0500
commit0529df9a8021d09f592e0a6eef0c19e148c5336e (patch)
tree2cc030a1a9d17bd1ae6441e5733cabf14f363a56 /examples
parent2d7d9ad02e22adcb5461d96a3e558f0b45da910c (diff)
Add shader module.
* 2d/shader.scm: New module. * Makefile.am: Add 2d/shader.scm to SOURCES. * examples/shader.scm: New example. * examples/shaders/fragment-shader.glsl: New shader. * examples/shaders/vertex-shader.glsl: New shader.
Diffstat (limited to 'examples')
-rw-r--r--examples/shader.scm33
-rw-r--r--examples/shaders/fragment-shader.glsl7
-rw-r--r--examples/shaders/vertex-shader.glsl13
3 files changed, 53 insertions, 0 deletions
diff --git a/examples/shader.scm b/examples/shader.scm
new file mode 100644
index 0000000..49893ad
--- /dev/null
+++ b/examples/shader.scm
@@ -0,0 +1,33 @@
+(use-modules (2d game)
+ (2d shader)
+ (2d sprite)
+ (2d vector2)
+ (2d window))
+
+(load "common.scm")
+
+(define vertex-shader
+ (load-vertex-shader "shaders/vertex-shader.glsl"))
+(define fragment-shader
+ (load-fragment-shader "shaders/fragment-shader.glsl"))
+(define program (make-shader-program vertex-shader fragment-shader))
+
+(define window-width 800)
+(define window-height 600)
+(define sprite
+ (load-sprite "images/p1_front.png"
+ #:position (vector2 (/ window-width 2)
+ (/ window-height 2))))
+
+(define (draw dt alpha)
+ (with-shader-program program
+ ;; Shake the sprite around a bit.
+ (uniforms ((angle (/ (random:uniform) 100)))
+ (draw-sprite sprite))))
+
+(add-hook! draw-hook draw)
+
+(with-window (make-window #:title "Shaders"
+ #:resolution (vector2 window-width
+ window-height))
+ (run-game-loop))
diff --git a/examples/shaders/fragment-shader.glsl b/examples/shaders/fragment-shader.glsl
new file mode 100644
index 0000000..9be52f0
--- /dev/null
+++ b/examples/shaders/fragment-shader.glsl
@@ -0,0 +1,7 @@
+#version 120
+
+uniform sampler2D color_texture;
+
+void main (void) {
+ gl_FragColor = texture2D(color_texture, gl_TexCoord[0].st);
+}
diff --git a/examples/shaders/vertex-shader.glsl b/examples/shaders/vertex-shader.glsl
new file mode 100644
index 0000000..2a7a4a4
--- /dev/null
+++ b/examples/shaders/vertex-shader.glsl
@@ -0,0 +1,13 @@
+#version 120
+
+uniform float angle;
+
+void main(void) {
+ mat4 RotationMatrix = mat4(cos(angle), -sin(angle), 0.0, 0.0,
+ sin(angle), cos(angle), 0.0, 0.0,
+ 0.0, 0.0, 1.0, 0.0,
+ 0.0, 0.0, 0.0, 1.0);
+
+ gl_Position = gl_ModelViewProjectionMatrix * RotationMatrix * gl_Vertex;
+ gl_TexCoord[0] = gl_MultiTexCoord0;
+}