#region File Description
//-----------------------------------------------------------------------------
// CameraBase.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using System.Text;
using RobotGameData.GameInterface;
#endregion
namespace RobotGameData.Camera
{
///
/// This is the base class of every camera.
/// It can configure View matrix and Projection matrix.
///
public class CameraBase : GameNode
{
#region Fields
///
/// Field of View
///
float fov = MathHelper.PiOver4;
float screenWidth = 0;
float screenHeight = 0;
float aspectRatio = 0;
///
/// The Camera's near distance
///
float near = 1.0f;
///
/// The Camera's far distance
///
float far = 1000.0f;
///
/// The Camera position
///
Vector3 position = Vector3.Zero;
///
/// The Camera old position
///
Vector3 oldPosition = Vector3.Zero;
///
/// The Camera up vector
///
Vector3 up = Vector3.Up;
///
/// The Camera right vector
///
Vector3 right = Vector3.Right;
///
/// The Camera direction
///
Vector3 direction = Vector3.Forward;
///
/// The camera looks at the target position.
///
Vector3 target = Vector3.Forward;
///
/// The Camera velocity
///
Vector3 velocity = Vector3.Zero;
///
/// The projection matrix
///
Matrix projectionMatrix = Matrix.Identity;
///
/// The view matrix
///
Matrix viewMatrix = Matrix.Identity;
#endregion
#region Properties
public Matrix ProjectionMatrix
{
get { return projectionMatrix; }
protected set { projectionMatrix = value; }
}
public Matrix ViewMatrix
{
get { return viewMatrix; }
protected set { viewMatrix = value; }
}
public Vector3 Position
{
get { return position; }
}
public Vector3 Direction
{
get { return direction; }
}
public Vector3 Target
{
get { return target; }
}
public Vector3 Velocity
{
get { return velocity; }
set { velocity = value; }
}
public Vector3 Up
{
get { return up; }
}
public Vector3 Right
{
get { return right; }
}
public float AspectRatio
{
get { return aspectRatio; }
}
public float FieldOfView
{
get { return fov; }
}
public float Near
{
get { return near; }
}
public float Far
{
get { return far; }
}
public float Width
{
get { return screenWidth; }
}
public float Height
{
get { return screenHeight; }
}
#endregion
///
/// Constructor.
///
public CameraBase()
: base()
{
near = 1.0f;
far = 1000.0f;
position = oldPosition = Vector3.Zero;
up = Vector3.Up;
right = Vector3.Right;
direction = Vector3.Forward;
target = Vector3.Forward;
velocity = Vector3.Zero;
projectionMatrix = Matrix.Identity;
viewMatrix = Matrix.Identity;
}
///
/// Update a velocity
///
protected override void OnUpdate(GameTime gameTime)
{
if (oldPosition == this.Position)
{
this.velocity = Vector3.Zero;
}
else
{
this.velocity = this.position - oldPosition;
oldPosition = this.position;
}
base.OnUpdate(gameTime);
}
///
/// Set the view matrix of the camera
///
public Matrix SetView(Vector3 position, Vector3 target, Vector3 up)
{
// Make a camera's direction
this.direction = target - position;
this.direction.Normalize();
this.position = position;
this.target = target;
this.up = up;
// Make a camera's right vector
this.right = Vector3.Cross(direction, up);
this.right.Normalize();
// Make a camera's view matrix
ViewMatrix = Matrix.CreateLookAt(this.Position, this.target, this.Up);
return ViewMatrix;
}
///
/// Set the projection matrix of the camera
///
public Matrix SetPespective( float fov, float width, float height,
float near, float far)
{
this.screenWidth = width;
this.screenHeight = height;
this.fov = fov;
this.near = near;
this.far = far;
aspectRatio = screenWidth / screenHeight;
// Make a camera's projection matrix
projectionMatrix =
Matrix.CreatePerspectiveFieldOfView(fov, aspectRatio, near, far);
return projectionMatrix;
}
///
/// It changes the width and height of screen.
///
public void Resize(float width, float height)
{
SetPespective(this.fov, width, height, this.Near, this.Far);
}
///
/// Reset the camera
///
public void Reset()
{
this.position = Vector3.Zero;
this.up = Vector3.Up;
this.right = Vector3.Right;
this.direction = Vector3.Forward;
this.target = position + direction;
this.ViewMatrix = Matrix.Identity;
}
}
}