Ready for performance testing
[lambdamundo.git] / npc.lisp
blob4a9c4f8645c652f0c7b0220f830076610e56cec9
3 (in-package :lambdamundo)
5 (defclass npc (actor)
6 ((mesh :initarg :mesh :accessor mesh-of)))
9 ;; simple rendering method for debugging
10 (defmethod render ((self mixamesh:simple-mesh))
11 ;; (break)
12 (iterate
13 (for (values a b c) in-triangles (faces-of self))
14 (with-vertex3d-aref
15 ((vertices-of self) a
16 (ax ay az aw))
17 (with-vertex3d-aref
18 ((vertices-of self) b
19 (bx by bz bw))
20 (with-vertex3d-aref
21 ((vertices-of self) c
22 (cx cy cz cw))
23 (gl:with-begin gl:+lines+
24 (gl:vertex-3f ax ay az)
25 (gl:vertex-3f bx by bz)
26 (gl:vertex-3f bx by bz)
27 (gl:vertex-3f cx cy cz)
28 (gl:vertex-3f cx cy cz)
29 (gl:vertex-3f ax ay az)))))))
31 (defun render-npc (a)
32 (flet
33 ((render-x-axis ()
34 ;; x
35 (gl:color-3f 1.0 0.0 0.0)
36 (with-vertex3d
37 (location-of a)
38 (x y z w)
39 (gl:vertex-3f x y z))
40 (with-vector3d
41 (vector3d-sum
42 (vertex3d-vector3d
43 (location-of a))
44 (cross-of a))
45 (x y z)
46 (gl:vertex-3f x y z)))
48 (render-y-axis ()
49 ;; y
50 (gl:color-3f 0.0 1.0 0.0)
51 (with-vertex3d
52 (location-of a)
53 (x y z w)
54 (gl:vertex-3f x y z))
55 (with-vector3d
56 (vector3d-sum
57 (vertex3d-vector3d
58 (location-of a))
59 (up-of a))
60 (x y z)
61 (gl:vertex-3f x y z)))
64 (render-z-axis ()
65 ;; z
66 (gl:color-3f 0.0 0.0 1.0)
67 (with-vertex3d
68 (location-of a)
69 (x y z w)
70 (gl:vertex-3f x y z))
71 (with-vector3d
72 (vector3d-sum
73 (vertex3d-vector3d
74 (location-of a))
75 (direction-of a))
76 (x y z)
77 (gl:vertex-3f x y z))))
80 (gl:with-begin gl:+lines+
81 (render-x-axis)
82 (render-y-axis)
83 (render-z-axis))))
85 (defun render-aabb (mesh-aabb)
86 (with-aabb
87 (aabb mesh-aabb)
88 (minx maxx miny maxy minz maxz)
89 (gl:with-begin gl:+lines+
90 (gl:color-3f 1.0 1.0 1.0)
92 (gl:vertex-3f minx miny minz)
93 (gl:vertex-3f maxx miny minz)
95 (gl:vertex-3f maxx miny minz)
96 (gl:vertex-3f maxx miny maxz)
98 (gl:vertex-3f maxx miny maxz)
99 (gl:vertex-3f minx miny maxz)
101 (gl:vertex-3f minx miny maxz)
102 (gl:vertex-3f minx miny minz)
104 (gl:vertex-3f minx maxy minz)
105 (gl:vertex-3f maxx maxy minz)
107 (gl:vertex-3f maxx maxy minz)
108 (gl:vertex-3f maxx maxy maxz)
110 (gl:vertex-3f maxx maxy maxz)
111 (gl:vertex-3f minx maxy maxz)
113 (gl:vertex-3f minx maxy maxz)
114 (gl:vertex-3f minx maxy minz)
116 (gl:vertex-3f minx miny minz)
117 (gl:vertex-3f minx maxy minz)
119 (gl:vertex-3f maxx miny minz)
120 (gl:vertex-3f maxx maxy minz)
122 (gl:vertex-3f maxx miny maxz)
123 (gl:vertex-3f maxx maxy maxz)
125 (gl:vertex-3f minx miny maxz)
126 (gl:vertex-3f minx maxy maxz))))
128 (let ((modelview-matrix (new-matrix44)))
129 (defmethod render ((n npc))
130 (flet ((npc-modelview-matrix ()
131 (gl:matrix-mode gl:+modelview+)
132 (gl:load-matrix-f
133 (setf modelview-matrix
134 (make-matrix44
135 (transpose-matrix44
136 (with-vertex3d
137 (location-of n)
138 (x y z w)
139 (translation-matrix44 x y z))))))))
140 ;; (npc-modelview-matrix)
141 (gl:matrix-mode gl:+modelview+)
142 (gl:push-matrix)
143 (with-vertex3d (location-of n) (x y z w)
144 (gl:translate-f x y z))
145 (gl:scale-f 0.01 0.01 0.01)
146 ;; (render-npc n)
147 (render (or (gethash (mesh-of n) mixamesh:*compiled-meshes*)
148 (gethash (mesh-of n) mixamesh:*meshes*)))
149 (let ((aabb (gethash (mesh-of n) mixamesh:*bounding-boxes*)))
150 (when aabb
151 (render-aabb aabb)))
152 (gl:pop-matrix))))
154 (defmethod make-npc (mesh)
155 (let ((result
156 (make-actor 'npc
157 :mesh mesh)))
158 result))
160 (defmethod destroy ((n npc))
161 (remhash (mesh-of n) *bounding-boxes*)
162 (let ((mesh (gethash (mesh-of n) *meshes*))
163 (compiled-mesh (gethash (mesh-of n) *compiled-meshes*)))
164 (when mesh
165 (destroy mesh)
166 (remhash (mesh-of n) *meshes*))
167 (when compiled-mesh
168 (remhash (mesh-of n) *compiled-meshes*)
169 (destroy compiled-mesh))))