Browse Source

refactoring

master
Gabriel Pariat 3 years ago
parent
commit
76dea6b6a0
  1. 1
      business-tycoon.asd
  2. 19
      src/renderer.lisp
  3. 9
      src/shader.frag
  4. 47
      src/shader.lisp
  5. 11
      src/shader.vert

1
business-tycoon.asd

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
:components ((:module "src"
:serial t
:components ((:file "package")
(:file "shader")
(:file "observable")
(:file "renderer")
(:file "window")

19
src/renderer.lisp

@ -31,21 +31,10 @@ void main() @@ -31,21 +31,10 @@ 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 *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))
@ -77,7 +66,7 @@ void main() @@ -77,7 +66,7 @@ void main()
(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)))
(gl:enable-vertex-attrib-array 1))
(defun enable-wireframe ()
(gl:polygon-mode :front-and-back :line))
@ -98,7 +87,7 @@ void main() @@ -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)

9
src/shader.frag

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
void main()
{
FragColor = vec4(ourColor, 1.0);
}

47
src/shader.lisp

@ -0,0 +1,47 @@ @@ -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)

11
src/shader.vert

@ -0,0 +1,11 @@ @@ -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;
}
Loading…
Cancel
Save