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

Object.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 
00011 #include "Object.h"
00012 #include "Window.h"
00013 #include "TextureManager.h"
00014 #include "Opengl.h"
00015 #include "Configuration.h"
00016 
00017 //Zum Verzeichnis einlesen...
00018 #include <sys/types.h>
00019 #include <dirent.h>
00020 
00021 //Klassenvariable initialisieren und Speicherplatz reservieren:
00022 Object::classWindowEditAttributes * Object::editAttributes=0;
00023 
00028 double Object::getRadiusModified(double r) {
00029     //Radius reduzieren und linear skalieren
00030     return pow(r, 1.0/sqrt(double(ourConfiguration.radiusAdaption))) *
00031            sqr(ourConfiguration.radiusAdaption) *
00032            ourConfiguration.linearScaleFactor;
00033 }
00034 
00039 Vector Object::getDistanceModified(Vector pos) {
00040     //gegenwaertig nur linear skalieren
00041     return pos * ( ourConfiguration.linearScaleFactor );
00042 }
00043 
00047 Vector Object::getScaledPos() {
00048     return getDistanceModified(pos);
00049 }
00050 
00054 Vector Object::getScaledV() {
00055     return getDistanceModified(v);
00056 }
00057 
00062 double Object::getScaledRadius() {
00063     return getRadiusModified(radius);
00064 }
00065 
00070 void Object::deleteAdditionalInformation(void * additionalInformation) {}
00071 
00076 void Object::setName(const string & s) {
00077     //kopiere den String in das char array.
00078     const char * newstring = s.c_str();
00079     if(s.length() == 0)
00080         newstring= getType();
00081     strncpy(name,newstring,MAX_NAME_LENGTH);
00082     name[MAX_NAME_LENGTH] = 0;
00083 }
00084 
00089 void Object::load(istream & FILE) {
00090     int option_=0;
00091     string s;
00092     //Suche Anfang der Objektbeschreibung:
00093     while (! FILE.fail() ) {
00094         FILE >> s;
00095         if(s == "!!BEGINBASISOBJECT!!")
00096             break;
00097     }
00098     if(s != "!!BEGINBASISOBJECT!!")
00099         cerr << "WARNING OBJECT MARK NOT FOUND!" << endl;
00100 
00101     while (! FILE.fail() ) {
00102         //Lies Attributnummer ein und name (wird nicht beachtet)
00103         FILE >> option_ >> s;
00104         switch(option_) {
00105         case 0:
00106             FILE >> radius;
00107             break;
00108         case 1:
00109             FILE >>  mass;
00110             break;
00111         case 2:
00112             FILE >>  v;
00113             break;
00114         case 3:
00115             FILE >>  pos;
00116             break;
00117         case 4:
00118             FILE >> s;
00119             setName(s);
00120             break;
00121         case 5:
00122             FILE >> s;
00123             setTexture(s);
00124             break;
00125         case 1000: //alle Optionen wurden eingelesen !
00126             return;
00127         default:
00128             cerr << "WARNING read unknown option " << option_ << " "
00129             << s << endl;
00130         }
00131     }
00132 }
00133 
00138 void Object::save(ostream & FILE) {
00139     //Speichere alle Attribute in die Datei:
00140     FILE << getType() <<  endl;
00141     FILE << "!!BEGINBASISOBJECT!!" << endl;
00142     FILE << "0 radius: " << radius <<  endl;
00143     FILE << "1 mass: " << mass <<  endl;
00144     FILE << "2 v: " << v <<  endl;
00145     FILE << "3 pos: " << pos <<  endl;
00146     FILE << "4 name: " << getName() << endl;
00147     FILE << "5 texture: " << getTexture() << endl;
00148     FILE << "1000 !!ENDBASISOBJECT!!" << endl;
00149 }
00150 
00151 
00152 void Object::drawEnd() {
00153 
00154     if( ( ourConfiguration.constructMode ||  ourConfiguration.viewVectors)
00155             && v.length() != 0) {
00156         // Zeichne den Geschwindigkeitsvektor
00157         // Farbe des Vektors ist Pink (wenn lichteffekte aus sind)
00158         // und sonst weiss.
00159         glColor3f(1, 0, 1);
00160 
00161         Vector posS = getScaledPos();
00162         Vector vstart_(posS.x,posS.y,posS.z);
00163         Vector v=getScaledV();
00164 
00165         //Endpunkt des Vektors ermitteln
00166         Vector vend_=vstart_+ getDistanceModified(v *
00167                      ourConfiguration.getTime()) + v.getNormalised()*
00168                      getScaledRadius();
00169 
00170         //Zeichnen des Vektors:
00171         glBegin(GL_LINES);
00172         glVertex3f(vstart_.x,vstart_.y,vstart_.z);
00173         glVertex3f(vend_.x,vend_.y,vend_.z);
00174         glEnd();
00175     }
00176     //Name des Objekts entfernen.
00177     glPopName();
00178 }
00179 
00183 Object::Object():pos(0,0,0),v(0,0,0),mass(1),radius(1) {
00184     texture[0]=0;
00185 }
00186 
00191 void Object::setTexture(const string & s) {
00192     // wenn neue Textur gleich der alten Textur ist, ist nichts zu tun
00193     if(s == texture)
00194         return;
00195 
00196     //Alte Textur freigeben
00197     if(texture[0] != 0 )
00198         ourTextureManager.freeReference(textureNum);
00199 
00200     strncpy(texture,s.c_str(),MAX_TEXTURE_LENGTH);
00201     texture[MAX_TEXTURE_LENGTH] = 0;
00202 
00203     //Textur und pfadnahmen ermitteln:
00204     string texturePlusPath ="textures/";
00205 
00206     //Objekttyp (Sonne, etc.) ist Bestandteil des Pfades
00207     texturePlusPath += getType();
00208     texturePlusPath += "/";
00209     texturePlusPath += texture;
00210     texturePlusPath += ".png";
00211     textureNum = ourTextureManager.getTextureNumber(texturePlusPath);
00212 }
00213 
00214 void Object::FillTextureSelector() {
00215     GLUI_Listbox * fileSelector= editAttributes->textureSelector;
00216 
00217     //Texturpfad besteht auch aus objekttyp:
00218     string d="textures/";
00219     d+= editAttributes->objectType;
00220 
00221     //oeffnen des Verzeichnisses:
00222     DIR * dir=opendir(d.c_str());
00223     if(dir == 0 ) {
00224         cerr << "WARNUNG: Directory " << d << " does not exist!" << endl;
00225         return;
00226     }
00227     dirent * dentry;
00228     int textureCount=0;
00229     while ((dentry=readdir(dir))) {
00230         if(strlen(dentry->d_name) <= MAX_TEXTURE_LENGTH+4) {
00231             //Alle vorhandenen texturen im Pfad einlesen und in liste anfuegen:
00232             string s(dentry->d_name);
00233             uint pos = s.find(".png");
00234 
00235             if( pos != string::npos ) { //Nur png texturen koennen geladen werden
00236                 dentry->d_name[pos] = 0; // erweiterung .png abschneiden !
00237                 //in liste einfuegen:
00238                 fileSelector->add_item(textureCount++, dentry->d_name );
00239             }
00240         } else
00241             //Dateinamen duerfen nicht zu lange sein
00242             cerr << "WARNING "<< d  << "/" << dentry->d_name << " name is "
00243             << dentry->d_reclen << " long => longer than "
00244             << MAX_TEXTURE_LENGTH << " chars" << endl;
00245     }
00246     closedir(dir);
00247 }
00248 
00252 void Object::closeAttributeWindow() {
00253     if(editAttributes == 0)
00254         return;
00255 
00256     //Speichern der objekt einstellungen
00257     editAttributes->my->saveAttributes();
00258 
00259     //attribut fenster schliessen
00260     editAttributes->attributeWindow->close();
00261     if(editAttributes->additionalInformation != 0)
00262         //Fenster destruktur des tatsaechlichen Objekts aufrufen, damit zusatz
00263         // information freigegeben werden kann
00264         editAttributes->my->deleteAdditionalInformation(
00265             editAttributes->additionalInformation);
00266     //freigabe des speichers fuer die fuers Fenster benoetigte Informationen
00267     delete(editAttributes);
00268     //Anzeigen das das Fenster geschlossen ist:
00269     editAttributes = 0;
00270 }
00271 
00272 void Object::loadAttributes() {
00273     //Namen fuers Fenster kopieren:
00274     strcpy(editAttributes->name,name);
00275 
00276     //Texturauswahlliste auf aktuelle Textur festlegen:
00277     GLUI_Listbox * ts = editAttributes->textureSelector;
00278     GLUI_Listbox_Item * it = ts->get_item_ptr(texture);
00279     if(it == NULL) {
00280         cerr << "WARNING: texture " << texture
00281         << " does not exist for object " <<
00282         editAttributes->objectType << " !" << endl;
00283     } else
00284         ts->do_selection(it->id);
00285 
00286 }
00287 
00288 void Object::saveAttributes() {
00289     //Falls sich der Name geandert hat diesen speichern und
00290     //die Objektauswahlliste anpassen:
00291     if( strcmp(editAttributes->name, getName())) {
00292         setName(editAttributes->name);
00293         Window::reloadObjectSelector();
00294     }
00295 
00296     //Speichern der Textur
00297     setTexture((char *)editAttributes->textureSelector->curr_text);
00298 }
00299 
00303 void Object::createAttributeWindow() {
00304     if(editAttributes != 0 && editAttributes->my == this )
00305         return;
00306 
00307     const string & objectType = getType();
00308 
00309     //Altes Fenster schliessen sofern dieses geoeffnet ist
00310     Object::closeAttributeWindow();
00311     editAttributes = new classWindowEditAttributes;
00312 
00313     //Fensterposition festlegen:
00314     //glutInitWindowPosition(0,0);
00315     //Fenster erzeugen:
00316     editAttributes->attributeWindow = GLUI_Master.create_glui("Attributes");
00317 
00318     editAttributes->my = this;
00319     editAttributes->additionalInformation = 0;
00320     GLUI * aw= editAttributes->attributeWindow;
00321     editAttributes->objectType = objectType;
00322 
00323     //Kontrollen erzeugen
00324     aw->add_button( "Close",CANCEL,(GLUI_Update_CB)
00325                     Object::AttributeMenuCallback);
00326     aw->add_separator();
00327 
00328 
00329     aw->add_edittext("Name", GLUI_EDITTEXT_TEXT,& editAttributes->name);
00330     GLUI_Panel * pPos=aw->add_panel("pos");
00331     GLUI_Panel * pV=aw->add_panel("speed v");
00332 
00333     GLUI_Spinner * stmp= aw->add_spinner_to_panel(pPos,"x",
00334                          GLUI_EDITTEXT_DOUBLE, & this->pos.x);
00335     //Breite setzen:
00336     stmp->set_w(200);
00337     stmp=aw->add_spinner_to_panel(pPos,"y", GLUI_EDITTEXT_DOUBLE, & this->pos.y);
00338     stmp->set_w(200);
00339     stmp=aw->add_spinner_to_panel(pPos,"z", GLUI_EDITTEXT_DOUBLE, & this->pos.z);
00340     stmp->set_w(200);
00341 
00342     stmp=aw->add_spinner_to_panel(pV,"x", GLUI_EDITTEXT_DOUBLE, & this->v.x);
00343     stmp->set_w(200);
00344     stmp=aw->add_spinner_to_panel(pV,"y", GLUI_EDITTEXT_DOUBLE, & this->v.y);
00345     stmp->set_w(200);
00346     stmp=aw->add_spinner_to_panel(pV,"z", GLUI_EDITTEXT_DOUBLE, & this->v.z);
00347     stmp->set_w(200);
00348 
00349     stmp=aw->add_spinner("mass", GLUI_EDITTEXT_DOUBLE, & this->mass);
00350     stmp->set_w(200);
00351     stmp= aw->add_spinner("radius", GLUI_EDITTEXT_DOUBLE,& this->radius);
00352     stmp->set_double_limits(1e-100,1e15);
00353     stmp->set_w(200);
00354 
00355     //Textur Selector erzeugen und fuellen:
00356     editAttributes->textureSelector = aw->add_listbox("Texture",NULL ,TEXTURE,
00357                                       (GLUI_Update_CB)
00358                                       Object::AttributeMenuCallback);
00359     FillTextureSelector();
00360 
00361     //Hauptfenster aktualisieren wenn eine Live Variable geandert wird:
00362     aw->set_main_gfx_window(Window::getMainWindow());
00363 
00364     //Attributswerte laden
00365     refreshAttributes();
00366 }
00367 
00368 void Object::AttributeMenuCallback(int b) {
00369     if(Object::editAttributes == 0) {
00370         //Hierher sollten wir niemals kommen !
00371         cerr << "FEHLER Object::editAttributes uninitialisiert";
00372         return;
00373     }
00374 
00375     //Welche Kontrolle loesste die Callbackfunktion aus ?
00376     switch(b) {
00377     case(TEXTURE): {
00378             //Speichern des Texturnamens und neuzeichen:
00379             editAttributes->my->saveAttributes();
00380             Opengl::redraw();
00381             break;
00382         }
00383     case(CANCEL): {
00384             //Attributfenster schlie�n
00385             closeAttributeWindow();
00386             break;
00387         }
00388     default:
00389         cerr << "UNKNOWN KEY PRESSED " << b << " AttributeMenuCallback" << endl;
00390     }
00391 }
00392 
00396 GLuint Object::getOpenglTextureNumber() {
00397     return ourTextureManager.getOpenglTexture(textureNum);
00398 }
00399 
00400 void Object::drawBegin() {
00401     // Opengl soll gleichzeitig Zeiger auf Objekt zurueckgeben wenn Objekt
00402     // angeklickt
00403     // Verwende die speicherposition als Namen:
00404     glPushName(reinterpret_cast<uint>(this));
00405 }

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