Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

WP_Camera.h

Go to the documentation of this file.
00001 /* Copyright (C) 2001 W.P. van Paassen - peter@paassen.tmfweb.nl
00002 
00003    This program is free software; you can redistribute it and/or modify it under
00004    the terms of the GNU General Public License as published by the Free
00005    Software Foundation; either version 2 of the License, or (at your
00006    option) any later version.
00007 
00008    This program is distributed in the hope that it will be useful, but WITHOUT
00009    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00010    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00011    for more details.
00012 
00013    You should have received a copy of the GNU General Public License
00014    along with this program; see the file COPYING.  If not, write to the Free
00015    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
00016 
00017 #ifndef WP_CAMERA_H
00018 #define WP_CAMERA_H
00019 
00020 #include "WPCG.h"
00021 
00022 namespace WPCG
00023 {
00024 //forward declaration
00025 class WP_Object;
00026 
00045 class WP_Camera
00046 {
00047 public:
00048         ~WP_Camera(){};
00049 
00056         inline void slide(scalar deltaU, scalar deltaV, scalar deltaN)
00057           {
00058             WP_Vector3D translateU(u * deltaU);
00059             WP_Vector3D translateV(v * deltaV);
00060             WP_Vector3D translateN(n * deltaN);
00061             
00062             WP_Vector3D translate = translateU + translateV + translateN;
00063             eye += translate;
00064             setModelViewMatrixGL();
00065           }
00066 
00073         void rotate(scalar angleU, scalar angleV, scalar angleN);
00074 
00079         inline void pitch(scalar angle)
00080           {
00081             rotate(angle, 0.0f, 0.0f);
00082           }
00083 
00088         inline void roll(scalar angle)
00089           {
00090             rotate(0.0f, 0.0f, angle);
00091           }
00092 
00097         inline void yaw(scalar angle)
00098           {
00099             rotate(0.0f, angle, 0.0f);
00100           }
00101 
00106         static WP_Camera* getInstance()
00107         { 
00108                 if (!_instance)
00109                 {
00110                         _instance = new WP_Camera();
00111                 }
00112                 return _instance;
00113         };
00114 
00118         void followObject();
00119 
00131         void setFrustumAndCamera(scalar _viewAngle, unsigned int width, unsigned int height, scalar _nearPlane, scalar _farPlane, 
00132                                  const WP_Point3D& _eye, const WP_Point3D& _look, const WP_Vector3D& _up);
00133 
00141         void setPickingVolume(int width, int height, int x, int y);
00142         
00146         inline void setRenderVolume()
00147           {
00148             if (!normal_viewing_volume)
00149               {
00150                 state->projection();
00151                 glPopMatrix();
00152                 state->modelview();
00153                 normal_viewing_volume = true;
00154               }
00155           }
00156 
00163         inline WP_Ray3D createRayForTracing(int x, int y) const
00164           {
00165             WP_Ray3D ray;
00166             ray.start = eye;
00167             
00168             float H = nearPlane * tan(math->degreeToRad((int)(viewAngle / 2.0f)));
00169             float W = H * aspectRatio;
00170             
00171             ray.direction = n * -nearPlane + u * (-W + (W * ((x * 2.0f) / (float)screen_width))) + v * (-H + (H * ((y * 2.0f) / (float)screen_height)));
00172             return ray;
00173           }
00174 
00178         WP_Point3D eye;
00179 
00183         WP_Point3D look;
00184 
00188         WP_Vector3D up;
00189 
00193         WP_Matrix3D matrix;
00194 
00198         const WP_Object* fixed_object;
00199 
00203         scalar follow_distance;
00204 
00208         int follow_angleX;
00209 
00213         int follow_angleY;
00214 
00218         int follow_angleZ;
00219 
00223         int objects_in_frustum;
00224 
00228         unsigned int screen_width;
00229 
00233         unsigned int screen_height;
00234 
00238         bool normal_viewing_volume;
00239 
00240         Plane* getFrustum() 
00241           {
00242             return frustum;
00243           }
00244 
00245 private:
00246         WP_Camera();
00247 
00251         WP_Vector3D u;
00252 
00256         WP_Vector3D v;
00257 
00261         WP_Vector3D n;
00262         
00266         scalar viewAngle;
00267         
00271         scalar aspectRatio;
00272 
00276         scalar nearPlane;
00277 
00281         scalar farPlane;
00282 
00286         Plane frustum[6];
00287 
00291         static WP_Camera* _instance;
00292         
00296         inline void setModelViewMatrixGL()
00297           {
00298             matrix = WP_Matrix3D(eye.toVector(), u, v, n);
00299             glLoadMatrixf(matrix.data);
00300             computeFrustum(); //FIXME don't compute the frustum planes everytime, just translate, rotate etc them
00301           }
00302 
00306         void computeFrustum();
00307 
00311         WP_Math* math;
00312 
00316         WP_GLState* state;
00317 };
00318 }
00319 #endif
00320 

Generated on Tue Jan 28 20:26:33 2003 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002