diff options
author | Colin Okay <colin@cicadas.surf> | 2022-07-27 12:07:10 -0500 |
---|---|---|
committer | Colin Okay <colin@cicadas.surf> | 2022-07-27 12:07:10 -0500 |
commit | 11f8d6dad0078464ccbc29cab57908a0923ca447 (patch) | |
tree | ff56d4daf1c2d7cb657587a9fcacdeedfef36307 /src/interactive | |
parent | 0b743b90752bacf31923171af9af0e5ff1f08095 (diff) |
[refactor] represent scene as a vector; [refactor] shared text shader
Diffstat (limited to 'src/interactive')
-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) |