Hauptseite | Liste aller Namensbereiche | Klassenhierarchie | Alphabetische Liste | Datenstrukturen | Auflistung der Dateien | Datenstruktur-Elemente | Datei-Elemente

Camera.cpp

gehe zur Dokumentation dieser Datei
00001 /*
00002 Autor: $Author: kunkel $ State: $State: Exp $
00003 Datum: $Date: 2005/05/30 12:35:25 $
00004 Version: $Revision: 1.1 $
00005 */
00006 
00012 #include <GL/glut.h>
00013 
00014 #include "Camera.h"
00015 #include "Configuration.h"
00016 #include "Message.h"
00017 
00018 Camera ourCamera;
00019 
00023 void Camera::saveAktuallCameraPosition(int to) {
00024     if( to > CAMERA_SAVES)
00025         return;
00026     // speichere tatsaechliche Position indem der Einfluss vom Skalierungsfaktor
00027     // herausgerechnet wird
00028     positionList[to].place = place / ourConfiguration.linearScaleFactor;
00029     positionList[to].upper = upper / ourConfiguration.linearScaleFactor;
00030     positionList[to].projectionCenter = projectionCenter /
00031                                         ourConfiguration.linearScaleFactor;
00032 }
00033 
00038 void Camera::loadAktuallCameraPosition(int from) {
00039     if( from > CAMERA_SAVES)
00040         return;
00041     // passe die Cameraposition mit dem aktuellen Skalierungsfaktor an
00042     place = positionList[from].place * ourConfiguration.linearScaleFactor;
00043     upper = positionList[from].upper * ourConfiguration.linearScaleFactor;
00044     projectionCenter = positionList[from].projectionCenter *
00045                        ourConfiguration.linearScaleFactor;
00046     reloadAutomaticAdaption();
00047 }
00048 
00052 Camera::Camera(): screenwidth (800),screenheight (650),action(NONE) {
00053     home();
00054     for(int i=0; i < CAMERA_SAVES; i++)
00055         saveAktuallCameraPosition(i);
00056 }
00057 
00062 void Camera::saveCamera(ostream & FILE) {
00063     FILE << "!!BEGINCAMERA!!" << endl;
00064     FILE << "0 place: " <<  place << endl;
00065     FILE << "1 upper: " <<  upper << endl;
00066     FILE << "2 projectionCenter: " <<  projectionCenter << endl;
00067     FILE << "3 aperture: " <<  aperture << endl;
00068     FILE << "4 CameraSaveSlots: " << CAMERA_SAVES << endl ;
00069     for(int i=0; i < CAMERA_SAVES; i++)
00070         FILE << positionList[i].place << positionList[i].upper <<
00071         positionList[i].projectionCenter << endl;
00072     FILE << "1000 !!END CAMERA!!"<< endl;
00073 }
00074 
00079 void Camera::loadCamera(istream & FILE) {
00080     int option_=0;
00081     string s;
00082     // Suche Cameramarke
00083     while (! FILE.fail() ) {
00084         FILE >> s;
00085         if(s == "!!BEGINCAMERA!!")
00086             break;
00087     }
00088     if(s != "!!BEGINCAMERA!!")
00089         Message::msg(Message::ERROR, "BEGIN CAMERA MARK NOT FOUND!");
00090 
00091     // lade Eigenschaften der Camera aus Datei
00092     while (! FILE.fail() ) {
00093         FILE >> option_ >> s;
00094         switch(option_) {
00095         case 0:
00096             FILE >> place;
00097             break;
00098         case 1:
00099             FILE >>  upper;
00100             break;
00101         case 2:
00102             FILE >>  projectionCenter;
00103             break;
00104         case 3:
00105             FILE >>  aperture;
00106             break;
00107         case 4:
00108             int slots;
00109             FILE >> slots;
00110             if(slots > CAMERA_SAVES)
00111                 slots = CAMERA_SAVES;
00112             for(int i=0; i < slots; i++)
00113                 FILE >> positionList[i].place >> positionList[i].upper
00114                 >> positionList[i].projectionCenter;
00115             break;
00116             // alle Optionen wurden eingelesen !
00117         case 1000:
00118             return;
00119             break;
00120         default:
00121             cerr << "WARNING read unknown option " << option_ << " " << s
00122             << endl;
00123         }
00124     }
00125 }
00126 
00132 void Camera::setAllToViewObject(Object * obj) {
00133     if (obj == 0) {
00134         cout <<
00135         "WARNING void Camera::setAllToViewObject(object * obj) object = 0 !!!"
00136         << endl;
00137         return;
00138     }
00139     Vector viewDirection;
00140 
00141     Vector pos_=obj->getScaledPos();
00142     double r_ = obj->getScaledRadius();
00143     place = pos_ * (pos_.length()+r_*5)/pos_.length();
00144     if(pos_.length() == 0)
00145         place = Vector(5,0,0)*r_;
00146     projectionCenter = Vector(0,0,0);
00147     viewDirection = place*-1;
00148     viewDirection.normalise();
00149     upper = viewDirection.crossProduct(Vector(1,1,0));
00150     if(upper.length()==0)
00151         upper=Vector(0,1,0);
00152 }
00153 
00157 void Camera::reloadAutomaticAdaption() {
00158     if(ourConfiguration.autoAdaption) {
00159         // gut funktionierende heuristische Formel
00160         ourConfiguration.radiusAdaption =
00161             (int)floor(pow(getProjectionDistance() /
00162                            ourConfiguration.linearScaleFactor,0.4) )-5;
00163         if(ourConfiguration.radiusAdaption <= 0)
00164             ourConfiguration.radiusAdaption=1;
00165     }
00166 }
00167 
00172 void Camera::zoom(double multiplier) {
00173     Vector viewDirection= place-projectionCenter;
00174     Vector newv_=viewDirection * multiplier;
00175     if( ourConfiguration.clickedObject == 0 || multiplier >= 1.0 ||
00176             newv_.length() >=
00177              ourConfiguration.clickedObject->getScaledRadius() *2 ) {
00178         //auf keinen fall zu nah an das objekt heranzoomen !
00179         place = projectionCenter+newv_;
00180         reloadAutomaticAdaption();
00181     }
00182 }
00183 
00189 void Camera::moveForward(double multiplier) {
00190     Vector viewDirection= projectionCenter - place;
00191     viewDirection.normalise();
00192     Vector movement = viewDirection * multiplier * getProjectionDistance();
00193     place+=movement;
00194     projectionCenter += movement;
00195 }
00196 
00202 void Camera::move(double unten, double right) {
00203     if(unten != 0) {
00204         Vector movement =upper * unten * getProjectionDistance() * 0.1;
00205         place +=movement;
00206         projectionCenter +=movement;
00207     }
00208     if(right !=0) {
00209         Vector movement = getViewDirectionOrthogonal() * right *
00210                           getProjectionDistance()* 0.1;
00211         place +=movement;
00212         projectionCenter += movement;
00213     }
00214 }
00215 
00222 void Camera::zoomTo(Vector & pos_,double distance_) {
00223     //beibehalten von ViewDirection !
00224     Vector viewDirection= projectionCenter - place;
00225     Vector distanceVector_=viewDirection*distance_ / viewDirection.length();
00226     place = pos_ - distanceVector_;
00227     projectionCenter = pos_;
00228     reloadAutomaticAdaption();
00229 }
00230 
00236 void Camera::changeLinearScaleFactor(double multiplier) {
00237     projectionCenter *=multiplier;
00238     place *=multiplier;
00239     reloadAutomaticAdaption();
00240 }
00241 
00247 void Camera::rotateKeepPosition(int ix, int iy) {
00248     //bitte immer nur in einer komponente drehen !!
00249     if(ix != 0 && iy != 0) {
00250         rotateKeepPosition(0,iy);
00251         iy = 0;
00252     }
00253 
00254     Vector axis_;
00255     Vector viewDirection= projectionCenter - place;
00256     double viewDirectionLength = viewDirection.length();
00257     viewDirection.normalise();
00258     if(iy != 0)
00259         axis_ =upper * iy;
00260     if(ix !=0)
00261         axis_= viewDirection.crossProduct(upper)*ix;
00262 
00263     axis_.normalise();
00264 
00265     double theta_ = 1.0 / 180.0 * PI;
00266     double sin_ = sin(theta_);
00267     double cos_ = cos(theta_);
00268     double oneMinCos_ = 1-cos_;
00269     Vector tmp_=viewDirection;
00270 
00271     viewDirection.x = (axis_.x*axis_.x+cos_ * (1-axis_.x*axis_.x)) * tmp_.x +
00272                       (oneMinCos_*axis_.x*axis_.y+sin_*axis_.z) * tmp_.y +
00273                       (-sin_*axis_.y+oneMinCos_*axis_.x*axis_.z) * tmp_.z ;
00274 
00275     viewDirection.y = (oneMinCos_*axis_.x*axis_.y-sin_*axis_.z) * tmp_.x +
00276                       (axis_.y*axis_.y+cos_*(1-axis_.y*axis_.y)) * tmp_.y +
00277                        (sin_ *
00278                               axis_.x+oneMinCos_ * axis_.y *axis_.z) * tmp_.z;
00279 
00280     viewDirection.z = (sin_ *axis_.y + oneMinCos_ *axis_.x*axis_.z) * tmp_.x +
00281                       (-sin_*axis_.x+oneMinCos_*axis_.y*axis_.z) * tmp_.y +
00282                       (axis_.z*axis_.z+cos_*(1-axis_.z*axis_.z)) * tmp_.z;
00283 
00284     viewDirection.normalise();
00285 
00286     tmp_=upper;
00287     upper.x = (axis_.x*axis_.x+cos_ * (1-axis_.x*axis_.x)) * tmp_.x +
00288               (oneMinCos_*axis_.x*axis_.y+sin_*axis_.z) * tmp_.y +
00289               (-sin_*axis_.y+oneMinCos_*axis_.x*axis_.z) * tmp_.z ;
00290 
00291     upper.y = (oneMinCos_*axis_.x*axis_.y-sin_*axis_.z) * tmp_.x +
00292               (axis_.y*axis_.y+cos_*(1-axis_.y*axis_.y)) * tmp_.y + (sin_ *
00293                       axis_.x+oneMinCos_ * axis_.y *axis_.z) * tmp_.z;
00294 
00295     upper.z = (sin_ *axis_.y + oneMinCos_ *axis_.x*axis_.z) * tmp_.x +
00296               (-sin_*axis_.x+oneMinCos_*axis_.y*axis_.z) * tmp_.y +
00297               (axis_.z*axis_.z+cos_*(1-axis_.z*axis_.z)) * tmp_.z;
00298 
00299     upper.normalise();
00300 
00301     projectionCenter = viewDirection*viewDirectionLength + place;
00302 }
00303 
00304 
00311 void Camera::rotateKeepViewPoint(int ix,int iy, int iz) {
00312     //bitte immer nur in 1 komponente drehen !!
00313     Vector axis_;
00314     double theta_ = 1.0 / 180.0 * PI;
00315     Vector viewDirection= projectionCenter - place;
00316     viewDirection.normalise();
00317     if(iy != 0)
00318         axis_ =upper * iy;
00319     else if(ix !=0)
00320         axis_= viewDirection.crossProduct(upper)*ix;
00321     else if (iz != 0) {
00322         upper += viewDirection.crossProduct(upper) * theta_ * iz;
00323         upper.normalise();
00324         return;
00325     } else
00326         return;
00327     axis_.normalise();
00328 
00329     double sin_ = sin(theta_);
00330     double cos_ = cos(theta_);
00331     double oneMinCos_ = 1-cos_;
00332 
00333     Vector tmp_=upper;
00334     upper.x = (axis_.x*axis_.x+cos_ * (1-axis_.x*axis_.x)) * tmp_.x +
00335               (oneMinCos_*axis_.x*axis_.y+sin_*axis_.z) * tmp_.y +
00336               (-sin_*axis_.y+oneMinCos_*axis_.x*axis_.z) * tmp_.z ;
00337 
00338     upper.y = (oneMinCos_*axis_.x*axis_.y-sin_*axis_.z) * tmp_.x +
00339               (axis_.y*axis_.y+cos_*(1-axis_.y*axis_.y)) * tmp_.y + (sin_ *
00340                       axis_.x+oneMinCos_ * axis_.y *axis_.z) * tmp_.z;
00341 
00342     upper.z = (sin_ *axis_.y + oneMinCos_ *axis_.x*axis_.z) * tmp_.x +
00343               (-sin_*axis_.x+oneMinCos_*axis_.y*axis_.z) * tmp_.y +
00344               (axis_.z*axis_.z+cos_*(1-axis_.z*axis_.z)) * tmp_.z;
00345 
00346     upper.normalise();
00347 
00348     tmp_=place-projectionCenter;
00349     place.x = (axis_.x*axis_.x+cos_ * (1-axis_.x*axis_.x)) * tmp_.x +
00350               (oneMinCos_*axis_.x*axis_.y+sin_*axis_.z) * tmp_.y +
00351               (-sin_*axis_.y+oneMinCos_*axis_.x*axis_.z) * tmp_.z ;
00352 
00353     place.y = (oneMinCos_*axis_.x*axis_.y-sin_*axis_.z) * tmp_.x +
00354               (axis_.y*axis_.y+cos_*(1-axis_.y*axis_.y)) * tmp_.y + (sin_ *
00355                       axis_.x+oneMinCos_ * axis_.y *axis_.z) * tmp_.z;
00356 
00357     place.z = (sin_ *axis_.y + oneMinCos_ *axis_.x*axis_.z) * tmp_.x +
00358               (-sin_*axis_.x+oneMinCos_*axis_.y*axis_.z) * tmp_.y +
00359               (axis_.z*axis_.z+cos_*(1-axis_.z*axis_.z)) * tmp_.z;
00360 
00361     place +=projectionCenter;
00362 }
00363 
00367 void Camera::home() {
00368 
00369     // 45 Grad Oeffnungswinkel der Kamera
00370     aperture = 45;
00371 
00372     /* place = "View position"
00373     viewDirection = "View direction Vector"
00374     upper = "View up direction"
00375      projectionCenter = "Point to rotate about", um diesen Punkt wird gedreht; er
00376         ist immer gleichzeitig auch das Projektionszentrum */
00377 
00378     // Die Kamera im Punkt (0, 0, 1000) platzieren.
00379     place.x = 0.0;
00380     place.y = 0.0;
00381     place.z = 1000;
00382 
00383     // Projektionszentrum ist der Ursprung
00384     projectionCenter.x = 0.0;
00385     projectionCenter.y = 0.0;
00386     projectionCenter.z = 0.0;
00387 
00388     // "Oben" ist die positive Richtung der y-Achse.
00389     upper.x = 0.0;
00390     upper.y = 1.0;
00391     upper.z = 0.0;
00392 
00393     upper.normalise();
00394     reloadAutomaticAdaption();
00395 }

Erzeugt am Mon May 30 14:31:15 2005 für Sunsystembuildingandsimulation von doxygen 1.3.6