/* * Copyright (c) 2007-2009 SlimDX Group * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using SlimDX; namespace SampleFramework { /// /// Represents a view onto a 3D scene. /// public class Camera { Vector3 location; Vector3 target; float fieldOfView; float aspectRatio; float nearPlane; float farPlane; Matrix viewMatrix; Matrix projectionMatrix; bool viewDirty = true; bool projectionDirty = true; /// /// Gets or sets the location of the camera eye point. /// /// The location of the camera eye point. public Vector3 Location { get { return location; } set { if (location == value) return; location = value; viewDirty = true; } } /// /// Gets or sets the view target point. /// /// The view target point. public Vector3 Target { get { return target; } set { if (target == value) return; target = value; viewDirty = true; } } /// /// Gets or sets the field of view. /// /// The field of view. public float FieldOfView { get { return fieldOfView; } set { if (fieldOfView == value) return; fieldOfView = value; projectionDirty = true; } } /// /// Gets or sets the aspect ratio. /// /// The aspect ratio. public float AspectRatio { get { return aspectRatio; } set { if (aspectRatio == value) return; aspectRatio = value; projectionDirty = true; } } /// /// Gets or sets the near plane. /// /// The near plane. public float NearPlane { get { return nearPlane; } set { if (nearPlane == value) return; nearPlane = value; projectionDirty = true; } } /// /// Gets or sets the far plane. /// /// The far plane. public float FarPlane { get { return farPlane; } set { if (farPlane == value) return; farPlane = value; projectionDirty = true; } } /// /// Gets the view matrix. /// /// The view matrix. public Matrix ViewMatrix { get { if (viewDirty) RebuildViewMatrix(); return viewMatrix; } } /// /// Gets the projection matrix. /// /// The projection matrix. public Matrix ProjectionMatrix { get { if (projectionDirty) RebuildProjectionMatrix(); return projectionMatrix; } } /// /// Initializes a new instance of the class. /// public Camera() { } /// /// Rebuilds the view matrix. /// protected virtual void RebuildViewMatrix() { viewMatrix = Matrix.LookAtLH(Location, Target, Vector3.UnitY); viewDirty = false; } /// /// Rebuilds the projection matrix. /// protected virtual void RebuildProjectionMatrix() { projectionMatrix = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, NearPlane, FarPlane); projectionDirty = false; } } }