sources:


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code3/Aufgabe7/cginterpolation.cpp
download file

  1 //
  2 // Computergraphik II
  3 // Prof. Dr. Juergen Doellner
  4 // Wintersemester 2001/02
  5 //
  6 // Rahmenprogramm zu Aufgabenzettel 3
  7 //
  8
  9 #include "cginterpolation.h"
 10 #include "vector.h"
 11 #include <fstream.h>
 12 #include <stdlib.h>
 13
 14 CGInterpolation::CGInterpolation() {
 15     run_ = false;
 16     zoom_ = 0.15;
 17     culling_ = false;
 18
 19     max_ = 100;                    // maximale Laenge des Arrays
 20     counter_ = 0;                  // Anzahl der Vertices
 21     list_ = new Vector[max_];      // Array mit Vertices
 22     ref_ = Vector(8.0, -1.0, 1.0); // Referenzpunkt
 23
 24     loadGeometry();                // Import der Geometriedaten
 25 }
 26
 27 CGInterpolation::~CGInterpolation() {
 28 }
 29
 30 void CGInterpolation::drawScene() {
 31
 32     glPushMatrix();
 33     glScaled(zoom_, zoom_, zoom_);
 34
 35     static GLuint cache = 0;
 36     if (cache == 0) {
 37         cache = glGenLists(1); // Display-Liste
 38         glNewList(cache, GL_COMPILE_AND_EXECUTE); // Display-Liste
 39
 40         glColor3f(0.5, 0.4, 0.4);
 41         drawObject(ref_);
 42
 43         glMatrixMode(GL_MODELVIEW);
 44         glPushMatrix();
 45         glTranslatef(ref_[0],ref_[1],ref_[2]);
 46    
 47         glColor3f(0.9, 0.1, 0.2);
 48         glutSolidSphere(0.1,16,16);
 49         glPopMatrix();
 50
 51         glEndList(); // Display-Liste
 52     } else {                       
 53         glCallList(cache); // Display-Liste
 54     }   
 55
 56     glPopMatrix();
 57 }
 58
 59 void CGInterpolation::drawObject(const Vector& ref) {
 60     // draw geometry
 61
 62     // normieren Sie Ihre Abstandsfunktion
 63     double dMaxDistance = 0;
 64     double dMinDistance = 999999999999;
 65     for(unsigned int i=0; i<counter_; i++)
 66     {
 67         double* pVector  = list_[i].rep();
 68         double dDistance = sqrt(((pVector[0] - ref_[0]) * (pVector[0] - ref_[0])) +
 69                                 ((pVector[1] - ref_[1]) * (pVector[1] - ref_[1])) +
 70                                 ((pVector[2] - ref_[2]) * (pVector[2] - ref_[2])))
;
 71
 72         if (dDistance > dMaxDistance)
 73             dMaxDistance = dDistance;
 74         if (dDistance < dMinDistance)
 75             dMinDistance = dDistance;
 76     }
 77
 78
 79     // Zeichnen Sie die Dreiecke mit Farbwerten an den Vertices.
 80     glBegin(GL_TRIANGLES);
 81     for(i=0; i<counter_; i++) {
 82
 83         double* pVector  = list_[i].rep();
 84         double dDistance = sqrt(((pVector[0] - ref_[0]) * (pVector[0] - ref_[0])) +
 85                                 ((pVector[1] - ref_[1]) * (pVector[1] - ref_[1])) +
 86                                 ((pVector[2] - ref_[2]) * (pVector[2] - ref_[2])))
;
 87         double dNormalized = (dDistance - dMinDistance) / (dMaxDistance - dMinDistance);
 88
 89         glColor3d(dNormalized, dNormalized, dNormalized);
 90         glVertex3dv(list_[i].rep());
 91
 92     }
 93     glEnd();
 94
 95 }
 96
 97 void CGInterpolation::loadGeometry() {
 98     //ifstream s("small.txt");
 99     ifstream s("stegarosaurus.txt");
100     //ifstream s("triceratops.txt");
101    
102     char buf[100];               
103
104     while (!s.eof()) {
105         s >> buf;
106        
107         while (s.good()) {           
108             Vector v0;       
109             Vector v1;       
110             Vector v2;       
111             s >> v0 >> v1 >> v2;
112             if(counter_+3 >= max_) {
113                 max_*=2;
114                 Vector* newList = new Vector[max_];
115                 for(unsigned int i=0; i<counter_; i++) {
116                     newList[i] = list_[i];
117                 }
118                 delete list_;
119                 list_ = newList;
120             }
121             list_[counter_] = v0; counter_++;
122             list_[counter_] = v1; counter_++;
123             list_[counter_] = v2; counter_++;
124         }
125         if (s.rdstate() & ios::failbit) {
126             s.clear(s.rdstate() & ~ios::failbit);                                         
127         }
128     }
129 }
130
131 void CGInterpolation::onInit() {
132    
133     // automatische Normalisierung
134     glEnable(GL_NORMALIZE);
135    
136     // Tiefen Test aktivieren
137     glEnable(GL_DEPTH_TEST);
138    
139     // Smooth Schattierung aktivieren
140     glShadeModel(GL_SMOOTH);
141    
142     // Projection
143     glMatrixMode(GL_PROJECTION);
144     gluPerspective(60.0,1.0,2,100.0);   
145    
146     // LookAt
147     glMatrixMode(GL_MODELVIEW);
148     gluLookAt(
149         0.0, 0.0, 4.0,  // from (0,0,4)
150         0.0, 0.0, 0.0,  // to (0,0,0)
151         0.0, 1.0, 0.)
// up
152    
153     glClearColor(0.9,0.9,0.9,1.0);
154 }
155
156 void CGInterpolation::onSize(unsigned int newWidth,unsigned int newHeight) {         
157     width_ = newWidth;
158     height_ = newHeight;           
159     glMatrixMode(GL_PROJECTION);
160     glViewport(0, 0, width_ - 1, height_ - 1);       
161     glLoadIdentity();
162     gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0);
163     glMatrixMode(GL_MODELVIEW);     
164 }
165
166 void CGInterpolation::onKey(unsigned char key) {
167     switch (key) {
168     case 27: { exit(0); break; }                       
169     case '+': { zoom_*= 1.1; break; }
170     case '-': { zoom_*= 0.9; break; }
171     case ' ': { run_ = !run_; break; }
172     case 'c': { culling_ = !culling_; break; }
173     }
174     onDraw();
175 }
176
177 void CGInterpolation::onIdle()
178     if (run_) {
179         glRotatef(1, 0.0, 1.0, 0.0);         
180         onDraw();
181     }
182 }
183
184 void CGInterpolation::onDraw() {
185     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);               
186
187     if (culling_) glEnable(GL_CULL_FACE);
188
189     drawScene();
190
191     glDisable(GL_CULL_FACE);
192
193     // Nicht vergessen! Front- und Back-Buffer tauschen:
194     swapBuffers();
195 }
196
197 // Hauptprogramm
198 int main(int argc, char* argv[]) {
199     // Erzeuge eine Instanz der Beispiel-Anwendung:
200     CGInterpolation sample;
201    
202     cout << "Tastenbelegung:" << endl          << "ESC Programm beenden" << endl          << "Leertaste Objekt drehen" << endl          << "+ in die Szene hineinzoomen" << endl          << "- aus der Szene herauszoomen" << endl          << "c Culling de-/aktivieren" << endl;
203
204    // Starte die Beispiel-Anwendung:
205     sample.start("Stephan Brumme, 702544", true, 512, 512);
206     return(0);
207 }
208