most of the engine drawing code

This commit is contained in:
Dan Ballard 2011-08-02 22:55:24 -07:00
parent 9fac5b54a0
commit 86b3849ee3
5 changed files with 60 additions and 11 deletions

46
engine.lisp Normal file
View File

@ -0,0 +1,46 @@
(in-package #:flight-sim)
(defclass engine-object (game-object)
((start-time :initarg :start-time :accessor start-time :initform 0)
(activation-time :initarg :activation-time :accessor activation-time :initform 0)))
(defclass engine-model (model)
((template-vertices :initarg :template-vertices :accessor template-vertices :initform nil)
(template-colors :initarg :template-colors :accessor template-colors :initform nil)))
(defun generate-step-2d-array (2darr time)
(let ((len-arr (length 2darr))
(len-row (length (aref 2darr 0))))
(make-2d-array len-arr len-row
(loop for i from 0 to len-arr collecting
(loop for j from 0 to len-row collecting
(let ((item (aref (aref 2darr i) j)))
(if (listp item)
(converge (first item) (second item) (third item) time)
item)))))))
; take 2 seconds to fully fire
(defmethod regen-model ((model engine-model) time)
(setf (vertices model) (generate-step-2d-array *thruster-vertices* time))
(setf (colors model) (generate-step-2d-array *thruster-colors* time)))
(defparameter *thruster-vertices*
'((0.0 0.5 0.0) (-2.0 -0.5 0.0) (2.0 -0.5 0.0)
; z goes from 0 to 1 in 2 seconds
(0.0 0.0 (0 1 2))))
(defparameter *thruster-colors*
'(((16 64 2) (0 132 2) (32 164 2))
((16 64 2) (0 132 2) (32 164 2))
((16 64 2) (0 132 2) (32 164 2))
((0 255 2) (0 255 2) (64 255 2))))
(defmethod draw ((model engine-model) time)
(if (and (generated-model model) (< (- time (start-time model)) (activation-time model)))
(regen-model model time))
(call-next-method))

View File

@ -112,14 +112,14 @@
; (object-draw (engine object)))))
(defun draw-world ()
(defun draw-world (time)
;; clear the buffer
(gl:clear :color-buffer-bit :depth-buffer-bit)
;; move to eye position
;;draw (make-instance 'powered-object :motion (make-instance 'motion :coords (vector 0 0 -3)) :model *ship-model* :engine (engine *self*)))
(let ((orig-coords (coords (body *self*))))
(setf (coords (body *self*)) (vector 0 0 -3))
(draw *self*)
(draw *self* time)
(setf (coords (body *self*)) orig-coords))
(gl:translate (- (aref (coords (body *self*)) 0)) (- (aref (coords (body *self*)) 1)) (- (aref (coords (body *self*)) 2))) ;; eye
@ -127,7 +127,7 @@
(loop for entity across *world* do
; only draw if its infront of me
(if (< (aref (coords (body entity)) 2) (+ 10 (aref (coords (body *self*)) 2)))
(draw entity)))
(draw entity time)))
(gl:matrix-mode :modelview)
(gl:load-identity)
@ -192,7 +192,7 @@
; (phys-step time)
(draw-world)
(draw-world start-time)
(incf *num-frames*)

View File

@ -22,4 +22,4 @@
;(defun draw-entity (entity)
(defgeneric draw (object))
(defgeneric draw (object time))

View File

@ -6,7 +6,7 @@
(colors :initarg :colors :reader colors :initform (vector) :type shape-vector)
(face-colors :initarg :face-colors :accessor face-colors :initform (vector) :type shape-ref-vector)))
(defmethod draw ((model model))
(defmethod draw ((model model) time)
(loop for i from 0 to (1- (length (faces model))) do
(draw-triangle (get-vertecies (aref (faces model) i) (vertices model))
(get-vertecies (aref (face-colors model) i) (colors model)))))

View File

@ -11,17 +11,20 @@
;(defmethod coords ((object game-object))
; (coords (body object)))
(defmethod draw :before ((object game-object))
(defmethod activate ((object game-object) sym start-time)
(push sym (active-attachments object))
(activate (getf (attachments object) sym)))
(defmethod draw :before ((object game-object) time)
(gl:push-matrix)
(gl:translate (aref (coords (body object)) 0) (aref (coords (body object)) 1) (aref (coords (body object)) 2))
(gl:rotate (aref (angles (body object)) 0) 1 0 0)
(gl:rotate (aref (angles (body object)) 1) 0 1 0)
(gl:rotate (aref (angles (body object)) 2) 0 0 1))
(defmethod draw :after ((object game-object))
(defmethod draw :after ((object game-object) time)
(gl:pop-matrix))
(defmethod draw ((object game-object))
(draw (model object)))
(defmethod draw ((object game-object) time)
(draw (model object) time))