Changing to lambdamundo
[lambdamundo.git] / drawing.lisp
blob49c74e41e98f5af460a66c9e067db5414e13c7df
1 (in-package :lambdamundo)
3 ;; gl init and de-init
5 (defun begin-gl ()
6 (gl:enable gl:+depth-test+)
7 (gl:depth-func gl:+less+)
8 (gl:disable gl:+cull-face+)
9 (gl:disable gl:+lighting+))
11 (defun end-gl ())
13 ;; drawing --------------------
16 (defparameter *draw-fns* (make-hash-table :test 'equalp)
17 "An table of functions called in order to render the scene")
19 (defparameter *draw-array* (make-array 0 :adjustable t :fill-pointer 0)
20 "An array that indexes *draw-fns* to establish draw order" )
22 (defun reset-draw-fns ()
23 "Reset all drawing functions."
24 (setf *draw-fns* (make-hash-table :test 'equalp))
25 (setf *draw-array* (make-array 0 :adjustable t :fill-pointer 0)))
27 (defun extend-draw-array (name priority)
28 "If the name is in the array, adjust priority, else add it to the array"
29 (assert (not (null (gethash name *draw-fns*))) (name) "~S is not a drawable")
30 (let
31 ((draw-priority-pos
32 (position-if #'(lambda (x) (equal (car x) name)) *draw-array*)))
33 (if draw-priority-pos
34 (setf (aref *draw-array* draw-priority-pos) (cons name priority))
35 (vector-push-extend (cons name priority) *draw-array*))))
37 (defmacro make-draw-function (name priority &body forms)
38 "Macro to wrap a draw function body and add it to the arrays"
39 `(progn
40 (setf (gethash ,name *draw-fns*)
41 (compile nil '(lambda () ,@forms)))
42 (extend-draw-array ,name ,priority)
43 (sort *draw-array* #'(lambda (a b)
44 (< (cdr a) (cdr b))))))
46 (defun remove-draw-function (name)
47 (remhash name *draw-fns*))
49 (defun render ()
50 (gl:clear (logior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
51 (iterate
52 (for entry in-vector *draw-array*)
53 (let ((draw-fn
54 (gethash (car entry) *draw-fns*)))
55 (when draw-fn (funcall draw-fn)))))
58 (make-draw-function
59 "testcube" 1
60 (let ((vertices
61 (list
62 '(1.0 1.0 1.0)
63 '(1.0 1.0 0.0)
64 '(1.0 -1.0 1.0)
65 '(1.0 -1.0 0.0)
66 '(-1.0 1.0 1.0)
67 '(-1.0 1.0 0.0)
68 '(-1.0 -1.0 1.0)
69 '(-1.0 -1.0 0.0)))
70 (polys
71 (list
72 '(2 1 3 4)
73 '(5 6 7 8)
74 '(1 2 6 5)
75 '(4 3 7 8)
76 '(3 1 5 7)
77 '(2 4 8 6))))
78 (with-camera *camera*
79 (gl:with-begin gl:+quads+
80 (gl:color-3f 0.0 1.0 0.0)
81 (iterate
82 (for poly in polys)
83 (iterate
84 (for vertex in poly)
85 (apply #'gl:vertex-3f (nth (1- vertex) vertices))))))))
87 ;; (make-draw-function
88 ;; "triangle" 1
89 ;; (gl:with-begin gl:+triangles+
90 ;; (gl:color-3f 1.0 0.0 0.0) (gl:vertex-3f 1.0 0.0 -5.0)
91 ;; (gl:color-3f 0.0 1.0 0.0) (gl:vertex-3f -1.0 1.0 -5.0)
92 ;; (gl:color-3f 0.0 0.0 0.0) (gl:vertex-3f -1.0 -1.0 -5.0)))
94 ;; there will be a draw function per class of object