(defpackage #:texture (:shadow #:load) (:use #:cl) (:export #:load #:bind)) (in-package :texture) (defclass texture () ((id :accessor texture-id :initarg :id))) (defun load (png-image-path) (let* ((texture (gl:gen-texture)) (image (png-read:read-png-file png-image-path)) (image-data (png-read:image-data image)) (image-dimensions (array-dimensions image-data)) (data (loop for w below (first image-dimensions) nconc (loop for h below (second image-dimensions) nconc (loop for c below (third image-dimensions) collect (aref image-data w h c))))) (format (case (png-read:colour-type image) (:truecolor-alpha :rgba) (:truecolor :rgb)))) (gl:bind-texture :texture-2d texture) (gl:tex-parameter :texture-2d :texture-wrap-s :repeat) (gl:tex-parameter :texture-2d :texture-wrap-t :repeat) (gl:tex-parameter :texture-2d :texture-min-filter :linear) (gl:tex-parameter :texture-2d :texture-mag-filter :linear) (gl:tex-image-2d :texture-2d 0 :rgba (png-read:width image) (png-read:height image) 0 format :unsigned-byte (coerce data 'vector)) (gl:generate-mipmap :texture-2d) (make-instance 'texture :id texture))) (defmethod bind ((self texture)) (gl:bind-texture :texture-2d (texture-id self)))