camera.cpp

Go to the documentation of this file.
00001 /* MAINTAINER: Martin */
00002 
00003 #include "camera.h"
00004 
00005 #include "application_client.h"
00006 
00007 Camera::Camera(void):
00008         mPosition(0.0),
00009         mReferencePoint(0.0, 0.0, -1.0),
00010         mUpVector(0.0, 1.0, 0.0)
00011 {
00012 }
00013 
00014 Camera::Camera(const GLVector3d & pos, const GLVector3d & ref, const GLVector3d & up):
00015         mPosition(pos), mReferencePoint(ref), mUpVector(up)
00016 {}
00017 
00018 Camera::~Camera(void)
00019 {}
00020 
00021 void Camera::setPerspective(const GLdouble fovx, const GLdouble aspect, const GLdouble zNear, const GLdouble zFar)
00022 {
00023         mPerspectiveChanged = mPerspective.fovx != fovx || mPerspective.aspect != aspect || mPerspective.zNear != zNear || mPerspective.zFar != zFar;
00024         if (mPerspectiveChanged)
00025         {
00026                 mPerspective.fovx       = fovx;
00027                 mPerspective.aspect     = aspect;
00028                 mPerspective.zNear      = zNear;
00029                 mPerspective.zFar       = zFar;
00030         }
00031 }
00032 
00033 void Camera::computeLookAt(GLMatrix4d &m)
00034 {
00035         GLVector3d forward, center, up, side, eye;
00036         getRef(center);
00037         getPos(eye);
00038         getUp(up);
00039 
00040         forward = center - eye;
00041         forward.normalize();
00042         (side = forward).cross(up).normalize();
00043         (up = side).cross(forward);
00044         
00045         m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0; m[15] = 1;
00046 
00047         m[0] = side.x;
00048     m[4] = side.y;
00049     m[8] = side.z;
00050 
00051     m[1] = up.x;
00052     m[5] = up.y;
00053     m[9] = up.z;
00054 
00055     m[2]  = -forward.x;
00056     m[6]  = -forward.y;
00057     m[10] = -forward.z;
00058 
00059         m.applyTranslate(-eye);
00060 }
00061 
00062 void Camera::computePerspective(GLMatrix4d &m)
00063 {       
00064         // This code is based off the MESA source for gluPerspective
00065 /*
00066         if (!mPerspectiveChanged)
00067         {
00068 */
00069                 GLdouble xmin, xmax, ymin, ymax;
00070                 
00071                 xmax = mPerspective.zNear * tan(mPerspective.fovx * M_PI / 360.0);
00072                 xmin = -xmax;
00073                    
00074                 ymin = xmin / mPerspective.aspect;
00075                 ymax = xmax / mPerspective.aspect;
00076                 
00077                 m[1] = m[2] = m[3] = m[4] = m[6] = m[7] = m[12] = m[13] = m[15] = 0;
00078                 
00079                 // Set up the projection matrix
00080                 m[0] = (GLdouble)((2.0 * mPerspective.zNear) / (xmax - xmin));
00081                 m[5] = (GLdouble)((2.0 * mPerspective.zNear) / (ymax - ymin));
00082                 m[10] = -(GLdouble)((mPerspective.zFar + mPerspective.zNear) / (mPerspective.zFar - mPerspective.zNear));
00083                 
00084                 m[8] = (GLdouble)((xmax + xmin) / (xmax - xmin));
00085                 m[9] = (GLdouble)((ymax + ymin) / (ymax - ymin));
00086                 m[11] = -1.0f;
00087                 
00088                 m[14] = -(GLdouble)((2.0 * mPerspective.zFar * mPerspective.zNear) / (mPerspective.zFar - mPerspective.zNear));
00089 /*      }*/
00090 }               
00091                 
00092 void Camera::gluLookAt()
00093 {
00094         GLMatrix4d m;
00095         computeLookAt(m);
00096         m.glMultMatrix();
00097 }
00098 
00099 void Camera::gluPerspective()
00100 {
00101         GLMatrix4d m;
00102         computePerspective(m);
00103         m.glMultMatrix();
00104 }
00105 
00106 void Camera::computeFrustum()
00107 {
00108         // compute combo-matrix for frustum calculation
00109         GLMatrix4d mv, mp;
00110         computePerspective(mp);
00111         computeLookAt(mv);
00112         
00113         // set new frustum
00114         mFrustum.set(mp * mv, true);
00115 }
00116 
00117 void Camera::setView()
00118 {
00119         glMatrixMode(GL_PROJECTION);
00120         glLoadIdentity();
00121         this->gluPerspective();
00122 
00123         glMatrixMode(GL_MODELVIEW);
00124         glLoadIdentity();
00125         this->gluLookAt();
00126 }

Generated on Wed Apr 12 13:55:27 2006 for bjs by  doxygen 1.4.5