diff --git a/business-tycoon.asd b/business-tycoon.asd index 1e1c104..6dbb983 100644 --- a/business-tycoon.asd +++ b/business-tycoon.asd @@ -6,7 +6,7 @@ :license "Specify license here" :version "0.0.1" :serial t - :depends-on (#:cl-glfw3 #:cl-opengl #:trivial-main-thread #:cffi) + :depends-on (#:cl-glfw3 #:cl-opengl #:trivial-main-thread #:cffi #:png-read) :components ((:module "src" :serial t :components ((:file "package") diff --git a/src/renderer.lisp b/src/renderer.lisp index 91b57f7..6489268 100644 --- a/src/renderer.lisp +++ b/src/renderer.lisp @@ -32,9 +32,11 @@ void main() (defparameter *vao* 0) (defparameter *ebo* 0) (defparameter *shader-program* nil) +(defparameter *texture* nil) (defun init-renderer () (setf *shader-program* (shader:load #p"src/shader.vert" #p"src/shader.frag")) + (setf *texture* (texture:load #p"textures/wall.png")) (setf *vbo* (gl:gen-buffer)) (setf *ebo* (gl:gen-buffer)) (setf *vao* (gl:gen-vertex-array)) @@ -42,17 +44,20 @@ void main() (gl:bind-vertex-array *vao*) (gl:bind-buffer :array-buffer *vbo*) (gl:bind-buffer :element-array-buffer *ebo*) - - (let* ((vertices #( 0.5 -0.5 0.0 1.0 0.0 0.0 - -0.5 -0.5 0.0 0.0 1.0 0.0 - 0.0 0.5 0.0 0.0 0.0 1.0)) + ;; positions colors texture coords + (let* ((vertices #( 0.5 0.5 0.0 1.0 0.0 0.0 1.0 1.0 ;; top right + 0.5 -0.5 0.0 0.0 1.0 0.0 1.0 0.0 ;; bottom right + -0.5 -0.5 0.0 0.0 0.0 1.0 0.0 0.0 ;; bottom left + -0.5 0.5 0.0 1.0 1.0 0.0 0.0 1.0 ;; top left + )) (arr (gl:alloc-gl-array :float (length vertices)))) (dotimes (i (length vertices)) (setf (gl:glaref arr i) (aref vertices i))) (gl:buffer-data :array-buffer :static-draw arr) (gl:free-gl-array arr)) - (let* ((indices #(0 1 2)) + (let* ((indices #(0 1 3 + 1 2 3)) (arr (gl:alloc-gl-array :int (length indices)))) (dotimes (i (length indices)) (setf (gl:glaref arr i) (aref indices i))) @@ -60,13 +65,17 @@ void main() (gl:free-gl-array arr)) (gl:vertex-attrib-pointer 0 3 :float nil - (* 6 (cffi:foreign-type-size :float)) + (* 8 (cffi:foreign-type-size :float)) (cffi:null-pointer)) (gl:enable-vertex-attrib-array 0) (gl:vertex-attrib-pointer 1 3 :float nil - (* 6 (cffi:foreign-type-size :float)) + (* 8 (cffi:foreign-type-size :float)) (cffi:make-pointer (* 3 (cffi:foreign-type-size :float)))) - (gl:enable-vertex-attrib-array 1)) + (gl:enable-vertex-attrib-array 1) + (gl:vertex-attrib-pointer 2 2 :float nil + (* 8 (cffi:foreign-type-size :float)) + (cffi:make-pointer (* 6 (cffi:foreign-type-size :float)))) + (gl:enable-vertex-attrib-array 2)) (defun enable-wireframe () (gl:polygon-mode :front-and-back :line)) @@ -83,13 +92,10 @@ void main() (defun render () (gl:clear :color-buffer) - ;; (gl:with-pushed-matrix - ;; (gl:color 1 1 1) - ;; (gl:rect -25 -25 25 25)) - (shader:use *shader-program*) + (texture:bind *texture*) (gl:bind-vertex-array *vao*) - (gl:draw-elements :triangles (gl:make-null-gl-array :unsigned-int) :count 3) + (gl:draw-elements :triangles (gl:make-null-gl-array :unsigned-int) :count 6) (gl:bind-vertex-array 0) (glfw:swap-buffers)) diff --git a/src/shader.frag b/src/shader.frag index fdeb6ab..be3d118 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -2,8 +2,11 @@ out vec4 FragColor; in vec3 ourColor; +in vec2 TexCoord; + +uniform sampler2D ourTexture; void main() { - FragColor = vec4(ourColor, 1.0); + FragColor = texture(ourTexture, TexCoord); } diff --git a/src/shader.vert b/src/shader.vert index feb3bfc..22041cd 100644 --- a/src/shader.vert +++ b/src/shader.vert @@ -1,11 +1,14 @@ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; +layout (location = 2) in vec2 aTexCoord; out vec3 ourColor; +out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); ourColor = aColor; -} \ No newline at end of file + TexCoord = aTexCoord; +} diff --git a/src/texture.lisp b/src/texture.lisp new file mode 100644 index 0000000..1a8a6c9 --- /dev/null +++ b/src/texture.lisp @@ -0,0 +1,41 @@ +(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))) diff --git a/textures/transparency.png b/textures/transparency.png new file mode 100644 index 0000000..06709a4 Binary files /dev/null and b/textures/transparency.png differ diff --git a/textures/wall.jpg b/textures/wall.jpg new file mode 100644 index 0000000..8e68965 Binary files /dev/null and b/textures/wall.jpg differ diff --git a/textures/wall.png b/textures/wall.png new file mode 100644 index 0000000..f16e25f Binary files /dev/null and b/textures/wall.png differ