From 76dea6b6a08736cb615b1d08a33d4c3b4b32d3ad Mon Sep 17 00:00:00 2001 From: Gabriel Pariat Date: Thu, 28 Apr 2022 06:56:12 -0400 Subject: [PATCH] refactoring --- business-tycoon.asd | 1 + src/renderer.lisp | 81 ++++++++++++++++++++------------------------- src/shader.frag | 9 +++++ src/shader.lisp | 47 ++++++++++++++++++++++++++ src/shader.vert | 11 ++++++ 5 files changed, 103 insertions(+), 46 deletions(-) create mode 100644 src/shader.frag create mode 100644 src/shader.lisp create mode 100644 src/shader.vert diff --git a/business-tycoon.asd b/business-tycoon.asd index d1b30e1..1e1c104 100644 --- a/business-tycoon.asd +++ b/business-tycoon.asd @@ -10,6 +10,7 @@ :components ((:module "src" :serial t :components ((:file "package") + (:file "shader") (:file "observable") (:file "renderer") (:file "window") diff --git a/src/renderer.lisp b/src/renderer.lisp index b80ec1f..91b57f7 100644 --- a/src/renderer.lisp +++ b/src/renderer.lisp @@ -31,53 +31,42 @@ void main() (defparameter *vbo* 0) (defparameter *vao* 0) (defparameter *ebo* 0) -(defparameter *shader-program* 0) +(defparameter *shader-program* nil) (defun init-renderer () - (let ((vertex-shader (gl:create-shader :vertex-shader)) - (fragment-shader (gl:create-shader :fragment-shader))) - (setf *shader-program* (gl:create-program)) - (gl:shader-source vertex-shader *vertex-shader-source*) - (gl:compile-shader vertex-shader) - (gl:shader-source fragment-shader *fragment-shader-source*) - (gl:compile-shader fragment-shader) - (gl:attach-shader *shader-program* vertex-shader) - (gl:attach-shader *shader-program* fragment-shader) - (gl:link-program *shader-program*) - (gl:delete-shader vertex-shader) - (gl:delete-shader fragment-shader) - (setf *vbo* (gl:gen-buffer)) - (setf *ebo* (gl:gen-buffer)) - (setf *vao* (gl:gen-vertex-array)) - - (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)) - (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)) - (arr (gl:alloc-gl-array :int (length indices)))) - (dotimes (i (length indices)) - (setf (gl:glaref arr i) (aref indices i))) - (gl:buffer-data :element-array-buffer :static-draw arr) - (gl:free-gl-array arr)) - - (gl:vertex-attrib-pointer 0 3 :float nil - (* 6 (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)) - (cffi:make-pointer (* 3 (cffi:foreign-type-size :float)))) - (gl:enable-vertex-attrib-array 1))) + (setf *shader-program* (shader:load #p"src/shader.vert" #p"src/shader.frag")) + (setf *vbo* (gl:gen-buffer)) + (setf *ebo* (gl:gen-buffer)) + (setf *vao* (gl:gen-vertex-array)) + + (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)) + (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)) + (arr (gl:alloc-gl-array :int (length indices)))) + (dotimes (i (length indices)) + (setf (gl:glaref arr i) (aref indices i))) + (gl:buffer-data :element-array-buffer :static-draw arr) + (gl:free-gl-array arr)) + + (gl:vertex-attrib-pointer 0 3 :float nil + (* 6 (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)) + (cffi:make-pointer (* 3 (cffi:foreign-type-size :float)))) + (gl:enable-vertex-attrib-array 1)) (defun enable-wireframe () (gl:polygon-mode :front-and-back :line)) @@ -98,7 +87,7 @@ void main() ;; (gl:color 1 1 1) ;; (gl:rect -25 -25 25 25)) - (gl:use-program *shader-program*) + (shader:use *shader-program*) (gl:bind-vertex-array *vao*) (gl:draw-elements :triangles (gl:make-null-gl-array :unsigned-int) :count 3) (gl:bind-vertex-array 0) diff --git a/src/shader.frag b/src/shader.frag new file mode 100644 index 0000000..fdeb6ab --- /dev/null +++ b/src/shader.frag @@ -0,0 +1,9 @@ +#version 330 core +out vec4 FragColor; + +in vec3 ourColor; + +void main() +{ + FragColor = vec4(ourColor, 1.0); +} diff --git a/src/shader.lisp b/src/shader.lisp new file mode 100644 index 0000000..7a74a01 --- /dev/null +++ b/src/shader.lisp @@ -0,0 +1,47 @@ +(defpackage #:shader + (:shadow #:load) + (:use #:cl) + (:export #:load #:use #:set-bool #:set-int #:set-float)) + +(in-package :shader) + +(defclass shader () + ((program :accessor shader-program :initarg :program))) + +(defun read-shader-file (path) + (with-open-file (stream path) + (apply #'concatenate + 'string + (loop for line = (read-line stream nil nil) + while line + nconc (list line (string #\Newline)))))) + +(defun load (vertex-shader-path fragment-shader-path) + (let ((vertex-shader-file (read-shader-file vertex-shader-path)) + (fragment-shader-file (read-shader-file fragment-shader-path)) + (vertex-shader (gl:create-shader :vertex-shader)) + (fragment-shader (gl:create-shader :fragment-shader)) + (program (gl:create-program))) + (gl:shader-source vertex-shader vertex-shader-file) + (gl:compile-shader vertex-shader) + (gl:shader-source fragment-shader fragment-shader-file) + (gl:compile-shader fragment-shader) + (format t "~&Vertex shader info log: ~%~a" (gl:get-shader-info-log vertex-shader)) + (format t "~&Fragment shader info log: ~%~a" (gl:get-shader-info-log fragment-shader)) + (gl:attach-shader program vertex-shader) + (gl:attach-shader program fragment-shader) + (gl:link-program program) + (gl:delete-shader vertex-shader) + (gl:delete-shader fragment-shader) + (make-instance 'shader :program program))) + +(defmethod use ((self shader)) + (gl:use-program (shader-program self))) + +(defmacro define-set-uniform (name f) + `(defmethod ,name ((self shader) name value) + (,f (gl:get-uniform-location (shader-program self) name) value))) + +(define-set-uniform set-bool gl:uniformi) +(define-set-uniform set-int gl:uniformi) +(define-set-uniform set-float gl:uniformf) diff --git a/src/shader.vert b/src/shader.vert new file mode 100644 index 0000000..feb3bfc --- /dev/null +++ b/src/shader.vert @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; + +out vec3 ourColor; + +void main() +{ + gl_Position = vec4(aPos, 1.0); + ourColor = aColor; +} \ No newline at end of file