summaryrefslogtreecommitdiff
path: root/examples/triangle.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-09-01 09:00:01 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-11-08 21:35:34 -0500
commit1425a0dc73f7fa37612b7a82f090e9b10ddebf25 (patch)
tree97de459a6b021c7abb04944cdde512efe5909bd4 /examples/triangle.scm
parentd969c19756227899b39967989fa971fa3452e872 (diff)
First pass of rendering engine rewrite.
The (chickadee graphics gpu) module now handles most of the low-level OpenGL object creation/deletion/binding. The (chickadee graphics engine) module handles the with-graphics-state stuff via a render context object. There's lots of stuff that isn't great, but it's the first step towards a graphics backend agnostic rendering layer.
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)