• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/util/header/Camera.h

Go to the documentation of this file.
00001 
00002 class Camera
00003 {
00004 public:
00005         Camera(){
00006                 memset(this, 0, sizeof(this));
00007                 view.identity();
00008                 proj.identity();
00009                 position = float3(0.f,0.f,0.f);
00010                 rotation = float3(0.f,0.f,0.f);
00011                 fov = 60.f;
00012                 eyeSep = 0.06f;
00013                 focalLength = 20.f;
00014                 nearPlane = 0.1f;
00015                 farPlane = 1000.f;
00016                 width = 1024;
00017                 height = 768;
00018                 changedView = true;
00019                 changedProj = true;
00020         }
00021 
00022         ~Camera(){}
00023 
00024         void move(float forward, float right)
00025         {
00026                 float4x4 rotX = RotationX(-rotation.x);
00027                 float4x4 rotY = RotationY(-rotation.y);
00028                 float4x4 rotZ = RotationZ(-rotation.z);
00029                 float4x4 rotXYZ = rotX*rotY*rotZ;
00030                 float3 rightDir(rotXYZ(0,0), rotXYZ(0,1), rotXYZ(0,2));
00031                 float3 backwardDir(rotXYZ(2,0), rotXYZ(2,1), rotXYZ(2,2));
00032                 position += rightDir*right - backwardDir*forward;
00033                 changedView = true;
00034         }
00035 
00036         void rotate(float dx, float dy, float dz)
00037         {
00038                 rotation += float3(dx, dy, dz);
00039                 changedView = true;
00040         }
00041 
00042         void setPosition(float3 position)
00043         {
00044                 this->position = position;
00045                 changedView = true;
00046         }
00047 
00048         float3 getPosition(void){return position;}
00049 
00050         void setRotation(float3 rotation)
00051         {
00052                 this->rotation = rotation;
00053                 changedView = true;
00054         }
00055 
00056         float3 getRotation(void){return rotation;}
00057 
00058         void setFieldOfView(float fov){changedProj = true; this->fov = fov;}
00059         float getFieldOfView(void){return fov;}
00060 
00061         void setFocalLength(float focalLength){changedProj = true; this->focalLength = focalLength;}
00062         float getFocalLength(void){return focalLength;}
00063 
00064         void setEyeSeparation(float eyeSep){changedProj = true; this->eyeSep = eyeSep;}
00065         float getEyeSeparation(void){return eyeSep;}
00066 
00067         void setNearPlane(float nearPlane){changedProj = true; this->nearPlane = nearPlane;}
00068         float getNearPlane(void){return nearPlane;}
00069 
00070         void setFarPlane(float farPlane){changedProj = true; this->farPlane = farPlane;}
00071         float getFarPlane(void){return farPlane;}
00072 
00073         void setWidth(int width){changedProj = true; this->width = width;}
00074         int getWidth(void){return width;}
00075 
00076         void setHeight(int height){changedProj = true; this->height = height;}
00077         int getHeight(void){return height;}
00078 
00079         void swapEyes(void){changedProj = true; leftEye = !leftEye;}
00080         bool isLeftEye(void){return leftEye;}
00081 
00082         float3 getDirection(void)
00083         {
00084                 float4x4 rotX = RotationX(-rotation.x);
00085                 float4x4 rotY = RotationY(-rotation.y);
00086                 float4x4 rotZ = RotationZ(-rotation.z);
00087                 float4x4 rotXYZ = rotX*rotY*rotZ;
00088                 return float3(-rotXYZ(2,0), -rotXYZ(2,1), -rotXYZ(2,2));
00089         }
00090 
00091         const float4x4& getViewMatrix(void){return computeView(), view;}
00092         const float4x4& getProjMatrix(void){return computeProj(), proj;}
00093         const float4x4 getViewProjMatrix(void){return getProjMatrix()*getViewMatrix();}
00094 
00095 private:
00096 
00097         void computeView(void)
00098         {
00099                 if(changedView)
00100                 {
00101                         changedView = false;
00102                         float4x4 rotX = RotationX(-rotation.x);
00103                         float4x4 rotY = RotationY(-rotation.y);
00104                         float4x4 rotZ = RotationZ(-rotation.z);
00105                         view = rotX*rotY*rotZ;
00106                         view(0,3) = -(view(0,0)*position.x + view(0,1)*position.y + view(0,2)*position.z);
00107                         view(1,3) = -(view(1,0)*position.x + view(1,1)*position.y + view(1,2)*position.z);
00108                         view(2,3) = -(view(2,0)*position.x + view(2,1)*position.y + view(2,2)*position.z);
00109                 }
00110         }
00111 
00112         void computeProj(void)
00113         {
00114                 if(changedProj)
00115                 {
00116                         changedProj = false;
00117                         /*const float zn      =  nearPlane;
00118                         const float zf      =  farPlane;
00119                         const float ratio   =  width/float(height);
00120                         const float radians = (PI/180.f)*fov*.5f;
00121                         const float wd2     =  zn * tan(radians);
00122                         const float ndfl    =  zn / focalLength;
00123                         const float l = -ratio * wd2 - eyeSep * ndfl;
00124                         const float r =  ratio * wd2 - eyeSep * ndfl;
00125                         const float t =  wd2;
00126                         const float b = -wd2;
00127                         proj.set(
00128                                 2*zn/(r-l),   0,            (l+r)/(r-l),      0,
00129                                 0,            2*zn/(t-b),   (t+b)/(t-b),      0,
00130                                 0,            0,            zf/(zn-zf),       zn*zf/(zn-zf),
00131                                 0,            0,            -1,               0);*/
00132                         proj.projection(width, height, nearPlane, farPlane, fov);
00133                 }
00134         }
00135 
00136         float4x4 view;
00137         float4x4 proj;
00138         float3 position;
00139         float3 rotation;
00140         float fov;
00141         float focalLength;
00142         float eyeSep;
00143         float nearPlane;
00144         float farPlane;
00145         int width;
00146         int height;
00147         bool leftEye;
00148         bool changedView;
00149         bool changedProj;
00150 };

Generated on Fri Jun 18 2010 17:48:40 for Cannonball by  doxygen 1.7.0