Menu

[r10]: / Code / Viewer / Source / Scene / Camera3D.cs  Maximize  Restore  History

Download this file

153 lines (118 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using Microsoft.Xna.Framework;
namespace WaveRace360
{
public class Camera3D
{
// ------------------------------------------------------------------------
// Private Members
//
private Vector3 m_forward; // The up vector
private Vector3 m_right; // The right vector
private Vector3 m_up; // The up vector
private Vector3 m_pos; // The camera position
private float m_fieldOfView; // The field of view
private float m_aspectRatio; // The aspect ration
private float m_nearPlane; // The near plane
private float m_farPlane; // The far plane
private BoundingFrustum m_frustum; // The bounding frustum
private Matrix m_viewMatrix; // The view matrix
private Matrix m_projMatrix; // The projection matrix
private Matrix m_viewProjMatrix; // The combined view proj matrix
// ------------------------------------------------------------------------
// Public Members
//
public Vector3 Forward { get { return m_forward; } }
public Vector3 Right { get { return m_right; } }
public Vector3 Up { get { return m_up; } }
public Vector3 Pos { get { return m_pos; } }
public float FieldOfView { get { return m_fieldOfView; } }
public float AspectRatio { get { return m_aspectRatio; } }
public float NearPlane { get { return m_nearPlane; } }
public float FarPlane { get { return m_farPlane; } }
public BoundingFrustum Frustum { get { return m_frustum; } }
public Matrix ViewMatrix { get { return m_viewMatrix; } }
public Matrix ProjMatrix { get { return m_projMatrix; } }
public Matrix ViewProjMatrix { get { return m_viewProjMatrix; } }
// ------------------------------------------------------------------------
// Constructor
//
public Camera3D()
{
m_viewMatrix = Matrix.Identity;
m_projMatrix = Matrix.Identity;
m_frustum = new BoundingFrustum(Matrix.Identity);
SetProjection(MathHelper.PiOver4, 0.75f, 1.0f, 10000.0f);
SetView(Vector3.Forward, Vector3.Right, Vector3.Up);
}
// ------------------------------------------------------------------------
// Set the projection of the camera
//
public void SetProjection(float a_fieldOfView, float a_aspectRatio, float a_nearPlane, float a_farPlane)
{
m_fieldOfView = a_fieldOfView;
m_aspectRatio = a_aspectRatio;
m_nearPlane = a_nearPlane;
m_farPlane = a_farPlane;
UpdateMatrix();
}
// ------------------------------------------------------------------------
// Set the view
//
public void SetView(Vector3 a_forward, Vector3 a_right, Vector3 a_up)
{
m_forward = a_forward;
m_right = a_right;
m_up = a_up;
UpdateMatrix();
}
// ------------------------------------------------------------------------
// Set the view and position
//
public void SetView(Vector3 a_pos, Vector3 a_forward, Vector3 a_right, Vector3 a_up)
{
m_pos = a_pos;
m_forward = a_forward;
m_right = a_right;
m_up = a_up;
UpdateMatrix();
}
// ------------------------------------------------------------------------
// Set the position
//
public void SetPosition(Vector3 a_pos, Vector3 a_forward, Vector3 a_right, Vector3 a_up)
{
m_pos = a_pos;
UpdateMatrix();
}
// ------------------------------------------------------------------------
// Set the view using look-at vectors
//
public void SetLookAt(Vector3 a_eye, Vector3 a_at, Vector3 a_up)
{
m_pos = a_eye;
m_up = a_up;
m_forward = a_at - a_eye;
m_forward.Normalize();
m_right = Vector3.Cross(m_forward, m_up);
m_right.Normalize();
UpdateMatrix();
}
// ------------------------------------------------------------------------
// Update the projection and view matrices
//
private void UpdateMatrix()
{
// Calculate the projection matrix
m_projMatrix = Matrix.CreatePerspectiveFieldOfView(m_fieldOfView, m_aspectRatio, m_nearPlane, m_farPlane);
// Calculate the view matrix
m_viewMatrix = Matrix.CreateLookAt(m_pos, m_pos + m_forward, m_up);
// Calculate the bounding frustum
m_viewProjMatrix = Matrix.Multiply(m_viewMatrix, m_projMatrix);
m_frustum.Matrix = m_viewProjMatrix;
}
//
// ------------------------------------------------------------------------
}
}
// -- EOF