Browse Source

woah une texture!

master
Gabriel Pariat 3 years ago
parent
commit
306778d567
  1. 2
      business-tycoon.asd
  2. 32
      src/renderer.lisp
  3. 5
      src/shader.frag
  4. 5
      src/shader.vert
  5. 41
      src/texture.lisp
  6. BIN
      textures/transparency.png
  7. BIN
      textures/wall.jpg
  8. BIN
      textures/wall.png

2
business-tycoon.asd

@ -6,7 +6,7 @@
:license "Specify license here" :license "Specify license here"
:version "0.0.1" :version "0.0.1"
:serial t :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" :components ((:module "src"
:serial t :serial t
:components ((:file "package") :components ((:file "package")

32
src/renderer.lisp

@ -32,9 +32,11 @@ void main()
(defparameter *vao* 0) (defparameter *vao* 0)
(defparameter *ebo* 0) (defparameter *ebo* 0)
(defparameter *shader-program* nil) (defparameter *shader-program* nil)
(defparameter *texture* nil)
(defun init-renderer () (defun init-renderer ()
(setf *shader-program* (shader:load #p"src/shader.vert" #p"src/shader.frag")) (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 *vbo* (gl:gen-buffer))
(setf *ebo* (gl:gen-buffer)) (setf *ebo* (gl:gen-buffer))
(setf *vao* (gl:gen-vertex-array)) (setf *vao* (gl:gen-vertex-array))
@ -42,17 +44,20 @@ void main()
(gl:bind-vertex-array *vao*) (gl:bind-vertex-array *vao*)
(gl:bind-buffer :array-buffer *vbo*) (gl:bind-buffer :array-buffer *vbo*)
(gl:bind-buffer :element-array-buffer *ebo*) (gl:bind-buffer :element-array-buffer *ebo*)
;; positions colors texture coords
(let* ((vertices #( 0.5 -0.5 0.0 1.0 0.0 0.0 (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 0.5 -0.5 0.0 0.0 1.0 0.0 1.0 0.0 ;; bottom right
0.0 0.5 0.0 0.0 0.0 1.0)) -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)))) (arr (gl:alloc-gl-array :float (length vertices))))
(dotimes (i (length vertices)) (dotimes (i (length vertices))
(setf (gl:glaref arr i) (aref vertices i))) (setf (gl:glaref arr i) (aref vertices i)))
(gl:buffer-data :array-buffer :static-draw arr) (gl:buffer-data :array-buffer :static-draw arr)
(gl:free-gl-array 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)))) (arr (gl:alloc-gl-array :int (length indices))))
(dotimes (i (length indices)) (dotimes (i (length indices))
(setf (gl:glaref arr i) (aref indices i))) (setf (gl:glaref arr i) (aref indices i)))
@ -60,13 +65,17 @@ void main()
(gl:free-gl-array arr)) (gl:free-gl-array arr))
(gl:vertex-attrib-pointer 0 3 :float nil (gl:vertex-attrib-pointer 0 3 :float nil
(* 6 (cffi:foreign-type-size :float)) (* 8 (cffi:foreign-type-size :float))
(cffi:null-pointer)) (cffi:null-pointer))
(gl:enable-vertex-attrib-array 0) (gl:enable-vertex-attrib-array 0)
(gl:vertex-attrib-pointer 1 3 :float nil (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)))) (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 () (defun enable-wireframe ()
(gl:polygon-mode :front-and-back :line)) (gl:polygon-mode :front-and-back :line))
@ -83,13 +92,10 @@ void main()
(defun render () (defun render ()
(gl:clear :color-buffer) (gl:clear :color-buffer)
;; (gl:with-pushed-matrix
;; (gl:color 1 1 1)
;; (gl:rect -25 -25 25 25))
(shader:use *shader-program*) (shader:use *shader-program*)
(texture:bind *texture*)
(gl:bind-vertex-array *vao*) (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) (gl:bind-vertex-array 0)
(glfw:swap-buffers)) (glfw:swap-buffers))

5
src/shader.frag

@ -2,8 +2,11 @@
out vec4 FragColor; out vec4 FragColor;
in vec3 ourColor; in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main() void main()
{ {
FragColor = vec4(ourColor, 1.0); FragColor = texture(ourTexture, TexCoord);
} }

5
src/shader.vert

@ -1,11 +1,14 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord;
void main() void main()
{ {
gl_Position = vec4(aPos, 1.0); gl_Position = vec4(aPos, 1.0);
ourColor = aColor; ourColor = aColor;
} TexCoord = aTexCoord;
}

41
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)))

BIN
textures/transparency.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

BIN
textures/wall.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

BIN
textures/wall.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 KiB

Loading…
Cancel
Save