Navigation Menu

Skip to content

Commit

Permalink
Added some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
patzy committed Jun 13, 2011
1 parent 7eef9b0 commit 2994c44
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion glaw.asd
Expand Up @@ -22,8 +22,8 @@
(:file "anim")
(:file "2d")
(:file "3d")
(:file "gui")
(:file "font")
(:file "gui")
(:file "particles")
(:file "screen")
(:file "scheduler")
Expand Down
17 changes: 15 additions & 2 deletions src/3d.lisp
Expand Up @@ -20,7 +20,7 @@
(setf (matrix-r00 eye-mtx) (- (basis-r01 basis)) ;; -y --> x
(matrix-r01 eye-mtx) (- (basis-r11 basis))
(matrix-r02 eye-mtx) (- (basis-r21 basis))
(matrix-r10 eye-mtx) (basis-r02 basis) ;; z --> y
(matrix-r10 eye-mtx) (basis-r02 basis) ;; z --> y
(matrix-r11 eye-mtx) (basis-r12 basis)
(matrix-r12 eye-mtx) (basis-r22 basis)
(matrix-r20 eye-mtx) (- (basis-r00 basis)) ;; -x --> z
Expand Down Expand Up @@ -151,11 +151,13 @@
(defstruct mesh
names
shapes
dlists ;; TESTING: rendering performance
materials)

(defun create-mesh (&optional (nb-parts 0))
(make-mesh :shapes (make-array nb-parts :adjustable t :fill-pointer t :element-type 'shape)
:materials (make-array nb-parts :adjustable t :fill-pointer t)
:dlists (make-array nb-parts :adjustable t :fill-pointer t :element-type 'display-list)
:names (make-array nb-parts :adjustable t :fill-pointer t :element-type 'string)))

(defun mesh-nb-parts (mesh)
Expand All @@ -181,6 +183,14 @@
(defun mesh-add-part (mesh &key (shape (create-shape 0 0)) (material +default-material+)
(name (symbol-name (gensym "PART-"))))
(vector-push-extend shape (mesh-shapes mesh))
(vector-push-extend (load-primitive (shape-indices shape)
(shape-vertices shape)
:primitive (shape-primitive shape)
:colors (shape-colors shape)
:tex-coords (shape-tex-coords shape)
:normals (shape-normals shape)
:use-buffers t)
(mesh-dlists mesh))
(vector-push-extend material (mesh-materials mesh))
(vector-push-extend name (mesh-names mesh)))

Expand All @@ -192,9 +202,12 @@
(defun render-mesh (mesh)
(loop for i below (mesh-nb-parts mesh)
for shape = (mesh-shape mesh i)
for dl = (aref (mesh-dlists mesh) i)
for mat = (mesh-material mesh i)
do (set-material mat)
(render-shape shape)))
(call-primitive dl)
;;(render-shape shape)
))

;;; Wavefront OBJ :mesh asset
;; http://www.martinreddy.net/gfx/3d/OBJ.spec
Expand Down
45 changes: 42 additions & 3 deletions src/math.lisp
Expand Up @@ -351,9 +351,6 @@ Polygons *MUST* be adjacent. Returns NIL if merge is not possible."
(* (vector-3d-y v1) (vector-3d-y v2))
(* (vector-3d-z v1) (vector-3d-z v2))))

;; (defun vector-3d-perp-dot-product (v1 v2)
;; (vector-3d-dot-product (vector-3d-perp v1) v2))

(defun vector-3d-mag (v)
(sqrt (vector-3d-dot-product v v)))

Expand Down Expand Up @@ -793,6 +790,48 @@ Polygons *MUST* be adjacent. Returns NIL if merge is not possible."

(defsetf basis-xyz-orientation basis-set-xyz-orientation)

(defun basis-global-to-local-position (basis pos)
(let ((x (- (point-3d-x pos) (basis-tx basis)))
(y (- (point-3d-y pos) (basis-ty basis)))
(z (- (point-3d-z pos) (basis-tz basis))))
(make-point-3d :x (+ (* (basis-r00 basis) x) (* (basis-r10 basis) y) (* (basis-r20 basis) z))
:y (+ (* (basis-r01 basis) x) (* (basis-r11 basis) y) (* (basis-r21 basis) z))
:z (+ (* (basis-r02 basis) x) (* (basis-r12 basis) y) (* (basis-r22 basis) z)))))

(defun basis-local-to-global-position (basis pos)
(let ((x (point-3d-x pos))
(y (point-3d-y pos))
(z (point-3d-z pos)))
(make-point-3d :x (+ (* (basis-r00 basis) x) (* (basis-r01 basis) y) (* (basis-r02 basis) z)
(basis-tx basis))
:y (+ (* (basis-r10 basis) x) (* (basis-r11 basis) y) (* (basis-r12 basis) z)
(basis-ty basis))
:z (+ (* (basis-r20 basis) x) (* (basis-r21 basis) y) (* (basis-r22 basis) z)
(basis-tz basis)))))

(defun basis-global-to-local-vector (basis vec)
(let ((x (vector-3d-x pos))
(y (vector-3d-y pos))
(z (vector-3d-z pos)))
(make-point-3d :x (+ (* (basis-r00 basis) x) (* (basis-r10 basis) y) (* (basis-r20 basis) z))
:y (+ (* (basis-r01 basis) x) (* (basis-r11 basis) y) (* (basis-r21 basis) z))
:z (+ (* (basis-r02 basis) x) (* (basis-r12 basis) y) (* (basis-r22 basis) z)))))

(defun basis-local-to-global-vector (basis vec)
(let ((x (vector-3d-x pos))
(y (vector-3d-y pos))
(z (vector-3d-z pos)))
(make-point-3d :x (+ (* (basis-r00 basis) x) (* (basis-r01 basis) y) (* (basis-r02 basis) z))
:y (+ (* (basis-r10 basis) x) (* (basis-r11 basis) y) (* (basis-r12 basis) z))
:z (+ (* (basis-r20 basis) x) (* (basis-r21 basis) y) (* (basis-r22 basis) z)))))


;; ;;; Spatial transform
;; (defstruct (transform (:include matrix)
;; (:type (vector float)))
;; "Transformation matrix."
;; (identity t))

;;; Projection matrices
(defun matrix-set-ortho (mtx left right bottom top near far)
(setf (matrix-r00 mtx) (/ 2.0 (- right left))
Expand Down

0 comments on commit 2994c44

Please sign in to comment.