00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __camera_h__
00013 #define __camera_h__
00014
00015 #include "Vector3.h"
00016
00017 class Camera{
00018 private:
00019 Vector3<float> mCameraAngle;
00020 Vector3<float> mCameraPos;
00021 Vector3<float> mCameraVel;
00022 Vector3<float> mCameraAcc;
00023 Vector3<float> mLastCameraPos;
00024 float mAcc;
00025 public:
00026 Camera(Vector3<float> v=Vector3<float>(2, 1, -.1), float accScaling=.5){
00027 mCameraPos = v;
00028 mLastCameraPos = v;
00029 mCameraVel = Vector3<float>(0,0,0);
00030 mCameraAcc = Vector3<float>(0,0,0);
00031 mCameraVel = Vector3<float>(0,0,0);
00032 Vector3<float> direction = -mCameraPos;
00033 float r = sqrt(direction.x()*direction.x() + direction.y()*direction.y() + direction.z()*direction.z());
00034 float phi = sign(direction.x())*M_PI/2 - atan(direction.z()/direction.x());
00035 float theta = acos(direction.y()/r);
00036 mCameraAngle = Vector3<float>(1.0, phi/M_PI, theta/M_PI);
00037 mAcc = accScaling;
00038 }
00039 Vector3<float> getPosition() const { return mCameraPos; }
00040 Vector3<float> getLookAtVector() const{
00041 float x,y,z,theta,phi, r;
00042 r = mCameraAngle[0];
00043 phi = mCameraAngle[1];
00044 theta = mCameraAngle[2];
00045 x = sin(M_PI*theta)*sin(M_PI*phi);
00046 y = cos(M_PI*theta);
00047 z = sin(M_PI*theta)*cos(M_PI*phi);
00048 return Vector3<float>(x, y, z);
00049 }
00050 Vector3<float> getSideVector() const{
00051 return Vector3<float>(cross(getLookAtVector(), Vector3<float>(0,1.0,0)));
00052 }
00053 Vector3<float> getUpVector() const{
00054 return Vector3<float>(cross(getSideVector(), getLookAtVector()));
00055 }
00056 Vector3<float> getLookAtPoint() const{
00057 return Vector3<float>(mCameraPos + getLookAtVector());
00058 }
00059 bool moving() const {
00060 return (mLastCameraPos == mCameraPos);
00061 }
00062 void advect(float dt){
00063 mLastCameraPos = mCameraPos;
00064 mCameraVel += mCameraAcc*dt;
00065 mCameraPos += mCameraVel*dt;
00066 }
00067 void accelerate(Vector3<float> v){
00068 mCameraAcc += v*mAcc;
00069 }
00070 void rotateXY(float xr, float yr){
00071
00072 mCameraAngle[1] += xr;
00073 mCameraAngle[2] = clamp(mCameraAngle[2]+=yr, .0001f, .9999f);
00074 }
00075 void stopAcc(){
00076 mCameraAcc = 0;
00077 }
00078 void stop(){
00079 mCameraVel = 0;
00080 mCameraAcc = 0;
00081 }
00082 void accUp(){
00083 accelerate(getUpVector());
00084 }
00085 void accDown(){
00086 accelerate(-getUpVector());
00087 }
00088 void accForward(){
00089 accelerate(getLookAtVector());
00090 }
00091 void accBackward(){
00092 accelerate(-getLookAtVector());
00093 }
00094 void accLeft(){
00095 accelerate(-getSideVector());
00096 }
00097 void accRight(){
00098 accelerate(getSideVector());
00099 }
00100 void lookAtOrigo(){
00101 Vector3<float> direction = -mCameraPos;
00102 float r = direction.length();
00103 float phi = sign(direction.x())*M_PI/2 - atan(direction.z()/direction.x());
00104 float theta = acos(direction.y()/r);
00105 mCameraAngle = Vector3<float>(1.0, phi/M_PI, theta/M_PI);
00106 }
00107 void reset(){
00108 *this = Camera();
00109 }
00110 void dolly(float r){
00111 mCameraPos += getLookAtVector()*r;
00112 }
00113 };
00114
00115 #endif