From dd65cc83b0363c5d9952e2f30e4ffb8c3bd96377 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 30 Jul 2011 13:09:37 -0700 Subject: [PATCH] more restructuring of code into logical files and moving functions into classes --- flight-sim.asd | 2 ++ flight-sim.lisp | 41 ----------------------------------------- graphics.lisp | 25 +++++++++++++++++++++++++ model.lisp | 8 ++++++++ objects.lisp | 25 +++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 graphics.lisp create mode 100644 objects.lisp diff --git a/flight-sim.asd b/flight-sim.asd index b1d3620..934b1a7 100644 --- a/flight-sim.asd +++ b/flight-sim.asd @@ -8,7 +8,9 @@ :components ((:file "package") (:file "util") (:file "math") + (:file "graphics") (:file "model") (:file "phsyics") + (:file "objects") (:file "flight-sim"))) diff --git a/flight-sim.lisp b/flight-sim.lisp index be775b9..ea9d2bb 100644 --- a/flight-sim.lisp +++ b/flight-sim.lisp @@ -5,10 +5,6 @@ ;;; "flight-sim" goes here. Hacks and glory await! -(defclass game-object () - ((model :initarg :model :accessor model :initform (make-instance 'model)) - (body :initarg :body :accessor body :inotform (make-instance 'body)))) - (defclass engine-object (game-object) ((active :initarg :active :reader active :initform nil) @@ -102,44 +98,7 @@ (defparameter *num-frames* 0) -(defun draw-triangle (tri colors) - (declare (type shape-vector tri)) - (declare (type shape-vector colors)) - (gl:with-primitive :triangles - (let ((c (aref colors 0))) - (gl:color (aref c 0) (aref c 1) (aref c 2))) - (let ((v (aref tri 0))) - (gl:vertex (aref v 0) (aref v 1) (aref v 2))) - - (let ((c (aref colors 1))) - (gl:color (aref c 0) (aref c 1) (aref c 2))) - (let ((v (aref tri 1))) - (gl:vertex (aref v 0) (aref v 1) (aref v 2))) - - (let ((c (aref colors 2))) - (gl:color (aref c 0) (aref c 1) (aref c 2))) - (let ((v (aref tri 2))) - (gl:vertex (aref v 0) (aref v 1) (aref v 2))))) -(defun draw-entity (entity) - (gl:translate (aref (coords (motion entity)) 0) (aref (coords (motion entity)) 1) (aref (coords (motion entity)) 2)) - (gl:rotate (aref (angles entity) 0) 1 0 0) - (gl:rotate (aref (angles entity) 1) 0 1 0) - (gl:rotate (aref (angles entity) 2) 0 0 1) - (loop for i from 0 to (1- (length (faces (model entity)))) do - (draw-triangle (get-vertecies (aref (faces (model entity)) i) (vertices (model entity))) - (get-vertecies (aref (face-colors (model entity)) i) (colors (model entity)))))) - -(defgeneric object-draw (object)) - -(defmethod object-draw :before ((object game-object)) - (gl:push-matrix)) - -(defmethod object-draw :after ((object game-object)) - (gl:pop-matrix)) - -(defmethod object-draw ((object game-object)) - (draw-entity object)) (defmethod object-draw ((object powered-object)) (draw-entity object) diff --git a/graphics.lisp b/graphics.lisp new file mode 100644 index 0000000..5bc7007 --- /dev/null +++ b/graphics.lisp @@ -0,0 +1,25 @@ +(in-package #:flight-sim) + +(defun draw-triangle (tri colors) + (declare (type shape-vector tri)) + (declare (type shape-vector colors)) + (gl:with-primitive :triangles + (let ((c (aref colors 0))) + (gl:color (aref c 0) (aref c 1) (aref c 2))) + (let ((v (aref tri 0))) + (gl:vertex (aref v 0) (aref v 1) (aref v 2))) + + (let ((c (aref colors 1))) + (gl:color (aref c 0) (aref c 1) (aref c 2))) + (let ((v (aref tri 1))) + (gl:vertex (aref v 0) (aref v 1) (aref v 2))) + + (let ((c (aref colors 2))) + (gl:color (aref c 0) (aref c 1) (aref c 2))) + (let ((v (aref tri 2))) + (gl:vertex (aref v 0) (aref v 1) (aref v 2))))) + +;(defun draw-entity (entity) + + +(defgeneric draw (object)) \ No newline at end of file diff --git a/model.lisp b/model.lisp index 9eabed8..457ba93 100644 --- a/model.lisp +++ b/model.lisp @@ -6,6 +6,14 @@ (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)) + (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))))) + +(defclass animated-model () + ((start-time :initarg :start-time :accessor start-time :initform 0.0))) + (defmethod scale-colors ((model model)) (let ((colors (colors model))) (loop for i from 0 to (1- (length colors)) do diff --git a/objects.lisp b/objects.lisp new file mode 100644 index 0000000..ddb66a9 --- /dev/null +++ b/objects.lisp @@ -0,0 +1,25 @@ +(in-package #:flight-sim) + + + +(defclass game-object () + ((model :initarg :model :accessor model :initform (make-instance 'model)) + (body :initarg :body :accessor body :initform (make-instance 'body)) + (attachments :initarg :attachments :accessor attachments :initform '()) + (active-attachments :initarg :active-attachments :accessor active-attachments :initform '()))) + + + + +(defmethod draw :before ((object game-object)) + (gl:push-matrix) + (gl:translate (aref (coords (motion entity)) 0) (aref (coords (motion entity)) 1) (aref (coords (motion entity)) 2)) + (gl:rotate (aref (angles entity) 0) 1 0 0) + (gl:rotate (aref (angles entity) 1) 0 1 0) + (gl:rotate (aref (angles entity) 2) 0 0 1)) + +(defmethod draw :after ((object game-object)) + (gl:pop-matrix)) + +(defmethod draw ((object game-object)) + (draw (model object)))