CameraGL.cc

Go to the documentation of this file.
00001 #include <gra/camera/CameraGL.h>
00002 #include <gra/camera/PerspProj.h>
00003 #include <gra/camera/OrthoProj.h>
00004 
00005 using namespace gra;
00006 
00007 void CameraGL::_set_orthogonal_proj(const Projection& proj)
00008 {
00009     glMatrixMode(GL_PROJECTION);
00010     glLoadIdentity();
00011     glOrtho(proj.getLeftPlane(),
00012             proj.getRightPlane(),
00013             proj.getBottomPlane(),
00014             proj.getTopPlane(),
00015             proj.getNearPlane(),
00016             proj.getFarPlane());
00017     glMatrixMode(GL_MODELVIEW);
00018 }
00019 
00020 void CameraGL::_set_perspective_proj(const Projection& proj)
00021 {
00022     glMatrixMode(GL_PROJECTION);
00023     glLoadIdentity();
00024     glFrustum(proj.getLeftPlane(),
00025               proj.getRightPlane(),
00026               proj.getBottomPlane(),
00027               proj.getTopPlane(),
00028               proj.getNearPlane(),
00029               proj.getFarPlane());
00030     glMatrixMode(GL_MODELVIEW);
00031 }
00032 
00033 
00034 
00035 
00036 CameraGL::CameraGL(AutoPtr<Projection> * pAP)
00037     : Camera(pAP)
00038 {
00039     if (dynamic_cast<PerspProj*>(_pProjection))
00040         _set_perspective_proj(*_pProjection);
00041     else if (dynamic_cast<OrthoProj*>(_pProjection))
00042         CameraGL::_set_orthogonal_proj(*_pProjection);
00043     else {
00044         cerr << "CameraGL::CameraGL(): Uknown projection type, using orthogonal" << endl;
00045         CameraGL::_set_orthogonal_proj(*_pProjection);
00046     }
00047 }
00048 
00049 CameraGL::CameraGL(const Camera& src)
00050     : Camera(src)
00051 {
00052     if (dynamic_cast<PerspProj*>(_pProjection))
00053         CameraGL::_set_perspective_proj(*_pProjection);
00054     else if (dynamic_cast<OrthoProj*>(_pProjection))
00055         CameraGL::_set_orthogonal_proj(*_pProjection);
00056     else {
00057         cerr << "CameraGL::CameraGL(): Uknown projection type, using orthogonal" << endl;
00058         CameraGL::_set_orthogonal_proj(*_pProjection);
00059     }
00060 
00061     if (_pTrMat) CameraGL::setOpenGLTransform(*_pTrMat);
00062 }
00063 
00064 void CameraGL::setOpenGLTransform(const Matrix4& mat)
00065 {
00066     /*
00067      * We have transformation of the camera but we want to convert it to
00068      * the transformation of scene (vertices).
00069      * Tr. matrix of camera means firstly rotate and then translate.
00070      * Then we must exchange these operations and to do them
00071      * in oposite directions. Finally we must transpose consequent metrix
00072      * because OpenGL requires the "m" matrix have created by
00073      * scaning columns instead of lines.
00074      *
00075      * Have |R t|, want ||R^T 0|*|1 -t||^T = |R^T (R^T*-t)|^T = | R       0|
00076      *      |0 1|       ||0   1| |0  1||     |0   1       |     |(R^T*-t) 1|
00077      *
00078      */
00079 
00080     Matrix4 trMat(mat);
00081     Matrix3 rMat;
00082     Vector3 tVec;
00083     Vector3 sVec;
00084     float   glMat[16];
00085 
00086     trMat.get(rMat, tVec, sVec);
00087     rMat.transpose();
00088     rMat.transform(tVec);
00089     trMat(3,0) = -tVec.x; trMat(0,3) = 0;
00090     trMat(3,1) = -tVec.y; trMat(1,3) = 0;
00091     trMat(3,2) = -tVec.z; trMat(2,3) = 0;
00092     trMat.get(glMat);
00093     
00094     glMatrixMode(GL_MODELVIEW);
00095     glLoadIdentity();
00096     glMultMatrixf(glMat);
00097 }
00098 
00099 void CameraGL::setTransform(const Matrix4& trMat)
00100 {
00101     Camera::setTransform(trMat);
00102     CameraGL::setOpenGLTransform(trMat);
00103 }
00104 
00105 void CameraGL::rotateX(float a)
00106 {
00107     Camera::rotateX(a);
00108     CameraGL::setOpenGLTransform(*_pTrMat);
00109 }
00110 
00111 void CameraGL::rotateY(float a)
00112 {
00113     Camera::rotateY(a);
00114     CameraGL::setOpenGLTransform(*_pTrMat);
00115 }
00116 
00117 void CameraGL::rotateZ(float a)
00118 {
00119     Camera::rotateZ(a);
00120     CameraGL::setOpenGLTransform(*_pTrMat);
00121 }
00122 
00123 void CameraGL::rotate(float a, const Vector3& axis)
00124 {
00125     Camera::rotate(a, axis);
00126     CameraGL::setOpenGLTransform(*_pTrMat);
00127 }
00128 
00129 void CameraGL::translate(float x, float y, float z)
00130 {
00131     Camera::translate(x, y, z);
00132     CameraGL::setOpenGLTransform(*_pTrMat);
00133 }
00134 
00135 void CameraGL::unsetTransform()
00136 {
00137     Camera::unsetTransform();
00138     glMatrixMode(GL_MODELVIEW);
00139     glLoadIdentity();
00140 }
00141 
00142 void CameraGL::setProjection(AutoPtr<Projection> * pProj)
00143 {
00144     Camera::setProjection(pProj);
00145     
00146     if (dynamic_cast<PerspProj*>(_pProjection))
00147         CameraGL::_set_perspective_proj(*_pProjection);
00148     else if (dynamic_cast<OrthoProj*>(_pProjection))
00149         CameraGL::_set_orthogonal_proj(*_pProjection);
00150     else {
00151         cerr << "CameraGL::etProjection(): Uknown projection type, using orthogonal" << endl;
00152         CameraGL::_set_orthogonal_proj(*_pProjection);
00153     }
00154 }

Generated on Tue Nov 21 15:11:42 2006 for gra by  doxygen 1.4.6