-reimplemented most of lists as arrays or arrays of arrays

-wrote basic matrix* and rotation matrix generation function
-almost got math right
This commit is contained in:
Dan Ballard 2011-07-04 07:55:29 -07:00
parent 51f4d77f82
commit fb87c204b8
1 changed files with 36 additions and 16 deletions

View File

@ -24,8 +24,8 @@
;;(defparameter *t1* '( (-0.5 -0.5 0) (0 0.5 0) (0.5 -0.5 0))) ;;(defparameter *t1* '( (-0.5 -0.5 0) (0 0.5 0) (0.5 -0.5 0)))
(defun get-vertecies (face) (defun get-vertecies (faces)
(loop for f in (elt *faces* face) collecting (elt *v* f))) (loop for i from 0 to (1- (length faces)) collecting (aref *v* (aref faces i))))
(let ((time-units (/ 1.0 internal-time-units-per-second))) (let ((time-units (/ 1.0 internal-time-units-per-second)))
(defun wall-time (&key (offset 0)) (defun wall-time (&key (offset 0))
@ -43,6 +43,26 @@
(/ (+ (* (sin (+ (* 0.3 time) (* 4/3 PI))) 127) 128) 255))) (/ (+ (* (sin (+ (* 0.3 time) (* 4/3 PI))) 127) 128) 255)))
(defun make-rotation-matrix (xa ya za)
(let ((sxa (sin xa))
(cxa (cos xa))
(sya (sin ya))
(cya (cos ya))
(sza (sin za))
(cza (cos za)))
(make-array '(3 3) :initial-contents (list (list (* cya cza) (+ (- (* cxa sza)) (* sxa sya cza)) (+ (* sxa sza) (* cxa sya cza)))
(list (* cya sza) (+ (* cxa cza) (* sxa sya sza)) (+ (- (* sxa cza)) (* cxa sya sza)))
(list (- sya) (* sxa cya) (* cxa cya))))))
(defun rotate* (m v)
(let ((result (make-array 3 :initial-element 0)))
(dotimes (x 3)
(let ((cell (aref v x)))
(dotimes (y 3)
(incf (aref result x) (* cell (aref m x y))))))
result))
(defun rotate-vertex-2d (v rM) (defun rotate-vertex-2d (v rM)
v) v)
;; (let ((result (lm:* rM (lm:vector (first v) (second v))))) ;; (let ((result (lm:* rM (lm:vector (first v) (second v)))))
@ -56,30 +76,30 @@
; (list (+ (first v) (/ (sin time) 2)) (+ (second v) (/ (cos time) 2)) (third v))) ; (list (+ (first v) (/ (sin time) 2)) (+ (second v) (/ (cos time) 2)) (third v)))
(defun rotate-triangle (tri time) (defun rotate-triangle (tri time)
(let* ((angle (/ time 1000)) (list
(cos-a (cos angle)) (rotate* (make-rotation-matrix 0 0 time) (first tri))
(sin-a (sin angle)) (rotate* (make-rotation-matrix 0 0 time) (second tri))
(rM (lm:make-matrix 2 2 :initial-elements (rotate* (make-rotation-matrix 0 0 time) (third tri))))
'(cos-a sin-a ; (let* ((angle (/ time 1000))
(- sin-a) cos-a)))) ; (cos-a (cos angle))
(list (append (rotate-vertex-2d (first tri) rM) '((third (firt tri)))) ; (sin-a (sin angle))
(append (rotate-vertex-2d (second tri) rM) '((third (second tri)))) ; (rM nil)) ;lm:make-matrix 2 2 :initial-elements
(append (rotate-vertex-2d (third tri) rM) (third (third tri)))))) ; ; '(cos-a sin-a
; ; (- sin-a) cos-a))))
; (list (append (rotate-vertex-2d (first tri) rM) '((third (firt tri))))
; (append (rotate-vertex-2d (second tri) rM) '((third (second tri))))
; (append (rotate-vertex-2d (third tri) rM) (third (third tri))))))
;
(defun draw () (defun draw ()
"draw a frame" "draw a frame"
(format t "draw~%")
(let* ((time (- (wall-time) *start-time*)) (let* ((time (- (wall-time) *start-time*))
(t1 (rotate-triangle (get-vertecies (aref *faces* 0)) time))) (t1 (rotate-triangle (get-vertecies (aref *faces* 0)) time)))
(format t "after let~%")
(gl:clear :color-buffer-bit) (gl:clear :color-buffer-bit)
;;; draw a triangle ;;; draw a triangle
(gl:with-primitive :triangles (gl:with-primitive :triangles
(format t "color1~%")
(multiple-value-bind (red green blue) (shift-color time) (multiple-value-bind (red green blue) (shift-color time)
(gl:color red green blue)) (gl:color red green blue))
(format t "vertex1~%")
(let ((v (first t1))) (let ((v (first t1)))
(gl:vertex (aref v 0) (aref v 1) (aref v 2))) (gl:vertex (aref v 0) (aref v 1) (aref v 2)))