2 (in-package :lambdamundo
)
4 ;; camera -- this could be an actor..
6 (defclass camera
(actor)
7 ((speed :initform
1.0 :accessor speed-of
:type single-float
)
8 (zoom-speed-of :initform
1.0 :accessor zoom-speed-of
:type single-float
)))
11 (def-tuple-op translate-scaled-vertex
12 ((p vertex3d
(x y z w
))
13 (v vector3d
(vx vy vz
))
15 "Move the given vertex in the direction of the given vector, uniformly scaled by delta."
23 (defmethod pan ((cam camera
) dx dy
)
24 "Move the camera by dx along cross vector and by dy along z vector"
25 (setf (location-of cam
)
26 (translate-scaled-vertex
29 (* dy
(- (speed-of cam
)))))
30 (setf (location-of cam
)
31 (translate-scaled-vertex
34 (* dx
(speed-of cam
)))))
36 (defmethod dolly ((cam camera
) dx dy
)
37 (setf (orientation-of cam
)
40 (angle-axis-quaternion
41 (angle-axis* 0.0 0.0 1.0 (* *mouse-sensitivity
* dy
)))))
42 (setf (orientation-of cam
)
48 (angle-axis-quaternion
49 (angle-axis* cx cy cz
(* *mouse-sensitivity
* dy
)))))))
51 (defmethod zoom ((cam camera
) dz
)
52 (setf (location-of cam
)
56 (vertex3d* px py
(- pz
(* (zoom-speed-of cam
))) pw
))))
59 (defparameter *modelview-debug
* (make-array 16 :element-type
'(single-float)))
61 ;; (defmethod camera-modelview-matrix ((cam camera))
62 ;; (gl:matrix-mode gl:+modelview+)
65 ;; (vector3d-sum (vertex3d-vector3d (location-of cam)) (direction-of cam))
68 ;; (vertex3d-vector3d (location-of cam))
73 ;; #| (format *debug-io* "Look at~&Position ~A ~A ~A~&LookAt ~A ~A ~AUp ~A ~A ~A" px py pz lx ly lz ux uy uz) |#
81 (def-tuple-op gl-load-matrix44
82 ((mat matrix44
(e00 e01 e02 e03
86 "Load a matrix44 onto the opengl stack"
87 (cffi:with-foreign-object
(glmat :float
16)
89 (setf (cffi:mem-aref glmat
:float i
) (matrix44-aref mat i
)))
90 (gl::%load-matrix-f glmat
)))
93 (defmethod camera-modelview-matrix ((cam camera
))
94 "Return a matrix44 equivalent to glu:look-at"
95 (gl:matrix-mode gl
:+modelview
+)
110 (matrix44* sx sy sz lx
113 0.0 0.0 0.0 1.0)))))))
117 (defmacro with-camera
(camera &body forms
)
119 (camera-modelview-matrix ,camera
)
122 (defun make-camera () (make-instance 'camera
123 :location
(make-vertex3d* 0.0 0.0 -
5.0 1.0)
124 :orientation
(make-quaternion* 0.0 0.0 0.0 1.0)))
126 (defparameter *camera
* nil
)
128 (defun set-current-camera (cam) (setf *camera
* cam
))
130 (defun reset-camera ()
131 (setf *camera
* (make-instance 'camera
132 :position
(make-vertex3d* 0.0 0.0 -
5.0 1.0)
133 :orientation
(make-quaternion* 0.0 0.0 0.0 1.0))))
137 ;; camera change far plane function
138 ;; (make-key-function
140 ;; (if (eql (glfw:get-key glfw:+key-lshift+) glfw:+press+)
141 ;; (incf *z-far* 1.0)
142 ;; (decf *z-far* 1.0)))
144 (defmethod render ((c camera
))
145 (camera-modelview-matrix c
)
146 (gl:get-floatv gl
:+modelview-matrix
+ *modelview-debug
*))