stoe/engine/camera.lisp

39 lines
1.5 KiB
Common Lisp

#|
This file is a part of stoe project.
Copyright (c) 2015 Renaud Casenave-Péré (renaud@casenave-pere.fr)
|#
(uiop:define-package :stoe/engine/camera
(:use :cl :maths
:stoe/core/entity
:stoe/engine/scene-graph)
(:export #:camera-component #:projection #:view
#:update-view))
(in-package :stoe/engine/camera)
(defcomponent camera-component ()
((fovy :initarg :fovy)
(aspect :initarg :aspect)
(near :initarg :near)
(far :initarg :far)
(projection :initarg :projection :reader projection)
(view :accessor view))
(:needs scene-object-component)
(:documentation "Component for a camera representing a view of the game world."))
(defmethod initialize-instance :after ((camera camera-component) &key owner fovy aspect near far)
(with-slots (projection) camera
(setf projection (mperspective fovy aspect near far)))
(with-components (scene-object-component) owner
(with-slots (position direction) scene-object-component
(update-view camera position direction))))
(defmethod print-object ((camera camera-component) stream)
(with-slots (fovy aspect near far) camera
(print-unreadable-object (camera stream :type t)
(format stream "~@<~:_fovy = ~a ~:_aspect = ~a ~:_near = ~a ~:_far = ~a~:>"
fovy aspect near far))))
(defun update-view (camera position direction)
"Compute the world-to-view matrix from the position and the direction of the camera"
(setf (view camera) (m* (transpose (quat-to-mat4 direction)) (mtranslate (v- position)))))