Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

vpcamera.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 // ChangeLog
00006 // May 20, 2005 - Bruno de Oliveira Schneider
00007 // - Fixed vvBottom initialization (was 1, now is -1).
00008 // - GetDescription is now inline and returns by reference.
00009 // Dec 16, 2004 - Bruno de Oliveira Schneider
00010 // - Changed DrawOGL, so that it now sets the projection matrix.
00011 // - The projection type were changed from C preprocessor symbols to a public type
00012 //   ProjectionType. Changed GetProjectionType and SetProjectionType.
00013 // - Added aspectRatio attribute and related Get/Set methods.
00014 // - Renamed nearPlane and farPlane to nearPlaneDistance and farPlaneDistance, along
00015 //   with related Get/Set methods.
00016 // - Added vvLeft, vvRight, vvTop and vvBottom to allow any orthographic projection.
00017 //   Added related Get/Set methods.
00018 // - Commented out viewHeight, viewWidth, winBottomLeft, winTopRight and related
00019 //   Get/Set methods. It seems that they served to same purpose as vvLeft, vvRight, etc.
00020 // - Added an output operator.
00021 // - Added MoveSideways and MoveUp.
00022 // Sep 24, 2004 - Bruno de Oliveira Schneider
00023 // - Removed 'vp' preffix of every method name.
00024 // - Added methods 'YawAroundTarget', 'PitchAroundTarget', 'MoveForward' and 'Yaw'.
00025 // - Removed 'using namespace std;' from header file.
00026 // - Code typing has been cleaned (tabs replaced by spaces, excessive spaces
00027 //   removed, etc.) in the headers file because tabs and spaces were mixed together.
00028 // - Changed the class of "location", "target" and "up" to VPPoint4D.
00029 // - Added "const" to identify constant methods and to avoid copy large parameters
00030 //   (except to those that would require changing other classes).
00031 // - Added a copy constructor and "operator=" because this class is not static.
00032 // - Added "(position, focus, upVec)" constructor.
00033 // - Added "DrawOGL" method.
00034 // April/25/2001 - Isabel Harb Manssour
00035 // - New methods inclusion.
00036 // September/01/2000 - Julierme Krüger Gaviăo
00037 // - Gets and Sets of fovY attibute and ortho attributes modification.
00038 // June/13/2000 - Isabel Harb Manssour
00039 // - Gets and Sets update.
00040 // June 09, 2000 - Andre Rubin Santos
00041 // - Fixing Gets and Sets implementation.
00042 
00043 #include "vpcamera.h"
00044 #include "vptransform.h"
00045 #ifdef VP_OGL
00046 #include <GL/glu.h>
00047 #endif
00048 
00049 //?
00050 #include <iostream>
00051 
00052 using namespace std;
00053 
00054 VPCamera::VPCamera() {
00055     projectionType = PERSPECTIVE;
00056     fovY = 60;
00057     aspectRatio = 1.0f;
00058     vvLeft = -1.0;
00059     vvRight = 1.0;
00060     vvTop = 1.0;
00061     vvBottom = -1.0;
00062     nearPlaneDistance = 0.5f;
00063     farPlaneDistance = 100.0f;
00064 
00065     //~ viewHeight = 0;
00066     //~ viewWidth = 0;
00067 }
00068 
00069 VPCamera::VPCamera(const VPPoint4D& position, const VPPoint4D& focus, const VPPoint4D& upVec)
00070 {
00071     projectionType = PERSPECTIVE;
00072     fovY = 60;
00073     vvLeft = -1.0;
00074     vvRight = 1.0;
00075     vvTop = 1.0;
00076     vvBottom = -1.0;
00077     nearPlaneDistance = 0.5f;
00078     farPlaneDistance = 100.0f;
00079     aspectRatio = 1.0f;
00080     location = position;
00081     target = focus;
00082     up = upVec;
00083     // Make sure "up" is orthogonal to "front" and normalized
00084     VPPoint4D front = focus - position;
00085     up.Normalize();
00086     front.Normalize();
00087     VPPoint4D left = up.CrossProduct(front);
00088     up = front.CrossProduct(left);
00089 }
00090 
00091 VPCamera::VPCamera(const VPCamera& cam)
00092 {
00093     description = cam.description;
00094     projectionType = cam.projectionType;
00095     vvLeft = cam.vvLeft;
00096     vvRight = cam.vvRight;
00097     vvTop = cam.vvTop;
00098     vvBottom = cam.vvBottom;
00099     nearPlaneDistance = cam.nearPlaneDistance;
00100     farPlaneDistance = cam.farPlaneDistance;
00101     fovY = cam.fovY;
00102     location = cam.location;
00103     target = cam.target;
00104     up = cam.up;
00105     aspectRatio = cam.aspectRatio;
00106     //~ viewHeight = cam.viewHeight;
00107     //~ viewWidth = cam.viewWidth;
00108     //~ winBottomLeft = cam.winBottomLeft;
00109     //~ winTopRight = cam.winTopRight;
00110 }
00111 
00112 VPCamera& VPCamera::operator=(const VPCamera& cam)
00113 {
00114     description = cam.description;
00115     projectionType = cam.projectionType;
00116     vvLeft = cam.vvLeft;
00117     vvRight = cam.vvRight;
00118     vvTop = cam.vvTop;
00119     vvBottom = cam.vvBottom;
00120     nearPlaneDistance = cam.nearPlaneDistance;
00121     farPlaneDistance = cam.farPlaneDistance;
00122     fovY = cam.fovY;
00123     //~ viewHeight = cam.viewHeight;
00124     //~ viewWidth = cam.viewWidth;
00125     //~ winBottomLeft = cam.winBottomLeft;
00126     //~ winTopRight = cam.winTopRight;
00127     location = cam.location;
00128     target = cam.target;
00129     up = cam.up;
00130     aspectRatio = cam.aspectRatio;
00131     return (*this);
00132 }
00133 
00134 void VPCamera::SetDescription(const string& desc){
00135     description = desc;
00136 }
00137 
00138 //~ VPPoint2D VPCamera::GetWinBottomLeft() const {
00139     //~ return winBottomLeft;
00140 //~ }
00141 
00142 //~ void VPCamera::SetWinBottomLeft(VPPoint2D winBottomLeftValue){
00143     //~ winBottomLeft = winBottomLeftValue;
00144 //~ }
00145 
00146 // FixMe: This method should be const
00147 //~ void VPCamera::GetWinTopRight(float &x, float &y) {
00148     //~ x = winTopRight.vpGetX();
00149     //~ y = winTopRight.vpGetY();
00150 //~ }
00151 
00152 //~ void VPCamera::SetWinTopRight(float x, float y) {
00153     //~ winTopRight.vpSetX(x);
00154     //~ winTopRight.vpSetY(y);
00155 //~ }
00156 
00157 //~ VPPoint2D VPCamera::GetWinTopRight() const {
00158     //~ return winTopRight;
00159 //~ }
00160 
00161 //~ void VPCamera::SetWinTopRight(VPPoint2D winTopRightValue){
00162     //~ winTopRight = winTopRightValue;
00163 //~ }
00164 
00165 // FixMe: This method should be const
00166 //~ VPPoint2D VPCamera::GetWinLeftRight() {
00167     //~ VPPoint2D point(winBottomLeft.vpGetX(), winTopRight.vpGetY());
00168     //~ return point;
00169 //~ }
00170 
00171 //~ void VPCamera::SetWinLeftRight(VPPoint2D winLeftRightValue){
00172     //~ winBottomLeft.vpSetY(winLeftRightValue.vpGetX());
00173     //~ winTopRight.vpSetY(winLeftRightValue.vpGetY());
00174 //~ }
00175 
00176 // FixMe: This method should be const
00177 //~ VPPoint2D VPCamera::GetWinBottomTop() {
00178     //~ VPPoint2D point(winBottomLeft.vpGetX(), winTopRight.vpGetX());
00179     //~ return point;
00180 //~ }
00181 
00182 //~ void VPCamera::SetWinBottomTop(VPPoint2D winBottomTopValue){
00183     //~ winBottomLeft.vpSetX(winBottomTopValue.vpGetX());
00184     //~ winTopRight.vpSetX(winBottomTopValue.vpGetY());
00185 //~ }
00186 
00187 VPPoint4D VPCamera::GetLocation() const {
00188     return location;
00189 }
00190 
00191 void VPCamera::SetLocation(const VPPoint4D& locationValue){
00192     location = locationValue;
00193 }
00194 
00195 VPPoint4D VPCamera::GetTarget() const {
00196     return target;
00197 }
00198 
00199 void VPCamera::SetTarget(const VPPoint4D& targetValue){
00200     target = targetValue;
00201 }
00202 
00203 VPPoint4D VPCamera::GetUp() const {
00204     return up;
00205 }
00206 
00207 void VPCamera::SetUp(const VPPoint4D& upValue){
00208     up = upValue;
00209 }
00210 
00211 float VPCamera::GetFovY() const {
00212     return fovY;
00213 }
00214 
00215 void VPCamera::SetFovY(float f) {
00216     fovY = f;
00217 }
00218 
00219 void VPCamera::SetVisibleVolumeHeight(double newValue) {
00220     double halfHeight = newValue / 2;
00221     double halfWidth = halfHeight * aspectRatio;
00222     vvTop = halfHeight;
00223     vvBottom = -halfHeight;
00224     vvLeft = -halfWidth;
00225     vvRight = halfWidth;
00226 }
00227 
00228 //~ void VPCamera::SetViewDimension(int h, int w) {
00229     //~ viewHeight = h;
00230     //~ viewWidth = w;
00231 //~ }
00232 
00233 //~ void VPCamera::GetViewDimension(int &h, int &w) const {
00234     //~ h = viewHeight;
00235     //~ w = viewWidth;
00236 //~ }
00237 
00238 //~ int VPCamera::GetViewWidth() const {
00239     //~ return viewWidth;
00240 //~ }
00241 
00242 //~ int VPCamera::GetViewHeight() const {
00243     //~ return viewHeight;
00244 //~ }
00245 
00246 void VPCamera::YawAroundTarget(double radians) {
00247     VPTransform trans;
00248     trans.MakeRotation(target, up, radians);
00249     trans.ApplyTo(&location);
00250 }
00251 
00252 void VPCamera::Yaw(double radians) {
00253     VPTransform trans;
00254     trans.MakeRotation(location, up, radians);
00255     trans.ApplyTo(&target);
00256 }
00257 
00258 void VPCamera::PitchAroundTarget(double radians) {
00259     VPTransform trans;
00260     VPPoint4D left;
00261     VPPoint4D front = target - location;
00262     front.Normalize();
00263     left = up.CrossProduct(front);
00264     trans.MakeRotation(target, left, radians);
00265     trans.ApplyTo(&location);
00266     trans.ApplyTo(&up);
00267 }
00268 
00269 void VPCamera::MoveForward(double distance) {
00270     VPTransform trans;
00271     VPPoint4D front = target - location;
00272     front.Normalize();
00273     front *= distance;
00274     trans.MakeTranslation(front);
00275     trans.ApplyTo(&location);
00276     trans.ApplyTo(&target);
00277 }
00278 
00279 void VPCamera::MoveSideways(double distance) {
00280     VPTransform trans;
00281     VPPoint4D right;
00282     
00283     trans.MakeRotation(up,-1.5707963267948966192313216916398);
00284     right = trans * (target - location);
00285 
00286     right.Normalize();
00287     right *= distance;
00288     trans.MakeTranslation(right);
00289     trans.ApplyTo(&location);
00290     trans.ApplyTo(&target);
00291 }
00292 
00293 void VPCamera::MoveUp(double distance) {
00294     VPTransform trans;
00295     VPPoint4D translation(up);
00296 
00297     translation.Normalize();
00298     translation *= distance;
00299     trans.MakeTranslation(translation);
00300     trans.ApplyTo(&location);
00301     trans.ApplyTo(&target);
00302 }
00303 
00304 bool VPCamera::DrawOGL() const {
00305 #ifdef VP_OGL
00306     // It seems that setting the projection matrix at every rendering cycle is a
00307     // good idea because an application may have multiple cameras and multiple viewers.
00308     glMatrixMode(GL_PROJECTION);
00309     glLoadIdentity();
00310     if (projectionType == PERSPECTIVE)
00311         gluPerspective(fovY, aspectRatio, nearPlaneDistance, farPlaneDistance);
00312     else
00313         glOrtho(vvLeft, vvRight, vvBottom, vvTop, nearPlaneDistance, farPlaneDistance);
00314     glMatrixMode(GL_MODELVIEW);
00315     glLoadIdentity();
00316     gluLookAt(location.GetX(),location.GetY(),location.GetZ(),
00317               target.GetX(), target.GetY(), target.GetZ(),
00318               up.GetX(), up.GetY(), up.GetZ());
00319     return true;
00320 #else
00321     return false;
00322 #endif
00323 }
00324 
00325 ostream& operator<<(ostream& output, const VPCamera& cam)
00326 {
00327     output << "Camera(" << cam.description << ": pos"<< cam.location << " target" << cam.target
00328            << " up" << cam.up << " near(" << cam.nearPlaneDistance << ") far(" << cam.farPlaneDistance
00329            << "))";
00330     return output;
00331 }

Generated on Tue Sep 6 10:00:03 2005 for VPAT by  doxygen 1.4.4