00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
00066
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
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
00107
00108
00109
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
00124
00125
00126
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
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
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
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
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
00307
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 }