00001
00008 #ifndef CAMERA_H
00009 #define CAMERA_H
00010
00011 #include "MathCommon.h"
00012
00018 class Camera
00019 {
00020 public:
00021
00025 Camera() : minRadius(0.0f)
00026 {
00027 Set(Point3f(0.0f, 1.0f, 5.0f), Point3f(0.0f, 0.0f, 0.0f), Vector3f(0.0f, 1.0f, 0.0f));
00028 }
00029
00037 Camera(const Point3f &eye, const Point3f ¢er, const Vector3f &upVec) : minRadius(0.0f)
00038 {
00039 Set(eye, center, upVec);
00040 }
00041
00049 void Set(const Point3f &eye, const Point3f ¢er, const Vector3f &upVec)
00050 {
00051 pos = eye;
00052 lookAt = center;
00053
00054 forward = Normalize(center - eye);
00055 right = Normalize(forward.Cross(upVec));
00056 up = Normalize(right.Cross(forward));
00057 }
00058
00064 Matrixf GetViewMatrix() const
00065 {
00066 Matrixf m;
00067 m.MakeView(pos, right, up, forward);
00068 return m;
00069 }
00070
00076 void Move(const Vector3f &amt)
00077 {
00078 Vector3f dx = right * amt.x;
00079 Vector3f dz = Vector3f(forward.x, 0.0f, forward.z) * amt.z;
00080
00081 Vector3f d = dx + dz;
00082
00083 lookAt = lookAt + d;
00084 pos = pos + d;
00085 }
00086
00092 void SetMinRadius(float r)
00093 {
00094 minRadius = r;
00095 }
00096
00102 void Zoom(float d)
00103 {
00104 Point3f newPos = pos + forward * d;
00105
00106 Sphere s;
00107 s.c = lookAt;
00108 s.r = minRadius;
00109
00110 if(Inside(newPos, s))
00111 {
00112
00113 Ray r;
00114 r.p = lookAt;
00115 r.d = Normalize(-forward);
00116
00117 Point3f points[2];
00118 int n = Intersect(r, s, points);
00119 assert(n == 1);
00120
00121 pos = points[0];
00122 }
00123 else
00124 {
00125 pos = newPos;
00126 }
00127 }
00128
00134 void RotateRight(float a)
00135 {
00136 Matrixf R;
00137 R.MakeRotationY(a);
00138
00139 pos = R.Transform(pos);
00140 forward = Normalize(lookAt - pos);
00141 right = Normalize(forward.Cross(Vector3f(0,1,0)));
00142 up = Normalize(right.Cross(forward));
00143 }
00144
00150 void RotateUp(float a)
00151 {
00152 Matrixf R;
00153 R.MakeRotation(right, a);
00154
00155 pos = R.Transform(pos);
00156 forward = Normalize(lookAt - pos);
00157 up = Normalize(right.Cross(forward));
00158 }
00159
00169 void Get(Point3f *eye, Point3f *center, Vector3f *rightVec, Vector3f *upVec, Vector3f *forwardVec) const
00170 {
00171 if(eye)
00172 *eye = pos;
00173 if(center)
00174 *center = lookAt;
00175 if(rightVec)
00176 *rightVec = right;
00177 if(upVec)
00178 *upVec = up;
00179 if(forwardVec)
00180 *forwardVec = forward;
00181 }
00182
00188 Vector3f ViewDirection() const
00189 {
00190 return forward;
00191 }
00192
00193 private:
00194 Point3f pos;
00195 Point3f lookAt;
00196
00197 float minRadius;
00198
00199 Vector3f right;
00200 Vector3f up;
00201 Vector3f forward;
00202 };
00203
00204 #endif