KGLLib
core/kgllib/camera.h
00001 /*
00002  * Copyright (C) 2008 Rivo Laks <rivolaks@hot.ee>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either 
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public 
00015  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef KGLLIB_CAMERA_H
00019 #define KGLLIB_CAMERA_H
00020 
00021 
00022 #include "kgllib.h"
00023 
00024 #include <Eigen/Geometry>
00025 
00026 namespace KGLLib
00027 {
00028 
00060 class KGLLIB_EXPORT Camera
00061 {
00062 public:
00063     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00064 
00065     Camera();
00066     virtual ~Camera();
00067 
00073     virtual void applyPerspective();
00080     virtual void applyView(bool reset = true);
00081 
00086     virtual void applyViewport();
00087 
00091     void setViewport(int x, int y, int width, int height);
00092 
00096     void setFoV(float fov);
00101     void setAspect(float aspect);
00113     void setDepthRange(float near, float far);
00114 
00118     void setPosition(const Eigen::Vector3f& pos);
00119     void setPosition(float x, float y, float z)  { setPosition(Eigen::Vector3f(x, y, z)); }
00124     void setLookAt(const Eigen::Vector3f& lookat);
00125     void setLookAt(float x, float y, float z)  { setLookAt(Eigen::Vector3f(x, y, z)); }
00130     void setUp(const Eigen::Vector3f& up);
00131     void setUp(float x, float y, float z)  { setUp(Eigen::Vector3f(x, y, z)); }
00137     void setDirection(const Eigen::Vector3f& dir);
00138     void setDirection(float x, float y, float z)  { setDirection(Eigen::Vector3f(x, y, z)); }
00139 
00140     Eigen::Vector3f position() const  { return mPosition; }
00141     Eigen::Vector3f lookAt() const  { return mLookAt; }
00142     Eigen::Vector3f up() const  { return mUp; }
00143 
00156     void setModelviewMatrix(const Eigen::Transform3f& modelview);
00169     void setProjectionMatrix(const Eigen::Transform3f& projection);
00170 
00176     Eigen::Transform3f modelviewMatrix() const;
00183     Eigen::Transform3f projectionMatrix() const;
00184 
00191     Eigen::Vector3f project(const Eigen::Vector3f& v, bool* ok = 0) const;
00198     Eigen::Vector3f unProject(const Eigen::Vector3f& v, bool* ok = 0) const;
00199 
00200 protected:
00201     void recalculateModelviewMatrix();
00202     void recalculateProjectionMatrix();
00203 
00204 protected:
00205     Eigen::Vector3f mPosition;
00206     Eigen::Vector3f mLookAt;
00207     Eigen::Vector3f mUp;
00208     float mFoV, mAspect, mDepthNear, mDepthFar;
00209 
00210     Eigen::Transform3f mModelviewMatrix;
00211     bool mModelviewMatrixDirty;
00212     Eigen::Transform3f mProjectionMatrix;
00213     bool mProjectionMatrixDirty;
00214     int mViewport[4];
00215 };
00216 
00217 }
00218 
00219 #endif