summaryrefslogtreecommitdiff
path: root/chickadee/render/shapes.scm
blob: 415eff82dc681bd2f69b239d1cb125a136aef4e1 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
;;; Chickadee Game Toolkit
;;; Copyright © 2016 David Thompson <davet@gnu.org>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary
;;
;; Polylines as described in
;; http://jcgt.org/published/0002/02/08/paper.pdf
;;
;;; Code:

(define-module (chickadee render shapes)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-4)
  #:use-module (chickadee math matrix)
  #:use-module (chickadee math vector)
  #:use-module (chickadee render)
  #:use-module (chickadee render color)
  #:use-module (chickadee render shader)
  #:use-module (chickadee render buffer)
  #:export (draw-line))

(define draw-line
  (let* ((vertex-buffer
          (delay
            (make-streaming-typed-buffer 'vec2 'float 4
                                         #:name "line-typed-buffer")))
         (texcoord-buffer
          (delay
            (make-streaming-typed-buffer 'vec2 'float 4
                                         #:name "line-typed-buffer")))
         (index-buffer
          (delay
            (make-typed-buffer #:type 'scalar
                               #:component-type 'unsigned-int
                               #:buffer (make-buffer (u32vector 0 3 2 0 2 1)
                                                     #:target 'index))))
         (vertex-array
          (delay
            (make-vertex-array #:indices (force index-buffer)
                               #:attributes `((0 . ,(force vertex-buffer))
                                              (1 . ,(force texcoord-buffer))))))
         (default-shader
           (delay
             (strings->shader
              "
#version 330

in vec2 position;
in vec2 tex;
out vec2 frag_tex;
uniform mat4 projection;

void main(void) {
    frag_tex = tex;
    gl_Position = projection * vec4(position.xy, 0.0, 1.0);
}
"
              "
#version 330

in vec2 frag_tex;
uniform vec4 color;
uniform float r;
uniform float w;
uniform float t;
uniform float l;
uniform int cap;
float infinity = 1.0 / 0.0;

void main (void) {
  float hw = w / 2.0;
  float u = frag_tex.x;
  float v = frag_tex.y;
  float dx;
  float dy;
  float d;

  if (u < 0 || u > l) {
    if (u < 0) {
      dx = abs(u);
    } else {
      dx = u - l;
    }
    dy = abs(v);

    switch (cap) {
    // none
    case 0:
      d = infinity;
      break;
    // butt
    case 1:
      d = max(dx + w / 2 - 2 * r, dy);
      break;
    // square
    case 2:
      d = max(dx, dy);
      break;
    // round
    case 3:
      d = sqrt(dx * dx + dy * dy);
      break;
    // triangle out
    case 4:
      d = dx + dy;
      break;
    // triangle in
    case 5:
      d = max(dy, w / 2 - r + dx - dy);
      break;
    }
  } else {
    d = abs(v);
  }

  if (d <= hw) {
    gl_FragColor = color;
  } else {
    gl_FragColor = vec4(color.rgb, color.a * (1.0 - ((d - hw) / r)));
  }
}
"))))
    (lambda* (start end #:key
                    (thickness 0.5)
                    (feather 1.0)
                    (cap 'round)
                    (color white)
                    (shader (force default-shader)))
      "Draw a line segment from START to END. The line will be
THICKNESS pixels thick with an antialiased border FEATHER pixels wide.
The line will be colored COLOR. CAP specifies the type of end cap that
should be used to terminate the lines, either 'none', 'butt',
'square', 'round', 'triangle-in', or 'triangle-out'.  Advanced users
may use SHADER to override the built-in line segment shader."
      (let* ((x1 (vec2-x start))
             (y1 (vec2-y start))
             (x2 (vec2-x end))
             (y2 (vec2-y end))
             (dx (- x2 x1))
             (dy (- y2 y1))
             (length (sqrt (+ (expt dx 2) (expt dy 2))))
             (padding (/ (ceiling (+ thickness (* feather 2.5))) 2.0))
             (nx (/ dx length))
             (ny (/ dy length))
             (xpad (* nx padding))
             (ypad (* ny padding))
             ;; start left
             (vx1 (+ (- x1 xpad) ypad))
             (vy1 (+ (- y1 ypad) (- xpad)))
             (s1 (- padding))
             (t1 padding)
             ;; start right
             (vx2 (+ (- x1 xpad) (- ypad)))
             (vy2 (+ (- y1 ypad) xpad))
             (s2 (- padding))
             (t2 (- padding))
             ;; end left
             (vx3 (+ x2 xpad (- ypad)))
             (vy3 (+ y2 ypad xpad))
             (s3 (+ length padding))
             (t3 (- padding))
             ;; end right
             (vx4 (+ (+ x2 xpad) ypad))
             (vy4 (+ (+ y2 ypad) (- xpad)))
             (s4 (+ length padding))
             (t4 padding))
        (with-mapped-typed-buffer (force vertex-buffer)
          (let ((bv (typed-buffer-data (force vertex-buffer))))
            (f32vector-set! bv 0 vx1)
            (f32vector-set! bv 1 vy1)
            (f32vector-set! bv 2 vx2)
            (f32vector-set! bv 3 vy2)
            (f32vector-set! bv 4 vx3)
            (f32vector-set! bv 5 vy3)
            (f32vector-set! bv 6 vx4)
            (f32vector-set! bv 7 vy4)))
        (with-mapped-typed-buffer (force texcoord-buffer)
          (let ((bv (typed-buffer-data (force texcoord-buffer))))
            (f32vector-set! bv 0 s1)
            (f32vector-set! bv 1 t1)
            (f32vector-set! bv 2 s2)
            (f32vector-set! bv 3 t2)
            (f32vector-set! bv 4 s3)
            (f32vector-set! bv 5 t3)
            (f32vector-set! bv 6 s4)
            (f32vector-set! bv 7 t4)))
        (with-blend-mode 'alpha
          (gpu-apply shader (force vertex-array)
                     #:projection (current-projection)
                     #:color color
                     #:w thickness
                     #:r feather
                     #:l length
                     #:cap (match cap
                             ('none 0)
                             ('butt 1)
                             ('square 2)
                             ('round 3)
                             ('triangle-out 4)
                             ('triangle-in 5))))))))