diff options
Diffstat (limited to 'src/interactive/text.lisp')
-rw-r--r-- | src/interactive/text.lisp | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/src/interactive/text.lisp b/src/interactive/text.lisp index dd99653..d79f7bf 100644 --- a/src/interactive/text.lisp +++ b/src/interactive/text.lisp @@ -9,6 +9,7 @@ (vao elem-count newlines :r) (shader :with :static :r))) + (defmethod model-matrix ((text text)) (let ((m (mat:meye 4))) (with-slots (font newlines x y base-width base-height scale-x scale-y rotation) text @@ -36,22 +37,34 @@ (mat:nmscale m (vec:vec scale-x scale-y 1.0)) m))) +(defvar *text-shader-program* nil) + +(defun make-shared-text-gpu-objects () + (unless *text-shader-program* + (setf *text-shader-program* + (create-shader + '(:vertex + ((vert :vec2) (col :vec2)) + ((transform :mat4)) + ((values + (* transform (vari:vec4 vert 0.0 1.0)) + col))) + '(:fragment + ((tc :vec2)) + ((tex :sampler-2d) + (color :vec4)) + ((* color (aref (vari:texture tex tc) 0))))))) + (unless (pre-exit-hook-exists-p :text-gpu-resources) + (pre-exit-hook + :text-gpu-resources + (lambda () + (when *text-shader-program* + (gl:delete-program *text-shader-program*) + (setf *text-shader-program* nil)))))) + (defmethod initialize-instance :after ((text text) &key) - (with-slots (content newlines font vao elem-count shader base-width base-height scale-x scale-y) text - (unless shader - (setf shader - (create-shader - '(:vertex - ((vert :vec2) (col :vec2)) - ((transform :mat4)) - ((values - (* transform (vari:vec4 vert 0.0 1.0)) - col))) - '(:fragment - ((tc :vec2)) - ((tex :sampler-2d) - (color :vec4)) - ((* color (aref (vari:texture tex tc) 0))))))) + (with-slots (content newlines font vao elem-count base-width base-height scale-x scale-y) text + (make-shared-text-gpu-objects) (multiple-value-bind (vao% count%) (cl-fond:compute-text (font-object font) content) (setf vao vao% elem-count count%)) @@ -67,29 +80,27 @@ (1+ newlines)))))) (defmethod cleanup ((text text)) - (with-slots (vao shader) text - (gl:delete-vertex-arrays (list vao)) - (when shader - (gl:delete-program shader)) - (setf vao nil - shader nil))) + (with-slots (vao) text + (when vao + (gl:delete-vertex-arrays (list vao))) + (setf vao nil))) (defmethod render ((text text)) - (with-slots (shader font vao elem-count color) text - (gl:use-program shader) + (with-slots (font vao elem-count color) text + (gl:use-program *text-shader-program*) (gl:active-texture 0) (gl:bind-texture :texture-2d (cl-fond:texture (font-object font))) (gl:program-uniform-matrix-4fv - shader - (gl:get-uniform-location shader "TRANSFORM") + *text-shader-program* + (gl:get-uniform-location *text-shader-program* "TRANSFORM") (projected-matrix text)) (gl:program-uniformi - shader - (gl:get-uniform-location shader "TEX") + *text-shader-program* + (gl:get-uniform-location *text-shader-program* "TEX") 0) (gl:program-uniformfv - shader - (gl:get-uniform-location shader "COLOR") + *text-shader-program* + (gl:get-uniform-location *text-shader-program* "COLOR") color) (gl:bind-vertex-array vao) (%gl:draw-elements :triangles elem-count :unsigned-int 0) |