summaryrefslogtreecommitdiff
path: root/examples/triangle.scm
blob: 2debc85b5f6b4c7f353259eb6dc4c1e85b7d9bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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)