forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.lisp
209 lines (187 loc) · 8.27 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
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
#|
This file is a part of trial
(c) 2017 Shirakumo http://tymoon.eu ([email protected])
Author: Nicolas Hafner <[email protected]>
|#
(in-package #:org.shirakumo.fraf.trial)
(define-shader-entity text (located-entity vertex-entity colored-entity textured-entity)
((texture :initarg :font :initform (error "FONT required.") :accessor font)
(text :initarg :text :accessor text)
(size :initarg :size :accessor size)
(wrap :initarg :wrap :accessor wrap)
(width :initarg :width :accessor width)
(height :initarg :height :accessor height)
(vbo) (ebo))
(:default-initargs
:color (vec 0 0 0 1)
:text ""
:wrap NIL
:width NIL
:height NIL)
(:inhibit-shaders (colored-entity :fragment-shader)
(textured-entity :fragment-shader)))
(defmethod initialize-instance :after ((text text) &key size)
(let* ((vbo (make-instance 'vertex-buffer :buffer-type :array-buffer
:data-usage :dynamic-draw
:size 0))
(ebo (make-instance 'vertex-buffer :buffer-type :element-array-buffer
:data-usage :dynamic-draw
:size 0))
(vao (make-instance 'vertex-array :bindings `((,vbo :size 2 :stride 16 :offset 0)
(,vbo :size 2 :stride 16 :offset 8)
,ebo))))
(setf (slot-value text 'vbo) vbo)
(setf (slot-value text 'ebo) ebo)
(setf (vertex-array text) vao)
(unless size (setf (size text) (size (font text))))))
(defmethod render ((text text) target)
(let ((r (/ (size text) (size (font text)))))
(with-pushed-matrix (*model-matrix*)
(scale-by r r r)
(call-next-method))))
(define-class-shader (text :fragment-shader)
"uniform vec4 objectcolor;
uniform sampler2D texture_image;
in vec2 texcoord;
out vec4 color;
void main(){
float intensity = texture(texture_image, texcoord, -0.65).r;
color = objectcolor * intensity;
}")
(defmethod (setf font) :after (font (text text))
(when (allocated-p font)
(setf (text text) (text text))))
;; FIXME: this is no longer usable.
;; (defmethod resources-ready ((text text))
;; (setf (text text) (text text)))
(defun wrap-text (font string width)
;; FIXME: This is really primitive.
(destructuring-bind (&key l r ((:t u)) b gap) (text-extent font " ")
(let ((s (+ r l))
(current-width 0)
(height (- u b))
(buffer (make-string-output-stream))
(vskip (/ (* (size font) (+ gap u)) (+ u b))))
(values (with-output-to-string (out)
(flet ((complete-word ()
(let* ((word (get-output-stream-string buffer))
(ext (text-extent font word))
(w (+ (getf ext :r) (getf ext :l))))
(when (<= width (+ current-width s w))
(write-char #\Linefeed out)
(setf current-width 0)
(incf height vskip))
(write-string word out)
(write-char #\Space out)
(incf current-width (+ s w)))))
(loop for c across string
do (case c
(#\Space
(complete-word))
(#\Linefeed
(complete-word)
(setf current-width width))
(T
(write-char c buffer)))
finally (complete-word))))
height))))
(defmethod (setf width) :after (width (text text))
(when (wrap text)
(setf (text text) (text text))))
(defmethod (setf text) :around ((string string) (text text))
(let ((vao (vertex-array text))
(vbo (slot-value text 'vbo))
(ebo (slot-value text 'ebo))
(font (font text)))
(when (and (allocated-p font)
(allocated-p vao))
(let ((s (/ (size (font text)) (size text))))
(if (wrap text)
(multiple-value-bind (string height) (wrap-text font string (* (width text) s))
(setf (height text) (/ height s))
(setf (size vao) (cl-fond:update-text font string (gl-name vbo) (gl-name ebo)))
(call-next-method string text))
(destructuring-bind (&key l r ((:t u)) b gap) (text-extent font string)
(declare (ignore gap))
(setf (width text) (/ (+ l r) s))
(setf (height text) (/ (- u b) s))
(setf (size vao) (cl-fond:update-text font string (gl-name vbo) (gl-name ebo)))
(call-next-method string text)))))))
(defmethod extent ((text text))
(text-extent text (text text)))
(defmethod text-extent ((text text) string)
(destructuring-bind (&key l r ((:t u)) b gap) (text-extent (font text) string)
(let ((s (/ (size text) (size (font text)))))
(list :l (* l s) :r (* r s) :t (* u s) :b (* b s) :gap (* gap s)))))
(define-shader-entity highlighted-text (text)
((cbo)
(color-regions :accessor color-regions))
(:default-initargs :color-regions ())
(:inhibit-shaders (text :fragment-shader)))
(defmethod initialize-instance :after ((text highlighted-text) &key color-regions)
(let* ((cbo (make-instance 'vertex-buffer :buffer-type :array-buffer
:data-usage :dynamic-draw
:size 0))
(ebo (slot-value text 'ebo))
(vbo (slot-value text 'vbo))
(vao (vertex-array text)))
(setf (slot-value text 'cbo) cbo)
(setf (bindings vao) `((,vbo :size 2 :stride 16 :offset 0)
(,vbo :size 2 :stride 16 :offset 8)
(,cbo :size 4 :stride 16 :offset 0)
,ebo))
(setf (color-regions text) color-regions)))
(defun %update-highlight-buffer (text length)
(let ((cbo (slot-value text 'cbo))
(size (* 4 4 length))
(unit (color text)))
(cffi:with-foreign-object (array :float size)
(loop with regions = (color-regions text)
for i from 0 below length
do (flet ((insert (c)
;; FIXME: Ahead-of-time translation of colors to buffers
;; and then simply use memcpy here.
(loop for j from (* 4 4 i) by 4 repeat 4
do (setf (cffi:mem-aref array :float (+ j 0)) (vx c))
(setf (cffi:mem-aref array :float (+ j 1)) (vy c))
(setf (cffi:mem-aref array :float (+ j 2)) (vz c))
(setf (cffi:mem-aref array :float (+ j 3)) (vw c)))))
(if regions
(destructuring-bind (s e c) (first regions)
(cond ((< i s)
(insert unit))
((< i e)
(insert c))
(T
(pop regions)
(decf i))))
(insert unit))))
(setf (size cbo) (* 4 size))
(resize-buffer/ptr cbo (size cbo) array))))
(defmethod (setf color-regions) :around (regions (text highlighted-text))
;; FIXME: Check for overlapping regions.
(let ((regions (sort regions #'< :key #'first)))
(call-next-method regions text)
(when (allocated-p (slot-value text 'cbo))
(%update-highlight-buffer text (length (text text))))))
(defmethod (setf text) :before (string (text highlighted-text))
(let ((cbo (slot-value text 'cbo))
(font (font text)))
(when (and (allocated-p font)
(allocated-p cbo))
(%update-highlight-buffer text (length string)))))
(define-class-shader (highlighted-text :vertex-shader)
"layout (location = 2) in vec4 color;
out vec4 character_color;
void main(){
character_color = color;
}")
(define-class-shader (highlighted-text :fragment-shader)
"uniform sampler2D texture_image;
in vec2 texcoord;
in vec4 character_color;
out vec4 color;
void main(){
float intensity = texture(texture_image, texcoord, -0.65).r;
color = character_color*intensity;
}")