CCamera.h

Go to the documentation of this file.
00001 /*
00002  * CCamera.h
00003  * $Id: CCamera.h,v 1.5 2003/06/24 14:50:02 anxo Exp $
00004  *
00005  * Copyright (C) 1999, 2000 Markus Janich
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * As a special exception to the GPL, the QGLViewer authors (Markus
00022  * Janich, Michael Meissner, Richard Guenther, Alexander Buck and Thomas
00023  * Woerner) give permission to link this program with Qt (non-)commercial
00024  * edition, and distribute the resulting executable, without including
00025  * the source code for the Qt (non-)commercial edition in the source
00026  * distribution.
00027  *
00028  */
00029 
00030 //  Description : Class CCamera
00031 //  Purpose     : Provides funcionality (rotate, move, etc.)
00032 
00033 #ifndef __CCAMERA_H_
00034 #define __CCAMERA_H_
00035 
00036 
00037 // Qt
00039 
00040 
00041 // System
00043 #include <math.h>
00044 #if _MSC_VER >= 1300
00045 #include <iostream>
00046 #else
00047 #include <iostream.h>
00048 #endif
00049 
00050 // Own
00052 #include "GeoGeneric.h"
00053 #include "CP4D.h"
00054 #include "CV4D.h"
00055 #include "CP3D.h"
00056 #include "CV3D.h"
00057 #include "CMat4D.h"
00058 #include "CBoundingBox3D.h"
00059 
00060 #ifndef M_PI
00061 #define M_PI 3.14159265358979323846
00062 #endif
00063 
00064 
00066 
00073 class CCamera {
00074 
00075 public:
00077   enum CameraType {
00078     orthographic,       
00079     perspective         
00080   };
00081 
00082 
00086   CCamera() :
00087     m_CameraType( perspective ),
00088     m_cBBox(CBoundingBox3D(-1, -1, -1, 1, 1, 1)),
00089     m_rdVerAngle(30.0),
00090     m_rdRatio(4.0/3.0),
00091     m_rdNearPlane(0.0001), m_rdFarPlane(10000.0),
00092     m_nVPHeight(480),
00093     m_fValidViewDir(false), m_fValidViewRight(false),
00094     m_cEyePos(CP3D(0,0,-1)),
00095     m_cRefPoint(CP3D(0,0,0)),
00096     m_cViewUp(CV3D(0,1,0)),
00097     m_nTimeStep(0)
00098     {
00099          setEyePos(m_cEyePos); // updates near/far plane
00100     };
00101 
00102 
00115   CCamera( double rdEyePosX, double rdEyePosY, double rdEyePosZ,
00116            double rdRefPointX, double rdRefPointY, double rdRefPointZ,
00117            double rdViewUpX, double rdViewUpY, double rdViewUpZ,
00118            const CBoundingBox3D &cBBox=CBoundingBox3D(-1, -1, -1, 1, 1, 1),
00119            double rdVerAngle=30.0, int nVPHeight=480,
00120            double rdRatio=4.0/3.0,
00121            double rdNearPlane=0.0001, double rdFarPlane=10000.0,
00122            int nTimeStep = 0)
00123     : m_CameraType( perspective ),
00124       m_cBBox(cBBox),
00125       m_rdVerAngle(rdVerAngle),
00126       m_rdRatio(rdRatio),
00127       m_rdNearPlane(rdNearPlane), m_rdFarPlane(rdFarPlane),
00128       m_nVPHeight(nVPHeight),
00129       m_fValidViewDir(false), m_fValidViewRight(false),
00130       m_cEyePos(CP3D(rdEyePosX, rdEyePosY, rdEyePosZ)),
00131       m_cRefPoint(CP3D(rdRefPointX, rdRefPointY, rdRefPointZ)),
00132       m_cViewUp(CV3D(rdViewUpX, rdViewUpY, rdViewUpZ)),
00133      m_nTimeStep(nTimeStep)
00134     { 
00135          setEyePos(m_cEyePos); // updates near/far plane
00136     };
00137 
00139   CCamera( CP3D cEyePos, CP3D cRefPoint, CV3D cViewUp,
00140            const CBoundingBox3D &cBBox=CBoundingBox3D(-1, -1, -1, 1, 1, 1),
00141            double rdVerAngle=30.0, int nVPHeight=480,
00142            double rdRatio=4.0/3.0,
00143            double rdNearPlane=0.0001, double rdFarPlane=10000.0,
00144            CameraType ctype=perspective,
00145            int nTimeStep = 0)
00146     : m_CameraType( ctype ),
00147       m_cBBox(cBBox),
00148       m_rdVerAngle(rdVerAngle),
00149       m_rdRatio(rdRatio),
00150       m_rdNearPlane(rdNearPlane), m_rdFarPlane(rdFarPlane),
00151       m_nVPHeight(nVPHeight),
00152       m_fValidViewDir(false), m_fValidViewRight(false),
00153       m_cEyePos(cEyePos),
00154       m_cRefPoint(cRefPoint),
00155       m_cViewUp(cViewUp),
00156      m_nTimeStep(nTimeStep)
00157     {
00158          setEyePos(cEyePos); // updates near/far plane
00159     };
00160 
00161   // Use default copy constructor
00163 
00165   virtual ~CCamera() {};
00166 
00173   void rotate(double rdAngle, CV3D cAxis, bool global=true);
00174 
00178   void translate(CV3D vDiff);
00179 
00181   void setRefPoint(const CP3D &cRefPoint) {
00182     m_fValidViewRight = m_fValidViewDir = false;
00183     m_cRefPoint = cRefPoint;
00184   };
00185 
00187   const CP3D& getRefPoint() const {
00188     return m_cRefPoint;
00189   };
00190 
00192   void setEyePos(const CP3D &cEyePos);
00193 
00195   const CP3D& getEyePos() const {
00196     return m_cEyePos;
00197   };
00198 
00200   void setViewUp(const CV3D &cViewUp) {
00201     m_cViewUp = cViewUp;
00202   };
00203 
00205   const CV3D& getViewUp() const {
00206     return m_cViewUp;
00207   };
00208 
00210   // Same problem as before.
00211   const CV3D& getViewDir() const;
00212 
00214   // Same problem as before.
00215   const CV3D& getViewRight() const;
00216 
00231   void setHVAngle(double rdHorAngle, double rdVerAngle) {
00232     m_rdVerAngle = rdVerAngle>180.0 ? 180.0 : rdVerAngle;
00233 
00234     rdHorAngle = rdHorAngle/180.0*M_PI;
00235     rdVerAngle = rdVerAngle/180.0*M_PI;
00236 
00237     //m_rdRatio = rdHorAngle/rdVerAngle;
00238     m_rdRatio = tan(rdHorAngle)/tan(rdVerAngle);
00239   }
00240 
00245   void getHVAngle(double &rdHorAngle, double &rdVerAngle) const {
00246     rdVerAngle = m_rdVerAngle;
00247     //rdHorAngle = m_rdVerAngle * m_rdRatio;
00248     rdHorAngle = atan(tan(m_rdVerAngle) * m_rdRatio);
00249     //cout << rdHorAngle << "," << rdVerAngle << endl;
00250   }
00251 
00253   void setClipPlanes(double rdNearPlane, double rdFarPlane) {
00254     m_rdNearPlane = rdNearPlane;
00255     m_rdFarPlane = rdFarPlane;
00256   }
00257 
00259   void getClipPlanes(double &rdNearPlane, double &rdFarPlane) const {
00260     rdNearPlane = m_rdNearPlane;
00261     rdFarPlane = m_rdFarPlane;
00262   }
00263 
00266   void setBoundingBox(const CBoundingBox3D &cBBox, bool fViewAll=true) {
00267     m_cBBox = cBBox;
00268     if (fViewAll)
00269       viewAll();
00270   }
00271 
00272 
00274   const CBoundingBox3D &getBoundingBox()  const {
00275      return m_cBBox;
00276   }
00277     
00279   void setCameraType( CameraType type ) {
00280     m_CameraType = type;
00281   };
00282 
00284   CameraType getCameraType() const {
00285     return m_CameraType;
00286   };
00287 
00297   void getVVolume( double array[6] ) const;
00298 
00312   void setVPRes(  int nVPWidth, int nVPHeight) {
00313     m_nVPHeight = nVPHeight;
00314 
00315     m_rdRatio = double(nVPWidth)/nVPHeight;
00316   }
00317 
00319   void getVPRes(  int &nVPWidth, int &nVPHeight) const {
00320     nVPHeight = m_nVPHeight;
00321     nVPWidth = int(m_nVPHeight * m_rdRatio);
00322   }
00323 
00331   void getVPParams( CP3D &origin, CV3D &xStep, CV3D &yStep, int nXSize, int  nYSize) const;
00332 
00335   CMat4D getModelview() const;
00336 
00338   CMat4D getOrtho() const;
00339 
00341   CMat4D getFrustrum() const;
00342 
00345   CMat4D getProjection() const;
00346 
00349   CMat4D getVPTrans(int nXSize, int nYSize) const;
00350 
00353   void setRatio(double rdRatio) {
00354     m_rdRatio = rdRatio;
00355   }
00356 
00358   double getRatio() const {
00359     return m_rdRatio;
00360   }
00361 
00363   void setFovy(double rdFovy) {
00364     m_rdVerAngle = rdFovy;
00365   }
00366 
00368   double getFovy() const {
00369     return m_rdVerAngle;
00370   }
00371 
00373   void setVPHeight(int nVPHeight) {
00374     m_nVPHeight = nVPHeight;
00375   }
00376 
00378   int getVPHeight() const {
00379     return m_nVPHeight;
00380   }
00381 
00383   void setTimeStep(int nTimeStep) {
00384      m_nTimeStep = nTimeStep;
00385   }
00386 
00388   int getTimeStep() const {
00389      return m_nTimeStep;
00390   }
00391 
00394   void viewAll();
00395 
00397   void print();
00398 
00400   //friend ostream& operator<<(ostream&, const CCamera&);
00401 
00403   //friend istream& operator>>(istream&, CCamera&);
00404 
00405 
00406 
00407 private:
00409   // PRIVATE METHODS //
00411 
00413   // PRIVATE MEMBERS //
00415 
00416   CameraType m_CameraType; // holds the current type of the camera
00417   CBoundingBox3D m_cBBox;    // boundingbox of the scene is needed
00418                            // to switch bwtween perspective and orthographic
00419 
00420   double m_rdVerAngle;     // vertical focus angle in degrees
00421   double m_rdRatio;        // ratio between height and width, or hor. and vert. angle
00422 
00423   double m_rdNearPlane;    // distance between eyepoint and near clipplane
00424   double m_rdFarPlane;     // distance between eyepoint and far clipplane
00425 
00426   int m_nVPHeight;         // size of viewplane in y-direction
00427 
00428   mutable bool m_fValidViewDir;    // is true if the m_cViewDir has a valid value
00429   mutable bool m_fValidViewRight;  // is true if the m_cViewRight has a valid value
00430 
00431   CP3D m_cEyePos;
00432   CP3D m_cRefPoint;
00433   CV3D m_cViewUp;
00434 
00435   mutable CV3D m_cViewDir;       // NOTE: Don't use this direct. Use 'getViewDir()' instead.
00436   mutable CV3D m_cViewRight;     // NOTE: Don't use this direct. Use 'getViewRight()' instead.
00437 
00438   int m_nTimeStep;
00439 };
00440 
00441 #endif // __CCAMERA_H_
00442 

Generated on Tue Jul 11 10:42:56 2006 for QGLViewer by  doxygen 1.4.7