summaryrefslogtreecommitdiff
path: root/examples/triangle.scm
diff options
context:
space:
mode:
Diffstat (limited to 'examples/triangle.scm')
-rw-r--r--examples/triangle.scm84
1 files changed, 84 insertions, 0 deletions
diff --git a/examples/triangle.scm b/examples/triangle.scm
new file mode 100644
index 0000000..2debc85
--- /dev/null
+++ b/examples/triangle.scm
@@ -0,0 +1,84 @@
+(use-modules (chickadee)
+ (chickadee math vector)
+ (chickadee graphics buffer)
+ (chickadee graphics engine)
+ (chickadee graphics shader))
+
+(use-modules (gl enums))
+
+(define vertex-array #f)
+(define shader #f)
+
+(define (load)
+ (define verts
+ (make-buffer #f32(0.0 0.0
+ 640.0 0.0
+ 320.0 480.0)))
+ (define colors
+ (make-buffer #f32(1.0 0.0 0.0 1.0
+ 0.0 1.0 0.0 1.0
+ 0.0 0.0 1.0 1.0)))
+ ;; This isn't necessary for a single triangle, but we're doing it
+ ;; anyway just to exercise that code.
+ (define indices (make-buffer #u32(0 1 2) #:target 'index))
+ (set! vertex-array
+ (make-vertex-array
+ #:indices (make-vertex-attribute
+ #:buffer indices
+ #:type 'scalar
+ #:component-type 'unsigned-int)
+ #:attributes `((0 . ,(make-vertex-attribute
+ #:buffer verts
+ #:type 'vec2
+ #:component-type 'float))
+ (1 . ,(make-vertex-attribute
+ #:buffer colors
+ #:type 'color
+ #:component-type 'float)))))
+ (set! shader (strings->shader
+ "
+#ifdef GLSL330
+layout (location = 0) in vec2 position;
+layout (location = 1) in vec4 color;
+#elif defined(GLSL130)
+in vec2 position;
+in vec4 color;
+#elif defined(GLSL120)
+attribute vec2 position;
+attribute vec4 color;
+#endif
+#ifdef GLSL120
+varying vec4 fragColor;
+#else
+out vec4 fragColor;
+#endif
+uniform mat4 mvp;
+
+void main(void) {
+ fragColor = color;
+ gl_Position = mvp * vec4(position, 0.0, 1.0);
+}
+"
+ "
+#ifdef GLSL120
+varying vec4 fragColor;
+#else
+in vec4 fragColor;
+ #endif
+#ifdef GLSL330
+out vec4 outFragColor;
+#endif
+
+void main (void) {
+#ifdef GLSL330
+ outFragColor = fragColor;
+#else
+ gl_FragColor = fragColor;
+#endif
+}
+")))
+
+(define (draw alpha)
+ (shader-apply shader vertex-array #:mvp (current-projection)))
+
+(run-game #:load load #:draw draw)