From 1425a0dc73f7fa37612b7a82f090e9b10ddebf25 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 1 Sep 2023 09:00:01 -0400 Subject: 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. --- examples/triangle.scm | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 examples/triangle.scm (limited to 'examples/triangle.scm') 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) -- cgit v1.2.3