forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.lisp
74 lines (65 loc) · 2.9 KB
/
text.lisp
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
#|
This file is a part of trial
(c) 2017 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.trial)
(define-asset (trial ascii) image
#p"ascii.png"
:mag-filter :nearest)
(defun print-ascii-text (text array)
(let ((i -1) (x 0.0))
(adjust-array array (* 4 6 (length text)))
(macrolet ((vertex (&rest vals)
`(progn ,@(loop for val in vals
collect `(setf (aref array (incf i)) ,val)))))
(flet ((print-letter (char)
(let* ((c (clamp 0 (- (char-code char) (char-code #\Space)) 95))
(u0 (float (/ c 96)))
(u1 (float (/ (1+ c) 96))))
(vertex (+ x 0.0) 0.0 u0 0.0)
(vertex (+ x 7.0) 0.0 u1 0.0)
(vertex (+ x 7.0) 7.0 u1 1.0)
(vertex (+ x 7.0) 7.0 u1 1.0)
(vertex (+ x 0.0) 7.0 u0 1.0)
(vertex (+ x 0.0) 0.0 u0 0.0)
(incf x 6.0))))
(loop for char across text
do (print-letter char))))
array))
(define-shader-entity debug-text (located-entity vertex-entity textured-entity)
((texture :initarg :font :initform (// 'trial 'ascii) :accessor font)
(text :initarg :text :initform "" :accessor text)
(foreground :initarg :foreground :initform (vec4 0 0 0 1) :accessor foreground)
(background :initarg :background :initform (vec4 0 0 0 0) :accessor background))
(:inhibit-shaders (textured-entity :fragment-shader)))
(defmethod initialize-instance :after ((text debug-text) &key)
(let* ((array (make-array 0 :element-type 'single-float :adjustable T))
(vbo (make-instance 'vertex-buffer :buffer-data array))
(vao (make-instance 'vertex-array :bindings `((,vbo :size 2 :offset 0 :stride 16)
(,vbo :size 2 :offset 8 :stride 16))
:size (* 6 (length (text text))))))
(print-ascii-text (text text) array)
(setf (vertex-array text) vao)))
(defmethod (setf text) :after (_ (text debug-text))
(when (allocated-p (vertex-array text))
(let* ((vao (vertex-array text))
(vbo (caar (bindings vao)))
(array (buffer-data vbo))
(text (text text)))
(print-ascii-text text array)
(setf (size vao) (* 6 (length text)))
(resize-buffer vbo (* 4 (length array)) :data array))))
(defmethod render :before ((text debug-text) (program shader-program))
(setf (uniform program "foreground") (foreground text))
(setf (uniform program "background") (background text)))
(define-class-shader (debug-text :fragment-shader)
"in vec2 texcoord;
out vec4 color;
uniform sampler2D texture_image;
uniform vec4 foreground;
uniform vec4 background;
void main(){
float fg_bg = texture(texture_image, texcoord).r;
color = mix(foreground, background, fg_bg);
}")