sources:
cgapplication.cpp (3.8k)
cgapplication.h (3.7k)
cgscooter.cpp (8.6k)
cgscooter.h (973 bytes)


website:
more info here


screenshot:
studies/grafik/Computergrafik-Code6/cgscooter.cpp
download file

  1 //
  2 // Computergraphik I
  3 // Prof. Dr. Juergen Doellner
  4 // Sommersemester 2001
  5 //
  6 // Rahmenprogramm fuer Aufgabenzettel 6
  7 //
  8
  9 #include "cgscooter.h"
 10
 11 CGScooter::CGScooter() {
 12     rotate_ = true;
 13     run_ = false;
 14     axis_ = true;
 15 }
 16
 17 CGScooter::~CGScooter() {   
 18 }
 19 void CGScooter::drawBox() {   
 20     glutSolidCube(1);
 21 }
 22
 23 void CGScooter::drawSphere() {
 24     static GLUquadricObj* q = gluNewQuadric();   
 25     gluSphere(q,1.0,10,10);   
 26 }
 27
 28 void CGScooter::drawTorus(double innerRadius, double outerRadius) {
 29     static GLUquadricObj* q = gluNewQuadric();   
 30     glutSolidTorus(outerRadius-innerRadius, outerRadius, 18, 20);
 31 }
 32
 33 void CGScooter::drawCylinder(bool caps) {
 34     static GLUquadricObj* q = gluNewQuadric();
 35     static GLUquadricObj* d = gluNewQuadric();
 36        
 37     gluCylinder(q,0.5,0.5,1,8,1);
 38     if (caps) {
 39         glPushMatrix();
 40         gluDisk(d,0,0.5,8,1);
 41         glTranslated(0,0,1);   
 42         gluDisk(d,0,0.5,8,1);
 43         glPopMatrix();
 44     }
 45 }
 46
 47 void CGScooter::drawWheel() {   
 48     // ein Rad besteht aus Nabe, Speichen, Felge und Bereifung
 49     // Durchmesser des Rades: 1
 50     // Breite (Reifen): 0.25
 51     // Ausrichtung in der xy-Ebene, zentriert im Ursprung
 52     glPushMatrix();
 53
 54     // Nabe (Durchmesser 0.4, Breite 0.5+0.5)
 55     glPushMatrix();
 56     glColor3f(0.9, 0.9, 0.9);
 57
 58     glScalef(0.4, 0.4, 1);
 59     glTranslatef(0, 0, -0.5);
 60     drawCylinder(true);
 61
 62     glPopMatrix();
 63
 64     // Speichen (jeweils um den Mittelpunkt)
 65     glPushMatrix();
 66     glColor3f(0.9, 0.9, 0.9);
 67
 68     static double angle = 0;
 69     if (rotate_)
 70         angle += 1.0;
 71     if (angle > 360)
 72         angle -= 360;
 73     glRotatef(angle, 0, 0, 1);
 74    
 75     const int nSpeichen = 10;
 76     for (int i=0; i<nSpeichen; i++)
 77     {
 78         glPushMatrix();
 79         glRotatef(i*360/nSpeichen, 0, 0, 1);
 80         // in die xy-Ebene bringen
 81         glRotatef(90, 1, 0, 0);
 82         // Durchmesser verkleinern
 83         glScalef(0.1, 0.1, 0.75);
 84        
 85         drawCylinder(false);
 86         glPopMatrix();
 87     }
 88     glPopMatrix();
 89
 90     // Felge
 91     drawTorus(0.7, 0.8);
 92
 93     // Reifen
 94     glColor3f(0.1, 0.1, 0.1);
 95     drawTorus(0.75, 1);
 96
 97     glPopMatrix();
 98 }
 99
