aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/interactive-units/bitmap.lisp
diff options
context:
space:
mode:
authorColin Okay <colin@cicadas.surf>2022-06-29 11:54:24 -0500
committerColin Okay <colin@cicadas.surf>2022-06-29 11:54:24 -0500
commit82f71b0d13788b1cff9a24c5b652effd11631523 (patch)
treef0ec127b2f10f46029983ee95b6c72ef29bc504c /src/interactive-units/bitmap.lisp
parent4d1ee56c96ce254134b692f0e3b3271c87a42b54 (diff)
[refactor] [structure] modularized project file structure
Diffstat (limited to 'src/interactive-units/bitmap.lisp')
-rw-r--r--src/interactive-units/bitmap.lisp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/interactive-units/bitmap.lisp b/src/interactive-units/bitmap.lisp
new file mode 100644
index 0000000..95dfff5
--- /dev/null
+++ b/src/interactive-units/bitmap.lisp
@@ -0,0 +1,71 @@
+;;;; bitmap.lisp
+
+(in-package #:wheelwork)
+
+(defclass/std bitmap (unit interactive)
+ ((texture :ri :std (error "A bitmap requires a texture."))
+ (vao shader :with :r :static)))
+
+(defmethod initialize-instance :after ((bitmap bitmap) &key)
+ (with-slots (vao shader base-width base-height texture) bitmap
+ (setf base-height (texture-height texture)
+ base-width (texture-width texture))
+ (unless shader
+ (setf shader
+ (create-shader
+ '(:vertex
+ ((vert :vec2))
+ ((transform :mat4))
+ ((values
+ (* transform (vari:vec4 vert 0.0 1.0))
+ vert))) ;color
+ '(:fragment
+ ((tc :vec2))
+ ((tex :sampler-2d))
+ ((let ((frag (vari:texture tex tc)))
+ (if (< (aref frag 3) 0.01)
+ (vari:discard)
+ frag))))))
+ (gl:program-uniformi
+ shader
+ (gl:get-uniform-location shader "TEX")
+ 0))
+ (unless vao
+ (setf vao (gl:gen-vertex-array))
+ (gl:bind-vertex-array vao)
+ (let ((vbo (gl:gen-buffer)))
+ (with-gl-array (verts :float
+ 0.0 1.0
+ 1.0 0.0
+ 0.0 0.0
+
+ 0.0 1.0
+ 1.0 1.0
+ 1.0 0.0 )
+ (gl:bind-buffer :array-buffer vbo)
+ (gl:buffer-data :array-buffer :static-draw verts)))
+ (gl:enable-vertex-attrib-array 0)
+ (gl:vertex-attrib-pointer 0 2 :float 0 (* +float-size+ 2) 0)
+ (gl:bind-buffer :array-buffer 0)
+ (gl:bind-vertex-array 0))))
+
+(defmethod cleanup ((bitmap bitmap))
+ (with-slots (vao shader) bitmap
+ (when vao
+ (gl:delete-vertex-arrays (list vao)))
+ (when shader
+ (gl:delete-program shader))
+ (setf vao nil
+ shader nil)))
+(defmethod render ((bitmap bitmap))
+ (with-slots (texture vao shader) bitmap
+ (gl:active-texture 0)
+ (gl:bind-texture :texture-2d (texture-id texture))
+ (gl:use-program shader)
+ (gl:program-uniform-matrix-4fv
+ shader
+ (gl:get-uniform-location shader "TRANSFORM")
+ (projected-matrix bitmap))
+ (gl:bind-vertex-array vao)
+ (gl:draw-arrays :triangles 0 6)
+ (gl:bind-vertex-array 0)))