converting to more manual stuff

This commit is contained in:
Dan Ballard 2011-07-03 15:26:41 -07:00
parent fa03a91a8d
commit 51f4d77f82
1 changed files with 60 additions and 27 deletions

View File

@ -5,12 +5,33 @@
(progn ,@body) (progn ,@body)
(continue () :report "Continue"))) (continue () :report "Continue")))
(defun make-2d-array (h w contents)
(let ((arr (make-array h)))
(do ((i 0 (incf i))
(rest-list contents (rest rest-list)))
((eql i h))
(setf (aref arr i) (make-array w :initial-contents (car rest-list))))
arr))
(defparameter *n* (make-array 3 :initial-contents '(0 0 1)))
(defparameter *v* (make-2d-array 3 3 '((0 0.5 0) (0.5 -0.5 0.0) (-0.5 -0.5 0.0))))
(defparameter *faces* (make-2d-array 1 3 '((0 1 2))))
(defparameter *start-time* (wall-time))
(defparameter *last-time* nil)
(defparameter *num-frames* 0)
;;(defparameter *t1* '( (-0.5 -0.5 0) (0 0.5 0) (0.5 -0.5 0)))
(defun get-vertecies (face)
(loop for f in (elt *faces* face) collecting (elt *v* f)))
(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))
(+ (* (get-internal-real-time) time-units) (+ (* (get-internal-real-time) time-units)
offset))) offset)))
(defparameter *start-time* (wall-time))
(defun shift-color (time) (defun shift-color (time)
(values (values
@ -21,46 +42,56 @@
;;; blue ;;; blue
(/ (+ (* (sin (+ (* 0.3 time) (* 4/3 PI))) 127) 128) 255))) (/ (+ (* (sin (+ (* 0.3 time) (* 4/3 PI))) 127) 128) 255)))
(defparameter *t1* '( (-0.5 -0.5 0) (0 0.5 0) (0.5 -0.5 0)))
(defun rotate-vertex (v time) (defun rotate-vertex-2d (v rM)
(let* ((x (first v)) v)
(y (second v)) ;; (let ((result (lm:* rM (lm:vector (first v) (second v)))))
(theta (atan (if (eql 0 x) 1000000 (/ y x)))) ;; (list (lm:elt result 0) (lm:elt result 1))))
(hyp (sqrt (+ (* x x) (* y y)))))
(list (/ (cos (+ theta time)) hyp) (/ (sin (+ theta time)) hyp) (third v)))) ;; (let* ((x (first v))
;; (y (second v))
;; (theta (atan (if (eql 0 x) 1000000 (/ y x))))
;; (hyp (sqrt (+ (* x x) (* y y)))))
;; (list (/ (cos (+ theta time)) hyp) (/ (sin (+ theta time)) hyp) (third v))))
; (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)
(list (rotate-vertex (first tri) time) (let* ((angle (/ time 1000))
(rotate-vertex (second tri) time) (cos-a (cos angle))
(rotate-vertex (third tri) time))) (sin-a (sin angle))
(rM (lm:make-matrix 2 2 :initial-elements
'(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))))))
(defparameter *last-time* nil)
(defparameter *num-frames* 0)
(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 *t1* time))) (t1 (rotate-triangle (get-vertecies (aref *faces* 0)) time)))
;;;(setf *last-time* (wall-time)) (format t "after let~%")
; (format t "~a ~a: ~a ~a ~a~%" *start-time* time red green blue)
(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))
(multiple-value-bind (v1 v2 v3) (values-list (first t1)) (format t "vertex1~%")
(gl:vertex v1 v2 v3)) (let ((v (first t1)))
(gl:vertex (aref v 0) (aref v 1) (aref v 2)))
(multiple-value-bind (green blue red) (shift-color time) (multiple-value-bind (green blue red) (shift-color time)
(gl:color red green blue)) (gl:color red green blue))
(multiple-value-bind (v1 v2 v3) (values-list (second t1)) (let ((v (second t1)))
(gl:vertex v1 v2 v3)) (gl:vertex (aref v 0) (aref v 1) (aref v 2)))
(multiple-value-bind (blue green red) (shift-color time) (multiple-value-bind (blue green red) (shift-color time)
(gl:color red green blue)) (gl:color red green blue))
(multiple-value-bind (v1 v2 v3) (values-list (third t1)) (let ((v (third t1)))
(gl:vertex v1 v2 v3))) (gl:vertex (aref v 0) (aref v 1) (aref v 2))))
;; finish the frame ;; finish the frame
(gl:flush) (gl:flush)
@ -72,17 +103,19 @@
(short-fps (floor (if (zerop short-interval) 0 (/ 1 short-interval)))) (short-fps (floor (if (zerop short-interval) 0 (/ 1 short-interval))))
(long-fps (floor (if (zerop long-interval) 0 (/ *num-frames* long-interval))))) (long-fps (floor (if (zerop long-interval) 0 (/ *num-frames* long-interval)))))
(format t "FPS since last:~a since start:~a~%" short-fps long-fps)))
(format t "FPS since last:(~a - ~a) ~a->~a since start:~a/~a)->~a ~%" time *last-time* short-interval short-fps *num-frames* long-interval long-fps)))
(setf *last-time* time))) (setf *last-time* time)))
(defun init ()
(defun main-loop ()
(setf *start-time* (wall-time)) (setf *start-time* (wall-time))
(setf *num-frames* 0) (setf *num-frames* 0)
(setf *last-time* 0) (setf *last-time* 0)
)
(defun main-loop ()
(init)
(sdl:with-init () (sdl:with-init ()
(sdl:window 320 240 :flags sdl:sdl-opengl) (sdl:window 320 240 :flags sdl:sdl-opengl)
;; cl-opengl needs platform specific support to be able to load GL ;; cl-opengl needs platform specific support to be able to load GL