camera.h

Go to the documentation of this file.
00001 /*
00002  * camera.h
00003  *
00004  * Copyright (C) 2008,2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Basic camera object.
00032  *
00033  * NOTE: These libraries use a right-handed coordinate system where positive Y
00034  * is up, and positive X is to the right.  The viewer is then looking at -Z.
00035  *
00036  * All angles are in radians unless noted otherwise.
00037  */
00038 
00039 #ifndef WAVE_GLUT_GLUT_CAMERA_H__
00040 #define WAVE_GLUT_GLUT_CAMERA_H__
00041 
00042 // includes --------------------------------------------------------------------
00043 #include <math.h>
00044 
00045 #include "geometry/placement.h"
00046 #include "perf/perf.h"
00047 
00048 
00049 namespace glut {
00050 
00051 /// \ingroup glut
00052 /*@{*/
00053 
00054 
00055 // TODO: should this be pulled into a shared library?  Not rendering-specific
00056 
00057 /// Class that represents the viewer of a 3D scene.
00058 class Viewer {
00059 public:
00060         // constructor ---------------------------------------------------------
00061         Viewer(void) throw();           // starts cleared
00062 
00063         // glut::Viewer public class methods -----------------------------------
00064         void clear(void) throw();               // position = rotations = zero
00065         void rotateY(IN float dy) throw();      // rotate around Y axis
00066         void rotateUp(IN float dz) throw();     // look up/down
00067         void move(IN float forward, IN float sideways, IN float up) throw();
00068         void setPosition(IN const point3d_t& pos) throw() { m_position = pos; }
00069         void setYRotation(IN float rot) throw() { m_yRot = rot; }
00070         void setUpRotation(IN float rot) throw() { m_upRot = rot; }
00071         point3d_t getPosition(void) const throw() { return m_position; }
00072         float getYRotation(void) const throw() { return m_yRot; }
00073         float getUpRotation(void) const throw() { return m_upRot; }
00074         point3d_t getFacing(void) const throw();
00075         point3d_t getUp(void) const throw();
00076 
00077 protected:
00078         // data fields ---------------------------------------------------------
00079         point3d_t               m_position;
00080         float                   m_yRot;         // rotation about local y-axis
00081         float                   m_upRot;        // look up/down
00082 };
00083 
00084 
00085 
00086 
00087 /// camera_t : basic physical properties of a camera
00088 struct camera_t {
00089         // constructor, manipulators -------------------------------------------
00090         camera_t(void) throw() { this->clear(); }
00091         void clear(void) throw() {
00092                         fovy = 45.0;            // default
00093                         aspect = 1.0;
00094                         zNear = 0.2;
00095                         zFar = 5000.0;
00096                 }
00097         float getFovy(void) const throw() { return fovy; }
00098         float getAspect(void) const throw() { return aspect; }
00099         float getZNear(void) const throw() { return zNear; }
00100         float getZFar(void) const throw() { return zFar; }
00101 
00102         void setAspect(IN float a) throw() { aspect = a; }
00103         void setAspect(IN int width, IN int height) throw()
00104                 {
00105                         ASSERT(width > 0, "Bad width: %d", width);
00106                         ASSERT(height > 0, "Bad height: %d", height);
00107                         this->setAspect((1.0 * width) / height);
00108                 }
00109         void setZFar(IN float z) throw() { zFar = z; }
00110 
00111 protected:
00112         // data fields ---------------------------------------------------------
00113         float   fovy;           // field of view, y-axis (DEGREES)
00114         float   aspect;         // aspect ratio
00115         float   zNear;          // near clipping plane
00116         float   zFar;           // far clipping plane
00117 };
00118 
00119 
00120 
00121 // TODO: pull fps_t out of here?   Not glut-specific...
00122 
00123 /// fps_t : handy object for frames-per-second calculations
00124 struct fps_t {
00125         fps_t(void) throw() { }
00126         fps_t(IN const perf::time_t& startTime) throw() { this->init(startTime); }
00127         void init(IN const perf::time_t& startTime) throw() {
00128                         m_startTime = startTime;
00129                         m_frames = 0;
00130                         m_fps = 0.0;
00131                 }
00132         void tick(IN const perf::time_t& currTime) throw() {
00133                         ++m_frames;
00134                         perf::time_t delta = currTime;
00135                         delta.decrement(m_startTime);
00136                         const float maxDelta = 3.0;
00137                         float deltaS = delta.getSeconds();
00138                         if (deltaS >= maxDelta) {
00139                                 m_fps = m_frames / deltaS;
00140                                 m_startTime = currTime;
00141                                 m_frames = 0;
00142                         }
00143                 }
00144         float getFPS(void) const throw() { return m_fps; }
00145 
00146         // data fields ---------------------------------------------------------
00147         perf::time_t    m_startTime;
00148         long            m_frames;
00149         float           m_fps;
00150 };
00151 
00152 
00153 /// getTrailingViewer() -- for over-the-shoulder views
00154 void getTrailingViewer(IN const Viewer& subject,
00155                         IN float distance_back,
00156                         OUT Viewer& view) throw();
00157 
00158 /// setOpenGLProjection() - set up OpenGL projection transformation
00159 void setOpenGLProjection(IN const camera_t& camera) throw();
00160 
00161 /// setOpenGLViewer() - set up initial OpenGL model/view transformation
00162 void setOpenGLViewer(IN const Viewer& viewer) throw();
00163 
00164 /// addQuaternionRotation() - rotates current matrix by given quaternion
00165 void addQuaternionRotation(IN const quaternion_t& q) throw();
00166 
00167 /// setPlacement() - updates current modelview transformation for placement
00168 void setPlacement(IN const placement_t& p) throw();
00169 
00170 
00171 };      // glut namespace
00172 
00173 #endif  // WAVE_GLUT_GLUT_CAMERA_H__
00174