00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 #if !defined(AFX_GLCAMERA_H__A41E0133_3B1F_11D4_BCBB_00A0C9A4CA3E__INCLUDED_) 00022 #define AFX_GLCAMERA_H__A41E0133_3B1F_11D4_BCBB_00A0C9A4CA3E__INCLUDED_ 00023 00024 #include <GLEXT/GLState.h> 00025 #include <common/Line.h> 00026 00027 /** 00028 A class that wraps setting and moving the current viewport. 00029 It sets the projection matrix and the modelview matrix such 00030 that any OpenGL calls will be "looking" at the given position. 00031 */ 00032 class GLCamera 00033 { 00034 public: 00035 typedef float (*MinHeightFunc)(int, int, void *); 00036 typedef float (*MaxHeightFunc)(int, int, void *); 00037 00038 /** 00039 Create the camera. 00040 The camera has a width and height that it will use for its 00041 viewport. 00042 */ 00043 GLCamera(GLsizei windowWidth, GLsizei windowHeight); 00044 virtual ~GLCamera(); 00045 00046 /** 00047 Sets a function that can be used to limit the cameras minimum 00048 height at a specified position. This can be used for example 00049 to prevent the camera from entering the landscape. 00050 */ 00051 void setMinHeightFunc(MinHeightFunc func, void *heightData = 0); 00052 void setMaxHeightFunc(MaxHeightFunc func, void *heightData = 0); 00053 /** 00054 Turns the user of the height function on or off. 00055 See setHeightFunc. 00056 */ 00057 void setUseHeightFunc(bool toggle); 00058 /** 00059 Sets the position that the camera will point at. 00060 The camera will gradualy move to look at this position unless 00061 the instant flag is given in which case the camera will 00062 move instantly. 00063 */ 00064 void setLookAt(Vector &lookAt, bool instant = false); 00065 00066 /** 00067 Sets the position that the camera will look from. 00068 */ 00069 void setCurrentPos(Vector &pos); 00070 /** 00071 Sets the position that the camera will look from. 00072 This position is relative to the current look at position. 00073 The camera will gradualy move to look from this position unless 00074 the instant flag is given in which case the camera will 00075 move instantly. 00076 */ 00077 void setOffSet(Vector &offSet, bool instant = false); 00078 Vector &getOffSet() { return wantedOffset_; } 00079 00080 /** 00081 Changes the current viewport size (w, h dimension) of the viewport. 00082 The viewport is the area of the window that is drawn to. 00083 */ 00084 void setWindowSize(GLsizei windowWidth, GLsizei windowHeight); 00085 /** 00086 Sets the current viewport location (x, y position) of the viewport. 00087 The viewport is the area of the window that is drawn to. 00088 */ 00089 void setWindowOffset(GLsizei windowLeft, GLsizei windowTop); 00090 00091 /** 00092 Causes the current model and projection matrixs to be replaced 00093 so they are "looking" from the from position to the to position. 00094 */ 00095 void draw(); 00096 /** 00097 Causes the camera to move if gradual movements are being made. 00098 */ 00099 void simulate(float frameTime = 0.02f); 00100 /** 00101 Causes the camera to shake the viewport randomly. 00102 This can be used to simulate ground shake. 00103 e.g. during large explosions. 00104 The larger the shake the longer the camera will shake. 00105 */ 00106 void addShake(float shake); 00107 00108 /** 00109 Moves the current look from position to a new location. 00110 The current look from position in overwritten. 00111 The look from position is relative to the look at position. 00112 XY = horizontal rotation for the new point from the old 00113 YZ = vertical rotation for the new point from the old 00114 Z = Zoom closeness 00115 */ 00116 void movePosition(float XY, float YZ, float Z); 00117 /** 00118 Moves the current look from position to a new location. 00119 This position is relative to the current look from position. 00120 The look from position is relative to the look at position. 00121 XY = horizontal rotation for the new point from the old 00122 YZ = vertical rotation for the new point from the old 00123 Z = Zoom closeness 00124 */ 00125 void movePositionDelta(float XY, float YZ, float Z); 00126 /** 00127 Uses the current matrixs to turn a two 2D points into 00128 a 3D line. 00129 */ 00130 bool getDirectionFromPt(GLfloat ptX, GLfloat ptY, Line &direction); 00131 00132 /** 00133 Get the point the camera is currently looking from 00134 */ 00135 Vector &getCurrentPos() { return currentPosition_; } 00136 /** 00137 Get the point the camera is currently looking at (observing) 00138 */ 00139 Vector &getLookAt() { return lookAt_; } 00140 /** 00141 Get the speed of movement of the camera looking from position 00142 */ 00143 Vector &getVelocity() { return velocity_; } 00144 00145 /** 00146 Returns the current camera horizontal rotation. 00147 As set by the move position methods. 00148 */ 00149 float getRotationXY() { return rotationXY_; } 00150 /** 00151 Returns the current camera vertical rotation. 00152 As set by the move position methods. 00153 */ 00154 float getRotationYZ() { return rotationYZ_; } 00155 /** 00156 Returns the current camera zoom. 00157 As set by the move position methods. 00158 */ 00159 float getZoom() { return zoom_; } 00160 00161 /** 00162 Scrolls the camera along the ground, in the direction given. 00163 */ 00164 enum ScrollDir 00165 { 00166 eScrollLeft, 00167 eScrollRight, 00168 eScrollUp, 00169 eScrollDown 00170 }; 00171 void scroll(ScrollDir direction, float minWidth, float minHeight, 00172 float maxWidth, float maxHeight, float amount); 00173 void scroll(float x, float y, float minWidth, float minHeight, 00174 float maxWidth, float maxHeight); 00175 00176 static GLCamera *getCurrentCamera() { return currentCamera_; } 00177 00178 protected: 00179 static GLCamera *currentCamera_; 00180 GLsizei windowW_, windowH_; 00181 GLsizei windowL_, windowT_; 00182 GLfloat windowAspect_; 00183 GLfloat rotationXY_, rotationYZ_, zoom_; 00184 float shake_; 00185 float totalTime_; 00186 Vector shakeV_; 00187 bool useHeightFunc_; 00188 Vector lookAt_; 00189 Vector wantedLookAt_; 00190 Vector wantedOffset_; 00191 Vector currentPosition_; 00192 Vector velocity_; 00193 MinHeightFunc minHeightFunc_; 00194 MaxHeightFunc maxHeightFunc_; 00195 void *minHeightData_; 00196 void *maxHeightData_; 00197 00198 virtual void calculateWantedOffset(); 00199 virtual void moveViewport(Vector &lookFrom, Vector &lookAt); 00200 00201 }; 00202 00203 #endif // !defined(AFX_GLCAMERA_H__A41E0133_3B1F_11D4_BCBB_00A0C9A4CA3E__INCLUDED_)
1.5.3