00001 #ifdef WIN32
00002
00003 #include <windows.h>
00004 #endif
00005
00006 #include <string.h>
00007 #include <stdlib.h>
00008 #include <math.h>
00009 #include <iostream.h>
00010 #include <GL/gl.h>
00011 #include <GL/glu.h>
00012 #include "vuCamera.h"
00013
00014 #include "vuPerspectiveCamera.h"
00015
00016 #ifndef M_PI
00017 #define M_PI 3.141592653589793238462643383279502884197169399375105820974944
00018
00019
00020 #endif
00021
00022
00023 vuPerspectiveCamera::vuPerspectiveCamera() : m_FOV((float)45), m_Aspect((float)1.333333333)
00024
00025 {
00026 m_Position = vuVector(0,0,0);
00027 m_LookAtVector = vuVector(0,0,-1);
00028 m_UpVector = vuVector(0,1,0);
00029
00030 fp = NULL;
00031 line_number = 0;
00032 m_IsRecording = 0;
00033
00034 m_IsChanged = true;
00035 }
00036
00037 void vuPerspectiveCamera::set_defaults ()
00038
00039 {
00040 m_Position = vuVector(0,0,0);
00041 m_LookAtVector = vuVector(0,0,-1);
00042 m_UpVector = vuVector(0,1,0);
00043
00044
00045
00046
00047
00048
00049 m_IsChanged = true;
00050 }
00051
00052
00053 vuPerspectiveCamera::vuPerspectiveCamera(const vuPerspectiveCamera& c)
00054 {
00055 m_Position = c.m_Position;
00056 m_LookAtVector = c.m_LookAtVector;
00057 m_UpVector = c.m_UpVector;
00058 m_RightVector = c.m_RightVector;
00059 m_FOV = c.m_FOV;
00060 m_IsRecording = 0;
00061 fp = NULL;
00062 line_number = 0;
00063
00064 m_Aspect = c.m_Aspect;
00065 m_Width = c.m_Width;
00066 m_Height = c.m_Height;
00067 m_TopLeft = c.m_TopLeft;
00068 m_XStep = c.m_XStep;
00069 m_YStep = c.m_YStep;
00070
00071 m_IsChanged = true;
00072 }
00073
00074
00075 vuPerspectiveCamera::~vuPerspectiveCamera()
00076 {
00077 clear_lines ();
00078 }
00079
00080
00081 float vuPerspectiveCamera::getFOV(void)
00082 {
00083 return m_FOV;
00084 }
00085
00086
00087 void vuPerspectiveCamera::setFOV(float fov)
00088 {
00089 m_FOV = fov;
00090
00091 m_IsChanged = true;
00092 }
00093
00094
00095 float vuPerspectiveCamera::getAspect(void)
00096 {
00097 return m_Aspect;
00098 }
00099
00100
00101 void vuPerspectiveCamera::setAspect(float aspect)
00102 {
00103 m_Aspect = aspect;
00104
00105 m_IsChanged = true;
00106 }
00107
00108 vuVector vuPerspectiveCamera::getTopLeft (void)
00109
00110 {
00111 return m_TopLeft;
00112 }
00113
00114 void vuPerspectiveCamera::setTopLeft (vuVector &temp)
00115
00116 {
00117 init ();
00118
00119 m_IsChanged = true;
00120 }
00121
00122 vuVector vuPerspectiveCamera::getXStep (void)
00123
00124 {
00125 return m_XStep;
00126 }
00127
00128 void vuPerspectiveCamera::setXStep (vuVector &temp)
00129
00130 {
00131 init ();
00132
00133 m_IsChanged = true;
00134 }
00135
00136 vuVector vuPerspectiveCamera::getYStep (void)
00137
00138 {
00139 return m_YStep;
00140 }
00141
00142 void vuPerspectiveCamera::setYStep (vuVector &temp)
00143
00144 {
00145 init ();
00146
00147 m_IsChanged = true;
00148 }
00149
00150
00151
00152
00153
00154 void vuPerspectiveCamera::init(void)
00155 {
00156 vuVector a;
00157 vuVector b;
00158 float HalfWidth;
00159 float HalfHeight;
00160
00161 if (m_Width<0)
00162 throw "CAMERA: width of view plane must be 0 or more pixels.";
00163 if (m_Height<0)
00164 throw "CAMERA: height of view plane must be 0 or more pixels.";
00165
00166
00167
00168 a = m_LookAtVector;
00169 vuVector vup = m_UpVector;
00170 vup.makeUnit();
00171 b = a.cross(vup);
00172 b.makeUnit();
00173 if ((b[0] == 0) && (b[1] == 0) && (b[2] == 0))
00174 throw "CAMERA: eye-vector and up-vector are coincident.";
00175
00176 HalfWidth = (float)tan(0.5*M_PI/180.0*m_FOV)*a.norm();
00177 HalfHeight = HalfWidth/m_Aspect;
00178 vuVector tmpvec = b*HalfWidth;
00179
00180 m_TopLeft = (HalfHeight*m_UpVector)-(tmpvec)+m_LookAtVector+m_Position;
00181 m_XStep = b*(2.0f*HalfWidth/(float)m_Width);
00182 m_YStep = m_UpVector*(-2.0f*HalfHeight/(float)m_Height);
00183 m_TopLeft = m_TopLeft + m_XStep;
00184 tmpvec = m_YStep*0.5f;
00185 m_TopLeft = m_TopLeft + tmpvec;
00186 m_TopLeft -= m_Position;
00187
00188 m_IsChanged = true;
00189 }
00190
00191
00192
00193 vuRay vuPerspectiveCamera::getRay(float xpixel, float ypixel)
00194 {
00195 vuRay r;
00196 if ((xpixel>m_Width-0.5) || (xpixel<-0.5) ||
00197 (ypixel>m_Height-0.5) || (ypixel<-0.5))
00198 throw "CAMERA: attempt to shoot ray outside view plane.";
00199 r.m_Position = m_Position;
00200 r.m_Direction = m_TopLeft + (m_XStep*xpixel) + (m_YStep*ypixel);
00201 return r;
00202 }
00203
00204
00205 vuPerspectiveCamera& vuPerspectiveCamera::operator=(const vuPerspectiveCamera& rhs)
00206 {
00207 if (this == &rhs)
00208 return *this;
00209
00210 cout<< "nice equals" << endl;
00211 m_Position = rhs.m_Position;
00212 m_LookAtVector = rhs.m_LookAtVector;
00213 m_UpVector = rhs.m_UpVector;
00214 m_RightVector = rhs.m_RightVector;
00215 m_FOV = rhs.m_FOV;
00216 m_Aspect = rhs.m_Aspect;
00217 m_Width = rhs.m_Width;
00218 m_Height = rhs.m_Height;
00219 m_TopLeft = rhs.m_TopLeft;
00220 m_XStep = rhs.m_XStep;
00221 m_YStep = rhs.m_YStep;
00222
00223 m_IsChanged = true;
00224
00225 return *this;
00226 }
00227
00228 void vuPerspectiveCamera::glInit()
00229 {
00230
00231 ::gluPerspective(m_FOV, m_Aspect, 1.0, 10000.0);
00232
00233 }
00234
00235 void vuPerspectiveCamera::TakeSnapShot ()
00236
00237 {
00238 if (m_IsRecording)
00239
00240 {
00241 char Shot [1024];
00242
00243 TakeSnapShot (Shot);
00244
00245 fputs (Shot, fp);
00246 }
00247 }
00248
00249 void vuPerspectiveCamera::TakeSnapShot (char* Shot)
00250
00251 {
00252 char temp [512];
00253
00254 TakeSnapShotBasic (Shot);
00255
00256 TakeSnapShotPersp (temp);
00257
00258 strcat (Shot, ", ");
00259 strcat (Shot, temp);
00260 }
00261
00262 void vuPerspectiveCamera::TakeSnapShotPersp (char* Shot)
00263
00264 {
00265 char temp [256];
00266
00267 gcvt (m_FOV, 14, Shot);
00268 strcat (Shot, ", \0");
00269
00270 gcvt (m_Aspect, 14, temp);
00271 strcat (Shot, temp);
00272 strcat (Shot, ", \0");
00273
00274
00275 gcvt (m_Width, 10, temp);
00276 strcat (Shot, temp);
00277 strcat (Shot, ", \0");
00278
00279
00280 gcvt (m_Height, 10, temp);
00281 strcat (Shot, temp);
00282 strcat (Shot, ", (\0");
00283
00284 m_TopLeft.save (temp);
00285 strcat (Shot, temp);
00286 strcat (Shot, "), (\0");
00287
00288 m_XStep.save (temp);
00289 strcat (Shot, temp);
00290 strcat (Shot, "), (\0");
00291
00292 m_YStep.save (temp);
00293 strcat (Shot, temp);
00294 strcat (Shot, ")\n\0");
00295 }
00296
00297 int vuPerspectiveCamera::RestoreShot (char* Shot)
00298
00299 {
00300 int position = RestoreShotBasic (Shot);
00301
00302 position += get_next_comma (&(Shot [position]));
00303
00304 return position + RestoreShotPersp (&(Shot [position]));
00305 }
00306
00307 int vuPerspectiveCamera::RestoreShotPersp (char* Shot)
00308
00309 {
00310 int position = 0;
00311
00312 m_FOV = atof (&(Shot [position]));
00313
00314 position = get_next_comma (&(Shot [position])) + 1;
00315 position += clear_blanks (&(Shot [position]));
00316
00317 m_Aspect = atof (&(Shot [position]));
00318
00319 position += get_next_comma (&(Shot [position])) + 1;
00320 position += clear_blanks (&(Shot [position]));
00321
00322 m_Width = atoi (&(Shot [position]));
00323
00324 position += get_next_comma (&(Shot [position])) + 1;
00325 position += clear_blanks (&(Shot [position]));
00326
00327 m_Height = atoi (&(Shot [position]));
00328
00329 position += get_next_comma (&(Shot [position])) + 1;
00330 position += clear_blanks (&(Shot [position]));
00331
00332 position += get_next_open (&(Shot [position])) + 1;
00333 m_TopLeft.load (&(Shot [position]));
00334 position += get_next_close (&(Shot [position])) + 1;
00335
00336 position += get_next_open (&(Shot [position])) + 1;
00337 m_XStep.load (&(Shot [position]));
00338 position += get_next_close (&(Shot [position])) + 1;
00339
00340 position += get_next_open (&(Shot [position])) + 1;
00341 m_YStep.load (&(Shot [position]));
00342 position += get_next_close (&(Shot [position])) + 1;
00343
00344 return position;
00345 }
00346
00347 int vuPerspectiveCamera::RestoreNextShot ()
00348
00349 {
00350
00351
00352 if (IsNextAvailable ())
00353
00354 {
00355
00356
00357 int position = RestoreShotBasic (lines [++line_number]);
00358
00359 position += get_next_comma (&((lines [line_number]) [position])) + 1;
00360 return RestoreShotPersp (&((lines [line_number]) [position]));
00361 }
00362
00363 return -1;
00364 }
00365
00366 int vuPerspectiveCamera::RestorePreviousShot ()
00367
00368 {
00369
00370 if (IsPreviousAvailable ())
00371
00372 {
00373 int position = RestoreShotBasic (lines [--line_number]);
00374
00375 position += get_next_comma (&((lines [line_number]) [position])) + 1;
00376 return RestoreShotPersp (&((lines [line_number]) [position]));
00377 }
00378
00379 return -1;
00380 }
00381
00382
00383 char id_string_cgs_xq2 [] = "vuPerspectiveCameraD1.00__\0";
00384
00385 char* vuPerspectiveCamera::get_id ()
00386
00387 {
00388 return id_string_cgs_xq2;
00389 }
00390
00391 bool vuPerspectiveCamera::verify_id (char* id)
00392
00393 {
00394 return (strncmp (id, get_id (), strlen (get_id ())) == 0);
00395 }
00396
00397 ostream& operator<<(ostream& out, vuPerspectiveCamera &cam)
00398
00399 {
00400 char Shot [1024];
00401
00402 cam.TakeSnapShot (Shot);
00403
00404 out << Shot;
00405
00406 cam.m_IsChanged = true;
00407
00408 return out;
00409 }
00410
00411 istream& operator>>(istream& in, vuPerspectiveCamera &cam)
00412
00413 {
00414 char Shot [1024];
00415
00416 in >> Shot;
00417
00418 cam.RestoreShot (Shot);
00419
00420 return in;
00421 }
00422
00423 vuCamera* vuPerspectiveCamera::create_new ()
00424
00425 {
00426 vuPerspectiveCamera* ptr = new vuPerspectiveCamera [1];
00427
00428 return (vuCamera*) (ptr);
00429 }
00430
00431 vuPerspectiveCamera temp_cam_vpc_cgs__;
00432
00433 vuCamera* vuPerspectiveCamera::operator* (float t)
00434
00435 {
00436 vuPerspectiveCamera* temp_cam_vpc_cgs__ = (vuPerspectiveCamera *) (create_new ());
00437
00438
00439
00440 temp_cam_vpc_cgs__->setPosition (m_Position * t);
00441 temp_cam_vpc_cgs__->setUpVector (m_UpVector * t);
00442 temp_cam_vpc_cgs__->setLookAtVector (m_LookAtVector * t);
00443
00444
00445 temp_cam_vpc_cgs__->setFOV (m_FOV * t);
00446 temp_cam_vpc_cgs__->setAspect (m_Aspect * t);
00447
00448 temp_cam_vpc_cgs__->setWidth((int)(m_Width * t));
00449 temp_cam_vpc_cgs__->setHeight((int)(m_Height * t));
00450
00451 temp_cam_vpc_cgs__->init ();
00452
00453 m_IsChanged = true;
00454
00455 return (vuCamera *) (temp_cam_vpc_cgs__);
00456 }
00457
00458 vuCamera* vuPerspectiveCamera::operator*= (float t)
00459
00460 {
00461
00462
00463
00464 setPosition (m_Position *= t);
00465 setUpVector (m_UpVector *= t);
00466 setLookAtVector (m_LookAtVector *= t);
00467
00468
00469 setFOV (m_FOV *= t);
00470 setAspect (m_Aspect *= t);
00471
00472 m_Width = (int)(m_Width * t);
00473 m_Height = (int)(m_Height * t);
00474
00475 setWidth(m_Width);
00476 setHeight(m_Height);
00477
00478 init ();
00479
00480 m_IsChanged = true;
00481
00482 return (vuCamera *) (this);
00483 }
00484
00485 vuCamera* operator* (float t, vuPerspectiveCamera &cam)
00486
00487 {
00488 vuPerspectiveCamera* temp_cam_vpc_cgs__ = (vuPerspectiveCamera *) (cam.create_new ());
00489
00490
00491
00492 temp_cam_vpc_cgs__->setPosition (cam.m_Position * t);
00493 temp_cam_vpc_cgs__->setUpVector (cam.m_UpVector * t);
00494 temp_cam_vpc_cgs__->setLookAtVector (cam.m_LookAtVector * t);
00495
00496
00497 temp_cam_vpc_cgs__->setFOV (cam.m_FOV * t);
00498 temp_cam_vpc_cgs__->setAspect (cam.m_Aspect * t);
00499
00500 temp_cam_vpc_cgs__->setWidth((int)(cam.m_Width * t));
00501 temp_cam_vpc_cgs__->setHeight((int)(cam.m_Height * t));
00502
00503 temp_cam_vpc_cgs__->init ();
00504
00505 cam.m_IsChanged = true;
00506
00507 return (vuCamera *) (temp_cam_vpc_cgs__);
00508 }
00509
00510 vuCamera* vuPerspectiveCamera::operator+ (vuPerspectiveCamera &rhs)
00511
00512 {
00513 vuPerspectiveCamera* temp_cam_vpc_cgs__ = (vuPerspectiveCamera *) (create_new ());
00514
00515
00516
00517 temp_cam_vpc_cgs__->setPosition (m_Position + rhs.getPosition ());
00518 temp_cam_vpc_cgs__->setUpVector (m_UpVector + rhs.getUpVector ());
00519 temp_cam_vpc_cgs__->setLookAtVector (m_LookAtVector + rhs.getLookAtVector ());
00520
00521
00522 temp_cam_vpc_cgs__->setFOV (m_FOV + rhs.getFOV ());
00523 temp_cam_vpc_cgs__->setAspect (m_Aspect + rhs.getAspect ());
00524
00525 temp_cam_vpc_cgs__->setWidth(m_Width + rhs.getWidth ());
00526 temp_cam_vpc_cgs__->setHeight(m_Height + rhs.getHeight ());
00527
00528 temp_cam_vpc_cgs__->init ();
00529
00530 m_IsChanged = true;
00531
00532 return (vuCamera *) (temp_cam_vpc_cgs__);
00533 }
00534
00535 vuCamera* vuPerspectiveCamera::operator+= (vuPerspectiveCamera &rhs)
00536
00537 {
00538
00539
00540 setPosition (m_Position += rhs.getPosition ());
00541 setUpVector (m_UpVector += rhs.getUpVector ());
00542 setLookAtVector (m_LookAtVector += rhs.getLookAtVector ());
00543
00544
00545 setFOV (m_FOV += rhs.getFOV ());
00546 setAspect (m_Aspect += rhs.getAspect ());
00547
00548 setWidth(m_Width += rhs.getWidth ());
00549 setHeight(m_Height += rhs.getHeight ());
00550
00551 init ();
00552
00553 m_IsChanged = true;
00554
00555 return (vuCamera *) (this);
00556 }
00557
00558 vuCamera* vuPerspectiveCamera::operator+ (vuPerspectiveCamera *rhs)
00559
00560 {
00561 vuPerspectiveCamera* temp_cam_vpc_cgs__ = (vuPerspectiveCamera *) (create_new ());
00562
00563
00564
00565 temp_cam_vpc_cgs__->setPosition (m_Position + rhs->getPosition ());
00566 temp_cam_vpc_cgs__->setUpVector (m_UpVector + rhs->getUpVector ());
00567 temp_cam_vpc_cgs__->setLookAtVector (m_LookAtVector + rhs->getLookAtVector ());
00568
00569
00570 temp_cam_vpc_cgs__->setFOV (m_FOV + rhs->getFOV ());
00571 temp_cam_vpc_cgs__->setAspect (m_Aspect + rhs->getAspect ());
00572
00573 temp_cam_vpc_cgs__->setWidth(m_Width + rhs->getWidth ());
00574 temp_cam_vpc_cgs__->setHeight(m_Height + rhs->getHeight ());
00575
00576 temp_cam_vpc_cgs__->init ();
00577
00578 m_IsChanged = true;
00579
00580 return (vuCamera *) (temp_cam_vpc_cgs__);
00581 }
00582
00583 vuCamera* vuPerspectiveCamera::operator+= (vuPerspectiveCamera *rhs)
00584
00585 {
00586
00587
00588 setPosition (m_Position += rhs->getPosition ());
00589 setUpVector (m_UpVector += rhs->getUpVector ());
00590 setLookAtVector (m_LookAtVector += rhs->getLookAtVector ());
00591
00592
00593 setFOV (m_FOV += rhs->getFOV ());
00594 setAspect (m_Aspect += rhs->getAspect ());
00595
00596 setWidth(m_Width += rhs->getWidth ());
00597 setHeight(m_Height += rhs->getHeight ());
00598
00599 init ();
00600
00601 m_IsChanged = true;
00602
00603 return (vuCamera *) (&temp_cam_vpc_cgs__);
00604 }
00605
00606 vuPerspectiveCamera* vuPerspectiveCamera::operator= (vuPerspectiveCamera *rhs)
00607
00608 {
00609 m_Position = rhs->m_Position;
00610 m_UpVector = rhs->m_UpVector;
00611 m_LookAtVector = rhs->m_LookAtVector;
00612 m_RightVector = rhs->m_RightVector;
00613
00614 m_FOV = rhs->m_FOV;
00615 m_Aspect = rhs->m_Aspect;
00616
00617 m_Width = rhs->m_Width;
00618 m_Height = rhs->m_Height;
00619
00620 m_TopLeft = rhs->m_TopLeft;
00621 m_XStep = rhs->m_XStep;
00622 m_YStep = rhs->m_YStep;
00623
00624 m_IsChanged = true;
00625
00626 return this;
00627 }
00628
00629 vuCamera* vuPerspectiveCamera::set_equal_to_interp (vuCamera* cam1, vuCamera* cam2, float t1, float t2)
00630
00631 {
00632 vuPerspectiveCamera *pcam1 = (vuPerspectiveCamera *) (cam1);
00633 vuPerspectiveCamera *pcam2 = (vuPerspectiveCamera *) (cam2);
00634
00635 m_Position = pcam1->m_Position * t1;
00636 m_UpVector = pcam1->m_UpVector * t1;
00637 m_LookAtVector = pcam1->m_LookAtVector * t1;
00638 m_RightVector = pcam1->m_RightVector * t1;
00639
00640 m_FOV = pcam1->m_FOV * t1;
00641 m_Aspect = pcam1->m_Aspect * t1;
00642
00643 m_Width = (int)(pcam1->m_Width *t1 + pcam2->m_Width*t2);
00644 m_Height = (int)(pcam1->m_Height*t1 + pcam2->m_Height*t2);
00645 m_TopLeft = pcam1->m_TopLeft * t1;
00646 m_XStep = pcam1->m_XStep * t1;
00647 m_YStep = pcam1->m_YStep * t1;
00648
00649 m_Position += pcam2->m_Position * t2;
00650 m_UpVector += pcam2->m_UpVector * t2;
00651 m_LookAtVector += pcam2->m_LookAtVector * t2;
00652 m_RightVector += pcam2->m_RightVector * t2;
00653
00654 m_FOV += pcam1->m_FOV * t2;
00655 m_Aspect += pcam1->m_Aspect * t2;
00656 m_TopLeft += pcam1->m_TopLeft * t2;
00657 m_XStep += pcam1->m_XStep * t2;
00658 m_YStep += pcam1->m_YStep * t2;
00659
00660
00661
00662
00663
00664
00665 cout << "t1:" << t1 << "t2:" << t2 << endl;
00666
00667 m_IsChanged = true;
00668
00669 return (vuCamera *) (pcam1);
00670 }
00671