100 void CGScooter::drawSteering()
101 {
102     // Vorderrad ist im Ursprung zentriert
103     // Gesamthöhe der Lenkerkonstruktion: 1
104
105     glColor3f(0.5, 0.8, 0.8);
106
107     // linke Lenkerstange
108     glPushMatrix();
109     glTranslatef(0, 0, 0.07);
110     glScalef(0.05, 0.9, 0.05);
111     // auf y-Achse
112     glRotatef(270, 1, 0, 0);
113     drawCylinder(true);
114     glPopMatrix();
115
116     // rechte Lenkerstange
117     glPushMatrix();
118     glTranslatef(0, 0, -0.07);
119     glScalef(0.05, 0.9, 0.05);
120     // auf y-Achse
121     glRotatef(270, 1, 0, 0);
122     drawCylinder(false);
123     glPopMatrix();
124
125     // obere Querstange
126     glPushMatrix();
127     glColor3f(1, 0.5, 0.5);
128     // auf Lenkerstangen auflegen
129     glTranslatef(0, 0.9, 0);
130     // etwas schmaler
131     glScalef(0.075, 0.075, 0.5);
132     // Zylinder in Mittelpunkt zentrieren
133     glTranslatef(0, 0, -0.5);
134     drawCylinder(true);
135     glPopMatrix();
136
137     // mittlere Querstange
138     glPushMatrix();
139     glColor3f(0.5, 0.8, 0.8);
140     // etwa halbe Höhe des Lenkers
141     glTranslatef(0, 0.6, 0);
142     // etwas schmaler
143     glScalef(0.05, 0.05, 0.15);
144     // Zylinder in Mittelpunkt zentrieren
145     glTranslatef(0, 0, -0.5);
146     drawCylinder(false);
147     glPopMatrix();
148
149     // untere Querstange
150     glPushMatrix();
151     // knapp über dem Vorderrad
152     glTranslatef(0, 0.3, 0);
153     // etwas schmaler
154     glScalef(0.05, 0.05, 0.15);
155     // Zylinder im Mittelpunkt zentrieren
156     glTranslatef(0, 0, -0.5);
157     glColor3f(0.5, 0.8, 0.8);
158     drawCylinder(false);
159     glPopMatrix();
160
161     // Lampe
162     glPushMatrix();
163     // an der unteren Querstange
164     glTranslatef(-0.025, 0.3, 0);
165     glRotatef(90, 0, 1, 0);
166     glScalef(0.1, 0.1, 0.075);
167     // Zylinder im Mittelpunkt zentrieren
168     glTranslatef(0, 0, -0.5);
169     glColor3f(1,1,1);
170     drawCylinder(true);
171     glPopMatrix();
172
173     // Vorderrad
174     glPushMatrix();
175     glScalef(0.2, 0.2, 0.2);
176     drawWheel();
177     glPopMatrix();
178 }
179
180 void CGScooter::drawScooter()
181 {
182     glPushMatrix();
183     // rotiere Lenker, wenn nötig
184     static float angle = 0.0;
185     static float angle_inc = 0.5;
186     if (rotate_)
187     {
188         angle += angle_inc;
189
190         if (abs(angle) > 45)
191             angle_inc = -angle_inc;
192     }
193     glRotatef(angle, 0, 1, 0);
194     // zeichne Lenker
195     drawSteering();
196     glPopMatrix();
197
198     // Hinterrad
199     glPushMatrix();
200     glTranslatef(0.8, 0, 0);
201     glScalef(0.2, 0.2, 0.2);
202     drawWheel();
203     glPopMatrix();
204
205     // Trittbrett
206     glColor3f(0.5, 0.8, 0.8);
207     glPushMatrix();
208     glTranslatef(0.4, 0, 0);
209     glScalef(0.25, 0.05, 0.2);
210     // eigentliches Brett
211     drawBox();
212     glPopMatrix();
213
214     // Streben zur Hinterachse
215     glPushMatrix();
216     glTranslatef(0.5, 0, -0.07);
217     glScalef(0.25, 0.05, 0.05);
218     // auf x-Achse
219     glRotatef(90, 0, 1, 0);
220     drawCylinder(true);
221     glPopMatrix();
222
223     glPushMatrix();
224     glTranslatef(0.5, 0, +0.07);
225     glScalef(0.3, 0.05, 0.05);
226     // auf x-Achse
227     glRotatef(90, 0, 1, 0);
228     drawCylinder(true);
229     glPopMatrix();
230
231     // Strebe zum Lenker
232     glPushMatrix();
233     glTranslatef(0, 0.6, 0);
234     // schräg zum Lenker hoch
235     glRotatef(62, 0, 0, -1);
236     // auf x-Achse
237     glRotatef(90, 0, 1, 0);
238     glScalef(0.1, 0.05, 0.7);
239     drawCylinder(true);
240     glPopMatrix();
241
242     // rotes Katzenlicht am Hinterrad
243     glPushMatrix();
244     glColor3f(1, 0, 0);
245     glBegin(GL_TRIANGLES);
246     glVertex3f(0.84, -0.05, 0.1);
247     glVertex3f(0.84, -0.05, 0.05);
248     glVertex3f(0.84,  0.05,0.075);
249     glEnd();
250     glPopMatrix();
251 }
252
253 void CGScooter::onInit() {
254     // OpenGL Lichtquelle
255     static GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};  /* diffuse light. */
256     static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */   
257     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
258     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
259     glEnable(GL_LIGHTING);
260     glEnable(GL_LIGHT0);
261    
262     // automatische Normalisierung
263     glEnable(GL_NORMALIZE);
264    
265     glEnable(GL_COLOR_MATERIAL);
266     glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
267     // Tiefen Test aktivieren
268     glEnable(GL_DEPTH_TEST);
269    
270     // Smooth Schattierung aktivieren
271     glShadeModel(GL_SMOOTH);
272
273     // Projection
274     glMatrixMode(GL_PROJECTION);
275     gluPerspective(60.0,1.0,0.2,20.0);   
276
277     // LookAt
278     glMatrixMode(GL_MODELVIEW);
279 // gluLookAt(0.0, 0.0, 4.0, // from (0,0,4)
280     gluLookAt(0.0, 1.0, 3.0,  // from (1,1,3)
281               0.0, 0.0, 0.0,  // to (0,0,0)
282               0.0, 1.0, 0.0)
; // up
283    
284     glClearColor(1,1,1,1);
285 }
286
287 void CGScooter::onSize(unsigned int newWidth,unsigned int newHeight) {     
288     if((newWidth > 0) && (newHeight > 0))
289     {
290         // Passe den OpenGL-Viewport an die neue Fenstergroesse an:
291         glViewport(0, 0, newWidth - 1, newHeight - 1);
292
293         // Passe die OpenGL-Projektionsmatrix an die neue
294         // Fenstergroesse an:
295         glMatrixMode(GL_PROJECTION);
296         glLoadIdentity();
297         gluPerspective(40.0,float(newWidth)/float(newHeight),1.0, 10.0);       
298
299         // Schalte zurueck auf die Modelview-Matrix
300         glMatrixMode(GL_MODELVIEW);   
301     }
302 }
303
304 void CGScooter::onKey(unsigned char key) {
305     switch (key) {
306     case 27 :
307     case 'q': exit(0);
308               break;
309
310     case 'l': glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break;
311     case 'f': glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break;
312
313     case 'c': glEnable(GL_CULL_FACE); break;
314     case 'n': glDisable(GL_CULL_FACE); break;
315
316     case ' ': run_ = !run_; break;
317     case 'a': axis_ = !axis_; break;
318
319     case 'r': rotate_ = !rotate_; break;
320     }
321
322     glutPostRedisplay();
323 }
324
325 void CGScooter::onIdle()
326     if (run_)
327         glRotatef(1,0,1,0);
328    
329     glutPostRedisplay();
330 }
331
332 void CGScooter::onDrag(double dx, double dy)
333 {
334     glRotatef(dx*20, 0, 1, 0);
335 // glRotatef(-dy*20, 1, 0, 0);
336 }
337
338 void CGScooter::onDraw() {
339     // Loesche den Farb- und Tiefenspeicher
340     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);               
341  
342     // draw coordinate system
343     if (axis_) {
344         glBegin(GL_LINES);
345         // x
346         glColor3d(1.0,0,0);
347         glVertex3d(0.0,0,0);
348         glVertex3d(3.0,0,0);
349
350         // y
351         glColor3d(0,1,0);
352         glVertex3d(0,0,0);
353         glVertex3d(0,3,0);
354
355         // z
356         glColor3d(0,0,1);
357         glVertex3d(0,0,0);
358         glVertex3d(0,0,3);
359         glEnd();
360     }
361    
362     // Matrixmodus setzen
363     glMatrixMode(GL_MODELVIEW);       
364
365     // hier den Scooter zeichnen
366     glPushMatrix();     
367     glTranslatef(-0.5, 0, 0);
368     drawScooter();
369     glPopMatrix();
370
371     // Plattform
372     glPushMatrix();
373     glColor3f(0.5, 0.8, 0.5);
374     glTranslatef(0, -0.3, 0);
375     glScalef(2.5, 0.1, 2);
376     drawBox();
377     glPopMatrix();
378
379     // Nicht vergessen! Front- und Back-Buffer tauschen:
380     swapBuffers();
381 }
382
383 // Hauptprogramm
384 int main(int argc, char* argv[]) {
385     // Erzeuge eine Instanz der Beispiel-Anwendung:
386     CGScooter sample;
387    
388     // Starte die Beispiel-Anwendung:
389     sample.start("CGScooter, Stephan Brumme, 702544");
390     return(0);
391 }
392