From 86b3849ee3155a7b88f5fc27966e90de7459d9e1 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 2 Aug 2011 22:55:24 -0700 Subject: [PATCH] most of the engine drawing code --- engine.lisp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ flight-sim.lisp | 8 ++++---- graphics.lisp | 2 +- model.lisp | 2 +- objects.lisp | 13 ++++++++----- 5 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 engine.lisp diff --git a/engine.lisp b/engine.lisp new file mode 100644 index 0000000..95f5260 --- /dev/null +++ b/engine.lisp @@ -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)) + + diff --git a/flight-sim.lisp b/flight-sim.lisp index f1a88e2..db04ecb 100644 --- a/flight-sim.lisp +++ b/flight-sim.lisp @@ -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*) diff --git a/graphics.lisp b/graphics.lisp index 5bc7007..5c7f06b 100644 --- a/graphics.lisp +++ b/graphics.lisp @@ -22,4 +22,4 @@ ;(defun draw-entity (entity) -(defgeneric draw (object)) \ No newline at end of file +(defgeneric draw (object time)) \ No newline at end of file diff --git a/model.lisp b/model.lisp index 5568804..a4e2248 100644 --- a/model.lisp +++ b/model.lisp @@ -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))))) diff --git a/objects.lisp b/objects.lisp index ac2160e..06259e6 100644 --- a/objects.lisp +++ b/objects.lisp @@ -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))