sources:
cgapplication.cpp (3.2k)
cgapplication.h (3.5k)
cglinestyle.cpp (5.7k)
cglinestyle.h (873 bytes)
vector.cpp (1.4k)
vector.h (4.9k)


binaries:
Release/cglinestyle.exe (28.0k)
Release/triceratops.txt (433.6k)


website:
more info here


screenshot:
studies/grafik2/Computergrafik-Code3/Aufgabe6/cglinestyle.cpp
download file

  1 //
  2 // Computergraphik II
  3 // Prof. Dr. Juergen Doellner
  4 // Wintersemester 2001/02
  5 //
  6 // Rahmenprogramm zu Aufgabenzettel 2
  7 //
  8
  9 #include "cglinestyle.h"
 10 #include "vector.h"
 11 #include <fstream.h>
 12 #include <stdlib.h>
 13
 14 CGLinestyle::CGLinestyle() {
 15     run_ = false;
 16     zoom_ = 0.15;
 17     culling_ = false;
 18     lighting_ = true;
 19     mode_ = SEGMENTED;
 20     haloewidth_ = 5.0;
 21     segmentwidth_ = 1.0;
 22 }
 23
 24 CGLinestyle::~CGLinestyle( ) {   
 25 }
 26
 27 void CGLinestyle::drawScene() {   
 28
 29     glPushMatrix();
 30     glScaled(zoom_, zoom_, zoom_);
 31
 32     static GLuint cache = 0;
 33     if (cache == 0) {
 34         cache = glGenLists(1);       
 35         glNewList(cache, GL_COMPILE_AND_EXECUTE);       
 36         glMatrixMode(GL_MODELVIEW);
 37         glPushMatrix();
 38
 39         //ifstream s("small.txt");
 40         //ifstream s("stegarosaurus.txt");
 41         ifstream s("triceratops.txt")
 42         char buf[100];               
 43
 44         glBegin(GL_TRIANGLES);
 45         Vector v[3];               
 46         while (!s.eof()) {
 47             s >> buf;
 48            
 49             while (s.good()) {           
 50                 s >> v[0] >> v[1] >> v[2];
 51                 Vector n = (v[1]-v[0])*(v[2]-v[0]);
 52                 glNormal3dv(n.rep());
 53                 glVertex3dv(v[0].rep());
 54                 glVertex3dv(v[1].rep());
 55                 glVertex3dv(v[2].rep());
 56             }
 57             if (s.rdstate() & ios::failbit) {
 58                 s.clear(s.rdstate() & ~ios::failbit);                                         
 59             }
 60         }
 61         glEnd();   
 62         glPopMatrix();
 63         glEndList();
 64     } else {                       
 65         glCallList(cache);   
 66     }   
 67
 68     glPopMatrix();
 69 }
 70
 71 void CGLinestyle::onInit() {
 72     // OpenGL Lichtquelle
 73     static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};  /* diffuse light. */
 74     static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */   
 75     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
 76     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
 77     glEnable(GL_LIGHTING);
 78     glEnable(GL_LIGHT0);
 79    
 80     // automatische Normalisierung
 81     glEnable(GL_NORMALIZE);
 82    
 83     glEnable(GL_COLOR_MATERIAL);
 84     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
 85
 86     // Tiefen Test aktivieren
 87     glEnable(GL_DEPTH_TEST);
 88    
 89     // Smooth Schattierung aktivieren
 90     glShadeModel(GL_SMOOTH);
 91    
 92     // Projection
 93     glMatrixMode(GL_PROJECTION);
 94     gluPerspective(60.0,1.0,2,2000.0);   
 95    
 96     // LookAt
 97     glMatrixMode(GL_MODELVIEW);
 98     gluLookAt(0.0, 0.0, 4.0,  // from (0,0,4)
 99         0.0, 0.0, 0.0,  // to (0,0,0)
100         0.0, 1.0, 0.)
// up
101    
102     glClearColor(0.0,0.0,0.0,1.0);
103 }
104
105 void CGLinestyle::onSize(unsigned int newWidth,unsigned int newHeight) {         
106     width_ = newWidth;
107     height_ = newHeight;           
108     glMatrixMode(GL_PROJECTION);
109     glViewport(0, 0, width_ - 1, height_ - 1);       
110     glLoadIdentity();
111     gluPerspective(40.0,float(width_)/float(height_),2.0, 100.0);
112     glMatrixMode(GL_MODELVIEW);     
113 }
114
115 void CGLinestyle::onKey(unsigned char key) {
116     switch (key) {
117     case 27: { exit(0); break; }                       
118     case '+': { zoom_*= 1.1; break; }
119     case '-': { zoom_*= 0.9; break; }
120     case ' ': { run_ = !run_; break; }
121     case 'c': { culling_ = !culling_; break; }
122     case 'l': { lighting_ = !lighting_; break; }
123     case 's': { mode_ = SEGMENTED; break; }
124     case 'h': { mode_ = HALOED; break; }
125     case '1': { haloewidth_*= 0.9; break; }
126     case '2': { haloewidth_*= 1.1; break; }
127     case '3': { segmentwidth_*= 0.9; break; }
128     case '4': { segmentwidth_*= 1.1; break; }
129     }
130     onDraw();
131 }
132
133 void CGLinestyle::onIdle()
134     if (run_) {
135         glRotatef(1,0.1,1,0.2);         
136         onDraw();
137     }
138 }
139
140 void CGLinestyle::onDraw() {
141     // clear buffers
142     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
143     glDepthMask(GL_TRUE);
144     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);               
145
146
147     if (culling_) glEnable(GL_CULL_FACE);
148     if (!lighting_) glDisable(GL_LIGHTING);
149
150
151     if (mode_ == SEGMENTED)
152     {
153         // SEGMENTED
154
155         // important ! default is GL_LESS
156         glDepthFunc(GL_LESS);
157
158         // wireframe
159         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
160
161         glLineWidth(segmentwidth_);
162         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
163
164         drawScene();
165
166         // filled
167         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
168
169         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
170
171         drawScene();
172     }
173     else
174     {
175         // HALOED
176
177         // important ! default is GL_LESS
178         glDepthFunc(GL_LEQUAL);
179    
180         // wireframe I
181         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
182
183         glLineWidth(haloewidth_);
184         glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
185
186         drawScene();
187
188         // wireframe II
189         glLineWidth(1);
190         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
191
192         drawScene();
193     }
194
195     // clean up
196     glEnable(GL_LIGHTING);
197     glDisable(GL_CULL_FACE);
198
199     // Front- und Back-Buffer tauschen:
200     swapBuffers();
201 }
202
203 // Hauptprogramm
204 int main(int argc, char* argv[]) {
205     // Erzeuge eine Instanz der Beispiel-Anwendung:
206     CGLinestyle sample;
207
208     cout << "Tastenbelegung:" << endl
209          << "ESC Programm beenden" << endl
210          << "Leertaste Objekt drehen" << endl
211          << "+ in die Szene hineinzoomen" << endl
212          << "- aus der Szene herauszoomen" << endl
213          << "c Culling de-/aktivieren" << endl
214          << "l Beleuchtung de-/aktivieren" << endl
215          << "s segmented surfaces" << endl
216          << "h haloed wires" << endl
217          << "1 halo verringern" << endl
218          << "2 halo verstärken" << endl
219          << "3 Abstand der Segmente verringern" << endl
220          << "4 Abstand der Segmente vergrößern" << endl;
221
222     // Starte die Beispiel-Anwendung:
223     sample.start("Stephan Brumme, 702544", true, 550, 550);
224     return(0);
225 }
226