Permalink
Browse files

Large scale refactoring

- Introduce shader-entity and shader-entity-class to allow entities to be drawn with shaders without having to be subjects.
- shader-subject / shader-subject-class are now the logical combinations of subject and shader-entity
- subject.lisp merged with helpers.lisp and definitions rewritten to shader-entities where applicable
- Remove explicit instances list from shader-subject-classes, instead opting for the make-instances-obsolete / update-instance-for-redefined-class
- Rename cascade-option-changes to compute-effective-handlers / compute-effective-shaders respectively.
  • Loading branch information...
Shinmera committed Aug 17, 2017
1 parent fc41a9a commit 8541c82c6603131451b9602142baca03a1d80251
Showing with 325 additions and 283 deletions.
  1. +1 −1 asset-pool.lisp
  2. +17 −17 assets/font.lisp
  3. +100 −0 helpers.lisp
  4. +25 −22 package.lisp
  5. +144 −0 shader-entity.lisp
  6. +2 −97 shader-subject.lisp
  7. +8 −8 sprite.lisp
  8. +19 −22 subject.lisp
  9. +0 −107 subjects.lisp
  10. +6 −6 trial.asd
  11. +3 −3 ui/elements.lisp
View
@@ -101,7 +101,7 @@
(asset (gensym "ASSET"))
(inputs (substitute-asset-paths inputs pool)))
`(let ((,scene (when (window :main) (scene (window :main))))
(,asset (asset ',pool ',name)))
(,asset (asset ',pool ',name NIL)))
(cond ((and ,asset (eql (type-of ,asset) ',type))
(reinitialize-instance ,asset :inputs (list ,@inputs) ,@initargs)
(when (and ,scene (resource ,asset))
View
@@ -38,7 +38,7 @@
:size (size asset)))
(v:debug :trial.asset "Loaded font ~a" (first (coerced-inputs asset))))
(define-shader-subject text (vertex-subject textured-subject located-entity)
(define-shader-entity text (vertex-entity textured-entity located-entity)
((font :initarg :font :accessor font)
(text :initarg :text :accessor text)
(color :initarg :color :initform (vec 0 0 0 1) :accessor color)
@@ -67,11 +67,11 @@
(,vbo :size 2 :stride 16 :offset 8)
,ebo)))))
(defmethod paint :before ((subject text) (pass shader-pass))
(let ((program (shader-program-for-pass pass subject)))
(defmethod paint :before ((entity text) (pass shader-pass))
(let ((program (shader-program-for-pass pass entity)))
(setf (uniform program "text_color")
(color subject)))
(let ((r (/ (size subject) (size (font subject)))))
(color entity)))
(let ((r (/ (size entity) (size (font entity)))))
(scale-by r r r)))
(define-class-shader (text :fragment-shader)
@@ -82,22 +82,22 @@ void main(){
color = text_color*intensity;
}")
(defmethod load progn ((subject text))
(load (slot-value subject 'vbo))
(load (slot-value subject 'ebo))
(load (vertex-array subject))
(setf (font subject) (load (font subject))))
(defmethod load progn ((entity text))
(load (slot-value entity 'vbo))
(load (slot-value entity 'ebo))
(load (vertex-array entity))
(setf (font entity) (load (font entity))))
(defmethod offload progn ((subject text))
(offload (vertex-array subject)))
(defmethod offload progn ((entity text))
(offload (vertex-array entity)))
(defmethod (setf font) :after (font (subject text))
(defmethod (setf font) :after (font (entity text))
(when (resource font)
(setf (resource (texture subject)) (cl-fond:texture (resource font)))
(setf (text subject) (text subject))))
(setf (resource (texture entity)) (cl-fond:texture (resource font)))
(setf (text entity) (text entity))))
(defmethod (setf text) :before (text (subject text))
(let ((vao (vertex-array subject))
(defmethod (setf text) :before (text (entity text))
(let ((vao (vertex-array entity))
(vbo (slot-value subject 'vbo))
(ebo (slot-value subject 'ebo))
(font (resource (font subject))))
View
@@ -50,3 +50,103 @@
(with-pushed-matrix ()
(translate (pivot obj))
(call-next-method)))
(define-subject clocked-subject (clock)
())
(define-handler (clocked-subject advance-time tick) (ev)
(flare:update clocked-subject))
(define-shader-entity vertex-entity ()
((vertex-array :initarg :vertex-array :accessor vertex-array)
(vertex-form :initarg :vertex-form :initform :triangles :accessor vertex-form)))
(defmethod paint ((subject vertex-entity) (pass shader-pass))
(let ((shader (shader-program-for-pass pass subject)))
(setf (uniform shader "model_matrix") (model-matrix))
(setf (uniform shader "view_matrix") (view-matrix))
(setf (uniform shader "projection_matrix") (projection-matrix)))
(let ((vao (vertex-array subject)))
(gl:bind-vertex-array (resource vao))
(%gl:draw-elements (vertex-form subject) (size vao) :unsigned-int 0)
(gl:bind-vertex-array 0)))
(defmethod load progn ((subject vertex-entity))
(load (vertex-array subject)))
(define-class-shader (vertex-entity :vertex-shader)
"layout (location = 0) in vec3 position;
uniform mat4 model_matrix;
uniform mat4 view_matrix;
uniform mat4 projection_matrix;
void main(){
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(position, 1.0f);
}")
;; FIXME: Make sure to coerce for proper colour format!
(define-shader-entity colored-entity ()
((color :initarg :color :initform (vec 0 0 1 1) :accessor color)))
(defmethod paint :before ((obj colored-entity) (pass shader-pass))
(let ((shader (shader-program-for-pass pass obj)))
(setf (uniform shader "objectcolor") (color obj))))
(define-class-shader (colored-entity :fragment-shader)
"uniform vec4 objectcolor;
out vec4 color;
void main(){
color *= objectcolor;
}")
(define-shader-entity vertex-colored-entity ()
())
(define-class-shader (vertex-colored-entity :vertex-shader)
"layout (location = 2) in vec4 in_vertexcolor;
out vec4 vertexcolor;
void main(){
vertexcolor = in_vertexcolor;
}")
(define-class-shader (vertex-colored-entity :fragment-shader)
"in vec4 vertexcolor;
out vec4 color;
void main(){
color *= vertexcolor;
}")
(define-shader-entity textured-entity ()
((texture :initform NIL :initarg :texture :accessor texture)))
(defmethod paint :around ((obj textured-entity) target)
(let ((tex (texture obj)))
(when tex
(gl:active-texture :texture0)
(gl:bind-texture (target tex) (resource tex))
(call-next-method)
(gl:bind-texture (target tex) 0))))
(defmethod load progn ((subject textured-entity))
(load (texture subject)))
(define-class-shader (textured-entity :vertex-shader)
"layout (location = 1) in vec2 in_texcoord;
out vec2 texcoord;
void main(){
texcoord = in_texcoord;
}")
(define-class-shader (textured-entity :fragment-shader)
"in vec2 texcoord;
out vec4 color;
uniform sampler2D texture_image;
void main(){
color *= texture(texture_image, texcoord);
}")
View
@@ -268,7 +268,16 @@
#:rotation
#:axis-rotated-entity
#:pivoted-entity
#:pivot)
#:pivot
#:clocked-subject
#:vertex-entity
#:vertex-array
#:vertex-form
#:colored-entity
#:color
#:vertex-colored-entity
#:textured-subject
#:texture)
;; input.lisp
(:export
#:input-event
@@ -398,6 +407,19 @@
#:selectable
#:selection-color
#:find-new-selection-color)
;; shader-entity.lisp
(:export
#:shader-entity-class
#:effective-shaders
#:direct-shaders
#:inhibit-shaders
#:compute-effective-shaders
#:class-shader
#:remove-class-shader
#:make-class-shader-program
#:define-class-shader
#:shader-entity
#:define-shader-entity)
;; shader-pass.lisp
(:export
#:shader-pass-class
@@ -430,12 +452,6 @@
;; shader-subject.lisp
(:export
#:shader-subject-class
#:effective-shaders
#:direct-shaders
#:class-shader
#:remove-class-shader
#:make-class-shader-program
#:define-class-shader
#:shader-subject
#:define-shader-subject)
;; skybox.lisp
@@ -444,7 +460,7 @@
#:texture)
;; sprite.lisp
(:export
#:sprite-subject
#:sprite-entity
#:tile
#:size
#:animated-sprite-subject
@@ -463,27 +479,14 @@
(:export
#:subject-class-redefined
#:subject-class
#:subject-class
#:effective-handlers
#:instances
#:cascade-option-changes
#:compute-effective-handlers
#:subject
#:event-loop
#:regenerate-handlers
#:define-subject
#:define-handler
#:define-generic-handler)
;; subjects.lisp
(:export
#:clocked-subject
#:vertex-subject
#:vertex-array
#:vertex-form
#:colored-subject
#:color
#:vertex-colored-subject
#:textured-subject
#:texture)
;; toolkit.lisp
(:export
#:finalize
Oops, something went wrong.

0 comments on commit 8541c82

Please sign in to comment.