forked from cbaggers/play-with-verts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.lisp
79 lines (65 loc) · 2.35 KB
/
camera.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
(in-package #:play-with-verts)
;;------------------------------------------------------------
(defclass camera ()
((pos :initform (v! -0.43 25.33 43.20)
:accessor pos)
(rot :initform (v! 0.97 -0.20 -0.01 0.0)
:accessor rot)
(near :initform 1f0
:accessor near)
(far :initform 400f0
:accessor far)
(frame-size :initform nil
:accessor frame-size)))
(defclass orthographic-camera (camera) ())
(defclass perspective-camera (camera)
((fov :initform 45f0 :accessor fov)))
(defparameter *camera* (make-instance 'perspective-camera))
(defparameter *camera-1* (make-instance 'orthographic-camera))
(defparameter *current-camera* *camera*)
(defun get-world->view-space (camera)
(m4:* (q:to-mat4 (q:inverse (rot camera)))
(m4:translation (v3:negate (pos camera)))))
(defmethod update ((camera camera) dt)
(let ((factor 10))
(when (keyboard-button (keyboard) key.lshift)
(setf factor 20))
(when (keyboard-button (keyboard) key.w)
(v3:incf (pos camera)
(v3:*s (q:to-direction (rot camera))
(* factor dt))))
(when (keyboard-button (keyboard) key.s)
(v3:decf (pos camera)
(v3:*s (q:to-direction (rot camera))
(* factor dt)))))
(when (mouse-button (mouse) mouse.left)
(let ((move (v2:*s (mouse-move (mouse))
0.03)))
(setf (rot camera)
(q:normalize
(q:* (rot camera)
(q:normalize
(q:* (q:from-axis-angle (v! 1 0 0) (- (y move)))
(q:from-axis-angle (v! 0 1 0) (- (x move)))))))))))
(defun reset-camera (&optional (cam *current-camera*))
(setf (pos cam) (v! -0.43 25.33 43.20)
(rot cam) (v! 0.97 -0.20 -0.01 0.0))
cam)
(defmethod projection ((camera perspective-camera))
(let ((fs (or (frame-size camera)
(viewport-resolution (current-viewport)))))
(rtg-math.projection:perspective
(x fs)
(y fs)
(near camera)
(far camera)
(fov camera))))
(defmethod projection ((camera orthographic-camera))
(let ((fs (or (frame-size camera)
(viewport-resolution (current-viewport)))))
(rtg-math.projection:orthographic
(x fs)
(y fs)
(near camera)
(far camera))))
;;------------------------------------------------------------