/* * PerspectiveCamera3D.cs * Authors: Bradley Blankenship * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; namespace tAC_Engine.Graphics.Cameras { /// /// Defines a camera that allows for a 3D scene by allowing change in camera /// position and look at angle and by giving depth /// public class Perspective3DCamera : PerspectiveCamera { #region Properties /// /// Gets/Sets the eye vector /// public override Vector3 Eye { get { return mEye; } set { // Check to see if we need to set values and recalculate if (mEye != value) { mEye = value; CreateView(); } } } /// /// Gets/Sets the look at vector /// public override Vector3 LookAt { get { return mLookAt; } set { // Check to see if we need to set values and recalculate if (mLookAt != value) { mLookAt = value; CreateView(); } } } /// /// Gets/Sets the up vector /// public override Vector3 Up { get { return mUp; } set { // Check to see if we need to set values and recalculate if (mUp != value) { mUp = value; CreateView(); } } } float mRotation = 0; public float Rotation { get { return mRotation; } set { float twoPi = 2 * (float)Math.PI; if (value < 0) { mRotation = twoPi - value; } else if (value > twoPi) { mRotation = value - twoPi; } else { mRotation = value; } } } float mTilt = 0; public float Tilt { get { return mTilt; } set { mTilt = MathHelper.Clamp(value, -(5 * (float)Math.PI / 12), -(1 * (float)Math.PI/12)); //mTilt = value; UpdateCamera(); } } float mOffset = 0f; public float Offset { get { return mOffset; } set { mOffset = value; UpdateCamera(); } } public void UpdateCamera() { Matrix offset = Matrix.CreateTranslation(new Vector3(0,0,mOffset)); Matrix tiltMat = Matrix.CreateRotationX(mTilt); Matrix rotMat = Matrix.CreateRotationY(mRotation); Matrix tempPos = offset * tiltMat * rotMat * Matrix.CreateTranslation(LookAt); Eye = tempPos.Translation; } #endregion #region Constructs /// /// Default Projection Camera with no values set. /// public Perspective3DCamera() { // Create the projection and view matrixes CreateView(); CreateProjection(); } /// /// Paramaterized Constructor for perspective camera. /// /// The position vector of the camera /// The lookAt vector of the camera /// The up vector of the camera /// The field of view, in radians /// The aspect ratio for the camera /// The distance to the near plane /// The distance to the far plane public Perspective3DCamera(float offset, Vector3 lookAt, Vector3 up, float fov, float aspectRatio, float near, float far) { // Set all of the values //mEye = eye; mLookAt = lookAt; mUp = up; mFOV = fov; mAspectRatio = aspectRatio; mNear = near; mFar = far; Rotation = 0; Tilt = 0f; Offset = offset; UpdateCamera(); // Create the view and projection matrixes CreateView(); CreateProjection(); } public Perspective3DCamera(Vector3 eye, Vector3 lookAt, Vector3 up, float fov, float aspectRatio, float near, float far) { // Set all of the values mEye = eye; mLookAt = lookAt; mUp = up; mFOV = fov; mAspectRatio = aspectRatio; mNear = near; mFar = far; //Rotation = 0; //Tilt = -.5f; //Offset = offset; //UpdateCamera(); // Create the view and projection matrixes CreateView(); CreateProjection(); } #endregion #region Helpers /// /// Creates the projection Matrix for the given camera. /// protected override void CreateProjection() { // Create the projection matrix mProjection = Matrix.CreatePerspectiveFieldOfView(mFOV, mAspectRatio, mNear, mFar); } #endregion } }