aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/affine.lisp
blob: 671c1842977b1dc2faff641f1a83ebc899bc3822 (plain)
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
;;;; affine.lisp

(in-package #:wheelwork)

(defclass/std affine (unit)
  ((cached-model cached-projected-matrix cached-application cached-rectangle :a)
   (base-width base-height :r :std 1.0 :doc "Determined by content.")
   (scale-x scale-y :std 1.0)
   (rotation x y :std 0.0)))

(defmethod (setf closer-mop:slot-value-using-class) :after
    (newval class (affine affine) slot)
  (case (closer-mop:slot-definition-name slot)
    ((x y scale-x scale-y rotation)
     (setf (cached-model affine) nil
           (cached-projected-matrix affine) nil
           (cached-rectangle affine) nil))))

(defun scale-by (affine amount)
  (with-accessors ((sx scale-x) (sy scale-y)) affine
    (setf sx (* amount sx)
          sy (* amount sy))))

(defun set-width-preserve-aspect (affine new-width)
  (scale-by affine (/ new-width (width affine))))

(defun set-height-preserve-aspect (affine new-height)
  (scale-by affine (/ new-height (height affine) )))

(defmethod width ((affine affine))
  (with-slots (scale-x base-width) affine
    (* scale-x base-width)))

(defmethod height ((affine affine))
  (with-slots (scale-y base-height) affine
    (* scale-y base-height)))

(defmethod (setf width) (newval (affine affine))
  (with-slots (scale-x base-width) affine
    (setf scale-x (coerce (/ newval base-width) 'single-float))))

(defmethod (setf height) (newval (affine affine))
  (with-slots (scale-y base-height) affine
    (setf scale-y (coerce (/ newval base-height) 'single-float))))

(defmethod model-matrix :around ((u affine))
  (or (cached-model u)
      (setf (cached-model u)
            (call-next-method))))

(defmethod model-matrix ((u affine))
  (let ((m (mat:meye 4)))
    (with-slots (x y base-width scale-x base-height scale-y rotation) u 
      (let ((uw (* base-width scale-x))
            (uh (* base-height scale-y)))
        (mat:nmtranslate m (vec:vec x y 0.0))

        (mat:nmtranslate m (vec:v* 0.5 (vec:vec uw uh 0.0)))
        (mat:nmrotate  m vec:+vz+ rotation)
        (mat:nmtranslate m (vec:v* -0.5 (vec:vec uw uh 0.0)))
        
        (mat:nmscale m (vec:vec uw uh 1.0))))
    m))


(defun projected-matrix (affine)
  (or (cached-projected-matrix affine)
      (setf (cached-projected-matrix affine)
            (mat:marr (mat:m* (application-projection (app-of-unit affine))
                              (model-matrix affine))))))

(defmethod get-rect ((affine-unit affine))
  (or (cached-rectangle affine-unit)
      (setf (cached-rectangle affine-unit)
            (with-accessors ((x x) (y y) (w width) (h height) (r rotation)) affine-unit
              (let ((m
                      (mat:meye 4))
                    (tr
                      (vec:vec (+ x (* 0.5 w)) (+ y (* 0.5 h)) 0.0)))
                (mat:nmtranslate m tr)
                (mat:nmrotate  m vec:+vz+ r)
                (mat:nmtranslate m (vec:v* -1.0 tr))
                
                (list (mat:m* m (vec:vec x y 0.0 1.0))
                      (mat:m* m (vec:vec x (+ y h) 0.0 1.0))
                      (mat:m* m (vec:vec (+ x w) (+ y h) 0.0 1.0))
                      (mat:m* m (vec:vec (+ x w) y 0.0 1.0))
                      (mat:m* m (vec:vec x y 0.0 1.0))))))))

(defun  units-intersect-p (au1 au2)
  "Returns T if the two units AU1 an AU2 intersect. Both must implement GET-RECT."
  (paths-intersect-p (get-rect au1) (get-rect au2)))