-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniforms.lisp
111 lines (92 loc) · 4.17 KB
/
uniforms.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
;; uniforms.lisp
;;
;; Copyright (c) 2022 Jeremiah LaRocco <[email protected]>
(in-package #:simple-gl)
;; TODO: Consider creating float-uniform, mat4-uniform, etc. subclasses that call the
;; correct gl:uniform* functions, and remove the big cond in use-uniform.
(defclass uniform ()
((name :initarg :name
:type string
:documentation "Uniform variable name.")
(type :initarg :type
:documentation "Uniform variable type.")
(value :initarg :value
:initform t
:type t
:documentation "Current uniform value.")
(modified :initform t
:documentation "Whether the uniform's current value has been sent to OpenGL."))
(:documentation "A uniform variable parameter to a shader."))
(defmethod show-info ((uniform uniform) &key (indent 0))
(let ((this-ws (indent-whitespace indent)))
(with-slots (name type value modified) uniform
(format t "~a~a ~a = ~a ~a~%" this-ws type name value (if modified "modified" "not modified")))))
(defmethod print-object ((object uniform) stream)
(with-slots (name type value modified) object
(format stream "~a ~a = ~a (~a)~%" type name value (if modified "modified" "not modified"))))
(defmethod update ((uniform uniform) elapsed-seconds)
(declare (ignorable uniform elapsed-seconds))
nil)
(defmethod use-uniform ((uniform uniform) program)
"Bind the uniform's value in the program."
(with-slots (name type value modified) uniform
(when modified
(let ((location (gl:get-uniform-location program name)))
;; Only assign values to uniforms that are used by the program
(when (and modified (>= location 0))
(cond (
(eq :mat4 type)
(gl:program-uniform-matrix program location
4
(vector (marr4 value))
t))
((eq :mat3 type)
(gl:program-uniform-matrix program location
3
(vector (marr3 value))
t))
((eq :dmat4 type)
(gl:program-uniform-matrix program location
4
(map 'vector (lambda (v) (coerce v 'double-float)) (vector (marr4 value)))
t))
((eq :dmat3 type)
(gl:program-uniform-matrix program location
3
(map 'vector (lambda (v) (coerce v 'double-float)) (vector (marr3 value)))
t))
((eq :int type)
(gl:program-uniformi program location
value))
((eq :float type)
(gl:program-uniformf program location
value))
((eq :vec2 type)
(gl:program-uniformf program location
(vx value)
(vy value)))
((eq :vec3 type)
(gl:program-uniformf program location
(vx value)
(vy value)
(vz value)))
((eq :vec4 type)
(gl:program-uniformf program location
(vx value)
(vy value)
(vz value)
(vw value)))
(t
(error "Don't know how to set type ~a" type)))
(setf modified nil))))))
(defmethod set-value (uniform new-value &optional (new-type nil))
(with-slots (value modified type) uniform
(setf value new-value)
(when new-type
(setf type new-type))
(setf modified t)))
(defmethod get-value (uniform)
(with-slots (value modified type) uniform
value))
(defmethod cleanup ((uniform uniform))
t)