00001
00002 #ifndef _GK_CAMERA_H
00003 #define _GK_CAMERA_H
00004
00005 #include "Transform.h"
00006
00007
00008 namespace gk {
00009
00010
00011 class Camera
00012 {
00013 protected:
00014 Transform m_view;
00015 Transform m_projection;
00016 Transform m_viewport;
00017
00018 float m_width;
00019 float m_height;
00020
00021
00022 const Point origin( ) const
00023 {
00024 return m_view.inverse( Point(0.f, 0.f, 0.f) );
00025 }
00026
00027
00028 const Vector up( ) const
00029 {
00030 return m_view.inverse( Vector(0.f, 1.f, 0.f) );
00031 }
00032
00033
00034 const Vector right( ) const
00035 {
00036 return m_view.inverse( Vector(1.f, 0.f, 0.f) );
00037 }
00038
00039
00040 const Vector forward( ) const
00041 {
00042 return m_view.inverse( Vector(0.f, 0.f, 1.f) );
00043 }
00044
00045 public:
00046
00047 Camera( )
00048 :
00049 m_view(),
00050 m_projection(),
00051 m_viewport( Viewport(100.f, 100.f) ),
00052 m_width(100.f),
00053 m_height(100.f)
00054 {}
00055
00056
00057 Camera( const Transform& projection, const int width= 100, const int height= 100 )
00058 :
00059 m_view(),
00060 m_projection(projection),
00061 m_viewport( Viewport(width, height) ),
00062 m_width(width),
00063 m_height(height)
00064 {}
00065
00066
00067 ~Camera() {}
00068
00069
00070 void setViewport( const int width, const int height )
00071 {
00072 m_width= width;
00073 m_height= height;
00074 m_viewport= Viewport(width, height);
00075 }
00076
00077
00078 void setViewport( int viewport[4] )
00079 {
00080 m_width= viewport[2];
00081 m_height= viewport[3];
00082 m_viewport= Viewport(viewport[2], viewport[3]);
00083 }
00084
00085
00086 void setProjection( const Transform& projection )
00087 {
00088 m_projection= projection;
00089 }
00090
00091
00092 const Matrix4x4& projection( )
00093 {
00094 return m_projection.matrix();
00095 }
00096
00097
00098 const Transform& projectionTransform( )
00099 {
00100 return m_projection;
00101 }
00102
00103
00104 const Matrix4x4& view( )
00105 {
00106 return m_view.matrix();
00107 }
00108
00109
00110 const Transform& viewTransform( )
00111 {
00112 return m_view;
00113 }
00114
00115
00116 const Matrix4x4& viewport( )
00117 {
00118 return m_viewport.matrix();
00119 }
00120
00121
00122 const Transform& viewportTransform( )
00123 {
00124 return m_viewport;
00125 }
00126 };
00127
00128
00129 class PerspectiveCamera : public gk::Camera
00130 {
00131 public:
00132
00133 PerspectiveCamera( )
00134 :
00135 gk::Camera()
00136 {}
00137
00138
00139 PerspectiveCamera( const float fov, const float aspect, const float znear, const float zfar, const int width= 100, const int height= 100 )
00140 :
00141 Camera( Perspective(fov, aspect, znear, zfar), width, height)
00142 {}
00143
00144
00145 ~PerspectiveCamera( ) {}
00146 };
00147
00148
00149 class FirstPersonCamera : public PerspectiveCamera
00150 {
00151 protected:
00152 Point m_position;
00153 float m_rotation_x;
00154 float m_rotation_y;
00155 float m_rotation_z;
00156
00157 public:
00158 FirstPersonCamera( )
00159 :
00160 PerspectiveCamera(),
00161 m_position(),
00162 m_rotation_x(0.f),
00163 m_rotation_y(0.f),
00164 m_rotation_z(0.f)
00165 {}
00166
00167
00168 FirstPersonCamera( const float fov, const float aspect, const float znear, const float zfar, const int width= 100, const int height= 100 )
00169 :
00170 PerspectiveCamera(fov, aspect, znear, zfar, width, height),
00171 m_position(),
00172 m_rotation_x(0.f),
00173 m_rotation_y(0.f),
00174 m_rotation_z(0.f)
00175 {}
00176
00177 ~FirstPersonCamera( ) {}
00178
00179
00180 void moveForward( const float v )
00181 {
00182 m_position= m_position + forward() * v;
00183 }
00184
00185
00186 void moveRight( const float v )
00187 {
00188 m_position= m_position + right() * v;
00189 }
00190
00191
00192 void moveUp( const float v )
00193 {
00194 m_position= m_position + up() * v;
00195 }
00196
00197
00198 void move( const Point& p )
00199 {
00200 m_position= p;
00201 }
00202
00203
00204 void rotateUp( const float v )
00205 {
00206 m_rotation_y+= v;
00207 }
00208
00209
00210 void rotateRight( const float v )
00211 {
00212 m_rotation_x+= v;
00213 }
00214
00215
00216 void rotateForward( const float v )
00217 {
00218 m_rotation_z+= v;
00219 }
00220
00221
00222 const Point& position( )
00223 {
00224 return m_position;
00225 }
00226
00227
00228 const Matrix4x4& view( )
00229 {
00230 return viewTransform().matrix();
00231 }
00232
00233
00234 const Transform& viewTransform( )
00235 {
00236 m_view= (Translate( Vector(m_position) ) * RotateZ(m_rotation_z) * RotateX(m_rotation_x) * RotateY(m_rotation_y)).getInverse();
00237 return m_view;
00238 }
00239 };
00240
00241
00242
00243 class OrthographicCamera : public gk::Camera
00244 {
00245 public:
00246
00247 OrthographicCamera( )
00248 :
00249 gk::Camera()
00250 {}
00251
00252
00253 OrthographicCamera( const float znear, const float zfar, const int width= 100, const int height= 100 )
00254 :
00255 Camera( Orthographic(znear, zfar), width, height )
00256 {}
00257
00258
00259 ~OrthographicCamera( ) {}
00260 };
00261
00262 }
00263
00264 #endif